Author Topic: Problems switching audio device of mixer channel  (Read 193 times)

kafffee

  • Posts: 173
Hello there :-)

I have a combo box on my UI to let the user switch audio output devices for playig back the music.

However, I ran into the following issue:

When I first play something on the mixer channel, the switching does work, but when I switch devices before starting a playback, the playback will not start at all. Here is my code:

Code: [Select]
Public Sub New     'constructor of class
GetPlaybackDevices()          'method to retrieve playback devices
MainModule.mixer = Un4seen.Bass.AddOn.Mix.BassMix.BASS_Mixer_StreamCreate(44100, 2, BASSFlag.BASS_DEFAULT Or BASSFlag.BASS_SAMPLE_FLOAT)     'initializing the mixer channel
End Sub

Public Sub GetPlaybackDevices()
        Dim PlaybackDeviceInfo As New BASS_DEVICEINFO
        For i = 0 To Bass.BASS_GetDeviceCount - 1
            Bass.BASS_GetDeviceInfo(i, PlaybackDeviceInfo)
            Speakers.Add(PlaybackDeviceInfo.name)  'adding device names to List(Of String) to display in combo box
            If IsInDesignMode = False Then
                Bass.BASS_Init(i, 44100, BASSInit.BASS_DEVICE_DEFAULT, Nothing)   'initialising all devices
                Bass.BASS_PluginLoad("bass_aac.dll")
                Bass.BASS_PluginLoad("bassalac.dll")
                Bass.BASS_PluginLoad("bassflac.dll")
                Bass.BASS_PluginLoad("bassopus.dll")
                Bass.BASS_PluginLoad("basswma.dll")
                Bass.BASS_PluginLoad("basswv.dll")
            End If
        Next

        If Speakers.Count > 0 Then
            SpeakerIndex = 1         'preselect default device
        End If
    End Sub

Private _SpeakerIndex As Integer
    Public Property SpeakerIndex As Integer        'this property is bound to the selected index of the combo box, which lets the user switch output devices
        Get
            Return _SpeakerIndex
        End Get
        Set(value As Integer)
            _SpeakerIndex = value
            MainModule.MySettings.LautsprecherIndex = value    'store index for later use (saving settings)
            If MainModule.mixer <> 0 Then
                Bass.BASS_ChannelSetDevice(MainModule.mixer, value)   'if selected index of combo box is changed and mixer channel is initialized, set device to selected index
            End If
            RaisePropertyChanged()
        End Set
    End Property

Is there anything wrong with the order of the method calls? Any help appreciated  :)

Ian @ un4seen

  • Administrator
  • Posts: 24936
Re: Problems switching audio device of mixer channel
« Reply #1 on: 7 Feb '23 - 16:05 »
When there are multiple initialized devices, stream creation functions (including BASS_Mixer_StreamCreate) will use the current thread's device context, which can be set via BASS_SetDevice. If your app is single threaded then you could put the BASS_SetDevice call next to the BASS_ChannelSetDevice call. Otherwise, you should put it just before the BASS_Mixer_StreamCreate call:

Code: [Select]
Bass.BASS_SetDevice(MainModule.MySettings.LautsprecherIndex) 'set device number for the mixer
MainModule.mixer = Un4seen.Bass.AddOn.Mix.BassMix.BASS_Mixer_StreamCreate(44100, 2, BASSFlag.BASS_DEFAULT Or BASSFlag.BASS_SAMPLE_FLOAT)     'initializing the mixer channel

Regarding the BASS_PluginLoad calls, plugins only need to be loaded once, not again for each device. So you should move those calls out of your device initialization loop. Duplicate BASS_PluginLoad calls won't do any harm, but they won't do any good either :)

kafffee

  • Posts: 173
Re: Problems switching audio device of mixer channel
« Reply #2 on: 8 Feb '23 - 12:38 »
OK I tried this, but no success. I believe that I have multiple threads, but I tried either way... I mean I do not run multiple threads on purpose, but I know my app does by occasion....

The weird thing is:

BASS_ChannelSetDevice returns error code 0.

When I switch devices by code it works, but if I choose a device from my combo box, it doesnt, even though this should have the same effect...

The file is getting loaded, and it even shows a waveform, but it just won't play the music... Anything else I could have missed?

Even if I hit "Play" again after switching, the music wont start playing. It seems to somehow "be blocked"...

Do you need more code?
« Last Edit: 8 Feb '23 - 12:49 by kafffee »

Ian @ un4seen

  • Administrator
  • Posts: 24936
