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

86
LINES

< > BotCompany Repo | #1027007 // Audio Output using aplay [dev.]

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

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

!7

import static x30_pkg.x30_util.VF1;
import javax.sound.sampled.AudioFormat;

cmodule APlayAudioOutput > DynPrintLog {
  switchable int bufSizeMS = 200;
  switchable int sampleRate = 44100;
  switchable int channels = 2; // only 2 is tested
  switchable int bits = 16;
  switchable float gain = 1;
  switchable bool registerService;
  
  transient L<VF1<double[]>> sources = notifyingList(r setNumSources);
  transient SimpleLiveValue<Int> numSources = new(Int, 0);
  transient StreamSoundToAPlay loop;
  transient int bufSizeInFrames;
  transient short[] buffer;

  visualize {
    ret centerAndSouth(super.visualize(),
      withLeftAndRightMargin(vstackWithSpacing(
        withLabel("Volume (0-200%):", liveSliderZeroToOne(gain/2, voidfunc(float f) { setField(gain := f*2) })),
        jrightalignedline(withLabel("Sound sources:", jLiveValueLabel(numSources)))
      )));
  }
  
  start {
    ownResource(loop = new StreamSoundToAPlay);
    copyFields(module(), loop, 'sampleRate, 'channels, 'bits, 'bufSizeMS);
    bufSizeInFrames = msToSamples_int(bufSizeMS, sampleRate);
    buffer = new short[bufSizeInFrames*2];
    fillBuffer();
    loop.start();
    loop.writeSamples(buffer);
    doEveryAndNow(bufSizeMS, r {
      fillBuffer();
      loop.writeSamples(buffer);
    });

    dm_registerAs audioOutputModule();
    
    if (registerService)
      dm_registerService('playWAVAndWait, func(S method, O[] args) -> bool {
        temp enter();
        callMC(f dm_playWAV, args);
        true;
      });
  }
  
  void fillBuffer enter {
    VF1<VF1<double[]>> vfRemoveSource = voidfunc(VF1<double[]> source) {
      removeSource(source)
    };
    
    double[] pair = new double[2];
    for i over buffer: {
      audio_makeSampleFromSources(sources, pair, gain, vfRemoveSource);
      buffer[i++] = clipShort(pair[0]);
      buffer[i] = clipShort(pair[1]);
    }
  }
  
  void setNumSources { numSources.set(l(sources)); }
  
  // hold audio sources during reload
  
  O _getReloadData() { ret sources; }
  void _setReloadData(L<VF1<double[]>> sources) { addAll(this.sources, sources); }

  // API
  
  void addSource(VF1<double[]> source) {
    sources.add(source);
    print("Audio source added.");
  }
  
  void removeSource(VF1<double[]> source) {
    sources.remove(source);
    print("Audio source removed.");
  }
  
  AudioFormat getAudioFormat() { ret new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sampleRate, bits, channels, channels*bits/2, sampleRate, false); }
  
  long samplesPlayed() { ret loop.frameCounter; }
}

Author comment

Began life as a copy of #1016699

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: #1027007
Snippet name: Audio Output using aplay [dev.]
Eternal ID of this version: #1027007/5
Text MD5: 18465ff50ead49ba99642cdc01f5fa06
Transpilation MD5: 2cc043245e8067c17d23de77b71dcdc4
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-06 23:14:32
Source code size: 2628 bytes / 86 lines
Pitched / IR pitched: No / No
Views / Downloads: 278 / 394
Version history: 4 change(s)
Referenced in: