-
Notifications
You must be signed in to change notification settings - Fork 89
Description
Trying this library out to learn more about how audio streams work, and ran into an issue on my Win 10 machine.
I am attempting to send audio from my Electron app to two output devices: my computer speakers and my virtual audio cable. The following code appears to work for the most part:
import * as portAudio from 'naudiodon';
import * as fs from 'fs';
import * as wav from 'wav';
let wavFiles:fs.ReadStream[] = [];
let wavReader;
let headPhones, virtualCable;
let paused = false;
setupPortAudioDevices();
export function setupPortAudioDevices(){
const fullDeviceList = portAudio.getDevices();
const selectedDeviceList : portAudio.DeviceInfo[] = [];
// Lookup the speakers and virtual cable devices.
fullDeviceList.map((device)=>{
if(device.name === "Speakers (VB-Audio CABLE-A)" || device.name === "Realtek Digital Output (Realtek"){
selectedDeviceList.push(device);
}
});
// Create the headphones
headPhones = portAudio.AudioIO({
outOptions: {
channelCount: 1,
sampleFormat: portAudio.SampleFormat16Bit,
sampleRate: 48000,
deviceId: selectedDeviceList[0].id,
closeOnError: false}
});
// Create the virtual speakers (Crashes here on the second run)
virtualCable = portAudio.AudioIO({
outOptions: {
channelCount: 1,
sampleFormat: portAudio.SampleFormat16Bit,
sampleRate: 48000,
deviceId: selectedDeviceList[1].id,
closeOnError: false}
});
console.log("PortAudio devices created.");
}
export function routeAudio(file: string){
if(wavReader){
wavReader = undefined;
}
wavReader = new wav.Reader();
wavReader.on('pause',(stuff)=>{
console.log("Wave reader paused.");
});
wavReader.on('close',(stuff)=>{
console.log("---- Wave reader CLOSED.");
});
console.log("Wave Reader created.");
const newWaveFile = fs.createReadStream(file);
newWaveFile.on('data', (chunk)=>{
console.log("data:",chunk);
});
newWaveFile.on('end', () => {
console.log('Wave ended.');
});
newWaveFile.on('error', (err) => {
console.error('Wave Error!');
console.error(err);
});
newWaveFile.on('close', (err) => {
console.error('----------- Wave Closed!');
// Re-open the now finished port audio devices
setupPortAudioDevices();
});
newWaveFile.on('pause', ()=>{
console.log("PAUSED the wav file");
});
// Pipe the WAV file through the reader to strip off the WAV header.
newWaveFile.pipe(wavReader);
// Pipe the stripped WAV to the two output devices
wavReader.pipe(headPhones);
wavReader.pipe(virtualCable);
// Start the devices
headPhones.start();
virtualCable.start();
}
Essentially when I run routeAudio(FILENAME)
and pass it a WAV file it plays it out both devices to completion. However when the WAV file completes (on close) and I rerun the setupPortAudioDevices()
function the headphones connect again properly but when attempting to connect the virtual cable device I get the following error:
I'm sure I'm doing something wrong here which is likely simple but I'm still rather new to this. Any assistance would be most welcome. Also, thank you for making such a cool library, it's greatly appreciated!