Skip to content

Commit

Permalink
Merge pull request #1456 from theGreatWhiteShark/phil-fix-audio-drive…
Browse files Browse the repository at this point in the history
…r-checks

use dynamic_cast to tell audio drivers apart
  • Loading branch information
theGreatWhiteShark authored Jan 17, 2022
2 parents 56bc77a + d387a49 commit ecc7ccb
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 82 deletions.
69 changes: 21 additions & 48 deletions src/core/AudioEngine/AudioEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ void AudioEngine::clearAudioBuffers( uint32_t nFrames )

#ifdef H2CORE_HAVE_JACK
if ( Hydrogen::get_instance()->haveJackAudioDriver() ) {
JackAudioDriver * pJackAudioDriver = dynamic_cast<JackAudioDriver*>(m_pAudioDriver);
JackAudioDriver* pJackAudioDriver = static_cast<JackAudioDriver*>(m_pAudioDriver);

if( pJackAudioDriver ) {
pJackAudioDriver->clearPerTrackAudioBuffers( nFrames );
Expand Down Expand Up @@ -494,61 +494,38 @@ AudioOutput* AudioEngine::createDriver( const QString& sDriver )

if ( sDriver == "OSS" ) {
pDriver = new OssDriver( m_AudioProcessCallback );
if ( pDriver->class_name() == NullDriver::_class_name() ) {
delete pDriver;
pDriver = nullptr;
}
} else if ( sDriver == "JACK" ) {
pDriver = new JackAudioDriver( m_AudioProcessCallback );
if ( pDriver->class_name() == NullDriver::_class_name() ) {
delete pDriver;
pDriver = nullptr;
} else {
#ifdef H2CORE_HAVE_JACK
static_cast<JackAudioDriver*>(pDriver)->setConnectDefaults(
Preferences::get_instance()->m_bJackConnectDefaults
);
#endif
if ( auto pJackDriver = dynamic_cast<JackAudioDriver*>( pDriver ) ) {
pJackDriver->setConnectDefaults(
Preferences::get_instance()->m_bJackConnectDefaults
);
}
#endif
} else if ( sDriver == "ALSA" ) {
pDriver = new AlsaAudioDriver( m_AudioProcessCallback );
if ( pDriver->class_name() == NullDriver::_class_name() ) {
delete pDriver;
pDriver = nullptr;
}
} else if ( sDriver == "PortAudio" ) {
pDriver = new PortAudioDriver( m_AudioProcessCallback );
if ( pDriver->class_name() == NullDriver::_class_name() ) {
delete pDriver;
pDriver = nullptr;
}
}
//#ifdef Q_OS_MACX
else if ( sDriver == "CoreAudio" ) {
} else if ( sDriver == "CoreAudio" ) {
___INFOLOG( "Creating CoreAudioDriver" );
pDriver = new CoreAudioDriver( m_AudioProcessCallback );
if ( pDriver->class_name() == NullDriver::_class_name() ) {
delete pDriver;
pDriver = nullptr;
}
}
//#endif
else if ( sDriver == "PulseAudio" ) {
} else if ( sDriver == "PulseAudio" ) {
pDriver = new PulseAudioDriver( m_AudioProcessCallback );
if ( pDriver->class_name() == NullDriver::_class_name() ) {
delete pDriver;
pDriver = nullptr;
}
}
else if ( sDriver == "Fake" ) {
} else if ( sDriver == "Fake" ) {
___WARNINGLOG( "*** Using FAKE audio driver ***" );
pDriver = new FakeDriver( m_AudioProcessCallback );
} else {
___ERRORLOG( "Unknown driver " + sDriver );
raiseError( Hydrogen::UNKNOWN_DRIVER );
}

if ( dynamic_cast<NullDriver*>(pDriver) != nullptr ) {
delete pDriver;
pDriver = nullptr;
}

if ( pDriver ) {
if ( pDriver != nullptr ) {
// initialize the audio driver
int res = pDriver->init( pPref->m_nBufferSize );
if ( res != 0 ) {
Expand Down Expand Up @@ -681,8 +658,7 @@ void AudioEngine::setAudioDriver( AudioOutput* pAudioDriver ) {
mx.unlock();
this->unlock();

if ( m_pAudioDriver != nullptr &&
m_pAudioDriver->class_name() != DiskWriterDriver::_class_name() ) {
if ( m_pAudioDriver != nullptr ) {
int res = m_pAudioDriver->connect();
if ( res != 0 ) {
raiseError( Hydrogen::ERROR_STARTING_DRIVER );
Expand All @@ -697,11 +673,9 @@ void AudioEngine::setAudioDriver( AudioOutput* pAudioDriver ) {
m_pAudioDriver->connect();
}

#ifdef H2CORE_HAVE_JACK
if ( pSong != nullptr ) {
if ( pSong != nullptr && pHydrogen->haveJackAudioDriver() ) {
pHydrogen->renameJackPorts( pSong );
}
#endif

setupLadspaFX();
}
Expand Down Expand Up @@ -1113,7 +1087,7 @@ int AudioEngine::audioEngine_process( uint32_t nframes, void* /*arg*/ )
RIGHT_HERE ) ) {
___ERRORLOG( QString( "Failed to lock audioEngine in allowed %1 ms, missed buffer" ).arg( fSlackTime ) );

if ( pAudioEngine->m_pAudioDriver->class_name() == DiskWriterDriver::_class_name() ) {
if ( dynamic_cast<DiskWriterDriver*>(pAudioEngine->m_pAudioDriver) != nullptr ) {
return 2; // inform the caller that we could not aquire the lock
}

Expand Down Expand Up @@ -1152,9 +1126,8 @@ int AudioEngine::audioEngine_process( uint32_t nframes, void* /*arg*/ )
pAudioEngine->stop();
pAudioEngine->locate( 0 ); // locate 0, reposition from start of the song

if ( (pAudioEngine->m_pAudioDriver->class_name() == DiskWriterDriver::_class_name() )
|| ( pAudioEngine->m_pAudioDriver->class_name() == FakeDriver::_class_name() )
) {
if ( dynamic_cast<DiskWriterDriver*>(pAudioEngine->m_pAudioDriver) != nullptr
|| dynamic_cast<FakeDriver*>(pAudioEngine->m_pAudioDriver) != nullptr ) {
___INFOLOG( "End of song." );

return 1; // kill the audio AudioDriver thread
Expand Down Expand Up @@ -1837,7 +1810,7 @@ void AudioEngine::play() {

setNextState( State::Playing );

if ( m_pAudioDriver->class_name() == FakeDriver::_class_name() ) {
if ( dynamic_cast<FakeDriver*>(m_pAudioDriver) != nullptr ) {
static_cast<FakeDriver*>( m_pAudioDriver )->processCallback();
}
}
Expand Down
30 changes: 10 additions & 20 deletions src/core/Hydrogen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,23 +636,13 @@ void Hydrogen::startExportSong( const QString& filename)

DiskWriterDriver* pDiskWriterDriver = static_cast<DiskWriterDriver*>(pAudioEngine->getAudioDriver());
pDiskWriterDriver->setFileName( filename );

if ( pDiskWriterDriver->connect() != 0 ) {
ERRORLOG( "Error starting disk writer driver [DiskWriterDriver::connect()]" );
}

pDiskWriterDriver->write();
}

void Hydrogen::stopExportSong()
{
AudioEngine* pAudioEngine = m_pAudioEngine;

if ( pAudioEngine->getAudioDriver()->class_name() != DiskWriterDriver::_class_name() ) {
return;
}

pAudioEngine->getSampler()->stopPlayingNotes();
pAudioEngine->getAudioDriver()->disconnect();
pAudioEngine->reset();
}

Expand All @@ -668,7 +658,7 @@ void Hydrogen::stopExportSession()

pAudioEngine->startAudioDrivers();
if ( pAudioEngine->getAudioDriver() == nullptr ) {
ERRORLOG( "pAudioEngine->getAudioDriver() = nullptr" );
ERRORLOG( "Unable to restart previous audio driver after exporting song." );
}
m_bExportSessionIsActive = false;
}
Expand Down Expand Up @@ -1301,7 +1291,7 @@ bool Hydrogen::haveJackAudioDriver() const {
#ifdef H2CORE_HAVE_JACK
AudioEngine* pAudioEngine = m_pAudioEngine;
if ( pAudioEngine->getAudioDriver() != nullptr ) {
if ( JackAudioDriver::_class_name() == pAudioEngine->getAudioDriver()->class_name() ){
if ( dynamic_cast<JackAudioDriver*>(pAudioEngine->getAudioDriver()) != nullptr ) {
return true;
}
}
Expand All @@ -1315,7 +1305,7 @@ bool Hydrogen::haveJackTransport() const {
#ifdef H2CORE_HAVE_JACK
AudioEngine* pAudioEngine = m_pAudioEngine;
if ( pAudioEngine->getAudioDriver() != nullptr ) {
if ( JackAudioDriver::_class_name() == pAudioEngine->getAudioDriver()->class_name() &&
if ( dynamic_cast<JackAudioDriver*>(pAudioEngine->getAudioDriver()) != nullptr &&
Preferences::get_instance()->m_bJackTransportMode ==
Preferences::USE_JACK_TRANSPORT ){
return true;
Expand All @@ -1330,13 +1320,13 @@ bool Hydrogen::haveJackTransport() const {
float Hydrogen::getMasterBpm() const {
#ifdef H2CORE_HAVE_JACK
if ( m_pAudioEngine->getAudioDriver() != nullptr ) {
if ( JackAudioDriver::_class_name() == m_pAudioEngine->getAudioDriver()->class_name() ) {
return static_cast<JackAudioDriver*>(m_pAudioEngine->getAudioDriver())->getMasterBpm();
} else {
return std::nan("No JACK driver");
}
if ( dynamic_cast<JackAudioDriver*>(m_pAudioEngine->getAudioDriver()) != nullptr ) {
return static_cast<JackAudioDriver*>(m_pAudioEngine->getAudioDriver())->getMasterBpm();
} else {
return std::nan("No JACK driver");
}
} else {
return std::nan("No audio driver");
return std::nan("No audio driver");
}
#else
return std::nan("No JACK support");
Expand Down
19 changes: 8 additions & 11 deletions src/core/IO/DiskWriterDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,31 +263,28 @@ int DiskWriterDriver::init( unsigned nBufferSize )
INFOLOG( QString( "Init, buffer size: %1" ).arg( nBufferSize ) );

m_nBufferSize = nBufferSize;

m_pOut_L = new float[ m_nBufferSize ];
m_pOut_R = new float[ m_nBufferSize ];

return 0;
}


///
/// Connect
/// return 0: Ok
///
int DiskWriterDriver::connect()
{
return 0;
}

void DiskWriterDriver::write()
{
INFOLOG( "" );

m_pOut_L = new float[ m_nBufferSize ];
m_pOut_R = new float[ m_nBufferSize ];

pthread_attr_t attr;
pthread_attr_init( &attr );

pthread_create( &diskWriterDriverThread, &attr, diskWriterDriver_thread, this );

return 0;
}


/// disconnect
void DiskWriterDriver::disconnect()
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/IO/DiskWriterDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class DiskWriterDriver : public Object<DiskWriterDriver>, public AudioOutput
virtual int connect() override;
virtual void disconnect() override;

void write( float* buffer_L, float* buffer_R, unsigned int bufferSize );
void write();

virtual unsigned getBufferSize() override {
return m_nBufferSize;
Expand Down
2 changes: 1 addition & 1 deletion src/core/IO/OssDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void* ossDriver_processCaller( void* param )


OssDriver::OssDriver( audioProcessCallback processCallback )
: AudioOutput( __class_name )
: AudioOutput()
{
audioBuffer = NULL;
ossDriver_running = false;
Expand Down
1 change: 0 additions & 1 deletion src/gui/src/SongEditor/SongEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include <core/LocalFileMng.h>
#include <core/Timeline.h>
#include <core/Helpers/Xml.h>
#include <core/IO/DiskWriterDriver.h>
using namespace H2Core;

#include "UndoActions.h"
Expand Down

0 comments on commit ecc7ccb

Please sign in to comment.