[SNDREC32]

* Attempt to fix a.. crime against style.

svn path=/trunk/; revision=61314
This commit is contained in:
Amine Khaldi 2013-12-21 13:29:23 +00:00
parent b4e3ee3563
commit 5170eaeaaf
20 changed files with 1846 additions and 4418 deletions

View file

@ -1,12 +1,7 @@
#ifndef __AUDIO_API__
#define __AUDIO_API__
//#include "audio_def.hpp"
//#include "audio_format.hpp"
#include "audio_membuffer.hpp"
//#include "audio_producer.hpp"
//#include "audio_receiver.hpp"
//#include "audio_resampler_acm.hpp"
#include "audio_wavein.hpp"
#include "audio_waveout.hpp"

View file

@ -5,21 +5,10 @@
* PROGRAMMERS: Marco Pagliaricci (irc: rendar)
*/
#ifndef _AUDIO_DEF__H_
#define _AUDIO_DEF__H_
//#include <iostream>
//
// Defaults
//
/* Defaults */
#define _AUDIO_DEFAULT_FORMAT A44100_16BIT_STEREO
@ -31,40 +20,16 @@
#define _AUDIO_DEFAULT_BUFSECS 1.0f
//
// Namespace stuff
//
/* Namespace stuff */
#define _AUDIO_NAMESPACE_START_ namespace snd {
#define _AUDIO_NAMESPACE_END_ };
//
// Platform depend stuff
//
//#include <windows.h>
/* Platform depend stuff */
#include <mmsystem.h> // Windows MultiMedia (WINMM) audio apis
#include <mmreg.h> // codecs stuff
#include <msacm.h> // codecs stuff
//#pragma comment(lib, "winmm.lib")
//#pragma comment(lib, "msacm32.lib")
#endif //ifdef _AUDIO_DEF__H_

View file

@ -5,31 +5,15 @@
* PROGRAMMERS: Marco Pagliaricci (irc: rendar)
*/
#include "stdafx.h"
#include "audio_format.hpp"
_AUDIO_NAMESPACE_START_
//
// Standard audio formats (declared as
// externs in `audio_format.hpp')
//
/* Standard audio formats (declared as externs in `audio_format.hpp') */
audio_format UNNKOWN_FORMAT(0, 0, 0);
audio_format A44100_16BIT_STEREO(44100, 16, 2);
audio_format A44100_16BIT_MONO(44100, 16, 1);
_AUDIO_NAMESPACE_END_

View file

@ -5,159 +5,91 @@
* PROGRAMMERS: Marco Pagliaricci (irc: rendar)
*/
#ifndef _AUDIOFORMAT__H_
#define _AUDIOFORMAT__H_
#include "audio_def.hpp"
_AUDIO_NAMESPACE_START_
class audio_format
{
protected:
unsigned int samples_psec;
unsigned short int bits_psample;
unsigned short int chan;
public:
//
// Ctors
//
/* Ctors */
audio_format(unsigned int samples_per_second,
unsigned short int bits_per_sample, unsigned short int channels )
: samples_psec( samples_per_second ), bits_psample( bits_per_sample ),
unsigned short int bits_per_sample,
unsigned short int channels) : samples_psec(samples_per_second),
bits_psample(bits_per_sample),
chan(channels)
{ }
//
// Dtor
//
virtual ~audio_format( void )
{ }
//
// Operators
//
bool operator==( audio_format & eq ) const
{
//
// The same audio format is when samples per second,
// bit per sample, and channels mono/stereo are equal.
//
return (( samples_psec == eq.samples_psec )
&& ( bits_psample == eq.bits_psample ) && ( chan == eq.chan ));
}
/* Dtor */
virtual ~audio_format(void)
{
}
/* Operators */
bool operator==(audio_format & eq) const
{
/* The same audio format is when samples per second,
bit per sample, and channels mono/stereo are equal */
return ((samples_psec == eq.samples_psec) &&
(bits_psample == eq.bits_psample) &&
(chan == eq.chan));
}
//
// Public Functions
//
/* Public Functions */
unsigned int sample_rate(void) const
{ return samples_psec; }
{
return samples_psec;
}
unsigned short int bits(void) const
{ return bits_psample; }
{
return bits_psample;
}
unsigned short int channels(void) const
{ return chan; }
{
return chan;
}
unsigned int byte_rate(void) const
{ return ( samples_psec * chan * ( bits_psample / 8 )); }
{
return (samples_psec * chan * (bits_psample / 8));
}
unsigned int block_align(void) const
{ return ( chan * ( bits_psample / 8 )); }
{
return (chan * (bits_psample / 8));
}
unsigned int samples_in_seconds(float seconds) const
{
return ( unsigned int )
((( float )samples_psec * ( float ) chan ) * seconds );
return (unsigned int)(((float)samples_psec * (float) chan) * seconds);
}
unsigned int samples_in_bytes(unsigned int bytes) const
{
return (bytes / ((bits_psample / 8) * chan));
}
unsigned int bytes_in_samples(unsigned int samples) const
{
return (samples * ((bits_psample / 8) * chan));
}
};
extern audio_format UNKNOWN_FORMAT;
extern audio_format A44100_16BIT_STEREO;
extern audio_format A44100_16BIT_MONO;
_AUDIO_NAMESPACE_END_
#endif //ifdef _AUDIOFORMAT__H_
#endif /* _AUDIOFORMAT__H_ */

View file

