Jump to content

Easy To Use - Bass Example (by nend)


Recommended Posts

Easy To Use - Bass Example (by nend) with _BASSChannelGetTags()

 

nend says :

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

Global $BASS_PAUSE_POS

Func Example()

    Local $MusicHandle

    _Audio_init_start()

    $MusicHandle = _Set_url("http://ice1.somafm.com/groovesalad-128.mp3")
    _Set_volume(50)

    _Audio_play($MusicHandle)

    while 1
        ConsoleWrite(_Get_streamtitle($MusicHandle) & @CRLF)
        Sleep(1000)
    WEnd

    _Audio_init_stop($MusicHandle)

EndFunc

Example()

Func _Audio_init_start()
    If _BASS_STARTUP(@ScriptDir & "\bass.dll") Then
        If _BASS_Init(0, -1, 44100, 0) Then
            If _BASS_SetConfig($BASS_CONFIG_NET_PLAYLIST, 1) = 0 Then
                SetError(3)
            EndIf
        Else
            SetError(2)
        EndIf
    Else
        SetError(@error)
    EndIf
EndFunc

Func _Set_buffer($buffer)
    _BASS_SetConfig($BASS_CONFIG_NET_BUFFER, $buffer)
EndFunc

Func _Audio_stop($MusicHandle)
    _BASS_ChannelStop($MusicHandle)
EndFunc

Func _Audio_play($MusicHandle)
    _BASS_ChannelPlay($MusicHandle, 1)
EndFunc

Func _Audio_pause($MusicHandle)
    If _Get_playstate($MusicHandle) = 2 Then
        $BASS_PAUSE_POS = _BASS_ChannelGetPosition($MusicHandle, $BASS_POS_BYTE)
        _BASS_ChannelPause($MusicHandle)
    ElseIf _Get_playstate($MusicHandle) = 3 Then
        _Audio_play($MusicHandle)
        _BASS_ChannelSetPosition($MusicHandle, $BASS_PAUSE_POS, $BASS_POS_BYTE)
    EndIf
EndFunc

Func _Audio_init_stop($MusicHandle)
    _BASS_StreamFree($MusicHandle)
    _BASS_Free()
EndFunc

Func _Set_url($file)
    If FileExists($file) Then
        $MusicHandle = _BASS_StreamCreateFile(False, $file, 0, 0, 0)
    Else
        $MusicHandle = _BASS_StreamCreateURL($file, 0, 1)
    EndIf
    If @error Then
        Return SetError(1)
    EndIf
    Return $MusicHandle
EndFunc

Func _Get_pos($MusicHandle)
    $current = _BASS_ChannelGetPosition($MusicHandle, $BASS_POS_BYTE)
    Return Round(_Bass_ChannelBytes2Seconds($MusicHandle, $current))
EndFunc

Func _Get_len($MusicHandle)
    $current = _BASS_ChannelGetLength($MusicHandle, $BASS_POS_BYTE)
    Return Round(_Bass_ChannelBytes2Seconds($MusicHandle, $current))
EndFunc

Func _Set_pos($MusicHandle, $seconds)
    _BASS_ChannelSetPosition($MusicHandle, _BASS_ChannelSeconds2Bytes($MusicHandle, $seconds), $BASS_POS_BYTE)
EndFunc

Func _Set_volume($volume)
    _BASS_SetConfig($BASS_CONFIG_GVOL_STREAM, $volume * 100)
EndFunc

Func _Get_volume()
    Return _BASS_GetConfig($BASS_CONFIG_GVOL_STREAM) / 100
EndFunc

Func _Get_bitrate($MusicHandle)
    $a = Round(_Bass_ChannelBytes2Seconds($MusicHandle, _BASS_ChannelGetLength($MusicHandle, $BASS_POS_BYTE)))
    $return = Round(_BASS_StreamGetFilePosition($MusicHandle, $BASS_FILEPOS_END) * 8/ $a/ 1000)
    If StringInStr($return, "-") Then
        $return = _BASS_StreamGetFilePosition($MusicHandle, $BASS_FILEPOS_END) * 8 / _BASS_GetConfig($BASS_CONFIG_NET_BUFFER)
    EndIf
    Return $return
EndFunc

Func _Get_playstate($MusicHandle)
    Switch _BASS_ChannelIsActive($MusicHandle)
        Case $BASS_ACTIVE_STOPPED
            $returnstate = 1
        Case $BASS_ACTIVE_PLAYING
            $returnstate = 2
        Case $BASS_ACTIVE_PAUSED
            $returnstate = 3
        Case $BASS_ACTIVE_STALLED
            $returnstate = 4
    EndSwitch
    Return $returnstate
EndFunc

Func _Get_streamtitle($MusicHandle)
    $pPtr = _BASS_ChannelGetTags($MusicHandle, $BASS_TAG_META)
    $sStr = _BASS_PtrStringRead($pPtr)
    If StringInStr($sStr, ";") Then
        $infosplit = StringSplit($sStr, ";")
        $infosplit[1] = StringReplace($infosplit[1], "'", "")
        $infosplit[1] = StringReplace($infosplit[1], "StreamTitle=", "")
        If StringInStr($infosplit[1], "-") Then
            Return $infosplit[1]
        EndIf
    EndIf
EndFunc

You need :

1 dll : bass.dll source : http://www.un4seen.com/bass.html (v2.4.12.1)

2 au3 : bass.au3 source : bass related forum post (patch 9.2/Constant from 9.0)

Link to post
Share on other sites
  • 4 years later...

had a little fun with the gui

bass_audio_gui_2.png

#include <GUIConstantsEx.au3>
#include <Array.au3>
#include "StringSize To PixelSize\StringSize.au3"   ;https://www.autoitscript.com/forum/topic/114034-stringsize-m23-new-version-16-aug-11/
#include <GuiSlider.au3>

;~ #include <Bass.au3>
;~ #include <BassConstants.au3>
#include "BASS.au3\BASS\bass.au3"
#include "BASS.au3\BASS\BassConstants.au3"

Global $_THE_URL = "http://ice1.somafm.com/groovesalad-128.mp3"
Global Const $_DEFAULT_VOLUMN   = 7
Global $_BASS_PAUSE_POS
Global $_AUDIO_HANDLE
Global $_VOLUME_STATE
Global $_PLAY_PAUSE_STATE       = 1
Global $_LOAD_STATE             = 0
Global $_ACTIVE_AUDIO_STREAM
Global $_AUDIO_LENGTH
Global $_BASS_FOLDER            = 'BASS.au3\BASS'

; experimental - not uptodate
;~ HotKeySet("{F2}", "_CtrlShortcut")          ;VolUp
;~ HotKeySet("{F3}", "_CtrlShortcut")          ;VolDown
;~ HotKeySet("{F4}", "_CtrlShortcut")          ;Stop Music
;~ HotKeySet("{F5}", "_CtrlShortcut")          ;Pause Music
;~ HotKeySet("{F8}", "_CtrlShortcut")          ;Exit

HotKeySet("{ESC}", "_Terminate")          ;string2pixelsize


Func _Terminate()
  _terminate_audio_handle()
  Exit
EndFunc

; experimental - not uptodate
Func _CtrlShortcut()
    Switch @HotKeyPressed
        Case "{F2}"
;~             Send("{VOLUME_UP}")
            Local $iCurrentVol = _Get_volume() + 5
            If $iCurrentVol < 100 Then
              _Set_volume($iCurrentVol)
            Else
              _Set_volume(100)
            EndIf
            ConsoleWrite('>->'&@ScriptLineNumber&': '&@HotKeyPressed&' VolumnUp: '&$iCurrentVol&@CRLF) ;Blue/Cyan
            
        Case "{F3}"
;~             Send("{VOLUME_DOWN}")
            Local $iCurrentVol = _Get_volume() - 5
            If $iCurrentVol > 0 Then
              _Set_volume($iCurrentVol)
            Else
              _Set_volume(0)
            EndIf
            ConsoleWrite('+>+'&@ScriptLineNumber&': '&@HotKeyPressed&' VolumnDown: '&$iCurrentVol&@CRLF) ;Green/Grey
            
        Case "{F4}"
;~             Send("{VOLUME_MUTE}")
            _Audio_stop($_AUDIO_HANDLE)
            ConsoleWrite('-->'&@ScriptLineNumber&': '&@HotKeyPressed&' Stop Audio'&@CRLF) ;Yellow/Orange
             
        Case "{F5}"
;~             Send("{VOLUME_PAUSE}")
            _Audio_pause($_AUDIO_HANDLE)
            ConsoleWrite('   '&@ScriptLineNumber&': '&@HotKeyPressed& 'Pause Audio'&@CRLF) ;White
            
        Case "{F8}"
            ConsoleWrite('!>!'&@ScriptLineNumber&': '&@HotKeyPressed& 'Exit Audio Player'&@CRLF) ;Read/Brown
            
            _terminate_audio_handle()
            Exit
    EndSwitch
