You were right about that Ian. Changing it made it work. Now I am pulling my hairs out to get the 'end silece' detection workiing. It somehow alway returns 0. If anybody could have a look it would be greatly appreciated. Also I don't know what a feasible value for Cueout should be:
Private Function GetQueuePointsFromFile(myfile As String) As Tuple(Of Long, Long)
Dim bassstream As Integer = Bass.BASS_StreamCreateFile(myfile, 0, 0, BASSFlag.BASS_STREAM_DECODE Or BASSFlag.BASS_SAMPLE_FLOAT)
If bassstream <> 0 Then
Dim buffer As Single() = New Single(49999) {}
Dim cueIn As Long = 0L
Dim cueOut As Long = 0L
Dim bufferSize As Integer = 4096
Const InThreshold As Single = 0.005F
Const OutThreshold As Double = 0.005F
Try
' Detect start silence
Dim bytesRead As Integer
Do
bytesRead = Bass.BASS_ChannelGetData(bassstream, buffer, bufferSize)
If bytesRead <= 0 Then Exit Do
Dim silentSamples As Integer = 0
For i As Integer = 0 To bytesRead - 1 Step 4
If Math.Abs(buffer(i)) < InThreshold Then
silentSamples += 1
Else
Exit Do
End If
Next
cueIn += silentSamples * 4
Loop While bytesRead > 0
' Detect trailing silence
Dim lengthInBytes As Long = Bass.BASS_ChannelGetLength(bassstream, BASSMode.BASS_POS_BYTES)
Dim position As Long = lengthInBytes - bufferSize
Dim trailingSilenceStart As Long = 0
While position >= 0
Bass.BASS_ChannelSetPosition(bassstream, position, BASSMode.BASS_POS_BYTES)
bytesRead = Bass.BASS_ChannelGetData(bassstream, buffer, bufferSize)
If bytesRead <= 0 Then
Console.WriteLine("Failed to read bytes from the stream")
Exit While
End If
Dim hasAudio As Boolean = False
For i As Integer = 0 To bytesRead - 1
If Math.Abs(buffer(i)) > OutThreshold Then
hasAudio = True
Exit For
End If
Next
If hasAudio Then
trailingSilenceStart = position
Exit While
End If
position -= bufferSize
End While
cueOut = trailingSilenceStart
Console.WriteLine("leading: " & Bass.BASS_ChannelBytes2Seconds(bassstream, cueIn))
Console.WriteLine("trailing: " & Bass.BASS_ChannelBytes2Seconds(bassstream, cueOut))
Catch ex As Exception
Console.WriteLine("Error: " & ex.ToString())
End Try
Bass.BASS_StreamFree(bassstream)
Else
Console.WriteLine("Failed to create BASS stream")
End If
Return Tuple.Create(cueIn, cueOut)
End Function