Jump to content
Sign in to follow this  
VAG

_Bass_ChannelGetData on Recording Source

Recommended Posts

VAG

I've come across BrettF's BASS UDF, so tried to made a small app to directly read the frequency and dB level through my sound card recording source. I'm pumping in audio like 1KHz Sine tone for easily frequency reading.

I have accomplished the part on getting the dB level using _BASS_ChannelGetlevel with some conversion. But for FFT reading, I'm out of idea. I read about some BASS Helper method like Utils.FFTindex2Frequency but can't find it in the UDF. How do I get the return values in Hz?Posted Image

#include "Bass.au3"
#include "BassConstants.au3"
#include <WinAPI.au3>

;Configure Bass Audio Library
;----------------------------
;~ Global Const $bass_dll = DllOpen(@ScriptDir & "\BASS.dll")
Global Const $bass_dll = _BASS_Startup(@ScriptDir & "\bass.dll")
If @error = -1 Then
MsgBox(16, "ERROR!", "Could not load BASS.dll" & @CR & "Terminating... ")
Exit
EndIf
_BASS_SetConfig($BASS_CONFIG_REC_BUFFER, 1000)
_BASS_RecordInit(-1)

$Cnt = 0
$Name = _BASS_RecordGetInputName(0)
While $Name <> ""
$Input = _BASS_RecordGetInput($Cnt)
If BitAND($Input[0], $BASS_INPUT_TYPE_WAVE) Then _BASS_RecordSetInput($Cnt, $BASS_INPUT_TYPE_WAVE, 1)
$Cnt += 1
$Name = _BASS_RecordGetInputName($Cnt)
WEnd

If @OSVersion = "WIN_VISTA" Or @OSVersion = "WIN_2008" Then _BASS_RecordSetInput(0, $BASS_INPUT_TYPE_WAVE, 1)

$RecHandle = _BASS_RecordStart(44100, 2, _WinAPI_MakeLong(0, 10))

While 1

;Read Audio Level
$levels = _BASS_ChannelGetLevel($RecHandle)     ;Get dB Value
$LevelLeftCh = _WinAPI_LoWord($levels)
$LvlLeftPAR = Round(($LevelLeftCh / 32768) * (81/2))    ;EQ level(%)
$LevelLeftCh = Round((20*Log($LevelLeftCh/32768))/Log(10), 3)  ;Convert to dB
$LevelRightCh = _WinAPI_HiWord($levels)
$LvlRightPAR = Round(($LevelRightCh / 32768) * (81/2))    ;EQ level(%)
$LevelRightCh = Round((20*Log($LevelRightCh/32768))/Log(10), 3)  ;Convert to dB

;Read FFT Value
$FFTs = _BASS_ChannelGetData($RecHandle, 0, $BASS_DATA_AVAILABLE) ;Get Freq Value from recording src
$FFTLeftCh = _WinAPI_LoWord($FFTs)
$FFTRightCh = _WinAPI_LoWord($FFTs)

ToolTip( "L-Channel Freq: " & $FFTLeftCh & " R-Channel Freq: " & $FFTRightCh & " L-Channel dB: " & $LevelLeftCh & " R-Channel dB: " & $LevelRightCh, 0, 0)

Sleep(5)

WEnd

Share this post


Link to post
Share on other sites
VAG

Here is what I came out after looking thru un4seen forum, I have removed the dB reading part to simplify the code. The problem I'm facing now is that _Bass_ChannelGetData() always returning 0, so all the subsequence steps to figure out the peak amplitude is useless. Can anyone familiar with BASS udf give me a help?Posted Image

#include "Bass.au3"
#include "BassConstants.au3"
#include <WinAPI.au3>

;Configure Bass Audio Library
;----------------------------
Global Const $bass_dll = _BASS_Startup(@ScriptDir & "\bass.dll")
If @error = -1 Then
MsgBox(16, "ERROR!", "Could not load BASS.dll" & @CR & "Terminating... ")
Exit
EndIf
_BASS_SetConfig($BASS_CONFIG_REC_BUFFER, 1000)
_BASS_RecordInit(-1)

$Cnt = 0
$Name = _BASS_RecordGetInputName(0)
While $Name <> ""
$Input = _BASS_RecordGetInput($Cnt)
If BitAND($Input[0], $BASS_INPUT_TYPE_WAVE) Then _BASS_RecordSetInput($Cnt, $BASS_INPUT_TYPE_WAVE, 1)
$Cnt += 1
$Name = _BASS_RecordGetInputName($Cnt)
WEnd

If @OSVersion = "WIN_7" Or @OSVersion = "WIN_VISTA" Or @OSVersion = "WIN_2008" Then _BASS_RecordSetInput(0, $BASS_INPUT_TYPE_WAVE, 1)

$RecHandle = _BASS_RecordStart(44100, 2, _WinAPI_MakeLong(0, 10))

;Get FFT Data from Recording Channel
;----------------------------
While 1

;Read FFT Value
Dim $FFT[2048]
Dim $tempFFT[2048]

If _BASS_ChannelGetData($RecHandle, $tempFFT, $BASS_DATA_FFT4096) = 0 Then ;Get freq data from Rec Src
  ConsoleWriteError("_Bass_ChannelGetData : " & @error & @CRLF)
EndIf

For $a=1 to 2047 Step 1         ;Samples = 4096 / 2
  $FFT[$a] += $tempFFT[$a]      ;accumulate results (skip DC)
Next

Dim $peak = 0
For $a=1 to 2047 Step 1
  If $FFT[$a] > $FFT[$peak] Then
   $peak = $a            ;find the highest amplitude
  EndIf
Next

$SampleRate = _BASS_ChannelGetInfo($RecHandle)
$freq = $peak * $SampleRate[0] / 4096       ;freq = bin * samplerate / fftsize

ToolTip("Sample Rate: " & $SampleRate[0] & @CRLF & "Channel Freq: " & $freq)

Sleep(100)

WEnd

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.