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

56
LINES

< > BotCompany Repo | #1007039 // Capture Audio By Button Press & Recognize With CMU Sphinx [WORKS!]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 39910K of libraries. Click here for Pure Java version (4540L/31K/109K).

1  
!7
2  
3  
static JButton button;
4  
static volatile Capturer cap;
5  
static File wavFile;
6  
static Q recognizerQueue;
7  
static StreamSpeechRecognizer2 recognizer;
8  
9  
p-substance {
10  
  showControls(jCenteredLine(button = jbutton("Record!", f recordOrStop)));
11  
  recognizerQueue = new Q("Recognizer Queue", true);
12  
  recognizerQueue.add(r {
13  
    infoBox("Loading engine...");
14  
    recognizer = new StreamSpeechRecognizer2(makeSphinxConfig());
15  
    infoBox("Engine loaded!");
16  
  });
17  
}
18  
19  
svoid recordOrStop {
20  
  if (cap != null) {
21  
    cap.stop();
22  
    byte[] pcm = cap.getPCM();
23  
    infoMessage("Got " + l(pcm) + " bytes PCM!");
24  
    pcmToWAVE(pcm, cap.format(), wavFile);
25  
    infoBox("Wrote " + f2s(wavFile) + " (" + wavFile.length() + " bytes)");
26  
    cap = null;
27  
    setText(button, "Record!");
28  
    recognizeAudio(wavFile);
29  
  } else {
30  
    AudioLine line = first(allTargetDataLines());
31  
    assertNotNull("Need audio input line!", line);
32  
    DataLine dataLine = cast line.open();
33  
    print("Capturing");
34  
    wavFile = prepareProgramFile(ymd_minus_hms() + ".wav");
35  
    cap = captureAudioFromLine2(dataLine, sphinxAudioFormat());
36  
    setText(button, "Stop");
37  
  }
38  
}
39  
40  
svoid recognizeAudio(final File wavFile) {
41  
  recognizerQueue.add(r {
42  
    loading {
43  
      recognizer.startRecognition(new FileInputStream(wavFile));
44  
      SpeechResult result;
45  
      print("Recognizing audio...");
46  
      S line = null;
47  
      while ((result = recognizer.getResult()) != null) {
48  
        line = result.getHypothesis();
49  
        print("  Hypothesis: " + line);
50  
      }
51  
      print("Done recognizing");
52  
      infoBox("You said: " + line);
53  
      recognizer.stopRecognition();
54  
    }
55  
  });
56  
}

Author comment

Began life as a copy of #1007035

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1007039
Snippet name: Capture Audio By Button Press & Recognize With CMU Sphinx [WORKS!]
Eternal ID of this version: #1007039/5
Text MD5: df04b413c9e50bf670d288f53e23a0a5
Transpilation MD5: 62b70171589fbe00df185daa88560e13
Author: stefan
Category: javax / audio
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-02-26 00:31:20
Source code size: 1681 bytes / 56 lines
Pitched / IR pitched: No / No
Views / Downloads: 400 / 501
Version history: 4 change(s)
Referenced in: [show references]