I have spent the morning playing with the audio API, but so far haven't been able to confirm a proper setup. If anyone has experience in this field, I would appreciate a little guidance. Here are the steps I am following:
1) Initialize a WAVEFORMATEX structure to zero and set the sample rate, bits per second, channels, block alignment, average bytes per second, and set the format to WAVE_FORMAT_PCM.
sr = 8000;
bps = 8;
c = 1;
ba = bps * c;
abps = ba * sr;
f = WAVE_FORMAT_PCM;
2) Initialize a WAVEHDR structure to zero and set the buffer length to a size compatible with the value of abps from above, pass it a buffer of that size, and set the flags to WHDR_BEGINLOOP|WHDR_ENDLOOP.
3) Format the header.
[ waveInOpen(&hwavein, WAVE_MAPPED, &wavefmtx, hwnd, 0, CALLBACK_WINDOW|WAVE_MAPPER); ]
7) Unformat the header.
8) Free the buffer.
In the Window procedure, I am monitoring for the MM_WIM_OPEN, MM_WIM_CLOSE, and MM_WIM_DATA messages, and that is the problem. I am not recieving them as of yet, which leads me to believe I am doing something wrong. Any advice would be highly appreciated...
Lets see what I can remember...
First in your WAVEFORMATEX struct..
I believe your nBlockAlign is wrong. Well you multiply correctly but you need to divide out by 8.
nBlockAlign = (nChannels * wBitsPerSample) / 8;
You also want to use waveOutOpen( ) prototyped as follows:
Make sure the return code is MMSYSERR_NOERROR. The main value we are interested in is the hWaveOut value. This is a handle to our wave-output device obviously.
MMRESULT waveOutOpen( LPHWAVEOUT hWaveOut, UINT nDeviceID, LPWAVEFORMATEX pWaveFormatEx, DWORD dwCallback, DWORD dwCallbackInstance, DWORD dwOpen );
Second parameter should be the symbolic constant WAVE_MAPPER to indicate you want the default system wave output device.
Third parameter is your struct you already initialized.
Fourth parameter is a callback. This describes how we want to be notified of certain events. Use the handle to some window casted to a DWORD.
Fifth parameter is the actual callback data. Just set this to 0 we will ignore it. We will use our windows procedure to get messages.
Sixth parameter should be another constant CALLBACK_WINDOW to request messages.
Once you successfully open the device it will send a message MM_WOM_OPEN. This is a good thing ;)
Now we can prepare our "wave header struct".
Looking back over what you wrote I think you probably have this part down. I think you were just incorrectly calling waveInOpen instead of the waveOutOpen. A reasonable mistake. Let me know if you need the rest of the stuff I was going to type. Hopefully this will get you going!
Hey, thanks Wiz, I can see now why my calculation was wrong...need to convert bits to bytes and all...:)
I actually mean to open the mic but honestly don't know where to go from there, just needed the AOK of a system message to keep me going! OK so I will try outputing before inputting first. Eventually my goal is to analyze microphone data. I will let y'all know how it goes....
P.S. Merry Cristmas all. Have a safe and happy holiday.
I once made an application for analyzing audio received by a standard soundcard using the Win32 Audio API. The following is the procedure I used to open a wave device:
1. Check if there is a wave-device available using: waveInGetNumDevs(). The result should be larger than zero.
2. Check if the available wave-device is capable for recording purposes, using: waveInGetDevCaps(). The result should be MMSYSERR_NOERROR.
3. Create a recording thread, put it in suspended mode first.
4. Define the wave format.
5. Open the wave device using: waveInOpen().
6. Initialise recording process. Which consist of preparing the headers, adding the buffers and starting the process using waveInStart().
The real starting of the recording begins when resuming the created recording thread.
And for stopping the recording, the thread is put to suspended again and waveInStop() and then waveInReset() are called to finish communication with the soundcard.
There is a good section on FFT's and frequency analysis here and code for a simple windows app here
Another great site with good tutorials:
Since you're working with Windows, especially the Visual C++ Class may be interesting.
Thank you all for the intelligent replies. It's interesting you posted that reliable software link, Stoner. That was actually part of the inspiration for this project. :) I am basically going to try to implement some sort of voice recognition system, and I will plot the signals while I study them, probably using the FFT algorithm. Shiro, thanks for the tips you gave too. This should prove to be an interesting project!