Jump to content

Extra _Sound functions


Kip
 Share

Recommended Posts

I was searching how to set the volume of a _Sound file, and I found a view different functions too.

So I wrote a little _sound extension :)

Functions:

_SoundVolume() ; Set the master volume, (just for 1 sound Id, not for the whole computer like SoundSetWaveVolume() does.)

_SoundPanLeft() ; Set the volume of the left speakers,

_SoundPanRight() ; Set the volume of the right speakers,

_SoundSpeed() ; Set the speed,

_SoundTimeToMs() ; Convert HH:MM:SS to milliseconds,

_SoundMsToTime() ; Convert milliseconds to HH:MM:SS,

_SoundInfo() ; Returns almost all known information about a sound.

(Check end of this post for all possible parameters you want information about.)

<Sound.au3> should be included, otherwise the functions wont work.

The script with an example in it:

#include <Sound.au3>

$Sound = _SoundOpen("C:\Documents and Settings\HP_Administrator\Shared\Rob Mayth - Can I Get A Witness.mp3"); Pick a song that is at least 1 minute long

_SoundPlay($Sound)


ConsoleWrite(@CRLF&@CRLF&"First: Volume...")

For $i = 1000 to 0 Step -1
    _SoundVolume($Sound,$i)
    Sleep(1)
Next
For $i = 0 to 1000
    _SoundVolume($Sound,$i)
    Sleep(1)
Next

ConsoleWrite(@CRLF&@CRLF&"Second: Pan...")

for $i = 0 to 1000 Step 5
    _SoundPanLeft($Sound,$i)
    _SoundPanRight($Sound,1000-$i)
    Sleep(1)
Next
for $i = 0 to 1000 step 5
    _SoundPanLeft($Sound,1000-$i)
    _SoundPanRight($Sound,$i)
    Sleep(1)
Next

_SoundPanRight($Sound,1000); normal
_SoundPanLeft($Sound,1000); normal

ConsoleWrite(@CRLF&@CRLF&"Third: Speed...")



_SoundSpeed($Sound,500)
Sleep(3000)
_SoundSpeed($Sound,800)
Sleep(3000)
_SoundSpeed($Sound,1500)
Sleep(3000)
_SoundSpeed($Sound,2200)
Sleep(3000)
_SoundSpeed($Sound,1000); normal


ConsoleWrite(@CRLF&@CRLF&"Fourth: Info,")
    
    $Info = _SoundInfo($Sound, "Speed")
    ConsoleWrite(@CRLF&"Current speed: "& $Info)
    
    $Info = _SoundInfo($Sound, "Volume")
    ConsoleWrite(@CRLF&"Current volume: "& $Info)

ConsoleWrite(@CRLF&@CRLF&"Done! Enjoy the rest of the song."&@CRLF&@CRLF)


While 1
    
WEnd




Func _SoundTimeToMs($Hours, $Minutes, $Seconds)
    
    Return ($Hours*3600000)+($Minutes*60000)+($Seconds*1000)
    
EndFunc





Func _SoundMsToTime($ms)
    
    Local $Return[3]
    
    
    if $ms >= 3600000 Then; 1000*60*60 = 3600000
        $Hours = Floor($ms/3600000)
        $Rest = $ms-($Hours*3600000)
    Else
        $Hours = 0
        $Rest = $ms
    EndIf
    
    If $Rest >= 60000 Then; 1000*60 = 60000
        $Minutes = Floor($Rest/60000)
        $Rest = $Rest-($Minutes*60000)
    Else
        $Minutes = 0
        $Rest = $ms
    EndIf
    
    $Seconds = Round($Rest/1000)
    
    $Return[0] = $Hours
    $Return[1] = $Minutes
    $Return[2] = $Seconds
    
    Return $Return
    
EndFunc



Func _SoundInfo($sSnd_id, $Parameter); look at "http://msdn2.microsoft.com/en-us/library/ms713277(VS.85).aspx" at the table "digitalvideo" for possible parameters
    
    If StringInStr($sSnd_id,'!') Then Return SetError(3, 0, 0); invalid file/alias

;return status
    Return mciSendString("status " & FileGetShortName($sSnd_id) & " " &$Parameter)
EndFunc;==>_SoundInfo





