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

sclass StreamSoundToAPlay implements AutoCloseable {
  Process process;
  InputStream in;
  OutputStream out;
  int sampleRate = 44100, channels = 2, bits = 16;
  int bufSizeMS = 0; // 0 = default
  double duration = yearsToSeconds(1); // max seconds
  GSWavFile wavStream;
  long frameCounter = 0;
  
  void start ctex {
    assertOnPATH("aplay");
    Process process = Runtime.getRuntime().exec("aplay"
      + (bufSizeMS == 0 ? "" : " -B " + bufSizeMS));
    
    // should probably print those instead
    drainStreamInNewThread(process.getInputStream());
    drainStreamInNewThread(process.getErrorStream());
    
    out = process.getOutputStream();
    long numFrames = (long)(duration * sampleRate);
  
    wavStream = GSWavFile.newWavFile(out, null, channels, numFrames, bits, sampleRate);
  }
  
  void writeSamples(short[] samples) ctex {
    if (samples != null) for (short x : samples) {
      wavStream.writeSample(x);
      ++frameCounter;
    }
  }
  
  public void close {
    dispose wavStream; // also closes the stream & thereby ends the process
  }
}

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: 194 / 535
Version history: 8 change(s)
Referenced in: [show references]