I come from a .net background and fairly new to developing in C++.
I have a buffer like this:
short* pBuffer = &pGraph->m_pDataBuf[0];
How can I play this buffer from sound card?
Note: This buffer is filled in loop and in every cycle, I want to play the buffer.
My research shows that this command solves my problem, but by executing this command, no sound will be played...
::PlaySound((LPCSTR)pBuffer, NULL, SND_MEMORY | SND_ASYNC | SND_NODEFAULT);
Another solution is:
void PlaySoundBuffer()
{
static HWAVEOUT hwo;
CEMGDoc *pDoc = GetDocument();
CEMGGraph *pGraph = &pDoc->m_aGraphs[m_nCurGraph];
if (pGraph->m_nFlag & G_EMPTY)
return;
m_bInPlayingEMGMode = true;
int nHelperBufSize = (pGraph->m_nBufSize - pGraph->m_nEMGFirstVisibleIndex) * 2;
pGraph->m_pHelperDataBuf = new short[nHelperBufSize];
short *pHelperBuf = pGraph->m_pHelperDataBuf;
short *pBuffer = &pGraph->m_pDataBuf[0];
// This Coe. calibrate sounds that wave plays with the one that line-in plays
for (int i = 0; i < nHelperBufSize; i++, i++)
pHelperBuf[i] = pHelperBuf[i + 1] = short(pBuffer[i >> 1] * 1.75f);
WAVEFORMATEX wfx;
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = 2;
wfx.nSamplesPerSec = m_nSampleRate;
wfx.wBitsPerSample = 16;
wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
wfx.cbSize = 0;
WICatchError(waveOutOpen(&hwo, _Master_Audio_ID, &wfx, 0, 0, CALLBACK_NULL));
WAVEHDR hdr;
hdr.lpData = (char *)pHelperBuf;
hdr.dwBufferLength = nHelperBufSize * sizeof(short);
hdr.dwLoops = 1;
hdr.dwFlags = WHDR_BEGINLOOP | WHDR_ENDLOOP;
WICatchError(waveOutPrepareHeader(hwo, &hdr, sizeof(WAVEHDR)));
WICatchError(waveOutWrite(hwo, &hdr, sizeof(WAVEHDR)));
while (!(hdr.dwFlags & WHDR_DONE))
{
//wait while buffer played...
}
WICatchError(waveOutReset(hwo));
WICatchError(waveOutUnprepareHeader(hwo, &hdr, sizeof(WAVEHDR)));
WICatchError(waveOutClose(hwo));
delete[] pHelperBuf;
}
But no sound will be played yet...