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).

!7

// Original source: AudioLoop from jsresources.org

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.readMonoSamples(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;
  double nextSample() {
    double f = samples[sampleIndex]/32767.0;
    sampleIndex = (sampleIndex+1) % l(samples);
    ret f;
  }

  public void run° {
    while (licensed() && !stopFlag.isUp()) {
      for (int i = 0; i < l(buf); i += 4) {
        double f = nextSample();
        int x = iround(max(0, min(1, f))*32767);
        buf[i] = buf[i+2] = (byte) x;
        buf[i+1] = buf[i+3] = (byte) (x >> 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 #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: 530 / 1090
Version history: 6 change(s)
Referenced in: [show references]