Author Topic: Scanning peaks occasionally freezes our code  (Read 199 times)

Chris Oakley

  • Posts: 17
Hope someone can shed some light on this because I'm tearing my hair out here. I have written my own routine to scan for a threshold. A bit like the DetectCuePoints routine. I would say I stopped using the DetectCuePoints routine because every now and then it would just stop working and hold up the app. However there was no reason for it and no errors. It's like the internal routine itself had locked.

So I wrote this:

Code: [Select]
    Public Function ThresholdPosition(ByVal Filename As String, ByVal ThresholdDB As Integer, ByVal Reverse As Boolean) As Double

        'Return ThredholdDB position in Milliseconds
        Dim Position As Double = -1
        Dim Stream As Integer = Bass.BASS_StreamCreateFile(Filename, 0, 0, BASSFlag.BASS_STREAM_DECODE Or BASSFlag.BASS_SAMPLE_FLOAT Or BASSFlag.BASS_STREAM_PRESCAN)
        If Stream <> 0 Then
            Dim _Threshold As Integer = 32767 * Math.Pow(10, (ThresholdDB / 20))
            Dim streamFX As Integer = BassFx.BASS_FX_ReverseCreate(Stream, 2.0F, BASSFlag.BASS_FX_FREESOURCE Or BASSFlag.BASS_STREAM_DECODE)
            Bass.BASS_ChannelSetAttribute(streamFX, BASSAttribute.BASS_ATTRIB_REVERSE_DIR, CSng(IIf(Reverse, BASSFXReverse.BASS_FX_RVS_REVERSE, BASSFXReverse.BASS_FX_RVS_FORWARD)))
            Dim blockLength As Integer = CInt(Bass.BASS_ChannelSeconds2Bytes(Stream, 0.3F))
            Dim buffer As Short() = New Short(blockLength / 2 - 1) {}

            Dim StartPos As Integer = 0
            Dim pos As Long = Bass.BASS_ChannelGetPosition(streamFX)
            StartPos = Bass.BASS_ChannelBytes2Seconds(streamFX, pos) * 1000
            LogMe("StartPosition=" & StartPos)

            Dim TimeOut As Int16 = 0
            Dim TimerStart = My.Computer.Clock.TickCount ' Start tick count
            Dim Level As Integer = 0
                If My.Computer.Clock.TickCount - TimerStart > 1000 Then
                    TimeOut += 1
                    TimerStart = My.Computer.Clock.TickCount
                    LogMe("ThresholdPosition TimeOut Pass " & TimeOut)
                End If
                If TimeOut > 30 Then
                    LogMe("ThreholdPosition TimeOut Exhausted. Exiting routine.")
                    Exit Do
                End If

                Level = Bass.BASS_ChannelGetLevel(streamFX)
                If Level < 0 Then
                    Position = StartPos
                    Exit Do
                End If

                Dim left As Integer = Utils.LowWord32(Level)
                Dim right As Integer = Utils.HighWord32(Level)
                If Math.Max(left, right) > _Threshold Then
                    pos = Bass.BASS_ChannelGetPosition(streamFX)
                    Position = Bass.BASS_ChannelBytes2Seconds(streamFX, pos) * 1000
                    Exit Do
                End If

            Loop Until Level < 0
            Stream = 0
        End If
        Return Position

End Function

Now there are a few things I should mention about this routine. The LogMe is our own sub so we can log to a file what's going on. That way we're able to see exactly what things are doing. I use the TickCount to provide a method of timeout in case it gets stuck in a loop, which it shouldn't do, I'm sure you'll agree.

I call this routine 3 times in quick succession to detect 3 different threshold points on a file. The TrimIn which is where the audio actually starts past the initial silence. Extro which is a reasonable fade point in the audio at the end and TrimOut which is like TrimIn but at the other end of the file.

The interesting thing is it always hangs up on the 3rd call when it hangs up and it's driving me mad. Any ideas would be great.

Ian @ un4seen

  • Administrator
  • Posts: 20387
Re: Scanning peaks occasionally freezes our code
« Reply #1 on: 20 Sep '17 - 17:15 »
Do you know where in the code it is getting stuck, eg. if you run it in the debugger and "break" when it gets stuck? When it gets stuck, can you reproduce it again with the same parameters, or is it random? To narrow it down, does it still happen if you remove the BASS_FX_ReverseCreate call?

Chris Oakley

  • Posts: 17
Re: Scanning peaks occasionally freezes our code
« Reply #2 on: 20 Sep '17 - 19:36 »
I don't know yet. I'm doing a lot of debugging myself but sadly this is running outside of the debugger so it's hard to know what's going on. What I have noticed is that it always stops on the 3rd call of the code in quick succession so I've moved the 3rd pass to be the first to see if that makes a difference and to see if it gets stuck on that or still on the 3rd.

It appears to be random but as I always say, computers aren't very good at random, there will be a pattern, it's just spotting it.