@ -5,502 +5,225 @@
* PROGRAMMERS: Marco Pagliaricci (irc: rendar)
*/
#include "stdafx.h"
#include "audio_membuffer.hpp"
_AUDIO_NAMESPACE_START_
//////////////////////////////////////
/////// Protected Functions /////////
//////////////////////////////////////
/* Protected Functions */
void
audio_membuffer::alloc_mem_(unsigned int bytes)
{
//
// Some checking
//
/* Some checking */
if (bytes == 0)
return;
//
// Checks previsiously alloc'd memory
// and frees it.
//
/* Checks previsiously alloc'd memory and frees it */
if (audio_data)
delete[] audio_data;
//
// Allocs new memory and zeros it.
//
/* Allocs new memory and zeros it */
audio_data = new BYTE[bytes];
memset(audio_data, 0, bytes * sizeof(BYTE));
//
// Sets the correct buffer size
//
/* Sets the correct buffer size */
buf_size = bytes;
init_size = bytes;
}
void
audio_membuffer::free_mem_(void)
{
if (audio_data)
delete[] audio_data;
buf_size = 0;
audio_data = 0;
}
void
audio_membuffer::resize_mem_(unsigned int new_size)
{
if (new_size == 0)
return;
//
// The new_size, cannot be <= of the
// `bytes_received' member value of the
// parent class `audio_receiver'.
// We cannot touch received audio data,
// so we have to alloc at least
// bytes_received+1 bytes.
//
// But we can truncate unused memory, so
// `new_size' can be < of `buf_size'.
//
/* The new_size, cannot be <= of the `bytes_received' member value of the
parent class `audio_receiver'. We cannot touch received audio data,
so we have to alloc at least bytes_received+1 bytes. But we can truncate
unused memory, so `new_size' can be < of `buf_size' */
if (new_size <= bytes_received)
return;
BYTE * new_mem;
//
// Allocs new memory and zeros it.
//
/* Allocs new memory and zeros it */
new_mem = new BYTE[new_size];
memset(new_mem, 0, new_size * sizeof(BYTE));
if (audio_data)
{
//
// Copies received audio data, and discard
// unused memory.
//
/* Copies received audio data, and discard unused memory */
memcpy(new_mem, audio_data, bytes_received);
//
// Frees old memory.
//
/* Frees old memory */
delete[] audio_data;
//
// Commit new memory.
//
/* Commit new memory */
audio_data = new_mem;
buf_size = new_size;
} else {
audio_data = new_mem;
buf_size = new_size;
}
if (buffer_resized)
buffer_resized(new_size);
}
void
audio_membuffer::truncate_(void)
{
//
// If `buf_size' is already = to the
// `bytes_received' of audio data, then
// this operation is useless; simply return.
//
/* If `buf_size' is already = to the `bytes_received' of audio data,
then this operation is useless; simply return */
if (bytes_received == buf_size)
return;
if (audio_data)
{
//
// Allocs a new buffer.
//
/* Allocs a new buffer */
BYTE * newbuf = new BYTE[bytes_received];
//
// Copies audio data.
//
/* Copies audio data */
memcpy(newbuf, audio_data, bytes_received);
//
// Frees old memory.
//
/* Frees old memory */
delete[] audio_data;
//
// Commit the new buffer.
//
/* Commit the new buffer */
audio_data = newbuf;
buf_size = bytes_received;
//
// Buffer truncation successfull.
// Now the buffer size is exactly big
// as much audio data was received.
//
/* Buffer truncation successfull. Now the buffer size is exactly big
as much audio data was received */
}
}
}
//////////////////////////////////////
/////// Public Functions ///////////
//////////////////////////////////////
/* Public Functions */
void
audio_membuffer::clear(void)
{
free_mem_();
bytes_received = 0;
}
void
audio_membuffer::reset(void)
{
//
// Frees memory and reset
// to initial state.
//
/* Frees memory and reset to initial state */
clear();
//
// Alloc memory of size specified
// at the constructor.
//
/* Alloc memory of size specified at the constructor */
alloc_mem_(init_size);
}
void
audio_membuffer::alloc_bytes(unsigned int bytes)
{
alloc_mem_(bytes);
}
void
audio_membuffer::alloc_seconds(unsigned int secs)
{
alloc_mem_(aud_info.byte_rate() * secs);
}
void
audio_membuffer::alloc_seconds(float secs)
{
alloc_mem_((unsigned int)((float)aud_info.byte_rate() * secs));
}
void
audio_membuffer::resize_bytes(unsigned int bytes)
{
resize_mem_(bytes);
}
void
audio_membuffer::resize_seconds(unsigned int secs)
{
resize_mem_(aud_info.byte_rate() * secs);
}
void
audio_membuffer::resize_seconds(float secs)
{
resize_mem_(( unsigned int )
(( float )aud_info.byte_rate() * secs )
);
resize_mem_((unsigned int)((float)aud_info.byte_rate() * secs));
}
///////////////////////////////////////
/////// Inherited Functions /////////
///////////////////////////////////////
/* Inherited Functions */
void
audio_membuffer::audio_receive
( unsigned char * data, unsigned int size )
audio_membuffer::audio_receive(unsigned char *data, unsigned int size)
{
//
// If there isn't a buffer, allocs memory for
// it of size*2, and copies audio data arrival.
//
/* If there isn't a buffer, allocs memory for it of size*2, and copies audio data arrival */
if ((audio_data == 0) || (buf_size == 0))
{
alloc_mem_(size * 2);
memcpy(audio_data, data, size);
return;
}
//
// If buffer's free memory is < of `size',
// we have to realloc buffer memory of
// buf_size*2, while free memory is enough
// to contain `size' bytes.
//
// In this case free memory is represented
// by `buf_size - bytes_recorded'.
//
unsigned int tot_mem = buf_size,
free_mem = buf_size - bytes_received;
/* If buffer's free memory is < of `size', we have to realloc buffer memory
of buf_size*2, while free memory is enough to contain `size' bytes.
In this case free memory is represented by `buf_size - bytes_recorded' */
unsigned int tot_mem = buf_size, free_mem = buf_size - bytes_received;
if (free_mem < size)
{
//
// Calcs new buffer size.
// TODO: flags for other behaviour?
/* Calcs new buffer size */
/* TODO: flags for other behaviour? */
while (free_mem < size)
{
tot_mem *= 2;
free_mem = tot_mem - bytes_received;
}
//
// Resize buffer memory.
//
/* Resize buffer memory */
resize_mem_(tot_mem);
}
//
// Now we have enough free space in the
// buffer, so let's copy audio data arrivals.
//
/* Now we have enough free space in the buffer, so let's copy audio data arrivals */
memcpy(audio_data + bytes_received, data, size);
if (audio_arrival)
audio_arrival(aud_info.samples_in_bytes(size));
}
unsigned int
audio_membuffer::read(BYTE *out_buf, unsigned int bytes)
{
//
// Some checking
//
/* Some checking */
if (!audio_data)
return 0;
if (bytes_played_ >= bytes_received)
return 0;
unsigned int to_play = bytes_received - bytes_played_;
unsigned int to_copy = bytes > to_play ? to_play : bytes;
unsigned int to_play =
bytes_received - bytes_played_;
unsigned int to_copy =
bytes > to_play ? to_play : bytes;
//
// Copies the audio data out.
//
/* Copies the audio data out */
if ((out_buf) && (to_copy) && (audio_data))
memcpy(out_buf, audio_data + bytes_played_, to_copy);
//
// Increments the number of total bytes
// played (audio data gone out from the
// `audio_producer' object).
//
/* Increments the number of total bytes played (audio data gone out from
the `audio_producer' object) */
bytes_played_ += to_copy;
if (audio_arrival)
audio_arrival(aud_info.samples_in_bytes(to_copy));
//
// Returns the exact size of audio data
// produced.
//
/* Returns the exact size of audio data produced */
return to_copy;
}
bool
audio_membuffer::finished(void)
{
@ -510,5 +233,4 @@ audio_membuffer::finished( void )
return true;
}
_AUDIO_NAMESPACE_END_

