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

43
LINES

< > BotCompany Repo | #1017138 // Audio Input [Dyn Module]

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

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

!7

cmodule AudioInput > DynPrintLog {
  switchable int sampleRate = 44100;
  switchable int channels = 2;
  switchable int bufSize = 40960;
  switchable bool sendOutClonedData; // makes things easier for clients
  switchable bool verbose;
  transient recordToAudioListeners_AudioLoop loop;
  transient SimpleLiveValue<Float> lvVolume = floatLiveValue(); // in percent
  transient L<VF1<short[]>> audioListeners = synchroList();
  
  visual centerAndSouthWithMargins(super.visualize(),
    jCenteredLine(jLiveValueLabel(mapLiveValue(func(float volume) -> S { "Volume: " + iround(volume) + " %" }, S, lvVolume))));

  start {
    ownTimer(loop = recordToAudioListeners(audioListeners, bufSize, lvVolume, getAudioFormat()));
    loop.verbose = verbose;
    dm_watchFieldAndNow sendOutClonedData(() -> { loop.cloneDataOnce = sendOutClonedData; });
    loop.start();
    dm_registerAs audioInputModule();
    JComponent trayIcon = jMinWidth(16, jVerticalGreenBar(lvVolume, max := 100));
    componentPopupMenuItem(trayIcon, "Audio Input", r { dm_showModule(module()) });
    dm_setTrayIcon(trayIcon);
  }
  
  // hold audio listeners during reload
  
  O _getReloadData() { ret audioListeners; }
  void _setReloadData(L<VF1<short[]>> listeners) { addAll(audioListeners, listeners); }

  // API
  
  void addAudioListener(VF1<short[]> l) { audioListeners.add(l); }
  void removeAudioListener(VF1<short[]> l) { audioListeners.remove(l); }
  
  AudioFormat getAudioFormat() { ret new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sampleRate, 16, channels, channels*2, sampleRate, false); }
  
  float getVolume() { ret lvVolume!; }
  
  // for debugging
  O currentListener() { ret loop.currentListener; }
}

Author comment

Began life as a copy of #1016699

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1017138
Snippet name: Audio Input [Dyn Module]
Eternal ID of this version: #1017138/53
Text MD5: 6e66c9579001afd3562dfab4eb559dfc
Transpilation MD5: b9e307ca49ddd67ca71145c68ccee410
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 13:43:21
Source code size: 1741 bytes / 43 lines
Pitched / IR pitched: No / No
Views / Downloads: 628 / 135815
Version history: 52 change(s)
Referenced in: #1017156 - dm_audioInputModule
#1018253 - Audio Input + Output [dev., Dyn Module, TODO: sync-start]
#1026861 - Audio Input with other sample rate / channels [dev.]
#1026862 - Audio Input [Dyn Module, backup before sample rate field]
#1027053 - Audio Input From Phone [OK]