Re: Problems switching audio device of mixer channel
« Reply #3 on: 8 Feb '23 - 15:21 »
The weird thing is:

BASS_ChannelSetDevice returns error code 0.

When I switch devices by code it works, but if I choose a device from my combo box, it doesnt, even though this should have the same effect...

Are you sure the device number is the same in both cases? Perhaps they're different, and so the stream is being moved to a different device than expected in the combobox case? Try logging the device numbers in the calls to confirm.

The file is getting loaded, and it even shows a waveform, but it just won't play the music... Anything else I could have missed?

Even if I hit "Play" again after switching, the music wont start playing. It seems to somehow "be blocked"...

The mention of a "waveform" suggests that it may be playing but just not audible? If so, it sounds like it's either muted or playing on another device.

kafffee

  • Posts: 173
Re: Problems switching audio device of mixer channel
« Reply #4 on: 9 Feb '23 - 00:13 »
What Do you mean by "both" cases? By code or by combobox? If so, all my devices are connected and switched on, so I should hear it. Further, the devices in the combobox should be in the same order as detected by BASS_GetDeviceInfo (see code in Post 1) but I can double-check again tomorrow.

I think it's something different though: You mentioned the music might be muted, but as a matter of fact, I have a slider that Shows the current playback Position via ChannelGetPosition and it stays at Position 0. Also, my waveform should be moving, as it's also bound to ChannelGetPosition: I have the waveform drawn once when the file is loaded and then have it moving on a canvas, on whose middle I have drawn a vertical Line. So either one should still move in case it's only muted right?

I think it might make sense if I post the complete Sub that handles starting the playback, so you can see in which order/way  I am calling the bass.dll functions. It's a MVVM application with different ViewModels (Code files) so I cannot exclude a threading issue...
« Last Edit: 9 Feb '23 - 00:36 by kafffee »

Ian @ un4seen

  • Administrator
  • Posts: 24936
Re: Problems switching audio device of mixer channel
« Reply #5 on: 9 Feb '23 - 17:50 »
I think it's something different though: You mentioned the music might be muted, but as a matter of fact, I have a slider that Shows the current playback Position via ChannelGetPosition and it stays at Position 0. Also, my waveform should be moving, as it's also bound to ChannelGetPosition: I have the waveform drawn once when the file is loaded and then have it moving on a canvas, on whose middle I have drawn a vertical Line. So either one should still move in case it's only muted right?

Yes, if the position isn't advancing then that sounds like playback failed to start rather than it being muted. Is the BASS_ChannelPlay/Start call reporting success in its return value? Also, is BASS_ChannelGetPosition definitely returning 0, or could it be returning -1, ie. failing? Check what the error code is (with BASS_ErrorGetCode) if either is failing.

kafffee

  • Posts: 173
Re: Problems switching audio device of mixer channel
« Reply #6 on: 9 Feb '23 - 19:26 »
This is really weird: I get error code 0 on the following calls:

BASS_ChannelPlay
BASS_StreamAddChannel
BASS_ChannelSetDevice
BASS_ChannelGetPosition

I don't you if I understood you right:

Quote
When there are multiple initialized devices, stream creation functions (including BASS_Mixer_StreamCreate) will use the current thread's device context, which can be set via BASS_SetDevice. If your app is single threaded then you could put the BASS_SetDevice call next to the BASS_ChannelSetDevice call. Otherwise, you should put it just before the BASS_Mixer_StreamCreate call:

My setup is like this:

Code: [Select]
Public Class SettingsViewModel
Sub New()
Bass.BASS_SetDevice(SpeakersIndex)
        MainModule.mixer = Un4seen.Bass.AddOn.Mix.BassMix.BASS_Mixer_StreamCreate(44100, 2, BASSFlag.BASS_DEFAULT Or BASSFlag.BASS_SAMPLE_FLOAT)
 Bass.BASS_ChannelSetDevice(MainModule.mixer, SpeakersIndex)
        Bass.BASS_ChannelPlay(MainModule.mixer, False)
End Sub

Private _SpeakersIndex As Integer
    Public Property SpeakersIndex As Integer
        Get
            Return _SpeakersIndex
        End Get
        Set(value As Integer)
            _SpeakersIndex = value
            MainModule.MySettings.SpeakersIndex = value
            Dim success As Boolean = Bass.BASS_ChannelSetDevice(MainModule.mixer, value)     
            RaisePropertyChanged()
        End Set
    End Property
End Class

This works so far. When I add a source channel to MainModule.mixer first, I can switch devices by using the combobox, whose SelectedIndex is bound to SpeakersIndex.
But when I try switching devices on initialization by code or if I use the combobox before playback, it won't start playing...

Chris

  • Posts: 2115
Re: Problems switching audio device of mixer channel
« Reply #7 on: 9 Feb '23 - 20:49 »
try to catch the current Device that bass will use via
BASS_ChannelGetDevice(MainModule.mixer);

kafffee

  • Posts: 173
Re: Problems switching audio device of mixer channel
« Reply #8 on: 10 Feb '23 - 06:26 »
@Chris
It returns 2, just like it's supposed to be when I switch devices first. If not, it returns 0...

This is how I start playback:

Code: [Select]
Public Sub Abspielen_Execute(obj As Object)

If IsNewSong = True Then

Bass.BASS_ChannelStop(stream)                            'stop source channel
BassMix.BASS_Mixer_ChannelRemove(MainModule.streamfx(WhichDeck - 1))
If stream <> 0 Then Bass.BASS_StreamFree(stream)          'free source channel
Dim AudioFile As System.IO.FileStream = System.IO.File.OpenRead(AktuelleMP3Info.FileName)       'load audio file
Dim Length As Long = AudioFile.Length
Dim Buffer(CInt(Length)) As Byte
AudioFile.Read(Buffer, 0, CInt(Laenge))
AudioFile.Close()

Dim Handle As System.Runtime.InteropServices.GCHandle = Runtime.InteropServices.GCHandle.Alloc(Buffer, Runtime.InteropServices.GCHandleType.Pinned)
stream = Bass.BASS_StreamCreateFile(Handle.AddrOfPinnedObject(), 0L, Length, BASSFlag.BASS_STREAM_PRESCAN Or BASSFlag.BASS_SAMPLE_FLOAT Or BASSFlag.BASS_STREAM_DECODE)
CreateMyWaveForm()
WaveFormTimer.Start()

If MainModule.streamfx(WhichDeck - 1) <> 0 Then Bass.BASS_StreamFree(MainModule.streamfx(WhichDeck - 1))    'free source effects channel

MainModule.streamfx(WhichDeck - 1) = BassFx.BASS_FX_TempoCreate(stream, BASSFlag.BASS_STREAM_DECODE)        'set source effects channel

BassMix.BASS_Mixer_StreamAddChannel(MainModule.mixer, MainModule.streamfx(WhichDeck - 1), BASSFlag.BASS_MIXER_NORAMPIN Or BASSFlag.BASS_MIXER_BUFFER)  'start playing
SyncStreamFreed = New SYNCPROC(AddressOf FreedSync)
Bass.BASS_ChannelSetSync(MainModule.streamfx(WhichDeck - 1), BASSSync.BASS_SYNC_FREE, 0, SyncStreamFreed, IntPtr.Zero)SyncTrackEnd = New SYNCPROC(AddressOf WhenTrackEnded)
Bass.BASS_ChannelSetSync(stream, BASSSync.BASS_SYNC_END, 0, SyncTrackEnd, IntPtr.Zero)
End If
End Sub

Public Sub CreateMyWaveForm()
        MyWaveForm = New WaveForm(AktuelleMP3Info.Dateiname, New WAVEFORMPROC(AddressOf MeineWellenFormCallBackFunktion), Nothing)
        InitializeWaveForm()
        MyWaveFrom.RenderStart(True, BASSFlag.BASS_SAMPLE_FLOAT Or BASSFlag.BASS_STREAM_PRESCAN Or BASSFlag.BASS_DEFAULT)
End Sub

Private Sub InitializeWaveForm()
        MeineWellenForm.ColorBackground = System.Drawing.Color.Black
        [... all the different colors of the wavform are being set here...]
End Sub

Chris

  • Posts: 2115
Re: Problems switching audio device of mixer channel
« Reply #9 on: 10 Feb '23 - 13:24 »
Quote
it returns 0
0 will mean  no sound Device

kafffee

  • Posts: 173
Re: Problems switching audio device of mixer channel
« Reply #10 on: 10 Feb '23 - 13:33 »
@Chris

Yes I know. The problem is not switching the device. The problem is after switching, the music won't start playing...

jpf

  • Posts: 163
Re: Problems switching audio device of mixer channel
« Reply #11 on: 10 Feb '23 - 14:09 »
Maybe try BASS_SetConfig(BASS_CONFIG_DEV_NONSTOP) ?

You said that switching devices worked fine if there was music playing (and heard).

I had a maybe related problem time ago: setting a VST on a mixer while it wasn't playing returned a valid handle but the effect wasn't heard. I didn't want to dig to the root of the problem so I just added a channel playing silence all the time.

Ian @ un4seen

  • Administrator
  • Posts: 24936
Re: Problems switching audio device of mixer channel
« Reply #12 on: 10 Feb '23 - 14:31 »
This is really weird: I get error code 0 on the following calls:

BASS_ChannelPlay
BASS_StreamAddChannel
BASS_ChannelSetDevice
BASS_ChannelGetPosition

It isn't shown in your posted code, so just to be sure, where are you checking the error codes? Note that error codes are thread-specific, so it needs to be checked in the same thread as the failed BASS function call, and just after the failed call (the error code may change if there's another BASS call between).

But when I try switching devices on initialization by code or if I use the combobox before playback, it won't start playing...

Have you initialized the devices before then? Note BASS_SetDevice can only switch to an initialized device.

Please also confirm that you're using the latest BASS and BASSmix versions, ie. what do BASS_GetVersion and BASS_Mixer_GetVersion return? If either isn't the latest then try upgrading, in case the problem is caused by an old bug.

This is how I start playback:
...

Code: [Select]
Public Sub Abspielen_Execute(obj As Object)

If IsNewSong = True Then

Bass.BASS_ChannelStop(stream)                            'stop source channel
BassMix.BASS_Mixer_ChannelRemove(MainModule.streamfx(WhichDeck - 1))
If stream <> 0 Then Bass.BASS_StreamFree(stream)          'free source channel
...

It looks like you're freeing the last decoder every time here, even if it's for a different deck? I would recommend using the BASS_FX_FREESOURCE flag in the BASS_FX_TempoCreate call instead, so that the decoder is freed automatically when the tempo stream is.

Code: [Select]
Dim Handle As System.Runtime.InteropServices.GCHandle = Runtime.InteropServices.GCHandle.Alloc(Buffer, Runtime.InteropServices.GCHandleType.Pinned)

Are you sure this isn't a memory leak, ie. where is this memory being freed? Perhaps it would be best to just pass the filename to BASS_StreamCreateFile instead (of loading to memory) to simplify things, at least until you've got it all working?

kafffee

  • Posts: 173
Re: Problems switching audio device of mixer channel
« Reply #13 on: 11 Feb '23 - 09:44 »
@Ian

I know, BASS_ErrorGetCode calls only right in the next line. The devices are all initialized. I have the following versions:

bass version: 33820679
bassmixer version: 33819136

Thanks fot the hint to free the memory, I just overlooked that in the docs...

jpf's idea with BASS_SetConfig(BASS_CONFIG_DEV_NONSTOP) actually works, so we re through with this I guess :-)

Ian @ un4seen

  • Administrator
  • Posts: 24936
Re: Problems switching audio device of mixer channel
« Reply #14 on: 13 Feb '23 - 12:55 »
If enabling BASS_CONFIG_DEV_NONSTOP prevents the problem then that may mean there's a bug, as the device should usually be resumed automatically when there's data to play. From the posted version numbers, you are currently using old versions of both BASS (2.4.16.7) and BASSmix (2.4.10.0). Please try upgrading to the latest releases and see if BASS_CONFIG_DEV_NONSTOP is still required then. Also try this latest BASS build:

   www.un4seen.com/stuff/bass.zip

If the problem still happens, please try adding BASS_ChannelIsActive(mixer) to your monitoring/logging to see what that is, eg. before and after the BASS_ChannelPlay/BASS_ChannelSetDevice/BASS_Mixer_StreamAddChannel calls. If it's stuck on BASS_ACTIVE_STALLED, are you calling BASS_ChannelSetDevice before or after BASS_Mixer_StreamAddChannel, and does adding the BASS_MIXER_RESUME flag to the BASS_Mixer_StreamCreate call help?

kafffee

  • Posts: 173
Re: Problems switching audio device of mixer channel
« Reply #15 on: 13 Feb '23 - 13:21 »
Okay I will do that. Are the new versions backward compatible, or do I have to do my testing all over again?

I was thinking of switching my app to 64 Bit anyways, so I can use 64 Bit vst plugins, so I could do this one a a time... Are there going to be any issues or do I just have to replace the bass.dll files, addons and bass.net.dll with their 64 Bit versions and then compile as 64 Bit release?

Ian @ un4seen

  • Administrator
  • Posts: 24936
Re: Problems switching audio device of mixer channel
« Reply #16 on: 13 Feb '23 - 16:35 »
It should be fine to simply replace the old BASS DLLs with the new ones, without any code changes.