View file

@ -5,342 +5,195 @@
* PROGRAMMERS: Marco Pagliaricci (irc: rendar)
*/
#ifndef _AUDIOMEMBUFFER__H_
#define _AUDIOMEMBUFFER__H_
//#include "audio_def.hpp"
#include "audio_receiver.hpp"
#include "audio_format.hpp"
#include "audio_producer.hpp"
_AUDIO_NAMESPACE_START_
class audio_membuffer : public audio_receiver, public audio_producer
{
protected:
BYTE * audio_data;
audio_format aud_info;
unsigned int buf_size;
unsigned int init_size;
/* Protected Functions */
//
// Protected Functions
//
//allocs N bytes for the audio buffer.
/* allocs N bytes for the audio buffer */
void alloc_mem_(unsigned int);
//frees memory
/* frees memory */
void free_mem_(void);
//resizes memory, and copies old
//audio data to new-size memory
/* resizes memory, and copies old audio data to new-size memory */
void resize_mem_(unsigned int);
//truncates and discards unused memory.
//`buf_size' will be the same as `bytes_received'.
/* truncates and discards unused memory. `buf_size' will be the same as `bytes_received' */
void truncate_(void);
public:
void (* audio_arrival)(unsigned int);
void (* buffer_resized)(unsigned int);
//
// Ctors
//
audio_membuffer( void )
: audio_data( 0 ), aud_info( _AUDIO_DEFAULT_FORMAT ),
buf_size( 0 ), init_size( 0 )
{
//
// Allocs memory for at least 1 or some seconds
// of recording.
//
init_size = ( unsigned int )
(( float )aud_info.byte_rate() * _AUDIO_DEFAULT_BUFSECS );
alloc_mem_( init_size );
}
audio_membuffer( audio_format aud_fmt )
: audio_data( 0 ), aud_info( aud_fmt ), buf_size( 0 ),
/* Ctors */
audio_membuffer(void) : audio_data(0),
aud_info(_AUDIO_DEFAULT_FORMAT),
buf_size(0),
init_size(0)
{
//
// Allocs memory for at least 1 or some seconds
// of recording.
//
init_size = ( unsigned int )
(( float )aud_info.byte_rate() * _AUDIO_DEFAULT_BUFSECS );
/* Allocs memory for at least 1 or some seconds of recording */
init_size = (unsigned int)((float)aud_info.byte_rate() * _AUDIO_DEFAULT_BUFSECS);
alloc_mem_(init_size);
}
audio_membuffer( audio_format aud_fmt, unsigned int seconds )
: audio_data( 0 ), aud_info( aud_fmt ), buf_size( 0 ),
audio_membuffer(audio_format aud_fmt) : audio_data(0),
aud_info(aud_fmt),
buf_size(0),
init_size(0)
{
/* Allocs memory for at least 1 or some seconds of recording */
init_size = (unsigned int)((float)aud_info.byte_rate() * _AUDIO_DEFAULT_BUFSECS);
alloc_mem_(init_size);
}
//
// Allocs memory for audio recording
// the specified number of seconds.
//
audio_membuffer(audio_format aud_fmt, unsigned int seconds) : audio_data(0),
aud_info(aud_fmt),
buf_size(0),
init_size(0)
{
/* Allocs memory for audio recording the specified number of seconds */
init_size = aud_info.byte_rate() * seconds;
alloc_mem_(init_size);
}
audio_membuffer( audio_format aud_fmt, float seconds )
: audio_data( 0 ), aud_info( aud_fmt ), buf_size( 0 ),
audio_membuffer(audio_format aud_fmt, float seconds) : audio_data(0),
aud_info(aud_fmt),
buf_size(0),
init_size(0)
{
//
// Allocs memory for audio recording
// the specified number of seconds.
//
init_size = ( unsigned int )(( float ) aud_info.byte_rate() *
seconds <= 0 ? 1 : seconds );
/* Allocs memory for audio recording the specified number of seconds */
init_size = (unsigned int)((float)aud_info.byte_rate() * seconds <= 0 ? 1 : seconds);
alloc_mem_(init_size);
}
audio_membuffer( unsigned int bytes )
: audio_data( 0 ), aud_info( _AUDIO_DEFAULT_FORMAT ),
buf_size( 0 ), init_size( 0 )
audio_membuffer(unsigned int bytes) : audio_data(0),
aud_info(_AUDIO_DEFAULT_FORMAT),
buf_size(0),
init_size(0)
{
//
// Allocs memory for the specified bytes
//
/* Allocs memory for the specified bytes */
init_size = bytes;
alloc_mem_(init_size);
}
//
// Dtor
//
/* Dtor */
virtual ~audio_membuffer(void)
{
//
// Frees memory and reset values.
//
/* Frees memory and reset values */
clear();
}
/* Public functions */
//
// Public functions
//
//returns the audio buffer size in bytes.
/* returns the audio buffer size in bytes */
unsigned int mem_size(void) const
{ return buf_size; }
{
return buf_size;
}
//returns how many audio data has been
//received, in bytes.
/* returns how many audio data has been received, in bytes */
unsigned int bytes_recorded(void) const
{ return bytes_received; }
{
return bytes_received;
}
//returns the integer number of seconds
//that the buffer can record
/* returns the integer number of seconds that the buffer can record */
unsigned int seconds_total(void) const
{ return buf_size / aud_info.byte_rate(); }
{
return buf_size / aud_info.byte_rate();
}
//returns the integer number of seconds
//that the buffer can record
/* returns the integer number of seconds that the buffer can record */
unsigned int seconds_recorded(void) const
{ return bytes_received / aud_info.byte_rate(); }
{
return bytes_received / aud_info.byte_rate();
}
//returns the float number of seconds
//that the buffer can record
/* returns the float number of seconds that the buffer can record */
float fseconds_total(void) const
{ return ( float )(( float ) buf_size /
( float ) aud_info.byte_rate()); }
{
return (float)((float) buf_size / (float)aud_info.byte_rate());
}
//returns the float number of seconds
//that has been recorded
/* returns the float number of seconds that has been recorded */
float fseconds_recorded(void) const
{ return ( float )(( float ) bytes_received /
( float ) aud_info.byte_rate()); }
{
return (float)((float)bytes_received / (float)aud_info.byte_rate());
}
unsigned int total_samples(void) const
{
return (aud_info.samples_in_seconds(fseconds_total()));
}
unsigned int samples_received(void) const
{
return (aud_info.samples_in_bytes(bytes_received));
}
//returns a pointer to the audio buffer
/* returns a pointer to the audio buffer */
BYTE * audio_buffer(void) const
{ return audio_data; }
{
return audio_data;
}
//frees memory and resets values.
/* frees memory and resets values */
void clear(void);
audio_format & audinfo(void)
{
return aud_info;
}
audio_format & audinfo( void ) { return aud_info; }
//discard audio data, resets values,
//but, instead of clear() which frees memory,
//reset the memory to the initial size, ready
//for receiving "new" audio data.
/* discard audio data, resets values, but, instead of clear() which
frees memory, reset the memory to the initial size, ready for
receiving "new" audio data. */
void reset(void);
//truncates and discards unused memory.
//`buf_size' will be the same as `bytes_received'.
/* truncates and discards unused memory. `buf_size' will be the same as `bytes_received' */
void truncate(void)
{ truncate_( ); }//TODO: fare truncate N bytes
{
truncate_();
} /* TODO: fare truncate N bytes */
//if there is a buffer, discards current buffer
//memory and realloc a new memory buffer with a
//new size expressed in bytes.
/* if there is a buffer, discards current buffer memory and realloc
a new memory buffer with a new size expressed in bytes. */
void alloc_bytes(unsigned int);
//if there is a buffer, discards current buffer
//memory and realloc a new memory buffer with a
//new size expressed in seconds, integer and float.
/* if there is a buffer, discards current buffer memory and realloc
a new memory buffer with a new size expressed in seconds, integer and float. */
void alloc_seconds(unsigned int);
void alloc_seconds(float);
//resizes in bytes the current buffer,
//without discarding previsiously audio data received.
/* resizes in bytes the current buffer, without discarding
previsiously audio data received */
void resize_bytes(unsigned int);
//resizes in seconds the current buffer,
//without discarding previsiously audio data received.
/* resizes in seconds the current buffer, without discarding
previsiously audio data received */
void resize_seconds( unsigned int );
void resize_seconds( float );
//
// Inherited Functions from `audio_receiver'
//
/* Inherited Functions from `audio_receiver' */
void audio_receive(unsigned char *, unsigned int);
//
// Inherited Functions from `audio_buffer'
//
/* Inherited Functions from `audio_buffer' */
unsigned int read(BYTE *, unsigned int);
bool finished(void);
};
_AUDIO_NAMESPACE_END_
#endif //ifdef _AUDIOMEMBUFFER__H_
#endif /* _AUDIOMEMBUFFER__H_ */

