L
Larry
Hi,
I am in the process of coding a C++ programm to capture input audio data
and save it real time (wave file) by using the waveForm API.
Now this is the code I have written so far:
<<main.cpp
#include <windows.h>
#pragma comment (lib, "winmm.lib")
#include <mmsystem.h>
#include <iostream>
#include <fstream>
#include <stdlib.h> // Define "system" function
#include <string>
#define system_buf_len 4096
void CALLBACK waveInProc(HWAVEIN hwi,UINT uMsg,DWORD dwInstance,DWORD
dwParam1,DWORD dwParam2);
bool addbuffer(WAVEHDR *pWaveHdr);
int main()
{
// Definisco la struttura WAVEFORMATEX
WAVEFORMATEX waveFormat;
waveFormat.wFormatTag = WAVE_FORMAT_PCM;
waveFormat.wBitsPerSample = 16;
waveFormat.nChannels = 1;
waveFormat.nSamplesPerSec = 8000;
waveFormat.nBlockAlign = (waveFormat.nChannels *
waveFormat.wBitsPerSample) / 8;
waveFormat.nAvgBytesPerSec = (waveFormat.nSamplesPerSec *
waveFormat.nBlockAlign);
waveFormat.cbSize = 0;
MMRESULT mmres; // ...
HWAVEIN phvi; // Handle for the input device
UINT uDevice = 0; // Device id "Gruppo Microfoni"
// waveInOpen
mmres = waveInOpen(&phvi,
uDevice,
(LPWAVEFORMATEX)&waveFormat,
(DWORD)waveInProc,
0,
CALLBACK_FUNCTION
);
// Prepare Buffer
int i=0;
int num_buff = 3;
WAVEHDR *buffer = (WAVEHDR *) malloc(sizeof(WAVEHDR)*num_buff);
for (i=0; i<num_buff; i++)
{
buffer.lpData = (LPSTR) malloc(system_buf_len);
buffer.dwBufferLength = system_buf_len;
buffer.dwBytesRecorded = 0;
buffer.dwUser = 0;
buffer.dwFlags = 0;
buffer.dwLoops = 0;
waveInPrepareHeader(phvi, &buffer, sizeof(WAVEHDR));
waveInAddBuffer(phvi, &buffer, sizeof(WAVEHDR));
}
//waveInStart;
waveInStart(phvi);
system("pause");
//waveInClose;
waveInClose(phvi);
return 0;
}
void CALLBACK waveInProc(HWAVEIN hwi,UINT uMsg,DWORD dwInstance,DWORD
dwParam1,DWORD dwParam2)
{
WAVEHDR* pWaveHdr;
switch(uMsg)
{
case MM_WIM_DATA:
pWaveHdr = ((WAVEHDR*)dwParam1);
if (pWaveHdr && hwi)
{
if (pWaveHdr->dwFlags && WHDR_DONE == WHDR_DONE)
{
addbuffer(pWaveHdr);
waveInAddBuffer(hwi, pWaveHdr, sizeof(WAVEHDR));
}
}
break;
case MM_WIM_OPEN:
std::cout << "MM_WIN_OPEN" << std::endl;
break;
case MM_WIM_CLOSE:
std::cout << "MM_WIN_CLOSE" << std::endl;
break;
}
}
bool addbuffer(WAVEHDR *pWaveHdr)
{
std::cout << pWaveHdr->dwBytesRecorded << std::endl;
char * buff = pWaveHdr->lpData;
std:fstream usc;
usc.open("ciao.wave", std::ios::app|std::ios::binary);
usc.write(buff,pWaveHdr->dwBytesRecorded);
usc.close();
return true;
}
<</main.cpp
I run across sort of similar code where it uses a different way to allocate
memory:
<<diff.cpp
pwf.wBitsPerSample= 16;
pwf.wf.nChannels = 1;
pwf.wf.nSamplesPerSec = 8000;
pwf.wf.wFormatTag = WAVE_FORMAT_PCM;
pwf.wf.nBlockAlign =
pwf.wf.nChannels * pwf.wBitsPerSample / 8;
pwf.wf.nAvgBytesPerSec =
pwf.wf.nSamplesPerSec * pwf.wf.nBlockAlign;
if (waveInOpen(&rip->hwi,
/*WAVE_MAPPER*/ 0, (LPWAVEFORMATEX)&pwf,
(DWORD) rip->eventh, 0, CALLBACK_EVENT )) {
printf("Couldn't open sound device. Leaving..\n");
goto problem;
}
/* Preparing system buffers */
sb = (WAVEHDR**) malloc(sizeof(WAVEHDR**) * system_buf_num);
if(!sb)
goto problem;
for (i = 0; i < system_buf_num; i++)
sb = NULL;
for (i = 0; i < system_buf_num; i++) {
count = i;
sb = (WAVEHDR*) malloc(sizeof(WAVEHDR));
if (sb == NULL) {
put_debug_message("malloc() error!\n");
goto problem;
}
sb->lpData = (LPBYTE) malloc(system_buf_len);
sb->dwBufferLength = system_buf_len;
sb->dwBytesRecorded = 0;
sb->dwUser = 0;
sb->dwFlags = 0;
sb->dwLoops = 0;
if(!sb->lpData)
goto problem;
if (waveInPrepareHeader(rip->hwi, sb, sizeof(WAVEHDR))) {
put_debug_message("waveInPrepareHeader problem!\n");
goto problem;
}
if (waveInAddBuffer(rip->hwi, sb, sizeof(WAVEHDR))) {
put_debug_message("waveInAddBuffer problem!\n");
goto problem;
}
}
<</diff.cpp
So, basically I am using:
WAVEHDR *buffer = (WAVEHDR *) malloc(sizeof(WAVEHDR)*num_buff);
whereas the other code is using:
sb = (WAVEHDR**) malloc(sizeof(WAVEHDR**) * system_buf_num);
Now, I am really interested in this latter form of allocating! what is the
main differences?
How should I declare "sb" before allocating it? would: /WAVEHDR *sb/ ok?
Also, in my code I use: buffer.lpData to access data...in the other code
is used: sb->lpData... why?
thanks
I am in the process of coding a C++ programm to capture input audio data
and save it real time (wave file) by using the waveForm API.
Now this is the code I have written so far:
<<main.cpp
#include <windows.h>
#pragma comment (lib, "winmm.lib")
#include <mmsystem.h>
#include <iostream>
#include <fstream>
#include <stdlib.h> // Define "system" function
#include <string>
#define system_buf_len 4096
void CALLBACK waveInProc(HWAVEIN hwi,UINT uMsg,DWORD dwInstance,DWORD
dwParam1,DWORD dwParam2);
bool addbuffer(WAVEHDR *pWaveHdr);
int main()
{
// Definisco la struttura WAVEFORMATEX
WAVEFORMATEX waveFormat;
waveFormat.wFormatTag = WAVE_FORMAT_PCM;
waveFormat.wBitsPerSample = 16;
waveFormat.nChannels = 1;
waveFormat.nSamplesPerSec = 8000;
waveFormat.nBlockAlign = (waveFormat.nChannels *
waveFormat.wBitsPerSample) / 8;
waveFormat.nAvgBytesPerSec = (waveFormat.nSamplesPerSec *
waveFormat.nBlockAlign);
waveFormat.cbSize = 0;
MMRESULT mmres; // ...
HWAVEIN phvi; // Handle for the input device
UINT uDevice = 0; // Device id "Gruppo Microfoni"
// waveInOpen
mmres = waveInOpen(&phvi,
uDevice,
(LPWAVEFORMATEX)&waveFormat,
(DWORD)waveInProc,
0,
CALLBACK_FUNCTION
);
// Prepare Buffer
int i=0;
int num_buff = 3;
WAVEHDR *buffer = (WAVEHDR *) malloc(sizeof(WAVEHDR)*num_buff);
for (i=0; i<num_buff; i++)
{
buffer.lpData = (LPSTR) malloc(system_buf_len);
buffer.dwBufferLength = system_buf_len;
buffer.dwBytesRecorded = 0;
buffer.dwUser = 0;
buffer.dwFlags = 0;
buffer.dwLoops = 0;
waveInPrepareHeader(phvi, &buffer, sizeof(WAVEHDR));
waveInAddBuffer(phvi, &buffer, sizeof(WAVEHDR));
}
//waveInStart;
waveInStart(phvi);
system("pause");
//waveInClose;
waveInClose(phvi);
return 0;
}
void CALLBACK waveInProc(HWAVEIN hwi,UINT uMsg,DWORD dwInstance,DWORD
dwParam1,DWORD dwParam2)
{
WAVEHDR* pWaveHdr;
switch(uMsg)
{
case MM_WIM_DATA:
pWaveHdr = ((WAVEHDR*)dwParam1);
if (pWaveHdr && hwi)
{
if (pWaveHdr->dwFlags && WHDR_DONE == WHDR_DONE)
{
addbuffer(pWaveHdr);
waveInAddBuffer(hwi, pWaveHdr, sizeof(WAVEHDR));
}
}
break;
case MM_WIM_OPEN:
std::cout << "MM_WIN_OPEN" << std::endl;
break;
case MM_WIM_CLOSE:
std::cout << "MM_WIN_CLOSE" << std::endl;
break;
}
}
bool addbuffer(WAVEHDR *pWaveHdr)
{
std::cout << pWaveHdr->dwBytesRecorded << std::endl;
char * buff = pWaveHdr->lpData;
std:fstream usc;
usc.open("ciao.wave", std::ios::app|std::ios::binary);
usc.write(buff,pWaveHdr->dwBytesRecorded);
usc.close();
return true;
}
<</main.cpp
I run across sort of similar code where it uses a different way to allocate
memory:
<<diff.cpp
pwf.wBitsPerSample= 16;
pwf.wf.nChannels = 1;
pwf.wf.nSamplesPerSec = 8000;
pwf.wf.wFormatTag = WAVE_FORMAT_PCM;
pwf.wf.nBlockAlign =
pwf.wf.nChannels * pwf.wBitsPerSample / 8;
pwf.wf.nAvgBytesPerSec =
pwf.wf.nSamplesPerSec * pwf.wf.nBlockAlign;
if (waveInOpen(&rip->hwi,
/*WAVE_MAPPER*/ 0, (LPWAVEFORMATEX)&pwf,
(DWORD) rip->eventh, 0, CALLBACK_EVENT )) {
printf("Couldn't open sound device. Leaving..\n");
goto problem;
}
/* Preparing system buffers */
sb = (WAVEHDR**) malloc(sizeof(WAVEHDR**) * system_buf_num);
if(!sb)
goto problem;
for (i = 0; i < system_buf_num; i++)
sb = NULL;
for (i = 0; i < system_buf_num; i++) {
count = i;
sb = (WAVEHDR*) malloc(sizeof(WAVEHDR));
if (sb == NULL) {
put_debug_message("malloc() error!\n");
goto problem;
}
sb->lpData = (LPBYTE) malloc(system_buf_len);
sb->dwBufferLength = system_buf_len;
sb->dwBytesRecorded = 0;
sb->dwUser = 0;
sb->dwFlags = 0;
sb->dwLoops = 0;
if(!sb->lpData)
goto problem;
if (waveInPrepareHeader(rip->hwi, sb, sizeof(WAVEHDR))) {
put_debug_message("waveInPrepareHeader problem!\n");
goto problem;
}
if (waveInAddBuffer(rip->hwi, sb, sizeof(WAVEHDR))) {
put_debug_message("waveInAddBuffer problem!\n");
goto problem;
}
}
<</diff.cpp
So, basically I am using:
WAVEHDR *buffer = (WAVEHDR *) malloc(sizeof(WAVEHDR)*num_buff);
whereas the other code is using:
sb = (WAVEHDR**) malloc(sizeof(WAVEHDR**) * system_buf_num);
Now, I am really interested in this latter form of allocating! what is the
main differences?
How should I declare "sb" before allocating it? would: /WAVEHDR *sb/ ok?
Also, in my code I use: buffer.lpData to access data...in the other code
is used: sb->lpData... why?
thanks