EndFunc


; gui
Global $hGUI                    = GUICreate("BASS.au3 UDF Audio Player", 350, 200)

; volumn
Global $iBarW = 330
Global $cAudioVolLabel          = GUICtrlCreateLabel("", 10, 10, $iBarW, 20)
;~ Global $cAudioVolCtrl        = GUICtrlCreateProgress(10, 10, $iBarW, 20)
Global $cAudioVolCtrl           = GUICtrlCreateSlider(10, 10, $iBarW, 20)
; volum slider numeration
Local $iSliderWidth                   = $iBarW
Local $iSliderX                       = 17
Local $iSliderMin                     = 0
Local $iSliderMax                     = 100   ; <<< length ajd
Local $iTicInterval                   = 25    ; <<< frequency ajd
Local $iAlignIndicatorToInterval      = 26    ; <<< match up numberated to marker on slider ajd
Local $iLabelPos                      = Floor(($iSliderWidth - $iAlignIndicatorToInterval) * $iTicInterval / ($iSliderMax - $iSliderMin))
Local $iLabelCount                    = $iSliderMax / $iTicInterval
Local $iLabelWidth                    = 20
_GUICtrlSlider_SetRange($cAudioVolCtrl, 0, $iSliderMax)
_GUICtrlSlider_SetTicFreq($cAudioVolCtrl, $iTicInterval)
For $i = 0 To $iLabelCount + 1
    Local $bOverspill       = False
    ; Calculate tic value
    Local $iLabelValue      = $i * $iTicInterval
    ; Check if over max
    If $iLabelValue >= $iSliderMax Then
        $bOverspill         = True
        $iLabelValue        = $iSliderMax
    EndIf
    GUICtrlCreateLabel($iLabelValue, $iSliderX + ($i * $iLabelPos), 35, $iLabelWidth, 15)
    If $bOverspill Then ExitLoop
Next

; tooltip/track
Local $iLabelW = 1000 ; to keep long str as 1 liner
;~ Local $iLabelW = 100 ; to keep long str as 1 liner
Global $cAudioTitle            = GUICtrlCreateLabel("", 10, 65, $iLabelW, 20)

; control
Local $iBtnL        = 10
Local $iBtnT        = 100
Local $iBtnGap      = 5
Local $iItemCount   = 5
Local $iBtnW        = ($iBarW / $iItemCount) - $iBtnGap
Local $iBtnSpc      = $iBtnW + $iBtnGap
Local $iBtnH        = 20
Global $cAudioLoad      = GUICtrlCreateButton("Load",       10                  , $iBtnT, $iBtnW, $iBtnH)
Global $cAudioPlayPause = GUICtrlCreateButton("Play",       10 + $iBtnSpc       , $iBtnT, $iBtnW, $iBtnH)
Global $cAudioStop      = GUICtrlCreateButton("Stop",       10 + $iBtnSpc * 2   , $iBtnT, $iBtnW, $iBtnH)
Global $cAudioMute      = GUICtrlCreateButton("Mute",       10 + $iBtnSpc * 3   , $iBtnT, $iBtnW, $iBtnH)
Global $cAudioRepeat    = GUICtrlCreateCheckbox('Repeat',   10 + $iBtnSpc * 4   , $iBtnT, $iBtnW, $iBtnH)
GUICtrlSetState(-1, $GUI_CHECKED)

; play-bar
Local $iTop = 145
Global $cAudioPlayLabel        = GUICtrlCreateLabel("", 10, $iTop, $iBarW, 20)
GUICtrlSetBkColor(-1, 0x878787) ; <<<<< Change color.
Global $cAudioPlayProcess      = GUICtrlCreateProgress(10+1, $iTop+1, $iBarW-2, 20-2)

; position percentage duration
Local $iTop = 170
$cAudioPosition         = GUICtrlCreateLabel("00:00:00", 10, $iTop, 60, 20)
$cAudioDuration         = GUICtrlCreateLabel("00:00:00", 298, $iTop, 60, 20)
$cAudioPercent          = GUICtrlCreateLabel("0%", 160, $iTop, 60, 20)

GUISetState()
Global $iCount = 0, $iDown = 0          ;for marquee
AdlibRegister('_MoveMarquee', 100)


