2121 */
2222#include " asioaudiodriver.h"
2323
24+ #include " global/async/notification.h"
25+
2426#include " ASIOSDK/common/asio.h"
2527#include " ASIOSDK/host/asiodrivers.h"
2628
@@ -30,6 +32,7 @@ using namespace muse;
3032using namespace muse ::audio;
3133
3234struct Data {
35+ // Drivers list
3336 AsioDrivers drivers;
3437
3538 // ASIOInit()
@@ -62,6 +65,9 @@ struct Data {
6265
6366 // ASIOCallbacks
6467 ASIOCallbacks callbacks;
68+
69+ // Reset
70+ async::Notification resetRequest;
6571};
6672
6773static Data s_data;
@@ -265,28 +271,29 @@ static ASIOTime* s_bufferSwitchTimeInfo(ASIOTime* /*params*/, long index, ASIOBo
265271 return nullptr ;
266272}
267273
274+ static void s_resetRequest ()
275+ {
276+ s_data.resetRequest .notify ();
277+ }
278+
268279static void s_sampleRateChanged (ASIOSampleRate rate)
269280{
270281 LOGI () << " rate: " << rate;
282+ s_resetRequest ();
271283}
272284
273- static long s_asioMessages (long selector, long value, void * message, double * /* opt*/ )
285+ static long s_asioMessages (long selector, long value, void * /* message*/ , double * /* opt*/ )
274286{
275287 LOGI () << " selector: " << selector
276- << " value: " << value
277- << " message: " << (const char *)message;
288+ << " value: " << value;
278289
279290 long ret = 0 ;
280291 switch (selector) {
281292 case kAsioSelectorSupported :
282293 if (value == kAsioEngineVersion
283- // || value == kAsioResetRequest
284- // || value == kAsioResyncRequest
285- // || value == kAsioLatenciesChanged
286- // // the following three were added for ASIO 2.0, you don't necessarily have to support them
287- // || value == kAsioSupportsTimeInfo
288- // || value == kAsioSupportsTimeCode
289- // || value == kAsioSupportsInputMonitor
294+ || value == kAsioResetRequest
295+ || value == kAsioResyncRequest
296+ || value == kAsioLatenciesChanged
290297 ) {
291298 ret = 1L ;
292299 }
@@ -296,22 +303,28 @@ static long s_asioMessages(long selector, long value, void* message, double* /*o
296303 // You cannot reset the driver right now, as this code is called from the driver.
297304 // Reset the driver is done by completely destruct is. I.e. ASIOStop(), ASIODisposeBuffers(), Destruction
298305 // Afterwards you initialize the driver again.
299- ret = 0L ;
306+ s_resetRequest ();
307+ ret = 1L ;
300308 break ;
309+ case kAsioBufferSizeChange :
310+ s_resetRequest ();
311+ ret = 1L ;
301312 case kAsioResyncRequest :
302313 // This informs the application, that the driver encountered some non fatal data loss.
303314 // It is used for synchronization purposes of different media.
304315 // Added mainly to work around the Win16Mutex problems in Windows 95/98 with the
305316 // Windows Multimedia system, which could loose data because the Mutex was hold too long
306317 // by another thread.
307318 // However a driver can issue it in other situations, too.
319+ s_resetRequest ();
308320 ret = 1L ;
309321 break ;
310322 case kAsioLatenciesChanged :
311323 // This will inform the host application that the drivers were latencies changed.
312324 // Beware, it this does not mean that the buffer sizes have changed!
313325 // You might need to update internal delay data.
314- ret = 0L ;
326+ s_resetRequest ();
327+ ret = 1L ;
315328 break ;
316329 case kAsioEngineVersion :
317330 // return the supported ASIO version of the host application
@@ -487,6 +500,10 @@ bool AsioAudioDriver::doOpen(const AudioDeviceID& device, const Spec& spec, Spec
487500 return ok;
488501 }
489502
503+ s_data.resetRequest .onNotify (this , [this ]() {
504+ reset ();
505+ }, async::Asyncable::Mode::SetReplace);
506+
490507 m_running = true ;
491508 m_thread = std::thread ([this ]() {
492509 bool ok = ASIOStart () == ASE_OK;
@@ -530,6 +547,18 @@ void AsioAudioDriver::close()
530547 s_data.drivers .removeCurrentDriver ();
531548}
532549
550+ void AsioAudioDriver::reset ()
551+ {
552+ LOGI () << " ! driver reset called" ;
553+ if (!isOpened ()) {
554+ return ;
555+ }
556+
557+ Spec spec = s_data.activeSpec ;
558+ close ();
559+ open (spec, nullptr );
560+ }
561+
533562bool AsioAudioDriver::isOpened () const
534563{
535564 return m_running;
0 commit comments