Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Source code for USB audio streaming

Status
Not open for further replies.

r2d2droids

Member level 2
Joined
Jan 28, 2005
Messages
46
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Activity points
335
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, &current_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...
 

Re: usb audio streaming

I've used WaveLib to detect DTMF tones coming into the audio line in and then sending speech out the audio speaker port. I used C#/.NET but I'm fairly sure there's a C++ version.

What is the problem with the code you're using?
You have to be very diligent about setting up the audio mixer and its controls or you won't hear anything.
 

Re: usb audio streaming

in this sample, audio input is from the sound card via mic and the allocated memory is filled datas taken mic and sound card plays it like a wave file, etc.
but i wanna select the wave input as usb device. and the data can be taken with "FT_Read(ftHandle,RxBuffer,1,&BytesReceived)" structure. also the RxBuffer includes written datas. bu how to add this structure in this code?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top