While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            _Audio_pause($_AUDIO_HANDLE)
            _Audio_init_stop($_AUDIO_HANDLE)
            Exit
            
        Case $cAudioVolLabel
            Local $aMpos = GUIGetCursorInfo($hGUI)
            Local $iPercent = Int(100 * ($aMpos[0] - 12) / 320)
            GUICtrlSetData($cAudioVolCtrl, $iPercent)
            Local $sMute = GUICtrlRead($cAudioMute)
            
            GUICtrlSetData($cAudioMute, $iPercent &'% '& StringRegExpReplace($sMute, '(?:^|\d{1,3}\%\s)?(\w{2,6})$', '$1'))
;~             GUICtrlSetData($cAudioMute, $iPercent &' '& StringRegExpReplace($sMute, '^(\w{2,6})$', '$1'))
            ConsoleWrite('$aMpos[0]' &' - vol: '& $iPercent &'%'& @CRLF)
            _Set_volume($iPercent)
            
        Case $cAudioPlayLabel
            Local $aMpos = GUIGetCursorInfo($hGUI)
            Local $iPercent = Int(100 * ($aMpos[0] - 12) / 320)
            GUICtrlSetData($cAudioPlayProcess, $iPercent)
            ConsoleWrite('$aMpos[0]' &' - Process: '& $iPercent &'%'& @CRLF)
            
            Local $iPos = ($iPercent/100) * $_AUDIO_LENGTH
            Local $_BASS_PAUSE_POS = _BASS_ChannelSeconds2Bytes($_AUDIO_HANDLE, $iPos)
            _BASS_ChannelSetPosition($_AUDIO_HANDLE, $_BASS_PAUSE_POS, $BASS_POS_BYTE)
            
        Case $cAudioLoad
            Global $iCount = 0, $iDown = 0
            $Fileopen = FileOpenDialog("Select Audio","", _
                        "(*.m4a;*.mp3;*.acc;*.ac3;*.wav;*.ogg;*.m3u;*.pls)|" & _
                        "(*.m4a)|(*.mp3)|(*.acc)|(*.ac3)|(*.wav)|(*.ogg)|(*.m3u)|(*.pls)|All(*.*)")
            If Not @error Then $_THE_URL = $Fileopen
;~                 Local $sFileName = StringRegExpReplace($Fileopen, "(.*?[\\/])*(.*?)((?:\.\w+\z|\z))", "$2")
            Local $sFileName = StringRegExpReplace($Fileopen, "^.+\\(.+\.\w{1,5})$", "$1")
            
            _terminate_audio_handle()
            _Audio_init_start()
            $_AUDIO_HANDLE = _Set_url($_THE_URL)
            
            If _Get_volume() < 10 Or _Get_volume() > 90 Then 
                GUICtrlsetdata($cAudioVolCtrl, $_DEFAULT_VOLUMN )
                _Set_volume($_DEFAULT_VOLUMN)
            EndIf
            
            If $_AUDIO_HANDLE > 1 Then
                $_AUDIO_LENGTH = _Get_len($_AUDIO_HANDLE) 
                GUICtrlSetData($cAudioTitle, 'Playing... '& $sFileName)
                GUICtrlSetData($cAudioDuration, _seconds_to_duration($_AUDIO_LENGTH))
                _Audio_pause($_AUDIO_HANDLE)
;~                 _Toggle_PlayPause()
            Else
                GUICtrlSetData($cAudioTitle, $_AUDIO_HANDLE& ': Audio fails to load')
            EndIf

        Case $cAudioPlayPause
            ;1=loaded, 2=playing, 3=pause
            If $_AUDIO_HANDLE > 1 Then
              _Audio_pause($_AUDIO_HANDLE)
            Else 
              GUICtrlSetData($cAudioTitle, $_AUDIO_HANDLE& ': load/add new track')
            EndIf
            
        Case $cAudioStop
            _terminate_audio_handle()

        Case $cAudioMute
            _Audio_mute()
    EndSwitch
    
    Sleep(10)
    ; live update
    If $_AUDIO_HANDLE > 1 Then 
        Local $sAudioTitle = _Get_streamtitle($_AUDIO_HANDLE)
        If $sAudioTitle <> $_ACTIVE_AUDIO_STREAM Then
            $_ACTIVE_AUDIO_STREAM = $sAudioTitle
            GUICtrlSetData($cAudioTitle, $sAudioTitle)
        Else
            Local $iAudioPosition  = _Get_pos($_AUDIO_HANDLE)
            Local $iAudioPositionCountDown = $_AUDIO_LENGTH - $iAudioPosition
            Local $iAudioPositionPercent = Int(100 * ($iAudioPosition) / $_AUDIO_LENGTH)
            
            GUICtrlSetData($cAudioPlayProcess, $iAudioPositionPercent)
            GUICtrlSetData($cAudioPercent, $iAudioPositionPercent&'%')
  ;~           GUICtrlSetData($cAudioPosition, _seconds_to_duration($iAudioPosition))             ; count up
            GUICtrlSetData($cAudioPosition, _seconds_to_duration($iAudioPositionCountDown))       ; count down
            
            _Audio_Repeat($iAudioPositionPercent)
          EndIf
    EndIf
