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

86
LINES

< > BotCompany Repo | #1027038 // Speech Correlator, simple version (one recording at a time)

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 911K of libraries. Click here for Pure Java version (6366L/29K).

1  
!7
2  
3  
cmodule SpeechCorrelator1 > DynPrintLog {
4  
  switchable bool react; // react to new recording
5  
  long audioReceived; // timestamp
6  
  S startTime /*, endTime*/;
7  
  switchable int correlationTimeout = 100;
8  
  switchable S actionWhenCorrelated; // however we interpret this
9  
  
10  
  new L<Interpretation> interpretations_raw;
11  
  transient L<Interpretation> interpretations;
12  
  //Interpretation bestInterpretation;
13  
  
14  
  transient new RestartableCountdown countdown;
15  
16  
  srecord Interpretation(S text, S info, long received) {}
17  
  
18  
  start {
19  
    ownResource(countdown);
20  
    interpretations = dm_synchroList(interpretations_raw);
21  
    dm_registerAs_direct speechCorrelator();
22  
    dm_vmBus_answerToMessage dontApplyRecognizedSpeech(() -> react);
23  
    dm_onNewRecording(voidfunc(File f) enter {
24  
      if (!react) ret;
25  
      print("Have WAV: " + f);
26  
      //print(renderFileDateWithSeconds(f));
27  
      setStartTime(extractYMDminusHMS(fileName(f)));
28  
29  
      triggerSpeechRecognitions("wav", f);
30  
    });
31  
32  
    dm_vmBus_onMessage_q newMP3Recording(voidfunc(File f) enter {
33  
      print("Have MP3: " + f);
34  
      setStartTime(extractYMDminusHMS(fileName(f)));
35  
      triggerSpeechRecognitions("mp3", f);
36  
    });
37  
38  
    dm_vmBus_onMessage_q speechRecognized_raw(voidfunc(Map map) enter{
39  
      // module := module(), +info, +text, audioFile := f)
40  
      File f = cast map.get("audioFile");
41  
      S date = f == null ? ymdMinusHms() : extractYMDminusHMS(fileName(f));
42  
      if (f == null) setStartTime(date); // e.g. for Chrome recognizer
43  
      if (!eq(date, startTime)) ret with print("Ignoring speech result for time " + date);
44  
      Interpretation i;
45  
      interpretations.add(i = nu Interpretation(
46  
        text := map.get("text"),
47  
        info := map.get("info"),
48  
        received := now()-audioReceived
49  
      ));
50  
      printWithMillis("New interpretation (total: " + l(interpretations) + "): " + i);
51  
    });
52  
  }
53  
  
54  
  visual centerAndSouthWithMargins(super,
55  
    westCenterAndEastWithMargin(
56  
      withLabel("Correlation timeout:",
57  
        jMinWidth(75, dm_textIntField correlationTimeout())),
58  
        dm_textFieldWithLabel actionWhenCorrelated(),
59  
        dm_checkBox react()));
60  
61  
  // unused. type = "mp3" or "wav"
62  
  void triggerSpeechRecognitions(S type, File audioFile) {
63  
  }
64  
65  
  void setStartTime(S startTime) {
66  
    if (setField(+startTime)) {
67  
      print(+startTime);
68  
      setField(audioReceived := now());
69  
      clear(interpretations);
70  
      // TODO: Fire existing one?
71  
      countdown.start(correlationTimeout, r fireCorrelation);
72  
    }
73  
  }
74  
  
75  
  swappable Interpretation chooseInterpretation() {
76  
    ret first(interpretations);
77  
  }
78  
  
79  
  void fireCorrelation enter {
80  
    Interpretation i = chooseInterpretation();
81  
    if (i != null) {
82  
      printWithMillis("Speech correlated: " + i.text);
83  
      vmBus_send speechCorrelated(module(), startTime, i.text);
84  
    }
85  
  }
86  
}

Author comment

Began life as a copy of #1026982

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: #1027038
Snippet name: Speech Correlator, simple version (one recording at a time)
Eternal ID of this version: #1027038/28
Text MD5: ddff74d14afd942bb277fdd152d17640
Transpilation MD5: db5b57f1fdd81d1cbf9a08a697be40ad
Author: stefan
Category: javax / speech recognition
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-03-03 13:00:56
Source code size: 2922 bytes / 86 lines
Pitched / IR pitched: No / No
Views / Downloads: 340 / 4784
Version history: 27 change(s)
Referenced in: [show references]