2006-12-18 15:39:33 +01:00
|
|
|
/******************************************************************************\
|
2008-04-08 20:38:55 +02:00
|
|
|
* Copyright (c) 2004-2008
|
2006-12-18 15:39:33 +01:00
|
|
|
*
|
|
|
|
* Author(s):
|
|
|
|
* Volker Fischer
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Sound card interface for Windows operating systems
|
|
|
|
*
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation; either version 2 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
\******************************************************************************/
|
|
|
|
|
|
|
|
#include "Sound.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Implementation *************************************************************/
|
2007-12-17 22:28:40 +01:00
|
|
|
#ifdef USE_ASIO_SND_INTERFACE
|
2008-07-12 13:48:51 +02:00
|
|
|
#include <qmutex.h>
|
2007-12-17 22:28:40 +01:00
|
|
|
|
2007-12-18 21:52:48 +01:00
|
|
|
// external references
|
|
|
|
extern AsioDrivers* asioDrivers;
|
|
|
|
bool loadAsioDriver ( char *name );
|
|
|
|
|
2008-07-12 13:48:51 +02:00
|
|
|
// mutex
|
|
|
|
QMutex ASIOMutex;
|
|
|
|
|
|
|
|
// TODO the following variables should be in the class definition but we cannot
|
|
|
|
// do it here since we have static callback functions which cannot access the
|
|
|
|
// class members :-(((
|
|
|
|
|
2008-07-11 23:06:15 +02:00
|
|
|
// ASIO stuff
|
|
|
|
ASIODriverInfo driverInfo;
|
|
|
|
ASIOBufferInfo bufferInfos[2 * NUM_IN_OUT_CHANNELS]; // for input and output buffers -> "2 *"
|
|
|
|
ASIOChannelInfo channelInfos[2 * NUM_IN_OUT_CHANNELS];
|
|
|
|
bool bASIOPostOutput;
|
|
|
|
ASIOCallbacks asioCallbacks;
|
2008-07-13 01:33:27 +02:00
|
|
|
int iBufferSizeMono;
|
|
|
|
int iBufferSizeStereo;
|
2008-07-13 09:26:16 +02:00
|
|
|
int iASIOBufferSizeMono;
|
2008-07-11 23:06:15 +02:00
|
|
|
|
2008-07-12 13:48:51 +02:00
|
|
|
// event
|
|
|
|
HANDLE m_ASIOEvent;
|
|
|
|
|
|
|
|
// wave in
|
2008-07-16 00:09:48 +02:00
|
|
|
short* psCaptureBuffer;
|
|
|
|
int iBufferPosCapture;
|
|
|
|
bool bCaptureBufferOverrun;
|
2008-07-12 13:48:51 +02:00
|
|
|
|
|
|
|
// wave out
|
2008-07-16 00:09:48 +02:00
|
|
|
short* psPlayBuffer;
|
|
|
|
int iBufferPosPlay;
|
|
|
|
bool bPlayBufferUnderrun;
|
2008-07-12 13:48:51 +02:00
|
|
|
|
|
|
|
int iCurNumSndBufIn;
|
|
|
|
int iCurNumSndBufOut;
|
2008-07-14 00:57:31 +02:00
|
|
|
int iNewNumSndBufIn;
|
|
|
|
int iNewNumSndBufOut;
|
2008-07-12 13:48:51 +02:00
|
|
|
|
|
|
|
// we must implement these functions here to get access to global variables
|
2008-07-16 00:09:48 +02:00
|
|
|
int CSound::GetOutNumBuf() { return iNewNumSndBufOut; }
|
|
|
|
int CSound::GetInNumBuf() { return iNewNumSndBufIn; }
|
2008-07-12 13:48:51 +02:00
|
|
|
|
2007-12-18 21:52:48 +01:00
|
|
|
|
2007-12-17 22:28:40 +01:00
|
|
|
/******************************************************************************\
|
|
|
|
* Wave in *
|
|
|
|
\******************************************************************************/
|
|
|
|
bool CSound::Read ( CVector<short>& psData )
|
|
|
|
{
|
2008-07-16 00:09:48 +02:00
|
|
|
int i;
|
2008-07-14 00:57:31 +02:00
|
|
|
bool bError;
|
2007-12-17 22:28:40 +01:00
|
|
|
|
|
|
|
// check if device must be opened or reinitialized
|
2008-01-27 11:05:15 +01:00
|
|
|
if ( bChangParamIn )
|
2007-12-17 22:28:40 +01:00
|
|
|
{
|
2008-07-13 22:03:37 +02:00
|
|
|
// reinit sound interface (init recording requires stereo buffer size)
|
2008-07-13 01:33:27 +02:00
|
|
|
InitRecordingAndPlayback ( iBufferSizeStereo );
|
2007-12-17 22:28:40 +01:00
|
|
|
|
2008-07-13 22:03:37 +02:00
|
|
|
// reset flag
|
2008-01-27 11:05:15 +01:00
|
|
|
bChangParamIn = false;
|
2007-12-17 22:28:40 +01:00
|
|
|
}
|
|
|
|
|
2008-07-16 00:09:48 +02:00
|
|
|
// wait until enough data is available
|
2008-07-17 20:39:24 +02:00
|
|
|
int iWaitCount = 0;
|
2008-07-16 00:09:48 +02:00
|
|
|
while ( iBufferPosCapture < iBufferSizeStereo )
|
2007-12-17 22:28:40 +01:00
|
|
|
{
|
2008-07-17 20:39:24 +02:00
|
|
|
if ( bBlockingRec )
|
2007-12-17 22:28:40 +01:00
|
|
|
{
|
2008-07-17 20:39:24 +02:00
|
|
|
if ( !bCaptureBufferOverrun )
|
|
|
|
{
|
|
|
|
// regular case
|
|
|
|
WaitForSingleObject ( m_ASIOEvent, INFINITE );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// it seems that the buffers are too small, wait
|
|
|
|
// just one time to avoid CPU to go up to 100% and
|
|
|
|
// then leave this function
|
|
|
|
if ( iWaitCount == 0 )
|
|
|
|
{
|
|
|
|
WaitForSingleObject ( m_ASIOEvent, INFINITE );
|
|
|
|
iWaitCount++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2007-12-17 22:28:40 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-16 00:09:48 +02:00
|
|
|
return true;
|
2007-12-17 22:28:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-12 13:48:51 +02:00
|
|
|
ASIOMutex.lock(); // get mutex lock
|
|
|
|
{
|
2008-07-14 00:57:31 +02:00
|
|
|
// check for buffer overrun in ASIO thread
|
2008-07-16 00:09:48 +02:00
|
|
|
bError = bCaptureBufferOverrun;
|
|
|
|
if ( bCaptureBufferOverrun )
|
2008-07-14 00:57:31 +02:00
|
|
|
{
|
|
|
|
// reset flag
|
2008-07-16 00:09:48 +02:00
|
|
|
bCaptureBufferOverrun = false;
|
2008-07-14 00:57:31 +02:00
|
|
|
}
|
|
|
|
|
2008-07-16 00:09:48 +02:00
|
|
|
// copy data from sound card capture buffer in function output buffer
|
2008-07-13 01:33:27 +02:00
|
|
|
for ( i = 0; i < iBufferSizeStereo; i++ )
|
2008-07-12 13:48:51 +02:00
|
|
|
{
|
2008-07-16 00:09:48 +02:00
|
|
|
psData[i] = psCaptureBuffer[i];
|
2008-07-12 13:48:51 +02:00
|
|
|
}
|
2008-01-02 23:16:38 +01:00
|
|
|
|
2008-07-12 13:48:51 +02:00
|
|
|
// move all other data in buffer
|
2008-07-16 00:09:48 +02:00
|
|
|
const int iLenCopyRegion = iBufferPosCapture - iBufferSizeStereo;
|
2008-07-17 20:39:24 +02:00
|
|
|
for ( i = 0; i < iLenCopyRegion; i++ )
|
2008-07-12 13:48:51 +02:00
|
|
|
{
|
2008-07-16 00:09:48 +02:00
|
|
|
psCaptureBuffer[i] = psCaptureBuffer[iBufferSizeStereo + i];
|
2008-07-12 13:48:51 +02:00
|
|
|
}
|
2007-12-17 22:28:40 +01:00
|
|
|
|
2008-07-12 13:48:51 +02:00
|
|
|
// adjust "current block to write" pointer
|
2008-07-16 00:09:48 +02:00
|
|
|
iBufferPosCapture -= iBufferSizeStereo;
|
2008-07-13 22:03:37 +02:00
|
|
|
|
|
|
|
// in case more than one buffer was ready, reset event
|
|
|
|
ResetEvent ( m_ASIOEvent );
|
2008-07-12 13:48:51 +02:00
|
|
|
}
|
|
|
|
ASIOMutex.unlock();
|
2007-12-17 22:28:40 +01:00
|
|
|
|
|
|
|
return bError;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSound::SetInNumBuf ( int iNewNum )
|
|
|
|
{
|
|
|
|
// check new parameter
|
|
|
|
if ( ( iNewNum >= MAX_SND_BUF_IN ) || ( iNewNum < 1 ) )
|
|
|
|
{
|
|
|
|
iNewNum = NUM_SOUND_BUFFERS_IN;
|
|
|
|
}
|
|
|
|
|
|
|
|
// change only if parameter is different
|
|
|
|
if ( iNewNum != iCurNumSndBufIn )
|
|
|
|
{
|
2008-07-14 00:57:31 +02:00
|
|
|
iNewNumSndBufIn = iNewNum;
|
2008-01-27 11:05:15 +01:00
|
|
|
bChangParamIn = true;
|
2007-12-17 22:28:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************\
|
|
|
|
* Wave out *
|
|
|
|
\******************************************************************************/
|
|
|
|
bool CSound::Write ( CVector<short>& psData )
|
|
|
|
{
|
2008-07-14 00:57:31 +02:00
|
|
|
bool bError;
|
2007-12-17 22:28:40 +01:00
|
|
|
|
|
|
|
// check if device must be opened or reinitialized
|
2008-01-27 11:05:15 +01:00
|
|
|
if ( bChangParamOut )
|
2007-12-17 22:28:40 +01:00
|
|
|
{
|
2008-07-13 01:33:27 +02:00
|
|
|
// reinit sound interface (init recording requires stereo buffer size)
|
|
|
|
InitRecordingAndPlayback ( iBufferSizeStereo );
|
2007-12-17 22:28:40 +01:00
|
|
|
|
|
|
|
// reset flag
|
2008-01-27 11:05:15 +01:00
|
|
|
bChangParamOut = false;
|
2007-12-17 22:28:40 +01:00
|
|
|
}
|
|
|
|
|
2008-07-12 23:10:17 +02:00
|
|
|
ASIOMutex.lock(); // get mutex lock
|
2007-12-17 22:28:40 +01:00
|
|
|
{
|
2008-07-14 00:57:31 +02:00
|
|
|
// check for buffer underrun in ASIO thread
|
2008-07-16 00:09:48 +02:00
|
|
|
bError = bPlayBufferUnderrun;
|
|
|
|
if ( bPlayBufferUnderrun )
|
2008-07-14 00:57:31 +02:00
|
|
|
{
|
|
|
|
// reset flag
|
2008-07-16 00:09:48 +02:00
|
|
|
bPlayBufferUnderrun = false;
|
2008-07-14 00:57:31 +02:00
|
|
|
}
|
|
|
|
|
2008-07-16 00:09:48 +02:00
|
|
|
// first check if enough data in buffer is available
|
|
|
|
const int iPlayBufferLen = iCurNumSndBufOut * iBufferSizeStereo;
|
|
|
|
|
|
|
|
if ( iBufferPosPlay + iBufferSizeStereo > iPlayBufferLen )
|
2007-12-17 22:28:40 +01:00
|
|
|
{
|
2008-07-16 00:09:48 +02:00
|
|
|
// buffer overrun, return error
|
|
|
|
bError = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// copy stereo data from function input in soundcard play buffer
|
2008-07-13 01:33:27 +02:00
|
|
|
for ( int i = 0; i < iBufferSizeStereo; i++ )
|
2008-07-12 23:10:17 +02:00
|
|
|
{
|
2008-07-16 00:09:48 +02:00
|
|
|
psPlayBuffer[iBufferPosPlay + i] = psData[i];
|
2008-07-12 23:10:17 +02:00
|
|
|
}
|
2007-12-17 22:28:40 +01:00
|
|
|
|
2008-07-16 00:09:48 +02:00
|
|
|
iBufferPosPlay += iBufferSizeStereo;
|
2007-12-17 22:28:40 +01:00
|
|
|
}
|
|
|
|
}
|
2008-07-12 23:10:17 +02:00
|
|
|
ASIOMutex.unlock();
|
2007-12-17 22:28:40 +01:00
|
|
|
|
|
|
|
return bError;
|
|
|
|
}
|
|
|
|
|
2007-12-19 21:16:50 +01:00
|
|
|
void CSound::SetOutNumBuf ( int iNewNum )
|
2007-12-17 22:28:40 +01:00
|
|
|
{
|
2007-12-19 21:16:50 +01:00
|
|
|
// check new parameter
|
|
|
|
if ( ( iNewNum >= MAX_SND_BUF_OUT ) || ( iNewNum < 1 ) )
|
|
|
|
{
|
|
|
|
iNewNum = NUM_SOUND_BUFFERS_OUT;
|
|
|
|
}
|
2007-12-17 22:28:40 +01:00
|
|
|
|
2007-12-19 21:16:50 +01:00
|
|
|
// change only if parameter is different
|
|
|
|
if ( iNewNum != iCurNumSndBufOut )
|
2007-12-17 22:28:40 +01:00
|
|
|
{
|
2008-07-14 00:57:31 +02:00
|
|
|
iNewNumSndBufOut = iNewNum;
|
2008-01-27 11:05:15 +01:00
|
|
|
bChangParamOut = true;
|
2007-12-17 22:28:40 +01:00
|
|
|
}
|
2007-12-19 21:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************\
|
|
|
|
* Common *
|
|
|
|
\******************************************************************************/
|
|
|
|
void CSound::InitRecordingAndPlayback ( int iNewBufferSize )
|
|
|
|
{
|
2008-07-16 00:09:48 +02:00
|
|
|
int i;
|
2007-12-19 21:16:50 +01:00
|
|
|
|
2008-07-12 23:10:17 +02:00
|
|
|
// first, stop audio and dispose ASIO buffers
|
2007-12-19 21:16:50 +01:00
|
|
|
ASIOStop();
|
2008-07-12 23:10:17 +02:00
|
|
|
ASIODisposeBuffers();
|
2007-12-17 22:28:40 +01:00
|
|
|
|
2008-07-12 13:48:51 +02:00
|
|
|
ASIOMutex.lock(); // get mutex lock
|
2008-04-13 18:43:21 +02:00
|
|
|
{
|
2008-07-13 09:26:16 +02:00
|
|
|
// set internal buffer size value and calculate mono buffer size
|
|
|
|
iBufferSizeStereo = iNewBufferSize;
|
|
|
|
iBufferSizeMono = iBufferSizeStereo / 2;
|
2008-07-13 01:33:27 +02:00
|
|
|
|
2008-07-12 13:48:51 +02:00
|
|
|
// calculate "nearest" buffer size and set internal parameter accordingly
|
|
|
|
// first check minimum and maximum values
|
2008-07-13 09:26:16 +02:00
|
|
|
if ( iBufferSizeMono < HWBufferInfo.lMinSize )
|
2008-04-13 18:43:21 +02:00
|
|
|
{
|
2008-07-13 09:26:16 +02:00
|
|
|
iASIOBufferSizeMono = HWBufferInfo.lMinSize;
|
2008-04-13 18:43:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-13 09:26:16 +02:00
|
|
|
if ( iBufferSizeMono > HWBufferInfo.lMaxSize )
|
2008-07-12 13:48:51 +02:00
|
|
|
{
|
2008-07-13 09:26:16 +02:00
|
|
|
iASIOBufferSizeMono = HWBufferInfo.lMaxSize;
|
2008-07-12 13:48:51 +02:00
|
|
|
}
|
|
|
|
else
|
2008-04-13 18:43:21 +02:00
|
|
|
{
|
2008-07-12 13:48:51 +02:00
|
|
|
// initialization
|
|
|
|
int iTrialBufSize = HWBufferInfo.lMinSize;
|
|
|
|
int iLastTrialBufSize = HWBufferInfo.lMinSize;
|
|
|
|
bool bSizeFound = false;
|
|
|
|
|
|
|
|
// test loop
|
|
|
|
while ( ( iTrialBufSize <= HWBufferInfo.lMaxSize ) && ( !bSizeFound ) )
|
2008-04-13 18:43:21 +02:00
|
|
|
{
|
2008-07-13 09:26:16 +02:00
|
|
|
if ( iTrialBufSize >= iBufferSizeMono )
|
2008-07-12 13:48:51 +02:00
|
|
|
{
|
|
|
|
// test which buffer size fits better: the old one or the
|
|
|
|
// current one
|
2008-07-13 09:26:16 +02:00
|
|
|
if ( ( iTrialBufSize - iBufferSizeMono ) <
|
|
|
|
( iBufferSizeMono - iLastTrialBufSize ) )
|
2008-07-12 13:48:51 +02:00
|
|
|
{
|
2008-07-13 01:33:27 +02:00
|
|
|
iBufferSizeMono = iTrialBufSize;
|
2008-07-12 13:48:51 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-13 01:33:27 +02:00
|
|
|
iBufferSizeMono = iLastTrialBufSize;
|
2008-07-12 13:48:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// exit while loop
|
|
|
|
bSizeFound = true;
|
|
|
|
}
|
|
|
|
|
2008-07-13 09:26:16 +02:00
|
|
|
if ( !bSizeFound )
|
2008-04-13 18:43:21 +02:00
|
|
|
{
|
2008-07-13 09:26:16 +02:00
|
|
|
// store old trial buffer size
|
|
|
|
iLastTrialBufSize = iTrialBufSize;
|
|
|
|
|
|
|
|
// increment trial buffer size (check for special case first)
|
|
|
|
if ( HWBufferInfo.lGranularity == -1 )
|
|
|
|
{
|
|
|
|
// special case: buffer sizes are a power of 2
|
|
|
|
iTrialBufSize *= 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
iTrialBufSize += HWBufferInfo.lGranularity;
|
|
|
|
}
|
2008-04-13 18:43:21 +02:00
|
|
|
}
|
|
|
|
}
|
2008-07-13 09:26:16 +02:00
|
|
|
|
|
|
|
// set ASIO buffer size
|
|
|
|
iASIOBufferSizeMono = iTrialBufSize;
|
2008-04-13 18:43:21 +02:00
|
|
|
}
|
|
|
|
}
|
2008-04-08 20:38:55 +02:00
|
|
|
|
2008-07-13 15:16:31 +02:00
|
|
|
// prepare input channels
|
|
|
|
for ( i = 0; i < NUM_IN_OUT_CHANNELS; i++ )
|
|
|
|
{
|
|
|
|
bufferInfos[i].isInput = ASIOTrue;
|
|
|
|
bufferInfos[i].channelNum = i;
|
|
|
|
bufferInfos[i].buffers[0] = 0;
|
|
|
|
bufferInfos[i].buffers[1] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepare output channels
|
|
|
|
for ( i = 0; i < NUM_IN_OUT_CHANNELS; i++ )
|
|
|
|
{
|
|
|
|
bufferInfos[NUM_IN_OUT_CHANNELS + i].isInput = ASIOFalse;
|
|
|
|
bufferInfos[NUM_IN_OUT_CHANNELS + i].channelNum = i;
|
|
|
|
bufferInfos[NUM_IN_OUT_CHANNELS + i].buffers[0] = 0;
|
|
|
|
bufferInfos[NUM_IN_OUT_CHANNELS + i].buffers[1] = 0;
|
|
|
|
}
|
2008-07-13 01:33:27 +02:00
|
|
|
|
|
|
|
// create and activate ASIO buffers (buffer size in samples)
|
2008-07-13 15:16:31 +02:00
|
|
|
ASIOCreateBuffers ( bufferInfos, 2 /* in/out */ * NUM_IN_OUT_CHANNELS /* stereo */,
|
2008-07-13 09:26:16 +02:00
|
|
|
iASIOBufferSizeMono, &asioCallbacks );
|
2008-04-08 20:38:55 +02:00
|
|
|
|
2008-07-13 15:16:31 +02:00
|
|
|
// now get some buffer details
|
2008-07-12 13:48:51 +02:00
|
|
|
for ( i = 0; i < 2 * NUM_IN_OUT_CHANNELS; i++ )
|
|
|
|
{
|
2008-07-13 15:16:31 +02:00
|
|
|
channelInfos[i].channel = bufferInfos[i].channelNum;
|
2008-07-12 13:48:51 +02:00
|
|
|
channelInfos[i].isInput = bufferInfos[i].isInput;
|
|
|
|
ASIOGetChannelInfo ( &channelInfos[i] );
|
2007-12-17 22:28:40 +01:00
|
|
|
|
2008-07-12 13:48:51 +02:00
|
|
|
// only 16 bit is supported
|
2008-07-24 20:15:48 +02:00
|
|
|
if ( ( channelInfos[i].type != ASIOSTInt16LSB ) &&
|
|
|
|
( channelInfos[i].type != ASIOSTInt24LSB ) &&
|
|
|
|
( channelInfos[i].type != ASIOSTInt32LSB ) )
|
2008-07-12 13:48:51 +02:00
|
|
|
{
|
2008-07-24 20:15:48 +02:00
|
|
|
throw CGenErr ( "Required audio sample format not available (16/24/32 bit LSB)." );
|
2008-07-12 13:48:51 +02:00
|
|
|
}
|
|
|
|
}
|
2007-12-17 22:28:40 +01:00
|
|
|
|
|
|
|
|
2008-07-14 00:57:31 +02:00
|
|
|
// Our buffer management -----------------------------------------------
|
|
|
|
// store new buffer number values
|
|
|
|
iCurNumSndBufIn = iNewNumSndBufIn;
|
|
|
|
iCurNumSndBufOut = iNewNumSndBufOut;
|
|
|
|
|
|
|
|
// initialize write block pointer in and overrun flag
|
2008-07-16 00:09:48 +02:00
|
|
|
iBufferPosCapture = 0;
|
|
|
|
bCaptureBufferOverrun = false;
|
2007-12-17 22:28:40 +01:00
|
|
|
|
2008-07-16 00:09:48 +02:00
|
|
|
// create memory for capture buffer
|
|
|
|
if ( psCaptureBuffer != NULL )
|
|
|
|
{
|
|
|
|
delete[] psCaptureBuffer;
|
|
|
|
}
|
|
|
|
psCaptureBuffer = new short[iCurNumSndBufIn * iBufferSizeStereo];
|
2007-12-17 22:28:40 +01:00
|
|
|
|
2008-07-14 00:57:31 +02:00
|
|
|
// initialize write block pointer out and underrun flag
|
2008-07-16 00:09:48 +02:00
|
|
|
iBufferPosPlay = 0;
|
|
|
|
bPlayBufferUnderrun = false;
|
2007-12-17 22:28:40 +01:00
|
|
|
|
2008-07-16 00:09:48 +02:00
|
|
|
// create memory for play buffer
|
|
|
|
if ( psPlayBuffer != NULL )
|
|
|
|
{
|
|
|
|
delete[] psPlayBuffer;
|
|
|
|
}
|
|
|
|
psPlayBuffer = new short[iCurNumSndBufOut * iBufferSizeStereo];
|
2008-07-15 19:16:41 +02:00
|
|
|
|
2008-07-16 00:09:48 +02:00
|
|
|
// clear new buffer
|
|
|
|
for ( i = 0; i < iCurNumSndBufOut * iBufferSizeStereo; i++ )
|
|
|
|
{
|
|
|
|
psPlayBuffer[i] = 0;
|
|
|
|
}
|
2008-07-15 19:16:41 +02:00
|
|
|
|
2008-07-12 13:48:51 +02:00
|
|
|
// reset event
|
|
|
|
ResetEvent ( m_ASIOEvent );
|
|
|
|
}
|
|
|
|
ASIOMutex.unlock();
|
2007-12-19 21:16:50 +01:00
|
|
|
|
|
|
|
// initialization is done, (re)start audio
|
|
|
|
ASIOStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSound::Close()
|
|
|
|
{
|
2007-12-17 22:28:40 +01:00
|
|
|
// set event to ensure that thread leaves the waiting function
|
2008-07-12 13:48:51 +02:00
|
|
|
if ( m_ASIOEvent != NULL )
|
2007-12-17 22:28:40 +01:00
|
|
|
{
|
2008-07-12 23:10:17 +02:00
|
|
|
SetEvent ( m_ASIOEvent );
|
2007-12-17 22:28:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// wait for the thread to terminate
|
|
|
|
Sleep ( 500 );
|
|
|
|
|
2008-07-13 01:33:27 +02:00
|
|
|
// stop audio and dispose ASIO buffers
|
|
|
|
ASIOStop();
|
|
|
|
ASIODisposeBuffers();
|
|
|
|
|
2007-12-17 22:28:40 +01:00
|
|
|
// set flag to open devices the next time it is initialized
|
2008-01-27 11:05:15 +01:00
|
|
|
bChangParamIn = true;
|
|
|
|
bChangParamOut = true;
|
2007-12-17 22:28:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CSound::CSound()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// init number of sound buffers
|
2008-07-14 00:57:31 +02:00
|
|
|
iNewNumSndBufIn = NUM_SOUND_BUFFERS_IN;
|
2007-12-17 22:28:40 +01:00
|
|
|
iCurNumSndBufIn = NUM_SOUND_BUFFERS_IN;
|
2008-07-14 00:57:31 +02:00
|
|
|
iNewNumSndBufOut = NUM_SOUND_BUFFERS_OUT;
|
2007-12-17 22:28:40 +01:00
|
|
|
iCurNumSndBufOut = NUM_SOUND_BUFFERS_OUT;
|
|
|
|
|
|
|
|
// should be initialized because an error can occur during init
|
2008-07-12 13:48:51 +02:00
|
|
|
m_ASIOEvent = NULL;
|
2007-12-17 22:28:40 +01:00
|
|
|
|
2007-12-18 21:52:48 +01:00
|
|
|
// get available ASIO driver names in system
|
|
|
|
char* cDriverNames[MAX_NUMBER_SOUND_CARDS];
|
|
|
|
for ( i = 0; i < MAX_NUMBER_SOUND_CARDS; i++ )
|
|
|
|
{
|
|
|
|
cDriverNames[i] = new char[32];
|
|
|
|
}
|
|
|
|
|
|
|
|
loadAsioDriver ( "dummy" ); // to initialize external object
|
2008-07-13 09:26:16 +02:00
|
|
|
const long lNumDetDriv =
|
|
|
|
asioDrivers->getDriverNames ( cDriverNames, MAX_NUMBER_SOUND_CARDS );
|
2007-12-18 21:52:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
// load and initialize first valid ASIO driver
|
|
|
|
bool bValidDriverDetected = false;
|
|
|
|
int iCurDriverIdx = 0;
|
|
|
|
|
|
|
|
while ( !bValidDriverDetected && iCurDriverIdx < lNumDetDriv )
|
|
|
|
{
|
|
|
|
if ( loadAsioDriver ( cDriverNames[iCurDriverIdx] ) )
|
|
|
|
{
|
|
|
|
if ( ASIOInit ( &driverInfo ) == ASE_OK )
|
|
|
|
{
|
|
|
|
bValidDriverDetected = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// driver could not be loaded, free memory
|
|
|
|
asioDrivers->removeCurrentDriver();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// try next driver
|
|
|
|
iCurDriverIdx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// in case we do not have a driver available, throw error
|
|
|
|
if ( !bValidDriverDetected )
|
|
|
|
{
|
|
|
|
throw CGenErr ( "No suitable ASIO audio device found." );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TEST we only use one driver for a first try
|
|
|
|
iNumDevs = 1;
|
|
|
|
pstrDevices[0] = driverInfo.name;
|
|
|
|
|
|
|
|
|
2007-12-19 21:16:50 +01:00
|
|
|
// check the number of available channels
|
|
|
|
long lNumInChan;
|
|
|
|
long lNumOutChan;
|
|
|
|
ASIOGetChannels ( &lNumInChan, &lNumOutChan );
|
2008-07-13 09:26:16 +02:00
|
|
|
if ( ( lNumInChan < NUM_IN_OUT_CHANNELS ) ||
|
|
|
|
( lNumOutChan < NUM_IN_OUT_CHANNELS ) )
|
2007-12-19 21:16:50 +01:00
|
|
|
{
|
2008-07-16 00:09:48 +02:00
|
|
|
throw CGenErr ( "The audio device does not support the "
|
|
|
|
"required number of channels." );
|
2007-12-19 21:16:50 +01:00
|
|
|
}
|
2007-12-18 21:52:48 +01:00
|
|
|
|
2008-04-13 18:43:21 +02:00
|
|
|
// query the usable buffer sizes
|
|
|
|
ASIOGetBufferSize ( &HWBufferInfo.lMinSize,
|
|
|
|
&HWBufferInfo.lMaxSize,
|
|
|
|
&HWBufferInfo.lPreferredSize,
|
|
|
|
&HWBufferInfo.lGranularity );
|
2007-12-17 22:28:40 +01:00
|
|
|
|
2007-12-19 21:16:50 +01:00
|
|
|
// set the sample rate and check if sample rate is supported
|
|
|
|
ASIOSetSampleRate ( SND_CRD_SAMPLE_RATE );
|
|
|
|
|
|
|
|
ASIOSampleRate sampleRate;
|
|
|
|
ASIOGetSampleRate ( &sampleRate );
|
|
|
|
if ( sampleRate != SND_CRD_SAMPLE_RATE )
|
2007-12-17 22:28:40 +01:00
|
|
|
{
|
2008-07-16 00:09:48 +02:00
|
|
|
throw CGenErr ( "The audio device does not support the "
|
|
|
|
"required sample rate." );
|
2007-12-19 21:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// check wether the driver requires the ASIOOutputReady() optimization
|
|
|
|
// (can be used by the driver to reduce output latency by one block)
|
|
|
|
bASIOPostOutput = ( ASIOOutputReady() == ASE_OK );
|
|
|
|
|
|
|
|
// set up the asioCallback structure and create the ASIO data buffer
|
2008-03-16 14:10:46 +01:00
|
|
|
asioCallbacks.bufferSwitch = &bufferSwitch;
|
|
|
|
asioCallbacks.sampleRateDidChange = &sampleRateChanged;
|
|
|
|
asioCallbacks.asioMessage = &asioMessages;
|
2007-12-19 21:16:50 +01:00
|
|
|
asioCallbacks.bufferSwitchTimeInfo = &bufferSwitchTimeInfo;
|
|
|
|
|
|
|
|
// init buffer pointer to zero
|
2008-07-16 00:09:48 +02:00
|
|
|
psCaptureBuffer = NULL;
|
|
|
|
psPlayBuffer = NULL;
|
2008-07-15 19:16:41 +02:00
|
|
|
|
2008-07-12 13:48:51 +02:00
|
|
|
// we use an event controlled structure
|
|
|
|
// create event
|
|
|
|
m_ASIOEvent = CreateEvent ( NULL, FALSE, FALSE, NULL );
|
2007-12-17 22:28:40 +01:00
|
|
|
|
2008-07-13 17:49:42 +02:00
|
|
|
// init flags
|
2008-07-12 23:10:17 +02:00
|
|
|
bChangParamIn = false;
|
|
|
|
bChangParamOut = false;
|
2007-12-17 22:28:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CSound::~CSound()
|
|
|
|
{
|
2007-12-19 21:16:50 +01:00
|
|
|
// cleanup ASIO stuff
|
|
|
|
ASIOStop();
|
|
|
|
ASIODisposeBuffers();
|
|
|
|
ASIOExit();
|
|
|
|
asioDrivers->removeCurrentDriver();
|
|
|
|
|
2007-12-17 22:28:40 +01:00
|
|
|
// delete allocated memory
|
2008-07-16 00:09:48 +02:00
|
|
|
if ( psCaptureBuffer != NULL )
|
|
|
|
{
|
|
|
|
delete[] psCaptureBuffer;
|
|
|
|
}
|
|
|
|
if ( psPlayBuffer != NULL )
|
|
|
|
{
|
|
|
|
delete[] psPlayBuffer;
|
|
|
|
}
|
2007-12-17 22:28:40 +01:00
|
|
|
|
2008-07-12 13:48:51 +02:00
|
|
|
// close the handle for the event
|
|
|
|
if ( m_ASIOEvent != NULL )
|
2007-12-17 22:28:40 +01:00
|
|
|
{
|
2008-07-12 13:48:51 +02:00
|
|
|
CloseHandle ( m_ASIOEvent );
|
2007-12-17 22:28:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-19 21:16:50 +01:00
|
|
|
// ASIO callbacks -------------------------------------------------------------
|
2008-07-13 09:26:16 +02:00
|
|
|
ASIOTime* CSound::bufferSwitchTimeInfo ( ASIOTime *timeInfo,
|
|
|
|
long index,
|
|
|
|
ASIOBool processNow )
|
2007-12-19 21:16:50 +01:00
|
|
|
{
|
2008-03-16 14:10:46 +01:00
|
|
|
bufferSwitch ( index, processNow );
|
|
|
|
return 0L;
|
|
|
|
}
|
2007-12-19 21:16:50 +01:00
|
|
|
|
2008-07-13 12:56:40 +02:00
|
|
|
void CSound::bufferSwitch ( long index, ASIOBool processNow )
|
2008-03-16 14:10:46 +01:00
|
|
|
{
|
2008-07-12 23:10:17 +02:00
|
|
|
int iCurSample;
|
|
|
|
|
2008-07-12 13:48:51 +02:00
|
|
|
ASIOMutex.lock(); // get mutex lock
|
|
|
|
{
|
2008-07-16 00:09:48 +02:00
|
|
|
// first check buffer state of capture and play buffers
|
|
|
|
const int iCaptureBufferLen = iCurNumSndBufIn * iBufferSizeStereo;
|
2008-07-15 19:16:41 +02:00
|
|
|
|
2008-07-16 00:09:48 +02:00
|
|
|
bCaptureBufferOverrun =
|
|
|
|
( iBufferPosCapture + 2 * iASIOBufferSizeMono > iCaptureBufferLen );
|
2008-07-15 19:16:41 +02:00
|
|
|
|
2008-07-16 00:09:48 +02:00
|
|
|
bPlayBufferUnderrun = ( 2 * iASIOBufferSizeMono > iBufferPosPlay );
|
2008-07-15 19:16:41 +02:00
|
|
|
|
2008-07-16 00:09:48 +02:00
|
|
|
// perform the processing for input and output
|
2008-07-13 12:56:40 +02:00
|
|
|
for ( int i = 0; i < 2 * NUM_IN_OUT_CHANNELS; i++ ) // stereo
|
2008-07-12 13:48:51 +02:00
|
|
|
{
|
2008-07-16 00:09:48 +02:00
|
|
|
if ( bufferInfos[i].isInput == ASIOTrue )
|
2008-07-12 13:48:51 +02:00
|
|
|
{
|
2008-07-13 01:33:27 +02:00
|
|
|
// CAPTURE -----------------------------------------------------
|
2008-07-13 12:56:40 +02:00
|
|
|
// first check if space in buffer is available
|
2008-07-16 00:09:48 +02:00
|
|
|
if ( !bCaptureBufferOverrun )
|
2008-07-12 13:48:51 +02:00
|
|
|
{
|
2008-07-24 20:15:48 +02:00
|
|
|
// copy new captured block in thread transfer buffer (copy
|
|
|
|
// mono data interleaved in stereo buffer)
|
|
|
|
switch ( channelInfos[i].type )
|
|
|
|
{
|
|
|
|
case ASIOSTInt16LSB:
|
|
|
|
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
|
|
|
|
{
|
|
|
|
psCaptureBuffer[iBufferPosCapture +
|
|
|
|
2 * iCurSample + bufferInfos[i].channelNum] =
|
|
|
|
((short*) bufferInfos[i].buffers[index])[iCurSample];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ASIOSTInt24LSB:
|
|
|
|
|
|
|
|
// not yet tested, horrible things might happen with the following code ;-)
|
|
|
|
|
|
|
|
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
|
|
|
|
{
|
|
|
|
// convert current sample in 16 bit format
|
|
|
|
int iCurSam = 0;
|
|
|
|
memcpy ( &iCurSam, ((char*) bufferInfos[i].buffers[index]) + iCurSample * 3, 3);
|
|
|
|
iCurSam >>= 8;
|
|
|
|
|
|
|
|
psCaptureBuffer[iBufferPosCapture +
|
|
|
|
2 * iCurSample + bufferInfos[i].channelNum] = static_cast<short> ( iCurSam );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ASIOSTInt32LSB:
|
|
|
|
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
|
|
|
|
{
|
|
|
|
// convert to 16 bit
|
|
|
|
psCaptureBuffer[iBufferPosCapture +
|
|
|
|
2 * iCurSample + bufferInfos[i].channelNum] =
|
|
|
|
(((int*) bufferInfos[i].buffers[index])[iCurSample] >> 16);
|
|
|
|
}
|
|
|
|
break;
|
2008-07-12 13:48:51 +02:00
|
|
|
}
|
|
|
|
}
|
2008-07-16 00:09:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// PLAYBACK ----------------------------------------------------
|
|
|
|
if ( !bPlayBufferUnderrun )
|
|
|
|
{
|
2008-07-24 20:15:48 +02:00
|
|
|
// copy data from sound card in output buffer (copy
|
|
|
|
// interleaved stereo data in mono sound card buffer)
|
|
|
|
switch ( channelInfos[i].type )
|
|
|
|
{
|
|
|
|
case ASIOSTInt16LSB:
|
|
|
|
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
|
|
|
|
{
|
|
|
|
((short*) bufferInfos[i].buffers[index])[iCurSample] =
|
|
|
|
psPlayBuffer[2 * iCurSample + bufferInfos[i].channelNum];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ASIOSTInt24LSB:
|
|
|
|
|
|
|
|
// not yet tested, horrible things might happen with the following code ;-)
|
|
|
|
|
|
|
|
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
|
|
|
|
{
|
|
|
|
// convert current sample in 24 bit format
|
|
|
|
int iCurSam = static_cast<int> ( psPlayBuffer[2 * iCurSample + bufferInfos[i].channelNum] );
|
|
|
|
iCurSam <<= 8;
|
|
|
|
|
|
|
|
memcpy ( ((char*) bufferInfos[i].buffers[index]) + iCurSample * 3, &iCurSam, 3);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ASIOSTInt32LSB:
|
|
|
|
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
|
|
|
|
{
|
|
|
|
// convert to 32 bit
|
|
|
|
int iCurSam = static_cast<int> ( psPlayBuffer[2 * iCurSample + bufferInfos[i].channelNum] );
|
|
|
|
((int*) bufferInfos[i].buffers[index])[iCurSample] = ( iCurSam << 16 );
|
|
|
|
}
|
|
|
|
break;
|
2008-07-16 00:09:48 +02:00
|
|
|
}
|
|
|
|
}
|
2008-07-13 01:33:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Manage thread interface buffers for input and output ----------------
|
2008-07-16 00:09:48 +02:00
|
|
|
// capture
|
|
|
|
if ( !bCaptureBufferOverrun )
|
|
|
|
{
|
|
|
|
iBufferPosCapture += 2 * iASIOBufferSizeMono;
|
|
|
|
}
|
|
|
|
|
|
|
|
// play
|
|
|
|
if ( !bPlayBufferUnderrun )
|
2008-07-13 01:33:27 +02:00
|
|
|
{
|
2008-07-16 00:09:48 +02:00
|
|
|
// move all other data in play buffer
|
|
|
|
const int iLenCopyRegion = iBufferPosPlay - 2 * iASIOBufferSizeMono;
|
|
|
|
for ( iCurSample = 0; iCurSample < iLenCopyRegion; iCurSample++ )
|
2008-07-13 01:33:27 +02:00
|
|
|
{
|
2008-07-16 00:09:48 +02:00
|
|
|
psPlayBuffer[iCurSample] =
|
|
|
|
psPlayBuffer[2 * iASIOBufferSizeMono + iCurSample];
|
2008-07-12 13:48:51 +02:00
|
|
|
}
|
2008-07-13 01:33:27 +02:00
|
|
|
|
|
|
|
// adjust "current block to write" pointer
|
2008-07-16 00:09:48 +02:00
|
|
|
iBufferPosPlay -= 2 * iASIOBufferSizeMono;
|
2008-07-13 01:33:27 +02:00
|
|
|
}
|
2008-07-13 15:16:31 +02:00
|
|
|
|
2008-07-12 23:10:17 +02:00
|
|
|
|
2008-07-13 22:03:37 +02:00
|
|
|
// finally if the driver supports the ASIOOutputReady() optimization,
|
2008-07-16 00:09:48 +02:00
|
|
|
// do it here, all data are in place -----------------------------------
|
2008-07-13 22:03:37 +02:00
|
|
|
if ( bASIOPostOutput )
|
|
|
|
{
|
|
|
|
ASIOOutputReady();
|
|
|
|
}
|
2008-07-12 23:10:17 +02:00
|
|
|
|
2008-07-13 22:03:37 +02:00
|
|
|
// set event
|
|
|
|
SetEvent ( m_ASIOEvent );
|
|
|
|
}
|
|
|
|
ASIOMutex.unlock();
|
2007-12-19 21:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
long CSound::asioMessages ( long selector, long value, void* message, double* opt )
|
|
|
|
{
|
|
|
|
long ret = 0;
|
|
|
|
switch(selector)
|
|
|
|
{
|
|
|
|
case kAsioEngineVersion:
|
|
|
|
// return the supported ASIO version of the host application
|
|
|
|
ret = 2L;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-12-17 22:28:40 +01:00
|
|
|
#else // USE_ASIO_SND_INTERFACE
|
|
|
|
|
2006-12-18 15:39:33 +01:00
|
|
|
/******************************************************************************\
|
|
|
|
* Wave in *
|
|
|
|
\******************************************************************************/
|
2007-09-08 12:45:14 +02:00
|
|
|
bool CSound::Read ( CVector<short>& psData )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2008-01-27 11:05:15 +01:00
|
|
|
int i;
|
|
|
|
bool bError;
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// check if device must be opened or reinitialized
|
2008-01-27 11:05:15 +01:00
|
|
|
if ( bChangParamIn )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
|
|
|
OpenInDevice();
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// Reinit sound interface
|
|
|
|
InitRecording ( iBufferSizeIn, bBlockingRec );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// Reset flag
|
2008-01-27 11:05:15 +01:00
|
|
|
bChangParamIn = false;
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// wait until data is available
|
|
|
|
if ( ! ( m_WaveInHeader[iWhichBufferIn].dwFlags & WHDR_DONE ) )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2008-01-27 11:05:15 +01:00
|
|
|
if ( bBlockingRec )
|
2007-09-08 12:45:14 +02:00
|
|
|
{
|
|
|
|
WaitForSingleObject ( m_WaveInEvent, INFINITE );
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
else
|
2007-09-08 12:45:14 +02:00
|
|
|
{
|
2008-01-27 11:05:15 +01:00
|
|
|
return false;
|
2007-09-08 12:45:14 +02:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// check if buffers got lost
|
2006-12-18 15:39:33 +01:00
|
|
|
int iNumInBufDone = 0;
|
2007-09-08 12:45:14 +02:00
|
|
|
for ( i = 0; i < iCurNumSndBufIn; i++ )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
if ( m_WaveInHeader[i].dwFlags & WHDR_DONE )
|
|
|
|
{
|
2006-12-18 15:39:33 +01:00
|
|
|
iNumInBufDone++;
|
2007-09-08 12:45:14 +02:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the number of done buffers equals the total number of buffers, it is
|
|
|
|
very likely that a buffer got lost -> set error flag */
|
2007-09-08 12:45:14 +02:00
|
|
|
if ( iNumInBufDone == iCurNumSndBufIn )
|
|
|
|
{
|
2008-01-27 11:05:15 +01:00
|
|
|
bError = true;
|
2007-09-08 12:45:14 +02:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
else
|
2007-09-08 12:45:14 +02:00
|
|
|
{
|
2008-01-27 11:05:15 +01:00
|
|
|
bError = false;
|
2007-09-08 12:45:14 +02:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// copy data from sound card in output buffer
|
|
|
|
for ( i = 0; i < iBufferSizeIn; i++ )
|
|
|
|
{
|
2006-12-18 15:39:33 +01:00
|
|
|
psData[i] = psSoundcardBuffer[iWhichBufferIn][i];
|
2007-09-08 12:45:14 +02:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// add the buffer so that it can be filled with new samples
|
2006-12-18 15:39:33 +01:00
|
|
|
AddInBuffer();
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// in case more than one buffer was ready, reset event
|
|
|
|
ResetEvent ( m_WaveInEvent );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
return bError;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSound::AddInBuffer()
|
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
// unprepare old wave-header
|
|
|
|
waveInUnprepareHeader (
|
|
|
|
m_WaveIn, &m_WaveInHeader[iWhichBufferIn], sizeof ( WAVEHDR ) );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// prepare buffers for sending to sound interface
|
|
|
|
PrepareInBuffer ( iWhichBufferIn );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// send buffer to driver for filling with new data
|
|
|
|
waveInAddBuffer ( m_WaveIn, &m_WaveInHeader[iWhichBufferIn], sizeof ( WAVEHDR ) );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// toggle buffers
|
2006-12-18 15:39:33 +01:00
|
|
|
iWhichBufferIn++;
|
2007-09-08 12:45:14 +02:00
|
|
|
if ( iWhichBufferIn == iCurNumSndBufIn )
|
|
|
|
{
|
2006-12-18 15:39:33 +01:00
|
|
|
iWhichBufferIn = 0;
|
2007-09-08 12:45:14 +02:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
void CSound::PrepareInBuffer ( int iBufNum )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
// set struct entries
|
|
|
|
m_WaveInHeader[iBufNum].lpData = (LPSTR) &psSoundcardBuffer[iBufNum][0];
|
2006-12-18 15:39:33 +01:00
|
|
|
m_WaveInHeader[iBufNum].dwBufferLength = iBufferSizeIn * BYTES_PER_SAMPLE;
|
2007-09-08 12:45:14 +02:00
|
|
|
m_WaveInHeader[iBufNum].dwFlags = 0;
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// prepare wave-header
|
|
|
|
waveInPrepareHeader ( m_WaveIn, &m_WaveInHeader[iBufNum], sizeof ( WAVEHDR ) );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
void CSound::InitRecording ( int iNewBufferSize, bool bNewBlocking )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
// check if device must be opened or reinitialized
|
2008-01-27 11:05:15 +01:00
|
|
|
if ( bChangParamIn )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
|
|
|
OpenInDevice();
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// reset flag
|
2008-01-27 11:05:15 +01:00
|
|
|
bChangParamIn = false;
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// set internal parameter
|
2006-12-18 15:39:33 +01:00
|
|
|
iBufferSizeIn = iNewBufferSize;
|
2008-04-08 20:38:55 +02:00
|
|
|
bBlockingRec = bNewBlocking;
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// reset interface so that all buffers are returned from the interface
|
|
|
|
waveInReset ( m_WaveIn );
|
|
|
|
waveInStop ( m_WaveIn );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
/* reset current buffer ID (it is important to do this BEFORE calling
|
2006-12-18 15:39:33 +01:00
|
|
|
"AddInBuffer()" */
|
|
|
|
iWhichBufferIn = 0;
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// create memory for sound card buffer
|
|
|
|
for ( int i = 0; i < iCurNumSndBufIn; i++ )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
|
|
|
/* Unprepare old wave-header in case that we "re-initialized" this
|
|
|
|
module. Calling "waveInUnprepareHeader()" with an unprepared
|
|
|
|
buffer (when the module is initialized for the first time) has
|
|
|
|
simply no effect */
|
2007-09-08 12:45:14 +02:00
|
|
|
waveInUnprepareHeader ( m_WaveIn, &m_WaveInHeader[i], sizeof ( WAVEHDR ) );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
if ( psSoundcardBuffer[i] != NULL )
|
|
|
|
{
|
2006-12-18 15:39:33 +01:00
|
|
|
delete[] psSoundcardBuffer[i];
|
2007-09-08 12:45:14 +02:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
psSoundcardBuffer[i] = new short[iBufferSizeIn];
|
|
|
|
|
|
|
|
|
2008-01-22 20:58:53 +01:00
|
|
|
// Send all buffers to driver for filling the queue --------------------
|
2007-09-08 12:45:14 +02:00
|
|
|
// prepare buffers before sending them to the sound interface
|
|
|
|
PrepareInBuffer ( i );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
AddInBuffer();
|
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// notify that sound capturing can start now
|
|
|
|
waveInStart ( m_WaveIn );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
/* This reset event is very important for initialization, otherwise we will
|
|
|
|
get errors! */
|
2007-09-08 12:45:14 +02:00
|
|
|
ResetEvent ( m_WaveInEvent );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSound::OpenInDevice()
|
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
// open wave-input and set call-back mechanism to event handle
|
|
|
|
if ( m_WaveIn != NULL )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
waveInReset ( m_WaveIn );
|
|
|
|
waveInClose ( m_WaveIn );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
MMRESULT result = waveInOpen ( &m_WaveIn, iCurInDev, &sWaveFormatEx,
|
|
|
|
(DWORD) m_WaveInEvent, NULL, CALLBACK_EVENT );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
if ( result != MMSYSERR_NOERROR )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
throw CGenErr ( "Sound Interface Start, waveInOpen() failed. This error "
|
|
|
|
"usually occurs if another application blocks the sound in." );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
void CSound::SetInDev ( int iNewDev )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
// set device to wave mapper if iNewDev is invalid
|
|
|
|
if ( ( iNewDev >= iNumDevs ) || ( iNewDev < 0 ) )
|
|
|
|
{
|
2006-12-18 15:39:33 +01:00
|
|
|
iNewDev = WAVE_MAPPER;
|
2007-09-08 12:45:14 +02:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// change only in case new device id is not already active
|
|
|
|
if ( iNewDev != iCurInDev )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
iCurInDev = iNewDev;
|
2008-01-27 11:05:15 +01:00
|
|
|
bChangParamIn = true;
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
void CSound::SetInNumBuf ( int iNewNum )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
// check new parameter
|
|
|
|
if ( ( iNewNum >= MAX_SND_BUF_IN ) || ( iNewNum < 1 ) )
|
|
|
|
{
|
2006-12-18 15:39:33 +01:00
|
|
|
iNewNum = NUM_SOUND_BUFFERS_IN;
|
2007-09-08 12:45:14 +02:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// change only if parameter is different
|
|
|
|
if ( iNewNum != iCurNumSndBufIn )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
|
|
|
iCurNumSndBufIn = iNewNum;
|
2008-01-27 11:05:15 +01:00
|
|
|
bChangParamIn = true;
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************\
|
|
|
|
* Wave out *
|
|
|
|
\******************************************************************************/
|
2007-09-08 12:45:14 +02:00
|
|
|
bool CSound::Write ( CVector<short>& psData )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
int iCntPrepBuf;
|
|
|
|
int iIndexDoneBuf;
|
|
|
|
bool bError;
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// check if device must be opened or reinitialized
|
2008-01-27 11:05:15 +01:00
|
|
|
if ( bChangParamOut )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
|
|
|
OpenOutDevice();
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// reinit sound interface
|
|
|
|
InitPlayback ( iBufferSizeOut, bBlockingPlay );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// reset flag
|
2008-01-27 11:05:15 +01:00
|
|
|
bChangParamOut = false;
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// get number of "done"-buffers and position of one of them
|
|
|
|
GetDoneBuffer ( iCntPrepBuf, iIndexDoneBuf );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// now check special cases (Buffer is full or empty)
|
|
|
|
if ( iCntPrepBuf == 0 )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2008-01-27 11:05:15 +01:00
|
|
|
if ( bBlockingPlay )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2008-01-27 11:05:15 +01:00
|
|
|
/* Blocking wave out routine. Always
|
2006-12-18 15:39:33 +01:00
|
|
|
ensure that the buffer is completely filled to avoid buffer
|
|
|
|
underruns */
|
2007-09-08 12:45:14 +02:00
|
|
|
while ( iCntPrepBuf == 0 )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
WaitForSingleObject ( m_WaveOutEvent, INFINITE );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
GetDoneBuffer ( iCntPrepBuf, iIndexDoneBuf );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-01-22 20:58:53 +01:00
|
|
|
// All buffers are filled, dump new block --------------------------
|
2006-12-18 15:39:33 +01:00
|
|
|
// It would be better to kill half of the buffer blocks to set the start
|
|
|
|
// back to the middle: TODO
|
2008-01-27 11:05:15 +01:00
|
|
|
return true; // an error occurred
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2007-09-08 12:45:14 +02:00
|
|
|
{
|
|
|
|
if ( iCntPrepBuf == iCurNumSndBufOut )
|
|
|
|
{
|
|
|
|
/* -----------------------------------------------------------------
|
|
|
|
Buffer is empty -> send as many cleared blocks to the sound-
|
|
|
|
interface until half of the buffer size is reached */
|
|
|
|
// send half of the buffer size blocks to the sound-interface
|
|
|
|
for ( j = 0; j < iCurNumSndBufOut / 2; j++ )
|
|
|
|
{
|
|
|
|
// first, clear these buffers
|
|
|
|
for ( i = 0; i < iBufferSizeOut; i++ )
|
|
|
|
{
|
|
|
|
psPlaybackBuffer[j][i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// then send them to the interface
|
|
|
|
AddOutBuffer ( j );
|
|
|
|
}
|
|
|
|
|
|
|
|
// set index for done buffer
|
|
|
|
iIndexDoneBuf = iCurNumSndBufOut / 2;
|
|
|
|
|
2008-01-27 11:05:15 +01:00
|
|
|
bError = true;
|
2007-09-08 12:45:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-01-27 11:05:15 +01:00
|
|
|
bError = false;
|
2007-09-08 12:45:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy stereo data from input in soundcard buffer
|
|
|
|
for ( i = 0; i < iBufferSizeOut; i++ )
|
|
|
|
{
|
2006-12-18 15:39:33 +01:00
|
|
|
psPlaybackBuffer[iIndexDoneBuf][i] = psData[i];
|
2007-09-08 12:45:14 +02:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// now, send the current block
|
|
|
|
AddOutBuffer ( iIndexDoneBuf );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
return bError;
|
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
void CSound::GetDoneBuffer ( int& iCntPrepBuf, int& iIndexDoneBuf )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
// get number of "done"-buffers and position of one of them
|
2006-12-18 15:39:33 +01:00
|
|
|
iCntPrepBuf = 0;
|
2007-09-08 12:45:14 +02:00
|
|
|
for ( int i = 0; i < iCurNumSndBufOut; i++ )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
if ( m_WaveOutHeader[i].dwFlags & WHDR_DONE )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
|
|
|
iCntPrepBuf++;
|
|
|
|
iIndexDoneBuf = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
void CSound::AddOutBuffer ( int iBufNum )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2008-01-22 20:58:53 +01:00
|
|
|
// unprepare old wave-header
|
2007-09-08 12:45:14 +02:00
|
|
|
waveOutUnprepareHeader (
|
|
|
|
m_WaveOut, &m_WaveOutHeader[iBufNum], sizeof ( WAVEHDR ) );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2008-01-22 20:58:53 +01:00
|
|
|
// prepare buffers for sending to sound interface
|
2007-09-08 12:45:14 +02:00
|
|
|
PrepareOutBuffer ( iBufNum );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2008-01-22 20:58:53 +01:00
|
|
|
// send buffer to driver for filling with new data
|
2007-09-08 12:45:14 +02:00
|
|
|
waveOutWrite ( m_WaveOut, &m_WaveOutHeader[iBufNum], sizeof ( WAVEHDR ) );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
void CSound::PrepareOutBuffer ( int iBufNum )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2008-01-22 20:58:53 +01:00
|
|
|
// set Header data
|
2007-09-08 12:45:14 +02:00
|
|
|
m_WaveOutHeader[iBufNum].lpData = (LPSTR) &psPlaybackBuffer[iBufNum][0];
|
2006-12-18 15:39:33 +01:00
|
|
|
m_WaveOutHeader[iBufNum].dwBufferLength = iBufferSizeOut * BYTES_PER_SAMPLE;
|
2007-09-08 12:45:14 +02:00
|
|
|
m_WaveOutHeader[iBufNum].dwFlags = 0;
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2008-01-22 20:58:53 +01:00
|
|
|
// prepare wave-header
|
2007-09-08 12:45:14 +02:00
|
|
|
waveOutPrepareHeader ( m_WaveOut, &m_WaveOutHeader[iBufNum], sizeof ( WAVEHDR ) );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
void CSound::InitPlayback ( int iNewBufferSize, bool bNewBlocking )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// check if device must be opened or reinitialized
|
2008-01-27 11:05:15 +01:00
|
|
|
if ( bChangParamOut )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
|
|
|
OpenOutDevice();
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// reset flag
|
2008-01-27 11:05:15 +01:00
|
|
|
bChangParamOut = false;
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// set internal parameters
|
2006-12-18 15:39:33 +01:00
|
|
|
iBufferSizeOut = iNewBufferSize;
|
2007-09-08 12:45:14 +02:00
|
|
|
bBlockingPlay = bNewBlocking;
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// reset interface
|
|
|
|
waveOutReset ( m_WaveOut );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
for ( j = 0; j < iCurNumSndBufOut; j++ )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
|
|
|
/* Unprepare old wave-header (in case header was not prepared before,
|
|
|
|
simply nothing happens with this function call */
|
2007-09-08 12:45:14 +02:00
|
|
|
waveOutUnprepareHeader ( m_WaveOut, &m_WaveOutHeader[j], sizeof ( WAVEHDR ) );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// create memory for playback buffer
|
|
|
|
if ( psPlaybackBuffer[j] != NULL )
|
|
|
|
{
|
2006-12-18 15:39:33 +01:00
|
|
|
delete[] psPlaybackBuffer[j];
|
2007-09-08 12:45:14 +02:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
psPlaybackBuffer[j] = new short[iBufferSizeOut];
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// clear new buffer
|
|
|
|
for ( i = 0; i < iBufferSizeOut; i++ )
|
|
|
|
{
|
2006-12-18 15:39:33 +01:00
|
|
|
psPlaybackBuffer[j][i] = 0;
|
2007-09-08 12:45:14 +02:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// prepare buffer for sending to the sound interface
|
|
|
|
PrepareOutBuffer ( j );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// initially, send all buffers to the interface
|
|
|
|
AddOutBuffer ( j );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSound::OpenOutDevice()
|
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
if ( m_WaveOut != NULL )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
waveOutReset ( m_WaveOut );
|
|
|
|
waveOutClose ( m_WaveOut );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
MMRESULT result = waveOutOpen ( &m_WaveOut, iCurOutDev, &sWaveFormatEx,
|
|
|
|
(DWORD) m_WaveOutEvent, NULL, CALLBACK_EVENT );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
if ( result != MMSYSERR_NOERROR )
|
|
|
|
{
|
|
|
|
throw CGenErr ( "Sound Interface Start, waveOutOpen() failed." );
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
void CSound::SetOutDev ( int iNewDev )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
// set device to wave mapper if iNewDev is invalid
|
|
|
|
if ( ( iNewDev >= iNumDevs ) || ( iNewDev < 0 ) )
|
|
|
|
{
|
2006-12-18 15:39:33 +01:00
|
|
|
iNewDev = WAVE_MAPPER;
|
2007-09-08 12:45:14 +02:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// change only in case new device id is not already active
|
|
|
|
if ( iNewDev != iCurOutDev )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
iCurOutDev = iNewDev;
|
2008-01-27 11:05:15 +01:00
|
|
|
bChangParamOut = true;
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
void CSound::SetOutNumBuf ( int iNewNum )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
// check new parameter
|
|
|
|
if ( ( iNewNum >= MAX_SND_BUF_OUT ) || ( iNewNum < 1 ) )
|
|
|
|
{
|
2006-12-18 15:39:33 +01:00
|
|
|
iNewNum = NUM_SOUND_BUFFERS_OUT;
|
2007-09-08 12:45:14 +02:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// change only if parameter is different
|
|
|
|
if ( iNewNum != iCurNumSndBufOut )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
|
|
|
iCurNumSndBufOut = iNewNum;
|
2008-01-27 11:05:15 +01:00
|
|
|
bChangParamOut = true;
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************\
|
|
|
|
* Common *
|
|
|
|
\******************************************************************************/
|
|
|
|
void CSound::Close()
|
|
|
|
{
|
2008-01-27 11:05:15 +01:00
|
|
|
int i;
|
|
|
|
MMRESULT result;
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// reset audio driver
|
|
|
|
if ( m_WaveOut != NULL )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
result = waveOutReset ( m_WaveOut );
|
|
|
|
|
|
|
|
if ( result != MMSYSERR_NOERROR )
|
|
|
|
{
|
|
|
|
throw CGenErr ( "Sound Interface, waveOutReset() failed." );
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
if ( m_WaveIn != NULL )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
result = waveInReset ( m_WaveIn );
|
|
|
|
|
|
|
|
if ( result != MMSYSERR_NOERROR )
|
|
|
|
{
|
|
|
|
throw CGenErr ( "Sound Interface, waveInReset() failed." );
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// set event to ensure that thread leaves the waiting function
|
|
|
|
if ( m_WaveInEvent != NULL )
|
|
|
|
{
|
2006-12-18 15:39:33 +01:00
|
|
|
SetEvent(m_WaveInEvent);
|
2007-09-08 12:45:14 +02:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// wait for the thread to terminate
|
|
|
|
Sleep ( 500 );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// unprepare wave-headers
|
|
|
|
if ( m_WaveIn != NULL )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
for ( i = 0; i < iCurNumSndBufIn; i++ )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
result = waveInUnprepareHeader (
|
|
|
|
m_WaveIn, &m_WaveInHeader[i], sizeof ( WAVEHDR ) );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
if ( result != MMSYSERR_NOERROR )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
throw CGenErr ( "Sound Interface, waveInUnprepareHeader()"
|
|
|
|
" failed." );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// close the sound in device
|
|
|
|
result = waveInClose ( m_WaveIn );
|
|
|
|
if ( result != MMSYSERR_NOERROR )
|
|
|
|
{
|
|
|
|
throw CGenErr ( "Sound Interface, waveInClose() failed." );
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
if ( m_WaveOut != NULL )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
for ( i = 0; i < iCurNumSndBufOut; i++ )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
result = waveOutUnprepareHeader (
|
|
|
|
m_WaveOut, &m_WaveOutHeader[i], sizeof ( WAVEHDR ) );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
if ( result != MMSYSERR_NOERROR )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
throw CGenErr ( "Sound Interface, waveOutUnprepareHeader()"
|
|
|
|
" failed." );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// close the sound out device
|
|
|
|
result = waveOutClose ( m_WaveOut );
|
|
|
|
if ( result != MMSYSERR_NOERROR )
|
|
|
|
{
|
|
|
|
throw CGenErr ( "Sound Interface, waveOutClose() failed." );
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// set flag to open devices the next time it is initialized
|
2008-01-27 11:05:15 +01:00
|
|
|
bChangParamIn = true;
|
|
|
|
bChangParamOut = true;
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CSound::CSound()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// init number of sound buffers
|
|
|
|
iCurNumSndBufIn = NUM_SOUND_BUFFERS_IN;
|
2006-12-18 15:39:33 +01:00
|
|
|
iCurNumSndBufOut = NUM_SOUND_BUFFERS_OUT;
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// should be initialized because an error can occur during init
|
|
|
|
m_WaveInEvent = NULL;
|
2006-12-18 15:39:33 +01:00
|
|
|
m_WaveOutEvent = NULL;
|
2007-09-08 12:45:14 +02:00
|
|
|
m_WaveIn = NULL;
|
|
|
|
m_WaveOut = NULL;
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// init buffer pointer to zero
|
|
|
|
for ( i = 0; i < MAX_SND_BUF_IN; i++ )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
memset ( &m_WaveInHeader[i], 0, sizeof ( WAVEHDR ) );
|
2006-12-18 15:39:33 +01:00
|
|
|
psSoundcardBuffer[i] = NULL;
|
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
for ( i = 0; i < MAX_SND_BUF_OUT; i++ )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
memset ( &m_WaveOutHeader[i], 0, sizeof ( WAVEHDR ) );
|
2006-12-18 15:39:33 +01:00
|
|
|
psPlaybackBuffer[i] = NULL;
|
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// init wave-format structure
|
2008-01-27 11:05:15 +01:00
|
|
|
sWaveFormatEx.wFormatTag = WAVE_FORMAT_PCM;
|
|
|
|
sWaveFormatEx.nChannels = NUM_IN_OUT_CHANNELS;
|
|
|
|
sWaveFormatEx.wBitsPerSample = BITS_PER_SAMPLE;
|
|
|
|
sWaveFormatEx.nSamplesPerSec = SND_CRD_SAMPLE_RATE;
|
|
|
|
sWaveFormatEx.nBlockAlign = sWaveFormatEx.nChannels *
|
2006-12-18 15:39:33 +01:00
|
|
|
sWaveFormatEx.wBitsPerSample / 8;
|
|
|
|
sWaveFormatEx.nAvgBytesPerSec = sWaveFormatEx.nBlockAlign *
|
|
|
|
sWaveFormatEx.nSamplesPerSec;
|
2008-01-27 11:05:15 +01:00
|
|
|
sWaveFormatEx.cbSize = 0;
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// get the number of digital audio devices in this computer, check range
|
2006-12-18 15:39:33 +01:00
|
|
|
iNumDevs = waveInGetNumDevs();
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
if ( iNumDevs > MAX_NUMBER_SOUND_CARDS )
|
|
|
|
{
|
2006-12-18 15:39:33 +01:00
|
|
|
iNumDevs = MAX_NUMBER_SOUND_CARDS;
|
2007-09-08 12:45:14 +02:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// at least one device must exist in the system
|
|
|
|
if ( iNumDevs == 0 )
|
|
|
|
{
|
|
|
|
throw CGenErr ( "No audio device found." );
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// get info about the devices and store the names
|
|
|
|
for ( i = 0; i < iNumDevs; i++ )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
if ( !waveInGetDevCaps ( i, &m_WaveInDevCaps, sizeof ( WAVEINCAPS ) ) )
|
|
|
|
{
|
2006-12-18 15:39:33 +01:00
|
|
|
pstrDevices[i] = m_WaveInDevCaps.szPname;
|
2007-09-08 12:45:14 +02:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// we use an event controlled wave-in (wave-out) structure
|
|
|
|
// create events
|
2008-01-27 11:05:15 +01:00
|
|
|
m_WaveInEvent = CreateEvent ( NULL, FALSE, FALSE, NULL );
|
2007-09-08 12:45:14 +02:00
|
|
|
m_WaveOutEvent = CreateEvent ( NULL, FALSE, FALSE, NULL );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// set flag to open devices
|
2008-01-27 11:05:15 +01:00
|
|
|
bChangParamIn = true;
|
|
|
|
bChangParamOut = true;
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// default device number, "wave mapper"
|
|
|
|
iCurInDev = WAVE_MAPPER;
|
2006-12-18 15:39:33 +01:00
|
|
|
iCurOutDev = WAVE_MAPPER;
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// non-blocking wave out is default
|
2008-01-27 11:05:15 +01:00
|
|
|
bBlockingPlay = false;
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// blocking wave in is default
|
2008-01-27 11:05:15 +01:00
|
|
|
bBlockingRec = true;
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CSound::~CSound()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// delete allocated memory
|
|
|
|
for ( i = 0; i < iCurNumSndBufIn; i++ )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
if ( psSoundcardBuffer[i] != NULL )
|
|
|
|
{
|
2006-12-18 15:39:33 +01:00
|
|
|
delete[] psSoundcardBuffer[i];
|
2007-09-08 12:45:14 +02:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
for ( i = 0; i < iCurNumSndBufOut; i++ )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-09-08 12:45:14 +02:00
|
|
|
if ( psPlaybackBuffer[i] != NULL )
|
|
|
|
{
|
2006-12-18 15:39:33 +01:00
|
|
|
delete[] psPlaybackBuffer[i];
|
2007-09-08 12:45:14 +02:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
// close the handle for the events
|
|
|
|
if ( m_WaveInEvent != NULL )
|
|
|
|
{
|
|
|
|
CloseHandle ( m_WaveInEvent );
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-09-08 12:45:14 +02:00
|
|
|
if ( m_WaveOutEvent != NULL )
|
|
|
|
{
|
|
|
|
CloseHandle ( m_WaveOutEvent );
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
2007-12-17 22:28:40 +01:00
|
|
|
|
|
|
|
#endif // USE_ASIO_SND_INTERFACE
|