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

95
LINES

< > BotCompany Repo | #1020224 // "Alexa Mode" [always listen to speech, OK]

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

Uses 206K of libraries. Click here for Pure Java version (15898L/114K).

1  
!7
2  
3  
module ListenOnNoise > DynPrintLogAndEnabled {
4  
  double minLevel = 20; // percent
5  
  double maxPause = 1; // pause allowed while we keep recording, seconds
6  
  double minDuration = 0.5; // min seconds of complete recording to submit
7  
  double maxDuration = 10; // always stop after this many seconds
8  
  double extendAfter = 0.5; // seconds to add at the end
9  
  
10  
  transient int interval = 100;
11  
  transient long noiseSince, lastNoise;
12  
  transient L<short[]> buffer = synchroList();
13  
  transient bool talking;
14  
  transient TrayIcon externalTrayIcon;
15  
  
16  
  start {
17  
    onChangeAndNow(r {
18  
      if (enabled && externalTrayIcon == null)
19  
        externalTrayIcon = installTrayIcon(#1101460, "Alexa Mode (continously listening)", r { dm_activateOSAndModule(module()) },
20  
          "Stop Listening", r stopListening);
21  
      else if (!enabled && externalTrayIcon != null) {
22  
        removeTrayIcon(externalTrayIcon);
23  
        externalTrayIcon = null;
24  
      }
25  
    });
26  
  
27  
    dm_vmBus_onMessage('talking, r { talking = true });
28  
    dm_vmBus_onMessage('doneTalking, r { talking = false });
29  
    
30  
    dm_addAudioListener(voidfunc(short[] data) {
31  
      if (noiseSince == 0) clear(buffer);
32  
      buffer.add(cloneShortArray(data));
33  
    });
34  
    
35  
    dm_onTopInput(voidfunc(S s) {
36  
      if "stop listening|voice of" stopListening()
37  
    });
38  
 
39  
    doEvery(interval, r {
40  
      if (noiseSince != 0 && elapsedSeconds(noiseSince) >= maxDuration-0.05) {
41  
        // too long, discard, maybe it's constant noise
42  
        printWithSeconds("Recording too long (" + maxDuration + " s), discarding");
43  
        noiseSince = 0;
44  
        clear(buffer);
45  
      }
46  
      
47  
      float volume = dm_inputVolumeLevel();
48  
      //print("Volume: " + volume);
49  
      if (volume >= minLevel && !talking) {
50  
        // got noise
51  
        lastNoise = sysNow();
52  
        if (noiseSince == 0)
53  
          noiseSince = lastNoise;
54  
      } else if (noiseSince != 0) { // got no noise, but am recording
55  
        if (elapsedSeconds(lastNoise) > maxPause-0.05) {
56  
          double duration = toSeconds(lastNoise-noiseSince+toMS(extendAfter));
57  
          if (duration >= minDuration-0.05) {
58  
            printWithSeconds((enabled ? "Keeping" : "Would keep") + " recording of " + formatDouble(duration, 1) + " seconds");
59  
            if (enabled) {
60  
              L<short[]> chunks = cloneList(buffer);
61  
              
62  
              // drop last silent chunks
63  
              int len = lengthLevel2_shortArrays(chunks);
64  
              int wantedLengthInSamples = iround(duration*44100*2);
65  
              int dropped = 0;
66  
              while (len-l(last(chunks)) >= wantedLengthInSamples) {
67  
                len -= l(last(chunks));
68  
                popLast(chunks);
69  
                ++dropped;
70  
              }
71  
              //print("Dropped " + n2(dropped, "chunk"));
72  
              
73  
              File wavFile = anonymousWAVEFile();
74  
              multiShortArraysToWAVE(chunks, wavFile);
75  
              //printFileInfo(wavFile);
76  
              vmBus_send('newRecording, wavFile);
77  
            }
78  
          } else
79  
            printWithSeconds("Discarding recording of " + formatDouble(duration, 1) + " seconds");
80  
          noiseSince = 0;
81  
          clear(buffer);
82  
        }
83  
      }
84  
    });
85  
  }
86  
  
87  
  void cleanMeUp {
88  
    removeTrayIcon(externalTrayIcon);
89  
  }
90  
  
91  
  void stopListening enter {
92  
    if (setField(enabled := false))
93  
      blipInNewThread();
94  
  }
95  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 8 computer(s): bhatertpkbcr, cfunsshuasjs, gwrvuhgaqvyk, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1020224
Snippet name: "Alexa Mode" [always listen to speech, OK]
Eternal ID of this version: #1020224/35
Text MD5: 9397adaca5bd892975c943abc831e07b
Transpilation MD5: 8db2957bc85f5958aa7bb93e43652edf
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: 2018-12-12 23:30:51
Source code size: 3453 bytes / 95 lines
Pitched / IR pitched: No / No
Views / Downloads: 430 / 935
Version history: 34 change(s)
Referenced in: [show references]