View file

@ -7,4 +7,3 @@
#include "stdafx.h"
#include "audio_producer.hpp"

View file

@ -8,82 +8,38 @@
#ifndef _AUDIOAUDBUF__H_
#define _AUDIOAUDBUF__H_
#include "audio_def.hpp"
//#include "audio_producer.hpp"
_AUDIO_NAMESPACE_START_
class audio_producer
{
protected:
unsigned int bytes_played_;
public:
//
// Ctors
//
/* Ctors */
audio_producer() : bytes_played_(0), play_finished(0)
{ }
//
// Dtor
//
{
}
/* Dtor */
virtual ~audio_producer(void)
{ }
{
}
/* Public Functions */
//
// Public Functions
//
//reads N bytes from the buffer
/* reads N bytes from the buffer */
virtual unsigned int read(BYTE *, unsigned int) = 0;
virtual bool finished(void) = 0;
unsigned int bytes_played(void) const
{
return bytes_played_;
}
void set_position(unsigned int pos)
{
bytes_played_ = pos;
@ -94,34 +50,19 @@ class audio_producer
bytes_played_ = 0;
}
void forward(unsigned int bytes)
{
bytes_played_ += bytes;
}
void backward(unsigned int bytes)
{
bytes_played_ += bytes;
}
void (* play_finished)(void);
};
_AUDIO_NAMESPACE_END_
#endif //ifdef _AUDIOAUDBUF__H_
#endif /* _AUDIOAUDBUF__H_ */

View file