Func _SoundSpeed($sSnd_id, $Speed); $Speed: 0 - 2267,  1000= normal
;Declare variables
    Local $iRet
    
    If StringInStr($sSnd_id,'!') Then Return SetError(3, 0, 0); invalid file/alias
    
    if $Speed < 0 or $Speed > 2267 Then Return SetError(1, 0, 0)
    
    $iRet = mciSendString("set " & FileGetShortName($sSnd_id) & " speed "&$Speed)
;return
    If $iRet = 0 Then
        Return 1
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc;==>_SoundSpeed





Func _SoundPanLeft($sSnd_id, $Pan); $Pan: 0 - 1000,  1000= normal
;Declare variables
    Local $iRet
    
    If StringInStr($sSnd_id,'!') Then Return SetError(3, 0, 0); invalid file/alias
    
    if $Pan < 0 or $Pan > 1000 Then Return SetError(1, 0, 0)
    
    $iRet = mciSendString("setaudio " & FileGetShortName($sSnd_id) & " left volume to "&$Pan)
;return
    If $iRet = 0 Then
        Return 1
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc;==>_SoundPanLeft





Func _SoundPanRight($sSnd_id, $Pan); $Pan: 0 - 1000,  1000= normal
;Declare variables
    Local $iRet
    
    If StringInStr($sSnd_id,'!') Then Return SetError(3, 0, 0); invalid file/alias
    
    if $Pan < 0 or $Pan > 1000 Then Return SetError(1, 0, 0)
    
    $iRet = mciSendString("setaudio " & FileGetShortName($sSnd_id) & " right volume to "&$Pan)
;return
    If $iRet = 0 Then
        Return 1
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc;==>_SoundPanRight





Func _SoundVolume($sSnd_id, $Volume); $Volume: 0 - 1000,  1000= normal
;Declare variables
    Local $iRet
    
    If StringInStr($sSnd_id,'!') Then Return SetError(3, 0, 0); invalid file/alias
    
    if $Volume < 0 or $Volume > 1000 Then Return SetError(1, 0, 0)
    
    $iRet = mciSendString("setaudio " & FileGetShortName($sSnd_id) & " volume to "&$Volume)
;return
    If $iRet = 0 Then
        Return 1
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc;==>_SoundVolume

_SoundInfo() Parameters:

audio

audio alignment

audio bitspersample

audio breaks

audio bytespersec

audio input

audio record

audio source

audio samplespersec

audio stream

bass

bitsperpel

brightness

color

contrast

current track

file completion

file format

file mode

forward

frames skipped

gamma

input

left volume

length

media present

mode

monitor

monitor method

nominal

nominal frame rate

nominal record frame rate

number of tracks

output

palette handle

pause mode

play speed

position

ready

record frame rate

reserved size

right volume

seek exactly

sharpness

smpte

speed

start position

still file format

time format

tint

treble

unsaved

video

video key index

video key color

video record

video source

video source number

video stream

volume

window handle

window visible

window minimized

window maximized

Edited by kip
Link to comment
Share on other sites

Great job! :)

I once saw a little app in VB that was capable of generating sounds with a given frequency, tone, etc... (no, not Beep().. using the sound card). This sounds very doable, but I don't quite get the functions in Audio.au3 or Sound.au3 and I don't have internet at home anymore... Anyway, if you can accept the challenge...

Link to comment
Share on other sites

Thanks these

_SoundPanLeft() ; Set the volume of the left speakers

_SoundPanRight() ; Set the volume of the right speakers

funtions I posted about getting some help for along with some other mciSendString Commands

I will try to add these into amp 3, thanks!

[center][/center]

Link to comment
Share on other sites

I once saw a little app in VB that was capable of generating sounds with a given frequency, tone, etc... (no, not Beep().. using the sound card). This sounds very doable, but I don't quite get the functions in Audio.au3 or Sound.au3 and I don't have internet at home anymore... Anyway, if you can accept the challenge...

do you maybe know where that script is? :)
Link to comment
Share on other sites

I found one in C# http://www.codeproject.com/KB/audio-video/...eGenerator.aspx (you need to register to download source)

This is the thread where I saw the code: http://forums.devx.com/archive/index.php/t-75011.html

And the code:

