ircam-ismm / node-web-audio-api

Web Audio API for Node.js
BSD 3-Clause "New" or "Revised" License
92 stars 12 forks source link

channelCount constraint of getUserMedia is ignored #74

Open trevorparscal opened 5 months ago

trevorparscal commented 5 months ago

When using an audio interface with more than 2 channels, getUserMedia should be able to return a media stream with more than 2 channels by providing a channelCount constraint. Browsers generally don't support more than 2, but I was hoping since the spec does support it that this project might achieve it.

Here's some test code. One of my audio devices has 18 inputs, but it reports 2. I tired using 18, { exact: 18 } and { ideal: 18 } as constraints, but all were ignored.

import { mediaDevices, AudioContext } from 'node-web-audio-api';

async function init() {
    const audioContext = new AudioContext();

    const devices = await mediaDevices.enumerateDevices();

    console.log( audioContext.destination.channelCount, 'outputs' );

    for ( const device of devices ) {
        if ( device.kind === 'audioinput' ) {
            console.log( device.label );
            const mediaStream = await mediaDevices.getUserMedia( {
                audio: {
                    deviceId: device.deviceId,
                    channelCount: 18
                }
            } );
            const source = audioContext.createMediaStreamSource( mediaStream );
            console.log( '>', source.channelCount, 'inputs' );
            source.disconnect();
        }
    }

    audioContext.close();
}

init()
    .then( () => process.exit() )
    .catch( console.error );
b-ma commented 5 months ago

Hey, thanks for the feedback,

Indeed, I don't think multi channel input is something we have really tested. Would be really nice to have, but I guess this also relates to the upstream crate and not only the bindings. I really can't assure this will be fixed in the short term as we are focusing on consolidating Web Audio spec compliance right now (...and media devices API is not per se part of the Web Audio spec).

Let's keep this open as a reminder

trevorparscal commented 5 months ago

I opened an issue upstream

https://github.com/orottier/web-audio-api-rs/issues/446

trevorparscal commented 3 months ago

I tested today with version web-audio-api 0.44.0 and still am only seeing 2 inputs and 2 outputs when using my multichannel interface. In this case, the interface is a Presonus StudioLive 16.0.2 USB device, which under MacOS (with no special driver installed) reports 18 inputs and 16 outputs.

Here's my test code...

use web_audio_api::context::AudioContext;
use web_audio_api::media_devices::{self, MediaTrackConstraints};
use web_audio_api::media_devices::MediaStreamConstraints;
use web_audio_api::node::AudioNode;

fn main() {
    let context = AudioContext::default();

    let devices = media_devices::enumerate_devices_sync();
    for device in devices.iter() {
        println!( "{}", device.label() );

        let mut media_track_constraints = MediaTrackConstraints::default();
        media_track_constraints.device_id = Some( device.device_id().to_string() );
        media_track_constraints.channel_count = Some( 18 );
        let input = media_devices::get_user_media_sync(
            MediaStreamConstraints::AudioWithConstraints( media_track_constraints )
        );
        let stream = context.create_media_stream_source(&input);

        println!( "> {}", stream.channel_count() );
    }
}