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).

!7

cmodule ListenOnNoise > DynPrintLogAndEnabled {
  switchable double threshold = 50;  // volume percentage
  switchable double minDurationAfterNoise = 1.5; // seconds
  switchable double maxDuration = 10.0; // seconds
  switchable bool verbose;

  transient float lastVolume;
  transient long lastNoise; // in sysNow time
  
  JPanel makeControlArea() {
    ret centerAndEastWithMargin(
      centerAndEastWithMargin(
        dm_calculatedLabel(() -> "Threshold: " + formatDouble(threshold, 1) + "%"),
        jPopDownButton_noText("Set threshold...", r { dm_doubleFieldSetterDialog threshold() })),
      super.makeControlArea());
  }
  
  start {
    dm_watchField enabled(r {
      if (!enabled)
        dm_call(dm_quickAudioRecordModuleOpt(), 'stopRecording);
    });
    dm_addAudioListener(voidfunc(short[] data) enter {
      if (!enabled) ret;
      if (verbose) printWithTime("Got audio data");
      
      // Quickly calculate the volume
      float vol = shortSamplesToPercentVolume(data);
      
      short[] clonedBuf = vol >= threshold && lastVolume < threshold
        ? cloneShortArray(data) : null;

      dm_q(r {
        S recorder = dm_quickAudioRecordModuleOpt();
        long recordingSince = cast dm_call(recorder, 'recordingSince);
        bool noise = vol >= threshold;
        bool isRecording = recordingSince != 0;
        if (noise) lastNoise = sysNow();
        if (isRecording) {
          // stop if max duration reached or no recent noise
          if (elapsedSeconds(recordingSince) >= maxDuration
            || elapsedSeconds(lastNoise) > minDurationAfterNoise) {
            print("Stopping");
            dm_call(recorder, 'stopRecording);
          }
        } else {
          // start recording
          if (noise) {
            sleep(10); // avoid audio listener race condition
            print("Starting");
            dm_call(recorder, 'startRecording, clonedBuf); 
          }
        }
      });
    });
  }
}

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: 292 / 13643
Version history: 18 change(s)
Referenced in: [show references]