Option Explicit

Private Const PI As Double = 3.141592654

Private wavBuffer() As Integer

Private Sub cmdMakeSound_Click()

Dim wavFormat As WAVEFORMATEX
Dim wavHead As WAVEHDR
Dim hWaveOut As Long
Dim i As Long
Dim Frequency As Double
Dim FreqConst As Double

If ((Val(txtFreq.Text) > 0) Or (Val(txtBufferLength.Text) > 0)) Then
' Create the buffer:
ReDim wavBuffer(0 To 44100 * Val(txtBufferLength.Text) - 1)
' Setup the nessasary info for outputting CD quality sound:

With wavFormat
.wFormatTag = WAVE_FORMAT_PCM
.nChannels = 2
.wBitsPerSample = 16
.nSamplesPerSec = 44100
.nBlockAlign = 2
.nAvgBytesPerSec = 176400
End With

With wavHead
.lpData = VarPtr(wavBuffer(LBound(wavBuffer)))
.dwBufferLength = LenB(wavBuffer(LBound(wavBuffer))) *
(UBound(wavBuffer) - LBound(wavBuffer) + 1)
End With

Call waveOutOpen(hWaveOut, WAVE_MAPPER, wavFormat, AddressOf
waveOutProc, 0, CALLBACK_FUNCTION)
' Debug.Print "hWaveOut: " & Hex(hWaveOut)
Call waveOutPrepareHeader(hWaveOut, wavHead, LenB(wavHead))
FinishedPlaying = False
cmdMakeSound.Enabled = False
' Debug.Print "Filling buffer..."

' Increasing frequency:
' For i = LBound(wavBuffer) To UBound(wavBuffer)
' wavBuffer(i) = 32767 * Sin(i * Frequency)
' Frequency = Frequency + 0.000001
' Next i

' White noise:
' Randomize
' For i = LBound(wavBuffer) To UBound(wavBuffer)
' wavBuffer(i) = 32767 * Rnd
' Next i

' Specific frequency:
FreqConst = 44100 / (PI * 2) / Val(txtFreq.Text)

For i = LBound(wavBuffer) To UBound(wavBuffer)
wavBuffer(i) = hsbVolume.Value * Sin((i Mod 44100) / FreqConst)
Next i

' Start playing buffer contents
Call waveOutWrite(hWaveOut, wavHead, LenB(wavHead))

Do While (Not FinishedPlaying)
DoEvents
Loop

Call waveOutUnprepareHeader(hWaveOut, wavHead, LenB(wavHead))
Call waveOutClose(hWaveOut)

Erase wavBuffer
cmdMakeSound.Enabled = True
End If

End Sub
===========================

Code module:

===========================
Option Explicit

Public Type WAVEHDR
lpData As Long
dwBufferLength As Long
dwBytesRecorded As Long
dwUser As Long
dwFlags As Long
dwLoops As Long
lpNext As Long
Reserved As Long
End Type

Public Type WAVEFORMATEX
wFormatTag As Integer
nChannels As Integer
nSamplesPerSec As Long
nAvgBytesPerSec As Long
nBlockAlign As Integer
wBitsPerSample As Integer
cbSize As Integer
End Type

Public Declare Function waveOutOpen Lib "winmm.dll" (ByRef lphWaveOut As
Long, ByVal uDeviceID As Long, ByRef lpFormat As WAVEFORMATEX, ByVal
dwCallback As Long, ByVal dwInstance As Long, ByVal dwFlags As Long) As Long
Public Declare Function waveOutClose Lib "winmm.dll" (ByVal hWaveOut As
Long) As Long
Public Declare Function waveOutPrepareHeader Lib "winmm.dll" (ByVal
hWaveOut As Long, ByRef lpWaveOutHdr As WAVEHDR, ByVal uSize As Long) As
Long
Public Declare Function waveOutUnprepareHeader Lib "winmm.dll" (ByVal
hWaveOut As Long, ByRef lpWaveOutHdr As WAVEHDR, ByVal uSize As Long) As
Long
Public Declare Function waveOutWrite Lib "winmm.dll" (ByVal hWaveOut As
Long, ByRef lpWaveOutHdr As WAVEHDR, ByVal uSize As Long) As Long