@ -5,104 +5,44 @@
* PROGRAMMERS: Marco Pagliaricci (irc: rendar)
*/
#ifndef _AUDIORECEIVER_DEF__H_
#define _AUDIORECEIVER_DEF__H_
#include "audio_def.hpp"
_AUDIO_NAMESPACE_START_
class audio_receiver
{
//
// The `audio_wavein' class, while is
// recording audio, has to access to
// protected members of `audio_receiver'
// such as `bytes_received' protected
// variable.
//
/* The `audio_wavein' class, while is recording audio, has to access to
protected members of `audio_receiver' such as `bytes_received'
protected variable */
friend class audio_wavein;
protected:
unsigned int bytes_received;
public:
/* Ctors */
audio_receiver(void) : bytes_received(0)
{
}
//
// Ctors
//
audio_receiver( void )
: bytes_received( 0 )
{ }
//
// Dtor
//
/* Dtor */
virtual ~audio_receiver(void)
{ }
{
}
//
// Public Functions
//
/* Public Functions */
virtual void audio_receive(unsigned char *, unsigned int) = 0;
void set_b_received(unsigned int r)
{ bytes_received = r; }
{
bytes_received = r;
}
};
_AUDIO_NAMESPACE_END_
#endif //ifdef _AUDIORECEIVER_DEF__H_
#endif /* _AUDIORECEIVER_DEF__H_ */

View file

@ -5,53 +5,28 @@
* PROGRAMMERS: Marco Pagliaricci (irc: rendar)
*/
#include "stdafx.h"
#include "audio_resampler_acm.hpp"
//#include <stdio.h>
_AUDIO_NAMESPACE_START_
/////////////////////////////////////////
/////// Private Functions ////////
/////////////////////////////////////////
/* Private Functions */
void
audio_resampler_acm::init_(void)
{
//
// Zeroing structures
//
/* Zeroing structures */
ZeroMemory(&acm_header, sizeof(ACMSTREAMHEADER));
ZeroMemory(&wformat_src, sizeof(WAVEFORMATEX));
ZeroMemory(&wformat_dst, sizeof(WAVEFORMATEX));
//
// Setting structures sizes
//
/* Setting structures sizes */
acm_header.cbStruct = sizeof(ACMSTREAMHEADER);
wformat_src.cbSize = sizeof(WAVEFORMATEX);
wformat_dst.cbSize = sizeof(WAVEFORMATEX);
//
// Setting WAVEFORMATEX structure parameters
// according to `audio_format' in/out classes
//
/* Setting WAVEFORMATEX structure parameters
according to `audio_format' in/out classes */
wformat_src.wFormatTag = WAVE_FORMAT_PCM;
wformat_src.nSamplesPerSec = audfmt_in.sample_rate();
@ -60,7 +35,6 @@ audio_resampler_acm::init_( void )
wformat_src.nAvgBytesPerSec = audfmt_in.byte_rate();
wformat_src.nBlockAlign = audfmt_in.block_align();
wformat_dst.wFormatTag = WAVE_FORMAT_PCM;
wformat_dst.nSamplesPerSec = audfmt_out.sample_rate();
wformat_dst.nChannels = audfmt_out.channels();
@ -68,298 +42,152 @@ audio_resampler_acm::init_( void )
wformat_dst.nAvgBytesPerSec = audfmt_out.byte_rate();
wformat_dst.nBlockAlign = audfmt_out.block_align();
//
// Init acm structures completed successfull
//
/* Init acm structures completed successfull */
}
/////////////////////////////////////////
/////// Public Functions ////////
/////////////////////////////////////////
/* Public Functions */
void
audio_resampler_acm::open(void)
{
MMRESULT err;
//
// Opens ACM stream
//
err = acmStreamOpen( &acm_stream, 0, &wformat_src, &wformat_dst,
0, 0, 0, ACM_STREAMOPENF_NONREALTIME );
/* Opens ACM stream */
err = acmStreamOpen(&acm_stream,
0,
&wformat_src,
&wformat_dst,
0, 0, 0,
ACM_STREAMOPENF_NONREALTIME);
if (err != MMSYSERR_NOERROR)
{
//TODO: throw error
/* TODO: throw error */
MessageBox(0, _T("acmOpen error: %i"), _T("ERROR"), MB_ICONERROR);
}
/* Calcs source buffer length */
src_buflen = (unsigned int)((float)audfmt_in.byte_rate() * (float)buf_secs);
//
// Calcs source buffer length
//
src_buflen = ( unsigned int )
(( float )audfmt_in.byte_rate() * ( float )buf_secs );
//
// Calcs destination source buffer length
// with help of ACM apis
//
/* Calcs destination source buffer length with help of ACM apis */
err = acmStreamSize(acm_stream,
src_buflen, &dst_buflen, ACM_STREAMSIZEF_SOURCE );
src_buflen,
&dst_buflen,
ACM_STREAMSIZEF_SOURCE);
if (err != MMSYSERR_NOERROR)
{
//TODO: throw error
/* TODO: throw error */
MessageBox(0, _T("acmStreamSize error"), _T("ERROR"), MB_ICONERROR);
}
//
// Initialize ACMSTREAMHEADER structure,
// and alloc memory for source and destination
// buffers.
//
/* Initialize ACMSTREAMHEADER structure,
and alloc memory for source and destination buffers */
acm_header.fdwStatus = 0;
acm_header.dwUser = 0;
acm_header.pbSrc = (LPBYTE) new BYTE[src_buflen];
acm_header.cbSrcLength = src_buflen;
acm_header.cbSrcLengthUsed = 0;
acm_header.dwSrcUser = src_buflen;
acm_header.pbDst = (LPBYTE) new BYTE[dst_buflen];
acm_header.cbDstLength = dst_buflen;
acm_header.cbDstLengthUsed = 0;
acm_header.dwDstUser = dst_buflen;
//
// Give ACMSTREAMHEADER initialized correctly to the
// driver.
//
/* Give ACMSTREAMHEADER initialized correctly to the driver */
err = acmStreamPrepareHeader(acm_stream, &acm_header, 0L);
if (err != MMSYSERR_NOERROR)
{
//TODO: throw error
/* TODO: throw error */
MessageBox(0, _T("acmStreamPrepareHeader error"), _T("ERROR"), MB_ICONERROR);
}
//
// ACM stream successfully opened.
//
/* ACM stream successfully opened */
stream_opened = true;
}
void
audio_resampler_acm::close(void)
{
MMRESULT err;
if (acm_stream)
{
if (acm_header.fdwStatus & ACMSTREAMHEADER_STATUSF_PREPARED)
{
acm_header.cbSrcLength = src_buflen;
acm_header.cbDstLength = dst_buflen;
err = acmStreamUnprepareHeader(acm_stream, &acm_header, 0L);
if (err != MMSYSERR_NOERROR)
{
//
// Free buffer memory
//
/* Free buffer memory */
if (acm_header.pbSrc != 0)
delete[] acm_header.pbSrc;
if (acm_header.pbDst != 0)
delete[] acm_header.pbDst;
//
// Re-init structures
//
/* Re-init structures */
init_();
//
// Updating status
//
/* Updating status */
stream_opened = false;
//TODO: throw error
/* TODO: throw error */
MessageBox(0, _T("acmStreamUnPrepareHeader error"), _T("ERROR"), MB_ICONERROR);
}
}
err = acmStreamClose(acm_stream, 0);
acm_stream = 0;
if (err != MMSYSERR_NOERROR)
{
//
// Free buffer memory
//
/* Free buffer memory */
if (acm_header.pbSrc != 0)
delete[] acm_header.pbSrc;
if (acm_header.pbDst != 0)
delete[] acm_header.pbDst;
//
// Re-init structures
//
/* Re-init structures */
init_();
//
// Updating status
//
/* Updating status */
stream_opened = false;
//TODO: throw error!
/* TODO: throw error! */
MessageBox(0, _T("acmStreamClose error"), _T("ERROR"), MB_ICONERROR);
}
} /* if acm_stream != 0 */
}//if acm_stream != 0
//
// Free buffer memory
//
/* Free buffer memory */
if (acm_header.pbSrc != 0)
delete[] acm_header.pbSrc;
if (acm_header.pbDst != 0)
delete[] acm_header.pbDst;
//
// Re-init structures
//
/* Re-init structures */
init_();
//
// Updating status
//
/* Updating status */
stream_opened = false;
//
// ACM sream successfully closed.
//
/* ACM sream successfully closed */
}
void
audio_resampler_acm::audio_receive(unsigned char *data, unsigned int size)
{
MMRESULT err;
//
// Checking for acm stream opened
//
/* Checking for acm stream opened */
if (stream_opened)
{
//
// Copy audio data from extern to
// internal source buffer
//
/* Copy audio data from extern to internal source buffer */
memcpy(acm_header.pbSrc, data, size);
acm_header.cbSrcLength = size;
acm_header.cbDstLengthUsed = 0;
@ -367,41 +195,16 @@ audio_resampler_acm::audio_receive( unsigned char * data, unsigned int size )
if (err != MMSYSERR_NOERROR)
{
//TODO: throw error
/* TODO: throw error */
MessageBox(0, _T("acmStreamConvert error"), _T("ERROR"), MB_ICONERROR);
}
//
// Wait for sound conversion
//
/* Wait for sound conversion */
while ((ACMSTREAMHEADER_STATUSF_DONE & acm_header.fdwStatus) == 0);
//
// Copy resampled audio, to destination buffer.
//
/* Copy resampled audio, to destination buffer */
//memcpy(pbOutputData, acm_header.pbDst, acm_header.cbDstLengthUsed);
}
}
_AUDIO_NAMESPACE_END_

View file

@ -5,34 +5,21 @@
* PROGRAMMERS: Marco Pagliaricci (irc: rendar)
*/
#ifndef _AUDIORESAMPLERACM__H_
#define _AUDIORESAMPLERACM__H_
//#include "audio_def.hpp"
#include "audio_receiver.hpp"
#include "audio_format.hpp"
_AUDIO_NAMESPACE_START_
//TODO: inherit from a base resampler?
/* TODO: inherit from a base resampler? */
class audio_resampler_acm : public audio_receiver
{
private:
void init_(void);
protected:
HACMSTREAM acm_stream;
ACMSTREAMHEADER acm_header;
DWORD src_buflen;
@ -47,61 +34,31 @@ class audio_resampler_acm : public audio_receiver
WAVEFORMATEX wformat_src;
WAVEFORMATEX wformat_dst;
public:
//
// Ctors
//
audio_resampler_acm( audio_format fmt_in, audio_format fmt_out )
: acm_stream( 0 ), src_buflen( 0 ), dst_buflen( 0 ),
stream_opened( false ), audfmt_in( fmt_in ), audfmt_out( fmt_out ),
/* Ctors */
audio_resampler_acm(audio_format fmt_in,
audio_format fmt_out) : acm_stream(0),
src_buflen(0),
dst_buflen(0),
stream_opened(false),
audfmt_in(fmt_in),
audfmt_out(fmt_out),
buf_secs(_AUDIO_DEFAULT_BUFSECS)
{
init_();
}
//
// Dtor
//
/* Dtor */
~audio_resampler_acm(void)
{ }
//
// Public functions
//
{
}
/* Public functions */
void open(void);
void close(void);
void audio_receive(unsigned char *, unsigned int);
};
_AUDIO_NAMESPACE_END_
#endif //ifdef _AUDIORESAMPLERACM_H_
#endif /* _AUDIORESAMPLERACM__H_ */

View file

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

View file

