<font color="#000066">While trying to record 44100 Hz it records the file as record.raw and i can use the sample to test it. But when i try to record as 48000 sample rate or above, it does nothing.</font><div><font class="Apple-style-span" color="#000066"><br>
</font></div><div><font class="Apple-style-span" color="#000066"><div>uname -a : Linux example 2.6.32.12-114.fc12.i686 #1 SMP Tue Apr 27 21:29:58 UTC 2010 i686 i686 i386 GNU/Linux</div><div><br></div></font></div><div><font class="Apple-style-span" color="#000066"><br>
</font></div><div><font class="Apple-style-span" color="#000066">Here is my code record.cpp:</font></div><div><font class="Apple-style-span" color="#000066"><br></font></div><div><div><font class="Apple-style-span" color="#000066">/******************************************/</font></div>
<div><font class="Apple-style-span" color="#000066">/*</font></div><div><font class="Apple-style-span" color="#000066"> record.cpp</font></div><div><font class="Apple-style-span" color="#000066"> by Gary P. Scavone, 2007</font></div>
<div><font class="Apple-style-span" color="#000066"><br></font></div><div><font class="Apple-style-span" color="#000066"> This program records audio from a device and writes it to a</font></div><div><font class="Apple-style-span" color="#000066"> header-less binary file. Use the 'playraw', with the same</font></div>
<div><font class="Apple-style-span" color="#000066"> parameters and format settings, to playback the audio.</font></div><div><font class="Apple-style-span" color="#000066">*/</font></div><div><font class="Apple-style-span" color="#000066">/******************************************/</font></div>
<div><font class="Apple-style-span" color="#000066"><br></font></div><div><font class="Apple-style-span" color="#000066">#include "RtAudio.h"</font></div><div><font class="Apple-style-span" color="#000066">#include <iostream></font></div>
<div><font class="Apple-style-span" color="#000066">#include <cstdlib></font></div><div><font class="Apple-style-span" color="#000066">#include <cstring></font></div><div><font class="Apple-style-span" color="#000066">#include <stdio.h></font></div>
<div><font class="Apple-style-span" color="#000066"><br></font></div><div><font class="Apple-style-span" color="#000066">/*</font></div><div><font class="Apple-style-span" color="#000066">typedef char MY_TYPE;</font></div>
<div><font class="Apple-style-span" color="#000066">#define FORMAT RTAUDIO_SINT8</font></div><div><font class="Apple-style-span" color="#000066">*/</font></div><div><font class="Apple-style-span" color="#000066"><br></font></div>
<div><font class="Apple-style-span" color="#000066">typedef signed short MY_TYPE;</font></div><div><font class="Apple-style-span" color="#000066">#define FORMAT RTAUDIO_SINT16</font></div><div><font class="Apple-style-span" color="#000066"><br>
</font></div><div><font class="Apple-style-span" color="#000066">/*</font></div><div><font class="Apple-style-span" color="#000066">typedef signed long MY_TYPE;</font></div><div><font class="Apple-style-span" color="#000066">#define FORMAT RTAUDIO_SINT24</font></div>
<div><font class="Apple-style-span" color="#000066"><br></font></div><div><font class="Apple-style-span" color="#000066">typedef signed long MY_TYPE;</font></div><div><font class="Apple-style-span" color="#000066">#define FORMAT RTAUDIO_SINT32</font></div>
<div><font class="Apple-style-span" color="#000066"><br></font></div><div><font class="Apple-style-span" color="#000066">typedef float MY_TYPE;</font></div><div><font class="Apple-style-span" color="#000066">#define FORMAT RTAUDIO_FLOAT32</font></div>
<div><font class="Apple-style-span" color="#000066"><br></font></div><div><font class="Apple-style-span" color="#000066">typedef double MY_TYPE;</font></div><div><font class="Apple-style-span" color="#000066">#define FORMAT RTAUDIO_FLOAT64</font></div>
<div><font class="Apple-style-span" color="#000066">*/</font></div><div><font class="Apple-style-span" color="#000066"><br></font></div><div><font class="Apple-style-span" color="#000066">// Platform-dependent sleep routines.</font></div>
<div><font class="Apple-style-span" color="#000066">#if defined( __WINDOWS_ASIO__ ) || defined( __WINDOWS_DS__ )</font></div><div><font class="Apple-style-span" color="#000066"> #include <windows.h></font></div><div>
<font class="Apple-style-span" color="#000066"> #define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds ) </font></div><div><font class="Apple-style-span" color="#000066">#else // Unix variants</font></div><div><font class="Apple-style-span" color="#000066"> #include <unistd.h></font></div>
<div><font class="Apple-style-span" color="#000066"> #define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )</font></div><div><font class="Apple-style-span" color="#000066">#endif</font></div><div>
<font class="Apple-style-span" color="#000066"><br></font></div><div><font class="Apple-style-span" color="#000066">void usage( void ) {</font></div><div><font class="Apple-style-span" color="#000066"> // Error function in case of incorrect command-line</font></div>
<div><font class="Apple-style-span" color="#000066"> // argument specifications</font></div><div><font class="Apple-style-span" color="#000066"> std::cout << "\nuseage: record N fs <duration> <device> <channelOffset>\n";</font></div>
<div><font class="Apple-style-span" color="#000066"> std::cout << " where N = number of channels,\n";</font></div><div><font class="Apple-style-span" color="#000066"> std::cout << " fs = the sample rate,\n";</font></div>
<div><font class="Apple-style-span" color="#000066"> std::cout << " duration = optional time in seconds to record (default = 2.0),\n";</font></div><div><font class="Apple-style-span" color="#000066"> std::cout << " device = optional device to use (default = 0),\n";</font></div>
<div><font class="Apple-style-span" color="#000066"> std::cout << " and channelOffset = an optional channel offset on the device (default = 0).\n\n";</font></div><div><font class="Apple-style-span" color="#000066"> exit( 0 );</font></div>
<div><font class="Apple-style-span" color="#000066">}</font></div><div><font class="Apple-style-span" color="#000066"><br></font></div><div><font class="Apple-style-span" color="#000066">struct InputData {</font></div><div>
<font class="Apple-style-span" color="#000066"> MY_TYPE* buffer;</font></div><div><font class="Apple-style-span" color="#000066"> unsigned long bufferBytes;</font></div><div><font class="Apple-style-span" color="#000066"> unsigned long totalFrames;</font></div>
<div><font class="Apple-style-span" color="#000066"> unsigned long frameCounter;</font></div><div><font class="Apple-style-span" color="#000066"> unsigned int channels;</font></div><div><font class="Apple-style-span" color="#000066">};</font></div>
<div><font class="Apple-style-span" color="#000066"><br></font></div><div><font class="Apple-style-span" color="#000066">// Interleaved buffers</font></div><div><font class="Apple-style-span" color="#000066">int input( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,</font></div>
<div><font class="Apple-style-span" color="#000066"> double streamTime, RtAudioStreamStatus status, void *data )</font></div><div><font class="Apple-style-span" color="#000066">{</font></div><div><font class="Apple-style-span" color="#000066"> InputData *iData = (InputData *) data;</font></div>
<div><font class="Apple-style-span" color="#000066"><br></font></div><div><font class="Apple-style-span" color="#000066"> // Simply copy the data to our allocated buffer.</font></div><div><font class="Apple-style-span" color="#000066"> unsigned int frames = nBufferFrames;</font></div>
<div><font class="Apple-style-span" color="#000066"> if ( iData->frameCounter + nBufferFrames > iData->totalFrames ) {</font></div><div><font class="Apple-style-span" color="#000066"> frames = iData->totalFrames - iData->frameCounter;</font></div>
<div><font class="Apple-style-span" color="#000066"> iData->bufferBytes = frames * iData->channels * sizeof( MY_TYPE );</font></div><div><font class="Apple-style-span" color="#000066"> }</font></div><div><font class="Apple-style-span" color="#000066"><br>
</font></div><div><font class="Apple-style-span" color="#000066"> unsigned long offset = iData->frameCounter * iData->channels;</font></div><div><font class="Apple-style-span" color="#000066"> memcpy( iData->buffer+offset, inputBuffer, iData->bufferBytes );</font></div>
<div><font class="Apple-style-span" color="#000066"> iData->frameCounter += frames;</font></div><div><font class="Apple-style-span" color="#000066"><br></font></div><div><font class="Apple-style-span" color="#000066"> if ( iData->frameCounter >= iData->totalFrames ) return 2;</font></div>
<div><font class="Apple-style-span" color="#000066"> return 0;</font></div><div><font class="Apple-style-span" color="#000066">}</font></div><div><font class="Apple-style-span" color="#000066"><br></font></div><div><font class="Apple-style-span" color="#000066">int main( int argc, char *argv[] )</font></div>
<div><font class="Apple-style-span" color="#000066">{</font></div><div><font class="Apple-style-span" color="#000066"> unsigned int channels, fs, bufferFrames, device = 0, offset = 0;</font></div><div><font class="Apple-style-span" color="#000066"> double time = 2.0;</font></div>
<div><font class="Apple-style-span" color="#000066"> FILE *fd;</font></div><div><font class="Apple-style-span" color="#000066"><br></font></div><div><font class="Apple-style-span" color="#000066"> // minimal command-line checking</font></div>
<div><font class="Apple-style-span" color="#000066"> if ( argc < 3 || argc > 6 ) usage();</font></div><div><font class="Apple-style-span" color="#000066"><br></font></div><div><font class="Apple-style-span" color="#000066"> RtAudio adc;</font></div>
<div><font class="Apple-style-span" color="#000066"> if ( adc.getDeviceCount() < 1 ) {</font></div><div><font class="Apple-style-span" color="#000066"> std::cout << "\nNo audio devices found!\n";</font></div>
<div><font class="Apple-style-span" color="#000066"> exit( 1 );</font></div><div><font class="Apple-style-span" color="#000066"> }</font></div><div><font class="Apple-style-span" color="#000066"><br></font></div><div>
<font class="Apple-style-span" color="#000066"> channels = (unsigned int) atoi( argv[1] );</font></div><div><font class="Apple-style-span" color="#000066"> fs = (unsigned int) atoi( argv[2] );</font></div><div><font class="Apple-style-span" color="#000066"> if ( argc > 3 )</font></div>
<div><font class="Apple-style-span" color="#000066"> time = (double) atof( argv[3] );</font></div><div><font class="Apple-style-span" color="#000066"> if ( argc > 4 )</font></div><div><font class="Apple-style-span" color="#000066"> device = (unsigned int) atoi( argv[4] );</font></div>
<div><font class="Apple-style-span" color="#000066"> if ( argc > 5 )</font></div><div><font class="Apple-style-span" color="#000066"> offset = (unsigned int) atoi( argv[5] );</font></div><div><font class="Apple-style-span" color="#000066"><br>
</font></div><div><font class="Apple-style-span" color="#000066"> // Let RtAudio print messages to stderr.</font></div><div><font class="Apple-style-span" color="#000066"> adc.showWarnings( true );</font></div><div><font class="Apple-style-span" color="#000066"><br>
</font></div><div><font class="Apple-style-span" color="#000066"> // Set our stream parameters for input only.</font></div><div><font class="Apple-style-span" color="#000066"> bufferFrames = 512;</font></div><div><font class="Apple-style-span" color="#000066"> RtAudio::StreamParameters iParams;</font></div>
<div><font class="Apple-style-span" color="#000066"> iParams.deviceId = device;</font></div><div><font class="Apple-style-span" color="#000066"> iParams.nChannels = channels;</font></div><div><font class="Apple-style-span" color="#000066"> iParams.firstChannel = offset;</font></div>
<div><font class="Apple-style-span" color="#000066"><br></font></div><div><font class="Apple-style-span" color="#000066"> InputData data;</font></div><div><font class="Apple-style-span" color="#000066"> data.buffer = 0;</font></div>
<div><font class="Apple-style-span" color="#000066"> try {</font></div><div><font class="Apple-style-span" color="#000066"> adc.openStream( NULL, &iParams, FORMAT, fs, &bufferFrames, &input, (void *)&data );</font></div>
<div><font class="Apple-style-span" color="#000066"> }</font></div><div><font class="Apple-style-span" color="#000066"> catch ( RtError& e ) {</font></div><div><font class="Apple-style-span" color="#000066"> std::cout << '\n' << e.getMessage() << '\n' << std::endl;</font></div>
<div><font class="Apple-style-span" color="#000066"> goto cleanup;</font></div><div><font class="Apple-style-span" color="#000066"> }</font></div><div><font class="Apple-style-span" color="#000066"><br></font></div><div>
<font class="Apple-style-span" color="#000066"> data.bufferBytes = bufferFrames * channels * sizeof( MY_TYPE );</font></div><div><font class="Apple-style-span" color="#000066"> data.totalFrames = (unsigned long) (fs * time);</font></div>
<div><font class="Apple-style-span" color="#000066"> data.frameCounter = 0;</font></div><div><font class="Apple-style-span" color="#000066"> data.channels = channels;</font></div><div><font class="Apple-style-span" color="#000066"> unsigned long totalBytes;</font></div>
<div><font class="Apple-style-span" color="#000066"> totalBytes = data.totalFrames * channels * sizeof( MY_TYPE );</font></div><div><font class="Apple-style-span" color="#000066"><br></font></div><div><font class="Apple-style-span" color="#000066"> // Allocate the entire data buffer before starting stream.</font></div>
<div><font class="Apple-style-span" color="#000066"> data.buffer = (MY_TYPE*) malloc( totalBytes );</font></div><div><font class="Apple-style-span" color="#000066"> if ( data.buffer == 0 ) {</font></div><div><font class="Apple-style-span" color="#000066"> std::cout << "Memory allocation error ... quitting!\n";</font></div>
<div><font class="Apple-style-span" color="#000066"> goto cleanup;</font></div><div><font class="Apple-style-span" color="#000066"> }</font></div><div><font class="Apple-style-span" color="#000066"><br></font></div><div>
<font class="Apple-style-span" color="#000066"> try {</font></div><div><font class="Apple-style-span" color="#000066"> adc.startStream();</font></div><div><font class="Apple-style-span" color="#000066"> }</font></div>
<div><font class="Apple-style-span" color="#000066"> catch ( RtError& e ) {</font></div><div><font class="Apple-style-span" color="#000066"> std::cout << '\n' << e.getMessage() << '\n' << std::endl;</font></div>
<div><font class="Apple-style-span" color="#000066"> goto cleanup;</font></div><div><font class="Apple-style-span" color="#000066"> }</font></div><div><font class="Apple-style-span" color="#000066"><br></font></div><div>
<font class="Apple-style-span" color="#000066"> std::cout << "\nRecording for " << time << " seconds ... writing file 'record.raw' (buffer frames = " << bufferFrames << ")." << std::endl;</font></div>
<div><font class="Apple-style-span" color="#000066"> while ( adc.isStreamRunning() ) {</font></div><div><font class="Apple-style-span" color="#000066"> SLEEP( 100 ); // wake every 100 ms to check if we're done</font></div>
<div><font class="Apple-style-span" color="#000066"> }</font></div><div><font class="Apple-style-span" color="#000066"><br></font></div><div><font class="Apple-style-span" color="#000066"> // Now write the entire data to the file.</font></div>
<div><font class="Apple-style-span" color="#000066"> fd = fopen( "record.raw", "wb" );</font></div><div><font class="Apple-style-span" color="#000066"> fwrite( data.buffer, sizeof( MY_TYPE ), data.totalFrames * channels, fd );</font></div>
<div><font class="Apple-style-span" color="#000066"> fclose( fd );</font></div><div><font class="Apple-style-span" color="#000066"><br></font></div><div><font class="Apple-style-span" color="#000066"> cleanup:</font></div>
<div><font class="Apple-style-span" color="#000066"> if ( adc.isStreamOpen() ) adc.closeStream();</font></div><div><font class="Apple-style-span" color="#000066"> if ( data.buffer ) free( data.buffer );</font></div><div>
<font class="Apple-style-span" color="#000066"><br></font></div><div><font class="Apple-style-span" color="#000066"> return 0;</font></div><div><font class="Apple-style-span" color="#000066">}</font></div><div style="color: rgb(0, 0, 102); ">
<br></div></div>