Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

44
LINES

< > BotCompany Repo | #1026973 // StreamingWitAIUploader

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (6236L/42K).

1  
sclass StreamingWitAIUploader {
2  
  S mimeType = "audio/mpeg3";
3  
  S language = "english";
4  
  URL url;
5  
  URLConnection con;
6  
  OutputStream out;
7  
  long bytesSent;
8  
9  
  void start ctex {
10  
    print("Initiating wit.ai call");
11  
    
12  
    S token = witAITokenForLanguage(language);
13  
    url = new URL("https://api.wit.ai/speech?v=20160526");
14  
    con = url.openConnection();
15  
    con.setRequestProperty("Authorization", "Bearer " + token);
16  
    con.setRequestProperty("Content-Type", mimeType);
17  
    con.setDoOutput(true);
18  
    out = con.getOutputStream();
19  
  }
20  
  
21  
  void sendData(byte[] data) ctex {
22  
    out.write(data);
23  
    bytesSent += l(data);
24  
  }
25  
  
26  
  S get() ctex {
27  
    if (out == null) null;
28  
    printWithMilliseconds("Sent " + nBytes(bytesSent) + " to wit.ai");
29  
    out.flush();
30  
    S json = loadPage(con, url);
31  
    S text = getString(jsonDecodeMap(json), "_text");
32  
    printWithMilliseconds("wit.ai responded: " + text);
33  
    if (eq(text, "hallo")) {
34  
      text = "";
35  
      print("WIT.AI WORKAROUND");
36  
    }
37  
    dispose out;
38  
    ret text;
39  
  }
40  
  
41  
  void cleanMeUp {
42  
    dispose out;
43  
  }
44  
}

Author comment

Began life as a copy of #1017980

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1026973
Snippet name: StreamingWitAIUploader
Eternal ID of this version: #1026973/11
Text MD5: ab8e3ff2d296a6fe75dffafa27c5cf5a
Transpilation MD5: 265dac5cb296c8dfaec7368791ba7c88
Author: stefan
Category: javax / speech recognition
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-15 14:12:54
Source code size: 1117 bytes / 44 lines
Pitched / IR pitched: No / No
Views / Downloads: 319 / 708
Version history: 10 change(s)
Referenced in: [show references]