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

59
LINES

< > BotCompany Repo | #1026863 // Record On Noise [e.g. for wit.ai recognition with #1019331]

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

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

1  
!7
2  
3  
cmodule ListenOnNoise > DynPrintLogAndEnabled {
4  
  switchable double threshold = 50;  // volume percentage
5  
  switchable double minDurationAfterNoise = 1.5; // seconds
6  
  switchable double maxDuration = 10.0; // seconds
7  
  switchable bool verbose;
8  
9  
  transient float lastVolume;
10  
  transient long lastNoise; // in sysNow time
11  
  
12  
  JPanel makeControlArea() {
13  
    ret centerAndEastWithMargin(
14  
      centerAndEastWithMargin(
15  
        dm_calculatedLabel(() -> "Threshold: " + formatDouble(threshold, 1) + "%"),
16  
        jPopDownButton_noText("Set threshold...", r { dm_doubleFieldSetterDialog threshold() })),
17  
      super.makeControlArea());
18  
  }
19  
  
20  
  start {
21  
    dm_watchField enabled(r {
22  
      if (!enabled)
23  
        dm_call(dm_quickAudioRecordModuleOpt(), 'stopRecording);
24  
    });
25  
    dm_addAudioListener(voidfunc(short[] data) enter {
26  
      if (!enabled) ret;
27  
      if (verbose) printWithTime("Got audio data");
28  
      
29  
      // Quickly calculate the volume
30  
      float vol = shortSamplesToPercentVolume(data);
31  
      
32  
      short[] clonedBuf = vol >= threshold && lastVolume < threshold
33  
        ? cloneShortArray(data) : null;
34  
35  
      dm_q(r {
36  
        S recorder = dm_quickAudioRecordModuleOpt();
37  
        long recordingSince = cast dm_call(recorder, 'recordingSince);
38  
        bool noise = vol >= threshold;
39  
        bool isRecording = recordingSince != 0;
40  
        if (noise) lastNoise = sysNow();
41  
        if (isRecording) {
42  
          // stop if max duration reached or no recent noise
43  
          if (elapsedSeconds(recordingSince) >= maxDuration
44  
            || elapsedSeconds(lastNoise) > minDurationAfterNoise) {
45  
            print("Stopping");
46  
            dm_call(recorder, 'stopRecording);
47  
          }
48  
        } else {
49  
          // start recording
50  
          if (noise) {
51  
            sleep(10); // avoid audio listener race condition
52  
            print("Starting");
53  
            dm_call(recorder, 'startRecording, clonedBuf); 
54  
          }
55  
        }
56  
      });
57  
    });
58  
  }
59  
}

Author comment

Began life as a copy of #1017159

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: #1026863
Snippet name: Record On Noise [e.g. for wit.ai recognition with #1019331]
Eternal ID of this version: #1026863/19
Text MD5: d0b876ae1715915506ec5e2b99b220ef
Transpilation MD5: eaa26b6f1b979f6471890d9ec1a05286
Author: stefan
Category: javax / audio
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-15 14:09:25
Source code size: 2020 bytes / 59 lines
Pitched / IR pitched: No / No
Views / Downloads: 219 / 13560
Version history: 18 change(s)
Referenced in: [show references]