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

52
LINES

< > BotCompany Repo | #1016687 // playAudioFromSampleMakingFunction

JavaX fragment (include)

1  
static playAudioFromSampleMakingFunction_AudioLoop playAudioFromSampleMakingFunction(int bufSize, VF1<double[]> sampleMaker) {
2  
  playAudioFromSampleMakingFunction_AudioLoop loop = new(bufSize, sampleMaker);
3  
  loop.start();
4  
  ret loop;
5  
}
6  
7  
static playAudioFromSampleMakingFunction_AudioLoop playAudioFromSampleMakingFunction_dontStart(int bufSize, VF1<double[]> sampleMaker) {
8  
  ret playAudioFromSampleMakingFunction_AudioLoop(bufSize, sampleMaker);
9  
}
10  
11  
sclass playAudioFromSampleMakingFunction_AudioLoop extends Thread implements AutoCloseable {
12  
  SourceDataLine out;
13  
  new Flag stopFlag;
14  
  byte[] buf;
15  
  VF1<double[]> sampleMaker;
16  
  AudioFormat quality = javaSound_cdQuality();
17  
  long samplesPlayed;
18  
  
19  
  *(int bufSize, VF1<double[]> *sampleMaker) {
20  
    out = javaSound_outputLine(quality, bufSize);
21  
    buf = new byte[bufSize];
22  
  }
23  
  
24  
  public void start { out.start°; super.start°; }
25  
  void stopPlaying { // "stop()" was taken
26  
    if (stopFlag.isUp()) ret;
27  
    stopFlag.raise°; out.close°;
28  
    print("Audio output loop stopped.");
29  
  }
30  
  public void close { stopPlaying(); }
31  
  
32  
  public void run° {
33  
    print("Audio output loop started. Buffer size=" + l(buf) + " (" + iround(l(buf)/44100.0/4*1000) + " ms)");
34  
    double[] pair = new double[2];
35  
    while (licensed() && !stopFlag.isUp()) {
36  
      for (int i = 0; i < l(buf); i += 4) {
37  
        sampleMaker.get(pair);
38  
        short l = clipShort(pair[0]);
39  
        short r = clipShort(pair[1]);
40  
        buf[i] = (byte) l;
41  
        buf[i+1] = (byte) (l >> 8);
42  
        buf[i+2] = (byte) r;
43  
        buf[i+3] = (byte) (r >> 8);
44  
      }
45  
      
46  
      //print("Writing " + l(buf) + " bytes");
47  
      int n = out.write(buf, 0, l(buf));
48  
      samplesPlayed += n;
49  
      if (n != l(buf)) ret with print("huh: " + n + " of " + l(buf));
50  
    }
51  
  }
52  
}

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: 517 / 550
Version history: 20 change(s)
Referenced in: [show references]