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

1  
!7
2  
3  
p-exp {
4  
  assertOnPATH("aplay");
5  
  Process process = Runtime.getRuntime().exec("aplay");
6  
  InputStream in = process.getInputStream();
7  
  drainStreamInNewThread(in); // just to take care of it
8  
  
9  
  OutputStream out = process.getOutputStream();
10  
   
11  
  int sampleRate = 44100;    // Samples per second
12  
  double duration = 5.0;     // Seconds
13  
14  
  // Calculate the number of frames required for specified duration
15  
  long numFrames = (long)(duration * sampleRate);
16  
  
17  
  // Create a wav file with the name specified as the first argument
18  
  GSWavFile wavFile = GSWavFile.newWavFile(out, null, 2, numFrames, 16, sampleRate);
19  
  
20  
  // Create a buffer of 100 frames
21  
  double[][] buffer = new double[2][100];
22  
  long frameCounter = 0;
23  
24  
  while (frameCounter < numFrames) {
25  
    // Determine how many frames to write, up to a maximum of the buffer size
26  
    long remaining = wavFile.getFramesRemaining();
27  
    int toWrite = (remaining > 100) ? 100 : (int) remaining;
28  
29  
    // Fill the buffer, one tone per channel
30  
    for (int s=0 ; s<toWrite ; s++, frameCounter++)
31  
    {
32  
       buffer[0][s] = Math.sin(2.0 * Math.PI * 400 * frameCounter / sampleRate);
33  
       buffer[1][s] = Math.sin(2.0 * Math.PI * 500 * frameCounter / sampleRate);
34  
    }
35  
36  
    // Write the buffer
37  
    wavFile.writeFrames(buffer, toWrite);
38  
  }
39  
40  
  // Close the wavFile
41  
  wavFile.close();
42  
}

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: 311 / 903
Version history: 1 change(s)
Referenced in: [show references]