Updates to playsaw.cpp and testall.cpp for new behaviour
authorGary Scavone <gary@music.mcgill.ca>
Fri, 5 Apr 2019 19:47:28 +0000 (15:47 -0400)
committerGary Scavone <gary@music.mcgill.ca>
Fri, 19 Apr 2019 19:11:35 +0000 (15:11 -0400)
RtAudio.cpp
tests/playsaw.cpp
tests/testall.cpp

index f930114cf2e15131ff1ac87a2d6cfef62773032a..fd8380b63d79c303a03fa402e6257e0b84349793 100644 (file)
@@ -1439,9 +1439,9 @@ bool RtApiCore :: probeDeviceOpen( unsigned int device, StreamMode mode, unsigne
 
   // Setup a listener to detect a possible device disconnect.
   property.mSelector = kAudioDevicePropertyDeviceIsAlive;
-  property.mScope = kAudioObjectPropertyScopeGlobal;
   result = AudioObjectAddPropertyListener( id , &property, disconnectListener, (void *) &stream_.callbackInfo );
   if ( result != noErr ) {
+    AudioObjectRemovePropertyListener( id, &property, xrunListener, (void *) handle );
     errorStream_ << "RtApiCore::probeDeviceOpen: system error setting disconnect listener for device (" << device << ").";
     errorText_ = errorStream_.str();
     goto error;
@@ -1489,9 +1489,13 @@ void RtApiCore :: closeStream( void )
         kAudioObjectPropertyElementMaster };
 
       property.mSelector = kAudioDeviceProcessorOverload;
-      property.mScope = kAudioObjectPropertyScopeGlobal;
       if (AudioObjectRemovePropertyListener( handle->id[0], &property, xrunListener, (void *) handle ) != noErr) {
-        errorText_ = "RtApiCore::closeStream(): error removing property listener!";
+        errorText_ = "RtApiCore::closeStream(): error removing xrun property listener!";
+        error( RtAudioError::WARNING );
+      }
+      property.mSelector = kAudioDevicePropertyDeviceIsAlive;
+      if (AudioObjectRemovePropertyListener( handle->id[0], &property, disconnectListener, (void *) &stream_.callbackInfo ) != noErr) {
+        errorText_ = "RtApiCore::closeStream(): error removing disconnect property listener!";
         error( RtAudioError::WARNING );
       }
     }
@@ -1512,9 +1516,13 @@ void RtApiCore :: closeStream( void )
         kAudioObjectPropertyElementMaster };
 
       property.mSelector = kAudioDeviceProcessorOverload;
-      property.mScope = kAudioObjectPropertyScopeGlobal;
       if (AudioObjectRemovePropertyListener( handle->id[1], &property, xrunListener, (void *) handle ) != noErr) {
-        errorText_ = "RtApiCore::closeStream(): error removing property listener!";
+        errorText_ = "RtApiCore::closeStream(): error removing xrun property listener!";
+        error( RtAudioError::WARNING );
+      }
+      property.mSelector = kAudioDevicePropertyDeviceIsAlive;
+      if (AudioObjectRemovePropertyListener( handle->id[1], &property, disconnectListener, (void *) &stream_.callbackInfo ) != noErr) {
+        errorText_ = "RtApiCore::closeStream(): error removing disconnect property listener!";
         error( RtAudioError::WARNING );
       }
     }
index 41c4e26488431688451a660e7b3f932d8d58b0c0..beaf714a4c220ef16fe89ceca9e956d2dc48361e 100644 (file)
@@ -68,11 +68,7 @@ void errorCallback( RtAudioError::Type /*type*/, const std::string &errorText )
 {
   // This example error handling function does exactly the same thing
   // as the embedded RtAudio::error() function.
-  std::cout << "in errorCallback" << std::endl;
-  //if ( type == RtAudioError::WARNING )
-    std::cerr << '\n' << errorText << "\n\n";
-    //else if ( type != RtAudioError::WARNING )
-    //throw( RtAudioError( errorText, type ) );
+  std::cerr << "\nerrorCallback: " << errorText << "\n\n";
 }
 
 unsigned int channels;
@@ -80,15 +76,15 @@ RtAudio::StreamOptions options;
 unsigned int frameCounter = 0;
 bool checkCount = false;
 unsigned int nFrames = 0;
