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

57
LINES

< > BotCompany Repo | #1016683 // Play Mono Loop From WAV Spike (OK)

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (3051L/21K).

1  
!7
2  
3  
// Original source: AudioLoop from jsresources.org
4  
5  
static int DEFAULT_INTERNAL_BUFSIZ = 40960, DEFAULT_EXTERNAL_BUFSIZ = 40960;
6  
7  
p {
8  
  temp WAVDecoder decoder = new(newFileInputStream(loadLibrary(#1400093)));
9  
  new L<short[]> chunks;
10  
  while licensed {
11  
    short[] buf = new short[4096];
12  
    int n = decoder.readMonoSamples(buf);
13  
    if (n <= 0) break;
14  
    chunks.add(subArray(buf, 0, n));
15  
  }
16  
  short[] samples = concatShortArrays(chunks);
17  
  new AudioLoop(javaSound_cdQuality(), DEFAULT_INTERNAL_BUFSIZ, DEFAULT_EXTERNAL_BUFSIZ, samples).start();
18  
}
19  
20  
sclass AudioLoop extends Thread {
21  
  TargetDataLine in;
22  
  SourceDataLine out;
23  
  new Flag stopFlag;
24  
  int externalBufferSize;
25  
  byte[] buf;
26  
  short[] samples;
27  
  
28  
  *(AudioFormat format, int internalBufferSize, int *externalBufferSize, short[] *samples) {
29  
    out = javaSound_outputLine(format, internalBufferSize);
30  
    buf = new byte[internalBufferSize];
31  
  }
32  
  
33  
  public void start { out.start°; super.start°; }
34  
  void stopPlaying { out.close°; stopFlag.raise°; } // "stop()" was taken
35  
  
36  
  int sampleIndex = 0;
37  
  double nextSample() {
38  
    double f = samples[sampleIndex]/32767.0;
39  
    sampleIndex = (sampleIndex+1) % l(samples);
40  
    ret f;
41  
  }
42  
43  
  public void run° {
44  
    while (licensed() && !stopFlag.isUp()) {
45  
      for (int i = 0; i < l(buf); i += 4) {
46  
        double f = nextSample();
47  
        int x = iround(max(0, min(1, f))*32767);
48  
        buf[i] = buf[i+2] = (byte) x;
49  
        buf[i+1] = buf[i+3] = (byte) (x >> 8);
50  
      }
51  
      
52  
      //print("Writing " + l(buf) + " bytes");
53  
      int n = out.write(buf, 0, l(buf));
54  
      if (n != l(buf)) print("huh: " + n + " of " + l(buf));
55  
    }
56  
  }
57  
}

Author comment

Began life as a copy of #1016678

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1016683
Snippet name: Play Mono Loop From WAV Spike (OK)
Eternal ID of this version: #1016683/7
Text MD5: 77d7c9b5d2bcc9c8185a76d08e7a4b24
Transpilation MD5: f0685057857f767dbe37f43a7553c4b3
Author: stefan
Category: javax / sound
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-06-28 00:31:46
Source code size: 1712 bytes / 57 lines
Pitched / IR pitched: No / No
Views / Downloads: 424 / 837
Version history: 6 change(s)
Referenced in: [show references]