pyepl (1.1.0-3) code/hardware/sound/eplSound.cpp

Summary

 code/hardware/sound/eplSound.cpp |  174 +++++++++++++++++++++++++++++++--------
 1 file changed, 141 insertions(+), 33 deletions(-)

    
download this patch

Patch contents

--- pyepl-1.1.0.orig/code/hardware/sound/eplSound.cpp
+++ pyepl-1.1.0/code/hardware/sound/eplSound.cpp
@@ -19,6 +19,9 @@
 		   unsigned int sampRate, unsigned int bufSize)
 {
   // get audio instance
+  RtAudio audio;
+  playAudio = NULL;
+  recAudio = NULL;
 
   // let's see warnings
   audio.showWarnings( true );
@@ -84,37 +87,53 @@
 
   try {
     // open the proper stream
-    if ((playChans>0) && (recChans>0))
+    if ((playChans>0) && (recChans>0) && (iParams.deviceId == oParams.deviceId))
     {
+      // it's duplex
+      isDuplex = 1;
+      
       // open both
-      audio.openStream( &oParams, &iParams, FORMAT, sampleRate, 
-			&bufferSize, &inout, (void *)data );
+      playAudio = new RtAudio();
+      playAudio->showWarnings( true );
+      playAudio->openStream( &oParams, &iParams, FORMAT, sampleRate, 
+			    &bufferSize, &inout, (void *)data );
+      recAudio = playAudio;
     }
-    else if (playChans>0)
+    else 
+    {
+      // not full duplex
+      isDuplex = 0;
+
+      if (playChans>0)
+      {
+	// open output
+	playAudio = new RtAudio();
+	playAudio->showWarnings( true );
+	playAudio->openStream( &oParams, NULL, FORMAT, sampleRate, 
+			       &bufferSize, &inout, (void *)data );
+      }
+      if (recChans>0)
+      {
+	// open input
+	recAudio = new RtAudio();
+	recAudio->showWarnings( true );
+	recAudio->openStream( NULL, &iParams, FORMAT, sampleRate, 
+			      &bufferSize, &inout, (void *)data );	
+      }
+    }
+
+    if (playChans==0)
     {
-      // open only output
-      audio.openStream( &oParams, NULL, FORMAT, sampleRate, 
-			&bufferSize, &inout, (void *)data );
       // No input chans discovered
       cerr << "No default input device with correct channel info was found!" << endl;
       cerr << "You will only be able to record sound." << endl;
     }
-    else if (recChans>0)
+    if (recChans==0)
     {
-      // open only input
-      audio.openStream( NULL, &iParams, FORMAT, sampleRate, 
-			&bufferSize, &inout, (void *)data );
-
       // No output chans discovered
       cerr << "No default output device with correct channel info was found!" << endl;
       cerr << "You will not be able to play sound." << endl;
     }
-    else
-    {
-      cerr << "No default i/o device with correct channel info was found!" << endl;
-      cerr << "You will not be able to play or record sound." << endl;
-      //exit( EXIT_FAILURE );
-    }
   }
   catch ( RtError& e ) {
     e.printMessage();
@@ -127,10 +146,27 @@
 
 eplSound::~eplSound()
 {
-  if (audio.isStreamOpen() > 0)
+  // stop streaming
+  stopstream(1);
+
+  // close the play stream
+  if (playAudio != NULL)
   {
-    stopstream();
-    audio.closeStream();
+    if (playAudio->isStreamOpen() > 0)
+      playAudio->closeStream();
+
+    // clean it up
+    delete playAudio;
+  }
+
+  // see if should clean up record stream
+  if ((!isDuplex) && (recAudio != NULL))
+  {
+    if (recAudio->isStreamOpen() > 0)
+      recAudio->closeStream();
+
+    // clean it up
+    delete recAudio;
   }
   
   delete data;
@@ -143,6 +179,18 @@
   return;
 }
 
+void eplSound::clearPlayBuffer()
+{
+  data->playBuf->clear();
+  return;
+}
+
+void eplSound::clearRecBuffer()
+{
+  data->recBuf->clear();
+  return;
+}
+
 int eplSound::recstart()
 {
 
@@ -171,31 +219,71 @@
   //if (!(audio.isStreamOpen()) && (streaming == 0))
   if (streaming == 0)
   {
-    try {
-      audio.startStream();
+    // start playing
+    if (playAudio != NULL)
+    {
+      try {
+	playAudio->startStream();
+      }
+      catch (RtError &error) {
+	error.printMessage();
+	exit(EXIT_FAILURE);
+      }
     }
-    catch (RtError &error) {
-      error.printMessage();
-      exit(EXIT_FAILURE);
+
+    if ((recAudio != NULL) && (recAudio != playAudio))
+    {
+      try {
+	recAudio->startStream();
+      }
+      catch (RtError &error) {
+	error.printMessage();
+	exit(EXIT_FAILURE);
+      }
     }
+
+    // say we are streaming
     streaming = 1;
   }
 
   return 0; 
 }
 
-int eplSound::stopstream()
+int eplSound::stopstream(int abort)
 {
   //if (audio.isStreamOpen() && streaming == 1))
   if (streaming == 1)
   {
-    try {
-      audio.stopStream();
+    // stop playing
+    if (playAudio != NULL)
+    {
+      try {
+	if (abort)
+	  playAudio->abortStream();
+	else
+	  playAudio->stopStream();
+      }
+      catch (RtError &error) {
+	error.printMessage();
+	exit(EXIT_FAILURE);
+      }
     }
-    catch (RtError &error) {
-      error.printMessage();
-      exit(EXIT_FAILURE);
+
+    // stop playing
+    if ((recAudio != NULL) && (recAudio != playAudio))
+    {
+      try {
+	if (abort)
+	  recAudio->abortStream();
+	else
+	  recAudio->stopStream();
+      }
+      catch (RtError &error) {
+	error.printMessage();
+	exit(EXIT_FAILURE);
+      }
     }
+
     streaming = 0;
     data->recording = 0;
   }
@@ -256,11 +344,31 @@
   return sampleRate;
 }
 
+unsigned int eplSound::getPlayStreamSampleRate()
+{
+  return playAudio->getStreamSampleRate();
+}
+
+unsigned int eplSound::getRecStreamSampleRate()
+{
+  return recAudio->getStreamSampleRate();
+}
+
 long eplSound::getBufferUsed()
 {
   return data->playBuf->getUsed();
 }
 
+long eplSound::getPlayStreamLatency()
+{
+  return playAudio->getStreamLatency();
+}
+
+long eplSound::getRecStreamLatency()
+{
+  return recAudio->getStreamLatency();
+}
+
 int inout( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
            double streamTime, RtAudioStreamStatus status, void *data )
 {
@@ -297,7 +405,7 @@
 	     sizeof(MY_TYPE)*((nBufferFrames*mydata->playChans)-written));
     }
   }
-  
+
   return 0;
 }