Author Topic: ASIO Inputs and Outputs with Mixer  (Read 734 times)

james2k2

  • Posts: 29
ASIO Inputs and Outputs with Mixer
« on: 26 Jun '19 - 12:54 »
Hi All,

So I'm trying to write some software where I can take an ASIO input (in this case from an instrument/microphone via a USB audio interface), pass this to a mixer, then get an audio file that also feeds to a mixer, and then play the mixer out to a different ASIO device. So I have two completely separate ASIO devices - One is being used purely for input, and the other for output. I am using Bass.Net for this task. The code I've done below is in VB, however I'm happy with any replies being in C# as I can easily interchange between the two myself. I am getting audio from the local file stream (albeit it's playing at the wrong speed - sample rate or some issue I'm sure is an easy fix?) but no audio being passed through from the ASIO input.

This is what I have thus far:
Code: [Select]
Dim stream As Integer
Dim mixer As Integer
Dim mic As BassAsioHandler
Dim mp3 As BassAsioHandler

......

'Init our devices
Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero)
mp3 = New BassAsioHandler(False, 0, 0, 1, BASSASIOFormat.BASS_ASIO_FORMAT_FLOAT, 44100)
mic = New BassAsioHandler(True, 1, 0, 1, BASSASIOFormat.BASS_ASIO_FORMAT_FLOAT, 44100)

'Start the ASIO in device
mic.Start(0, 0)

'Create our streams for a local file
stream = Bass.BASS_StreamCreateFile("C:\test2.m4a", 0, 0, BASSFlag.BASS_STREAM_DECODE Or BASSFlag.BASS_SAMPLE_FLOAT)
mixer = BassMix.BASS_Mixer_StreamCreate(44100, 2, BASSFlag.BASS_STREAM_DECODE Or BASSFlag.BASS_SAMPLE_FLOAT)

'Add our streams to our mixer
BassMix.BASS_Mixer_StreamAddChannel(mixer, mic.InputChannel, BASSFlag.BASS_MIXER_BUFFER)
BassMix.BASS_Mixer_StreamAddChannel(mixer, stream, BASSFlag.BASS_MIXER_BUFFER)

'Start our audio stream
mp3.OutputChannel = mixer
mp3.Start(0, 0)

I will say that further down, I have a timer that displays the VU level for both the mic and mp3 streams, and this actually works so I know something is working.
« Last Edit: 26 Jun '19 - 13:13 by james2k2 »

Ian @ un4seen

  • Administrator
  • Posts: 22959
Re: ASIO Inputs and Outputs with Mixer
« Reply #1 on: 26 Jun '19 - 14:38 »
It looks like you have set the ASIO output to mono and the mixer to stereo there. Please try changing one of them to match to other and see if the problems still happen then. You could also use the output's SampleRate property in the BASS_Mixer_StreamCreate call, just in case the request rate (44100) isn't supported by the device.

If the problems persist, what calls are you using in the VU level stuff, and does disabling that stop the problem? It could be that those calls are taking data away from the mixer, ie. the data used to get the level is then not available to the mixer.

james2k2

  • Posts: 29
Re: ASIO Inputs and Outputs with Mixer
« Reply #2 on: 26 Jun '19 - 15:08 »
Hi Ian,

Thank you for your reply. You were indeed correct on the number of channels front. I corrected that and it now works correctly in terms of the slow down issue. I have been reading a bit more documentation and found I wasn't actually grabbing the Push Stream. See the below code. This now works, the audio passes through, but has a very high latency ~ 0.5s to 1s. Any ideas on what I can do to reduce this latency? Many thanks!