WEnd



# ---funcs---


Func _Audio_Repeat($iAudioPositionPercent=0)
    If $iAudioPositionPercent == 100 Then
      Local $iRepeatStatus = GUICtrlRead($cAudioRepeat)
      Switch $iRepeatStatus
        Case 1
          _Audio_stop($_AUDIO_HANDLE)
          _Audio_pause($_AUDIO_HANDLE)
        Case 4
;~           _Audio_stop($_AUDIO_HANDLE)
          _terminate_audio_handle()
      EndSwitch
    EndIf
EndFunc

Func _GuiCtrl_GetStringPixelSize($guiCtrlHandle='')
  $sText = GUICtrlRead($guiCtrlHandle)
  $aSize = _StringSize($sText)
  Return $aSize[2]
EndFunc

;~ Func _MoveMarquee($opt=2) ; error warning why?
Func _MoveMarquee()
    If $_AUDIO_HANDLE > 1 Then
      
;~     Local $opt = 1
    Local $opt = 2

    Switch $opt
      Case 1 ;loop
;~           Local $iLabelW = 370                                                       ; preferablely width $iLabelW of $cAudioTitle or shorter or abit longer
          Local $iLabelW    = ControlGetPos($hGUI, '', $cAudioPlayLabel)                ; get Width ref equal to $cAudioTitle Width ($cAudioTitle gui set exceed Gui Width to prevent string collapse into 2lines)
          Local $iTextW     = _GuiCtrl_GetStringPixelSize($cAudioTitle)                 ; calculate pixels from string
          Local $aCpos      = ControlGetPos($hGUI, '', $cAudioTitle)                    ; current Ctrl position
          If $aCpos[0] >= $iLabelW[2]  Then $aCpos[0] = -($iTextW-($iLabelW[0]*3)) 
          Local $speed = 1                                                         ; + for forwards | - for backwards
          ControlMove($hGUI, '', $cAudioTitle, $aCpos[0] + $speed , $aCpos[1])          ; forward
    
      Case 2 ;snake/pingpong
          Local $iLabelY    = ControlGetPos($hGUI, '', $cAudioTitle)        ; get Y-Pos
          Local $iLabelW    = ControlGetPos($hGUI, '', $cAudioPlayLabel)    ; get Width ref equal to $cAudioTitle Width ($cAudioTitle gui set exceed Gui Width to prevent string collapse into 2lines)
          Local $iTextW     = _GuiCtrl_GetStringPixelSize($cAudioTitle)     ; calculate pixels from string
          Local $aCpos      = ControlGetPos($hGUI, '', $cAudioTitle)        ; current Ctrl position

  #cs
          ;testing
          Local $iTailStop
          $iLabelWx = 300 ;$iLabelW[2]
          
          #$iTextWx = 400
          $iTextWx = 100
          $iTextWx = $iTextWx/2
          
          If $iTextWx < $iLabelWx Then
            $iTailStop = -10 ;$iLabelW[0]
          ElseIf $iTextWx >= $iLabelWx Then
            $iTailStop = -(($iTextWx - $iLabelWx) + ($iLabelWx/2)) ;
          EndIf
  ;~ #ce
          ;               <$iTextWx/2 begin end here 
          ;               |       <then beginin would stop here
          ;               |       |       <$iTextWx/2 tail stop here
          ;               |       |       |       |        <want tail of str stop  here
          #------100------50------0++++++50++++++100++++++150+++...+++300

  ;~ #cs
          ; 
          If $iCount = 0 Then
              $iDown = 0
          ElseIf $iCount = 100 Then
              $iDown = 1
          EndIf
  #ce
          
          ; calc pos for the snake head or tail to rebounce
          If $iTextW < $iLabelW[2] Then
            $iHeadStop = $iLabelW[2] - $iTextW 
            $iTailStop = -($iLabelW[0])
          ElseIf $iTextW >= $iLabelW Then
            $iHeadStop = $iLabelW[2]/2
            $iTailStop = (($iTextW - $iLabelW[2]) + ($iLabelW[2]/2))
          EndIf
          
          ; direct switch
          If $aCpos[0] <= -($iTailStop) Then
              $iDown = 0
          ElseIf $aCpos[0] > $iHeadStop Then
              $iDown = 1
          EndIf
          
          ; step
          Switch $iDown
              Case 0
                  $iCount += 1
              Case Else
                  $iCount -= 1
          EndSwitch
          
          ; walking
          Local $marqueeX = ($iCount / 100) * $iTextW
          ControlMove($hGUI, "", $cAudioTitle, $marqueeX, $iLabelY[1])
    EndSwitch
    EndIf
