Solved

How to create a blank .wav file?

Posted on 1997-12-06
13
4,512 Views
Last Modified: 2008-03-03
I need to create a blank .wav file to be used later in calling sound recorder.  I tried just creating a blank file with the .wav extension, but for some reason it will only allow for 15 seconds of recording in this file.  Is there a call to create a file of a certain windows type?
0
Comment
Question by:kbakst
  • 4
  • 4
  • 3
  • +1
13 Comments
 
LVL 8

Expert Comment

by:mrmick
ID: 1443954
The 15 seconds isn't a limitation of the file you've created, it's a limitation of the method you're using to record the data.  Post your code so I can check it out.
0
 

Author Comment

by:kbakst
ID: 1443955
Pretty simple, really - since I'm using Sound Recorder to do the actual recording:

 sndfile = sndpath + ID.Caption + ".wav"

     If Len(Dir(sndfile)) = 0 Then
       ptr = FreeFile
       Open sndfile For Append As #ptr
       Close #ptr
     End If
     
     sndcmd = "c:\windows\sndrec32.exe " + sndfile
     
     ReturnVal = Shell(sndcmd, vbNormalFocus)

Let me know if you need other info - and thanks in advance.
0
 
LVL 8

Expert Comment

by:mrmick
ID: 1443956
Sorry kbakst,

I can see you're creating an empty file, but that's it.  Your shell command doesn't tell me anything about the app that is recording the file... which is fine, because I thought your were using an API call - for which I've got documentation and could probably catch a mistake its usage.

I would note this though, the file you've created isn't limited by your VB code.  Your file could be filled with as much data as desired with the only limitation being the amount of free space on your hard drive.  I'd look into the app you're shelling.


0
 
LVL 4

Expert Comment

by:rantanen
ID: 1443957
Windows Sound Recorder determines based on the sound quality you want a default length for recording session. It varies from about seven seconds to one minute if I recall correctly. So this is max length of one "recording session". But you can keep adding to the end of the file similar chunks as many as you like. Not a very nice feature if you want to record your version of "I Did It My Way", but the  best you can do with Sound Recorder.

If you have to be able to record longer at one time then you have to look at other solutions.
0
 

Author Comment

by:kbakst
ID: 1443958
mrmick - the application I'm using is Windows Sound Recorder - see rantanen's proposed answer .

rantanen - what other solutions?  I'll be happy to explore other options.
0
 
LVL 8

Expert Comment

by:mrmick
ID: 1443959
I'm not familiar with the comments rantanen has made, besides,  when you are seeking a clarification and/or you are not sure the answer does not satisfy your question... don't reject it... Instead, post a comment; let rantanen provide more of an explanation if he/she has one.  You might consider uping the points if you don't get a response.

My knowledge of the Windows Sound Recorder is very limited.  I couldn't offer any more on this issue anyway, but I do  recommend that you try rantanen's suggestion regarding concatenating the bit streams.

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 8

Expert Comment

by:mrmick
ID: 1443960
I did find a bit of info that's not quite as simple to implement...

From the Win32 SDK:

Recording Waveform Audio
If the MCI waveform-audio recording services do not meet the specifications of your application, you can handle waveform-audio recording using the waveform-audio services. For more information, see MCI. Waveform-Audio Input Data Types.   The following data types are defined for  aveform-audio input functions:
Type      Description
HWAVEIN      Handle of an open waveform-audio input device.
WAVEFORMATEX      Structure that specifies the data formats supported by a particular waveform-audio input device. This structure is also used for waveform-audio output devices.
WAVEHDR      Structure used as a header for a block of waveform-audio input data. This structure is also used for waveform-audio output devices.
WAVEINCAPS      Structure used to inquire about the capabilities of a particular waveform-audio input device.
 
Querying Waveform-Audio Input Devices
Before recording waveform audio, you should call the waveInGetDevCaps function to determine the waveform-audio input capabilities of the system. This function fills a WAVEINCAPS structure with information about the capabilities of a specified device. This information includes the manufacturer and product identifiers, a product name for the device, and the version number of the device driver. In addition, the WAVEINCAPS structure provides information about the standard waveform-audio formats that the device supports.
Opening Waveform-Audio Input Devices
Use the waveInOpen function to open a waveform-audio input device for recording. This function opens the device associated with the specified device identifier and returns a handle of the open device by writing the handle of a specified memory location.
Some multimedia computers have multiple waveform-audio input devices. Unless you know you want to open a specific waveform-audio input device in a system, you should use the WAVE_MAPPER constant for the device identifier when you open a device. The waveInOpen function will choose the device in the system best able to record in the specified data format.
Managing Waveform-Audio Recording
After you open a waveform-audio input device, you can begin recording waveform-audio data. Waveform-audio data is recorded into application-supplied buffers specified by a WAVEHDR structure. These data blocks must be prepared before they are used; for more information, see Audio Data Blocks.
Windows provides the following functions to manage waveform-audio recording.
Function      Description
waveInAddBuffer      Sends a buffer to the device driver so it can be filled with recorded waveform-audio data.
waveInReset      Stops waveform-audio recording and marks all pending buffers as done.
waveInStart      Starts waveform-audio recording.
waveInStop      Stops waveform-audio recording.
 
