简体   繁体   中英

C# NAudio asio and wasapi

I'm using NAudio lib in C# for my audio editor project. I am using wasapi to play multiple wav files at the same time.

            bool useEventSync = false;
            AudioClientShareMode share = AudioClientShareMode.Shared;
            wasapiOut = new WasapiOut(share, useEventSync, 50);
            wasapiOut.Volume = 1.0f;
            reader = new AudioFileReader(filename);
            wasapiOut.Init(reader);
            wasapiOut.Play();

I have this "play" button on all of my UserControls so i can play multiple wav files. There is also another button that's using AsioOut to play mic input on speakers in real time, in other words, you can hear back what you say through speakers immediately.

            bufferke = new BufferedWaveProvider(new WaveFormat(44100,16,2));
            string[]driverek=AsioOut.GetDriverNames();
            asioOut = new AsioOut(driverek[0]);
            asioOut.InputChannelOffset = 0;
            asioOut.InitRecordAndPlayback(bufferke, 2, 44100);
            asioOut.AudioAvailable += new EventHandler<AsioAudioAvailableEventArgs>(asioout_AudioAvailable);
            asioOut.Play();

And the asioout_AudioAvailable:

    private void asioout_AudioAvailable(object sender, AsioAudioAvailableEventArgs e)
    {
        byte[] OutputBuffer = new byte[e.SamplesPerBuffer * 4];
        for (int i = 0; i < e.InputBuffers.Length; i++)
        {
            Marshal.Copy(e.InputBuffers[i], OutputBuffer, 0, e.SamplesPerBuffer * 4);
            Marshal.Copy(OutputBuffer, 0, e.OutputBuffers[i], e.SamplesPerBuffer * 4);
        }

        e.WrittenToOutputBuffers = true;
    }

The problem is i am not able to play wav files and use my asio monitoring button at the same time. Basically when i play wav files the asio monitoring button simply stops them. What am i doing wrong?

Generally speaking its not a good idea to mix ASIO with other output drivers. Instead, if you're using ASIO, use that for both playback and record.

The playback part of AsioOut could be driven by a MixingSampleProvider which you can add files that need to be played into as well as having an additional input that handles the loopback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM