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

57
LINES

< > BotCompany Repo | #1016684 // Play Stereo 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).

!7

static int DEFAULT_INTERNAL_BUFSIZ = 40960, DEFAULT_EXTERNAL_BUFSIZ = 40960;

p {
  temp WAVDecoder decoder = new(newFileInputStream(loadLibrary(#1400093)));
  new L<short[]> chunks;
  while licensed {
    short[] buf = new short[4096];
    int n = decoder.readStereoSamples(buf);
    if (n <= 0) break;
    chunks.add(subArray(buf, 0, n));
  }
  short[] samples = concatShortArrays(chunks);
  new AudioLoop(javaSound_cdQuality(), DEFAULT_INTERNAL_BUFSIZ, DEFAULT_EXTERNAL_BUFSIZ, samples).start();
}

sclass AudioLoop extends Thread {
  TargetDataLine in;
  SourceDataLine out;
  new Flag stopFlag;
  int externalBufferSize;
  byte[] buf;
  short[] samples;
  
  *(AudioFormat format, int internalBufferSize, int *externalBufferSize, short[] *samples) {
    out = javaSound_outputLine(format, internalBufferSize);
    buf = new byte[internalBufferSize];
  }
  
  public void start { out.start°; super.start°; }
  void stopPlaying { out.close°; stopFlag.raise°; } // "stop()" was taken
  
  int sampleIndex = 0;
  void nextSamples(short[] pair) {
    pair[0] = samples[sampleIndex];
    pair[1] = samples[sampleIndex+1];
    sampleIndex = (sampleIndex+2) % l(samples);
  }

  public void run° {
    short[] pair = new short[2];
    while (licensed() && !stopFlag.isUp()) {
      for (int i = 0; i < l(buf); i += 4) {
        nextSamples(pair);
        buf[i] = (byte) pair[0];
        buf[i+1] = (byte) (pair[0] >> 8);
        buf[i+2] = (byte) pair[1];
        buf[i+3] = (byte) (pair[1] >> 8);
      }
      
      //print("Writing " + l(buf) + " bytes");
      int n = out.write(buf, 0, l(buf));
      if (n != l(buf)) print("huh: " + n + " of " + l(buf));
    }
  }
}

Author comment

Began life as a copy of #1016683

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1016684
Snippet name: Play Stereo Loop From WAV Spike (OK)
Eternal ID of this version: #1016684/5
Text MD5: 9a9873ce2d6938a1a40df5f6deb2fa39
Transpilation MD5: 8c2df96444375417d21f10b30c1ff760
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:55
Source code size: 1736 bytes / 57 lines
Pitched / IR pitched: No / No
Views / Downloads: 359 / 797
Version history: 4 change(s)
Referenced in: [show references]