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

1  
!7
2  
3  
cmodule QuickAudioRecord > DynPrintLog {
4  
  transient volatile L<short[]> recording;
5  
  transient long recordingSince; // in sysNow time
6  
  transient JButton btn;
7  
  transient bool stop;
8  
  transient JLabel trayIcon;
9  
  transient File wavFile;
10  
  //transient short[] lastChunk; // for Record On Noise
11  
  switchable bool normalizeRecordings;
12  
13  
  visualize {
14  
    btn = jImageAndTextButton(#1101400, "Record [hold button down]");
15  
    onMouseDownUp(btn, r startRecording, r stopRecording);
16  
    ret northAndCenterWithMargins(btn, super.visualize());
17  
  }
18  
  
19  
  start {
20  
    preloadImage(#1101417);
21  
    componentFieldsToKeep = initHashSetAndAdd(componentFieldsToKeep, 'trayIcon);
22  
    
23  
    dm_addAudioListener(voidfunc(short[] data) { handleChunk(data) });
24  
    
25  
    if (trayIcon == null) {
26  
      trayIcon = jimage(#1101416);
27  
      onMouseDownUp(trayIcon, r startRecording, r stopRecording);
28  
      setToolTip(trayIcon, "Click & hold to record audio!");
29  
    }
30  
    dm_addTrayIcon(trayIcon);
31  
  }
32  
  
33  
  void handleChunk(short[] data) enter {
34  
    final L<short[]> l = recording;
35  
    if (l == null) ret;
36  
    short[] clonedData = cloneShortArray(data);
37  
    l.add(clonedData);
38  
    vmBus_send recordedChunk(clonedData);
39  
    
40  
    if (stop) {
41  
      stop = false;
42  
      recording = null;
43  
      recordingSince = 0;
44  
      vmBus_send doneRecording();
45  
      thread "Save WAV" {
46  
        temp enter();
47  
        print("Recording done. Got " + l(l) + " chunks");
48  
        if (normalizeRecordings)
49  
          print("Normalized recording: " + toIntPercent(normalizeShortArrays(l)) + "%");
50  
        multiShortArraysToWAVE(l, wavFile, format := dm_audioInputFormat());
51  
        printFileInfo(wavFile);
52  
        vmBus_send('newRecording, wavFile);
53  
      }
54  
    }
55  
  }
56  
  
57  
  // API
58  
  
59  
  void startRecording() {
60  
    startRecording(null);
61  
  }
62  
  
63  
  void startRecording(short[] initialChunk) {
64  
    if (isRecording()) ret;
65  
    setImage(trayIcon, #1101417);
66  
    stop = false;
67  
    print("Recording!");
68  
    wavFile = anonymousWAVEFile();
69  
    recordingSince = sysNow();
70  
    recording = synchroList(llNonNulls(initialChunk));
71  
    vmBus_send startedRecording(wavFile);
72  
    if (initialChunk != null) vmBus_send recordedChunk(initialChunk);
73  
    updatePrintLog();
74  
  }
75  
  
76  
  void stopRecording { setImage(trayIcon, #1101416); set stop; }
77  
  
78  
  bool isRecording() { ret recording != null; }
79  
  
80  
  long recordingSince() { ret recordingSince; }
81  
}

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: 505 / 132293
Version history: 34 change(s)
Referenced in: [show references]