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

36
LINES

< > BotCompany Repo | #1027005 // StreamSoundToAPlay [OK, but needs handling for ended process, e.g. when audio device changes in raspbian]

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3878L/25K).

1  
sclass StreamSoundToAPlay implements AutoCloseable {
2  
  Process process;
3  
  InputStream in;
4  
  OutputStream out;
5  
  int sampleRate = 44100, channels = 2, bits = 16;
6  
  int bufSizeMS = 0; // 0 = default
7  
  double duration = yearsToSeconds(1); // max seconds
8  
  GSWavFile wavStream;
9  
  long frameCounter = 0;
10  
  
11  
  void start ctex {
12  
    assertOnPATH("aplay");
13  
    Process process = Runtime.getRuntime().exec("aplay"
14  
      + (bufSizeMS == 0 ? "" : " -B " + bufSizeMS));
15  
    
16  
    // should probably print those instead
17  
    drainStreamInNewThread(process.getInputStream());
18  
    drainStreamInNewThread(process.getErrorStream());
19  
    
20  
    out = process.getOutputStream();
21  
    long numFrames = (long)(duration * sampleRate);
22  
  
23  
    wavStream = GSWavFile.newWavFile(out, null, channels, numFrames, bits, sampleRate);
24  
  }
25  
  
26  
  void writeSamples(short[] samples) ctex {
27  
    if (samples != null) for (short x : samples) {
28  
      wavStream.writeSample(x);
29  
      ++frameCounter;
30  
    }
31  
  }
32  
  
33  
  public void close {
34  
    dispose wavStream; // also closes the stream & thereby ends the process
35  
  }
36  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1027005
Snippet name: StreamSoundToAPlay [OK, but needs handling for ended process, e.g. when audio device changes in raspbian]
Eternal ID of this version: #1027005/9
Text MD5: c7ee10f3b96d24e0f6035fb08645e5b5
Transpilation MD5: 0c16ec256b32808e00a23eceb0d2c29a
Author: stefan
Category: javax / audio
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-07 00:32:29
Source code size: 1106 bytes / 36 lines
Pitched / IR pitched: No / No
Views / Downloads: 199 / 544
Version history: 8 change(s)
Referenced in: [show references]