Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MMControl recording question

Status
Not open for further replies.

kodr

Programmer
Dec 4, 2003
368
What I'm working on is capturing an audio recording (from a microphone) and passing it to another computer through the internet (yeah, voice chat.) Currently what I'm doing is capturing 1 second wav files and sending them.

There has got to be a better way. Is is possible to 'hook' or capture the data before sending it to a file and send it to memory instead?

Is there a better way then using the MMControl?

 
I found this some years ago. Can't remember the source but it may help you.

If you have Visual Basic 4.0 or newer then copy and paste this code into a module. (eg. module1.bas)

Declare Function mciSendString Lib "winmm.dll" Alias _
"mciSendStringA" (ByVal lpstrCommand As String, ByVal _
lpstrReturnString As Any, ByVal uReturnLength As Long, ByVal _
hwndCallback As Long) As Long


OPEN
We must OPEN a WAV file before we can do anything to with it. All of the other
MCI commands listed on this web page require that the OPEN command to be used
first, otherwise they will not work. The type of multimedia file we want to use is waveaudio and the alias name that we will use to refer to the AVI file is voice1 (Although we can use any alias name you desire, such as "Myvoice", "voice2", "john1", etc... , we'll use voice1 for easy reference in the examples that follow).

i = mciSendString("open c:\myvoice.wav type waveaudio alias voice1", 0&, 0, 0)

PLAY
When a WAV is open, we can play it. The keyword in this command is PLAY.
Notice that we use the alias voice1. This is the same as typing in c:\myvoice.wav, but use the alias because you can always refer to the same WAV file throughout your entire VB program without using a global variable.

i = mciSendString("play voice1", 0&, 0, 0)

Play from...
You don't have to start playing a WAV file from the beginning. You can start
anywhere within the file. This example starts at 2000 (milliseconds only) and plays to the end.

i = mciSendString("play voice1 from 2000", 0&, 0, 0)

Play from to...
Segments of an AVI file can be played. Perhaps you have an AVI file with several
different scenes, and want to play onlyone of them.


i = mciSendString("play voice1 from 15 to 20", 0&, 0, 0)

Wait...
A WAV file can be played without any interruption from other Windows. The wait
command sets the focus entirely on playing the WAV file until it has reached the end. It is a good idea not to use this command on long, time-consuming files. You are virtually "locked out" of your computer until the file completely finishes playing.

i = mciSendString("play voice1 wait", 0&, 0, 0)

Pause
While an WAV file is being played, you can pause it. Use the resume command to
resume playing.

i = mciSendString("pause voice1", 0&, 0, 0)

Resume...
When a WAV file is paused, you can resume and continue playing it.

i = mciSendString("resume voice1", 0&, 0, 0)

Stop...
While a WAV file is being played, you can stop it. Use the play command to start
playing the file again.

i = mciSendString("stop voice1", 0&, 0, 0)

TIP:
You can use the code below to play a WAV file that has been stopped.
This gives the user the "rewind illusion" that once a WAV file is stopped
(much like an Audio CD player), it will start from the beginning when
played again.

i = mciSendString("play voice1 from 0", 0&, 0, 0)

Milliseconds
When playing a WAV from one point to another, or seeking a specific point, you
can use milliseconds (ms). This sample shows how to set your WAV file for
milliseconds.

Dim mssg As String * 255

i = mciSendString("set voice1 time format ms", 0&, 0, 0)
i = mciSendString("status voice1 length", mssg, 255, 0)
msgbox "There are " & Str(mssg) & " milliseconds"

Bytes
When playing a WAV from one point to another, or seeking a specific point, you
can use bytes. This sample shows how to set your WAV file for bytes.

Dim mssg As String * 255

i = mciSendString("set voice1 time format bytes", 0&, 0, 0)
i = mciSendString("status voice1 length", mssg, 255, 0)
msgbox "There are " & Str(mssg) & " bytes"

Seek
Any position within a WAV can be set by using the seek command. This also sets
the position of the WAV for PLAY. The example below sets the WAV starting
position to 1000 (milliseconds).

i = mciSendString("seek voice1 to 1000", 0&, 0, 0)

Seek to beginning of WAV
i = mciSendString("seek voice1 to start", 0&, 0, 0)

Seek to end of WAV
i = mciSendString("seek voice1 to end", 0&, 0, 0)

Close...
Perhaps this is the most important MCI command code for your project. You must
close the WAV file (unless you need it open for another program). Leaving it open could cause problems with Windows. The opened WAV file will remain open, even when you exit your software. This is known as "being used", or "hanging".

i = mciSendString("close voice1", 0&, 0, 0)





Record (edit) an Existing WAV File
You can record a WAV file by using a one that is already open. This is not the only way to do it (see further down for more). Mainly, this command will allow you to record over any or all of an existing WAV file. The key word in this command line is record. Any changes that you make in the WAV file are not permanent until you use the save command.

i = mciSendString("record voice1", 0&, 0, 0)

Try this to record over a small segment of a WAV file.
i = mciSendString("record voice1 from 2000 to 4000", 0&, 0, 0)

Record... Overwrite...
Basically overwrites the existing WAV file.
i = mciSendString("record voice1 overwrite", 0&, 0, 0)

Or try this...
i = mciSendString("record voice1 overwrite from 1000 to 3000", 0&, 0, 0)

Record... Insert...
Specifies that new data is added to the device element.
i = mciSendString("record voice1 insert", 0&, 0, 0)

Or try this...
i = mciSendString("record voice1 insert from 3000 to 6000", 0&, 0, 0)

Delete
You can delete sections of a WAV file. The code below will do this.
i = mciSendString("delete voice1 from 2000 to 4000", 0&, 0, 0)

Save...
A WAV file is not modified until you save it. Any recorded data in a WAV file is
buffered, or placed into temporary memory. The code below will change the WAV
file with the newly recorded information, using the save command.

i = mciSendString("save voice1 c:\test.wav", 0&, 0, 0)

Close...
Probably the most important command is the close command. You should always
MAKE SURE that your WAV file is closed after you have finished using it, especially when you end your program. It is a good idea to add this close command in the "Form_Unload" sub. Not closing a WAV file will cause problems within WINDOWS, because the opened WAV file will remain open, known as "being used", or "hanging".

i = mciSendString("close voice1", 0&, 0, 0)


Record a WAV File From Scratch
Open... Capture...
Before you can record a WAV file, you must open the WAV device with a new
type, in this case it is waveaudio which we will refer as capture in the examples that follow. (Don't confuse this OPEN with the other OPEN statement).

i = mciSendString("open new type waveaudio alias capture", 0&, 0, 0)

Bits per Sample...
The Bits per Sample can be set using the bitspersample command with either 8
or 16 following it. The eight sets the wave file to record at 8 bits, the sixteen sets the wav file to record at 16 bits (16 bits has better sound quality).

i = mciSendString("set capture bitspersample 8", 0&, 0, 0)

Samples per Second...
The Samples per Second can be set using the samplespersec command.

Samples that are supported:
11025 low quality
22050 medium quality
44100 high quality (CD music quality)

i = mciSendString("set capture samplespersec 11025", 0&, 0, 0)

NOTE: Many people have e-mailed me saying that they cannot record a WAV at
different qualities. So far, I haven't been able to help them out. I do know this much. I have successfully recorded all possible types of WAV files, but noticed that determining the Sample rate always returns 11025 even though we know that the WAV is recorded at a higher level. A mystery that I haven't figured out yet (in VB5.0).

Mono... Stereo...
Set the wav to record with one channel (1 = mono) or two channels (2 = stereo).
Use the channels command in the example below.

i = mciSendString("set capture channels 1", 0&, 0, 0)

Record...
With or without the using settings shown above, you can still record a WAV file.
The line of code below is almost the same as the play command. When used, it
immediately starts recording until you stop or pause it.

i = mciSendString("record capture", 0&, 0, 0)

DELETE
You can delete any section of a WAV file. The nice thing about this, is the fact that you do not need to be a math wizard to figure out what you just deleted in case you want to delete multiple sections.

If you use the sample code below, it works. If you use it again during the same
session, nothing will happen. This is because you are refering to the size of the original file (before saving it). Deleting the fist part of the WAV doesn't make the file length shorter (until you save it as a new file), it just isn't played back. It is easy to delete a middle section or an end section when refering to the original file length.

i = mciSendString("delete capture from 1 to 200", 0&, 0, 0)

Save
A WAV file is not created until you save it. Any recorded data in a WAV file is
buffered, or placed into temporary memory. The code below will make the WAV file
with the newly recorded information, using the save command. Make sure you save
the file with a unique name. Saving a file with one that already exists will completely ruin the older file. The output in this example is "C:\NewWav.wav".

i = mciSendString("save capture c:\NewWave.wav", 0&, 0, 0)


Capabilities of a WAV File

Can it be played?
The WAv file that you opened may or may not be able to play. This may appear to
be nonsense, but this is an actual MCI command, so it is listed here.

Dim mssg As String * 255
Dim Rslt as string
x% = mciSendString("capability voice1 can play", mssg, 255, 0)
If Left$(mssg, 4) = "true" Then
Rslt = "Can be played" & vbCrLf
Else
Rslt = "Cannot be played" & vbCrLf
End If
Msgbox Rslt

Can it be played in reverse?
Most WAV files can be played in reverse. Use this code to see if the WAV you
opened has this capability.

Dim mssg As String * 255
Dim Rslt as string
x% = mciSendString("capability voice1 can reverse", mssg, 255, 0)
If Left$(mssg, 4) = "true" Then
Rslt = "Can be played in Reverse" & vbCrLf
Else
Rslt = "Cannot be played in Reverse" & vbCrLf
End If
Msgbox Rslt

Can it be Saved?
Again, some WAV files will not have this capability. The code below will give you this information.

Dim mssg As String * 255
Dim Rslt as string
x% = mciSendString("capability voice1 can save", mssg, 255, 0)
If Left$(mssg, 4) = "true" Then
Rslt = "Can be saved" & vbCrLf
Else
Rslt = "Cannot be saved" & vbCrLf
End If
Msgbox Rslt


Current Status of a WAV File

How Long is the WAV file
There will be a time when you to know how long a WAV file is. Below is some sample code to determine the length of a WAV file..

TIME:
Dim mssg As String * 255

i = mciSendString("set voice1 time format ms", 0&, 0, 0)
i = mciSendString("status voice1 length", mssg, 255, 0)
msgbox "There are " & Str(mssg) & " milliseconds"

BYTES:
Dim mssg As String * 255

i = mciSendString("set voice1 time format bytes", 0&, 0, 0)
i = mciSendString("status voice1 length", mssg, 255, 0)
msgbox "There are " & Str(mssg) & " bytes"

MODE
What exactly is the mode of the WAV file? This will tell you if your WAV file is not ready, paused, playing, stopped, recording, or seeking.

Dim mssg As String * 255
i = mciSendString("status voice1 mode", mssg, 255, 0)
MsgBox mssg

Is the WAV STEREO or MONO?
There is no need for long complicated coding to determine if a WAV file was recorded with one or two channels.

Dim mssg As String * 255

i = mciSendString("status voice1 channels", mssg, 255, 0)
If Str(mssg) = 1 Then
MsgBox "The WAV is Mono"
End If

If Str(mssg) = 2 Then
MsgBox "The WAV is Stereo"
End If

What Bits per Sample was the WAV recorded at?
This will return the bits of the WAV file.

Dim mssg As String * 255

i = mciSendString("status voice1 bitspersample", mssg, 255, 0)
MsgBox "The WAV recoded at " & Str(mssg) & " bits per sample"

What Bytes per Second (Hz) was the WAV recorded at?
You can determine the sound quality of a WAV file. The higher the number returned, the better the quality of the WAV. For example, if the return value is 11025, then it is telephone quality. If it is 44100, then the WAV is CD quality.

Dim mssg As String * 255

i = mciSendString("status voice1 bytespersec", mssg, 255, 0)
MsgBox "The WAV recoded at " & Str(mssg) & " bytes per second"


Alan

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top