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

45
LINES

< > BotCompany Repo | #1016678 // Sine Signal Test [OK]

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

Download Jar. Libraryless. Click here for Pure Java version (1038L/8K).

!7

// Original source: AudioLoop from jsresources.org

static int DEFAULT_INTERNAL_BUFSIZ = 40960, DEFAULT_EXTERNAL_BUFSIZ = 40960;

p {
  new AudioLoop(javaSound_cdQuality(), DEFAULT_INTERNAL_BUFSIZ, DEFAULT_EXTERNAL_BUFSIZ).start();
}

sclass AudioLoop extends Thread {
  TargetDataLine in;
  SourceDataLine out;
  new Flag stopFlag;
  int externalBufferSize;
  byte[] buf;
  
  *(AudioFormat format, int internalBufferSize, int *externalBufferSize) {
    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
  
  long count;
  double nextSample() {
    ret Math.sin(count++/44100.0*440*2*pi())*0.5;
  }

  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 #1009287

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: #1016678
Snippet name: Sine Signal Test [OK]
Eternal ID of this version: #1016678/13
Text MD5: 6e3d2051c8089e53f0491b5cf029e760
Transpilation MD5: 179baf12cfccba091e41bb70c219a6f6
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:13:04
Source code size: 1281 bytes / 45 lines
Pitched / IR pitched: No / No
Views / Downloads: 416 / 1067
Version history: 12 change(s)
Referenced in: #1016683 - Play Mono Loop From WAV Spike (OK)