Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to open a Virtual Device Stream the SECOND time. #82

Open
GeoffGodwin opened this issue Oct 4, 2022 · 0 comments
Open

Unable to open a Virtual Device Stream the SECOND time. #82

GeoffGodwin opened this issue Oct 4, 2022 · 0 comments

Comments

@GeoffGodwin
Copy link

GeoffGodwin commented Oct 4, 2022

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:

image

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant