2009-12-15 22:39:18 +00:00
|
|
|
/* PROJECT: ReactOS sndrec32
|
2009-11-30 18:54:41 +00:00
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
|
|
|
* FILE: base/applications/sndrec32/audio_waveout.cpp
|
2009-12-15 22:39:18 +00:00
|
|
|
* PURPOSE: Sound recording
|
|
|
|
* PROGRAMMERS: Marco Pagliaricci (irc: rendar)
|
2009-11-30 18:54:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "audio_waveout.hpp"
|
|
|
|
|
|
|
|
_AUDIO_NAMESPACE_START_
|
|
|
|
|
|
|
|
void
|
2013-12-21 13:29:23 +00:00
|
|
|
audio_waveout::init_(void)
|
2009-11-30 18:54:41 +00:00
|
|
|
{
|
2013-12-21 13:29:23 +00:00
|
|
|
ZeroMemory((LPVOID)&wave_format, sizeof(WAVEFORMATEX));
|
|
|
|
wave_format.cbSize = sizeof(WAVEFORMATEX);
|
2009-12-15 22:39:18 +00:00
|
|
|
waveout_handle = 0;
|
|
|
|
playthread_id = 0;
|
|
|
|
wakeup_playthread = 0;
|
|
|
|
buf_secs = _AUDIO_DEFAULT_WAVEOUTBUFSECS;
|
|
|
|
status = WAVEOUT_NOTREADY;
|
2009-11-30 18:54:41 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 21:32:16 +00:00
|
|
|
void
|
2013-12-21 13:29:23 +00:00
|
|
|
audio_waveout::alloc_buffers_mem_(unsigned int buffs, float secs)
|
2009-11-30 18:54:41 +00:00
|
|
|
{
|
2013-12-21 13:29:23 +00:00
|
|
|
unsigned int onebuf_size = 0, tot_size = 0;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Release old memory */
|
|
|
|
if (main_buffer)
|
2009-12-15 22:39:18 +00:00
|
|
|
delete[] main_buffer;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
if (wave_headers)
|
2009-12-15 22:39:18 +00:00
|
|
|
delete[] wave_headers;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Calcs size of the buffers */
|
2022-01-17 21:32:16 +00:00
|
|
|
onebuf_size = (unsigned int)((float)aud_info.byte_rate() * secs);
|
2009-12-15 22:39:18 +00:00
|
|
|
tot_size = onebuf_size * buffs;
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Allocs memory for the audio buffers */
|
|
|
|
main_buffer = new BYTE[tot_size];
|
|
|
|
/* Allocs memory for the `WAVEHDR' structures */
|
|
|
|
wave_headers = (WAVEHDR *) new BYTE[sizeof(WAVEHDR) * buffs];
|
|
|
|
/* Zeros memory */
|
|
|
|
ZeroMemory(main_buffer, tot_size);
|
|
|
|
ZeroMemory(wave_headers, sizeof(WAVEHDR) * buffs);
|
|
|
|
/* Updates total size of the buffers */
|
2009-12-15 22:39:18 +00:00
|
|
|
mb_size = tot_size;
|
2009-11-30 18:54:41 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 21:32:16 +00:00
|
|
|
void
|
2013-12-21 13:29:23 +00:00
|
|
|
audio_waveout::init_headers_(void)
|
2009-11-30 18:54:41 +00:00
|
|
|
{
|
2013-12-21 13:29:23 +00:00
|
|
|
/* If there is no memory for memory or headers, simply return */
|
|
|
|
if ((!wave_headers) || (!main_buffer))
|
2009-12-15 22:39:18 +00:00
|
|
|
return;
|
2009-12-03 19:00:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* This is the size for one buffer */
|
2009-12-15 22:39:18 +00:00
|
|
|
DWORD buf_sz = mb_size / buffers;
|
2013-12-21 13:29:23 +00:00
|
|
|
/* This is the base address for one buffer */
|
|
|
|
BYTE *buf_addr = main_buffer;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
ZeroMemory(wave_headers, sizeof(WAVEHDR) * buffers);
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Initializes headers */
|
|
|
|
for (unsigned int i = 0; i < buffers; ++i)
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Sets the correct base address and length for the little buffer */
|
|
|
|
wave_headers[i].dwBufferLength = mb_size / buffers;
|
|
|
|
wave_headers[i].lpData = (LPSTR) buf_addr;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Unsets the WHDR_DONE flag */
|
|
|
|
wave_headers[i].dwFlags &= ~WHDR_DONE;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Sets the WAVEHDR user data with an unique little buffer ID# */
|
|
|
|
wave_headers[i].dwUser = (unsigned int)i;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Increments little buffer base address */
|
2009-12-15 22:39:18 +00:00
|
|
|
buf_addr += buf_sz;
|
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 21:32:16 +00:00
|
|
|
void
|
2013-12-21 13:29:23 +00:00
|
|
|
audio_waveout::prep_headers_(void)
|
2009-11-30 18:54:41 +00:00
|
|
|
{
|
2009-12-15 22:39:18 +00:00
|
|
|
MMRESULT err;
|
|
|
|
bool error = false;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* If there is no memory for memory or headers, throw error */
|
|
|
|
if ((!wave_headers) || (!main_buffer) || (!waveout_handle))
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
2013-12-21 13:29:23 +00:00
|
|
|
/* TODO: throw error! */
|
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
for (unsigned int i = 0; i < buffers; ++i)
|
|
|
|
{
|
|
|
|
err = waveOutPrepareHeader(waveout_handle, &wave_headers[i], sizeof(WAVEHDR));
|
|
|
|
if (err != MMSYSERR_NOERROR)
|
2009-12-15 22:39:18 +00:00
|
|
|
error = true;
|
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
/* TODO: throw error indicating which header i-th is errorneous */
|
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 21:32:16 +00:00
|
|
|
void
|
2013-12-21 13:29:23 +00:00
|
|
|
audio_waveout::unprep_headers_(void)
|
2009-11-30 18:54:41 +00:00
|
|
|
{
|
2009-12-15 22:39:18 +00:00
|
|
|
MMRESULT err;
|
|
|
|
bool error = false;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* If there is no memory for memory or headers, throw error */
|
|
|
|
if ((!wave_headers) || (!main_buffer) || (!waveout_handle))
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
2013-12-21 13:29:23 +00:00
|
|
|
/* TODO: throw error! */
|
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
for (unsigned int i = 0; i < buffers; ++i)
|
|
|
|
{
|
|
|
|
err = waveOutUnprepareHeader(waveout_handle, &wave_headers[i], sizeof(WAVEHDR));
|
|
|
|
if (err != MMSYSERR_NOERROR)
|
2009-12-15 22:39:18 +00:00
|
|
|
error = true;
|
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
/* TODO: throw error indicating which header i-th is errorneous */
|
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 21:32:16 +00:00
|
|
|
void
|
2013-12-21 13:29:23 +00:00
|
|
|
audio_waveout::free_buffers_mem_(void)
|
2009-11-30 18:54:41 +00:00
|
|
|
{
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Frees memory */
|
|
|
|
if (main_buffer)
|
2009-12-15 22:39:18 +00:00
|
|
|
delete[] main_buffer;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
if (wave_headers)
|
2009-12-15 22:39:18 +00:00
|
|
|
delete[] wave_headers;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
main_buffer = 0;
|
|
|
|
wave_headers = 0;
|
2009-11-30 18:54:41 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 21:32:16 +00:00
|
|
|
void
|
2013-12-21 13:29:23 +00:00
|
|
|
audio_waveout::open(void)
|
2009-11-30 18:54:41 +00:00
|
|
|
{
|
2009-12-15 22:39:18 +00:00
|
|
|
MMRESULT err;
|
|
|
|
HANDLE playthread_handle = 0;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Checkin the status of the object */
|
|
|
|
if (status != WAVEOUT_NOTREADY)
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
2013-12-21 13:29:23 +00:00
|
|
|
/* TODO: throw error */
|
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Creating the EVENT object that will be signaled when
|
|
|
|
the playing thread has to wake up */
|
|
|
|
wakeup_playthread = CreateEvent(0, FALSE, FALSE, 0);
|
|
|
|
if (!wakeup_playthread)
|
|
|
|
{
|
2009-12-15 22:39:18 +00:00
|
|
|
status = WAVEOUT_ERR;
|
2013-12-21 13:29:23 +00:00
|
|
|
/* TODO: throw error */
|
2009-12-15 22:39:18 +00:00
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Inialize buffers for recording audio data from the wavein audio line */
|
|
|
|
alloc_buffers_mem_(buffers, buf_secs);
|
2009-12-15 22:39:18 +00:00
|
|
|
init_headers_();
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Sound format that will be captured by wavein */
|
2009-12-15 22:39:18 +00:00
|
|
|
wave_format.wFormatTag = WAVE_FORMAT_PCM;
|
|
|
|
wave_format.nChannels = aud_info.channels();
|
|
|
|
wave_format.nSamplesPerSec = aud_info.sample_rate();
|
|
|
|
wave_format.wBitsPerSample = aud_info.bits();
|
|
|
|
wave_format.nBlockAlign = aud_info.block_align();
|
|
|
|
wave_format.nAvgBytesPerSec = aud_info.byte_rate();
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Creating the recording thread */
|
|
|
|
playthread_handle = CreateThread(NULL,
|
|
|
|
0,
|
|
|
|
audio_waveout::playing_procedure,
|
|
|
|
(PVOID)this,
|
|
|
|
0,
|
|
|
|
&playthread_id);
|
|
|
|
/* Checking thread handle */
|
|
|
|
if (!playthread_handle)
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Updating status */
|
|
|
|
status = WAVEOUT_ERR;
|
|
|
|
/* TODO: throw error */
|
2009-12-15 22:39:18 +00:00
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* We don't need the thread handle anymore, so we can close it from now.
|
|
|
|
(We'll just need the thread ID for the `waveInOpen' API) */
|
|
|
|
CloseHandle(playthread_handle);
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Reset the `audio_source' to the start position */
|
2009-12-15 22:39:18 +00:00
|
|
|
audio_buf.set_position_start();
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Opens the WAVE_OUT device */
|
|
|
|
err = waveOutOpen(&waveout_handle,
|
|
|
|
WAVE_MAPPER,
|
|
|
|
&wave_format,
|
|
|
|
playthread_id,
|
|
|
|
0,
|
|
|
|
CALLBACK_THREAD | WAVE_ALLOWSYNC);
|
|
|
|
if (err != MMSYSERR_NOERROR)
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
|
|
|
MessageBox(0, _T("waveOutOpen Error"), 0, 0);
|
2013-12-21 13:29:23 +00:00
|
|
|
/* TODO: throw error */
|
2009-12-15 22:39:18 +00:00
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
status = WAVEOUT_READY;
|
2009-11-30 18:54:41 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 21:32:16 +00:00
|
|
|
void
|
2013-12-21 13:29:23 +00:00
|
|
|
audio_waveout::play(void)
|
2009-11-30 18:54:41 +00:00
|
|
|
{
|
2009-12-15 22:39:18 +00:00
|
|
|
MMRESULT err;
|
|
|
|
unsigned int i;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
if (!main_buffer)
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
2013-12-21 13:29:23 +00:00
|
|
|
/* TODO; throw error, or assert */
|
|
|
|
return;
|
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* If the status is PAUSED, we have to resume the audio playing */
|
|
|
|
if (status == WAVEOUT_PAUSED)
|
|
|
|
{
|
|
|
|
/* Updates status */
|
2009-12-15 22:39:18 +00:00
|
|
|
status = WAVEOUT_PLAYING;
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Tells to the driver to resume audio playing */
|
|
|
|
waveOutRestart(waveout_handle);
|
|
|
|
/* Wakeup playing thread */
|
|
|
|
SetEvent(wakeup_playthread);
|
2009-12-15 22:39:18 +00:00
|
|
|
return;
|
2013-12-21 13:29:23 +00:00
|
|
|
} /* if status == WAVEOUT_PAUSED */
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
if (status != WAVEOUT_READY)
|
2009-12-15 22:39:18 +00:00
|
|
|
return;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Prepares WAVEHDR structures */
|
2009-12-15 22:39:18 +00:00
|
|
|
prep_headers_();
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Sets correct status */
|
2009-12-15 22:39:18 +00:00
|
|
|
status = WAVEOUT_PLAYING;
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Reads the audio from the start */
|
2009-12-15 22:39:18 +00:00
|
|
|
//audio_buf.set_position_start();
|
2009-12-03 19:00:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Reads the first N bytes from the audio buffer, where N = the total
|
|
|
|
size of all little buffers */
|
|
|
|
audio_buf.read(main_buffer, mb_size);
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Wakeup the playing thread */
|
|
|
|
SetEvent(wakeup_playthread);
|
2009-12-03 19:00:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Sends all the little buffers to the audio driver, so it can play
|
|
|
|
the sound data */
|
|
|
|
for (i = 0; i < buffers; ++i)
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
2013-12-21 13:29:23 +00:00
|
|
|
err = waveOutWrite(waveout_handle, &wave_headers[i], sizeof(WAVEHDR));
|
|
|
|
if (err != MMSYSERR_NOERROR)
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
|
|
|
MessageBox(0, _T("waveOutWrite Error"), 0, 0);
|
2013-12-21 13:29:23 +00:00
|
|
|
/* TODO: throw error */
|
2009-12-15 22:39:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2022-01-17 21:32:16 +00:00
|
|
|
void
|
2013-12-21 13:29:23 +00:00
|
|
|
audio_waveout::pause(void)
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
|
|
|
MMRESULT err;
|
2009-12-03 19:00:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* If the waveout object is not playing audio, do nothing */
|
|
|
|
if (status == WAVEOUT_PLAYING)
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Updating status */
|
2009-12-15 22:39:18 +00:00
|
|
|
status = WAVEOUT_PAUSED;
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Tells to audio driver to pause audio */
|
|
|
|
err = waveOutPause(waveout_handle);
|
|
|
|
if (err != MMSYSERR_NOERROR)
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
|
|
|
MessageBox(0, _T("waveOutPause Error"), 0, 0);
|
2013-12-21 13:29:23 +00:00
|
|
|
/* TODO: throw error */
|
2009-12-15 22:39:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2022-01-17 21:32:16 +00:00
|
|
|
void
|
2013-12-21 13:29:23 +00:00
|
|
|
audio_waveout::stop(void)
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
|
|
|
MMRESULT err;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Checks the current status */
|
|
|
|
if ((status != WAVEOUT_PLAYING) &&
|
|
|
|
(status != WAVEOUT_FLUSHING) &&
|
|
|
|
(status != WAVEOUT_PAUSED))
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Do nothing */
|
2009-12-15 22:39:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Sets a new status */
|
2009-12-15 22:39:18 +00:00
|
|
|
status = WAVEOUT_STOP;
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Flushes pending audio datas */
|
2009-12-15 22:39:18 +00:00
|
|
|
err = waveOutReset( waveout_handle );
|
2013-12-21 13:29:23 +00:00
|
|
|
if (err != MMSYSERR_NOERROR)
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
|
|
|
MessageBox(0, _T("err WaveOutReset.\n"),_T("ERROR"), 0);
|
2013-12-21 13:29:23 +00:00
|
|
|
/* TODO: throw error */
|
2009-12-15 22:39:18 +00:00
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Sets the start position of the audio buffer */
|
2009-12-15 22:39:18 +00:00
|
|
|
audio_buf.set_position_start();
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Cleans little buffers */
|
2009-12-15 22:39:18 +00:00
|
|
|
unprep_headers_();
|
|
|
|
init_headers_();
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Refreshes the status */
|
2009-12-15 22:39:18 +00:00
|
|
|
status = WAVEOUT_READY;
|
2009-11-30 18:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-12-21 13:29:23 +00:00
|
|
|
audio_waveout::close(void)
|
2009-11-30 18:54:41 +00:00
|
|
|
{
|
2009-12-15 22:39:18 +00:00
|
|
|
MMRESULT err;
|
2009-12-03 19:00:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* If the `wave_out' object is playing audio, or it is in paused state,
|
|
|
|
we have to call the `stop' member function, to flush pending buffers */
|
|
|
|
if ((status == WAVEOUT_PLAYING) || (status== WAVEOUT_PAUSED))
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
|
|
|
stop();
|
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* When we have flushed all pending buffers, the wave out handle can be successfully closed */
|
|
|
|
err = waveOutClose(waveout_handle);
|
|
|
|
if (err != MMSYSERR_NOERROR)
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
|
|
|
MessageBox(0, _T("waveOutClose Error"), 0, 0);
|
2013-12-21 13:29:23 +00:00
|
|
|
/* TODO: throw error */
|
2009-12-15 22:39:18 +00:00
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
free_buffers_mem_();
|
2009-11-30 18:54:41 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 21:32:16 +00:00
|
|
|
DWORD WINAPI
|
2013-12-21 13:29:23 +00:00
|
|
|
audio_waveout::playing_procedure(LPVOID arg)
|
2009-11-30 18:54:41 +00:00
|
|
|
{
|
2009-12-15 22:39:18 +00:00
|
|
|
MSG msg;
|
2013-12-21 13:29:23 +00:00
|
|
|
WAVEHDR *phdr;
|
2009-12-15 22:39:18 +00:00
|
|
|
MMRESULT err;
|
2013-12-21 13:29:23 +00:00
|
|
|
audio_waveout *_this = (audio_waveout *)arg;
|
2009-12-15 22:39:18 +00:00
|
|
|
unsigned int read_size;
|
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Check the arg pointer */
|
|
|
|
if (_this == 0)
|
2009-12-15 22:39:18 +00:00
|
|
|
return 0;
|
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* The thread can go to sleep for now. It will be wake up only when
|
|
|
|
there is audio data to be recorded */
|
|
|
|
if (_this->wakeup_playthread)
|
2011-09-10 21:31:09 +00:00
|
|
|
WaitForSingleObject(_this->wakeup_playthread, INFINITE);
|
2009-12-15 22:39:18 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Entering main polling loop */
|
|
|
|
while (GetMessage(&msg, 0, 0, 0))
|
|
|
|
{
|
|
|
|
switch (msg.message)
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
|
|
|
case MM_WOM_DONE:
|
2013-12-21 13:29:23 +00:00
|
|
|
phdr = (WAVEHDR *)msg.lParam;
|
2009-12-03 19:00:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* If the status of the `wave_out' object is different
|
|
|
|
than playing, then the thread can go to sleep */
|
|
|
|
if ((_this->status != WAVEOUT_PLAYING) &&
|
|
|
|
(_this->status != WAVEOUT_FLUSHING) &&
|
|
|
|
(_this->wakeup_playthread))
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
2011-09-10 21:31:09 +00:00
|
|
|
WaitForSingleObject(_this->wakeup_playthread, INFINITE);
|
2009-12-15 22:39:18 +00:00
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* The playing thread doesn't have to sleep, so let's checking
|
|
|
|
first if the little buffer has been sent to the audio driver
|
|
|
|
(it has the WHDR_DONE flag). If it is, we can read new audio
|
|
|
|
datas from the `audio_producer' object, refill the little buffer,
|
|
|
|
and resend it to the driver with waveOutWrite( ) API */
|
|
|
|
if (phdr->dwFlags & WHDR_DONE)
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
2013-12-21 13:29:23 +00:00
|
|
|
if (_this->status == WAVEOUT_PLAYING)
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Here the thread is still playing a sound, so it can
|
|
|
|
read new audio data from the `audio_producer' object */
|
|
|
|
read_size = _this->audio_buf.read((BYTE *)phdr->lpData,
|
|
|
|
phdr->dwBufferLength);
|
2009-12-15 22:39:18 +00:00
|
|
|
} else
|
2013-12-21 13:29:23 +00:00
|
|
|
read_size = 0;
|
2009-12-15 22:39:18 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* If the `audio_producer' object, has produced some
|
|
|
|
audio data, so `read_size' will be > 0 */
|
|
|
|
if (read_size)
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Adjusts the correct effectively read size */
|
2009-12-15 22:39:18 +00:00
|
|
|
phdr->dwBufferLength = read_size;
|
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Before sending the little buffer to the driver,
|
|
|
|
we have to remove the `WHDR_DONE' flag, because
|
|
|
|
the little buffer now contain new audio data that have to be played */
|
2009-12-15 22:39:18 +00:00
|
|
|
phdr->dwFlags &= ~WHDR_DONE;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Plays the sound of the little buffer */
|
|
|
|
err = waveOutWrite(_this->waveout_handle,
|
|
|
|
phdr,
|
|
|
|
sizeof(WAVEHDR));
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Checking if any error has occured */
|
|
|
|
if (err != MMSYSERR_NOERROR)
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
2013-12-21 13:29:23 +00:00
|
|
|
MessageBox(0, _T("waveOutWrite Error"), 0, 0);
|
|
|
|
/* TODO: throw error */
|
2009-12-15 22:39:18 +00:00
|
|
|
}
|
2013-12-21 13:29:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Here `read_size' is 0, so the `audio_producer' object,
|
|
|
|
doesn't have any sound data to produce anymore. So,
|
|
|
|
now we have to see the little buffer #ID to establishing what to do */
|
|
|
|
if (phdr->dwUser == 0)
|
2009-12-15 22:39:18 +00:00
|
|
|
{
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Here `read_size' is 0, and the buffer user data
|
|
|
|
contain 0, so this is the first of N little
|
|
|
|
buffers that came back with `WHDR_DONE' flag;
|
|
|
|
this means that this is the last little buffer
|
|
|
|
in which we have to read data to; so we can
|
|
|
|
_STOP_ reading data from the `audio_producer'
|
|
|
|
object: doing this is accomplished just setting
|
|
|
|
the current status as "WAVEOUT_FLUSHING" */
|
2009-12-15 22:39:18 +00:00
|
|
|
_this->status = WAVEOUT_FLUSHING;
|
2013-12-21 13:29:23 +00:00
|
|
|
}
|
|
|
|
else if (phdr->dwUser == (_this->buffers - 1))
|
|
|
|
{
|
|
|
|
/* Here `read_size' and the buffer user data, that
|
|
|
|
contain a buffer ID#, is equal to the number of
|
|
|
|
the total buffers - 1. This means that this is
|
|
|
|
the _LAST_ little buffer that has been played by
|
|
|
|
the audio driver. We can STOP the `wave_out'
|
|
|
|
object now, or restart the sound playing, if we have a infinite loop */
|
2009-12-15 22:39:18 +00:00
|
|
|
_this->stop();
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Let the thread go to sleep */
|
|
|
|
if (_this->audio_buf.play_finished)
|
2009-12-15 22:39:18 +00:00
|
|
|
_this->audio_buf.play_finished();
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
if (_this->wakeup_playthread)
|
2011-09-10 21:31:09 +00:00
|
|
|
WaitForSingleObject(_this->wakeup_playthread,
|
|
|
|
INFINITE);
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
} /* if (phdr->dwUser == (_this->buffers - 1)) */
|
|
|
|
} /* if read_size != 0 */
|
|
|
|
} /* (phdr->dwFlags & WHDR_DONE) */
|
|
|
|
break; /* end case */
|
2009-12-15 22:39:18 +00:00
|
|
|
case MM_WOM_CLOSE:
|
2013-12-21 13:29:23 +00:00
|
|
|
/* The thread can exit now */
|
2009-12-15 22:39:18 +00:00
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case MM_WOM_OPEN:
|
2013-12-21 13:29:23 +00:00
|
|
|
/* Do nothing */
|
2009-12-15 22:39:18 +00:00
|
|
|
break;
|
2013-12-21 13:29:23 +00:00
|
|
|
} /* end switch(msg.message) */
|
|
|
|
} /* end while(GetMessage(...)) */
|
2009-12-03 19:00:41 +00:00
|
|
|
|
2013-12-21 13:29:23 +00:00
|
|
|
return 0;
|
2009-11-30 18:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_AUDIO_NAMESPACE_END_
|