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

42
LINES

< > BotCompany Repo | #1027004 // Stream WAV to aplay Spike [OK]

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

Download Jar. Libraryless. Click here for Pure Java version (7897L/55K).

!7

p-exp {
  assertOnPATH("aplay");
  Process process = Runtime.getRuntime().exec("aplay");
  InputStream in = process.getInputStream();
  drainStreamInNewThread(in); // just to take care of it
  
  OutputStream out = process.getOutputStream();
   
  int sampleRate = 44100;    // Samples per second
  double duration = 5.0;     // Seconds

  // Calculate the number of frames required for specified duration
  long numFrames = (long)(duration * sampleRate);
  
  // Create a wav file with the name specified as the first argument
  GSWavFile wavFile = GSWavFile.newWavFile(out, null, 2, numFrames, 16, sampleRate);
  
  // Create a buffer of 100 frames
  double[][] buffer = new double[2][100];
  long frameCounter = 0;

  while (frameCounter < numFrames) {
    // Determine how many frames to write, up to a maximum of the buffer size
    long remaining = wavFile.getFramesRemaining();
    int toWrite = (remaining > 100) ? 100 : (int) remaining;

    // Fill the buffer, one tone per channel
    for (int s=0 ; s<toWrite ; s++, frameCounter++)
    {
       buffer[0][s] = Math.sin(2.0 * Math.PI * 400 * frameCounter / sampleRate);
       buffer[1][s] = Math.sin(2.0 * Math.PI * 500 * frameCounter / sampleRate);
    }

    // Write the buffer
    wavFile.writeFrames(buffer, toWrite);
  }

  // Close the wavFile
  wavFile.close();
}

download  show line numbers  debug dex  old transpilations   

Travelled to 8 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, snaazhdonpnp, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027004
Snippet name: Stream WAV to aplay Spike [OK]
Eternal ID of this version: #1027004/2
Text MD5: 776c644ec6c917764e7ecc9b3c750c84
Transpilation MD5: 47f003f5adcba2b2c0a71f9dea17df68
Author: stefan
Category: javax / audio
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-06 14:46:54
Source code size: 1378 bytes / 42 lines
Pitched / IR pitched: No / No
Views / Downloads: 157 / 539
Version history: 1 change(s)
Referenced in: [show references]