Author Topic: Issue with Bass Init and USB devices  (Read 483 times)

ldami

  • Posts: 13
Issue with Bass Init and USB devices
« on: 27 Nov '21 - 00:11 »
Hello,

I'm trying to make a WPF (C#) application with Bass lib to make dynamic DMX lights.
All is going well with DMX and Bass lib separately, but when I call "Bass.Init(...)", SerialPort (used by DMX protocol) is not working anymore. I don't have any errors, but my lights are not working.

Is there anything in Bass.Init that could make issues on SerialPort ?

I'm using ManagedBass wrapper.

Code: [Select]
Bass.Init(0, _SamplingRate, DeviceInitFlags.Stereo);
Thanks a lot.
Best regards

Ian @ un4seen

  • Administrator
  • Posts: 24054
Re: Issue with Bass Init and USB devices
« Reply #1 on: 29 Nov '21 - 13:35 »
That sounds strange. I'm not sure how BASS could be affecting the serial port, especially when using device=0 (the "No Sound" device) in the BASS_Init call. Is the problem definitely happening within the BASS_Init call? If so, what happens if you call BASS_GetDeviceInfo first/instead? Does the problem then begin in that call? If so, what about if you call BASS_GetVersion?

If you try running one of the pre-compiled examples included in the BASS package (C\BIN folder), does the serial port stop working then too?

ldami

  • Posts: 13
Re: Issue with Bass Init and USB devices
« Reply #2 on: 1 Dec '21 - 21:18 »
Hi,
Thanks for the reply.

I have just test with only BASS_GetDeviceInfo and it appears that calling this function disturbs the Serial communication. The SerialPort stays open and communication continues correctly after that.
I don't have any Serial analyser so it's difficult to show what is happening..

Running my program and a program from exemples (3dtest, basstest, livefx, ...) does not disturb the SerialPort at all.
Could it be an issue from the C# wrapper ? I took a look at the source code and it seems to not add anything !

Regards,

Ian @ un4seen

  • Administrator
  • Posts: 24054
Re: Issue with Bass Init and USB devices
« Reply #3 on: 2 Dec '21 - 15:26 »
I have just test with only BASS_GetDeviceInfo and it appears that calling this function disturbs the Serial communication. The SerialPort stays open and communication continues correctly after that.
I don't have any Serial analyser so it's difficult to show what is happening..

Just to be sure, did you mean to say that calling BASS_GetDeviceInfo doesn't disturb the serial communication, ie. it only happens when calling BASS_Init? The reason I ask you to check BASS_GetDeviceInfo is because both it and BASS_Init will enumerate available soundcards, and BASS_Init doesn't otherwise do anything hardware-related when called with device=0. So if the problem is only happening in the BASS_Init call then I can't think of any possible reason for that. Might there be some other code in your app at around the same time as the BASS_Init call that's causing the problem?

Running my program and a program from exemples (3dtest, basstest, livefx, ...) does not disturb the SerialPort at all.
Could it be an issue from the C# wrapper ? I took a look at the source code and it seems to not add anything !

In that case, perhaps you could try creating 2 little C# apps - one that initializes BASS and one that uses the serial port, and then see if you can run them successfully at the same time?

ldami

  • Posts: 13
Re: Issue with Bass Init and USB devices
« Reply #4 on: 2 Dec '21 - 22:08 »
Just to be sure, did you mean to say that calling BASS_GetDeviceInfo doesn't disturb the serial communication, ie. it only happens when calling BASS_Init? The reason I ask you to check BASS_GetDeviceInfo is because both it and BASS_Init will enumerate available soundcards, and BASS_Init doesn't otherwise do anything hardware-related when called with device=0. So if the problem is only happening in the BASS_Init call then I can't think of any possible reason for that. Might there be some other code in your app at around the same time as the BASS_Init call that's causing the problem?

No ! I am saying that BASS_GetDeviceInfo disturb the SerialPort, but only once.
Bass_Init block the SerialPort until I call Bass_Free.
My USB device is a FTDI, I use it to work with DMX. When I call BASS_GetDeviceInfo the light blink once, but if I call BASS_Init the light go off and go on on BASS_Free.

In that case, perhaps you could try creating 2 little C# apps - one that initializes BASS and one that uses the serial port, and then see if you can run them successfully at the same time?

I can try that, but I won't create two application because the goal is to synchronize lights and audio !

If you want debug, there is my program: https://github.com/LDami/MIDI-to-DMX
Check the BeatDetector class.

Do you plan to make BASS open source in the future ?
« Last Edit: 2 Dec '21 - 22:28 by ldami »

Ian @ un4seen

  • Administrator
  • Posts: 24054
Re: Issue with Bass Init and USB devices
« Reply #5 on: 3 Dec '21 - 16:47 »
No ! I am saying that BASS_GetDeviceInfo disturb the SerialPort, but only once.
Bass_Init block the SerialPort until I call Bass_Free.
My USB device is a FTDI, I use it to work with DMX. When I call BASS_GetDeviceInfo the light blink once, but if I call BASS_Init the light go off and go on on BASS_Free.

Ah, I see. I guess the brief interruption in BASS_GetDeviceInfo is happening when it's enumerating the soundcards, but I don't see what could be keeping it after BASS_Init - are you still using device=0 in that call? Of course, even just enumerating soundcards shouldn't really affect it, but perhaps there's something odd about that particular device/driver. The other puzzling thing is why it doesn't happen when you run the pre-compiled examples.

In that case, perhaps you could try creating 2 little C# apps - one that initializes BASS and one that uses the serial port, and then see if you can run them successfully at the same time?

I can try that, but I won't create two application because the goal is to synchronize lights and audio !

Yes, I just meant for you to try that to see whether it works like when you ran the pre-compiled examples and perhaps narrow down what/where the problem is.

Please also confirm what Windows version you're using.

Do you plan to make BASS open source in the future ?

Things can change in future, but there are no plans for that currently.

ldami

  • Posts: 13
Re: Issue with Bass Init and USB devices
« Reply #6 on: 3 Dec '21 - 18:06 »
Hi,

I have tried with a new project, containing only BASS implementation, and it does not disturb the main project and its SerialPort.
So the issue seems to appear only when the SerialPort and Bass are in the same executable.

I am using Windows 10 64bits.
My projects are running on .NET framework 4.8.

Ian @ un4seen

  • Administrator
  • Posts: 24054
Re: Issue with Bass Init and USB devices
« Reply #7 on: 6 Dec '21 - 18:03 »
OK. To check whether it's only happening in .Net, would it be possible for you to create a little test app that does both things in another language, eg. C/C++ or Delphi?

I just had a look at the code in your GitHub, and I noticed that it's also initializing BASSWASAPI. Are you sure the problem isn't happening in that call rather than the BASS_Init call, ie. does it still happen if you remove that?

ldami

  • Posts: 13
