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

81
LINES

< > BotCompany Repo | #1018602 // Quick Audio Record [while holding button]

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

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

!7

cmodule QuickAudioRecord > DynPrintLog {
  transient volatile L<short[]> recording;
  transient long recordingSince; // in sysNow time
  transient JButton btn;
  transient bool stop;
  transient JLabel trayIcon;
  transient File wavFile;
  //transient short[] lastChunk; // for Record On Noise
  switchable bool normalizeRecordings;

  visualize {
    btn = jImageAndTextButton(#1101400, "Record [hold button down]");
    onMouseDownUp(btn, r startRecording, r stopRecording);
    ret northAndCenterWithMargins(btn, super.visualize());
  }
  
  start {
    preloadImage(#1101417);
    componentFieldsToKeep = initHashSetAndAdd(componentFieldsToKeep, 'trayIcon);
    
    dm_addAudioListener(voidfunc(short[] data) { handleChunk(data) });
    
    if (trayIcon == null) {
      trayIcon = jimage(#1101416);
      onMouseDownUp(trayIcon, r startRecording, r stopRecording);
      setToolTip(trayIcon, "Click & hold to record audio!");
    }
    dm_addTrayIcon(trayIcon);
  }
  
  void handleChunk(short[] data) enter {
    final L<short[]> l = recording;
    if (l == null) ret;
    short[] clonedData = cloneShortArray(data);
    l.add(clonedData);
    vmBus_send recordedChunk(clonedData);
    
    if (stop) {
      stop = false;
      recording = null;
      recordingSince = 0;
      vmBus_send doneRecording();
      thread "Save WAV" {
        temp enter();
        print("Recording done. Got " + l(l) + " chunks");
        if (normalizeRecordings)
          print("Normalized recording: " + toIntPercent(normalizeShortArrays(l)) + "%");
        multiShortArraysToWAVE(l, wavFile, format := dm_audioInputFormat());
        printFileInfo(wavFile);
        vmBus_send('newRecording, wavFile);
      }
    }
  }
  
  // API
  
  void startRecording() {
    startRecording(null);
  }
  
  void startRecording(short[] initialChunk) {
    if (isRecording()) ret;
    setImage(trayIcon, #1101417);
    stop = false;
    print("Recording!");
    wavFile = anonymousWAVEFile();
    recordingSince = sysNow();
    recording = synchroList(llNonNulls(initialChunk));
    vmBus_send startedRecording(wavFile);
    if (initialChunk != null) vmBus_send recordedChunk(initialChunk);
    updatePrintLog();
  }
  
  void stopRecording { setImage(trayIcon, #1101416); set stop; }
  
  bool isRecording() { ret recording != null; }
  
  long recordingSince() { ret recordingSince; }
}

Author comment

Began life as a copy of #1017158

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, hpgrupgrauku, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1018602
Snippet name: Quick Audio Record [while holding button]
Eternal ID of this version: #1018602/35
Text MD5: 07fa96f678384068d9ea7939acc357f7
Transpilation MD5: da00601ace61769d164120e50fcc095b
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-07 00:02:42
Source code size: 2453 bytes / 81 lines
Pitched / IR pitched: No / No
Views / Downloads: 586 / 132492
Version history: 34 change(s)
Referenced in: [show references]