2006-12-18 15:39:33 +01:00
|
|
|
/******************************************************************************\
|
2009-02-21 18:48:58 +01:00
|
|
|
* Copyright (c) 2004-2009
|
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-18 21:52:48 +01:00
|
|
|
// external references
|
|
|
|
extern AsioDrivers* asioDrivers;
|
|
|
|
bool loadAsioDriver ( char *name );
|
|
|
|
|
2008-07-12 13:48:51 +02:00
|
|
|
// 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 09:26:16 +02:00
|
|
|
int iASIOBufferSizeMono;
|
2009-03-05 21:07:41 +01:00
|
|
|
int iASIOBufferSizeStereo;
|
2008-07-11 23:06:15 +02:00
|
|
|
|
2009-03-01 12:17:35 +01:00
|
|
|
CVector<short> vecsTmpAudioSndCrdStereo;
|
2008-07-13 22:03:37 +02:00
|
|
|
|
2009-03-01 12:17:35 +01:00
|
|
|
QMutex ASIOMutex;
|
2007-12-17 22:28:40 +01:00
|
|
|
|
2009-03-01 12:17:35 +01:00
|
|
|
// TEST
|
|
|
|
CSound* pSound;
|
2007-12-19 21:16:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************\
|
|
|
|
* Common *
|
|
|
|
\******************************************************************************/
|
2009-03-08 18:14:37 +01:00
|
|
|
std::string CSound::SetDev ( const int iNewDev )
|
2008-10-31 00:23:26 +01:00
|
|
|
{
|
2009-03-08 18:14:37 +01:00
|
|
|
std::string strReturn = ""; // init with no error
|
|
|
|
|
2008-10-31 00:23:26 +01:00
|
|
|
// check if an ASIO driver was already initialized
|
|
|
|
if ( lCurDev >= 0 )
|
|
|
|
{
|
2009-03-07 21:45:00 +01:00
|
|
|
// a device was already been initialized and is used, first clean up
|
2008-10-31 21:27:55 +01:00
|
|
|
// dispose ASIO buffers
|
2008-10-31 00:23:26 +01:00
|
|
|
ASIODisposeBuffers();
|
|
|
|
|
|
|
|
// remove old driver
|
|
|
|
ASIOExit();
|
|
|
|
asioDrivers->removeCurrentDriver();
|
|
|
|
|
|
|
|
const std::string strErrorMessage = LoadAndInitializeDriver ( iNewDev );
|
|
|
|
|
|
|
|
if ( !strErrorMessage.empty() )
|
|
|
|
{
|
2009-03-07 22:13:52 +01:00
|
|
|
// loading and initializing the new driver failed, go back to
|
|
|
|
// original driver and display error message
|
2008-10-31 00:23:26 +01:00
|
|
|
LoadAndInitializeDriver ( lCurDev );
|
|
|
|
|
2009-03-08 18:14:37 +01:00
|
|
|
// store error return message
|
|
|
|
strReturn = strErrorMessage;
|
2008-10-31 00:23:26 +01:00
|
|
|
}
|
2009-03-05 16:22:54 +01:00
|
|
|
|
2009-03-05 21:07:41 +01:00
|
|
|
Init ( iASIOBufferSizeStereo );
|
2008-10-31 00:23:26 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-02 09:31:48 +01:00
|
|
|
if ( iNewDev != INVALID_SNC_CARD_DEVICE )
|
2008-10-31 00:23:26 +01:00
|
|
|
{
|
2009-03-07 22:13:52 +01:00
|
|
|
// This is the first time a driver is to be initialized, we first
|
|
|
|
// try to load the selected driver, if this fails, we try to load
|
|
|
|
// the first available driver in the system. If this fails, too, we
|
|
|
|
// throw an error that no driver is available -> it does not make
|
|
|
|
// sense to start the llcon software if no audio hardware is
|
|
|
|
// available
|
2009-03-07 21:45:00 +01:00
|
|
|
if ( !LoadAndInitializeDriver ( iNewDev ).empty() )
|
2008-11-02 09:31:48 +01:00
|
|
|
{
|
2009-03-07 22:13:52 +01:00
|
|
|
// loading and initializing the new driver failed, try to find
|
|
|
|
// at least one usable driver
|
2008-11-02 09:31:48 +01:00
|
|
|
if ( !LoadAndInitializeFirstValidDriver() )
|
|
|
|
{
|
2009-03-07 22:13:52 +01:00
|
|
|
throw CGenErr ( "No usable ASIO audio device "
|
|
|
|
"(driver) found." );
|
2008-11-02 09:31:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// try to find one usable driver (select the first valid driver)
|
2008-10-31 00:23:26 +01:00
|
|
|
if ( !LoadAndInitializeFirstValidDriver() )
|
|
|
|
{
|
|
|
|
throw CGenErr ( "No usable ASIO audio device (driver) found." );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-08 18:14:37 +01:00
|
|
|
|
|
|
|
return strReturn;
|
2008-10-31 00:23:26 +01:00
|
|
|
}
|
|
|
|
|
2009-03-05 16:22:54 +01:00
|
|
|
bool CSound::LoadAndInitializeFirstValidDriver()
|
|
|
|
{
|
|
|
|
// load and initialize first valid ASIO driver
|
|
|
|
bool bValidDriverDetected = false;
|
|
|
|
int iCurDriverIdx = 0;
|
|
|
|
|
|
|
|
// try all available drivers in the system ("lNumDevs" devices)
|
|
|
|
while ( !bValidDriverDetected && ( iCurDriverIdx < lNumDevs ) )
|
|
|
|
{
|
|
|
|
if ( LoadAndInitializeDriver ( iCurDriverIdx ).empty() )
|
|
|
|
{
|
|
|
|
// initialization was successful
|
|
|
|
bValidDriverDetected = true;
|
|
|
|
|
|
|
|
// store ID of selected driver
|
|
|
|
lCurDev = iCurDriverIdx;
|
|
|
|
}
|
|
|
|
|
|
|
|
// try next driver
|
|
|
|
iCurDriverIdx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bValidDriverDetected;
|
|
|
|
}
|
|
|
|
|
2008-10-31 21:27:55 +01:00
|
|
|
std::string CSound::LoadAndInitializeDriver ( int iDriverIdx )
|
2008-10-31 00:23:26 +01:00
|
|
|
{
|
2008-10-31 21:27:55 +01:00
|
|
|
// first check and correct input parameter
|
|
|
|
if ( iDriverIdx >= lNumDevs )
|
|
|
|
{
|
|
|
|
// we assume here that at least one driver is in the system
|
|
|
|
iDriverIdx = 0;
|
|
|
|
}
|
|
|
|
|
2008-10-31 00:23:26 +01:00
|
|
|
// load driver
|
|
|
|
loadAsioDriver ( cDriverNames[iDriverIdx] );
|
|
|
|
if ( ASIOInit ( &driverInfo ) != ASE_OK )
|
|
|
|
{
|
|
|
|
// clean up and return error string
|
|
|
|
asioDrivers->removeCurrentDriver();
|
|
|
|
return "The audio driver could not be initialized.";
|
|
|
|
}
|
|
|
|
|
2009-03-05 16:22:54 +01:00
|
|
|
// check device capabilities if it fullfills our requirements
|
|
|
|
const std::string strStat = CheckDeviceCapabilities();
|
2008-10-31 00:23:26 +01:00
|
|
|
|
2008-10-31 21:27:55 +01:00
|
|
|
// store ID of selected driver if initialization was successful
|
|
|
|
if ( strStat.empty() )
|
2008-10-31 00:23:26 +01:00
|
|
|
{
|
2008-10-31 21:27:55 +01:00
|
|
|
lCurDev = iDriverIdx;
|
2008-10-31 00:23:26 +01:00
|
|
|
}
|
2009-03-05 16:22:54 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// driver cannot be used, clean up
|
|
|
|
asioDrivers->removeCurrentDriver();
|
|
|
|
}
|
2008-10-31 00:23:26 +01:00
|
|
|
|
2008-10-31 21:27:55 +01:00
|
|
|
return strStat;
|
2008-10-31 00:23:26 +01:00
|
|
|
}
|
|
|
|
|
2009-03-05 16:22:54 +01:00
|
|
|
std::string CSound::CheckDeviceCapabilities()
|
2008-10-31 00:23:26 +01:00
|
|
|
{
|
2009-03-05 16:22:54 +01:00
|
|
|
// This function checks if our required input/output channel
|
|
|
|
// properties are supported by the selected device. If the return
|
|
|
|
// string is empty, the device can be used, otherwise the error
|
|
|
|
// message is returned.
|
|
|
|
|
|
|
|
// check the sample rate
|
|
|
|
const ASIOError CanSaRateReturn = ASIOCanSampleRate ( SND_CRD_SAMPLE_RATE );
|
|
|
|
if ( ( CanSaRateReturn == ASE_NoClock ) ||
|
|
|
|
( CanSaRateReturn == ASE_NotPresent ) )
|
2008-10-31 00:23:26 +01:00
|
|
|
{
|
2009-03-05 16:22:54 +01:00
|
|
|
// return error string
|
|
|
|
return "The audio device does not support the "
|
|
|
|
"required sample rate.";
|
2008-10-31 00:23:26 +01:00
|
|
|
}
|
|
|
|
|
2008-10-31 21:27:55 +01:00
|
|
|
// check the number of available channels
|
|
|
|
long lNumInChan;
|
|
|
|
long lNumOutChan;
|
|
|
|
ASIOGetChannels ( &lNumInChan, &lNumOutChan );
|
|
|
|
if ( ( lNumInChan < NUM_IN_OUT_CHANNELS ) ||
|
|
|
|
( lNumOutChan < NUM_IN_OUT_CHANNELS ) )
|
|
|
|
{
|
2009-03-05 16:22:54 +01:00
|
|
|
// return error string
|
2008-10-31 21:27:55 +01:00
|
|
|
return "The audio device does not support the "
|
|
|
|
"required number of channels.";
|
|
|
|
}
|
2007-12-17 22:28:40 +01:00
|
|
|
|
2009-03-05 16:22:54 +01:00
|
|
|
// check sample format
|
|
|
|
for ( int i = 0; i < 2 * NUM_IN_OUT_CHANNELS; i++ )
|
2009-02-08 23:44:18 +01:00
|
|
|
{
|
2009-03-05 16:22:54 +01:00
|
|
|
// check all used input and output channels
|
|
|
|
channelInfos[i].channel = i % NUM_IN_OUT_CHANNELS;
|
|
|
|
if ( i < NUM_IN_OUT_CHANNELS )
|
|
|
|
{
|
|
|
|
channelInfos[i].isInput = ASIOTrue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
channelInfos[i].isInput = ASIOFalse;
|
|
|
|
}
|
2009-03-05 14:55:00 +01:00
|
|
|
ASIOGetChannelInfo ( &channelInfos[i] );
|
|
|
|
|
|
|
|
// only 16/24/32 LSB is supported
|
|
|
|
if ( ( channelInfos[i].type != ASIOSTInt16LSB ) &&
|
|
|
|
( channelInfos[i].type != ASIOSTInt24LSB ) &&
|
|
|
|
( channelInfos[i].type != ASIOSTInt32LSB ) )
|
|
|
|
{
|
2009-03-05 16:22:54 +01:00
|
|
|
// return error string
|
|
|
|
return "Required audio sample format not "
|
|
|
|
"available (16/24/32 bit LSB).";
|
2009-03-05 14:55:00 +01:00
|
|
|
}
|
2009-02-08 23:44:18 +01:00
|
|
|
}
|
|
|
|
|
2009-03-07 22:13:52 +01:00
|
|
|
// everything is ok, return empty string for "no error" case
|
2009-03-05 14:55:00 +01:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
int CSound::GetActualBufferSize ( const int iDesiredBufferSizeMono )
|
|
|
|
{
|
|
|
|
int iActualBufferSizeMono;
|
|
|
|
|
|
|
|
// query the usable buffer sizes
|
|
|
|
ASIOGetBufferSize ( &HWBufferInfo.lMinSize,
|
|
|
|
&HWBufferInfo.lMaxSize,
|
|
|
|
&HWBufferInfo.lPreferredSize,
|
|
|
|
&HWBufferInfo.lGranularity );
|
|
|
|
|
2008-10-31 21:27:55 +01:00
|
|
|
// calculate "nearest" buffer size and set internal parameter accordingly
|
|
|
|
// first check minimum and maximum values
|
2009-02-08 23:44:18 +01:00
|
|
|
if ( iDesiredBufferSizeMono < HWBufferInfo.lMinSize )
|
2008-10-31 21:27:55 +01:00
|
|
|
{
|
2009-03-05 14:55:00 +01:00
|
|
|
iActualBufferSizeMono = HWBufferInfo.lMinSize;
|
2008-10-31 21:27:55 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-02-08 23:44:18 +01:00
|
|
|
if ( iDesiredBufferSizeMono > HWBufferInfo.lMaxSize )
|
2008-04-13 18:43:21 +02:00
|
|
|
{
|
2009-03-05 14:55:00 +01:00
|
|
|
iActualBufferSizeMono = HWBufferInfo.lMaxSize;
|
2008-04-13 18:43:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-10-31 21:27:55 +01:00
|
|
|
// initialization
|
|
|
|
int iTrialBufSize = HWBufferInfo.lMinSize;
|
|
|
|
int iLastTrialBufSize = HWBufferInfo.lMinSize;
|
|
|
|
bool bSizeFound = false;
|
2008-07-12 13:48:51 +02:00
|
|
|
|
2008-10-31 21:27:55 +01:00
|
|
|
// test loop
|
|
|
|
while ( ( iTrialBufSize <= HWBufferInfo.lMaxSize ) && ( !bSizeFound ) )
|
|
|
|
{
|
2009-02-08 23:44:18 +01:00
|
|
|
if ( iTrialBufSize >= iDesiredBufferSizeMono )
|
2008-04-13 18:43:21 +02:00
|
|
|
{
|
2008-10-31 21:27:55 +01:00
|
|
|
// test which buffer size fits better: the old one or the
|
|
|
|
// current one
|
2009-02-08 23:44:18 +01:00
|
|
|
if ( ( iTrialBufSize - iDesiredBufferSizeMono ) >
|
|
|
|
( iDesiredBufferSizeMono - iLastTrialBufSize ) )
|
2008-04-13 18:43:21 +02:00
|
|
|
{
|
2009-02-08 23:44:18 +01:00
|
|
|
iTrialBufSize = iLastTrialBufSize;
|
2008-04-13 18:43:21 +02:00
|
|
|
}
|
2008-10-31 21:27:55 +01:00
|
|
|
|
|
|
|
// exit while loop
|
|
|
|
bSizeFound = true;
|
2008-04-13 18:43:21 +02:00
|
|
|
}
|
2008-07-13 09:26:16 +02:00
|
|
|
|
2008-10-31 21:27:55 +01:00
|
|
|
if ( !bSizeFound )
|
|
|
|
{
|
|
|
|
// 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-10-31 21:27:55 +01:00
|
|
|
|
|
|
|
// set ASIO buffer size
|
2009-03-05 14:55:00 +01:00
|
|
|
iActualBufferSizeMono = iTrialBufSize;
|
2008-04-13 18:43:21 +02:00
|
|
|
}
|
2008-10-31 21:27:55 +01:00
|
|
|
}
|
2008-04-08 20:38:55 +02:00
|
|
|
|
2009-03-05 14:55:00 +01:00
|
|
|
return iActualBufferSizeMono;
|
2008-10-31 21:27:55 +01:00
|
|
|
}
|
|
|
|
|
2009-03-01 23:08:06 +01:00
|
|
|
int CSound::Init ( const int iNewPrefMonoBufferSize )
|
2008-10-31 21:27:55 +01:00
|
|
|
{
|
|
|
|
ASIOMutex.lock(); // get mutex lock
|
|
|
|
{
|
2009-03-05 21:07:41 +01:00
|
|
|
// get the actual sound card buffer size which is supported
|
|
|
|
// by the audio hardware
|
|
|
|
iASIOBufferSizeMono =
|
|
|
|
GetActualBufferSize ( iNewPrefMonoBufferSize );
|
|
|
|
|
2009-02-24 23:56:19 +01:00
|
|
|
// init base clasee
|
2009-03-05 21:07:41 +01:00
|
|
|
CSoundBase::Init ( iASIOBufferSizeMono );
|
2009-02-24 23:56:19 +01:00
|
|
|
|
2009-03-01 23:08:06 +01:00
|
|
|
// set internal buffer size value and calculate stereo buffer size
|
2009-03-05 21:07:41 +01:00
|
|
|
iASIOBufferSizeStereo = 2 * iASIOBufferSizeMono;
|
2009-03-05 14:55:00 +01:00
|
|
|
|
2009-03-05 21:07:41 +01:00
|
|
|
// set the sample rate
|
|
|
|
ASIOSetSampleRate ( SND_CRD_SAMPLE_RATE );
|
2009-03-05 14:55:00 +01:00
|
|
|
|
2009-03-01 12:17:35 +01:00
|
|
|
// create memory for intermediate audio buffer
|
2009-03-05 21:07:41 +01:00
|
|
|
vecsTmpAudioSndCrdStereo.Init ( iASIOBufferSizeStereo );
|
2009-03-05 16:22:54 +01:00
|
|
|
|
2009-03-08 12:22:55 +01:00
|
|
|
// create and activate ASIO buffers (buffer size in samples),
|
|
|
|
// dispose old buffers (if any)
|
|
|
|
ASIODisposeBuffers();
|
2009-03-05 21:07:41 +01:00
|
|
|
ASIOCreateBuffers ( bufferInfos,
|
|
|
|
2 /* in/out */ * NUM_IN_OUT_CHANNELS /* stereo */,
|
|
|
|
iASIOBufferSizeMono, &asioCallbacks );
|
2009-03-05 16:22:54 +01:00
|
|
|
|
2009-03-05 21:07:41 +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 );
|
2009-03-05 16:22:54 +01:00
|
|
|
}
|
2009-03-05 21:07:41 +01:00
|
|
|
ASIOMutex.unlock();
|
2009-03-05 16:22:54 +01:00
|
|
|
|
2009-03-05 21:07:41 +01:00
|
|
|
return iASIOBufferSizeMono;
|
|
|
|
}
|
2009-03-05 16:22:54 +01:00
|
|
|
|
2009-03-05 21:07:41 +01:00
|
|
|
void CSound::Start()
|
|
|
|
{
|
|
|
|
// start audio
|
|
|
|
ASIOStart();
|
2009-03-05 16:22:54 +01:00
|
|
|
|
2009-03-05 21:07:41 +01:00
|
|
|
// call base class
|
|
|
|
CSoundBase::Start();
|
|
|
|
}
|
2007-12-19 21:16:50 +01:00
|
|
|
|
2009-03-05 21:07:41 +01:00
|
|
|
void CSound::Stop()
|
|
|
|
{
|
|
|
|
// stop audio
|
|
|
|
ASIOStop();
|
2009-03-01 23:08:06 +01:00
|
|
|
|
2009-03-05 21:07:41 +01:00
|
|
|
// call base class
|
|
|
|
CSoundBase::Stop();
|
2007-12-19 21:16:50 +01:00
|
|
|
}
|
|
|
|
|
2009-02-24 23:56:19 +01:00
|
|
|
CSound::CSound ( void (*fpNewCallback) ( CVector<short>& psData, void* arg ), void* arg ) :
|
2009-03-01 12:17:35 +01:00
|
|
|
CSoundBase ( true, fpNewCallback, arg )
|
2007-12-17 22:28:40 +01:00
|
|
|
{
|
2009-03-05 21:07:41 +01:00
|
|
|
int i;
|
2007-12-17 22:28:40 +01:00
|
|
|
|
2009-03-01 12:17:35 +01:00
|
|
|
// TEST
|
|
|
|
pSound = this;
|
|
|
|
|
2007-12-17 22:28:40 +01:00
|
|
|
|
2007-12-18 21:52:48 +01:00
|
|
|
// get available ASIO driver names in system
|
2009-03-05 21:07:41 +01:00
|
|
|
for ( i = 0; i < MAX_NUMBER_SOUND_CARDS; i++ )
|
2007-12-18 21:52:48 +01:00
|
|
|
{
|
|
|
|
cDriverNames[i] = new char[32];
|
|
|
|
}
|
|
|
|
|
|
|
|
loadAsioDriver ( "dummy" ); // to initialize external object
|
2008-10-31 00:23:26 +01:00
|
|
|
lNumDevs = asioDrivers->getDriverNames ( cDriverNames, MAX_NUMBER_SOUND_CARDS );
|
2007-12-18 21:52:48 +01:00
|
|
|
|
|
|
|
// in case we do not have a driver available, throw error
|
2008-10-31 00:23:26 +01:00
|
|
|
if ( lNumDevs == 0 )
|
2007-12-19 21:16:50 +01:00
|
|
|
{
|
2008-10-31 00:23:26 +01:00
|
|
|
throw CGenErr ( "No ASIO audio device (driver) found." );
|
2007-12-19 21:16:50 +01:00
|
|
|
}
|
2008-10-31 00:23:26 +01:00
|
|
|
asioDrivers->removeCurrentDriver();
|
2007-12-19 21:16:50 +01:00
|
|
|
|
2008-10-31 00:23:26 +01:00
|
|
|
// init device index with illegal value to show that driver is not initialized
|
|
|
|
lCurDev = -1;
|
2007-12-19 21:16:50 +01:00
|
|
|
|
2009-03-05 21:07:41 +01:00
|
|
|
// init buffer infos, we always want to have two input and
|
|
|
|
// two output channels
|
|
|
|
for ( i = 0; i < NUM_IN_OUT_CHANNELS; i++ )
|
|
|
|
{
|
|
|
|
// prepare input channels
|
|
|
|
bufferInfos[i].isInput = ASIOTrue;
|
|
|
|
bufferInfos[i].channelNum = i;
|
|
|
|
bufferInfos[i].buffers[0] = 0;
|
|
|
|
bufferInfos[i].buffers[1] = 0;
|
|
|
|
|
|
|
|
// prepare output channels
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-02-22 12:07:18 +01:00
|
|
|
// set up the asioCallback structure
|
|
|
|
asioCallbacks.bufferSwitch = &bufferSwitch;
|
|
|
|
asioCallbacks.sampleRateDidChange = &sampleRateChanged;
|
|
|
|
asioCallbacks.asioMessage = &asioMessages;
|
|
|
|
asioCallbacks.bufferSwitchTimeInfo = &bufferSwitchTimeInfo;
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2009-02-22 12:07:18 +01:00
|
|
|
bufferSwitch ( index, processNow );
|
|
|
|
return 0L;
|
2008-03-16 14:10:46 +01:00
|
|
|
}
|
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
|
|
|
// perform the processing for input and output
|
2009-02-22 12:07:18 +01:00
|
|
|
for ( int i = 0; i < 2 * NUM_IN_OUT_CHANNELS; i++ ) // stereo
|
|
|
|
{
|
2008-07-16 00:09:48 +02:00
|
|
|
if ( bufferInfos[i].isInput == ASIOTrue )
|
2009-02-22 12:07:18 +01:00
|
|
|
{
|
2008-07-13 01:33:27 +02:00
|
|
|
// CAPTURE -----------------------------------------------------
|
2009-03-01 12:17:35 +01:00
|
|
|
// copy new captured block in thread transfer buffer (copy
|
|
|
|
// mono data interleaved in stereo buffer)
|
|
|
|
switch ( channelInfos[i].type )
|
2008-07-12 13:48:51 +02:00
|
|
|
{
|
2009-03-01 12:17:35 +01:00
|
|
|
case ASIOSTInt16LSB:
|
|
|
|
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
|
2009-02-22 12:07:18 +01:00
|
|
|
{
|
2009-03-01 12:17:35 +01:00
|
|
|
vecsTmpAudioSndCrdStereo[2 * iCurSample + bufferInfos[i].channelNum] =
|
|
|
|
( (short*) bufferInfos[i].buffers[index] )[iCurSample];
|
|
|
|
}
|
|
|
|
break;
|
2008-07-24 20:15:48 +02:00
|
|
|
|
2009-03-01 12:17:35 +01:00
|
|
|
case ASIOSTInt24LSB:
|
2008-07-24 20:15:48 +02:00
|
|
|
|
|
|
|
// not yet tested, horrible things might happen with the following code ;-)
|
|
|
|
|
2009-03-01 12:17:35 +01:00
|
|
|
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;
|
|
|
|
|
|
|
|
vecsTmpAudioSndCrdStereo[2 * iCurSample + bufferInfos[i].channelNum] =
|
|
|
|
static_cast<short> ( iCurSam );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ASIOSTInt32LSB:
|
|
|
|
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
|
|
|
|
{
|
|
|
|
// convert to 16 bit
|
|
|
|
vecsTmpAudioSndCrdStereo[2 * iCurSample + bufferInfos[i].channelNum] =
|
|
|
|
(((int*) bufferInfos[i].buffers[index])[iCurSample] >> 16);
|
2008-07-12 13:48:51 +02:00
|
|
|
}
|
2009-03-01 12:17:35 +01:00
|
|
|
break;
|
2008-07-12 13:48:51 +02:00
|
|
|
}
|
2009-02-22 12:07:18 +01:00
|
|
|
}
|
2009-03-01 12:17:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// call processing callback function
|
2009-03-08 22:38:38 +01:00
|
|
|
pSound->Callback ( vecsTmpAudioSndCrdStereo );
|
2009-03-01 12:17:35 +01:00
|
|
|
|
|
|
|
// perform the processing for input and output
|
|
|
|
for ( int i = 0; i < 2 * NUM_IN_OUT_CHANNELS; i++ ) // stereo
|
|
|
|
{
|
|
|
|
if ( bufferInfos[i].isInput != ASIOTrue )
|
2008-07-16 00:09:48 +02:00
|
|
|
{
|
|
|
|
// PLAYBACK ----------------------------------------------------
|
2009-03-01 12:17:35 +01:00
|
|
|
// copy data from sound card in output buffer (copy
|
|
|
|
// interleaved stereo data in mono sound card buffer)
|
|
|
|
switch ( channelInfos[i].type )
|
2008-07-16 00:09:48 +02:00
|
|
|
{
|
2009-03-01 12:17:35 +01:00
|
|
|
case ASIOSTInt16LSB:
|
|
|
|
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
|
2009-02-22 12:07:18 +01:00
|
|
|
{
|
2009-03-01 12:17:35 +01:00
|
|
|
( (short*) bufferInfos[i].buffers[index] )[iCurSample] =
|
|
|
|
vecsTmpAudioSndCrdStereo[2 * iCurSample + bufferInfos[i].channelNum];
|
|
|
|
}
|
|
|
|
break;
|
2008-07-24 20:15:48 +02:00
|
|
|
|
2009-03-01 12:17:35 +01:00
|
|
|
case ASIOSTInt24LSB:
|
2008-07-24 20:15:48 +02:00
|
|
|
|
|
|
|
// not yet tested, horrible things might happen with the following code ;-)
|
|
|
|
|
2009-03-01 12:17:35 +01:00
|
|
|
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
|
|
|
|
{
|
|
|
|
// convert current sample in 24 bit format
|
|
|
|
int iCurSam = static_cast<int> ( vecsTmpAudioSndCrdStereo[2 * iCurSample + bufferInfos[i].channelNum] );
|
|
|
|
iCurSam <<= 8;
|
2008-07-13 01:33:27 +02:00
|
|
|
|
2009-03-01 12:17:35 +01:00
|
|
|
memcpy ( ( (char*) bufferInfos[i].buffers[index] ) + iCurSample * 3, &iCurSam, 3 );
|
|
|
|
}
|
|
|
|
break;
|
2008-07-16 00:09:48 +02:00
|
|
|
|
2009-03-01 12:17:35 +01:00
|
|
|
case ASIOSTInt32LSB:
|
|
|
|
for ( iCurSample = 0; iCurSample < iASIOBufferSizeMono; iCurSample++ )
|
|
|
|
{
|
|
|
|
// convert to 32 bit
|
|
|
|
int iCurSam = static_cast<int> ( vecsTmpAudioSndCrdStereo[2 * iCurSample + bufferInfos[i].channelNum] );
|
|
|
|
( (int*) bufferInfos[i].buffers[index] )[iCurSample] = ( iCurSam << 16 );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-07-12 13:48:51 +02:00
|
|
|
}
|
2008-07-13 01:33:27 +02:00
|
|
|
}
|
2008-07-13 15:16:31 +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 )
|
|
|
|
{
|
2009-02-22 12:07:18 +01:00
|
|
|
ASIOOutputReady();
|
2008-07-13 22:03:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ASIOMutex.unlock();
|
2007-12-19 21:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
long CSound::asioMessages ( long selector, long value, void* message, double* opt )
|
|
|
|
{
|
2009-02-22 12:07:18 +01:00
|
|
|
long ret = 0;
|
|
|
|
switch(selector)
|
|
|
|
{
|
|
|
|
case kAsioEngineVersion:
|
|
|
|
// return the supported ASIO version of the host application
|
2009-03-07 22:13:52 +01:00
|
|
|
ret = 2L; // Host ASIO implementation version, 2 or higher
|
|
|
|
break;
|
|
|
|
|
|
|
|
// both messages might be send if the buffer size changes
|
|
|
|
case kAsioBufferSizeChange:
|
|
|
|
case kAsioResetRequest:
|
|
|
|
|
|
|
|
// TODO reinit sound interface and check for new buffer size
|
|
|
|
// requires changes in client class, too
|
|
|
|
|
|
|
|
|
2009-03-07 23:56:46 +01:00
|
|
|
ret = 0;//1L; // 1L if request is accepted or 0 otherwise
|
2009-02-22 12:07:18 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
2007-12-19 21:16:50 +01:00
|
|
|
}
|