Merge remote-tracking branch 'upstream/pr/136'
[rtaudio-cdist.git] / tests / record.cpp
index f43db19fb5179e0512813cbbaf6e4b53825194c2..0e48f77e02dfe87e7cf521cb56837c3e61a7c441 100644 (file)
 
 #include "RtAudio.h"
 #include <iostream>
+#include <cstdlib>
+#include <cstring>
+#include <stdio.h>
 
 /*
-typedef char  MY_TYPE;
+typedef char MY_TYPE;
 #define FORMAT RTAUDIO_SINT8
+*/
 
-typedef signed short  MY_TYPE;
+typedef signed short MY_TYPE;
 #define FORMAT RTAUDIO_SINT16
 
-typedef signed long  MY_TYPE;
+/*
+typedef S24 MY_TYPE;
 #define FORMAT RTAUDIO_SINT24
 
-typedef signed long  MY_TYPE;
+typedef signed long MY_TYPE;
 #define FORMAT RTAUDIO_SINT32
-*/
 
-typedef float  MY_TYPE;
+typedef float MY_TYPE;
 #define FORMAT RTAUDIO_FLOAT32
 
-/*
-typedef double  MY_TYPE;
+typedef double MY_TYPE;
 #define FORMAT RTAUDIO_FLOAT64
 */
 
 // Platform-dependent sleep routines.
-#if defined( __WINDOWS_ASIO__ ) || defined( __WINDOWS_DS__ )
+#if defined( __WINDOWS_ASIO__ ) || defined( __WINDOWS_DS__ ) || defined( __WINDOWS_WASAPI__ )
   #include <windows.h>
   #define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds ) 
 #else // Unix variants
@@ -64,8 +67,8 @@ struct InputData {
 };
 
 // Interleaved buffers
-int input( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
-           double streamTime, RtAudioStreamStatus status, void *data )
+int input( void * /*outputBuffer*/, void *inputBuffer, unsigned int nBufferFrames,
+           double /*streamTime*/, RtAudioStreamStatus /*status*/, void *data )
 {
   InputData *iData = (InputData *) data;
 
@@ -96,7 +99,7 @@ int main( int argc, char *argv[] )
   RtAudio adc;
   if ( adc.getDeviceCount() < 1 ) {
     std::cout << "\nNo audio devices found!\n";
-    exit( 0 );
+    exit( 1 );
   }
 
   channels = (unsigned int) atoi( argv[1] );
@@ -114,7 +117,10 @@ int main( int argc, char *argv[] )
   // Set our stream parameters for input only.
   bufferFrames = 512;
   RtAudio::StreamParameters iParams;
-  iParams.deviceId = device;
+  if ( device == 0 )
+    iParams.deviceId = adc.getDefaultInputDevice();
+  else
+    iParams.deviceId = device;
   iParams.nChannels = channels;
   iParams.firstChannel = offset;
 
@@ -123,7 +129,7 @@ int main( int argc, char *argv[] )
   try {
     adc.openStream( NULL, &iParams, FORMAT, fs, &bufferFrames, &input, (void *)&data );
   }
-  catch ( RtError& e ) {
+  catch ( RtAudioError& e ) {
     std::cout << '\n' << e.getMessage() << '\n' << std::endl;
     goto cleanup;
   }
@@ -145,21 +151,20 @@ int main( int argc, char *argv[] )
   try {
     adc.startStream();
   }
-  catch ( RtError& e ) {
+  catch ( RtAudioError& e ) {
     std::cout << '\n' << e.getMessage() << '\n' << std::endl;
     goto cleanup;
   }
 
   std::cout << "\nRecording for " << time << " seconds ... writing file 'record.raw' (buffer frames = " << bufferFrames << ")." << std::endl;
-  while ( 1 ) {
+  while ( adc.isStreamRunning() ) {
     SLEEP( 100 ); // wake every 100 ms to check if we're done
-    if ( adc.isStreamRunning() == false ) break;
   }
 
   // Now write the entire data to the file.
   fd = fopen( "record.raw", "wb" );
   fwrite( data.buffer, sizeof( MY_TYPE ), data.totalFrames * channels, fd );
-  fclose(fd);
+  fclose( fd );
 
  cleanup:
   if ( adc.isStreamOpen() ) adc.closeStream();