2009-12-15 22:39:18 +00:00
|
|
|
/* PROJECT: ReactOS sndrec32
|
2009-12-03 19:00:41 +00:00
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
|
|
|
* FILE: base/applications/sndrec32/audio_wavein.cpp
|
2009-12-15 22:39:18 +00:00
|
|
|
* PURPOSE: Sound recording
|
|
|
|
* PROGRAMMERS: Marco Pagliaricci (irc: rendar)
|
2009-12-03 19:00:41 +00:00
|
|
|
*/
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
|
|
|
|
|
2009-11-30 18:54:41 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "audio_wavein.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_AUDIO_NAMESPACE_START_
|
|
|
|
|
|
|
|
|
2009-12-03 19:00:41 +00:00
|
|
|
void
|
|
|
|
audio_wavein::init_( void )
|
2009-11-30 18:54:41 +00:00
|
|
|
{
|
2009-12-15 22:39:18 +00:00
|
|
|
ZeroMemory(( LPVOID ) &wave_format,
|
|
|
|
sizeof( WAVEFORMATEX ));
|
|
|
|
|
|
|
|
wave_format.cbSize = sizeof( WAVEFORMATEX );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
wavein_handle = 0;
|
|
|
|
recthread_id = 0;
|
|
|
|
wakeup_recthread = 0;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
data_flushed_event = 0;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
buf_secs = _AUDIO_DEFAULT_WAVEINBUFSECS;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
status = WAVEIN_NOTREADY;
|
2009-11-30 18:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2009-12-03 19:00:41 +00:00
|
|
|
audio_wavein::alloc_buffers_mem_( unsigned int buffs, float secs )
|
2009-11-30 18:54:41 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
unsigned int
|
|
|
|
onebuf_size = 0, tot_size = 0;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Release old memory
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( main_buffer )
|
|
|
|
delete[] main_buffer;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( wave_headers )
|
|
|
|
delete[] wave_headers;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Calcs size of the buffers
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
onebuf_size = ( unsigned int )
|
|
|
|
(( float )aud_info.byte_rate() * secs );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
tot_size = onebuf_size * buffs;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Allocs memory for the audio buffers
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
main_buffer = new BYTE [ tot_size ];
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Allocs memory for the `WAVEHDR' structures.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
wave_headers = ( WAVEHDR * )
|
|
|
|
new BYTE [ sizeof( WAVEHDR ) * buffs ];
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Zeros memory.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
ZeroMemory( main_buffer, tot_size );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
ZeroMemory( wave_headers,
|
|
|
|
sizeof( WAVEHDR ) * buffs );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Updates total size of the buffers.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
mb_size = tot_size;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
void
|
2009-12-03 19:00:41 +00:00
|
|
|
audio_wavein::free_buffers_mem_( void )
|
2009-11-30 18:54:41 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Frees memory
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( main_buffer )
|
|
|
|
delete[] main_buffer;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( wave_headers )
|
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
void
|
2009-12-03 19:00:41 +00:00
|
|
|
audio_wavein::init_headers_( void )
|
2009-11-30 18:54:41 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// If there is no memory for memory or
|
|
|
|
// headers, simply return.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if (( !wave_headers ) || ( !main_buffer ))
|
|
|
|
return;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// This is the size for one buffer
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
DWORD buf_sz = mb_size / buffers;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// This is the base address for one buffer
|
|
|
|
//
|
|
|
|
|
|
|
|
BYTE * buf_addr = main_buffer;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Initializes headers.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
for ( unsigned int i = 0; i < buffers; ++i )
|
|
|
|
{
|
|
|
|
wave_headers[ i ].dwBufferLength = mb_size / buffers;
|
|
|
|
wave_headers[ i ].lpData = ( LPSTR ) buf_addr;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
buf_addr += buf_sz;
|
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
void
|
2009-12-03 19:00:41 +00:00
|
|
|
audio_wavein::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
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// If there is no memory for memory or
|
|
|
|
// headers, throw error.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if (( !wave_headers )
|
|
|
|
|| ( !main_buffer ) || ( !wavein_handle ))
|
|
|
|
{} //TODO: throw error!
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
for ( unsigned int i = 0; i < buffers; ++i )
|
|
|
|
{
|
|
|
|
err = waveInPrepareHeader( wavein_handle,
|
|
|
|
&wave_headers[ i ], sizeof( WAVEHDR ));
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( err != MMSYSERR_NOERROR )
|
|
|
|
error = true;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
}
|
|
|
|
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( error )
|
|
|
|
MessageBox( 0, TEXT("waveInPrepareHeader Error."), 0, 0 );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
void
|
2009-12-03 19:00:41 +00:00
|
|
|
audio_wavein::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
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// If there is no memory for memory or
|
|
|
|
// headers, throw error.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if (( !wave_headers )
|
|
|
|
|| ( !main_buffer ) || ( !wavein_handle ))
|
|
|
|
{} //TODO: throw error!
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
for ( unsigned int i = 0; i < buffers; ++i )
|
|
|
|
{
|
|
|
|
err = waveInUnprepareHeader( wavein_handle,
|
|
|
|
&wave_headers[ i ], sizeof( WAVEHDR ));
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( err != MMSYSERR_NOERROR )
|
|
|
|
error = true;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
}
|
|
|
|
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( error )
|
|
|
|
MessageBox( 0, TEXT("waveInUnPrepareHeader Error."), 0, 0 );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
void
|
2009-12-03 19:00:41 +00:00
|
|
|
audio_wavein::add_buffers_to_driver_( 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
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// If there is no memory for memory or
|
|
|
|
// headers, throw error.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if (( !wave_headers )
|
|
|
|
|| ( !main_buffer ) || ( !wavein_handle ))
|
|
|
|
{} //TODO: throw error!
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
for ( unsigned int i = 0; i < buffers; ++i )
|
|
|
|
{
|
|
|
|
err = waveInAddBuffer( wavein_handle,
|
|
|
|
&wave_headers[ i ], sizeof( WAVEHDR ));
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( err != MMSYSERR_NOERROR )
|
|
|
|
error = true;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
}
|
|
|
|
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( error )
|
|
|
|
MessageBox( 0, TEXT("waveInAddBuffer Error."), 0, 0 );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2009-12-15 22:39:18 +00:00
|
|
|
audio_wavein::close( void )
|
2009-11-30 18:54:41 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// If wavein object is already in the status
|
|
|
|
// NOTREADY, nothing to do.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( status == WAVEIN_NOTREADY )
|
|
|
|
return;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// If the wavein is recording,
|
|
|
|
// then stop recording and close it.
|
|
|
|
//
|
|
|
|
|
|
|
|
if ( status == WAVEIN_RECORDING )
|
|
|
|
stop_recording();
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Updating status.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
status = WAVEIN_NOTREADY;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Wakeing up recording thread, so it
|
|
|
|
// can receive the `MM_WIM_CLOSE' message
|
|
|
|
// then dies.
|
|
|
|
//
|
|
|
|
if ( wakeup_recthread )
|
|
|
|
SetEvent( wakeup_recthread );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Closing wavein stream
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
while (( waveInClose( wavein_handle ))
|
|
|
|
!= MMSYSERR_NOERROR ) Sleep( 1 );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Release buffers memory.
|
|
|
|
//
|
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
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Re-initialize variables to the
|
|
|
|
// initial state.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
init_();
|
|
|
|
|
2009-11-30 18:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2009-12-03 19:00:41 +00:00
|
|
|
audio_wavein::open( void )
|
2009-11-30 18:54:41 +00:00
|
|
|
{
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
MMRESULT err;
|
|
|
|
HANDLE recthread_handle = 0;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Checkin the status of the object
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( status != WAVEIN_NOTREADY )
|
|
|
|
{} //TODO: throw error
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Creating the EVENT object that will be signaled
|
|
|
|
// when the recording thread has to wake up.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
wakeup_recthread =
|
|
|
|
CreateEvent( 0, FALSE, FALSE, 0 );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
data_flushed_event =
|
|
|
|
CreateEvent( 0, FALSE, FALSE, 0 );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if (( !wakeup_recthread ) || ( !data_flushed_event ))
|
|
|
|
{
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
status = WAVEIN_ERR;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
MessageBox( 0, TEXT("Thread Error."), 0, 0 );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//TODO: throw error
|
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Inialize buffers for recording audio
|
|
|
|
// data from the wavein audio line.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
alloc_buffers_mem_( buffers, buf_secs );
|
|
|
|
init_headers_();
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Sound format that will be captured by wavein
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
wave_format.wFormatTag = WAVE_FORMAT_PCM;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Creating the recording thread
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
recthread_handle =
|
|
|
|
CreateThread( NULL,
|
|
|
|
0,
|
|
|
|
audio_wavein::recording_procedure,
|
|
|
|
( PVOID ) this,
|
|
|
|
0,
|
|
|
|
&recthread_id
|
|
|
|
);
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Checking thread handle
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( !recthread_handle )
|
|
|
|
{
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Updating status
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
status = WAVEIN_ERR;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
MessageBox( 0, TEXT("Thread Error."), 0, 0 );
|
|
|
|
//TODO: throw error
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +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)
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
CloseHandle( recthread_handle );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Opening audio line wavein
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
err = waveInOpen( &wavein_handle,
|
|
|
|
0,
|
|
|
|
&wave_format,
|
|
|
|
recthread_id,
|
|
|
|
0,
|
|
|
|
CALLBACK_THREAD
|
|
|
|
);
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( err != MMSYSERR_NOERROR )
|
|
|
|
{
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Updating status
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
status = WAVEIN_ERR;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( err == WAVERR_BADFORMAT )
|
|
|
|
MessageBox( 0, TEXT("waveInOpen Error"), 0, 0 );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//TODO: throw error
|
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Update object status
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
status = WAVEIN_READY;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Now `audio_wavein' object is ready
|
|
|
|
// for audio recording!
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2009-12-03 19:00:41 +00:00
|
|
|
audio_wavein::start_recording( void )
|
2009-11-30 18:54:41 +00:00
|
|
|
{
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
MMRESULT err;
|
|
|
|
BOOL ev;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if (( status != WAVEIN_READY )
|
|
|
|
&& ( status != WAVEIN_STOP ))
|
|
|
|
{} //TODO: throw error
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Updating to the recording status
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
status = WAVEIN_RECORDING;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Let's prepare header of type WAVEHDR that
|
|
|
|
// we will pass to the driver with our
|
|
|
|
// audio informations, and buffer informations.
|
|
|
|
//
|
|
|
|
|
|
|
|
prep_headers_();
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// The waveInAddBuffer function sends an input buffer
|
|
|
|
// to the given waveform-audio input device.
|
|
|
|
// When the buffer is filled, the application is notified.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
add_buffers_to_driver_();
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Signaling event for waking up
|
|
|
|
// the recorder thread.
|
|
|
|
//
|
|
|
|
|
|
|
|
ev = SetEvent( wakeup_recthread );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( !ev )
|
|
|
|
{
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
MessageBox( 0, TEXT("Event Error."), 0, 0 );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
}
|
|
|
|
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Start recording
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
|
|
|
|
err = waveInStart( wavein_handle );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( err != MMSYSERR_NOERROR )
|
|
|
|
{
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Updating status
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
status = WAVEIN_ERR;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
MessageBox( 0, TEXT("waveInStart Error."), 0, 0 );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//TODO: throw error
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2009-12-03 19:00:41 +00:00
|
|
|
audio_wavein::stop_recording( void )
|
2009-11-30 18:54:41 +00:00
|
|
|
{
|
2009-12-15 22:39:18 +00:00
|
|
|
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
MMRESULT err;
|
|
|
|
DWORD wait;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( status != WAVEIN_RECORDING )
|
|
|
|
return;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
|
|
|
|
status = WAVEIN_FLUSHING;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// waveInReset will make all pending buffer as done.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
err = waveInReset( wavein_handle );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( err != MMSYSERR_NOERROR )
|
|
|
|
{
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//TODO: throw error
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
MessageBox( 0, TEXT("waveInReset Error."), 0, 0 );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( data_flushed_event )
|
|
|
|
wait = WaitForSingleObject(
|
|
|
|
data_flushed_event, INFINITE
|
|
|
|
);
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Stop recording.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
err = waveInStop( wavein_handle );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( err != MMSYSERR_NOERROR )
|
|
|
|
{
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//TODO: throw error
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
MessageBox( 0, TEXT("waveInStop Error."), 0, 0 );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// The waveInUnprepareHeader function cleans up the
|
|
|
|
// preparation performed by the waveInPrepareHeader function.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
unprep_headers_();
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = WAVEIN_STOP;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
DWORD WINAPI
|
2009-12-03 19:00:41 +00:00
|
|
|
audio_wavein::recording_procedure( LPVOID arg )
|
2009-11-30 18:54:41 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
MSG msg;
|
|
|
|
WAVEHDR * phdr;
|
|
|
|
DWORD wait;
|
|
|
|
audio_wavein * _this = ( audio_wavein * ) arg;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Check the arg pointer
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( _this == 0 )
|
|
|
|
return 0;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// The thread can go to sleep for now.
|
|
|
|
// It will be wake up only when there is audio data
|
|
|
|
// to be recorded.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( _this->wakeup_recthread )
|
|
|
|
wait = WaitForSingleObject(
|
|
|
|
_this->wakeup_recthread, INFINITE
|
|
|
|
);
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// If status of the `audio_wavein' object
|
|
|
|
// is not ready or recording the thread can exit.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if (( _this->status != WAVEIN_READY ) &&
|
|
|
|
( _this->status != WAVEIN_RECORDING ))
|
|
|
|
return 0;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Entering main polling loop
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
while ( GetMessage( &msg, 0, 0, 0 ))
|
|
|
|
{
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
switch ( msg.message )
|
|
|
|
{
|
|
|
|
|
|
|
|
case MM_WIM_DATA:
|
|
|
|
|
|
|
|
phdr = ( WAVEHDR * ) msg.lParam;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if (( _this->status == WAVEIN_RECORDING )
|
|
|
|
|| ( _this->status == WAVEIN_FLUSHING ))
|
|
|
|
{
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( phdr->dwFlags & WHDR_DONE )
|
|
|
|
{
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Flushes recorded audio data to
|
|
|
|
// the `audio_receiver' object.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
_this->audio_rcvd.audio_receive(
|
|
|
|
( unsigned char * )phdr->lpData,
|
|
|
|
phdr->dwBytesRecorded
|
|
|
|
);
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Updating `audio_receiver' total
|
|
|
|
// bytes received _AFTER_ calling
|
|
|
|
// `audio_receive' function.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
_this->audio_rcvd.bytes_received +=
|
|
|
|
phdr->dwBytesRecorded;
|
|
|
|
}
|
|
|
|
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// If status is not flushing data, then
|
|
|
|
// we can re-add the buffer for reusing it.
|
|
|
|
// Otherwise, if we are flushing pending data,
|
|
|
|
// we cannot re-add buffer because we don't need
|
|
|
|
// it anymore
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
if ( _this->status != WAVEIN_FLUSHING )
|
|
|
|
{
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// Let the audio driver reuse the buffer
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
waveInAddBuffer( _this->wavein_handle,
|
|
|
|
phdr, sizeof( WAVEHDR ));
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
} else {
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// If we are flushing pending data, we have
|
|
|
|
// to prepare to stop recording.
|
|
|
|
// Set WAVEHDR flag to 0, and fires the event
|
|
|
|
// `data_flushed_event', that will wake up
|
|
|
|
// the main thread that is sleeping into
|
|
|
|
// wavein_in::stop_recording() member function,
|
|
|
|
// waiting the last `MM_WIM_DATA' message that
|
|
|
|
// contain pending data.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
phdr->dwFlags = 0;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
SetEvent( _this->data_flushed_event );
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// The recording is gooing to stop, so the
|
|
|
|
// recording thread can go to sleep!
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
wait = WaitForSingleObject(
|
|
|
|
_this->wakeup_recthread, INFINITE );
|
|
|
|
|
|
|
|
}
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
}//if WAVEIN_RECORDING || WAVEIN_FLUSHING
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
break;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
case MM_WIM_CLOSE:
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
//
|
|
|
|
// The thread can exit now.
|
|
|
|
//
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
return 0;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
break;
|
2009-11-30 18:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
} //end switch( msg.message )
|
|
|
|
|
|
|
|
} //end while( GetMessage( ... ))
|
2009-11-30 18:54:41 +00:00
|
|
|
|
2009-12-15 22:39:18 +00:00
|
|
|
return 0;
|
2009-11-30 18:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_AUDIO_NAMESPACE_END_
|