Re: Issue with Bass Init and USB devices
« Reply #8 on: 6 Dec '21 - 19:49 »
I have tested with disabling Bass_Wasapi and the issue is still there. The issue is with Bass_Init.
I don't really know how to do the same thing on Cpp or Delphi, I don't know these languages  :(

Ian @ un4seen

  • Administrator
  • Posts: 24054
Re: Issue with Bass Init and USB devices
« Reply #9 on: 7 Dec '21 - 14:04 »
OK. A couple of other things that you could try to narrow it down are running your app outside of the IDE (ie. as a standalone EXE) and running it on another PC. Does the problem still happen in both cases?

ldami

  • Posts: 13
Re: Issue with Bass Init and USB devices
« Reply #10 on: 8 Dec '21 - 21:23 »
Tried on 2 computers, with debugger and standalone: same behavior.
It's not a debugger issue

Ian @ un4seen

  • Administrator
  • Posts: 24054
Re: Issue with Bass Init and USB devices
« Reply #11 on: 9 Dec '21 - 15:17 »
OK. It still seems highly unlikely to me that the problem is caused by BASS_Init. I suspect it must be something else in the code following that. To test this theory, please try stripping-out/disabling all of the other code in your BeatDetector.cs file (leaving only the BASS_Init call) and see if the problem still happens then.

ldami

  • Posts: 13
Re: Issue with Bass Init and USB devices
« Reply #12 on: 9 Dec '21 - 21:54 »
I created a new branch in my git:
https://github.com/LDami/MIDI-to-DMX/tree/Bass-Init-Only

Same issue, SerialPort isn't working when Bass is initialized but it's working well after calling the Free method.

Ian @ un4seen

  • Administrator
  • Posts: 24054
Re: Issue with Bass Init and USB devices
« Reply #13 on: 10 Dec '21 - 12:28 »
Can you try removing all of the other code from BeatDetector.cs? The reason I ask that is because the other code will depend on BASS initialization (eg. BASSWASAPI requires it), so removing BASS_Init will effectively disable the other code too. Therefore it doesn't necessarily mean the problem is caused by BASS_Init just because it goes away when you remove that call.

ldami

  • Posts: 13
Re: Issue with Bass Init and USB devices
« Reply #14 on: 10 Dec '21 - 18:38 »
In the new branch, I have duplicated BeatDetector.cs into BeatDetectorInitOnly.cs.
I have let only BASS_Init in the new file and then change the main form to call the new class.

Code: [Select]
using ManagedBass;
using System;


namespace DMX_MIDI
{
    public sealed class BeatDetectorInitOnly
    {
        public BeatDetectorInitOnly(int SamplingRate = 44100)
        {
            // Initialize BASS
            bool result = Bass.Init(0, SamplingRate, DeviceInitFlags.Stereo);

            if (!result)
            {
                throw new BassException(Bass.LastError);
            }
            else
                Console.WriteLine("Bass.Init done");
        }

        public void Free()
{
            Bass.Free();
}

    }
}

The SerialPort is not working when I call the Bass.Init method, and works again after calling Free();

Ian @ un4seen

  • Administrator
  • Posts: 24054
Re: Issue with Bass Init and USB devices
« Reply #15 on: 13 Dec '21 - 16:09 »
Ah. Sorry, I didn't notice that you had added a new source file there.

I'm baffled then. I really can't see how BASS_Init could be affecting your serial port. The fact that the problem doesn't happen if you use BASS in another app at the same time also seems to suggest that the serial port isn't being affected, but rather it's your app's processing that's affected, eg. the flow changes depending on whether BASS has been initialized? That's the only explanation I can think of. If you log your calls to the serial port, are they identical (including result) with and without BASS_Init?

You could also try using device=-1 or device=100 in the BASS_Init call, to see if the problem also happens when using a real output device or an invalid one (not only the "No Sound" device). I doubt it would make any difference but you could also try using BASS.Net instead of ManagedBass, to see if it does.

rv

  • Posts: 344
Re: Issue with Bass Init and USB devices
« Reply #16 on: 14 Dec '21 - 10:11 »
Sometimes the USB port has not enough power for many USB devices
Bass_Init may power some USB audio devices
Maybe you can try using powered USB hub?

ldami

  • Posts: 13
Re: Issue with Bass Init and USB devices
« Reply #17 on: 16 Dec '21 - 18:02 »
It worked for 2 minutes >.<
(proof: https://youtu.be/Ui-MKETZLps)

I thought it was because of the ReadTimeout that is been changed by Bass:

Code: [Select]
3207 IRP_MJ_DEVICE_CONTROL - Request operates a serial port (COM3)
    STATUS_SUCCESS
        IOCTL_SERIAL_SET_TIMEOUTS - Request sets the timeout value's that the driver uses with read and write requests
            ReadIntervalTimeout         - -1
            ReadTotalTimeoutMultiplier  - -1
            ReadTotalTimeoutConstant    - 500
            WriteTotalTimeoutMultiplier - 0
            WriteTotalTimeoutConstant   - 0

But it seems not be the cause of the issue.

I have monitored the serial port, and data are exactly the same with and without Bass.
I don't understand  ???
« Last Edit: 16 Dec '21 - 23:46 by ldami »

ldami

  • Posts: 13
Re: Issue with Bass Init and USB devices
« Reply #18 on: 19 Dec '21 - 00:57 »
Hello there,

I have managed to fix the issue with adding a small delay before each DMX message.
Without Bass, my threading is looping about 35 time per seconds, but with Bass its looping about 45 time per seconds !

My thread who is managing the SerialPort is faster when Bass is initialized  ???
This is very strange !

I add a Thread.Sleep to slow it a little bit and it works.

Thanks for your help :)

saga

  • Posts: 2565
Re: Issue with Bass Init and USB devices
« Reply #19 on: 19 Dec '21 - 15:52 »
BASS will most probably increase the Windows timer precision to make its own timing more accurate, but this affects the entire program and not just BASS itself (in earlier Windows versions it would also affect other programs); your software was essentially relying on a less precise clock.

Ian @ un4seen

  • Administrator
  • Posts: 24054
Re: Issue with Bass Init and USB devices
« Reply #20 on: 20 Dec '21 - 15:09 »
BASS_Init will indeed increase the timer resolution, to make the timing of non-mixtime sync callbacks more precise. If wanted, you can disable that via the undocumented BASS_CONFIG_NOTIMERES option, like this:

Code: [Select]
#define BASS_CONFIG_NOTIMERES 29
BASS_SetConfig(BASS_CONFIG_NOTIMERES, 1);

Anyway, it's great that the mystery is solved!