2013-01-23 11:41:13 +01:00
|
|
|
/******************************************************************************\
|
2020-01-01 15:41:43 +01:00
|
|
|
* Copyright (c) 2004-2020
|
2013-01-23 11:41:13 +01:00
|
|
|
*
|
|
|
|
* Author(s):
|
|
|
|
* Volker Fischer
|
|
|
|
*
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* 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 "soundbase.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Implementation *************************************************************/
|
|
|
|
CSoundBase::CSoundBase ( const QString& strNewSystemDriverTechniqueName,
|
2019-01-12 13:45:08 +01:00
|
|
|
const bool bNewIsCallbackAudioInterface,
|
|
|
|
void (*fpNewProcessCallback) ( CVector<int16_t>& psData, void* pParg ),
|
|
|
|
void* pParg,
|
2020-04-30 22:18:11 +02:00
|
|
|
const int iNewCtrlMIDIChannel ) :
|
2020-05-01 14:21:08 +02:00
|
|
|
fpProcessCallback ( fpNewProcessCallback ),
|
|
|
|
pProcessCallbackArg ( pParg ), bRun ( false ),
|
|
|
|
bIsCallbackAudioInterface ( bNewIsCallbackAudioInterface ),
|
2019-01-22 17:11:41 +01:00
|
|
|
strSystemDriverTechniqueName ( strNewSystemDriverTechniqueName ),
|
2020-05-01 14:21:08 +02:00
|
|
|
iCtrlMIDIChannel ( iNewCtrlMIDIChannel )
|
2013-01-23 11:41:13 +01:00
|
|
|
{
|
|
|
|
// initializations for the sound card names (default)
|
|
|
|
lNumDevs = 1;
|
|
|
|
strDriverNames[0] = strSystemDriverTechniqueName;
|
|
|
|
|
|
|
|
// set current device
|
|
|
|
lCurDev = 0; // default device
|
|
|
|
}
|
|
|
|
|
|
|
|
int CSoundBase::Init ( const int iNewPrefMonoBufferSize )
|
|
|
|
{
|
|
|
|
// init audio sound card buffer
|
|
|
|
if ( !bIsCallbackAudioInterface )
|
|
|
|
{
|
|
|
|
vecsAudioSndCrdStereo.Init ( 2 * iNewPrefMonoBufferSize /* stereo */ );
|
|
|
|
}
|
|
|
|
|
|
|
|
return iNewPrefMonoBufferSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSoundBase::Start()
|
|
|
|
{
|
|
|
|
bRun = true;
|
|
|
|
|
|
|
|
// TODO start audio interface
|
|
|
|
|
|
|
|
// start the audio thread in case we do not have an callback
|
|
|
|
// based audio interface
|
|
|
|
if ( !bIsCallbackAudioInterface )
|
|
|
|
{
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSoundBase::Stop()
|
|
|
|
{
|
|
|
|
// set flag so that thread can leave the main loop
|
|
|
|
bRun = false;
|
|
|
|
|
|
|
|
// give thread some time to terminate
|
|
|
|
if ( !bIsCallbackAudioInterface )
|
|
|
|
{
|
|
|
|
wait ( 5000 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSoundBase::run()
|
|
|
|
{
|
|
|
|
// main loop of working thread
|
|
|
|
while ( bRun )
|
|
|
|
{
|
|
|
|
// get audio from sound card (blocking function)
|
2014-01-02 21:11:56 +01:00
|
|
|
Read ( vecsAudioSndCrdStereo );
|
2013-01-23 11:41:13 +01:00
|
|
|
|
|
|
|
// process audio data
|
|
|
|
(*fpProcessCallback) ( vecsAudioSndCrdStereo, pProcessCallbackArg );
|
|
|
|
|
|
|
|
// play the new block
|
2014-01-02 21:11:56 +01:00
|
|
|
Write ( vecsAudioSndCrdStereo );
|
2013-01-23 11:41:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 13:45:08 +01:00
|
|
|
|
2013-01-23 11:41:13 +01:00
|
|
|
|
|
|
|
/******************************************************************************\
|
|
|
|
* Device handling *
|
|
|
|
\******************************************************************************/
|
|
|
|
QString CSoundBase::SetDev ( const int iNewDev )
|
|
|
|
{
|
|
|
|
// init return parameter with "no error"
|
|
|
|
QString strReturn = "";
|
|
|
|
|
|
|
|
// first check if valid input parameter
|
|
|
|
if ( iNewDev >= lNumDevs )
|
|
|
|
{
|
|
|
|
// we should actually never get here...
|
|
|
|
return tr ( "Invalid device selection." );
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if an ASIO driver was already initialized
|
|
|
|
if ( lCurDev != INVALID_SNC_CARD_DEVICE )
|
|
|
|
{
|
|
|
|
// a device was already been initialized and is used, first clean up
|
|
|
|
// driver
|
|
|
|
UnloadCurrentDriver();
|
|
|
|
|
2020-04-20 19:57:21 +02:00
|
|
|
const QString strErrorMessage = LoadAndInitializeDriver ( iNewDev, false );
|
2013-01-23 11:41:13 +01:00
|
|
|
|
|
|
|
if ( !strErrorMessage.isEmpty() )
|
|
|
|
{
|
|
|
|
if ( iNewDev != lCurDev )
|
|
|
|
{
|
|
|
|
// loading and initializing the new driver failed, go back to
|
|
|
|
// original driver and display error message
|
2020-04-20 19:57:21 +02:00
|
|
|
LoadAndInitializeDriver ( lCurDev, false );
|
2013-01-23 11:41:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// the same driver is used but the driver properties seems to
|
|
|
|
// have changed so that they are not compatible to our
|
|
|
|
// software anymore
|
|
|
|
QMessageBox::critical (
|
2019-05-17 22:55:46 +02:00
|
|
|
nullptr, APP_NAME, QString ( tr ( "The audio driver properties "
|
2020-05-21 15:27:07 +02:00
|
|
|
"have changed to a state which is incompatible with this "
|
2013-01-23 11:41:13 +01:00
|
|
|
"software. The selected audio device could not be used "
|
2020-05-07 09:16:28 +02:00
|
|
|
"because of the following error:" ) + " <b>" ) +
|
2013-01-23 11:41:13 +01:00
|
|
|
strErrorMessage +
|
2020-05-07 09:16:28 +02:00
|
|
|
QString ( "</b><br><br>" + tr ( "Please restart the software." ) ),
|
|
|
|
tr ( "Close" ), nullptr );
|
2013-01-23 11:41:13 +01:00
|
|
|
|
|
|
|
_exit ( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
// store error return message
|
|
|
|
strReturn = strErrorMessage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// init flag for "load any driver"
|
|
|
|
bool bTryLoadAnyDriver = false;
|
|
|
|
|
|
|
|
if ( iNewDev != INVALID_SNC_CARD_DEVICE )
|
|
|
|
{
|
|
|
|
// 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
|
2013-03-24 11:49:25 +01:00
|
|
|
// sense to start the software if no audio hardware is available.
|
2020-04-20 19:57:21 +02:00
|
|
|
if ( !LoadAndInitializeDriver ( iNewDev, false ).isEmpty() )
|
2013-01-23 11:41:13 +01:00
|
|
|
{
|
|
|
|
// loading and initializing the new driver failed, try to find
|
|
|
|
// at least one usable driver
|
|
|
|
bTryLoadAnyDriver = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// try to find one usable driver (select the first valid driver)
|
|
|
|
bTryLoadAnyDriver = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( bTryLoadAnyDriver )
|
|
|
|
{
|
|
|
|
// try to load and initialize any valid driver
|
2020-04-20 19:57:21 +02:00
|
|
|
QVector<QString> vsErrorList = LoadAndInitializeFirstValidDriver();
|
2013-01-23 11:41:13 +01:00
|
|
|
|
|
|
|
if ( !vsErrorList.isEmpty() )
|
|
|
|
{
|
|
|
|
// create error message with all details
|
2020-05-07 09:49:37 +02:00
|
|
|
QString sErrorMessage = "<b>" + tr ( "No usable " ) +
|
2013-01-23 11:41:13 +01:00
|
|
|
strSystemDriverTechniqueName + tr ( " audio device "
|
2020-05-07 09:45:11 +02:00
|
|
|
"(driver) found." ) + "</b><br><br>" + tr (
|
2013-01-23 11:41:13 +01:00
|
|
|
"In the following there is a list of all available drivers "
|
2020-05-07 09:45:11 +02:00
|
|
|
"with the associated error message:" ) + "<ul>";
|
2013-01-23 11:41:13 +01:00
|
|
|
|
|
|
|
for ( int i = 0; i < lNumDevs; i++ )
|
|
|
|
{
|
2020-03-21 10:27:31 +01:00
|
|
|
sErrorMessage += "<li><b>" + GetDeviceName ( i ) + "</b>: " + vsErrorList[i] + "</li>";
|
2013-01-23 11:41:13 +01:00
|
|
|
}
|
|
|
|
sErrorMessage += "</ul>";
|
|
|
|
|
2020-04-20 19:57:21 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
// to be able to access the ASIO driver setup for changing, e.g., the sample rate, we
|
|
|
|
// offer the user under Windows that we open the driver setups of all registered
|
|
|
|
// ASIO drivers
|
2020-05-07 09:45:11 +02:00
|
|
|
sErrorMessage = sErrorMessage + "<br/>" + tr ( "Do you want to open the ASIO driver setups?" );
|
2020-04-20 19:57:21 +02:00
|
|
|
|
|
|
|
if ( QMessageBox::Yes == QMessageBox::information ( nullptr, APP_NAME, sErrorMessage, QMessageBox::Yes|QMessageBox::No ) )
|
|
|
|
{
|
|
|
|
LoadAndInitializeFirstValidDriver ( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
sErrorMessage = APP_NAME + tr ( " could not be started because of audio interface issues." );
|
|
|
|
#endif
|
|
|
|
|
2013-01-23 11:41:13 +01:00
|
|
|
throw CGenErr ( sErrorMessage );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strReturn;
|
|
|
|
}
|
|
|
|
|
2020-04-20 19:57:21 +02:00
|
|
|
QVector<QString> CSoundBase::LoadAndInitializeFirstValidDriver ( const bool bOpenDriverSetup )
|
2013-01-23 11:41:13 +01:00
|
|
|
{
|
|
|
|
QVector<QString> vsErrorList;
|
|
|
|
|
|
|
|
// load and initialize first valid ASIO driver
|
|
|
|
bool bValidDriverDetected = false;
|
2020-03-21 10:27:31 +01:00
|
|
|
int iCurDriverIdx = 0;
|
2013-01-23 11:41:13 +01:00
|
|
|
|
|
|
|
// try all available drivers in the system ("lNumDevs" devices)
|
|
|
|
while ( !bValidDriverDetected && ( iCurDriverIdx < lNumDevs ) )
|
|
|
|
{
|
|
|
|
// try to load and initialize current driver, store error message
|
2020-04-20 19:57:21 +02:00
|
|
|
const QString strCurError = LoadAndInitializeDriver ( iCurDriverIdx, bOpenDriverSetup );
|
2013-01-23 11:41:13 +01:00
|
|
|
|
|
|
|
vsErrorList.append ( strCurError );
|
|
|
|
|
|
|
|
if ( strCurError.isEmpty() )
|
|
|
|
{
|
|
|
|
// initialization was successful
|
|
|
|
bValidDriverDetected = true;
|
|
|
|
|
|
|
|
// store ID of selected driver
|
|
|
|
lCurDev = iCurDriverIdx;
|
|
|
|
|
|
|
|
// empty error list shows that init was successful
|
|
|
|
vsErrorList.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// try next driver
|
|
|
|
iCurDriverIdx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vsErrorList;
|
|
|
|
}
|
2019-09-28 13:24:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************\
|
|
|
|
* MIDI handling *
|
|
|
|
\******************************************************************************/
|
|
|
|
void CSoundBase::ParseMIDIMessage ( const CVector<uint8_t>& vMIDIPaketBytes )
|
|
|
|
{
|
|
|
|
if ( vMIDIPaketBytes.Size() > 0 )
|
|
|
|
{
|
|
|
|
const uint8_t iStatusByte = vMIDIPaketBytes[0];
|
|
|
|
|
|
|
|
// check if status byte is correct
|
|
|
|
if ( ( iStatusByte >= 0x80 ) && ( iStatusByte < 0xF0 ) )
|
|
|
|
{
|
|
|
|
// zero-based MIDI channel number (i.e. range 0-15)
|
|
|
|
const int iMIDIChannelZB = iStatusByte & 0x0F;
|
|
|
|
|
|
|
|
/*
|
|
|
|
// debugging
|
|
|
|
printf ( "%02X: ", iMIDIChannelZB );
|
|
|
|
for ( int i = 0; i < vMIDIPaketBytes.Size(); i++ )
|
|
|
|
{
|
|
|
|
printf ( "%02X ", vMIDIPaketBytes[i] );
|
|
|
|
}
|
|
|
|
printf ( "\n" );
|
|
|
|
*/
|
|
|
|
|
|
|
|
// per definition if MIDI channel is 0, we listen to all channels
|
|
|
|
// note that iCtrlMIDIChannel is one-based channel number
|
|
|
|
if ( ( iCtrlMIDIChannel == 0 ) || ( iCtrlMIDIChannel - 1 == iMIDIChannelZB ) )
|
|
|
|
{
|
|
|
|
// we only want to parse controller messages
|
|
|
|
if ( ( iStatusByte >= 0xB0 ) && ( iStatusByte < 0xC0 ) )
|
|
|
|
{
|
|
|
|
// make sure paket is long enough
|
|
|
|
if ( vMIDIPaketBytes.Size() > 2 )
|
|
|
|
{
|
|
|
|
// we are assuming that the controller number is the same
|
|
|
|
// as the audio fader index and the range is 0-127
|
|
|
|
const int iFaderLevel = static_cast<int> ( static_cast<double> (
|
|
|
|
qMin ( vMIDIPaketBytes[2], uint8_t ( 127 ) ) ) / 127 * AUD_MIX_FADER_MAX );
|
|
|
|
|
|
|
|
// Behringer X-TOUCH: offset of 0x46
|
|
|
|
const int iChID = vMIDIPaketBytes[1] - 70;
|
|
|
|
|
|
|
|
EmitControllerInFaderLevel ( iChID, iFaderLevel );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|