Jump to content

Need help with recording sound


Recommended Posts

Hello.

I need some help with my script. I want to make a script that records sound from the microphone when the sound passes a certain level. I managed to do this, but there are 3 problems:

- sometimes the sound level resulted from my function isn't always corect (most of the times is 128 when is silence and sometimes stais 3-8 even if I speek in the mic).

- from 5 recordings made, 2 are just loud fuzzy noise.

- the recording begins even if the sound level is not in the limit 20-80 as I setted it to be.

Here is the code I wrote:

#include <Media.au3>
#include <string.au3>

Global $rec = 1
Global $VoiceActivationLevel = 20
Global $RecTime = 60000;ms

$lpszReturnString = _StringRepeat(" ", 256)
$cchReturn = StringLen($lpszReturnString)
$mciError = _mciSendString("open new type waveaudio alias tempwave", $lpszReturnString, $cchReturn, 0);
If $mciError[0] <> 0 Then
    _mciShowError($mciError[0], "Startup Error")
    Exit
EndIf


$timer = TimerInit()
$count_hit = 0
While 1
    $level = 0
    If $rec = 1 Then
        SoundLevelChk()
        ToolTip("Voice a level =" & $VoiceActivationLevel & @CRLF & "level =" & $level, 10, 10, "SOUND_REC")
        If $level >= $VoiceActivationLevel And $level <= 80 Then
            $count_hit += 1
            If $count_hit >= 2 Then
                ToolTip("Voice a level =" & $VoiceActivationLevel & @CRLF & "level =" & $level, 10, 10, "SOUND_REC - HIT")
                Record()
                $count_hit = 0
            EndIf
        Else
            $count_hit = 0
        EndIf
    EndIf
    
    Sleep(50)
WEnd

Func _mciSendString($lpszCommand, $lpszReturnString, $cchReturn, $hwndCallback)
    Return DllCall("winmm.dll", "long", "mciSendStringA", "str", $lpszCommand, "str", $lpszReturnString, "long", $cchReturn, "long", 0)
EndFunc   ;==>_mciSendString

Func _mciShowError($mciError, $add = "test")
    Dim $errStr
    $errStr = _StringRepeat(" ", 256)
    $result = DllCall("winmm.dll", "long", "mciGetErrorStringA", "long", $mciError, "string", $errStr, "long", StringLen($errStr))
    MsgBox(0, "MCI " & $add, "MCI Error Number " & $mciError & ":" & $result[2])
EndFunc   ;==>_mciShowError

Func SoundLevelChk()
    Global $lpszCommand, $lpszReturnString, $cchReturn
    $level = 0
    $mciError = _mciSendString("status tempwave level", $lpszReturnString, $cchReturn, 0)
    If $mciError[2] > 0 Then
        If $mciError[0] = 0 Then $level = $mciError[2]
    EndIf
EndFunc   ;==>SoundLevelChk

Func Record()
    $File = @ScriptDir & "\trimise\" & @MDAY & @MON & @YEAR & @HOUR & @MIN & @SEC & ".wav"
    If Not FileExists(@ScriptDir & "\trimise") Then DirCreate(@ScriptDir & "\trimise")
    $Media = _MediaCreate(6)
    If Not @error Then
        _MediaRecord($Media)
        If Not @error Then
            $counter_rec = TimerInit()
            While 1
                If TimerDiff($counter_rec) >= $RecTime Then
                    $counter_rec = ""
                    ExitLoop
                EndIf
                Sleep(100)
            WEnd
            _MediaStop($Media)
            If Not @error Then
                _MediaSave($Media, $File)
            EndIf
            _MediaClose($Media)
        Else
            _MediaStop($Media)
            If @error Then
                _MediaClose($Media)
            EndIf
        EndIf
    Else
        _MediaClose($Media)
    EndIf
EndFunc   ;==>Record

and this is the Media.au3 file i Used:

I do not like stupid and idiot people that write idiot things...If you are one, do not write.

Link to comment
Share on other sites

BASS.dll (check sig. From memory you are looking for the examples included with BASSENC...) does have recording functionality... Maybe worthy to check out.

Link to comment
Share on other sites

BASS.dll (check sig. From memory you are looking for the examples included with BASSENC...) does have recording functionality... Maybe worthy to check out.

Thx for reply.

I tried the bass.dll, and it works super ok, but the size of the file is to large. could you give me an example with bass.dll for the smallest possible size made with it?

I do not like stupid and idiot people that write idiot things...If you are one, do not write.

Link to comment
Share on other sites

Thx for reply.

I tried the bass.dll, and it works super ok, but the size of the file is to large. could you give me an example with bass.dll for the smallest possible size made with it?

someone?

I do not like stupid and idiot people that write idiot things...If you are one, do not write.

Link to comment
Share on other sites

What do you mean too large?

With bass.dll I managed to create sound files with aprox. 87kbps, but with the script I posted the sound file resulted has aprox 10 kbps.

I'd like to create sound files with bass.dll with aprox 10kbps or smaller if possible because for a recording time of 30 minutes, the recording file size will be 152 MB with 87kbps and 17 MB with 10kbps.

If you could help me with this it will be greatly apreciated.

Thx in advance.

I do not like stupid and idiot people that write idiot things...If you are one, do not write.

Link to comment
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
 Share

  • Recently Browsing   0 members

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