Public Const WAVE_MAPPER = -1&
Public Const WAVE_FORMAT_PCM = 1

Public Const CALLBACK_TYPEMASK = &H70000 ' callback type mask
Public Const CALLBACK_NULL = &H0 ' no callback
Public Const CALLBACK_WINDOW = &H10000 ' dwCallback is a HWND
Public Const CALLBACK_TASK = &H20000 ' dwCallback is a HTASK
Public Const CALLBACK_FUNCTION = &H30000 ' dwCallback is a
FARPROC
'#ifdef _WIN32
Public Const CALLBACK_THREAD = (CALLBACK_TASK) ' thread ID replaces 16
bit task
Public Const CALLBACK_EVENT = &H50000 ' dwCallback is an EVENT
Handle
'#endif
Public Const WAVE_FORMAT_QUERY = &H1
Public Const WAVE_ALLOWSYNC = &H2
'#if(WINVER >= &H0400)
Public Const WAVE_MAPPED = &H4
Public Const WAVE_FORMAT_DIRECT = &H8
Public Const WAVE_FORMAT_DIRECT_QUERY = (WAVE_FORMAT_QUERY Or
WAVE_FORMAT_DIRECT)
'#endif /* WINVER >= 0x0400 */

' flags for dwFlags field of WAVEHDR
Public Const WHDR_DONE = &H1 ' done bit
Public Const WHDR_PREPARED = &H2 ' set if this header has
been prepared
Public Const WHDR_BEGINLOOP = &H4 ' loop start block
Public Const WHDR_ENDLOOP = &H8 ' loop end block
Public Const WHDR_INQUEUE = &H10 ' reserved for driver

Public Const MM_WOM_OPEN = &H3BB ' waveform output
Public Const MM_WOM_CLOSE = &H3BC
Public Const MM_WOM_DONE = &H3BD

Public Const MM_WIM_OPEN = &H3BE ' waveform input
Public Const MM_WIM_CLOSE = &H3BF
Public Const MM_WIM_DATA = &H3C0

' wave callback messages
Public Const WOM_OPEN = MM_WOM_OPEN
Public Const WOM_CLOSE = MM_WOM_CLOSE
Public Const WOM_DONE = MM_WOM_DONE

Public Const WIM_OPEN = MM_WIM_OPEN
Public Const WIM_CLOSE = MM_WIM_CLOSE
Public Const WIM_DATA = MM_WIM_DATA

' general error return values
Public Const MMSYSERR_BASE = 0
Public Const MMSYSERR_NOERROR = 0 ' no error
Public Const MMSYSERR_ERROR = (MMSYSERR_BASE + 1) ' unspecified
error
Public Const MMSYSERR_BADDEVICEID = (MMSYSERR_BASE + 2) ' device ID
out of range
Public Const MMSYSERR_NOTENABLED = (MMSYSERR_BASE + 3) ' driver
failed enable
Public Const MMSYSERR_ALLOCATED = (MMSYSERR_BASE + 4) ' device
already allocated
Public Const MMSYSERR_INVALHANDLE = (MMSYSERR_BASE + 5) ' device
handle is invalid
Public Const MMSYSERR_NODRIVER = (MMSYSERR_BASE + 6) ' no device
driver present
Public Const MMSYSERR_NOMEM = (MMSYSERR_BASE + 7) ' memory
allocation error
Public Const MMSYSERR_NOTSUPPORTED = (MMSYSERR_BASE + 8) ' function
isn't supported
Public Const MMSYSERR_BADERRNUM = (MMSYSERR_BASE + 9) ' error value
out of range
Public Const MMSYSERR_INVALFLAG = (MMSYSERR_BASE + 10) ' invalid
flag passed
Public Const MMSYSERR_INVALPARAM = (MMSYSERR_BASE + 11) ' invalid
parameter passed
Public Const MMSYSERR_HANDLEBUSY = (MMSYSERR_BASE + 12) ' handle
being used simultaneously on another thread (eg callback) */
Public Const MMSYSERR_INVALIDALIAS = (MMSYSERR_BASE + 13) ' specified
alias not found
Public Const MMSYSERR_BADDB = (MMSYSERR_BASE + 14) ' bad
registry database
Public Const MMSYSERR_KEYNOTFOUND = (MMSYSERR_BASE + 15) ' registry
key not found
Public Const MMSYSERR_READERROR = (MMSYSERR_BASE + 16) ' registry
read error
Public Const MMSYSERR_WRITEERROR = (MMSYSERR_BASE + 17) ' registry
write error
Public Const MMSYSERR_DELETEERROR = (MMSYSERR_BASE + 18) ' registry
delete error
Public Const MMSYSERR_VALNOTFOUND = (MMSYSERR_BASE + 19) ' registry
value not found
Public Const MMSYSERR_NODRIVERCB = (MMSYSERR_BASE + 20) ' driver does
not call DriverCallback
Public Const MMSYSERR_MOREDATA = (MMSYSERR_BASE + 21) ' more data
to be returned
Public Const MMSYSERR_LASTERROR = (MMSYSERR_BASE + 21) ' last error
in range