Use the waveInAddBuffer function to send buffers to the device driver. As the buffers are filled with recorded waveform-audio data, the application is notified with a window message, callback message, thread message, or event, depending on the flag specified when the device was opened.
Before you begin recording by using waveInStart, you should send at least one buffer to the driver, or incoming data could be lost.
Before closing the device using waveInClose, call waveInReset to mark any pending data blocks as being done.
Using Window Messages to Manage Waveform-Audio Recording
The following messages can be sent to a window procedure function for managing waveform-audio recording.
Message      Description
MM_WIM_CLOSE      Sent when the device is closed by using the waveInClose function.
MM_WIM_DATA      Sent when the device driver is finished with a buffer sent by using the waveInAddBuffer function.
MM_WIM_OPEN      Sent when the device is opened by using the waveInOpen function.
 
The lParam parameter of MM_WIM_DATA specifies a pointer to a WAVEHDR structure that identifies the buffer. This buffer might not be completely filled with waveform-audio data; recording can stop before the buffer is filled. Use the dwBytesRecorded member of the WAVEHDR structure to determine the amount of valid data present in the buffer.
The most useful message is probably MM_WIM_DATA. When your application finishes using the data block sent by the device driver, you can clean up and free the data block. Unless you need to allocate memory or initialize variables, you probably do not need to use the MM_WIM_OPEN and MM_WIM_CLOSE messages.
The callback function for waveform-audio input devices is supplied by the application. For information about this callback function, see the waveInProc function.

0
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1443961
I think there is a much simpler solution - launch sound recorder, and use sendkeys to save the file.
0
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1443962
I think there is a much simpler solution - launch sound recorder, and use sendkeys to save the file.
0
 
LVL 4

Accepted Solution

by:
rantanen earned 150 total points
ID: 1443963
Here's a minimal wave recorder/player based on mci API calls (another alternative).

Option Explicit
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private szReturnString As String * 256

Private Sub btnPlay_Click()
    Dim lRetValue As Long
   
    lRetValue = mciSendString("play c:\test.wav", szReturnString, 256, 0)
End Sub


Private Sub btnRecord_Click()
    Dim lRetValue As Long
   
    lRetValue = mciSendString("open new type waveaudio alias recwave", szReturnString, 256, 0)
    lRetValue = mciSendString("set recwave bitspersample 8", szReturnString, 256, 0)
    lRetValue = mciSendString("set recwave samplespersec 22050", szReturnString, 256, 0)
    lRetValue = mciSendString("set recwave channels 1", szReturnString, 256, 0)
    lRetValue = mciSendString("record recwave", szReturnString, 256, 0)
   
End Sub


Private Sub btnSave_Click()
    Dim lRetValue As Long
   
    lRetValue = mciSendString("save recwave c:\test.wav", szReturnString, 256, 0)
    lRetValue = mciSendString("close recwave", szReturnString, 256, 0)
End Sub

' This stops recording, not playback
Private Sub btnStop_Click()
    Dim lRetValue As Long
   
    lRetValue = mciSendString("stop recwave", szReturnString, 256, 0)
End Sub

It is minimal as I said, no error handling, no fancy UI etc. but it works and hopefully gives you ideas.

0
 

Author Comment

by:kbakst
ID: 1443964
Ok - almost there.  Only problem is that the commands you included to change the sampling rate and channels don't work - and what I end up with is a wave file with really bad sound quality.

Points increased to 150 (due to added difficulty)

0
 
LVL 4

Expert Comment

by:rantanen
ID: 1443965
Have you tried different values, eg.

lRetValue = mciSendString("set recwave bitspersample 16", szReturnString, 256, 0)
lRetValue = mciSendString("set recwave samplespersec 44100", szReturnString, 256, 0)

Which soundcard do you have?

0
 

Author Comment

by:kbakst
ID: 1443966
Turns out you need to set ALL of the properties - including the average bytes and alignment.  Got it working just fine - thanks for the help.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now