Link to home
Start Free TrialLog in
Avatar of mdcarr
mdcarr

asked on

Can't record 8K 16Bit Mono wav file using mciSendString

Hello,

First thank you for your time.  My goal is to record a wav file with properties 8KHz 16Bit Mono.  I've included a code snippet that demonstrates my problem.  The first thing to notice is that even though 8000hz was specified the samples per second that is being recorded is 11025 as show in the debug.print statement.  What is the reason 11025 is returned when 8000 was specified?  More importantly, please modify the code to make it work.

Thank you very much,
Michael  


Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal CommandString As String, ByVal ReturnBuffer As String, ByVal ReturnBufferSize As Long, ByVal hCallback As Long) As Long 'MCIERROR

Private Sub cmdStartRecording8K16BitMonoWav_Click()
    Dim i As Integer
    Dim sMsg As String * 255
    Dim sSamplesPerSec As String
   
    i = mciSendString("open new Type waveaudio Alias voice3", 0&, 0, 0)
    i = mciSendString("stop voice3", 0&, 0, 0)
   
    i = mciSendString("set voice3 bitspersample 16", 0&, 0, 0)
    i = mciSendString("set voice3 channels 1", 0&, 0, 0)
    i = mciSendString("set voice3 samplespersec 8000", 0&, 0, 0)
    i = mciSendString("record voice3 ", 0&, 0, 0)

    i = mciSendString("status voice3 samplespersec", sMsg, 255, 0)
    sSamplesPerSec = Left(sMsg, InStr(sMsg, Chr(0)) - 1)
    Debug.Print sSamplesPerSec
End Sub

Private Sub cmdStopRecording_Click()
    Dim i As Integer
    Dim sMsg As String * 255
   
    i = mciSendString("stop voice3", 0&, 0, 0)
    i = mciSendString("save voice3 d:\temp\test3.wav", 0&, 0, 0)
    i = mciSendString("close all ", 0&, 0, 0)
End Sub
Avatar of DanRollins
DanRollins
Flag of United States of America image

You should check the return code from the set calls (the i value in the above code).  It's possible that mceSendString is rejecting the request (leaving the samplespersec at some default value).  

Also, it might be worth a try to use the "true" value of "8K" which is 8192

One other thing to check:  It's possible that the status command might be displaying something other than the actual samples-per-second rate.  If you have a good audio file utility program, take a look at the resulting file to verify exactly everything that there is to know about the recorded file.

-- Dan
Avatar of mdcarr
mdcarr

ASKER

Dear Dan,

First thank you for getting back to me.  

Taking your advice I displayed the error code from each mciSendString command that I called.  I am amazed that I missed it, but there was an error returned when I tried to set the samplespersec.  I tried both values for samplespersec: 8000 and 8192.  Both returned the same error code:  282.  Using mciGetErrorString and passing it a value of 282 gives an error text of: "The specified parameter is out of range for the specified command."

I did some internet searches trying to find the acceptable values for samplespersec, but I came up with nothing.  Do you happen to know these values, or at least do you know how I can find out?

Again, thank you for your help,
Michael
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mdcarr

ASKER

This is the information that I have been looking for.  Thank you very much.