1

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...

Community
  • 1
  • 1
  • Seems you are running windows isn't it? – OznOg Nov 30 '19 at 08:39
  • Yes... C++ in VC++ 6.0 – Rashid Bagheri Nov 30 '19 at 08:41
  • is there any solution? – Rashid Bagheri Nov 30 '19 at 09:05
  • If you are willing to use something else than WinApi I will recommend you to look that part of Gstreamer tutorial: https://gstreamer.freedesktop.org/documentation/tutorials/basic/short-cutting-the-pipeline.html?gi-language=c Especially `push_data` function which is responsible for producing audio samples. In your case it will be even simpler as you are reading them from the buffer. – bialy Nov 30 '19 at 10:29

0 Answers0