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

55
LINES

< > BotCompany Repo | #1027053 // Audio Input From Phone [OK]

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

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

!7

import javax.sound.sampled.*;

cmodule AudioInputFromPhone > DynPrintLog {
  switchable int sampleRate = 16000;
  switchable int channels = 1;
  switchable int bufSize = 8192;
  switchable bool sendOutClonedData; // makes things easier for clients
  switchable bool verbose;
  transient AudioInputDispatcher 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 {
    loop = new AudioInputDispatcher(bufSize);
    loop.audioListeners = audioListeners;
    loop.lvVolume = lvVolume;
    loop.verbose = verbose;
    dm_watchFieldAndNow sendOutClonedData(() -> { loop.cloneDataOnce = sendOutClonedData; });
    dm_vmBus_onMessage oic_gotLine(voidfunc(O module, S line, O socket, O state) enter {
      if (startsWith(line, "audio: ")) {
        //print("Have audio line");
        byte[] data = base64decode(substring(line, l("audio: ")));
        dm_q(r {
          loop.handleData(data);
        });
      }
    });
    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 #1017138

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: #1027053
Snippet name: Audio Input From Phone [OK]
Eternal ID of this version: #1027053/8
Text MD5: d77ee3e2da7d4ef4b00fe0dd2bc58940
Transpilation MD5: 704e3073a14fb2c035b518dbf59b06f0
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:31:39
Source code size: 2093 bytes / 55 lines
Pitched / IR pitched: No / No
Views / Downloads: 316 / 522
Version history: 7 change(s)
Referenced in: [show references]