r2d2droids
Member level 2
hi all,
i send analog audio signals using pic (A/D module) via sci to FT232 chip. after that i wanna write these digital signals to a specified buffer and after the buffer fills, sends the signals to audio device for playback.
i found a source code which can record and play the wave using microphone. but i couldnt adapt this code to my project.
the code is shown below:
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop
#include "WaveformAPI.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
/*********************************************************************\
* WaveHeader (WAVEHDR) wrapper functions
\*********************************************************************/
bool WaveMakeHeader(unsigned long ulSize, HGLOBAL &HData, HGLOBAL &HWaveHdr,
LPSTR &lpData, LPWAVEHDR &lpWaveHdr)
{
HData = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, ulSize);
if (!HData) return false;
lpData = (LPSTR)GlobalLock(HData);
if (!lpData)
{
GlobalFree(HData);
return false;
}
HWaveHdr = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, sizeof(WAVEHDR));
if (!HWaveHdr)
{
GlobalUnlock(HData);
GlobalFree(HData);
return false;
}
lpWaveHdr = (LPWAVEHDR)GlobalLock(HWaveHdr);
if (!lpWaveHdr)
{
GlobalUnlock(HWaveHdr);
GlobalFree(HWaveHdr);
GlobalUnlock(HData);
GlobalFree(HData);
return false;
}
ZeroMemory(lpWaveHdr, sizeof(WAVEHDR));
lpWaveHdr->lpData = lpData;
lpWaveHdr->dwBufferLength = ulSize;
return true;
}
//---------------------------------------------------------------------------
void WaveFreeHeader(HGLOBAL &HData, HGLOBAL &HWaveHdr)
{
GlobalUnlock(HWaveHdr);
GlobalFree(HWaveHdr);
GlobalUnlock(HData);
GlobalFree(HData);
}
//---------------------------------------------------------------------------
/*********************************************************************\
* WaveIn (recording) wrapper functions
\*********************************************************************/
bool WaveRecordOpen(LPHWAVEIN lphwi, HWND Hwnd, int nChannels,
long lFrequency, int nBits)
{
WAVEFORMATEX wfx;
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = (WORD)nChannels;
wfx.nSamplesPerSec = (DWORD)lFrequency;
wfx.wBitsPerSample = (WORD)nBits;
wfx.nBlockAlign = (WORD)((wfx.nChannels * wfx.wBitsPerSample) / 8);
wfx.nAvgBytesPerSec = (wfx.nSamplesPerSec * wfx.nBlockAlign);
wfx.cbSize = 0;
MMRESULT result = waveInOpen(lphwi, WAVE_MAPPER, &wfx, (LONG)Hwnd, NULL,
CALLBACK_WINDOW);
if (result == MMSYSERR_NOERROR) return true;
return false;
}
//---------------------------------------------------------------------------
bool WaveRecordBegin(HWAVEIN hwi, LPWAVEHDR &lpWaveHdr)
{
MMRESULT result = waveInPrepareHeader(hwi, lpWaveHdr, sizeof(WAVEHDR));
if (result == MMSYSERR_NOERROR)
{
MMRESULT result = waveInAddBuffer(hwi, lpWaveHdr, sizeof(WAVEHDR));
if (result == MMSYSERR_NOERROR)
{
MMRESULT result = waveInStart(hwi);
if (result == MMSYSERR_NOERROR) return true;
}
}
return false;
}
//---------------------------------------------------------------------------
void WaveRecordEnd(HWAVEIN hwi, LPWAVEHDR &lpWaveHdr)
{
waveInStop(hwi);
waveInReset(hwi);
waveInUnprepareHeader(hwi, lpWaveHdr, sizeof(WAVEHDR));
}
//---------------------------------------------------------------------------
void WaveRecordClose(HWAVEIN hwi)
{
waveInReset(hwi);
waveInClose(hwi);
}
//---------------------------------------------------------------------------
/*********************************************************************\
* WaveOut (playback) wrapper functions
\*********************************************************************/
bool WavePlayOpen(LPHWAVEOUT lphwo, HWND Hwnd, int nChannels,
long lFrequency, int nBits)
{
WAVEFORMATEX wfx;
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = (WORD)nChannels;
wfx.nSamplesPerSec = (DWORD)lFrequency;
wfx.wBitsPerSample = (WORD)nBits;
wfx.nBlockAlign = (WORD)((wfx.nChannels * wfx.wBitsPerSample) / 8);
wfx.nAvgBytesPerSec = (wfx.nSamplesPerSec * wfx.nBlockAlign);
wfx.cbSize = 0;
MMRESULT result = waveOutOpen(lphwo, WAVE_MAPPER, &wfx, (LONG)Hwnd, NULL,
CALLBACK_WINDOW);
if (result == MMSYSERR_NOERROR) return true;
return false;
}
//---------------------------------------------------------------------------
bool WavePlayBegin(HWAVEOUT hwo, LPWAVEHDR &lpWaveHdr)
{
MMRESULT result = waveOutPrepareHeader(hwo, lpWaveHdr, sizeof(WAVEHDR));
if (result == MMSYSERR_NOERROR)
{
MMRESULT result = waveOutWrite(hwo, lpWaveHdr, sizeof(WAVEHDR));
if (result == MMSYSERR_NOERROR) return true;
}
return false;
}
//---------------------------------------------------------------------------
void WavePlayEnd(HWAVEOUT hwo, LPWAVEHDR &lpWaveHdr)
{
waveOutReset(hwo);
waveOutUnprepareHeader(hwo, lpWaveHdr, sizeof(WAVEHDR));
}
//---------------------------------------------------------------------------
void WavePlayClose(HWAVEOUT hwo)
{
waveOutReset(hwo);
waveOutClose(hwo);
}
//---------------------------------------------------------------------------
/*********************************************************************\
* Example usage...
\*********************************************************************/
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
ulSize = 1000000;
FAllocated = false;
FRecording = false;
FPlaying = false;
TrackBar1->Max = 0xFFFF;
TrackBar1->Frequency = 1000;
DWORD current_volume;
waveOutGetVolume(0, ¤t_volume);
TrackBar1->Position = TrackBar1->Max - LOWORD(current_volume);
}
//---------------------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
if (FAllocated) WaveFreeHeader(HData, HWaveHdr);
if (FRecording) WaveRecordClose(hwi);
if (FPlaying) WavePlayClose(hwo);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RecordButtonClick(TObject *Sender)
{
if (WaveRecordOpen(&hwi, Handle, 2, 44100, 16))
{
if (WaveMakeHeader(ulSize, HData, HWaveHdr, lpData, lpWaveHdr))
{
FAllocated = true;
if (WaveRecordBegin(hwi, lpWaveHdr))
{
FRecording = true;
Caption = "Recording...";
Shape1->Brush->Color = clRed;
StopButton->Enabled = true;
RecordButton->Enabled = false;
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StopButtonClick(TObject *Sender)
{
if (FRecording)
{
WaveRecordEnd(hwi, lpWaveHdr);
FRecording = false;
}
if (FPlaying)
{
WavePlayEnd(hwo, lpWaveHdr);
FPlaying = false;
Shape2->Brush->Color = clBlack;
StopButton->Enabled = false;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MMWimData(TMessage &Msg)
{
Caption = "DONE RECORDING";
if (FRecording) WaveRecordEnd(hwi, lpWaveHdr);
WaveRecordClose(hwi);
if (WavePlayOpen(&hwo, Handle, 2, 44100, 16))
{
ShowMessage("Click OK to begin playback! If you're seeing this "
"message before you clicked the stop button, then "
"the buffer ran out (increase ulSize).");
if (WavePlayBegin(hwo, lpWaveHdr))
{
FPlaying = true;
Caption = "Playing...";
Shape1->Brush->Color = clBlack;
Shape2->Brush->Color = clGreen;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MMWomDone(TMessage &Msg)
{
Caption = "DONE PLAYING";
WavePlayClose(hwo);
RecordButton->Enabled = true;
StopButton->Enabled = false;
Shape2->Brush->Color = clBlack;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TrackBar1Change(TObject *Sender)
{
DWORD new_volume =
(DWORD)MAKEWPARAM(TrackBar1->Max - TrackBar1->Position,
TrackBar1->Max - TrackBar1->Position);
waveOutSetVolume((HWAVEOUT)WAVE_MAPPER, new_volume);
}
//---------------------------------------------------------------------------
does anyone know how to adapt this source code to my project or can show me another audio steraming project source code via usb?
thx...
i send analog audio signals using pic (A/D module) via sci to FT232 chip. after that i wanna write these digital signals to a specified buffer and after the buffer fills, sends the signals to audio device for playback.
i found a source code which can record and play the wave using microphone. but i couldnt adapt this code to my project.
the code is shown below:
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop
#include "WaveformAPI.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
/*********************************************************************\
* WaveHeader (WAVEHDR) wrapper functions
\*********************************************************************/
bool WaveMakeHeader(unsigned long ulSize, HGLOBAL &HData, HGLOBAL &HWaveHdr,
LPSTR &lpData, LPWAVEHDR &lpWaveHdr)
{
HData = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, ulSize);
if (!HData) return false;
lpData = (LPSTR)GlobalLock(HData);
if (!lpData)
{
GlobalFree(HData);
return false;
}
HWaveHdr = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, sizeof(WAVEHDR));
if (!HWaveHdr)
{
GlobalUnlock(HData);
GlobalFree(HData);
return false;
}
lpWaveHdr = (LPWAVEHDR)GlobalLock(HWaveHdr);
if (!lpWaveHdr)
{
GlobalUnlock(HWaveHdr);
GlobalFree(HWaveHdr);
GlobalUnlock(HData);
GlobalFree(HData);
return false;
}
ZeroMemory(lpWaveHdr, sizeof(WAVEHDR));
lpWaveHdr->lpData = lpData;
lpWaveHdr->dwBufferLength = ulSize;
return true;
}
//---------------------------------------------------------------------------
void WaveFreeHeader(HGLOBAL &HData, HGLOBAL &HWaveHdr)
{
GlobalUnlock(HWaveHdr);
GlobalFree(HWaveHdr);
GlobalUnlock(HData);
GlobalFree(HData);
}
//---------------------------------------------------------------------------
/*********************************************************************\
* WaveIn (recording) wrapper functions
\*********************************************************************/
bool WaveRecordOpen(LPHWAVEIN lphwi, HWND Hwnd, int nChannels,
long lFrequency, int nBits)
{
WAVEFORMATEX wfx;
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = (WORD)nChannels;
wfx.nSamplesPerSec = (DWORD)lFrequency;
wfx.wBitsPerSample = (WORD)nBits;
wfx.nBlockAlign = (WORD)((wfx.nChannels * wfx.wBitsPerSample) / 8);
wfx.nAvgBytesPerSec = (wfx.nSamplesPerSec * wfx.nBlockAlign);
wfx.cbSize = 0;
MMRESULT result = waveInOpen(lphwi, WAVE_MAPPER, &wfx, (LONG)Hwnd, NULL,
CALLBACK_WINDOW);
if (result == MMSYSERR_NOERROR) return true;
return false;
}
//---------------------------------------------------------------------------
bool WaveRecordBegin(HWAVEIN hwi, LPWAVEHDR &lpWaveHdr)
{
MMRESULT result = waveInPrepareHeader(hwi, lpWaveHdr, sizeof(WAVEHDR));
if (result == MMSYSERR_NOERROR)
{
MMRESULT result = waveInAddBuffer(hwi, lpWaveHdr, sizeof(WAVEHDR));
if (result == MMSYSERR_NOERROR)
{
MMRESULT result = waveInStart(hwi);
if (result == MMSYSERR_NOERROR) return true;
}
}
return false;
}
//---------------------------------------------------------------------------
void WaveRecordEnd(HWAVEIN hwi, LPWAVEHDR &lpWaveHdr)
{
waveInStop(hwi);
waveInReset(hwi);
waveInUnprepareHeader(hwi, lpWaveHdr, sizeof(WAVEHDR));
}
//---------------------------------------------------------------------------
void WaveRecordClose(HWAVEIN hwi)
{
waveInReset(hwi);
waveInClose(hwi);
}
//---------------------------------------------------------------------------
/*********************************************************************\
* WaveOut (playback) wrapper functions
\*********************************************************************/
bool WavePlayOpen(LPHWAVEOUT lphwo, HWND Hwnd, int nChannels,
long lFrequency, int nBits)
{
WAVEFORMATEX wfx;
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = (WORD)nChannels;
wfx.nSamplesPerSec = (DWORD)lFrequency;
wfx.wBitsPerSample = (WORD)nBits;
wfx.nBlockAlign = (WORD)((wfx.nChannels * wfx.wBitsPerSample) / 8);
wfx.nAvgBytesPerSec = (wfx.nSamplesPerSec * wfx.nBlockAlign);
wfx.cbSize = 0;
MMRESULT result = waveOutOpen(lphwo, WAVE_MAPPER, &wfx, (LONG)Hwnd, NULL,
CALLBACK_WINDOW);
if (result == MMSYSERR_NOERROR) return true;
return false;
}
//---------------------------------------------------------------------------
bool WavePlayBegin(HWAVEOUT hwo, LPWAVEHDR &lpWaveHdr)
{
MMRESULT result = waveOutPrepareHeader(hwo, lpWaveHdr, sizeof(WAVEHDR));
if (result == MMSYSERR_NOERROR)
{
MMRESULT result = waveOutWrite(hwo, lpWaveHdr, sizeof(WAVEHDR));
if (result == MMSYSERR_NOERROR) return true;
}
return false;
}
//---------------------------------------------------------------------------
void WavePlayEnd(HWAVEOUT hwo, LPWAVEHDR &lpWaveHdr)
{
waveOutReset(hwo);
waveOutUnprepareHeader(hwo, lpWaveHdr, sizeof(WAVEHDR));
}
//---------------------------------------------------------------------------
void WavePlayClose(HWAVEOUT hwo)
{
waveOutReset(hwo);
waveOutClose(hwo);
}
//---------------------------------------------------------------------------
/*********************************************************************\
* Example usage...
\*********************************************************************/
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
ulSize = 1000000;
FAllocated = false;
FRecording = false;
FPlaying = false;
TrackBar1->Max = 0xFFFF;
TrackBar1->Frequency = 1000;
DWORD current_volume;
waveOutGetVolume(0, ¤t_volume);
TrackBar1->Position = TrackBar1->Max - LOWORD(current_volume);
}
//---------------------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
if (FAllocated) WaveFreeHeader(HData, HWaveHdr);
if (FRecording) WaveRecordClose(hwi);
if (FPlaying) WavePlayClose(hwo);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RecordButtonClick(TObject *Sender)
{
if (WaveRecordOpen(&hwi, Handle, 2, 44100, 16))
{
if (WaveMakeHeader(ulSize, HData, HWaveHdr, lpData, lpWaveHdr))
{
FAllocated = true;
if (WaveRecordBegin(hwi, lpWaveHdr))
{
FRecording = true;
Caption = "Recording...";
Shape1->Brush->Color = clRed;
StopButton->Enabled = true;
RecordButton->Enabled = false;
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StopButtonClick(TObject *Sender)
{
if (FRecording)
{
WaveRecordEnd(hwi, lpWaveHdr);
FRecording = false;
}
if (FPlaying)
{
WavePlayEnd(hwo, lpWaveHdr);
FPlaying = false;
Shape2->Brush->Color = clBlack;
StopButton->Enabled = false;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MMWimData(TMessage &Msg)
{
Caption = "DONE RECORDING";
if (FRecording) WaveRecordEnd(hwi, lpWaveHdr);
WaveRecordClose(hwi);
if (WavePlayOpen(&hwo, Handle, 2, 44100, 16))
{
ShowMessage("Click OK to begin playback! If you're seeing this "
"message before you clicked the stop button, then "
"the buffer ran out (increase ulSize).");
if (WavePlayBegin(hwo, lpWaveHdr))
{
FPlaying = true;
Caption = "Playing...";
Shape1->Brush->Color = clBlack;
Shape2->Brush->Color = clGreen;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MMWomDone(TMessage &Msg)
{
Caption = "DONE PLAYING";
WavePlayClose(hwo);
RecordButton->Enabled = true;
StopButton->Enabled = false;
Shape2->Brush->Color = clBlack;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TrackBar1Change(TObject *Sender)
{
DWORD new_volume =
(DWORD)MAKEWPARAM(TrackBar1->Max - TrackBar1->Position,
TrackBar1->Max - TrackBar1->Position);
waveOutSetVolume((HWAVEOUT)WAVE_MAPPER, new_volume);
}
//---------------------------------------------------------------------------
does anyone know how to adapt this source code to my project or can show me another audio steraming project source code via usb?
thx...