EndFunc
        
Func _terminate_audio_handle()
    If $_AUDIO_HANDLE > 1 Then
        GUICtrlSetData($cAudioPlayPause, 'Play')
        GUICtrlSetData($cAudioTitle, '')
        Local $aCpos      = ControlGetPos($hGUI, '', $cAudioTitle)                    ; current Ctrl position
        ControlMove($hGUI, '', $cAudioTitle, 10 , $aCpos[1])
          
        _Audio_pause($_AUDIO_HANDLE)
        _Audio_stop($_AUDIO_HANDLE)
        $_AUDIO_HANDLE = _Audio_init_stop($_AUDIO_HANDLE)
    EndIf
EndFunc

Func _Toggle_PlayPause()
    If $_AUDIO_HANDLE Then 
        If $_PLAY_PAUSE_STATE Then
          $_PLAY_PAUSE_STATE = 0
          GUICtrlSetData($cAudioPlayPause, 'Pause')
          GUICtrlSetData($cAudioVolCtrl, _Get_volume())
          _Audio_play($_AUDIO_HANDLE)
        Else
          $_PLAY_PAUSE_STATE = 1
          GUICtrlSetData($cAudioPlayPause, 'Play')
          _Audio_pause($_AUDIO_HANDLE)
        EndIf
    Else
        GUICtrlSetData($cAudioTitle, 'Please load audio '&$_AUDIO_HANDLE)
    EndIf
EndFunc

Func _seconds_to_duration($seconds)
    Local $iHour = Int($seconds / 3600)
    Local $iMin = Int(($seconds - $iHour * 3600) / 60)
    Local $iSec = $seconds - $iHour * 3600 - $iMin * 60
    
    Return StringFormat('%02d:%02d:%02d', $iHour, $iMin, $iSec)
EndFunc

# --------------------------
;~ _Audio_Player_Main()
# --------------------------
Func _Audio_Player_Main()
    _Audio_init_start()

    $_AUDIO_HANDLE = _Set_url("http://ice1.somafm.com/groovesalad-128.mp3")
    $_AUDIO_HANDLE = _Set_url($_THE_URL)
    
    If _Get_volume() < 10 Or _Get_volume() > 90 Then _Set_volume($_DEFAULT_VOLUMN)
    
    _Audio_play($_AUDIO_HANDLE)
    while 1
        ConsoleWrite(_Get_streamtitle($_AUDIO_HANDLE) & @CRLF)
        Sleep(1000)
    WEnd
    _Audio_init_stop($_AUDIO_HANDLE)
EndFunc