Code: [Select]
        Bass.BASS_Init(0, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero)
        out = New BassAsioHandler(False, 0, 0, 2, BASSASIOFormat.BASS_ASIO_FORMAT_FLOAT, 44100)
        mic = New BassAsioHandler(True, 1, 0, 1, BASSASIOFormat.BASS_ASIO_FORMAT_FLOAT, 44100)
        mic.Start(0, 0)

        For Each device As BASS_ASIO_DEVICEINFO In BassAsio.BASS_ASIO_GetDeviceInfos
            ListBox1.Items.Add(device.name)
        Next

        stream = Bass.BASS_StreamCreateFile("C:\test2.m4a", 0, 0, BASSFlag.BASS_STREAM_DECODE Or BASSFlag.BASS_SAMPLE_FLOAT)
        mixer = BassMix.BASS_Mixer_StreamCreate(44100, 2, BASSFlag.BASS_STREAM_DECODE Or BASSFlag.BASS_SAMPLE_FLOAT)

        mic.SetFullDuplex(0, BASSFlag.BASS_STREAM_DECODE, False)
        BassMix.BASS_Mixer_StreamAddChannel(mixer, mic.OutputChannel, BASSFlag.BASS_MIXER_BUFFER)
        BassMix.BASS_Mixer_StreamAddChannel(mixer, stream, BASSFlag.BASS_MIXER_BUFFER)

        out.OutputChannel = mixer
        out.Start(0, 0)

The VU code as requested is below. ASIO Device 0 is the output.
Code: [Select]
BassAsio.BASS_ASIO_SetDevice(0)
        Try
            If BassAsio.BASS_ASIO_ChannelIsActive(False, 0) Then
                ProgressBar1.Value = BassAsio.BASS_ASIO_ChannelGetLevel(False, 0) * 100
            End If
        Catch
        End Try

        BassAsio.BASS_ASIO_SetDevice(1)
        If BassAsio.BASS_ASIO_ChannelIsActive(True, 0) Then
            Try
                ProgressBar2.Value = BassAsio.BASS_ASIO_ChannelGetLevel(True, 0) * 100
            Catch
            End Try
        End If

Ian @ un4seen

  • Administrator
  • Posts: 22959
Re: ASIO Inputs and Outputs with Mixer
« Reply #3 on: 26 Jun '19 - 16:24 »
That VU level stuff looks fine (I was concerned that you may be using BASS_ChannelGetLevel).

Regarding latency, does moving the "mic.Start" call to just before the "out.Start" call help?

james2k2

  • Posts: 29
Re: ASIO Inputs and Outputs with Mixer
« Reply #4 on: 26 Jun '19 - 16:33 »
That doesn't help unfortunately. I have also tried changing the config variables for buffer and update, to no avail.

I have tested changing:
out.OutputChannel = mixer

to

out.OutputChannel = mic.OutputChannel

And this has an interesting effect of near zero latency, but also corrupted audio in the sense that it's pitched up and sounds like a robot. In short, it appears the mixer is the element introducing the latency. I've tried changing the flags as well, but this produces either no audio or horrible buzz type noises from the speaker.

Cheers,
James

Ian @ un4seen

  • Administrator
  • Posts: 22959
Re: ASIO Inputs and Outputs with Mixer
« Reply #5 on: 26 Jun '19 - 17:11 »
That's strange. As it's a decoding channel (so doesn't have a playback buffer), the mixer shouldn't add any latency. I'm not sure if the BassAsioHandler class has any additional buffering internally. Assuming it doesn't, the "mic.OutputChannel" push stream seems like the only place where the latency could be coming from. If you monitor its buffer level with BASS_StreamPutData (length=0), what do you see?

james2k2

  • Posts: 29
Re: ASIO Inputs and Outputs with Mixer
« Reply #6 on: 26 Jun '19 - 20:21 »
Maybe I misunderstood, so I've tried with this:

Label1.Text = Bass.BASS_StreamPutData(mixer, 0, 0)

And I get -1 returned. Is this what you were meaning?

Cheers,
James

Ian @ un4seen

  • Administrator
  • Posts: 22959
Re: ASIO Inputs and Outputs with Mixer
« Reply #7 on: 27 Jun '19 - 13:44 »
BASS_StreamPutData only deals with "push" streams, so you should use the "mic.OutputChannel" stream handle (created by the BassAsioHandler class) in that call, rather than the mixer handle.