-const unsigned int callbackReturnValue = 2;
+const unsigned int callbackReturnValue = 1; // 1 = stop and drain, 2 = abort
 double streamTimePrintIncrement = 1.0; // seconds
 double streamTimePrintTime = 1.0; // seconds
 
-//#define USE_INTERLEAVED
+#define USE_INTERLEAVED
 #if defined( USE_INTERLEAVED )
 
 // Interleaved buffers
-int saw( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
+int saw( void *outputBuffer, void * /*inputBuffer*/, unsigned int nBufferFrames,
          double streamTime, RtAudioStreamStatus status, void *data )
 {
   unsigned int i, j;
@@ -194,15 +190,12 @@ int main( int argc, char *argv[] )
 #if !defined( USE_INTERLEAVED )
   options.flags |= RTAUDIO_NONINTERLEAVED;
 #endif
-  //  try {
-    dac.openStream( &oParams, NULL, FORMAT, fs, &bufferFrames, &saw, (void *)data, &options, &errorCallback );
-    if ( dac.isStreamOpen() == false ) goto cleanup;
-    dac.startStream();
-    //  }
-    //  catch ( RtAudioError& e ) {
-    //    e.printMessage();
-    //goto cleanup;
-    //  }
+
+  dac.openStream( &oParams, NULL, FORMAT, fs, &bufferFrames, &saw, (void *)data, &options, &errorCallback );
+  if ( dac.isStreamOpen() == false ) goto cleanup;
+
+  // Stream is open ... now start it.
+  dac.startStream();
 
   if ( checkCount ) {
     while ( dac.isStreamRunning() == true ) SLEEP( 100 );
@@ -213,17 +206,9 @@ int main( int argc, char *argv[] )
     std::cout << "\nPlaying ... press <enter> to quit (buffer size = " << bufferFrames << ").\n";
     std::cin.get( input );
 
-    //try {
-      // Stop the stream
-      dac.stopStream();
-      //dac.abortStream();
-  }
-      /*
-    catch ( RtAudioError& e ) {
-      e.printMessage();
-    }
+    // Block released ... stop the stream
+    dac.stopStream();  // or could call dac.abortStream();
   }
-      */
 
  cleanup:
   if ( dac.isStreamOpen() ) dac.closeStream();
index b94ce696d194114e1f36db55fe8a46e37134af13..cd6db4488069c02cb5fda6bedf9e9cd318720244 100644 (file)
@@ -133,10 +133,11 @@ int main( int argc, char *argv[] )
 
   RtAudio::StreamOptions options;
   options.flags = RTAUDIO_HOG_DEVICE;
-  try {
+  //try {
     dac.openStream( &oParams, NULL, RTAUDIO_FLOAT64, fs, &bufferFrames, &sawi, (void *)data, &options );
     std::cout << "\nStream latency = " << dac.getStreamLatency() << std::endl;
 
+    if ( !dac.isStreamOpen() ) goto cleanup;
     // Start the stream
     dac.startStream();
     std::cout << "\nPlaying ... press <enter> to stop.\n";
@@ -162,11 +163,13 @@ int main( int argc, char *argv[] )
 
     std::cout << "Playing again ... press <enter> to close the stream.\n";
     std::cin.get( input );
+    /*
   }
   catch ( RtAudioError& e ) {
     e.printMessage();
     goto cleanup;
   }
+    */
 
   if ( dac.isStreamOpen() ) dac.closeStream();
 
@@ -175,6 +178,8 @@ int main( int argc, char *argv[] )
   try {
     dac.openStream( &oParams, NULL, RTAUDIO_FLOAT64, fs, &bufferFrames, &sawni, (void *)data, &options );
 
+    if ( !dac.isStreamOpen() ) goto cleanup;
+    
     std::cout << "Press <enter> to start non-interleaved playback.\n";
     std::cin.get( input );
 
@@ -200,7 +205,8 @@ int main( int argc, char *argv[] )
   options.flags = RTAUDIO_NONINTERLEAVED;
   try {
     dac.openStream( &oParams, &iParams, RTAUDIO_SINT32, fs, &bufferFrames, &inout, (void *)&bufferBytes, &options );
-
+    if ( !dac.isStreamOpen() ) goto cleanup;
+    
     bufferBytes = bufferFrames * channels * 4;
 
     std::cout << "Press <enter> to start duplex operation.\n";