' waveform audio error return values
Public Const WAVERR_BASE = 32
Public Const WAVERR_BADFORMAT = (WAVERR_BASE + 0) ' unsupported
wave format
Public Const WAVERR_STILLPLAYING = (WAVERR_BASE + 1) ' still
something playing
Public Const WAVERR_UNPREPARED = (WAVERR_BASE + 2) ' header not
prepared
Public Const WAVERR_SYNC = (WAVERR_BASE + 3) ' device is
synchronous
Public Const WAVERR_LASTERROR = (WAVERR_BASE + 3) ' last error
in range

Public FinishedPlaying As Boolean

Public Sub waveOutProc(ByVal hWaveOut As Long, ByVal uMsg As Long, ByVal
dwInstance As Long, ByVal dwParam1 As Long, ByVal dwParam2 As Long)
If (uMsg = WOM_DONE) Then FinishedPlaying = True
End Sub

I don't understand VB, but it seems to use the functions in winmm.dll :) That's why I thought this could be possible in AutoIt...

Link to comment
Share on other sites

I'm first busy with pitch, treble and bass functions.

and:

Public Type WAVEFORMATEX
wFormatTag As Integer
nChannels As Integer
nSamplesPerSec As Long
nAvgBytesPerSec As Long
nBlockAlign As Integer
wBitsPerSample As Integer
cbSize As Integer
End Type


Dim wavFormat As WAVEFORMATEX
Public Const WAVE_FORMAT_PCM = 1


With wavFormat
.wFormatTag = WAVE_FORMAT_PCM
.nChannels = 2
.wBitsPerSample = 16
.nSamplesPerSec = 44100
.nBlockAlign = 2
.nAvgBytesPerSec = 176400
End With

doesnt this creates an object? Autoit cant do that.

Edited by kip
Link to comment
Share on other sites

I'm first busy with pitch, treble and bass functions.

and:

Public Type WAVEFORMATEX
wFormatTag As Integer
nChannels As Integer
nSamplesPerSec As Long
nAvgBytesPerSec As Long
nBlockAlign As Integer
wBitsPerSample As Integer
cbSize As Integer
End Type


Dim wavFormat As WAVEFORMATEX
Public Const WAVE_FORMAT_PCM = 1


With wavFormat
.wFormatTag = WAVE_FORMAT_PCM
.nChannels = 2
.wBitsPerSample = 16
.nSamplesPerSec = 44100
.nBlockAlign = 2
.nAvgBytesPerSec = 176400
End With

doesnt this creates an object? Autoit cant do that.

I'd so love you if you'd do the pitch function :)
Link to comment
Share on other sites

I assume this works just for XP, not for Vista. If it is ok with you, I like to add this to Audio.au3.

Edit: I've been testing, and see unless a sleep function is put in for the balance thing, it will revert right back. I like the speed thing you came up with.

Edited by Volly
Link to comment
Share on other sites

I assume this works just for XP, not for Vista. If it is ok with you, I like to add this to Audio.au3.

Edit: I've been testing, and see unless a sleep function is put in for the balance thing, it will revert right back. I like the speed thing you came up with.

I can confirm that is working perfectly with Vista, too. Pretty good jog kip :)

[size="2"]SFXMaker[/size] - The most complete switchless installer creator software[indent][/indent]
Link to comment
Share on other sites

  • 4 months later...

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...