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

52
LINES

< > BotCompany Repo | #1016687 // playAudioFromSampleMakingFunction

JavaX fragment (include)

static playAudioFromSampleMakingFunction_AudioLoop playAudioFromSampleMakingFunction(int bufSize, VF1<double[]> sampleMaker) {
  playAudioFromSampleMakingFunction_AudioLoop loop = new(bufSize, sampleMaker);
  loop.start();
  ret loop;
}

static playAudioFromSampleMakingFunction_AudioLoop playAudioFromSampleMakingFunction_dontStart(int bufSize, VF1<double[]> sampleMaker) {
  ret playAudioFromSampleMakingFunction_AudioLoop(bufSize, sampleMaker);
}

sclass playAudioFromSampleMakingFunction_AudioLoop extends Thread implements AutoCloseable {
  SourceDataLine out;
  new Flag stopFlag;
  byte[] buf;
  VF1<double[]> sampleMaker;
  AudioFormat quality = javaSound_cdQuality();
  long samplesPlayed;
  
  *(int bufSize, VF1<double[]> *sampleMaker) {
    out = javaSound_outputLine(quality, bufSize);
    buf = new byte[bufSize];
  }
  
  public void start { out.start°; super.start°; }
  void stopPlaying { // "stop()" was taken
    if (stopFlag.isUp()) ret;
    stopFlag.raise°; out.close°;
    print("Audio output loop stopped.");
  }
  public void close { stopPlaying(); }
  
  public void run° {
    print("Audio output loop started. Buffer size=" + l(buf) + " (" + iround(l(buf)/44100.0/4*1000) + " ms)");
    double[] pair = new double[2];
    while (licensed() && !stopFlag.isUp()) {
      for (int i = 0; i < l(buf); i += 4) {
        sampleMaker.get(pair);
        short l = clipShort(pair[0]);
        short r = clipShort(pair[1]);
        buf[i] = (byte) l;
        buf[i+1] = (byte) (l >> 8);
        buf[i+2] = (byte) r;
        buf[i+3] = (byte) (r >> 8);
      }
      
      //print("Writing " + l(buf) + " bytes");
      int n = out.write(buf, 0, l(buf));
      samplesPlayed += n;
      if (n != l(buf)) ret with print("huh: " + n + " of " + l(buf));
    }
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1016687
Snippet name: playAudioFromSampleMakingFunction
Eternal ID of this version: #1016687/21
Text MD5: 01ebec8c0ae07d5ea35743da099f59be
Author: stefan
Category: javax / sound
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-06 14:07:46
Source code size: 1837 bytes / 52 lines
Pitched / IR pitched: No / No
Views / Downloads: 392 / 450
Version history: 20 change(s)
Referenced in: [show references]