changeset 19708:b3363c531662

Stop audioplayer and audiorecorder when done or interrupted * audiodevinfo.cc (audioplayer::playblocking, audiorecorder::recordblocking): Use unwind_protect to stop stream.
author Mike Miller <mtmiller@ieee.org>
date Sat, 07 Feb 2015 19:38:44 -0500
parents cdf9c83b0e79
children 793d295fed4d
files libinterp/dldfcn/audiodevinfo.cc
diffstat 1 files changed, 21 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/dldfcn/audiodevinfo.cc	Sun Feb 01 19:20:40 2015 -0500
+++ b/libinterp/dldfcn/audiodevinfo.cc	Sat Feb 07 19:38:44 2015 -0500
@@ -41,6 +41,7 @@
 #include "ov-int32.h"
 #include "ov-struct.h"
 #include "parse.h"
+#include "unwind-prot.h"
 
 #if defined (HAVE_PORTAUDIO)
 
@@ -846,6 +847,12 @@
   return paContinue;
 }
 
+static void
+safe_audioplayer_stop (audioplayer *player)
+{
+  player->stop ();
+}
+
 audioplayer::audioplayer (void)
   : octave_callback_function (0),
     id (-1), fs (0), nbits (16), channels (0), sample_number (0),
@@ -1151,6 +1158,10 @@
   start = get_sample_number ();
   end = get_end_sample ();
 
+  unwind_protect frame;
+
+  frame.add_fcn (safe_audioplayer_stop, this);
+
   for (unsigned int i = start; i < end; i += buffer_size)
     {
       OCTAVE_QUIT;
@@ -1161,24 +1172,6 @@
 
       err = Pa_WriteStream (stream, buffer, buffer_size);
     }
-
-  err = Pa_StopStream (stream);
-  if (err != paNoError)
-    {
-      error ("audioplayer: failed to stop audio playback stream");
-      return;
-    }
-
-  err = Pa_CloseStream (stream);
-  if (err != paNoError)
-    {
-      error ("audioplayer: failed to close audio playback stream");
-      return;
-    }
-
-  stream = 0;
-  set_sample_number (0);
-  reset_end_sample ();
 }
 
 void
@@ -1534,6 +1527,12 @@
   return paContinue;
 }
 
+static void
+safe_audiorecorder_stop (audiorecorder *recorder)
+{
+  recorder->stop ();
+}
+
 audiorecorder::audiorecorder (void)
   : octave_callback_function (0),
     id (-1), fs (44100), nbits (16), channels (2), sample_number (0),
@@ -1826,6 +1825,10 @@
 
   unsigned int frames = seconds * get_fs ();
 
+  unwind_protect frame;
+
+  frame.add_fcn (safe_audiorecorder_stop, this);
+
   for (unsigned int i = 0; i < frames; i += buffer_size)
     {
       OCTAVE_QUIT;