@ -5,136 +5,67 @@
* PROGRAMMERS: Marco Pagliaricci (irc: rendar)
*/
#ifndef _AUDIOWAVEIN_H_
#define _AUDIOWAVEIN_H_
//#include "audio_def.hpp"
#include "audio_format.hpp"
#include "audio_receiver.hpp"
_AUDIO_NAMESPACE_START_
enum audio_wavein_status { WAVEIN_NOTREADY, WAVEIN_READY,
WAVEIN_RECORDING, WAVEIN_ERR,
WAVEIN_STOP, WAVEIN_FLUSHING
enum audio_wavein_status
{
WAVEIN_NOTREADY,
WAVEIN_READY,
WAVEIN_RECORDING,
WAVEIN_ERR,
WAVEIN_STOP,
WAVEIN_FLUSHING
};
class audio_wavein
{
private:
//
// The new recording thread sends message to this procedure
// about open recording, close, and sound data recorded
//
/* The new recording thread sends message to this procedure
about open recording, close, and sound data recorded */
static DWORD WINAPI recording_procedure(LPVOID);
//
// When this event is signaled, then the previsiously created
// recording thread will wake up and start recording audio
// and will pass audio data to an `audio_receiver' object.
//
/* When this event is signaled, then the previsiously created
recording thread will wake up and start recording audio
and will pass audio data to an `audio_receiver' object. */
HANDLE wakeup_recthread;
HANDLE data_flushed_event;
protected:
/* TODO: puts these structs in private?! */
//TODO: puts these structs in private?!
//
// Audio wavein device stuff
//
/* Audio wavein device stuff */
WAVEFORMATEX wave_format;
WAVEHDR *wave_headers;
HWAVEIN wavein_handle;
audio_format aud_info;
audio_receiver &audio_rcvd;
//
// Audio Recorder Thread id
//
/* Audio Recorder Thread id */
DWORD recthread_id;
//
// Object status
//
/* Object status */
audio_wavein_status status;
//
// How many seconds of audio
// can record the internal buffer
// before flushing audio data
// to the `audio_receiver' class?
//
/* How many seconds of audio can record the internal buffer before
flushing audio data to the `audio_receiver' class? */
float buf_secs;
//
// The temporary buffers for the audio
// data incoming from the wavein device
// and its size, and its total number.
//
/* The temporary buffers for the audio data incoming from the wavein
device and its size, and its total number */
BYTE *main_buffer;
unsigned int mb_size;
unsigned int buffers;
/* Protected Functions */
//
// Protected Functions
//
//initialize all structures and variables.
/* initialize all structures and variables */
void init_(void);
void alloc_buffers_mem_(unsigned int, float);
@ -145,135 +76,88 @@ class audio_wavein
void unprep_headers_(void);
void add_buffers_to_driver_(void);
public:
//
// Ctors
//
audio_wavein(
const audio_format & a_info, audio_receiver & a_receiver )
: wave_headers( 0 ),
aud_info( a_info ), audio_rcvd( a_receiver ),
status( WAVEIN_NOTREADY ), main_buffer( 0 ), mb_size( 0 ),
/* Ctors */
audio_wavein(const audio_format &a_info,
audio_receiver &a_receiver) : wave_headers(0),
aud_info(a_info),
audio_rcvd(a_receiver),
status(WAVEIN_NOTREADY),
main_buffer(0),
mb_size(0),
buffers(_AUDIO_DEFAULT_WAVEINBUFFERS)
{
//
// Initializing internal wavein data
//
/* Initializing internal wavein data */
init_();
aud_info = a_info;
}
//
// Dtor
//
/* Dtor */
~audio_wavein(void)
{
//close(); TODO!
}
//
// Public functions
//
/* Public functions */
void open(void);
void close(void);
void start_recording(void);
void stop_recording(void);
audio_wavein_status current_status (void) const
{
return status;
}
float buffer_secs(void) const
{ return buf_secs; }
{
return buf_secs;
}
void buffer_secs(float bsecs)
{
//
// Some checking
//
/* Some checking */
if (bsecs <= 0)
return;
//
// Set seconds length for each
// buffer.
//
/* Set seconds length for each buffer */
buf_secs = bsecs;
}
unsigned int total_buffers(void) const
{ return buffers; }
{
return buffers;
}
void total_buffers(unsigned int tot_bufs)
{
//
// Some checking
//
/* Some checking */
if (tot_bufs == 0)
return;
//
// Sets the number of total buffers.
//
/* Sets the number of total buffers */
buffers = tot_bufs;
}
audio_format format(void) const
{ return aud_info; }
{
return aud_info;
}
BYTE *buf(void)
{
return main_buffer;
}
BYTE * buf( void ) { return main_buffer; }
unsigned int bufsz( void ) { return mb_size; }
unsigned int bufsz(void)
{
return mb_size;
}
unsigned int samplevalue_max(void)
{
if (aud_info.bits() == 16)
return (unsigned int)65535;
@ -284,45 +168,26 @@ class audio_wavein
return 0;
}
unsigned tot_samples_buf(void)
{
return aud_info.samples_in_bytes(mb_size);
}
unsigned int nsample(unsigned int nsamp)
{
unsigned int svalue;
if (aud_info.bits() == 16)
svalue = (unsigned int)abs(*((short *)(main_buffer + aud_info.bytes_in_samples(nsamp))));
else if (aud_info.bits() == 8)
svalue = (unsigned int)((ptrdiff_t) *(main_buffer + aud_info.bytes_in_samples(nsamp)));
else
svalue = 0;
return svalue;
}
};
_AUDIO_NAMESPACE_END_
#endif //ifdef _AUDIOWAVEIN_H_
#endif /* _AUDIOWAVEIN_H_ */

File diff suppressed because it is too large Load diff

View file

@ -5,178 +5,90 @@
* PROGRAMMERS: Marco Pagliaricci (irc: rendar)
*/
#ifndef _AUDIOWAVEOUT__H_
#define _AUDIOWAVEOUT__H_
//#include "audio_def.hpp"
#include "audio_format.hpp"
#include "audio_producer.hpp"
_AUDIO_NAMESPACE_START_
enum audio_waveout_status { WAVEOUT_NOTREADY, WAVEOUT_READY,
WAVEOUT_PLAYING, WAVEOUT_FLUSHING,
WAVEOUT_PAUSED, WAVEOUT_STOP,
WAVEOUT_ERR,
enum audio_waveout_status
{
WAVEOUT_NOTREADY,
WAVEOUT_READY,
WAVEOUT_PLAYING,
WAVEOUT_FLUSHING,
WAVEOUT_PAUSED,
WAVEOUT_STOP,
WAVEOUT_ERR
};
class audio_waveout
{
friend class audio_buffer;
private:
static DWORD WINAPI
playing_procedure( LPVOID );
static DWORD WINAPI playing_procedure(LPVOID);
HANDLE wakeup_playthread;
protected:
WAVEFORMATEX wave_format;
WAVEHDR *wave_headers;
HWAVEOUT waveout_handle;
const audio_format &aud_info;
audio_producer &audio_buf;
//
// Audio Playing Thread id
//
/* Audio Playing Thread id */
DWORD playthread_id;
audio_waveout_status status;
float buf_secs;
/* The temporary buffers for the audio data outgoing to the waveout
device and its size, and its total number */
//
// The temporary buffers for the audio
// data outgoing to the waveout device
// and its size, and its total number.
//
//base address for entire memory
/* base address for entire memory */
BYTE *main_buffer;
//size in bytes for the entire memory
/* size in bytes for the entire memory */
unsigned int mb_size;
//number of little buffers
/* number of little buffers */
unsigned int buffers;
//
// Protected Functions
//
/* Protected Functions */
void init_(void);
void alloc_buffers_mem_(unsigned int, float);
void free_buffers_mem_(void);
void init_headers_(void);
void prep_headers_(void);
void unprep_headers_(void);
public:
//
// Ctors
//
/* Ctors */
audio_waveout(const audio_format &aud_fmt,
audio_producer & a_buf )
: wave_headers( 0 ), aud_info( aud_fmt ),
audio_buf( a_buf ), status( WAVEOUT_NOTREADY ),
main_buffer( 0 ), mb_size( 0 ),
audio_producer &a_buf) : wave_headers(0),
aud_info(aud_fmt),
audio_buf(a_buf),
status(WAVEOUT_NOTREADY),
main_buffer(0),
mb_size(0),
buffers(_AUDIO_DEFAULT_WAVEOUTBUFFERS)
{
//
// Initializing internal wavein data
//
/* Initializing internal wavein data */
init_();
}
//
// Dtor
//
/* Dtor */
~audio_waveout(void)
{ }
{
}
//
// Public Functions
//
/* Public Functions */
void open(void);
void play(void);
@ -184,68 +96,51 @@ class audio_waveout
void stop(void);
void close(void);
audio_waveout_status current_status(void)
{ return status; }
{
return status;
}
BYTE *buf(void)
{
return main_buffer;
}
BYTE * buf( void ) { return main_buffer; }
unsigned int bufsz( void ) { return mb_size; }
unsigned int bufsz(void)
{
return mb_size;
}
unsigned int samplevalue_max(void)
{
if (aud_info.bits() == 16)
return (unsigned int)65535;
else if (aud_info.bits() == 8)
return (unsigned int)255;
else
return 0;
}
unsigned tot_samples_buf(void)
{
return aud_info.samples_in_bytes(mb_size);
}
unsigned int nsample(unsigned int nsamp)
{
unsigned int svalue;
if (aud_info.bits() == 16)
svalue = (unsigned int)abs(*((short *)(main_buffer + aud_info.bytes_in_samples(nsamp))));
else if (aud_info.bits() == 8)
svalue = (unsigned int)((ptrdiff_t) *(main_buffer + aud_info.bytes_in_samples(nsamp)));
else
svalue = 0;
return svalue;
}
};
_AUDIO_NAMESPACE_END_
#endif //ifdef _AUDIOWAVEOUT__H_
#endif /* _AUDIOWAVEOUT__H_ */

View file

@ -10,4 +10,4 @@
#include "audio_producer.hpp"
#include "audio_receiver.hpp"
#endif //ifdef _KKAUDIO__H_
#endif /* _AUDIO__H_ */

File diff suppressed because it is too large Load diff

View file

@ -33,14 +33,11 @@
#define BUTTONS_CX (CONTROLS_CX)
#define BUTTONS_SPACE 5
#define SLIDER_CX CONTROLS_CX
#define SLIDER_CY 65
#define SLIDER_H 30
#define SLIDER_W 320
#define STRPOS_X 240
#define STRPOS_Y 5
@ -50,14 +47,12 @@
#define STRBUF_X (STRDUR_X)
#define STRBUF_Y (STRDUR_Y + 13)
#define STRFMT_X 10
#define STRFMT_Y (STRPOS_Y)
#define STRCHAN_X (STRFMT_X)
#define STRCHAN_Y (STRFMT_Y + 13)
#define WAVEBAR_X (CONTROLS_CX + 90)
#define WAVEBAR_Y (STRPOS_Y)
#define WAVEBAR_CX 130
@ -73,13 +68,11 @@
#define REFRESHA_CX (REFRESHA_X + 100)
#define REFRESHA_CY (REFRESHA_Y + 55)
#define REFRESHB_X (STRFMT_X)
#define REFRESHB_Y (STRFMT_Y)
#define REFRESHB_CX (REFRESHB_X + 85)
#define REFRESHB_CY (REFRESHB_Y + 55)
struct riff_hdr
{
DWORD magic;
@ -87,10 +80,8 @@ struct riff_hdr
DWORD format;
};
struct wave_hdr
{
DWORD Subchunkid;
DWORD Subchunk1Size;
WORD AudioFormat;
@ -108,32 +99,15 @@ struct data_chunk
//unsigned char data[];
};
/* Functions prototypes */
LRESULT CALLBACK Buttons_proc(HWND, UINT, WPARAM, LPARAM);
//
// Functions prototypes
//
LRESULT CALLBACK
Buttons_proc(HWND, UINT, WPARAM, LPARAM);
BOOL
write_wav( TCHAR * );
BOOL
open_wav( TCHAR * );
BOOL write_wav(TCHAR *);
BOOL open_wav(TCHAR *);
VOID enable_but(DWORD);
VOID disable_but(DWORD);
void
l_play_finished ( void );
void
l_audio_arrival ( unsigned int );
void
l_buffer_resized ( unsigned int );
void l_play_finished(void);
void l_audio_arrival(unsigned int);
void l_buffer_resized(unsigned int);