Func _Audio_init_start()
    If _BASS_STARTUP(@ScriptDir & '\' & $_BASS_FOLDER & '\bass.dll') Then
        If _BASS_Init(0, -1, 44100, 0) Then
            If _BASS_SetConfig($BASS_CONFIG_NET_PLAYLIST, 1) = 0 Then
                SetError(3)
            EndIf
        Else
            SetError(2)
        EndIf
    Else
        SetError(@error)
    EndIf
EndFunc

Func _Set_buffer($buffer)
    _BASS_SetConfig($BASS_CONFIG_NET_BUFFER, $buffer)
EndFunc

Func _Audio_stop($_AUDIO_HANDLE)
    _BASS_ChannelStop($_AUDIO_HANDLE)
EndFunc

Func _Audio_play($_AUDIO_HANDLE)
    _BASS_ChannelPlay($_AUDIO_HANDLE, 1)
EndFunc

Func _Audio_mute()
    $iVolState = GUICtrlRead($cAudioVolCtrl)
  
    If $iVolState > 0 Then
      _Set_volume(0)
      GUICtrlSetData($cAudioVolCtrl, 0)
      GUICtrlSetData($cAudioMute, 'unMute')
      $_VOLUME_STATE = $iVolState
      
    Else
      GUICtrlSetData($cAudioVolCtrl, $_VOLUME_STATE)
      GUICtrlSetData($cAudioMute, 'Mute')
      _Set_volume($_VOLUME_STATE)
    EndIf
EndFunc

;1=audio loaded, 2=audio playing, 3=audio pause
Func _Audio_pause($_AUDIO_HANDLE)
    If _Get_playstate($_AUDIO_HANDLE) = 1 Then
        _Audio_play($_AUDIO_HANDLE)
        ConsoleWrite('!>!'&@ScriptLineNumber&': $_BASS_PAUSE_POS #1 Loaded | '&$_BASS_PAUSE_POS&''&@CRLF) ;Read/Brown
        GUICtrlSetData($cAudioPlayPause, 'Pause')
      
    ElseIf _Get_playstate($_AUDIO_HANDLE) = 2 Then
        $_BASS_PAUSE_POS = _BASS_ChannelGetPosition($_AUDIO_HANDLE, $BASS_POS_BYTE)
        ConsoleWrite('!>!'&@ScriptLineNumber&': $_BASS_PAUSE_POS #2 Playing | '&$_BASS_PAUSE_POS&''&@CRLF) ;Read/Brown
        GUICtrlSetData($cAudioPlayPause, 'Play')
        
        _BASS_ChannelPause($_AUDIO_HANDLE)
    ElseIf _Get_playstate($_AUDIO_HANDLE) = 3 Then
        _Audio_play($_AUDIO_HANDLE)
        ConsoleWrite('!>!'&@ScriptLineNumber&': $_BASS_PAUSE_POS #3 Paused | '&$_BASS_PAUSE_POS&''&@CRLF) ;Read/Brown
        GUICtrlSetData($cAudioPlayPause, 'Pause')
        _BASS_ChannelSetPosition($_AUDIO_HANDLE, $_BASS_PAUSE_POS, $BASS_POS_BYTE)
    EndIf
EndFunc

Func _Audio_init_stop($_AUDIO_HANDLE)
    _BASS_StreamFree($_AUDIO_HANDLE)
    _BASS_Free()
EndFunc

Func _Set_url($file)
    If FileExists($file) Then
        $_AUDIO_HANDLE = _BASS_StreamCreateFile(False, $file, 0, 0, 0)
    Else
        $_AUDIO_HANDLE = _BASS_StreamCreateURL($file, 0, 1)
    EndIf
    If @error Then
        Return SetError(1)
    EndIf
    Return $_AUDIO_HANDLE
EndFunc

Func _Get_pos($_AUDIO_HANDLE)
    $current = _BASS_ChannelGetPosition($_AUDIO_HANDLE, $BASS_POS_BYTE)
    Return Round(_Bass_ChannelBytes2Seconds($_AUDIO_HANDLE, $current))
EndFunc

Func _Get_len($_AUDIO_HANDLE)
    $current = _BASS_ChannelGetLength($_AUDIO_HANDLE, $BASS_POS_BYTE)
    Return Round(_Bass_ChannelBytes2Seconds($_AUDIO_HANDLE, $current))
EndFunc

Func _Set_pos($_AUDIO_HANDLE, $seconds)
    _BASS_ChannelSetPosition($_AUDIO_HANDLE, _BASS_ChannelSeconds2Bytes($_AUDIO_HANDLE, $seconds), $BASS_POS_BYTE)
EndFunc

Func _Set_volume($volume)
    _BASS_SetConfig($BASS_CONFIG_GVOL_STREAM, $volume * 100)
EndFunc

Func _Get_volume()
    Return _BASS_GetConfig($BASS_CONFIG_GVOL_STREAM) / 100
EndFunc

Func _Get_bitrate($_AUDIO_HANDLE)
    $a = Round(_Bass_ChannelBytes2Seconds($_AUDIO_HANDLE, _BASS_ChannelGetLength($_AUDIO_HANDLE, $BASS_POS_BYTE)))
    $return = Round(_BASS_StreamGetFilePosition($_AUDIO_HANDLE, $BASS_FILEPOS_END) * 8/ $a/ 1000)
    If StringInStr($return, "-") Then
        $return = _BASS_StreamGetFilePosition($_AUDIO_HANDLE, $BASS_FILEPOS_END) * 8 / _BASS_GetConfig($BASS_CONFIG_NET_BUFFER)
    EndIf
    Return $return
EndFunc

;1=audio loaded, 2=audio playing, 3=audio pause
Func _Get_playstate($_AUDIO_HANDLE)
    Switch _BASS_ChannelIsActive($_AUDIO_HANDLE)
        Case $BASS_ACTIVE_STOPPED
            $returnstate = 1
        Case $BASS_ACTIVE_PLAYING
            $returnstate = 2
        Case $BASS_ACTIVE_PAUSED
            $returnstate = 3
        Case $BASS_ACTIVE_STALLED
            $returnstate = 4
    EndSwitch
    Return $returnstate
EndFunc

Func _Get_streamtitle($_AUDIO_HANDLE)
    $pPtr = _BASS_ChannelGetTags($_AUDIO_HANDLE, $BASS_TAG_META)
    $sStr = _BASS_PtrStringRead($pPtr)
    If StringInStr($sStr, ";") Then
        $infosplit = StringSplit($sStr, ";")
        $infosplit[1] = StringReplace($infosplit[1], "'", "")
        $infosplit[1] = StringReplace($infosplit[1], "StreamTitle=", "")
        If StringInStr($infosplit[1], "-") Then
            Return $infosplit[1]
        EndIf
    EndIf
EndFunc

 

Edited by zeenmakr
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By ripdad
      This is a project that I have been working on for several months off and on.
      It's a simple "What You Hear" MP3 @ 320Kbps and WAV audio recorder.
      Features:
      - LoudMax, a Gain Controller and Look-Ahead Limiter
      - Auto Shut-Off after one minute of silence
      - Side-by-Side Simulated LED Meter
      - Running Time Counter
      It started off being a large project, but I eventually stripped it down to its
      bare essentials because I came to realize that it would be very difficult to
      deal with every sound card and every way a PC is set up for audio. It would
      have been a nightmare that I was not willing to go through.
      This project contains the most up-to-date BASS.dll v.2.4.15.0 - December 17, 2019.
      All BASS Dll's are 32bit. Those and the needed UDF's are included in the zip file.
      I will try to explain how it works in the next post.

      Download:
      BASS VST Recorder v1.1.zip
       
    • By ripdad
      This is an example script of how to load and use a VST using BASS in AutoIt.
      It is NOT immediately Plug and Play. You will need to download 2 VST's to run it.
      They are free for personal use.
      All information is in the header of the script.

      Download:
      BASS VST HOST v0.4.zip
       
    • By ripdad
      I have been working on audio graphical scripts these last several months.
      This is my last script on this subject for now. On to other projects.
      Credits: UEZ, Eukalyptus, BrettF, Prog@ndy, Authenticity and the AutoIt Community.
      Thanks to all that made these scripts possible!
      Please let me know if you have any problems with it.

      Download:
      LIVE Stereo Audio Waveform v2.zip
       

    • By ripdad
      I have been playing with this script off and on for about a month - trying to get the needles to look more realistic.
      It's best to use with an audio limiter on the source audio, or else you'll be tweaking the input levels on every song.
      It should work with any version of AutoIt, starting with 3.3.8.1.
      Please report any problems with it. Credits are in the script.

      Download: LIVE Analog Stereo Meter.au3
    • By nss
      Hi all,
       
      I am making a program in which I use Bass audio library (with the wrapper for autoit that I found here on forums I think) because of its support for dx effects.
      My problem, though, is that when effects as reverb or echo/delay are added, the channel length is not extended as to fit the tail of the effect, so if the file was really short, you wouldn't even hear the reverb at all.
      I've tried setting the  buffer parameter even to 60k ms, updating the channel length to 60k ms, but nothing makes it so that the effects aren't being cut off.
      I've heard that I could add silence manually to wave files by adding the chr(0) characters, but haven't had any luck doing that, either.
      What I'm doing:
      initialize bass use streamCreateFile to load the wave file with the fx flag and length parameter set to 60000 set the config buffer to 60000 use channel set fx to add dx8 reverb use channel play to play the sound use bass update to update the length to 60000  
      I even tried having only silence in one wave file and tried joining two wave files together, but that didn't work either.
       
      Any help would be very much appreciated.
×
×
  • Create New...