Record a WAV from SCRATCH
You can record a WAV file completely from scratch.
The same way professional programmers do. The MCI language is very easy
to use and generous at handeling errors. Below you will learn how to record
a new WAV file in a few easy steps.
API Declaration for Visual Basic (32-bit OS)
Versions 4, 5, and 6. Copy and paste into a Module.
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
API Declaration for Visual Basic (16-bit OS)
Versions 3 and 4. Copy and paste into a Module.
Declare Function mciSendString Lib "mmsystem" (ByVal lpstrCommand$, ByVal lpstrReturnStr As Any, ByVal wReturnLen%, ByVal hCallBack%) As Long
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.
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)
Stop
While recording, you can stop it with the stop
command. You can use the resume
command to continue recording.
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 incase 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)