1 /******************************************/
\r
3 RtAudio - realtime sound I/O C++ class
\r
4 by Gary P. Scavone, 2001-2002.
\r
6 /******************************************/
\r
12 // Static variable definitions.
\r
13 const unsigned int RtAudio :: SAMPLE_RATES[] = {
\r
14 4000, 5512, 8000, 9600, 11025, 16000, 22050,
\r
15 32000, 44100, 48000, 88200, 96000, 176400, 192000
\r
17 const RtAudio::RTAUDIO_FORMAT RtAudio :: RTAUDIO_SINT8 = 1;
\r
18 const RtAudio::RTAUDIO_FORMAT RtAudio :: RTAUDIO_SINT16 = 2;
\r
19 const RtAudio::RTAUDIO_FORMAT RtAudio :: RTAUDIO_SINT24 = 4;
\r
20 const RtAudio::RTAUDIO_FORMAT RtAudio :: RTAUDIO_SINT32 = 8;
\r
21 const RtAudio::RTAUDIO_FORMAT RtAudio :: RTAUDIO_FLOAT32 = 16;
\r
22 const RtAudio::RTAUDIO_FORMAT RtAudio :: RTAUDIO_FLOAT64 = 32;
\r
24 #if defined(__WINDOWS_DS__)
\r
25 #define MUTEX_INITIALIZE(A) InitializeCriticalSection(A)
\r
26 #define MUTEX_LOCK(A) EnterCriticalSection(A)
\r
27 #define MUTEX_UNLOCK(A) LeaveCriticalSection(A)
\r
28 typedef unsigned THREAD_RETURN;
\r
29 typedef unsigned (__stdcall THREAD_FUNCTION)(void *);
\r
30 #else // pthread API
\r
31 #define MUTEX_INITIALIZE(A) pthread_mutex_init(A, NULL)
\r
32 #define MUTEX_LOCK(A) pthread_mutex_lock(A)
\r
33 #define MUTEX_UNLOCK(A) pthread_mutex_unlock(A)
\r
34 typedef void * THREAD_RETURN;
\r
37 // *************************************************** //
\r
39 // Public common (OS-independent) methods.
\r
41 // *************************************************** //
\r
43 RtAudio :: RtAudio()
\r
47 if (nDevices <= 0) {
\r
48 sprintf(message, "RtAudio: no audio devices found!");
\r
49 error(RtError::NO_DEVICES_FOUND);
\r
53 RtAudio :: RtAudio(int *streamId,
\r
54 int outputDevice, int outputChannels,
\r
55 int inputDevice, int inputChannels,
\r
56 RTAUDIO_FORMAT format, int sampleRate,
\r
57 int *bufferSize, int numberOfBuffers)
\r
61 if (nDevices <= 0) {
\r
62 sprintf(message, "RtAudio: no audio devices found!");
\r
63 error(RtError::NO_DEVICES_FOUND);
\r
67 *streamId = openStream(outputDevice, outputChannels, inputDevice, inputChannels,
\r
68 format, sampleRate, bufferSize, numberOfBuffers);
\r
70 catch (RtError &exception) {
\r
71 // deallocate the RTAUDIO_DEVICE structures
\r
72 if (devices) free(devices);
\r
73 error(exception.getType());
\r
77 RtAudio :: ~RtAudio()
\r
79 // close any existing streams
\r
80 while ( streams.size() )
\r
81 closeStream( streams.begin()->first );
\r
83 // deallocate the RTAUDIO_DEVICE structures
\r
84 if (devices) free(devices);
\r
87 int RtAudio :: openStream(int outputDevice, int outputChannels,
\r
88 int inputDevice, int inputChannels,
\r
89 RTAUDIO_FORMAT format, int sampleRate,
\r
90 int *bufferSize, int numberOfBuffers)
\r
92 static int streamKey = 0; // Unique stream identifier ... OK for multiple instances.
\r
94 if (outputChannels < 1 && inputChannels < 1) {
\r
95 sprintf(message,"RtAudio: one or both 'channel' parameters must be greater than zero.");
\r
96 error(RtError::INVALID_PARAMETER);
\r
99 if ( formatBytes(format) == 0 ) {
\r
100 sprintf(message,"RtAudio: 'format' parameter value is undefined.");
\r
101 error(RtError::INVALID_PARAMETER);
\r
104 if ( outputChannels > 0 ) {
\r
105 if (outputDevice >= nDevices || outputDevice < 0) {
\r
106 sprintf(message,"RtAudio: 'outputDevice' parameter value (%d) is invalid.", outputDevice);
\r
107 error(RtError::INVALID_PARAMETER);
\r
111 if ( inputChannels > 0 ) {
\r
112 if (inputDevice >= nDevices || inputDevice < 0) {
\r
113 sprintf(message,"RtAudio: 'inputDevice' parameter value (%d) is invalid.", inputDevice);
\r
114 error(RtError::INVALID_PARAMETER);
\r
118 // Allocate a new stream structure.
\r
119 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) calloc(1, sizeof(RTAUDIO_STREAM));
\r
120 if (stream == NULL) {
\r
121 sprintf(message, "RtAudio: memory allocation error!");
\r
122 error(RtError::MEMORY_ERROR);
\r
124 streams[++streamKey] = (void *) stream;
\r
125 stream->mode = UNINITIALIZED;
\r
126 MUTEX_INITIALIZE(&stream->mutex);
\r
128 bool result = SUCCESS;
\r
132 if ( outputChannels > 0 ) {
\r
134 device = outputDevice;
\r
136 channels = outputChannels;
\r
138 if (device == 0) { // Try default device first.
\r
139 for (int i=0; i<nDevices; i++) {
\r
140 if (devices[i].probed == false) {
\r
141 // If the device wasn't successfully probed before, try it
\r
143 clearDeviceInfo(&devices[i]);
\r
144 probeDeviceInfo(&devices[i]);
\r
145 if (devices[i].probed == false)
\r
148 result = probeDeviceOpen(i, stream, mode, channels, sampleRate,
\r
149 format, bufferSize, numberOfBuffers);
\r
150 if (result == SUCCESS)
\r
155 result = probeDeviceOpen(device, stream, mode, channels, sampleRate,
\r
156 format, bufferSize, numberOfBuffers);
\r
160 if ( inputChannels > 0 && result == SUCCESS ) {
\r
162 device = inputDevice;
\r
164 channels = inputChannels;
\r
166 if (device == 0) { // Try default device first.
\r
167 for (int i=0; i<nDevices; i++) {
\r
168 if (devices[i].probed == false) {
\r
169 // If the device wasn't successfully probed before, try it
\r
171 clearDeviceInfo(&devices[i]);
\r
172 probeDeviceInfo(&devices[i]);
\r
173 if (devices[i].probed == false)
\r
176 result = probeDeviceOpen(i, stream, mode, channels, sampleRate,
\r
177 format, bufferSize, numberOfBuffers);
\r
178 if (result == SUCCESS)
\r
183 result = probeDeviceOpen(device, stream, mode, channels, sampleRate,
\r
184 format, bufferSize, numberOfBuffers);
\r
188 if ( result == SUCCESS )
\r
191 // If we get here, all attempted probes failed. Close any opened
\r
192 // devices and delete the allocated stream.
\r
193 closeStream(streamKey);
\r
194 sprintf(message,"RtAudio: no devices found for given parameters.");
\r
195 error(RtError::INVALID_PARAMETER);
\r
200 int RtAudio :: getDeviceCount(void)
\r
205 void RtAudio :: getDeviceInfo(int device, RTAUDIO_DEVICE *info)
\r
207 if (device >= nDevices || device < 0) {
\r
208 sprintf(message, "RtAudio: invalid device specifier (%d)!", device);
\r
209 error(RtError::INVALID_DEVICE);
\r
212 // If the device wasn't successfully probed before, try it again.
\r
213 if (devices[device].probed == false) {
\r
214 clearDeviceInfo(&devices[device]);
\r
215 probeDeviceInfo(&devices[device]);
\r
218 // Clear the info structure.
\r
219 memset(info, 0, sizeof(RTAUDIO_DEVICE));
\r
221 strncpy(info->name, devices[device].name, 128);
\r
222 info->probed = devices[device].probed;
\r
223 if ( info->probed == true ) {
\r
224 info->maxOutputChannels = devices[device].maxOutputChannels;
\r
225 info->maxInputChannels = devices[device].maxInputChannels;
\r
226 info->maxDuplexChannels = devices[device].maxDuplexChannels;
\r
227 info->minOutputChannels = devices[device].minOutputChannels;
\r
228 info->minInputChannels = devices[device].minInputChannels;
\r
229 info->minDuplexChannels = devices[device].minDuplexChannels;
\r
230 info->hasDuplexSupport = devices[device].hasDuplexSupport;
\r
231 info->nSampleRates = devices[device].nSampleRates;
\r
232 if (info->nSampleRates == -1) {
\r
233 info->sampleRates[0] = devices[device].sampleRates[0];
\r
234 info->sampleRates[1] = devices[device].sampleRates[1];
\r
237 for (int i=0; i<info->nSampleRates; i++)
\r
238 info->sampleRates[i] = devices[device].sampleRates[i];
\r
240 info->nativeFormats = devices[device].nativeFormats;
\r
246 char * const RtAudio :: getStreamBuffer(int streamId)
\r
248 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
250 return stream->userBuffer;
\r
253 // This global structure is used to pass information to the thread
\r
254 // function. I tried other methods but had intermittent errors due to
\r
255 // variable persistence during thread startup.
\r
261 extern "C" THREAD_RETURN THREAD_TYPE callbackHandler(void * ptr);
\r
263 void RtAudio :: setStreamCallback(int streamId, RTAUDIO_CALLBACK callback, void *userData)
\r
265 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
267 stream->callback = callback;
\r
268 stream->userData = userData;
\r
269 stream->usingCallback = true;
\r
270 thread_info.object = this;
\r
271 thread_info.streamId = streamId;
\r
274 #if defined(__WINDOWS_DS__)
\r
275 unsigned thread_id;
\r
276 stream->thread = _beginthreadex(NULL, 0, &callbackHandler,
\r
277 &stream->usingCallback, 0, &thread_id);
\r
278 if (stream->thread == 0) err = -1;
\r
279 // When spawning multiple threads in quick succession, it appears to be
\r
280 // necessary to wait a bit for each to initialize ... another windism!
\r
283 err = pthread_create(&stream->thread, NULL, callbackHandler, &stream->usingCallback);
\r
287 stream->usingCallback = false;
\r
288 sprintf(message, "RtAudio: error starting callback thread!");
\r
289 error(RtError::THREAD_ERROR);
\r
293 // *************************************************** //
\r
295 // OS/API-specific methods.
\r
297 // *************************************************** //
\r
299 #if defined(__LINUX_ALSA__)
\r
301 #define MAX_DEVICES 16
\r
303 void RtAudio :: initialize(void)
\r
305 int card, result, device;
\r
307 char deviceNames[MAX_DEVICES][32];
\r
309 snd_ctl_card_info_t *info;
\r
310 snd_ctl_card_info_alloca(&info);
\r
312 // Count cards and devices
\r
315 snd_card_next(&card);
\r
316 while ( card >= 0 ) {
\r
317 sprintf(name, "hw:%d", card);
\r
318 result = snd_ctl_open(&handle, name, 0);
\r
320 sprintf(message, "RtAudio: ALSA control open (%i): %s.", card, snd_strerror(result));
\r
321 error(RtError::WARNING);
\r
324 result = snd_ctl_card_info(handle, info);
\r
326 sprintf(message, "RtAudio: ALSA control hardware info (%i): %s.", card, snd_strerror(result));
\r
327 error(RtError::WARNING);
\r
332 result = snd_ctl_pcm_next_device(handle, &device);
\r
334 sprintf(message, "RtAudio: ALSA control next device (%i): %s.", card, snd_strerror(result));
\r
335 error(RtError::WARNING);
\r
340 sprintf( deviceNames[nDevices++], "hw:%d,%d", card, device );
\r
341 if ( nDevices > MAX_DEVICES ) break;
\r
343 if ( nDevices > MAX_DEVICES ) break;
\r
345 snd_ctl_close(handle);
\r
346 snd_card_next(&card);
\r
349 if (nDevices == 0) return;
\r
351 // Allocate the RTAUDIO_DEVICE structures.
\r
352 devices = (RTAUDIO_DEVICE *) calloc(nDevices, sizeof(RTAUDIO_DEVICE));
\r
353 if (devices == NULL) {
\r
354 sprintf(message, "RtAudio: memory allocation error!");
\r
355 error(RtError::MEMORY_ERROR);
\r
358 // Write device ascii identifiers to device structures and then
\r
359 // probe the device capabilities.
\r
360 for (int i=0; i<nDevices; i++) {
\r
361 strncpy(devices[i].name, deviceNames[i], 32);
\r
362 probeDeviceInfo(&devices[i]);
\r
368 void RtAudio :: probeDeviceInfo(RTAUDIO_DEVICE *info)
\r
371 int open_mode = SND_PCM_ASYNC;
\r
373 snd_pcm_stream_t stream;
\r
375 // First try for playback
\r
376 stream = SND_PCM_STREAM_PLAYBACK;
\r
377 err = snd_pcm_open(&handle, info->name, stream, open_mode);
\r
379 sprintf(message, "RtAudio: ALSA pcm playback open (%s): %s.",
\r
380 info->name, snd_strerror(err));
\r
381 error(RtError::WARNING);
\r
382 goto capture_probe;
\r
385 snd_pcm_hw_params_t *params;
\r
386 snd_pcm_hw_params_alloca(¶ms);
\r
388 // We have an open device ... allocate the parameter structure.
\r
389 err = snd_pcm_hw_params_any(handle, params);
\r
391 snd_pcm_close(handle);
\r
392 sprintf(message, "RtAudio: ALSA hardware probe error (%s): %s.",
\r
393 info->name, snd_strerror(err));
\r
394 error(RtError::WARNING);
\r
395 goto capture_probe;
\r
398 // Get output channel information.
\r
399 info->minOutputChannels = snd_pcm_hw_params_get_channels_min(params);
\r
400 info->maxOutputChannels = snd_pcm_hw_params_get_channels_max(params);
\r
402 snd_pcm_close(handle);
\r
405 // Now try for capture
\r
406 stream = SND_PCM_STREAM_CAPTURE;
\r
407 err = snd_pcm_open(&handle, info->name, stream, open_mode);
\r
409 sprintf(message, "RtAudio: ALSA pcm capture open (%s): %s.",
\r
410 info->name, snd_strerror(err));
\r
411 error(RtError::WARNING);
\r
412 if (info->maxOutputChannels == 0)
\r
413 // didn't open for playback either ... device invalid
\r
415 goto probe_parameters;
\r
418 // We have an open capture device ... allocate the parameter structure.
\r
419 err = snd_pcm_hw_params_any(handle, params);
\r
421 snd_pcm_close(handle);
\r
422 sprintf(message, "RtAudio: ALSA hardware probe error (%s): %s.",
\r
423 info->name, snd_strerror(err));
\r
424 error(RtError::WARNING);
\r
425 if (info->maxOutputChannels > 0)
\r
426 goto probe_parameters;
\r
431 // Get input channel information.
\r
432 info->minInputChannels = snd_pcm_hw_params_get_channels_min(params);
\r
433 info->maxInputChannels = snd_pcm_hw_params_get_channels_max(params);
\r
435 // If device opens for both playback and capture, we determine the channels.
\r
436 if (info->maxOutputChannels == 0 || info->maxInputChannels == 0)
\r
437 goto probe_parameters;
\r
439 info->hasDuplexSupport = true;
\r
440 info->maxDuplexChannels = (info->maxOutputChannels > info->maxInputChannels) ?
\r
441 info->maxInputChannels : info->maxOutputChannels;
\r
442 info->minDuplexChannels = (info->minOutputChannels > info->minInputChannels) ?
\r
443 info->minInputChannels : info->minOutputChannels;
\r
445 snd_pcm_close(handle);
\r
448 // At this point, we just need to figure out the supported data
\r
449 // formats and sample rates. We'll proceed by opening the device in
\r
450 // the direction with the maximum number of channels, or playback if
\r
451 // they are equal. This might limit our sample rate options, but so
\r
454 if (info->maxOutputChannels >= info->maxInputChannels)
\r
455 stream = SND_PCM_STREAM_PLAYBACK;
\r
457 stream = SND_PCM_STREAM_CAPTURE;
\r
459 err = snd_pcm_open(&handle, info->name, stream, open_mode);
\r
461 sprintf(message, "RtAudio: ALSA pcm (%s) won't reopen during probe: %s.",
\r
462 info->name, snd_strerror(err));
\r
463 error(RtError::WARNING);
\r
467 // We have an open device ... allocate the parameter structure.
\r
468 err = snd_pcm_hw_params_any(handle, params);
\r
470 snd_pcm_close(handle);
\r
471 sprintf(message, "RtAudio: ALSA hardware reopen probe error (%s): %s.",
\r
472 info->name, snd_strerror(err));
\r
473 error(RtError::WARNING);
\r
477 // Test a non-standard sample rate to see if continuous rate is supported.
\r
479 if (snd_pcm_hw_params_test_rate(handle, params, 35500, dir) == 0) {
\r
480 // It appears that continuous sample rate support is available.
\r
481 info->nSampleRates = -1;
\r
482 info->sampleRates[0] = snd_pcm_hw_params_get_rate_min(params, &dir);
\r
483 info->sampleRates[1] = snd_pcm_hw_params_get_rate_max(params, &dir);
\r
486 // No continuous rate support ... test our discrete set of sample rate values.
\r
487 info->nSampleRates = 0;
\r
488 for (int i=0; i<MAX_SAMPLE_RATES; i++) {
\r
489 if (snd_pcm_hw_params_test_rate(handle, params, SAMPLE_RATES[i], dir) == 0) {
\r
490 info->sampleRates[info->nSampleRates] = SAMPLE_RATES[i];
\r
491 info->nSampleRates++;
\r
494 if (info->nSampleRates == 0) {
\r
495 snd_pcm_close(handle);
\r
500 // Probe the supported data formats ... we don't care about endian-ness just yet
\r
501 snd_pcm_format_t format;
\r
502 info->nativeFormats = 0;
\r
503 format = SND_PCM_FORMAT_S8;
\r
504 if (snd_pcm_hw_params_test_format(handle, params, format) == 0)
\r
505 info->nativeFormats |= RTAUDIO_SINT8;
\r
506 format = SND_PCM_FORMAT_S16;
\r
507 if (snd_pcm_hw_params_test_format(handle, params, format) == 0)
\r
508 info->nativeFormats |= RTAUDIO_SINT16;
\r
509 format = SND_PCM_FORMAT_S24;
\r
510 if (snd_pcm_hw_params_test_format(handle, params, format) == 0)
\r
511 info->nativeFormats |= RTAUDIO_SINT24;
\r
512 format = SND_PCM_FORMAT_S32;
\r
513 if (snd_pcm_hw_params_test_format(handle, params, format) == 0)
\r
514 info->nativeFormats |= RTAUDIO_SINT32;
\r
515 format = SND_PCM_FORMAT_FLOAT;
\r
516 if (snd_pcm_hw_params_test_format(handle, params, format) == 0)
\r
517 info->nativeFormats |= RTAUDIO_FLOAT32;
\r
518 format = SND_PCM_FORMAT_FLOAT64;
\r
519 if (snd_pcm_hw_params_test_format(handle, params, format) == 0)
\r
520 info->nativeFormats |= RTAUDIO_FLOAT64;
\r
522 // Check that we have at least one supported format
\r
523 if (info->nativeFormats == 0) {
\r
524 snd_pcm_close(handle);
\r
525 sprintf(message, "RtAudio: ALSA PCM device (%s) data format not supported by RtAudio.",
\r
527 error(RtError::WARNING);
\r
531 // That's all ... close the device and return
\r
532 snd_pcm_close(handle);
\r
533 info->probed = true;
\r
537 bool RtAudio :: probeDeviceOpen(int device, RTAUDIO_STREAM *stream,
\r
538 STREAM_MODE mode, int channels,
\r
539 int sampleRate, RTAUDIO_FORMAT format,
\r
540 int *bufferSize, int numberOfBuffers)
\r
542 #if defined(RTAUDIO_DEBUG)
\r
544 snd_output_stdio_attach(&out, stderr, 0);
\r
547 // I'm not using the "plug" interface ... too much inconsistent behavior.
\r
548 const char *name = devices[device].name;
\r
550 snd_pcm_stream_t alsa_stream;
\r
551 if (mode == PLAYBACK)
\r
552 alsa_stream = SND_PCM_STREAM_PLAYBACK;
\r
554 alsa_stream = SND_PCM_STREAM_CAPTURE;
\r
558 int alsa_open_mode = SND_PCM_ASYNC;
\r
559 err = snd_pcm_open(&handle, name, alsa_stream, alsa_open_mode);
\r
561 sprintf(message,"RtAudio: ALSA pcm device (%s) won't open: %s.",
\r
562 name, snd_strerror(err));
\r
563 error(RtError::WARNING);
\r
567 // Fill the parameter structure.
\r
568 snd_pcm_hw_params_t *hw_params;
\r
569 snd_pcm_hw_params_alloca(&hw_params);
\r
570 err = snd_pcm_hw_params_any(handle, hw_params);
\r
572 snd_pcm_close(handle);
\r
573 sprintf(message, "RtAudio: ALSA error getting parameter handle (%s): %s.",
\r
574 name, snd_strerror(err));
\r
575 error(RtError::WARNING);
\r
579 #if defined(RTAUDIO_DEBUG)
\r
580 fprintf(stderr, "\nRtAudio: ALSA dump hardware params just after device open:\n\n");
\r
581 snd_pcm_hw_params_dump(hw_params, out);
\r
584 // Set access ... try interleaved access first, then non-interleaved
\r
585 err = snd_pcm_hw_params_set_access(handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
\r
587 // No interleave support ... try non-interleave.
\r
588 err = snd_pcm_hw_params_set_access(handle, hw_params, SND_PCM_ACCESS_RW_NONINTERLEAVED);
\r
590 snd_pcm_close(handle);
\r
591 sprintf(message, "RtAudio: ALSA error setting access ( (%s): %s.",
\r
592 name, snd_strerror(err));
\r
593 error(RtError::WARNING);
\r
596 stream->deInterleave[mode] = true;
\r
599 // Determine how to set the device format.
\r
600 stream->userFormat = format;
\r
601 snd_pcm_format_t device_format;
\r
603 if (format == RTAUDIO_SINT8)
\r
604 device_format = SND_PCM_FORMAT_S8;
\r
605 else if (format == RTAUDIO_SINT16)
\r
606 device_format = SND_PCM_FORMAT_S16;
\r
607 else if (format == RTAUDIO_SINT24)
\r
608 device_format = SND_PCM_FORMAT_S24;
\r
609 else if (format == RTAUDIO_SINT32)
\r
610 device_format = SND_PCM_FORMAT_S32;
\r
611 else if (format == RTAUDIO_FLOAT32)
\r
612 device_format = SND_PCM_FORMAT_FLOAT;
\r
613 else if (format == RTAUDIO_FLOAT64)
\r
614 device_format = SND_PCM_FORMAT_FLOAT64;
\r
616 if (snd_pcm_hw_params_test_format(handle, hw_params, device_format) == 0) {
\r
617 stream->deviceFormat[mode] = format;
\r
621 // The user requested format is not natively supported by the device.
\r
622 device_format = SND_PCM_FORMAT_FLOAT64;
\r
623 if (snd_pcm_hw_params_test_format(handle, hw_params, device_format) == 0) {
\r
624 stream->deviceFormat[mode] = RTAUDIO_FLOAT64;
\r
628 device_format = SND_PCM_FORMAT_FLOAT;
\r
629 if (snd_pcm_hw_params_test_format(handle, hw_params, device_format) == 0) {
\r
630 stream->deviceFormat[mode] = RTAUDIO_FLOAT32;
\r
634 device_format = SND_PCM_FORMAT_S32;
\r
635 if (snd_pcm_hw_params_test_format(handle, hw_params, device_format) == 0) {
\r
636 stream->deviceFormat[mode] = RTAUDIO_SINT32;
\r
640 device_format = SND_PCM_FORMAT_S24;
\r
641 if (snd_pcm_hw_params_test_format(handle, hw_params, device_format) == 0) {
\r
642 stream->deviceFormat[mode] = RTAUDIO_SINT24;
\r
646 device_format = SND_PCM_FORMAT_S16;
\r
647 if (snd_pcm_hw_params_test_format(handle, hw_params, device_format) == 0) {
\r
648 stream->deviceFormat[mode] = RTAUDIO_SINT16;
\r
652 device_format = SND_PCM_FORMAT_S8;
\r
653 if (snd_pcm_hw_params_test_format(handle, hw_params, device_format) == 0) {
\r
654 stream->deviceFormat[mode] = RTAUDIO_SINT8;
\r
658 // If we get here, no supported format was found.
\r
659 sprintf(message,"RtAudio: ALSA pcm device (%s) data format not supported by RtAudio.", name);
\r
660 snd_pcm_close(handle);
\r
661 error(RtError::WARNING);
\r
665 err = snd_pcm_hw_params_set_format(handle, hw_params, device_format);
\r
667 snd_pcm_close(handle);
\r
668 sprintf(message, "RtAudio: ALSA error setting format (%s): %s.",
\r
669 name, snd_strerror(err));
\r
670 error(RtError::WARNING);
\r
674 // Determine whether byte-swaping is necessary.
\r
675 stream->doByteSwap[mode] = false;
\r
676 if (device_format != SND_PCM_FORMAT_S8) {
\r
677 err = snd_pcm_format_cpu_endian(device_format);
\r
679 stream->doByteSwap[mode] = true;
\r
680 else if (err < 0) {
\r
681 snd_pcm_close(handle);
\r
682 sprintf(message, "RtAudio: ALSA error getting format endian-ness (%s): %s.",
\r
683 name, snd_strerror(err));
\r
684 error(RtError::WARNING);
\r
689 // Determine the number of channels for this device. We support a possible
\r
690 // minimum device channel number > than the value requested by the user.
\r
691 stream->nUserChannels[mode] = channels;
\r
692 int device_channels = snd_pcm_hw_params_get_channels_max(hw_params);
\r
693 if (device_channels < channels) {
\r
694 snd_pcm_close(handle);
\r
695 sprintf(message, "RtAudio: channels (%d) not supported by device (%s).",
\r
697 error(RtError::WARNING);
\r
701 device_channels = snd_pcm_hw_params_get_channels_min(hw_params);
\r
702 if (device_channels < channels) device_channels = channels;
\r
703 stream->nDeviceChannels[mode] = device_channels;
\r
705 // Set the device channels.
\r
706 err = snd_pcm_hw_params_set_channels(handle, hw_params, device_channels);
\r
708 snd_pcm_close(handle);
\r
709 sprintf(message, "RtAudio: ALSA error setting channels (%d) on device (%s): %s.",
\r
710 device_channels, name, snd_strerror(err));
\r
711 error(RtError::WARNING);
\r
715 // Set the sample rate.
\r
716 err = snd_pcm_hw_params_set_rate(handle, hw_params, (unsigned int)sampleRate, 0);
\r
718 snd_pcm_close(handle);
\r
719 sprintf(message, "RtAudio: ALSA error setting sample rate (%d) on device (%s): %s.",
\r
720 sampleRate, name, snd_strerror(err));
\r
721 error(RtError::WARNING);
\r
725 // Set the buffer number, which in ALSA is referred to as the "period".
\r
727 int periods = numberOfBuffers;
\r
728 // Even though the hardware might allow 1 buffer, it won't work reliably.
\r
729 if (periods < 2) periods = 2;
\r
730 err = snd_pcm_hw_params_get_periods_min(hw_params, &dir);
\r
731 if (err > periods) periods = err;
\r
733 err = snd_pcm_hw_params_set_periods(handle, hw_params, periods, 0);
\r
735 snd_pcm_close(handle);
\r
736 sprintf(message, "RtAudio: ALSA error setting periods (%s): %s.",
\r
737 name, snd_strerror(err));
\r
738 error(RtError::WARNING);
\r
742 // Set the buffer (or period) size.
\r
743 err = snd_pcm_hw_params_get_period_size_min(hw_params, &dir);
\r
744 if (err > *bufferSize) *bufferSize = err;
\r
746 err = snd_pcm_hw_params_set_period_size(handle, hw_params, *bufferSize, 0);
\r
748 snd_pcm_close(handle);
\r
749 sprintf(message, "RtAudio: ALSA error setting period size (%s): %s.",
\r
750 name, snd_strerror(err));
\r
751 error(RtError::WARNING);
\r
755 stream->bufferSize = *bufferSize;
\r
757 // Install the hardware configuration
\r
758 err = snd_pcm_hw_params(handle, hw_params);
\r
760 snd_pcm_close(handle);
\r
761 sprintf(message, "RtAudio: ALSA error installing hardware configuration (%s): %s.",
\r
762 name, snd_strerror(err));
\r
763 error(RtError::WARNING);
\r
767 #if defined(RTAUDIO_DEBUG)
\r
768 fprintf(stderr, "\nRtAudio: ALSA dump hardware params after installation:\n\n");
\r
769 snd_pcm_hw_params_dump(hw_params, out);
\r
773 // Install the software configuration
\r
774 snd_pcm_sw_params_t *sw_params = NULL;
\r
775 snd_pcm_sw_params_alloca(&sw_params);
\r
776 snd_pcm_sw_params_current(handle, sw_params);
\r
777 err = snd_pcm_sw_params(handle, sw_params);
\r
779 snd_pcm_close(handle);
\r
780 sprintf(message, "RtAudio: ALSA error installing software configuration (%s): %s.",
\r
781 name, snd_strerror(err));
\r
782 error(RtError::WARNING);
\r
787 // Set handle and flags for buffer conversion
\r
788 stream->handle[mode] = handle;
\r
789 stream->doConvertBuffer[mode] = false;
\r
790 if (stream->userFormat != stream->deviceFormat[mode])
\r
791 stream->doConvertBuffer[mode] = true;
\r
792 if (stream->nUserChannels[mode] < stream->nDeviceChannels[mode])
\r
793 stream->doConvertBuffer[mode] = true;
\r
794 if (stream->nUserChannels[mode] > 1 && stream->deInterleave[mode])
\r
795 stream->doConvertBuffer[mode] = true;
\r
797 // Allocate necessary internal buffers
\r
798 if ( stream->nUserChannels[0] != stream->nUserChannels[1] ) {
\r
801 if (stream->nUserChannels[0] >= stream->nUserChannels[1])
\r
802 buffer_bytes = stream->nUserChannels[0];
\r
804 buffer_bytes = stream->nUserChannels[1];
\r
806 buffer_bytes *= *bufferSize * formatBytes(stream->userFormat);
\r
807 if (stream->userBuffer) free(stream->userBuffer);
\r
808 stream->userBuffer = (char *) calloc(buffer_bytes, 1);
\r
809 if (stream->userBuffer == NULL)
\r
813 if ( stream->doConvertBuffer[mode] ) {
\r
816 bool makeBuffer = true;
\r
817 if ( mode == PLAYBACK )
\r
818 buffer_bytes = stream->nDeviceChannels[0] * formatBytes(stream->deviceFormat[0]);
\r
819 else { // mode == RECORD
\r
820 buffer_bytes = stream->nDeviceChannels[1] * formatBytes(stream->deviceFormat[1]);
\r
821 if ( stream->mode == PLAYBACK ) {
\r
822 long bytes_out = stream->nDeviceChannels[0] * formatBytes(stream->deviceFormat[0]);
\r
823 if ( buffer_bytes > bytes_out )
\r
824 buffer_bytes = (buffer_bytes > bytes_out) ? buffer_bytes : bytes_out;
\r
826 makeBuffer = false;
\r
830 if ( makeBuffer ) {
\r
831 buffer_bytes *= *bufferSize;
\r
832 if (stream->deviceBuffer) free(stream->deviceBuffer);
\r
833 stream->deviceBuffer = (char *) calloc(buffer_bytes, 1);
\r
834 if (stream->deviceBuffer == NULL)
\r
839 stream->device[mode] = device;
\r
840 stream->state = STREAM_STOPPED;
\r
841 if ( stream->mode == PLAYBACK && mode == RECORD )
\r
842 // We had already set up an output stream.
\r
843 stream->mode = DUPLEX;
\r
845 stream->mode = mode;
\r
846 stream->nBuffers = periods;
\r
847 stream->sampleRate = sampleRate;
\r
852 if (stream->handle[0]) {
\r
853 snd_pcm_close(stream->handle[0]);
\r
854 stream->handle[0] = 0;
\r
856 if (stream->handle[1]) {
\r
857 snd_pcm_close(stream->handle[1]);
\r
858 stream->handle[1] = 0;
\r
860 if (stream->userBuffer) {
\r
861 free(stream->userBuffer);
\r
862 stream->userBuffer = 0;
\r
864 sprintf(message, "RtAudio: ALSA error allocating buffer memory (%s).", name);
\r
865 error(RtError::WARNING);
\r
869 void RtAudio :: cancelStreamCallback(int streamId)
\r
871 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
873 if (stream->usingCallback) {
\r
874 stream->usingCallback = false;
\r
875 pthread_cancel(stream->thread);
\r
876 pthread_join(stream->thread, NULL);
\r
877 stream->thread = 0;
\r
878 stream->callback = NULL;
\r
879 stream->userData = NULL;
\r
883 void RtAudio :: closeStream(int streamId)
\r
885 // We don't want an exception to be thrown here because this
\r
886 // function is called by our class destructor. So, do our own
\r
888 if ( streams.find( streamId ) == streams.end() ) {
\r
889 sprintf(message, "RtAudio: invalid stream identifier!");
\r
890 error(RtError::WARNING);
\r
894 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) streams[streamId];
\r
896 if (stream->usingCallback) {
\r
897 pthread_cancel(stream->thread);
\r
898 pthread_join(stream->thread, NULL);
\r
901 if (stream->state == STREAM_RUNNING) {
\r
902 if (stream->mode == PLAYBACK || stream->mode == DUPLEX)
\r
903 snd_pcm_drop(stream->handle[0]);
\r
904 if (stream->mode == RECORD || stream->mode == DUPLEX)
\r
905 snd_pcm_drop(stream->handle[1]);
\r
908 pthread_mutex_destroy(&stream->mutex);
\r
910 if (stream->handle[0])
\r
911 snd_pcm_close(stream->handle[0]);
\r
913 if (stream->handle[1])
\r
914 snd_pcm_close(stream->handle[1]);
\r
916 if (stream->userBuffer)
\r
917 free(stream->userBuffer);
\r
919 if (stream->deviceBuffer)
\r
920 free(stream->deviceBuffer);
\r
923 streams.erase(streamId);
\r
926 void RtAudio :: startStream(int streamId)
\r
928 // This method calls snd_pcm_prepare if the device isn't already in that state.
\r
930 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
932 MUTEX_LOCK(&stream->mutex);
\r
934 if (stream->state == STREAM_RUNNING)
\r
938 snd_pcm_state_t state;
\r
939 if (stream->mode == PLAYBACK || stream->mode == DUPLEX) {
\r
940 state = snd_pcm_state(stream->handle[0]);
\r
941 if (state != SND_PCM_STATE_PREPARED) {
\r
942 err = snd_pcm_prepare(stream->handle[0]);
\r
944 sprintf(message, "RtAudio: ALSA error preparing pcm device (%s): %s.",
\r
945 devices[stream->device[0]].name, snd_strerror(err));
\r
946 MUTEX_UNLOCK(&stream->mutex);
\r
947 error(RtError::DRIVER_ERROR);
\r
952 if (stream->mode == RECORD || stream->mode == DUPLEX) {
\r
953 state = snd_pcm_state(stream->handle[1]);
\r
954 if (state != SND_PCM_STATE_PREPARED) {
\r
955 err = snd_pcm_prepare(stream->handle[1]);
\r
957 sprintf(message, "RtAudio: ALSA error preparing pcm device (%s): %s.",
\r
958 devices[stream->device[1]].name, snd_strerror(err));
\r
959 MUTEX_UNLOCK(&stream->mutex);
\r
960 error(RtError::DRIVER_ERROR);
\r
964 stream->state = STREAM_RUNNING;
\r
967 MUTEX_UNLOCK(&stream->mutex);
\r
970 void RtAudio :: stopStream(int streamId)
\r
972 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
974 MUTEX_LOCK(&stream->mutex);
\r
976 if (stream->state == STREAM_STOPPED)
\r
980 if (stream->mode == PLAYBACK || stream->mode == DUPLEX) {
\r
981 err = snd_pcm_drain(stream->handle[0]);
\r
983 sprintf(message, "RtAudio: ALSA error draining pcm device (%s): %s.",
\r
984 devices[stream->device[0]].name, snd_strerror(err));
\r
985 MUTEX_UNLOCK(&stream->mutex);
\r
986 error(RtError::DRIVER_ERROR);
\r
990 if (stream->mode == RECORD || stream->mode == DUPLEX) {
\r
991 err = snd_pcm_drain(stream->handle[1]);
\r
993 sprintf(message, "RtAudio: ALSA error draining pcm device (%s): %s.",
\r
994 devices[stream->device[1]].name, snd_strerror(err));
\r
995 MUTEX_UNLOCK(&stream->mutex);
\r
996 error(RtError::DRIVER_ERROR);
\r
999 stream->state = STREAM_STOPPED;
\r
1002 MUTEX_UNLOCK(&stream->mutex);
\r
1005 void RtAudio :: abortStream(int streamId)
\r
1007 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
1009 MUTEX_LOCK(&stream->mutex);
\r
1011 if (stream->state == STREAM_STOPPED)
\r
1015 if (stream->mode == PLAYBACK || stream->mode == DUPLEX) {
\r
1016 err = snd_pcm_drop(stream->handle[0]);
\r
1018 sprintf(message, "RtAudio: ALSA error draining pcm device (%s): %s.",
\r
1019 devices[stream->device[0]].name, snd_strerror(err));
\r
1020 MUTEX_UNLOCK(&stream->mutex);
\r
1021 error(RtError::DRIVER_ERROR);
\r
1025 if (stream->mode == RECORD || stream->mode == DUPLEX) {
\r
1026 err = snd_pcm_drop(stream->handle[1]);
\r
1028 sprintf(message, "RtAudio: ALSA error draining pcm device (%s): %s.",
\r
1029 devices[stream->device[1]].name, snd_strerror(err));
\r
1030 MUTEX_UNLOCK(&stream->mutex);
\r
1031 error(RtError::DRIVER_ERROR);
\r
1034 stream->state = STREAM_STOPPED;
\r
1037 MUTEX_UNLOCK(&stream->mutex);
\r
1040 int RtAudio :: streamWillBlock(int streamId)
\r
1042 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
1044 MUTEX_LOCK(&stream->mutex);
\r
1046 int err = 0, frames = 0;
\r
1047 if (stream->state == STREAM_STOPPED)
\r
1050 if (stream->mode == PLAYBACK || stream->mode == DUPLEX) {
\r
1051 err = snd_pcm_avail_update(stream->handle[0]);
\r
1053 sprintf(message, "RtAudio: ALSA error getting available frames for device (%s): %s.",
\r
1054 devices[stream->device[0]].name, snd_strerror(err));
\r
1055 MUTEX_UNLOCK(&stream->mutex);
\r
1056 error(RtError::DRIVER_ERROR);
\r
1062 if (stream->mode == RECORD || stream->mode == DUPLEX) {
\r
1063 err = snd_pcm_avail_update(stream->handle[1]);
\r
1065 sprintf(message, "RtAudio: ALSA error getting available frames for device (%s): %s.",
\r
1066 devices[stream->device[1]].name, snd_strerror(err));
\r
1067 MUTEX_UNLOCK(&stream->mutex);
\r
1068 error(RtError::DRIVER_ERROR);
\r
1070 if (frames > err) frames = err;
\r
1073 frames = stream->bufferSize - frames;
\r
1074 if (frames < 0) frames = 0;
\r
1077 MUTEX_UNLOCK(&stream->mutex);
\r
1081 void RtAudio :: tickStream(int streamId)
\r
1083 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
1085 int stopStream = 0;
\r
1086 if (stream->state == STREAM_STOPPED) {
\r
1087 if (stream->usingCallback) usleep(50000); // sleep 50 milliseconds
\r
1090 else if (stream->usingCallback) {
\r
1091 stopStream = stream->callback(stream->userBuffer, stream->bufferSize, stream->userData);
\r
1094 MUTEX_LOCK(&stream->mutex);
\r
1096 // The state might change while waiting on a mutex.
\r
1097 if (stream->state == STREAM_STOPPED)
\r
1103 RTAUDIO_FORMAT format;
\r
1104 if (stream->mode == PLAYBACK || stream->mode == DUPLEX) {
\r
1106 // Setup parameters and do buffer conversion if necessary.
\r
1107 if (stream->doConvertBuffer[0]) {
\r
1108 convertStreamBuffer(stream, PLAYBACK);
\r
1109 buffer = stream->deviceBuffer;
\r
1110 channels = stream->nDeviceChannels[0];
\r
1111 format = stream->deviceFormat[0];
\r
1114 buffer = stream->userBuffer;
\r
1115 channels = stream->nUserChannels[0];
\r
1116 format = stream->userFormat;
\r
1119 // Do byte swapping if necessary.
\r
1120 if (stream->doByteSwap[0])
\r
1121 byteSwapBuffer(buffer, stream->bufferSize * channels, format);
\r
1123 // Write samples to device in interleaved/non-interleaved format.
\r
1124 if (stream->deInterleave[0]) {
\r
1125 void *bufs[channels];
\r
1126 size_t offset = stream->bufferSize * formatBytes(format);
\r
1127 for (int i=0; i<channels; i++)
\r
1128 bufs[i] = (void *) (buffer + (i * offset));
\r
1129 err = snd_pcm_writen(stream->handle[0], bufs, stream->bufferSize);
\r
1132 err = snd_pcm_writei(stream->handle[0], buffer, stream->bufferSize);
\r
1134 if (err < stream->bufferSize) {
\r
1135 // Either an error or underrun occured.
\r
1136 if (err == -EPIPE) {
\r
1137 snd_pcm_state_t state = snd_pcm_state(stream->handle[0]);
\r
1138 if (state == SND_PCM_STATE_XRUN) {
\r
1139 sprintf(message, "RtAudio: ALSA underrun detected.");
\r
1140 error(RtError::WARNING);
\r
1141 err = snd_pcm_prepare(stream->handle[0]);
\r
1143 sprintf(message, "RtAudio: ALSA error preparing handle after underrun: %s.",
\r
1144 snd_strerror(err));
\r
1145 MUTEX_UNLOCK(&stream->mutex);
\r
1146 error(RtError::DRIVER_ERROR);
\r
1150 sprintf(message, "RtAudio: ALSA error, current state is %s.",
\r
1151 snd_pcm_state_name(state));
\r
1152 MUTEX_UNLOCK(&stream->mutex);
\r
1153 error(RtError::DRIVER_ERROR);
\r
1158 sprintf(message, "RtAudio: ALSA audio write error for device (%s): %s.",
\r
1159 devices[stream->device[0]].name, snd_strerror(err));
\r
1160 MUTEX_UNLOCK(&stream->mutex);
\r
1161 error(RtError::DRIVER_ERROR);
\r
1166 if (stream->mode == RECORD || stream->mode == DUPLEX) {
\r
1168 // Setup parameters.
\r
1169 if (stream->doConvertBuffer[1]) {
\r
1170 buffer = stream->deviceBuffer;
\r
1171 channels = stream->nDeviceChannels[1];
\r
1172 format = stream->deviceFormat[1];
\r
1175 buffer = stream->userBuffer;
\r
1176 channels = stream->nUserChannels[1];
\r
1177 format = stream->userFormat;
\r
1180 // Read samples from device in interleaved/non-interleaved format.
\r
1181 if (stream->deInterleave[1]) {
\r
1182 void *bufs[channels];
\r
1183 size_t offset = stream->bufferSize * formatBytes(format);
\r
1184 for (int i=0; i<channels; i++)
\r
1185 bufs[i] = (void *) (buffer + (i * offset));
\r
1186 err = snd_pcm_readn(stream->handle[1], bufs, stream->bufferSize);
\r
1189 err = snd_pcm_readi(stream->handle[1], buffer, stream->bufferSize);
\r
1191 if (err < stream->bufferSize) {
\r
1192 // Either an error or underrun occured.
\r
1193 if (err == -EPIPE) {
\r
1194 snd_pcm_state_t state = snd_pcm_state(stream->handle[1]);
\r
1195 if (state == SND_PCM_STATE_XRUN) {
\r
1196 sprintf(message, "RtAudio: ALSA overrun detected.");
\r
1197 error(RtError::WARNING);
\r
1198 err = snd_pcm_prepare(stream->handle[1]);
\r
1200 sprintf(message, "RtAudio: ALSA error preparing handle after overrun: %s.",
\r
1201 snd_strerror(err));
\r
1202 MUTEX_UNLOCK(&stream->mutex);
\r
1203 error(RtError::DRIVER_ERROR);
\r
1207 sprintf(message, "RtAudio: ALSA error, current state is %s.",
\r
1208 snd_pcm_state_name(state));
\r
1209 MUTEX_UNLOCK(&stream->mutex);
\r
1210 error(RtError::DRIVER_ERROR);
\r
1215 sprintf(message, "RtAudio: ALSA audio read error for device (%s): %s.",
\r
1216 devices[stream->device[1]].name, snd_strerror(err));
\r
1217 MUTEX_UNLOCK(&stream->mutex);
\r
1218 error(RtError::DRIVER_ERROR);
\r
1222 // Do byte swapping if necessary.
\r
1223 if (stream->doByteSwap[1])
\r
1224 byteSwapBuffer(buffer, stream->bufferSize * channels, format);
\r
1226 // Do buffer conversion if necessary.
\r
1227 if (stream->doConvertBuffer[1])
\r
1228 convertStreamBuffer(stream, RECORD);
\r
1232 MUTEX_UNLOCK(&stream->mutex);
\r
1234 if (stream->usingCallback && stopStream)
\r
1235 this->stopStream(streamId);
\r
1238 extern "C" void *callbackHandler(void *ptr)
\r
1240 RtAudio *object = thread_info.object;
\r
1241 int stream = thread_info.streamId;
\r
1242 bool *usingCallback = (bool *) ptr;
\r
1244 while ( *usingCallback ) {
\r
1245 pthread_testcancel();
\r
1247 object->tickStream(stream);
\r
1249 catch (RtError &exception) {
\r
1250 fprintf(stderr, "\nCallback thread error (%s) ... closing thread.\n\n",
\r
1251 exception.getMessage());
\r
1259 //******************** End of __LINUX_ALSA__ *********************//
\r
1261 #elif defined(__LINUX_OSS__)
\r
1263 #include <sys/stat.h>
\r
1264 #include <sys/types.h>
\r
1265 #include <sys/ioctl.h>
\r
1266 #include <unistd.h>
\r
1267 #include <fcntl.h>
\r
1268 #include <sys/soundcard.h>
\r
1269 #include <errno.h>
\r
1272 #define DAC_NAME "/dev/dsp"
\r
1273 #define MAX_DEVICES 16
\r
1274 #define MAX_CHANNELS 16
\r
1276 void RtAudio :: initialize(void)
\r
1278 // Count cards and devices
\r
1281 // We check /dev/dsp before probing devices. /dev/dsp is supposed to
\r
1282 // be a link to the "default" audio device, of the form /dev/dsp0,
\r
1283 // /dev/dsp1, etc... However, I've seen one case where /dev/dsp was a
\r
1284 // real device, so we need to check for that. Also, sometimes the
\r
1285 // link is to /dev/dspx and other times just dspx. I'm not sure how
\r
1286 // the latter works, but it does.
\r
1287 char device_name[16];
\r
1288 struct stat dspstat;
\r
1291 if (lstat(DAC_NAME, &dspstat) == 0) {
\r
1292 if (S_ISLNK(dspstat.st_mode)) {
\r
1293 i = readlink(DAC_NAME, device_name, sizeof(device_name));
\r
1295 device_name[i] = '\0';
\r
1296 if (i > 8) { // check for "/dev/dspx"
\r
1297 if (!strncmp(DAC_NAME, device_name, 8))
\r
1298 dsplink = atoi(&device_name[8]);
\r
1300 else if (i > 3) { // check for "dspx"
\r
1301 if (!strncmp("dsp", device_name, 3))
\r
1302 dsplink = atoi(&device_name[3]);
\r
1306 sprintf(message, "RtAudio: cannot read value of symbolic link %s.", DAC_NAME);
\r
1307 error(RtError::SYSTEM_ERROR);
\r
1312 sprintf(message, "RtAudio: cannot stat %s.", DAC_NAME);
\r
1313 error(RtError::SYSTEM_ERROR);
\r
1316 // The OSS API doesn't provide a routine for determining the number
\r
1317 // of devices. Thus, we'll just pursue a brute force method. The
\r
1318 // idea is to start with /dev/dsp(0) and continue with higher device
\r
1319 // numbers until we reach MAX_DSP_DEVICES. This should tell us how
\r
1320 // many devices we have ... it is not a fullproof scheme, but hopefully
\r
1321 // it will work most of the time.
\r
1324 char names[MAX_DEVICES][16];
\r
1325 for (i=-1; i<MAX_DEVICES; i++) {
\r
1327 // Probe /dev/dsp first, since it is supposed to be the default device.
\r
1329 sprintf(device_name, "%s", DAC_NAME);
\r
1330 else if (i == dsplink)
\r
1331 continue; // We've aready probed this device via /dev/dsp link ... try next device.
\r
1333 sprintf(device_name, "%s%d", DAC_NAME, i);
\r
1335 // First try to open the device for playback, then record mode.
\r
1336 fd = open(device_name, O_WRONLY | O_NONBLOCK);
\r
1338 // Open device for playback failed ... either busy or doesn't exist.
\r
1339 if (errno != EBUSY && errno != EAGAIN) {
\r
1340 // Try to open for capture
\r
1341 fd = open(device_name, O_RDONLY | O_NONBLOCK);
\r
1343 // Open device for record failed.
\r
1344 if (errno != EBUSY && errno != EAGAIN)
\r
1347 sprintf(message, "RtAudio: OSS record device (%s) is busy.", device_name);
\r
1348 error(RtError::WARNING);
\r
1349 // still count it for now
\r
1354 sprintf(message, "RtAudio: OSS playback device (%s) is busy.", device_name);
\r
1355 error(RtError::WARNING);
\r
1356 // still count it for now
\r
1360 if (fd >= 0) close(fd);
\r
1361 strncpy(names[nDevices], device_name, 16);
\r
1365 if (nDevices == 0) return;
\r
1367 // Allocate the RTAUDIO_DEVICE structures.
\r
1368 devices = (RTAUDIO_DEVICE *) calloc(nDevices, sizeof(RTAUDIO_DEVICE));
\r
1369 if (devices == NULL) {
\r
1370 sprintf(message, "RtAudio: memory allocation error!");
\r
1371 error(RtError::MEMORY_ERROR);
\r
1374 // Write device ascii identifiers to device control structure and then probe capabilities.
\r
1375 for (i=0; i<nDevices; i++) {
\r
1376 strncpy(devices[i].name, names[i], 16);
\r
1377 probeDeviceInfo(&devices[i]);
\r
1383 void RtAudio :: probeDeviceInfo(RTAUDIO_DEVICE *info)
\r
1385 int i, fd, channels, mask;
\r
1387 // The OSS API doesn't provide a means for probing the capabilities
\r
1388 // of devices. Thus, we'll just pursue a brute force method.
\r
1390 // First try for playback
\r
1391 fd = open(info->name, O_WRONLY | O_NONBLOCK);
\r
1393 // Open device failed ... either busy or doesn't exist
\r
1394 if (errno == EBUSY || errno == EAGAIN)
\r
1395 sprintf(message, "RtAudio: OSS playback device (%s) is busy and cannot be probed.",
\r
1398 sprintf(message, "RtAudio: OSS playback device (%s) open error.", info->name);
\r
1399 error(RtError::WARNING);
\r
1400 goto capture_probe;
\r
1403 // We have an open device ... see how many channels it can handle
\r
1404 for (i=MAX_CHANNELS; i>0; i--) {
\r
1406 if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) == -1) {
\r
1407 // This would normally indicate some sort of hardware error, but under ALSA's
\r
1408 // OSS emulation, it sometimes indicates an invalid channel value. Further,
\r
1409 // the returned channel value is not changed. So, we'll ignore the possible
\r
1410 // hardware error.
\r
1411 continue; // try next channel number
\r
1413 // Check to see whether the device supports the requested number of channels
\r
1414 if (channels != i ) continue; // try next channel number
\r
1415 // If here, we found the largest working channel value
\r
1418 info->maxOutputChannels = channels;
\r
1420 // Now find the minimum number of channels it can handle
\r
1421 for (i=1; i<=info->maxOutputChannels; i++) {
\r
1423 if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) == -1 || channels != i)
\r
1424 continue; // try next channel number
\r
1425 // If here, we found the smallest working channel value
\r
1428 info->minOutputChannels = channels;
\r
1432 // Now try for capture
\r
1433 fd = open(info->name, O_RDONLY | O_NONBLOCK);
\r
1435 // Open device for capture failed ... either busy or doesn't exist
\r
1436 if (errno == EBUSY || errno == EAGAIN)
\r
1437 sprintf(message, "RtAudio: OSS capture device (%s) is busy and cannot be probed.",
\r
1440 sprintf(message, "RtAudio: OSS capture device (%s) open error.", info->name);
\r
1441 error(RtError::WARNING);
\r
1442 if (info->maxOutputChannels == 0)
\r
1443 // didn't open for playback either ... device invalid
\r
1445 goto probe_parameters;
\r
1448 // We have the device open for capture ... see how many channels it can handle
\r
1449 for (i=MAX_CHANNELS; i>0; i--) {
\r
1451 if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) == -1 || channels != i) {
\r
1452 continue; // as above
\r
1454 // If here, we found a working channel value
\r
1457 info->maxInputChannels = channels;
\r
1459 // Now find the minimum number of channels it can handle
\r
1460 for (i=1; i<=info->maxInputChannels; i++) {
\r
1462 if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) == -1 || channels != i)
\r
1463 continue; // try next channel number
\r
1464 // If here, we found the smallest working channel value
\r
1467 info->minInputChannels = channels;
\r
1470 // If device opens for both playback and capture, we determine the channels.
\r
1471 if (info->maxOutputChannels == 0 || info->maxInputChannels == 0)
\r
1472 goto probe_parameters;
\r
1474 fd = open(info->name, O_RDWR | O_NONBLOCK);
\r
1476 goto probe_parameters;
\r
1478 ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
\r
1479 ioctl(fd, SNDCTL_DSP_GETCAPS, &mask);
\r
1480 if (mask & DSP_CAP_DUPLEX) {
\r
1481 info->hasDuplexSupport = true;
\r
1482 // We have the device open for duplex ... see how many channels it can handle
\r
1483 for (i=MAX_CHANNELS; i>0; i--) {
\r
1485 if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) == -1 || channels != i)
\r
1486 continue; // as above
\r
1487 // If here, we found a working channel value
\r
1490 info->maxDuplexChannels = channels;
\r
1492 // Now find the minimum number of channels it can handle
\r
1493 for (i=1; i<=info->maxDuplexChannels; i++) {
\r
1495 if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) == -1 || channels != i)
\r
1496 continue; // try next channel number
\r
1497 // If here, we found the smallest working channel value
\r
1500 info->minDuplexChannels = channels;
\r
1505 // At this point, we need to figure out the supported data formats
\r
1506 // and sample rates. We'll proceed by openning the device in the
\r
1507 // direction with the maximum number of channels, or playback if
\r
1508 // they are equal. This might limit our sample rate options, but so
\r
1511 if (info->maxOutputChannels >= info->maxInputChannels) {
\r
1512 fd = open(info->name, O_WRONLY | O_NONBLOCK);
\r
1513 channels = info->maxOutputChannels;
\r
1516 fd = open(info->name, O_RDONLY | O_NONBLOCK);
\r
1517 channels = info->maxInputChannels;
\r
1521 // We've got some sort of conflict ... abort
\r
1522 sprintf(message, "RtAudio: OSS device (%s) won't reopen during probe.",
\r
1524 error(RtError::WARNING);
\r
1528 // We have an open device ... set to maximum channels.
\r
1530 if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) == -1 || channels != i) {
\r
1531 // We've got some sort of conflict ... abort
\r
1533 sprintf(message, "RtAudio: OSS device (%s) won't revert to previous channel setting.",
\r
1535 error(RtError::WARNING);
\r
1539 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &mask) == -1) {
\r
1541 sprintf(message, "RtAudio: OSS device (%s) can't get supported audio formats.",
\r
1543 error(RtError::WARNING);
\r
1547 // Probe the supported data formats ... we don't care about endian-ness just yet.
\r
1549 info->nativeFormats = 0;
\r
1550 #if defined (AFMT_S32_BE)
\r
1551 // This format does not seem to be in the 2.4 kernel version of OSS soundcard.h
\r
1552 if (mask & AFMT_S32_BE) {
\r
1553 format = AFMT_S32_BE;
\r
1554 info->nativeFormats |= RTAUDIO_SINT32;
\r
1557 #if defined (AFMT_S32_LE)
\r
1558 /* This format is not in the 2.4.4 kernel version of OSS soundcard.h */
\r
1559 if (mask & AFMT_S32_LE) {
\r
1560 format = AFMT_S32_LE;
\r
1561 info->nativeFormats |= RTAUDIO_SINT32;
\r
1564 if (mask & AFMT_S8) {
\r
1566 info->nativeFormats |= RTAUDIO_SINT8;
\r
1568 if (mask & AFMT_S16_BE) {
\r
1569 format = AFMT_S16_BE;
\r
1570 info->nativeFormats |= RTAUDIO_SINT16;
\r
1572 if (mask & AFMT_S16_LE) {
\r
1573 format = AFMT_S16_LE;
\r
1574 info->nativeFormats |= RTAUDIO_SINT16;
\r
1577 // Check that we have at least one supported format
\r
1578 if (info->nativeFormats == 0) {
\r
1580 sprintf(message, "RtAudio: OSS device (%s) data format not supported by RtAudio.",
\r
1582 error(RtError::WARNING);
\r
1588 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) == -1 || format != i) {
\r
1590 sprintf(message, "RtAudio: OSS device (%s) error setting data format.",
\r
1592 error(RtError::WARNING);
\r
1596 // Probe the supported sample rates ... first get lower limit
\r
1598 if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) == -1) {
\r
1599 // If we get here, we're probably using an ALSA driver with OSS-emulation,
\r
1600 // which doesn't conform to the OSS specification. In this case,
\r
1601 // we'll probe our predefined list of sample rates for working values.
\r
1602 info->nSampleRates = 0;
\r
1603 for (i=0; i<MAX_SAMPLE_RATES; i++) {
\r
1604 speed = SAMPLE_RATES[i];
\r
1605 if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) != -1) {
\r
1606 info->sampleRates[info->nSampleRates] = SAMPLE_RATES[i];
\r
1607 info->nSampleRates++;
\r
1610 if (info->nSampleRates == 0) {
\r
1616 info->sampleRates[0] = speed;
\r
1618 // Now get upper limit
\r
1620 if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) == -1) {
\r
1622 sprintf(message, "RtAudio: OSS device (%s) error setting sample rate.",
\r
1624 error(RtError::WARNING);
\r
1627 info->sampleRates[1] = speed;
\r
1628 info->nSampleRates = -1;
\r
1630 finished: // That's all ... close the device and return
\r
1632 info->probed = true;
\r
1636 bool RtAudio :: probeDeviceOpen(int device, RTAUDIO_STREAM *stream,
\r
1637 STREAM_MODE mode, int channels,
\r
1638 int sampleRate, RTAUDIO_FORMAT format,
\r
1639 int *bufferSize, int numberOfBuffers)
\r
1641 int buffers, buffer_bytes, device_channels, device_format;
\r
1642 int srate, temp, fd;
\r
1644 const char *name = devices[device].name;
\r
1646 if (mode == PLAYBACK)
\r
1647 fd = open(name, O_WRONLY | O_NONBLOCK);
\r
1648 else { // mode == RECORD
\r
1649 if (stream->mode == PLAYBACK && stream->device[0] == device) {
\r
1650 // We just set the same device for playback ... close and reopen for duplex (OSS only).
\r
1651 close(stream->handle[0]);
\r
1652 stream->handle[0] = 0;
\r
1653 // First check that the number previously set channels is the same.
\r
1654 if (stream->nUserChannels[0] != channels) {
\r
1655 sprintf(message, "RtAudio: input/output channels must be equal for OSS duplex device (%s).", name);
\r
1658 fd = open(name, O_RDWR | O_NONBLOCK);
\r
1661 fd = open(name, O_RDONLY | O_NONBLOCK);
\r
1665 if (errno == EBUSY || errno == EAGAIN)
\r
1666 sprintf(message, "RtAudio: OSS device (%s) is busy and cannot be opened.",
\r
1669 sprintf(message, "RtAudio: OSS device (%s) cannot be opened.", name);
\r
1673 // Now reopen in blocking mode.
\r
1675 if (mode == PLAYBACK)
\r
1676 fd = open(name, O_WRONLY | O_SYNC);
\r
1677 else { // mode == RECORD
\r
1678 if (stream->mode == PLAYBACK && stream->device[0] == device)
\r
1679 fd = open(name, O_RDWR | O_SYNC);
\r
1681 fd = open(name, O_RDONLY | O_SYNC);
\r
1685 sprintf(message, "RtAudio: OSS device (%s) cannot be opened.", name);
\r
1689 // Get the sample format mask
\r
1691 if (ioctl(fd, SNDCTL_DSP_GETFMTS, &mask) == -1) {
\r
1693 sprintf(message, "RtAudio: OSS device (%s) can't get supported audio formats.",
\r
1698 // Determine how to set the device format.
\r
1699 stream->userFormat = format;
\r
1700 device_format = -1;
\r
1701 stream->doByteSwap[mode] = false;
\r
1702 if (format == RTAUDIO_SINT8) {
\r
1703 if (mask & AFMT_S8) {
\r
1704 device_format = AFMT_S8;
\r
1705 stream->deviceFormat[mode] = RTAUDIO_SINT8;
\r
1708 else if (format == RTAUDIO_SINT16) {
\r
1709 if (mask & AFMT_S16_NE) {
\r
1710 device_format = AFMT_S16_NE;
\r
1711 stream->deviceFormat[mode] = RTAUDIO_SINT16;
\r
1713 #if BYTE_ORDER == LITTLE_ENDIAN
\r
1714 else if (mask & AFMT_S16_BE) {
\r
1715 device_format = AFMT_S16_BE;
\r
1716 stream->deviceFormat[mode] = RTAUDIO_SINT16;
\r
1717 stream->doByteSwap[mode] = true;
\r
1720 else if (mask & AFMT_S16_LE) {
\r
1721 device_format = AFMT_S16_LE;
\r
1722 stream->deviceFormat[mode] = RTAUDIO_SINT16;
\r
1723 stream->doByteSwap[mode] = true;
\r
1727 #if defined (AFMT_S32_NE) && defined (AFMT_S32_LE) && defined (AFMT_S32_BE)
\r
1728 else if (format == RTAUDIO_SINT32) {
\r
1729 if (mask & AFMT_S32_NE) {
\r
1730 device_format = AFMT_S32_NE;
\r
1731 stream->deviceFormat[mode] = RTAUDIO_SINT32;
\r
1733 #if BYTE_ORDER == LITTLE_ENDIAN
\r
1734 else if (mask & AFMT_S32_BE) {
\r
1735 device_format = AFMT_S32_BE;
\r
1736 stream->deviceFormat[mode] = RTAUDIO_SINT32;
\r
1737 stream->doByteSwap[mode] = true;
\r
1740 else if (mask & AFMT_S32_LE) {
\r
1741 device_format = AFMT_S32_LE;
\r
1742 stream->deviceFormat[mode] = RTAUDIO_SINT32;
\r
1743 stream->doByteSwap[mode] = true;
\r
1749 if (device_format == -1) {
\r
1750 // The user requested format is not natively supported by the device.
\r
1751 if (mask & AFMT_S16_NE) {
\r
1752 device_format = AFMT_S16_NE;
\r
1753 stream->deviceFormat[mode] = RTAUDIO_SINT16;
\r
1755 #if BYTE_ORDER == LITTLE_ENDIAN
\r
1756 else if (mask & AFMT_S16_BE) {
\r
1757 device_format = AFMT_S16_BE;
\r
1758 stream->deviceFormat[mode] = RTAUDIO_SINT16;
\r
1759 stream->doByteSwap[mode] = true;
\r
1762 else if (mask & AFMT_S16_LE) {
\r
1763 device_format = AFMT_S16_LE;
\r
1764 stream->deviceFormat[mode] = RTAUDIO_SINT16;
\r
1765 stream->doByteSwap[mode] = true;
\r
1768 #if defined (AFMT_S32_NE) && defined (AFMT_S32_LE) && defined (AFMT_S32_BE)
\r
1769 else if (mask & AFMT_S32_NE) {
\r
1770 device_format = AFMT_S32_NE;
\r
1771 stream->deviceFormat[mode] = RTAUDIO_SINT32;
\r
1773 #if BYTE_ORDER == LITTLE_ENDIAN
\r
1774 else if (mask & AFMT_S32_BE) {
\r
1775 device_format = AFMT_S32_BE;
\r
1776 stream->deviceFormat[mode] = RTAUDIO_SINT32;
\r
1777 stream->doByteSwap[mode] = true;
\r
1780 else if (mask & AFMT_S32_LE) {
\r
1781 device_format = AFMT_S32_LE;
\r
1782 stream->deviceFormat[mode] = RTAUDIO_SINT32;
\r
1783 stream->doByteSwap[mode] = true;
\r
1787 else if (mask & AFMT_S8) {
\r
1788 device_format = AFMT_S8;
\r
1789 stream->deviceFormat[mode] = RTAUDIO_SINT8;
\r
1793 if (stream->deviceFormat[mode] == 0) {
\r
1794 // This really shouldn't happen ...
\r
1796 sprintf(message, "RtAudio: OSS device (%s) data format not supported by RtAudio.",
\r
1801 // Determine the number of channels for this device. Note that the
\r
1802 // channel value requested by the user might be < min_X_Channels.
\r
1803 stream->nUserChannels[mode] = channels;
\r
1804 device_channels = channels;
\r
1805 if (mode == PLAYBACK) {
\r
1806 if (channels < devices[device].minOutputChannels)
\r
1807 device_channels = devices[device].minOutputChannels;
\r
1809 else { // mode == RECORD
\r
1810 if (stream->mode == PLAYBACK && stream->device[0] == device) {
\r
1811 // We're doing duplex setup here.
\r
1812 if (channels < devices[device].minDuplexChannels)
\r
1813 device_channels = devices[device].minDuplexChannels;
\r
1816 if (channels < devices[device].minInputChannels)
\r
1817 device_channels = devices[device].minInputChannels;
\r
1820 stream->nDeviceChannels[mode] = device_channels;
\r
1822 // Attempt to set the buffer size. According to OSS, the minimum
\r
1823 // number of buffers is two. The supposed minimum buffer size is 16
\r
1824 // bytes, so that will be our lower bound. The argument to this
\r
1825 // call is in the form 0xMMMMSSSS (hex), where the buffer size (in
\r
1826 // bytes) is given as 2^SSSS and the number of buffers as 2^MMMM.
\r
1827 // We'll check the actual value used near the end of the setup
\r
1829 buffer_bytes = *bufferSize * formatBytes(stream->deviceFormat[mode]) * device_channels;
\r
1830 if (buffer_bytes < 16) buffer_bytes = 16;
\r
1831 buffers = numberOfBuffers;
\r
1832 if (buffers < 2) buffers = 2;
\r
1833 temp = ((int) buffers << 16) + (int)(log10((double)buffer_bytes)/log10(2.0));
\r
1834 if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &temp)) {
\r
1836 sprintf(message, "RtAudio: OSS error setting fragment size for device (%s).",
\r
1840 stream->nBuffers = buffers;
\r
1842 // Set the data format.
\r
1843 temp = device_format;
\r
1844 if (ioctl(fd, SNDCTL_DSP_SETFMT, &device_format) == -1 || device_format != temp) {
\r
1846 sprintf(message, "RtAudio: OSS error setting data format for device (%s).",
\r
1851 // Set the number of channels.
\r
1852 temp = device_channels;
\r
1853 if (ioctl(fd, SNDCTL_DSP_CHANNELS, &device_channels) == -1 || device_channels != temp) {
\r
1855 sprintf(message, "RtAudio: OSS error setting %d channels on device (%s).",
\r
1860 // Set the sample rate.
\r
1861 srate = sampleRate;
\r
1863 if (ioctl(fd, SNDCTL_DSP_SPEED, &srate) == -1) {
\r
1865 sprintf(message, "RtAudio: OSS error setting sample rate = %d on device (%s).",
\r
1870 // Verify the sample rate setup worked.
\r
1871 if (abs(srate - temp) > 100) {
\r
1873 sprintf(message, "RtAudio: OSS error ... audio device (%s) doesn't support sample rate of %d.",
\r
1877 stream->sampleRate = sampleRate;
\r
1879 if (ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &buffer_bytes) == -1) {
\r
1881 sprintf(message, "RtAudio: OSS error getting buffer size for device (%s).",
\r
1886 // Save buffer size (in sample frames).
\r
1887 *bufferSize = buffer_bytes / (formatBytes(stream->deviceFormat[mode]) * device_channels);
\r
1888 stream->bufferSize = *bufferSize;
\r
1890 if (mode == RECORD && stream->mode == PLAYBACK &&
\r
1891 stream->device[0] == device) {
\r
1892 // We're doing duplex setup here.
\r
1893 stream->deviceFormat[0] = stream->deviceFormat[1];
\r
1894 stream->nDeviceChannels[0] = device_channels;
\r
1897 // Set flags for buffer conversion
\r
1898 stream->doConvertBuffer[mode] = false;
\r
1899 if (stream->userFormat != stream->deviceFormat[mode])
\r
1900 stream->doConvertBuffer[mode] = true;
\r
1901 if (stream->nUserChannels[mode] < stream->nDeviceChannels[mode])
\r
1902 stream->doConvertBuffer[mode] = true;
\r
1904 // Allocate necessary internal buffers
\r
1905 if ( stream->nUserChannels[0] != stream->nUserChannels[1] ) {
\r
1907 long buffer_bytes;
\r
1908 if (stream->nUserChannels[0] >= stream->nUserChannels[1])
\r
1909 buffer_bytes = stream->nUserChannels[0];
\r
1911 buffer_bytes = stream->nUserChannels[1];
\r
1913 buffer_bytes *= *bufferSize * formatBytes(stream->userFormat);
\r
1914 if (stream->userBuffer) free(stream->userBuffer);
\r
1915 stream->userBuffer = (char *) calloc(buffer_bytes, 1);
\r
1916 if (stream->userBuffer == NULL) {
\r
1918 sprintf(message, "RtAudio: OSS error allocating user buffer memory (%s).",
\r
1924 if ( stream->doConvertBuffer[mode] ) {
\r
1926 long buffer_bytes;
\r
1927 bool makeBuffer = true;
\r
1928 if ( mode == PLAYBACK )
\r
1929 buffer_bytes = stream->nDeviceChannels[0] * formatBytes(stream->deviceFormat[0]);
\r
1930 else { // mode == RECORD
\r
1931 buffer_bytes = stream->nDeviceChannels[1] * formatBytes(stream->deviceFormat[1]);
\r
1932 if ( stream->mode == PLAYBACK ) {
\r
1933 long bytes_out = stream->nDeviceChannels[0] * formatBytes(stream->deviceFormat[0]);
\r
1934 if ( buffer_bytes > bytes_out )
\r
1935 buffer_bytes = (buffer_bytes > bytes_out) ? buffer_bytes : bytes_out;
\r
1937 makeBuffer = false;
\r
1941 if ( makeBuffer ) {
\r
1942 buffer_bytes *= *bufferSize;
\r
1943 if (stream->deviceBuffer) free(stream->deviceBuffer);
\r
1944 stream->deviceBuffer = (char *) calloc(buffer_bytes, 1);
\r
1945 if (stream->deviceBuffer == NULL) {
\r
1947 free(stream->userBuffer);
\r
1948 sprintf(message, "RtAudio: OSS error allocating device buffer memory (%s).",
\r
1955 stream->device[mode] = device;
\r
1956 stream->handle[mode] = fd;
\r
1957 stream->state = STREAM_STOPPED;
\r
1958 if ( stream->mode == PLAYBACK && mode == RECORD ) {
\r
1959 stream->mode = DUPLEX;
\r
1960 if (stream->device[0] == device)
\r
1961 stream->handle[0] = fd;
\r
1964 stream->mode = mode;
\r
1969 if (stream->handle[0]) {
\r
1970 close(stream->handle[0]);
\r
1971 stream->handle[0] = 0;
\r
1973 error(RtError::WARNING);
\r
1977 void RtAudio :: cancelStreamCallback(int streamId)
\r
1979 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
1981 if (stream->usingCallback) {
\r
1982 stream->usingCallback = false;
\r
1983 pthread_cancel(stream->thread);
\r
1984 pthread_join(stream->thread, NULL);
\r
1985 stream->thread = 0;
\r
1986 stream->callback = NULL;
\r
1987 stream->userData = NULL;
\r
1991 void RtAudio :: closeStream(int streamId)
\r
1993 // We don't want an exception to be thrown here because this
\r
1994 // function is called by our class destructor. So, do our own
\r
1995 // streamId check.
\r
1996 if ( streams.find( streamId ) == streams.end() ) {
\r
1997 sprintf(message, "RtAudio: invalid stream identifier!");
\r
1998 error(RtError::WARNING);
\r
2002 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) streams[streamId];
\r
2004 if (stream->usingCallback) {
\r
2005 pthread_cancel(stream->thread);
\r
2006 pthread_join(stream->thread, NULL);
\r
2009 if (stream->state == STREAM_RUNNING) {
\r
2010 if (stream->mode == PLAYBACK || stream->mode == DUPLEX)
\r
2011 ioctl(stream->handle[0], SNDCTL_DSP_RESET, 0);
\r
2012 if (stream->mode == RECORD || stream->mode == DUPLEX)
\r
2013 ioctl(stream->handle[1], SNDCTL_DSP_RESET, 0);
\r
2016 pthread_mutex_destroy(&stream->mutex);
\r
2018 if (stream->handle[0])
\r
2019 close(stream->handle[0]);
\r
2021 if (stream->handle[1])
\r
2022 close(stream->handle[1]);
\r
2024 if (stream->userBuffer)
\r
2025 free(stream->userBuffer);
\r
2027 if (stream->deviceBuffer)
\r
2028 free(stream->deviceBuffer);
\r
2031 streams.erase(streamId);
\r
2034 void RtAudio :: startStream(int streamId)
\r
2036 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
2038 stream->state = STREAM_RUNNING;
\r
2040 // No need to do anything else here ... OSS automatically starts when fed samples.
\r
2043 void RtAudio :: stopStream(int streamId)
\r
2045 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
2047 MUTEX_LOCK(&stream->mutex);
\r
2049 if (stream->state == STREAM_STOPPED)
\r
2053 if (stream->mode == PLAYBACK || stream->mode == DUPLEX) {
\r
2054 err = ioctl(stream->handle[0], SNDCTL_DSP_SYNC, 0);
\r
2056 sprintf(message, "RtAudio: OSS error stopping device (%s).",
\r
2057 devices[stream->device[0]].name);
\r
2058 error(RtError::DRIVER_ERROR);
\r
2062 err = ioctl(stream->handle[1], SNDCTL_DSP_SYNC, 0);
\r
2064 sprintf(message, "RtAudio: OSS error stopping device (%s).",
\r
2065 devices[stream->device[1]].name);
\r
2066 error(RtError::DRIVER_ERROR);
\r
2069 stream->state = STREAM_STOPPED;
\r
2072 MUTEX_UNLOCK(&stream->mutex);
\r
2075 void RtAudio :: abortStream(int streamId)
\r
2077 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
2079 MUTEX_LOCK(&stream->mutex);
\r
2081 if (stream->state == STREAM_STOPPED)
\r
2085 if (stream->mode == PLAYBACK || stream->mode == DUPLEX) {
\r
2086 err = ioctl(stream->handle[0], SNDCTL_DSP_RESET, 0);
\r
2088 sprintf(message, "RtAudio: OSS error aborting device (%s).",
\r
2089 devices[stream->device[0]].name);
\r
2090 error(RtError::DRIVER_ERROR);
\r
2094 err = ioctl(stream->handle[1], SNDCTL_DSP_RESET, 0);
\r
2096 sprintf(message, "RtAudio: OSS error aborting device (%s).",
\r
2097 devices[stream->device[1]].name);
\r
2098 error(RtError::DRIVER_ERROR);
\r
2101 stream->state = STREAM_STOPPED;
\r
2104 MUTEX_UNLOCK(&stream->mutex);
\r
2107 int RtAudio :: streamWillBlock(int streamId)
\r
2109 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
2111 MUTEX_LOCK(&stream->mutex);
\r
2113 int bytes = 0, channels = 0, frames = 0;
\r
2114 if (stream->state == STREAM_STOPPED)
\r
2117 audio_buf_info info;
\r
2118 if (stream->mode == PLAYBACK || stream->mode == DUPLEX) {
\r
2119 ioctl(stream->handle[0], SNDCTL_DSP_GETOSPACE, &info);
\r
2120 bytes = info.bytes;
\r
2121 channels = stream->nDeviceChannels[0];
\r
2124 if (stream->mode == RECORD || stream->mode == DUPLEX) {
\r
2125 ioctl(stream->handle[1], SNDCTL_DSP_GETISPACE, &info);
\r
2126 if (stream->mode == DUPLEX ) {
\r
2127 bytes = (bytes < info.bytes) ? bytes : info.bytes;
\r
2128 channels = stream->nDeviceChannels[0];
\r
2131 bytes = info.bytes;
\r
2132 channels = stream->nDeviceChannels[1];
\r
2136 frames = (int) (bytes / (channels * formatBytes(stream->deviceFormat[0])));
\r
2137 frames -= stream->bufferSize;
\r
2138 if (frames < 0) frames = 0;
\r
2141 MUTEX_UNLOCK(&stream->mutex);
\r
2145 void RtAudio :: tickStream(int streamId)
\r
2147 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
2149 int stopStream = 0;
\r
2150 if (stream->state == STREAM_STOPPED) {
\r
2151 if (stream->usingCallback) usleep(50000); // sleep 50 milliseconds
\r
2154 else if (stream->usingCallback) {
\r
2155 stopStream = stream->callback(stream->userBuffer, stream->bufferSize, stream->userData);
\r
2158 MUTEX_LOCK(&stream->mutex);
\r
2160 // The state might change while waiting on a mutex.
\r
2161 if (stream->state == STREAM_STOPPED)
\r
2167 RTAUDIO_FORMAT format;
\r
2168 if (stream->mode == PLAYBACK || stream->mode == DUPLEX) {
\r
2170 // Setup parameters and do buffer conversion if necessary.
\r
2171 if (stream->doConvertBuffer[0]) {
\r
2172 convertStreamBuffer(stream, PLAYBACK);
\r
2173 buffer = stream->deviceBuffer;
\r
2174 samples = stream->bufferSize * stream->nDeviceChannels[0];
\r
2175 format = stream->deviceFormat[0];
\r
2178 buffer = stream->userBuffer;
\r
2179 samples = stream->bufferSize * stream->nUserChannels[0];
\r
2180 format = stream->userFormat;
\r
2183 // Do byte swapping if necessary.
\r
2184 if (stream->doByteSwap[0])
\r
2185 byteSwapBuffer(buffer, samples, format);
\r
2187 // Write samples to device.
\r
2188 result = write(stream->handle[0], buffer, samples * formatBytes(format));
\r
2190 if (result == -1) {
\r
2191 // This could be an underrun, but the basic OSS API doesn't provide a means for determining that.
\r
2192 sprintf(message, "RtAudio: OSS audio write error for device (%s).",
\r
2193 devices[stream->device[0]].name);
\r
2194 error(RtError::DRIVER_ERROR);
\r
2198 if (stream->mode == RECORD || stream->mode == DUPLEX) {
\r
2200 // Setup parameters.
\r
2201 if (stream->doConvertBuffer[1]) {
\r
2202 buffer = stream->deviceBuffer;
\r
2203 samples = stream->bufferSize * stream->nDeviceChannels[1];
\r
2204 format = stream->deviceFormat[1];
\r
2207 buffer = stream->userBuffer;
\r
2208 samples = stream->bufferSize * stream->nUserChannels[1];
\r
2209 format = stream->userFormat;
\r
2212 // Read samples from device.
\r
2213 result = read(stream->handle[1], buffer, samples * formatBytes(format));
\r
2215 if (result == -1) {
\r
2216 // This could be an overrun, but the basic OSS API doesn't provide a means for determining that.
\r
2217 sprintf(message, "RtAudio: OSS audio read error for device (%s).",
\r
2218 devices[stream->device[1]].name);
\r
2219 error(RtError::DRIVER_ERROR);
\r
2222 // Do byte swapping if necessary.
\r
2223 if (stream->doByteSwap[1])
\r
2224 byteSwapBuffer(buffer, samples, format);
\r
2226 // Do buffer conversion if necessary.
\r
2227 if (stream->doConvertBuffer[1])
\r
2228 convertStreamBuffer(stream, RECORD);
\r
2232 MUTEX_UNLOCK(&stream->mutex);
\r
2234 if (stream->usingCallback && stopStream)
\r
2235 this->stopStream(streamId);
\r
2238 extern "C" void *callbackHandler(void *ptr)
\r
2240 RtAudio *object = thread_info.object;
\r
2241 int stream = thread_info.streamId;
\r
2242 bool *usingCallback = (bool *) ptr;
\r
2244 while ( *usingCallback ) {
\r
2245 pthread_testcancel();
\r
2247 object->tickStream(stream);
\r
2249 catch (RtError &exception) {
\r
2250 fprintf(stderr, "\nCallback thread error (%s) ... closing thread.\n\n",
\r
2251 exception.getMessage());
\r
2259 //******************** End of __LINUX_OSS__ *********************//
\r
2261 #elif defined(__WINDOWS_DS__) // Windows DirectSound API
\r
2263 #include <dsound.h>
\r
2265 // Declarations for utility functions, callbacks, and structures
\r
2266 // specific to the DirectSound implementation.
\r
2267 static bool CALLBACK deviceCountCallback(LPGUID lpguid,
\r
2268 LPCSTR lpcstrDescription,
\r
2269 LPCSTR lpcstrModule,
\r
2270 LPVOID lpContext);
\r
2272 static bool CALLBACK deviceInfoCallback(LPGUID lpguid,
\r
2273 LPCSTR lpcstrDescription,
\r
2274 LPCSTR lpcstrModule,
\r
2275 LPVOID lpContext);
\r
2277 static char* getErrorString(int code);
\r
2279 struct enum_info {
\r
2286 // RtAudio methods for DirectSound implementation.
\r
2287 void RtAudio :: initialize(void)
\r
2289 int i, ins = 0, outs = 0, count = 0;
\r
2294 // Count DirectSound devices.
\r
2295 result = DirectSoundEnumerate((LPDSENUMCALLBACK)deviceCountCallback, &outs);
\r
2296 if ( FAILED(result) ) {
\r
2297 sprintf(message, "RtAudio: Unable to enumerate through sound playback devices: %s.",
\r
2298 getErrorString(result));
\r
2299 error(RtError::DRIVER_ERROR);
\r
2302 // Count DirectSoundCapture devices.
\r
2303 result = DirectSoundCaptureEnumerate((LPDSENUMCALLBACK)deviceCountCallback, &ins);
\r
2304 if ( FAILED(result) ) {
\r
2305 sprintf(message, "RtAudio: Unable to enumerate through sound capture devices: %s.",
\r
2306 getErrorString(result));
\r
2307 error(RtError::DRIVER_ERROR);
\r
2310 count = ins + outs;
\r
2311 if (count == 0) return;
\r
2313 std::vector<enum_info> info(count);
\r
2314 for (i=0; i<count; i++) {
\r
2315 info[i].name[0] = '\0';
\r
2316 if (i < outs) info[i].isInput = false;
\r
2317 else info[i].isInput = true;
\r
2320 // Get playback device info and check capabilities.
\r
2321 result = DirectSoundEnumerate((LPDSENUMCALLBACK)deviceInfoCallback, &info[0]);
\r
2322 if ( FAILED(result) ) {
\r
2323 sprintf(message, "RtAudio: Unable to enumerate through sound playback devices: %s.",
\r
2324 getErrorString(result));
\r
2325 error(RtError::DRIVER_ERROR);
\r
2328 // Get capture device info and check capabilities.
\r
2329 result = DirectSoundCaptureEnumerate((LPDSENUMCALLBACK)deviceInfoCallback, &info[0]);
\r
2330 if ( FAILED(result) ) {
\r
2331 sprintf(message, "RtAudio: Unable to enumerate through sound capture devices: %s.",
\r
2332 getErrorString(result));
\r
2333 error(RtError::DRIVER_ERROR);
\r
2336 // Parse the devices and check validity. Devices are considered
\r
2337 // invalid if they cannot be opened, they report no supported data
\r
2338 // formats, or they report < 1 supported channels.
\r
2339 for (i=0; i<count; i++) {
\r
2340 if (info[i].isValid && info[i].id == NULL ) // default device
\r
2344 // We group the default input and output devices together (as one
\r
2346 if (nDevices > 0) {
\r
2351 // Non-default devices are listed separately.
\r
2352 for (i=0; i<count; i++) {
\r
2353 if (info[i].isValid && info[i].id != NULL )
\r
2357 if (nDevices == 0) return;
\r
2359 // Allocate the RTAUDIO_DEVICE structures.
\r
2360 devices = (RTAUDIO_DEVICE *) calloc(nDevices, sizeof(RTAUDIO_DEVICE));
\r
2361 if (devices == NULL) {
\r
2362 sprintf(message, "RtAudio: memory allocation error!");
\r
2363 error(RtError::MEMORY_ERROR);
\r
2366 // Initialize the GUIDs to NULL for later validation.
\r
2367 for (i=0; i<nDevices; i++) {
\r
2368 devices[i].id[0] = NULL;
\r
2369 devices[i].id[1] = NULL;
\r
2372 // Rename the default device(s).
\r
2374 strcpy(devices[0].name, "Default Input/Output Devices");
\r
2376 // Copy the names and GUIDs to our devices structures.
\r
2377 for (i=0; i<count; i++) {
\r
2378 if (info[i].isValid && info[i].id != NULL ) {
\r
2379 strncpy(devices[index].name, info[i].name, 64);
\r
2380 if (info[i].isInput)
\r
2381 devices[index].id[1] = info[i].id;
\r
2383 devices[index].id[0] = info[i].id;
\r
2388 for (i=0;i<nDevices; i++)
\r
2389 probeDeviceInfo(&devices[i]);
\r
2394 void RtAudio :: probeDeviceInfo(RTAUDIO_DEVICE *info)
\r
2398 // Get the device index so that we can check the device handle.
\r
2400 for (index=0; index<nDevices; index++)
\r
2401 if ( info == &devices[index] ) break;
\r
2403 if ( index >= nDevices ) {
\r
2404 sprintf(message, "RtAudio: device (%s) indexing error in DirectSound probeDeviceInfo().",
\r
2406 error(RtError::WARNING);
\r
2410 // Do capture probe first. If this is not the default device (index
\r
2411 // = 0) _and_ GUID = NULL, then the capture handle is invalid.
\r
2412 if ( index != 0 && info->id[1] == NULL )
\r
2413 goto playback_probe;
\r
2415 LPDIRECTSOUNDCAPTURE input;
\r
2416 result = DirectSoundCaptureCreate( info->id[0], &input, NULL );
\r
2417 if ( FAILED(result) ) {
\r
2418 sprintf(message, "RtAudio: Could not create DirectSound capture object (%s): %s.",
\r
2419 info->name, getErrorString(result));
\r
2420 error(RtError::WARNING);
\r
2421 goto playback_probe;
\r
2425 in_caps.dwSize = sizeof(in_caps);
\r
2426 result = input->GetCaps( &in_caps );
\r
2427 if ( FAILED(result) ) {
\r
2429 sprintf(message, "RtAudio: Could not get DirectSound capture capabilities (%s): %s.",
\r
2430 info->name, getErrorString(result));
\r
2431 error(RtError::WARNING);
\r
2432 goto playback_probe;
\r
2435 // Get input channel information.
\r
2436 info->minInputChannels = 1;
\r
2437 info->maxInputChannels = in_caps.dwChannels;
\r
2439 // Get sample rate and format information.
\r
2440 if( in_caps.dwChannels == 2 ) {
\r
2441 if( in_caps.dwFormats & WAVE_FORMAT_1S16 ) info->nativeFormats |= RTAUDIO_SINT16;
\r
2442 if( in_caps.dwFormats & WAVE_FORMAT_2S16 ) info->nativeFormats |= RTAUDIO_SINT16;
\r
2443 if( in_caps.dwFormats & WAVE_FORMAT_4S16 ) info->nativeFormats |= RTAUDIO_SINT16;
\r
2444 if( in_caps.dwFormats & WAVE_FORMAT_1S08 ) info->nativeFormats |= RTAUDIO_SINT8;
\r
2445 if( in_caps.dwFormats & WAVE_FORMAT_2S08 ) info->nativeFormats |= RTAUDIO_SINT8;
\r
2446 if( in_caps.dwFormats & WAVE_FORMAT_4S08 ) info->nativeFormats |= RTAUDIO_SINT8;
\r
2448 if ( info->nativeFormats & RTAUDIO_SINT16 ) {
\r
2449 if( in_caps.dwFormats & WAVE_FORMAT_1S16 ) info->sampleRates[info->nSampleRates++] = 11025;
\r
2450 if( in_caps.dwFormats & WAVE_FORMAT_2S16 ) info->sampleRates[info->nSampleRates++] = 22050;
\r
2451 if( in_caps.dwFormats & WAVE_FORMAT_4S16 ) info->sampleRates[info->nSampleRates++] = 44100;
\r
2453 else if ( info->nativeFormats & RTAUDIO_SINT8 ) {
\r
2454 if( in_caps.dwFormats & WAVE_FORMAT_1S08 ) info->sampleRates[info->nSampleRates++] = 11025;
\r
2455 if( in_caps.dwFormats & WAVE_FORMAT_2S08 ) info->sampleRates[info->nSampleRates++] = 22050;
\r
2456 if( in_caps.dwFormats & WAVE_FORMAT_4S08 ) info->sampleRates[info->nSampleRates++] = 44100;
\r
2459 else if ( in_caps.dwChannels == 1 ) {
\r
2460 if( in_caps.dwFormats & WAVE_FORMAT_1M16 ) info->nativeFormats |= RTAUDIO_SINT16;
\r
2461 if( in_caps.dwFormats & WAVE_FORMAT_2M16 ) info->nativeFormats |= RTAUDIO_SINT16;
\r
2462 if( in_caps.dwFormats & WAVE_FORMAT_4M16 ) info->nativeFormats |= RTAUDIO_SINT16;
\r
2463 if( in_caps.dwFormats & WAVE_FORMAT_1M08 ) info->nativeFormats |= RTAUDIO_SINT8;
\r
2464 if( in_caps.dwFormats & WAVE_FORMAT_2M08 ) info->nativeFormats |= RTAUDIO_SINT8;
\r
2465 if( in_caps.dwFormats & WAVE_FORMAT_4M08 ) info->nativeFormats |= RTAUDIO_SINT8;
\r
2467 if ( info->nativeFormats & RTAUDIO_SINT16 ) {
\r
2468 if( in_caps.dwFormats & WAVE_FORMAT_1M16 ) info->sampleRates[info->nSampleRates++] = 11025;
\r
2469 if( in_caps.dwFormats & WAVE_FORMAT_2M16 ) info->sampleRates[info->nSampleRates++] = 22050;
\r
2470 if( in_caps.dwFormats & WAVE_FORMAT_4M16 ) info->sampleRates[info->nSampleRates++] = 44100;
\r
2472 else if ( info->nativeFormats & RTAUDIO_SINT8 ) {
\r
2473 if( in_caps.dwFormats & WAVE_FORMAT_1M08 ) info->sampleRates[info->nSampleRates++] = 11025;
\r
2474 if( in_caps.dwFormats & WAVE_FORMAT_2M08 ) info->sampleRates[info->nSampleRates++] = 22050;
\r
2475 if( in_caps.dwFormats & WAVE_FORMAT_4M08 ) info->sampleRates[info->nSampleRates++] = 44100;
\r
2478 else info->minInputChannels = 0; // technically, this would be an error
\r
2483 LPDIRECTSOUND output;
\r
2486 // Now do playback probe. If this is not the default device (index
\r
2487 // = 0) _and_ GUID = NULL, then the playback handle is invalid.
\r
2488 if ( index != 0 && info->id[0] == NULL )
\r
2489 goto check_parameters;
\r
2491 result = DirectSoundCreate( info->id[0], &output, NULL );
\r
2492 if ( FAILED(result) ) {
\r
2493 sprintf(message, "RtAudio: Could not create DirectSound playback object (%s): %s.",
\r
2494 info->name, getErrorString(result));
\r
2495 error(RtError::WARNING);
\r
2496 goto check_parameters;
\r
2499 out_caps.dwSize = sizeof(out_caps);
\r
2500 result = output->GetCaps( &out_caps );
\r
2501 if ( FAILED(result) ) {
\r
2502 output->Release();
\r
2503 sprintf(message, "RtAudio: Could not get DirectSound playback capabilities (%s): %s.",
\r
2504 info->name, getErrorString(result));
\r
2505 error(RtError::WARNING);
\r
2506 goto check_parameters;
\r
2509 // Get output channel information.
\r
2510 info->minOutputChannels = 1;
\r
2511 info->maxOutputChannels = ( out_caps.dwFlags & DSCAPS_PRIMARYSTEREO ) ? 2 : 1;
\r
2513 // Get sample rate information. Use capture device rate information
\r
2515 if ( info->nSampleRates == 0 ) {
\r
2516 info->sampleRates[0] = (int) out_caps.dwMinSecondarySampleRate;
\r
2517 info->sampleRates[1] = (int) out_caps.dwMaxSecondarySampleRate;
\r
2518 if ( out_caps.dwFlags & DSCAPS_CONTINUOUSRATE )
\r
2519 info->nSampleRates = -1;
\r
2520 else if ( out_caps.dwMinSecondarySampleRate == out_caps.dwMaxSecondarySampleRate ) {
\r
2521 if ( out_caps.dwMinSecondarySampleRate == 0 ) {
\r
2522 // This is a bogus driver report ... fake the range and cross
\r
2524 info->sampleRates[0] = 11025;
\r
2525 info->sampleRates[1] = 48000;
\r
2526 info->nSampleRates = -1; /* continuous range */
\r
2527 sprintf(message, "RtAudio: bogus sample rates reported by DirectSound driver ... using defaults (%s).",
\r
2529 error(RtError::WARNING);
\r
2532 info->nSampleRates = 1;
\r
2535 else if ( (out_caps.dwMinSecondarySampleRate < 1000.0) &&
\r
2536 (out_caps.dwMaxSecondarySampleRate > 50000.0) ) {
\r
2537 // This is a bogus driver report ... support for only two
\r
2538 // distant rates. We'll assume this is a range.
\r
2539 info->nSampleRates = -1;
\r
2540 sprintf(message, "RtAudio: bogus sample rates reported by DirectSound driver ... using range (%s).",
\r
2542 error(RtError::WARNING);
\r
2544 else info->nSampleRates = 2;
\r
2547 // Check input rates against output rate range
\r
2548 for ( int i=info->nSampleRates-1; i>=0; i-- ) {
\r
2549 if ( info->sampleRates[i] <= out_caps.dwMaxSecondarySampleRate )
\r
2551 info->nSampleRates--;
\r
2553 while ( info->sampleRates[0] < out_caps.dwMinSecondarySampleRate ) {
\r
2554 info->nSampleRates--;
\r
2555 for ( int i=0; i<info->nSampleRates; i++)
\r
2556 info->sampleRates[i] = info->sampleRates[i+1];
\r
2557 if ( info->nSampleRates <= 0 ) break;
\r
2561 // Get format information.
\r
2562 if ( out_caps.dwFlags & DSCAPS_PRIMARY16BIT ) info->nativeFormats |= RTAUDIO_SINT16;
\r
2563 if ( out_caps.dwFlags & DSCAPS_PRIMARY8BIT ) info->nativeFormats |= RTAUDIO_SINT8;
\r
2565 output->Release();
\r
2568 if ( info->maxInputChannels == 0 && info->maxOutputChannels == 0 )
\r
2570 if ( info->nSampleRates == 0 || info->nativeFormats == 0 )
\r
2573 // Determine duplex status.
\r
2574 if (info->maxInputChannels < info->maxOutputChannels)
\r
2575 info->maxDuplexChannels = info->maxInputChannels;
\r
2577 info->maxDuplexChannels = info->maxOutputChannels;
\r
2578 if (info->minInputChannels < info->minOutputChannels)
\r
2579 info->minDuplexChannels = info->minInputChannels;
\r
2581 info->minDuplexChannels = info->minOutputChannels;
\r
2583 if ( info->maxDuplexChannels > 0 ) info->hasDuplexSupport = true;
\r
2584 else info->hasDuplexSupport = false;
\r
2586 info->probed = true;
\r
2591 bool RtAudio :: probeDeviceOpen(int device, RTAUDIO_STREAM *stream,
\r
2592 STREAM_MODE mode, int channels,
\r
2593 int sampleRate, RTAUDIO_FORMAT format,
\r
2594 int *bufferSize, int numberOfBuffers)
\r
2597 HWND hWnd = GetForegroundWindow();
\r
2598 // According to a note in PortAudio, using GetDesktopWindow()
\r
2599 // instead of GetForegroundWindow() is supposed to avoid problems
\r
2600 // that occur when the application's window is not the foreground
\r
2601 // window. Also, if the application window closes before the
\r
2602 // DirectSound buffer, DirectSound can crash. However, for console
\r
2603 // applications, no sound was produced when using GetDesktopWindow().
\r
2609 // Check the numberOfBuffers parameter and limit the lowest value to
\r
2610 // two. This is a judgement call and a value of two is probably too
\r
2611 // low for capture, but it should work for playback.
\r
2612 if (numberOfBuffers < 2)
\r
2615 nBuffers = numberOfBuffers;
\r
2617 // Define the wave format structure (16-bit PCM, srate, channels)
\r
2618 WAVEFORMATEX waveFormat;
\r
2619 ZeroMemory(&waveFormat, sizeof(WAVEFORMATEX));
\r
2620 waveFormat.wFormatTag = WAVE_FORMAT_PCM;
\r
2621 waveFormat.nChannels = channels;
\r
2622 waveFormat.nSamplesPerSec = (unsigned long) sampleRate;
\r
2624 // Determine the data format.
\r
2625 if ( devices[device].nativeFormats ) { // 8-bit and/or 16-bit support
\r
2626 if ( format == RTAUDIO_SINT8 ) {
\r
2627 if ( devices[device].nativeFormats & RTAUDIO_SINT8 )
\r
2628 waveFormat.wBitsPerSample = 8;
\r
2630 waveFormat.wBitsPerSample = 16;
\r
2633 if ( devices[device].nativeFormats & RTAUDIO_SINT16 )
\r
2634 waveFormat.wBitsPerSample = 16;
\r
2636 waveFormat.wBitsPerSample = 8;
\r
2640 sprintf(message, "RtAudio: no reported data formats for DirectSound device (%s).",
\r
2641 devices[device].name);
\r
2642 error(RtError::WARNING);
\r
2646 waveFormat.nBlockAlign = waveFormat.nChannels * waveFormat.wBitsPerSample / 8;
\r
2647 waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign;
\r
2649 if ( mode == PLAYBACK ) {
\r
2651 if ( devices[device].maxOutputChannels < channels )
\r
2654 LPGUID id = devices[device].id[0];
\r
2655 LPDIRECTSOUND object;
\r
2656 LPDIRECTSOUNDBUFFER buffer;
\r
2657 DSBUFFERDESC bufferDescription;
\r
2659 result = DirectSoundCreate( id, &object, NULL );
\r
2660 if ( FAILED(result) ) {
\r
2661 sprintf(message, "RtAudio: Could not create DirectSound playback object (%s): %s.",
\r
2662 devices[device].name, getErrorString(result));
\r
2663 error(RtError::WARNING);
\r
2667 // Set cooperative level to DSSCL_EXCLUSIVE
\r
2668 result = object->SetCooperativeLevel(hWnd, DSSCL_EXCLUSIVE);
\r
2669 if ( FAILED(result) ) {
\r
2670 object->Release();
\r
2671 sprintf(message, "RtAudio: Unable to set DirectSound cooperative level (%s): %s.",
\r
2672 devices[device].name, getErrorString(result));
\r
2673 error(RtError::WARNING);
\r
2677 // Even though we will write to the secondary buffer, we need to
\r
2678 // access the primary buffer to set the correct output format.
\r
2679 // The default is 8-bit, 22 kHz!
\r
2680 // Setup the DS primary buffer description.
\r
2681 ZeroMemory(&bufferDescription, sizeof(DSBUFFERDESC));
\r
2682 bufferDescription.dwSize = sizeof(DSBUFFERDESC);
\r
2683 bufferDescription.dwFlags = DSBCAPS_PRIMARYBUFFER;
\r
2684 // Obtain the primary buffer
\r
2685 result = object->CreateSoundBuffer(&bufferDescription, &buffer, NULL);
\r
2686 if ( FAILED(result) ) {
\r
2687 object->Release();
\r
2688 sprintf(message, "RtAudio: Unable to access DS primary buffer (%s): %s.",
\r
2689 devices[device].name, getErrorString(result));
\r
2690 error(RtError::WARNING);
\r
2694 // Set the primary DS buffer sound format.
\r
2695 result = buffer->SetFormat(&waveFormat);
\r
2696 if ( FAILED(result) ) {
\r
2697 object->Release();
\r
2698 sprintf(message, "RtAudio: Unable to set DS primary buffer format (%s): %s.",
\r
2699 devices[device].name, getErrorString(result));
\r
2700 error(RtError::WARNING);
\r
2704 // Setup the secondary DS buffer description.
\r
2705 buffer_size = channels * *bufferSize * nBuffers * waveFormat.wBitsPerSample / 8;
\r
2706 ZeroMemory(&bufferDescription, sizeof(DSBUFFERDESC));
\r
2707 bufferDescription.dwSize = sizeof(DSBUFFERDESC);
\r
2708 bufferDescription.dwFlags = ( DSBCAPS_STICKYFOCUS |
\r
2709 DSBCAPS_GETCURRENTPOSITION2 |
\r
2710 DSBCAPS_LOCHARDWARE ); // Force hardware mixing
\r
2711 bufferDescription.dwBufferBytes = buffer_size;
\r
2712 bufferDescription.lpwfxFormat = &waveFormat;
\r
2714 // Try to create the secondary DS buffer. If that doesn't work,
\r
2715 // try to use software mixing. Otherwise, there's a problem.
\r
2716 result = object->CreateSoundBuffer(&bufferDescription, &buffer, NULL);
\r
2717 if ( FAILED(result) ) {
\r
2718 bufferDescription.dwFlags = ( DSBCAPS_STICKYFOCUS |
\r
2719 DSBCAPS_GETCURRENTPOSITION2 |
\r
2720 DSBCAPS_LOCSOFTWARE ); // Force software mixing
\r
2721 result = object->CreateSoundBuffer(&bufferDescription, &buffer, NULL);
\r
2722 if ( FAILED(result) ) {
\r
2723 object->Release();
\r
2724 sprintf(message, "RtAudio: Unable to create secondary DS buffer (%s): %s.",
\r
2725 devices[device].name, getErrorString(result));
\r
2726 error(RtError::WARNING);
\r
2731 // Get the buffer size ... might be different from what we specified.
\r
2733 dsbcaps.dwSize = sizeof(DSBCAPS);
\r
2734 buffer->GetCaps(&dsbcaps);
\r
2735 buffer_size = dsbcaps.dwBufferBytes;
\r
2737 // Lock the DS buffer
\r
2738 result = buffer->Lock(0, buffer_size, &audioPtr, &dataLen, NULL, NULL, 0);
\r
2739 if ( FAILED(result) ) {
\r
2740 object->Release();
\r
2741 sprintf(message, "RtAudio: Unable to lock DS buffer (%s): %s.",
\r
2742 devices[device].name, getErrorString(result));
\r
2743 error(RtError::WARNING);
\r
2747 // Zero the DS buffer
\r
2748 ZeroMemory(audioPtr, dataLen);
\r
2750 // Unlock the DS buffer
\r
2751 result = buffer->Unlock(audioPtr, dataLen, NULL, 0);
\r
2752 if ( FAILED(result) ) {
\r
2753 object->Release();
\r
2754 sprintf(message, "RtAudio: Unable to unlock DS buffer(%s): %s.",
\r
2755 devices[device].name, getErrorString(result));
\r
2756 error(RtError::WARNING);
\r
2760 stream->handle[0].object = (void *) object;
\r
2761 stream->handle[0].buffer = (void *) buffer;
\r
2762 stream->nDeviceChannels[0] = channels;
\r
2765 if ( mode == RECORD ) {
\r
2767 if ( devices[device].maxInputChannels < channels )
\r
2770 LPGUID id = devices[device].id[1];
\r
2771 LPDIRECTSOUNDCAPTURE object;
\r
2772 LPDIRECTSOUNDCAPTUREBUFFER buffer;
\r
2773 DSCBUFFERDESC bufferDescription;
\r
2775 result = DirectSoundCaptureCreate( id, &object, NULL );
\r
2776 if ( FAILED(result) ) {
\r
2777 sprintf(message, "RtAudio: Could not create DirectSound capture object (%s): %s.",
\r
2778 devices[device].name, getErrorString(result));
\r
2779 error(RtError::WARNING);
\r
2783 // Setup the secondary DS buffer description.
\r
2784 buffer_size = channels * *bufferSize * nBuffers * waveFormat.wBitsPerSample / 8;
\r
2785 ZeroMemory(&bufferDescription, sizeof(DSCBUFFERDESC));
\r
2786 bufferDescription.dwSize = sizeof(DSCBUFFERDESC);
\r
2787 bufferDescription.dwFlags = 0;
\r
2788 bufferDescription.dwReserved = 0;
\r
2789 bufferDescription.dwBufferBytes = buffer_size;
\r
2790 bufferDescription.lpwfxFormat = &waveFormat;
\r
2792 // Create the capture buffer.
\r
2793 result = object->CreateCaptureBuffer(&bufferDescription, &buffer, NULL);
\r
2794 if ( FAILED(result) ) {
\r
2795 object->Release();
\r
2796 sprintf(message, "RtAudio: Unable to create DS capture buffer (%s): %s.",
\r
2797 devices[device].name, getErrorString(result));
\r
2798 error(RtError::WARNING);
\r
2802 // Lock the capture buffer
\r
2803 result = buffer->Lock(0, buffer_size, &audioPtr, &dataLen, NULL, NULL, 0);
\r
2804 if ( FAILED(result) ) {
\r
2805 object->Release();
\r
2806 sprintf(message, "RtAudio: Unable to lock DS capture buffer (%s): %s.",
\r
2807 devices[device].name, getErrorString(result));
\r
2808 error(RtError::WARNING);
\r
2812 // Zero the buffer
\r
2813 ZeroMemory(audioPtr, dataLen);
\r
2815 // Unlock the buffer
\r
2816 result = buffer->Unlock(audioPtr, dataLen, NULL, 0);
\r
2817 if ( FAILED(result) ) {
\r
2818 object->Release();
\r
2819 sprintf(message, "RtAudio: Unable to unlock DS capture buffer (%s): %s.",
\r
2820 devices[device].name, getErrorString(result));
\r
2821 error(RtError::WARNING);
\r
2825 stream->handle[1].object = (void *) object;
\r
2826 stream->handle[1].buffer = (void *) buffer;
\r
2827 stream->nDeviceChannels[1] = channels;
\r
2830 stream->userFormat = format;
\r
2831 if ( waveFormat.wBitsPerSample == 8 )
\r
2832 stream->deviceFormat[mode] = RTAUDIO_SINT8;
\r
2834 stream->deviceFormat[mode] = RTAUDIO_SINT16;
\r
2835 stream->nUserChannels[mode] = channels;
\r
2836 *bufferSize = buffer_size / (channels * nBuffers * waveFormat.wBitsPerSample / 8);
\r
2837 stream->bufferSize = *bufferSize;
\r
2839 // Set flags for buffer conversion
\r
2840 stream->doConvertBuffer[mode] = false;
\r
2841 if (stream->userFormat != stream->deviceFormat[mode])
\r
2842 stream->doConvertBuffer[mode] = true;
\r
2843 if (stream->nUserChannels[mode] < stream->nDeviceChannels[mode])
\r
2844 stream->doConvertBuffer[mode] = true;
\r
2846 // Allocate necessary internal buffers
\r
2847 if ( stream->nUserChannels[0] != stream->nUserChannels[1] ) {
\r
2849 long buffer_bytes;
\r
2850 if (stream->nUserChannels[0] >= stream->nUserChannels[1])
\r
2851 buffer_bytes = stream->nUserChannels[0];
\r
2853 buffer_bytes = stream->nUserChannels[1];
\r
2855 buffer_bytes *= *bufferSize * formatBytes(stream->userFormat);
\r
2856 if (stream->userBuffer) free(stream->userBuffer);
\r
2857 stream->userBuffer = (char *) calloc(buffer_bytes, 1);
\r
2858 if (stream->userBuffer == NULL)
\r
2859 goto memory_error;
\r
2862 if ( stream->doConvertBuffer[mode] ) {
\r
2864 long buffer_bytes;
\r
2865 bool makeBuffer = true;
\r
2866 if ( mode == PLAYBACK )
\r
2867 buffer_bytes = stream->nDeviceChannels[0] * formatBytes(stream->deviceFormat[0]);
\r
2868 else { // mode == RECORD
\r
2869 buffer_bytes = stream->nDeviceChannels[1] * formatBytes(stream->deviceFormat[1]);
\r
2870 if ( stream->mode == PLAYBACK ) {
\r
2871 long bytes_out = stream->nDeviceChannels[0] * formatBytes(stream->deviceFormat[0]);
\r
2872 if ( buffer_bytes > bytes_out )
\r
2873 buffer_bytes = (buffer_bytes > bytes_out) ? buffer_bytes : bytes_out;
\r
2875 makeBuffer = false;
\r
2879 if ( makeBuffer ) {
\r
2880 buffer_bytes *= *bufferSize;
\r
2881 if (stream->deviceBuffer) free(stream->deviceBuffer);
\r
2882 stream->deviceBuffer = (char *) calloc(buffer_bytes, 1);
\r
2883 if (stream->deviceBuffer == NULL)
\r
2884 goto memory_error;
\r
2888 stream->device[mode] = device;
\r
2889 stream->state = STREAM_STOPPED;
\r
2890 if ( stream->mode == PLAYBACK && mode == RECORD )
\r
2891 // We had already set up an output stream.
\r
2892 stream->mode = DUPLEX;
\r
2894 stream->mode = mode;
\r
2895 stream->nBuffers = nBuffers;
\r
2896 stream->sampleRate = sampleRate;
\r
2901 if (stream->handle[0].object) {
\r
2902 LPDIRECTSOUND object = (LPDIRECTSOUND) stream->handle[0].object;
\r
2903 LPDIRECTSOUNDBUFFER buffer = (LPDIRECTSOUNDBUFFER) stream->handle[0].buffer;
\r
2905 buffer->Release();
\r
2906 stream->handle[0].buffer = NULL;
\r
2908 object->Release();
\r
2909 stream->handle[0].object = NULL;
\r
2911 if (stream->handle[1].object) {
\r
2912 LPDIRECTSOUNDCAPTURE object = (LPDIRECTSOUNDCAPTURE) stream->handle[1].object;
\r
2913 LPDIRECTSOUNDCAPTUREBUFFER buffer = (LPDIRECTSOUNDCAPTUREBUFFER) stream->handle[1].buffer;
\r
2915 buffer->Release();
\r
2916 stream->handle[1].buffer = NULL;
\r
2918 object->Release();
\r
2919 stream->handle[1].object = NULL;
\r
2921 if (stream->userBuffer) {
\r
2922 free(stream->userBuffer);
\r
2923 stream->userBuffer = 0;
\r
2925 sprintf(message, "RtAudio: error allocating buffer memory (%s).",
\r
2926 devices[device].name);
\r
2927 error(RtError::WARNING);
\r
2931 void RtAudio :: cancelStreamCallback(int streamId)
\r
2933 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
2935 if (stream->usingCallback) {
\r
2936 stream->usingCallback = false;
\r
2937 WaitForSingleObject( (HANDLE)stream->thread, INFINITE );
\r
2938 CloseHandle( (HANDLE)stream->thread );
\r
2939 stream->thread = 0;
\r
2940 stream->callback = NULL;
\r
2941 stream->userData = NULL;
\r
2945 void RtAudio :: closeStream(int streamId)
\r
2947 // We don't want an exception to be thrown here because this
\r
2948 // function is called by our class destructor. So, do our own
\r
2949 // streamId check.
\r
2950 if ( streams.find( streamId ) == streams.end() ) {
\r
2951 sprintf(message, "RtAudio: invalid stream identifier!");
\r
2952 error(RtError::WARNING);
\r
2956 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) streams[streamId];
\r
2958 if (stream->usingCallback) {
\r
2959 stream->usingCallback = false;
\r
2960 WaitForSingleObject( (HANDLE)stream->thread, INFINITE );
\r
2961 CloseHandle( (HANDLE)stream->thread );
\r
2964 DeleteCriticalSection(&stream->mutex);
\r
2966 if (stream->handle[0].object) {
\r
2967 LPDIRECTSOUND object = (LPDIRECTSOUND) stream->handle[0].object;
\r
2968 LPDIRECTSOUNDBUFFER buffer = (LPDIRECTSOUNDBUFFER) stream->handle[0].buffer;
\r
2971 buffer->Release();
\r
2973 object->Release();
\r
2976 if (stream->handle[1].object) {
\r
2977 LPDIRECTSOUNDCAPTURE object = (LPDIRECTSOUNDCAPTURE) stream->handle[1].object;
\r
2978 LPDIRECTSOUNDCAPTUREBUFFER buffer = (LPDIRECTSOUNDCAPTUREBUFFER) stream->handle[1].buffer;
\r
2981 buffer->Release();
\r
2983 object->Release();
\r
2986 if (stream->userBuffer)
\r
2987 free(stream->userBuffer);
\r
2989 if (stream->deviceBuffer)
\r
2990 free(stream->deviceBuffer);
\r
2993 streams.erase(streamId);
\r
2996 void RtAudio :: startStream(int streamId)
\r
2998 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
3000 MUTEX_LOCK(&stream->mutex);
\r
3002 if (stream->state == STREAM_RUNNING)
\r
3006 if (stream->mode == PLAYBACK || stream->mode == DUPLEX) {
\r
3007 LPDIRECTSOUNDBUFFER buffer = (LPDIRECTSOUNDBUFFER) stream->handle[0].buffer;
\r
3008 result = buffer->Play(0, 0, DSBPLAY_LOOPING );
\r
3009 if ( FAILED(result) ) {
\r
3010 sprintf(message, "RtAudio: Unable to start DS buffer (%s): %s.",
\r
3011 devices[stream->device[0]].name, getErrorString(result));
\r
3012 error(RtError::DRIVER_ERROR);
\r
3016 if (stream->mode == RECORD || stream->mode == DUPLEX) {
\r
3017 LPDIRECTSOUNDCAPTUREBUFFER buffer = (LPDIRECTSOUNDCAPTUREBUFFER) stream->handle[1].buffer;
\r
3018 result = buffer->Start(DSCBSTART_LOOPING );
\r
3019 if ( FAILED(result) ) {
\r
3020 sprintf(message, "RtAudio: Unable to start DS capture buffer (%s): %s.",
\r
3021 devices[stream->device[1]].name, getErrorString(result));
\r
3022 error(RtError::DRIVER_ERROR);
\r
3025 stream->state = STREAM_RUNNING;
\r
3028 MUTEX_UNLOCK(&stream->mutex);
\r
3031 void RtAudio :: stopStream(int streamId)
\r
3033 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
3035 MUTEX_LOCK(&stream->mutex);
\r
3037 if (stream->state == STREAM_STOPPED) {
\r
3038 MUTEX_UNLOCK(&stream->mutex);
\r
3042 // There is no specific DirectSound API call to "drain" a buffer
\r
3043 // before stopping. We can hack this for playback by writing zeroes
\r
3044 // for another bufferSize * nBuffers frames. For capture, the
\r
3045 // concept is less clear so we'll repeat what we do in the
\r
3046 // abortStream() case.
\r
3048 DWORD dsBufferSize;
\r
3049 LPVOID buffer1 = NULL;
\r
3050 LPVOID buffer2 = NULL;
\r
3051 DWORD bufferSize1 = 0;
\r
3052 DWORD bufferSize2 = 0;
\r
3053 if (stream->mode == PLAYBACK || stream->mode == DUPLEX) {
\r
3055 DWORD currentPos, safePos;
\r
3056 long buffer_bytes = stream->bufferSize * stream->nDeviceChannels[0];
\r
3057 buffer_bytes *= formatBytes(stream->deviceFormat[0]);
\r
3059 LPDIRECTSOUNDBUFFER dsBuffer = (LPDIRECTSOUNDBUFFER) stream->handle[0].buffer;
\r
3060 UINT nextWritePos = stream->handle[0].bufferPointer;
\r
3061 dsBufferSize = buffer_bytes * stream->nBuffers;
\r
3063 // Write zeroes for nBuffer counts.
\r
3064 for (int i=0; i<stream->nBuffers; i++) {
\r
3066 // Find out where the read and "safe write" pointers are.
\r
3067 result = dsBuffer->GetCurrentPosition(¤tPos, &safePos);
\r
3068 if ( FAILED(result) ) {
\r
3069 sprintf(message, "RtAudio: Unable to get current DS position (%s): %s.",
\r
3070 devices[stream->device[0]].name, getErrorString(result));
\r
3071 error(RtError::DRIVER_ERROR);
\r
3074 if ( currentPos < nextWritePos ) currentPos += dsBufferSize; // unwrap offset
\r
3075 DWORD endWrite = nextWritePos + buffer_bytes;
\r
3077 // Check whether the entire write region is behind the play pointer.
\r
3078 while ( currentPos < endWrite ) {
\r
3079 float millis = (endWrite - currentPos) * 900.0;
\r
3080 millis /= ( formatBytes(stream->deviceFormat[0]) * stream->sampleRate);
\r
3081 if ( millis < 1.0 ) millis = 1.0;
\r
3082 Sleep( (DWORD) millis );
\r
3084 // Wake up, find out where we are now
\r
3085 result = dsBuffer->GetCurrentPosition( ¤tPos, &safePos );
\r
3086 if ( FAILED(result) ) {
\r
3087 sprintf(message, "RtAudio: Unable to get current DS position (%s): %s.",
\r
3088 devices[stream->device[0]].name, getErrorString(result));
\r
3089 error(RtError::DRIVER_ERROR);
\r
3091 if ( currentPos < nextWritePos ) currentPos += dsBufferSize; // unwrap offset
\r
3094 // Lock free space in the buffer
\r
3095 result = dsBuffer->Lock (nextWritePos, buffer_bytes, &buffer1,
\r
3096 &bufferSize1, &buffer2, &bufferSize2, 0);
\r
3097 if ( FAILED(result) ) {
\r
3098 sprintf(message, "RtAudio: Unable to lock DS buffer during playback (%s): %s.",
\r
3099 devices[stream->device[0]].name, getErrorString(result));
\r
3100 error(RtError::DRIVER_ERROR);
\r
3103 // Zero the free space
\r
3104 ZeroMemory(buffer1, bufferSize1);
\r
3105 if (buffer2 != NULL) ZeroMemory(buffer2, bufferSize2);
\r
3107 // Update our buffer offset and unlock sound buffer
\r
3108 dsBuffer->Unlock (buffer1, bufferSize1, buffer2, bufferSize2);
\r
3109 if ( FAILED(result) ) {
\r
3110 sprintf(message, "RtAudio: Unable to unlock DS buffer during playback (%s): %s.",
\r
3111 devices[stream->device[0]].name, getErrorString(result));
\r
3112 error(RtError::DRIVER_ERROR);
\r
3114 nextWritePos = (nextWritePos + bufferSize1 + bufferSize2) % dsBufferSize;
\r
3115 stream->handle[0].bufferPointer = nextWritePos;
\r
3118 // If we play again, start at the beginning of the buffer.
\r
3119 stream->handle[0].bufferPointer = 0;
\r
3122 if (stream->mode == RECORD || stream->mode == DUPLEX) {
\r
3123 LPDIRECTSOUNDCAPTUREBUFFER buffer = (LPDIRECTSOUNDCAPTUREBUFFER) stream->handle[1].buffer;
\r
3127 result = buffer->Stop();
\r
3128 if ( FAILED(result) ) {
\r
3129 sprintf(message, "RtAudio: Unable to stop DS capture buffer (%s): %s",
\r
3130 devices[stream->device[1]].name, getErrorString(result));
\r
3131 error(RtError::DRIVER_ERROR);
\r
3134 dsBufferSize = stream->bufferSize * stream->nDeviceChannels[1];
\r
3135 dsBufferSize *= formatBytes(stream->deviceFormat[1]) * stream->nBuffers;
\r
3137 // Lock the buffer and clear it so that if we start to play again,
\r
3138 // we won't have old data playing.
\r
3139 result = buffer->Lock(0, dsBufferSize, &buffer1, &bufferSize1, NULL, NULL, 0);
\r
3140 if ( FAILED(result) ) {
\r
3141 sprintf(message, "RtAudio: Unable to lock DS capture buffer (%s): %s.",
\r
3142 devices[stream->device[1]].name, getErrorString(result));
\r
3143 error(RtError::DRIVER_ERROR);
\r
3146 // Zero the DS buffer
\r
3147 ZeroMemory(buffer1, bufferSize1);
\r
3149 // Unlock the DS buffer
\r
3150 result = buffer->Unlock(buffer1, bufferSize1, NULL, 0);
\r
3151 if ( FAILED(result) ) {
\r
3152 sprintf(message, "RtAudio: Unable to unlock DS capture buffer (%s): %s.",
\r
3153 devices[stream->device[1]].name, getErrorString(result));
\r
3154 error(RtError::DRIVER_ERROR);
\r
3157 // If we start recording again, we must begin at beginning of buffer.
\r
3158 stream->handle[1].bufferPointer = 0;
\r
3160 stream->state = STREAM_STOPPED;
\r
3162 MUTEX_UNLOCK(&stream->mutex);
\r
3165 void RtAudio :: abortStream(int streamId)
\r
3167 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
3169 MUTEX_LOCK(&stream->mutex);
\r
3171 if (stream->state == STREAM_STOPPED)
\r
3175 long dsBufferSize;
\r
3178 if (stream->mode == PLAYBACK || stream->mode == DUPLEX) {
\r
3179 LPDIRECTSOUNDBUFFER buffer = (LPDIRECTSOUNDBUFFER) stream->handle[0].buffer;
\r
3180 result = buffer->Stop();
\r
3181 if ( FAILED(result) ) {
\r
3182 sprintf(message, "RtAudio: Unable to stop DS buffer (%s): %s",
\r
3183 devices[stream->device[0]].name, getErrorString(result));
\r
3184 error(RtError::DRIVER_ERROR);
\r
3187 dsBufferSize = stream->bufferSize * stream->nDeviceChannels[0];
\r
3188 dsBufferSize *= formatBytes(stream->deviceFormat[0]) * stream->nBuffers;
\r
3190 // Lock the buffer and clear it so that if we start to play again,
\r
3191 // we won't have old data playing.
\r
3192 result = buffer->Lock(0, dsBufferSize, &audioPtr, &dataLen, NULL, NULL, 0);
\r
3193 if ( FAILED(result) ) {
\r
3194 sprintf(message, "RtAudio: Unable to lock DS buffer (%s): %s.",
\r
3195 devices[stream->device[0]].name, getErrorString(result));
\r
3196 error(RtError::DRIVER_ERROR);
\r
3199 // Zero the DS buffer
\r
3200 ZeroMemory(audioPtr, dataLen);
\r
3202 // Unlock the DS buffer
\r
3203 result = buffer->Unlock(audioPtr, dataLen, NULL, 0);
\r
3204 if ( FAILED(result) ) {
\r
3205 sprintf(message, "RtAudio: Unable to unlock DS buffer (%s): %s.",
\r
3206 devices[stream->device[0]].name, getErrorString(result));
\r
3207 error(RtError::DRIVER_ERROR);
\r
3210 // If we start playing again, we must begin at beginning of buffer.
\r
3211 stream->handle[0].bufferPointer = 0;
\r
3214 if (stream->mode == RECORD || stream->mode == DUPLEX) {
\r
3215 LPDIRECTSOUNDCAPTUREBUFFER buffer = (LPDIRECTSOUNDCAPTUREBUFFER) stream->handle[1].buffer;
\r
3219 result = buffer->Stop();
\r
3220 if ( FAILED(result) ) {
\r
3221 sprintf(message, "RtAudio: Unable to stop DS capture buffer (%s): %s",
\r
3222 devices[stream->device[1]].name, getErrorString(result));
\r
3223 error(RtError::DRIVER_ERROR);
\r
3226 dsBufferSize = stream->bufferSize * stream->nDeviceChannels[1];
\r
3227 dsBufferSize *= formatBytes(stream->deviceFormat[1]) * stream->nBuffers;
\r
3229 // Lock the buffer and clear it so that if we start to play again,
\r
3230 // we won't have old data playing.
\r
3231 result = buffer->Lock(0, dsBufferSize, &audioPtr, &dataLen, NULL, NULL, 0);
\r
3232 if ( FAILED(result) ) {
\r
3233 sprintf(message, "RtAudio: Unable to lock DS capture buffer (%s): %s.",
\r
3234 devices[stream->device[1]].name, getErrorString(result));
\r
3235 error(RtError::DRIVER_ERROR);
\r
3238 // Zero the DS buffer
\r
3239 ZeroMemory(audioPtr, dataLen);
\r
3241 // Unlock the DS buffer
\r
3242 result = buffer->Unlock(audioPtr, dataLen, NULL, 0);
\r
3243 if ( FAILED(result) ) {
\r
3244 sprintf(message, "RtAudio: Unable to unlock DS capture buffer (%s): %s.",
\r
3245 devices[stream->device[1]].name, getErrorString(result));
\r
3246 error(RtError::DRIVER_ERROR);
\r
3249 // If we start recording again, we must begin at beginning of buffer.
\r
3250 stream->handle[1].bufferPointer = 0;
\r
3252 stream->state = STREAM_STOPPED;
\r
3255 MUTEX_UNLOCK(&stream->mutex);
\r
3258 int RtAudio :: streamWillBlock(int streamId)
\r
3260 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
3262 MUTEX_LOCK(&stream->mutex);
\r
3266 if (stream->state == STREAM_STOPPED)
\r
3270 DWORD currentPos, safePos;
\r
3272 if (stream->mode == PLAYBACK || stream->mode == DUPLEX) {
\r
3274 LPDIRECTSOUNDBUFFER dsBuffer = (LPDIRECTSOUNDBUFFER) stream->handle[0].buffer;
\r
3275 UINT nextWritePos = stream->handle[0].bufferPointer;
\r
3276 channels = stream->nDeviceChannels[0];
\r
3277 DWORD dsBufferSize = stream->bufferSize * channels;
\r
3278 dsBufferSize *= formatBytes(stream->deviceFormat[0]) * stream->nBuffers;
\r
3280 // Find out where the read and "safe write" pointers are.
\r
3281 result = dsBuffer->GetCurrentPosition(¤tPos, &safePos);
\r
3282 if ( FAILED(result) ) {
\r
3283 sprintf(message, "RtAudio: Unable to get current DS position (%s): %s.",
\r
3284 devices[stream->device[0]].name, getErrorString(result));
\r
3285 error(RtError::DRIVER_ERROR);
\r
3288 if ( currentPos < nextWritePos ) currentPos += dsBufferSize; // unwrap offset
\r
3289 frames = currentPos - nextWritePos;
\r
3290 frames /= channels * formatBytes(stream->deviceFormat[0]);
\r
3293 if (stream->mode == RECORD || stream->mode == DUPLEX) {
\r
3295 LPDIRECTSOUNDCAPTUREBUFFER dsBuffer = (LPDIRECTSOUNDCAPTUREBUFFER) stream->handle[1].buffer;
\r
3296 UINT nextReadPos = stream->handle[1].bufferPointer;
\r
3297 channels = stream->nDeviceChannels[1];
\r
3298 DWORD dsBufferSize = stream->bufferSize * channels;
\r
3299 dsBufferSize *= formatBytes(stream->deviceFormat[1]) * stream->nBuffers;
\r
3301 // Find out where the write and "safe read" pointers are.
\r
3302 result = dsBuffer->GetCurrentPosition(¤tPos, &safePos);
\r
3303 if ( FAILED(result) ) {
\r
3304 sprintf(message, "RtAudio: Unable to get current DS capture position (%s): %s.",
\r
3305 devices[stream->device[1]].name, getErrorString(result));
\r
3306 error(RtError::DRIVER_ERROR);
\r
3309 if ( safePos < nextReadPos ) safePos += dsBufferSize; // unwrap offset
\r
3311 if (stream->mode == DUPLEX ) {
\r
3312 // Take largest value of the two.
\r
3313 int temp = safePos - nextReadPos;
\r
3314 temp /= channels * formatBytes(stream->deviceFormat[1]);
\r
3315 frames = ( temp > frames ) ? temp : frames;
\r
3318 frames = safePos - nextReadPos;
\r
3319 frames /= channels * formatBytes(stream->deviceFormat[1]);
\r
3323 frames = stream->bufferSize - frames;
\r
3324 if (frames < 0) frames = 0;
\r
3327 MUTEX_UNLOCK(&stream->mutex);
\r
3331 void RtAudio :: tickStream(int streamId)
\r
3333 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
3335 int stopStream = 0;
\r
3336 if (stream->state == STREAM_STOPPED) {
\r
3337 if (stream->usingCallback) Sleep(50); // sleep 50 milliseconds
\r
3340 else if (stream->usingCallback) {
\r
3341 stopStream = stream->callback(stream->userBuffer, stream->bufferSize, stream->userData);
\r
3344 MUTEX_LOCK(&stream->mutex);
\r
3346 // The state might change while waiting on a mutex.
\r
3347 if (stream->state == STREAM_STOPPED) {
\r
3348 MUTEX_UNLOCK(&stream->mutex);
\r
3349 if (stream->usingCallback && stopStream)
\r
3350 this->stopStream(streamId);
\r
3354 DWORD currentPos, safePos;
\r
3355 LPVOID buffer1 = NULL;
\r
3356 LPVOID buffer2 = NULL;
\r
3357 DWORD bufferSize1 = 0;
\r
3358 DWORD bufferSize2 = 0;
\r
3360 long buffer_bytes;
\r
3361 if (stream->mode == PLAYBACK || stream->mode == DUPLEX) {
\r
3363 // Setup parameters and do buffer conversion if necessary.
\r
3364 if (stream->doConvertBuffer[0]) {
\r
3365 convertStreamBuffer(stream, PLAYBACK);
\r
3366 buffer = stream->deviceBuffer;
\r
3367 buffer_bytes = stream->bufferSize * stream->nDeviceChannels[0];
\r
3368 buffer_bytes *= formatBytes(stream->deviceFormat[0]);
\r
3371 buffer = stream->userBuffer;
\r
3372 buffer_bytes = stream->bufferSize * stream->nUserChannels[0];
\r
3373 buffer_bytes *= formatBytes(stream->userFormat);
\r
3376 // No byte swapping necessary in DirectSound implementation.
\r
3378 LPDIRECTSOUNDBUFFER dsBuffer = (LPDIRECTSOUNDBUFFER) stream->handle[0].buffer;
\r
3379 UINT nextWritePos = stream->handle[0].bufferPointer;
\r
3380 DWORD dsBufferSize = buffer_bytes * stream->nBuffers;
\r
3382 // Find out where the read and "safe write" pointers are.
\r
3383 result = dsBuffer->GetCurrentPosition(¤tPos, &safePos);
\r
3384 if ( FAILED(result) ) {
\r
3385 sprintf(message, "RtAudio: Unable to get current DS position (%s): %s.",
\r
3386 devices[stream->device[0]].name, getErrorString(result));
\r
3387 error(RtError::DRIVER_ERROR);
\r
3390 if ( currentPos < nextWritePos ) currentPos += dsBufferSize; // unwrap offset
\r
3391 DWORD endWrite = nextWritePos + buffer_bytes;
\r
3393 // Check whether the entire write region is behind the play pointer.
\r
3394 while ( currentPos < endWrite ) {
\r
3395 // If we are here, then we must wait until the play pointer gets
\r
3396 // beyond the write region. The approach here is to use the
\r
3397 // Sleep() function to suspend operation until safePos catches
\r
3398 // up. Calculate number of milliseconds to wait as:
\r
3399 // time = distance * (milliseconds/second) * fudgefactor /
\r
3400 // ((bytes/sample) * (samples/second))
\r
3401 // A "fudgefactor" less than 1 is used because it was found
\r
3402 // that sleeping too long was MUCH worse than sleeping for
\r
3403 // several shorter periods.
\r
3404 float millis = (endWrite - currentPos) * 900.0;
\r
3405 millis /= ( formatBytes(stream->deviceFormat[0]) * stream->sampleRate);
\r
3406 if ( millis < 1.0 ) millis = 1.0;
\r
3407 Sleep( (DWORD) millis );
\r
3409 // Wake up, find out where we are now
\r
3410 result = dsBuffer->GetCurrentPosition( ¤tPos, &safePos );
\r
3411 if ( FAILED(result) ) {
\r
3412 sprintf(message, "RtAudio: Unable to get current DS position (%s): %s.",
\r
3413 devices[stream->device[0]].name, getErrorString(result));
\r
3414 error(RtError::DRIVER_ERROR);
\r
3416 if ( currentPos < nextWritePos ) currentPos += dsBufferSize; // unwrap offset
\r
3419 // Lock free space in the buffer
\r
3420 result = dsBuffer->Lock (nextWritePos, buffer_bytes, &buffer1,
\r
3421 &bufferSize1, &buffer2, &bufferSize2, 0);
\r
3422 if ( FAILED(result) ) {
\r
3423 sprintf(message, "RtAudio: Unable to lock DS buffer during playback (%s): %s.",
\r
3424 devices[stream->device[0]].name, getErrorString(result));
\r
3425 error(RtError::DRIVER_ERROR);
\r
3428 // Copy our buffer into the DS buffer
\r
3429 CopyMemory(buffer1, buffer, bufferSize1);
\r
3430 if (buffer2 != NULL) CopyMemory(buffer2, buffer+bufferSize1, bufferSize2);
\r
3432 // Update our buffer offset and unlock sound buffer
\r
3433 dsBuffer->Unlock (buffer1, bufferSize1, buffer2, bufferSize2);
\r
3434 if ( FAILED(result) ) {
\r
3435 sprintf(message, "RtAudio: Unable to unlock DS buffer during playback (%s): %s.",
\r
3436 devices[stream->device[0]].name, getErrorString(result));
\r
3437 error(RtError::DRIVER_ERROR);
\r
3439 nextWritePos = (nextWritePos + bufferSize1 + bufferSize2) % dsBufferSize;
\r
3440 stream->handle[0].bufferPointer = nextWritePos;
\r
3443 if (stream->mode == RECORD || stream->mode == DUPLEX) {
\r
3445 // Setup parameters.
\r
3446 if (stream->doConvertBuffer[1]) {
\r
3447 buffer = stream->deviceBuffer;
\r
3448 buffer_bytes = stream->bufferSize * stream->nDeviceChannels[1];
\r
3449 buffer_bytes *= formatBytes(stream->deviceFormat[1]);
\r
3452 buffer = stream->userBuffer;
\r
3453 buffer_bytes = stream->bufferSize * stream->nUserChannels[1];
\r
3454 buffer_bytes *= formatBytes(stream->userFormat);
\r
3457 LPDIRECTSOUNDCAPTUREBUFFER dsBuffer = (LPDIRECTSOUNDCAPTUREBUFFER) stream->handle[1].buffer;
\r
3458 UINT nextReadPos = stream->handle[1].bufferPointer;
\r
3459 DWORD dsBufferSize = buffer_bytes * stream->nBuffers;
\r
3461 // Find out where the write and "safe read" pointers are.
\r
3462 result = dsBuffer->GetCurrentPosition(¤tPos, &safePos);
\r
3463 if ( FAILED(result) ) {
\r
3464 sprintf(message, "RtAudio: Unable to get current DS capture position (%s): %s.",
\r
3465 devices[stream->device[1]].name, getErrorString(result));
\r
3466 error(RtError::DRIVER_ERROR);
\r
3469 if ( safePos < nextReadPos ) safePos += dsBufferSize; // unwrap offset
\r
3470 DWORD endRead = nextReadPos + buffer_bytes;
\r
3472 // Check whether the entire write region is behind the play pointer.
\r
3473 while ( safePos < endRead ) {
\r
3474 // See comments for playback.
\r
3475 float millis = (endRead - safePos) * 900.0;
\r
3476 millis /= ( formatBytes(stream->deviceFormat[1]) * stream->sampleRate);
\r
3477 if ( millis < 1.0 ) millis = 1.0;
\r
3478 Sleep( (DWORD) millis );
\r
3480 // Wake up, find out where we are now
\r
3481 result = dsBuffer->GetCurrentPosition( ¤tPos, &safePos );
\r
3482 if ( FAILED(result) ) {
\r
3483 sprintf(message, "RtAudio: Unable to get current DS capture position (%s): %s.",
\r
3484 devices[stream->device[1]].name, getErrorString(result));
\r
3485 error(RtError::DRIVER_ERROR);
\r
3488 if ( safePos < nextReadPos ) safePos += dsBufferSize; // unwrap offset
\r
3491 // Lock free space in the buffer
\r
3492 result = dsBuffer->Lock (nextReadPos, buffer_bytes, &buffer1,
\r
3493 &bufferSize1, &buffer2, &bufferSize2, 0);
\r
3494 if ( FAILED(result) ) {
\r
3495 sprintf(message, "RtAudio: Unable to lock DS buffer during capture (%s): %s.",
\r
3496 devices[stream->device[1]].name, getErrorString(result));
\r
3497 error(RtError::DRIVER_ERROR);
\r
3500 // Copy our buffer into the DS buffer
\r
3501 CopyMemory(buffer, buffer1, bufferSize1);
\r
3502 if (buffer2 != NULL) CopyMemory(buffer+bufferSize1, buffer2, bufferSize2);
\r
3504 // Update our buffer offset and unlock sound buffer
\r
3505 nextReadPos = (nextReadPos + bufferSize1 + bufferSize2) % dsBufferSize;
\r
3506 dsBuffer->Unlock (buffer1, bufferSize1, buffer2, bufferSize2);
\r
3507 if ( FAILED(result) ) {
\r
3508 sprintf(message, "RtAudio: Unable to unlock DS buffer during capture (%s): %s.",
\r
3509 devices[stream->device[1]].name, getErrorString(result));
\r
3510 error(RtError::DRIVER_ERROR);
\r
3512 stream->handle[1].bufferPointer = nextReadPos;
\r
3514 // No byte swapping necessary in DirectSound implementation.
\r
3516 // Do buffer conversion if necessary.
\r
3517 if (stream->doConvertBuffer[1])
\r
3518 convertStreamBuffer(stream, RECORD);
\r
3521 MUTEX_UNLOCK(&stream->mutex);
\r
3523 if (stream->usingCallback && stopStream)
\r
3524 this->stopStream(streamId);
\r
3527 // Definitions for utility functions and callbacks
\r
3528 // specific to the DirectSound implementation.
\r
3530 extern "C" unsigned __stdcall callbackHandler(void *ptr)
\r
3532 RtAudio *object = thread_info.object;
\r
3533 int stream = thread_info.streamId;
\r
3534 bool *usingCallback = (bool *) ptr;
\r
3536 while ( *usingCallback ) {
\r
3538 object->tickStream(stream);
\r
3540 catch (RtError &exception) {
\r
3541 fprintf(stderr, "\nCallback thread error (%s) ... closing thread.\n\n",
\r
3542 exception.getMessage());
\r
3547 _endthreadex( 0 );
\r
3551 static bool CALLBACK deviceCountCallback(LPGUID lpguid,
\r
3552 LPCSTR lpcstrDescription,
\r
3553 LPCSTR lpcstrModule,
\r
3556 int *pointer = ((int *) lpContext);
\r
3562 static bool CALLBACK deviceInfoCallback(LPGUID lpguid,
\r
3563 LPCSTR lpcstrDescription,
\r
3564 LPCSTR lpcstrModule,
\r
3567 enum_info *info = ((enum_info *) lpContext);
\r
3568 while (strlen(info->name) > 0) info++;
\r
3570 strncpy(info->name, lpcstrDescription, 64);
\r
3571 info->id = lpguid;
\r
3574 info->isValid = false;
\r
3575 if (info->isInput == true) {
\r
3577 LPDIRECTSOUNDCAPTURE object;
\r
3579 hr = DirectSoundCaptureCreate( lpguid, &object, NULL );
\r
3580 if( hr != DS_OK ) return true;
\r
3582 caps.dwSize = sizeof(caps);
\r
3583 hr = object->GetCaps( &caps );
\r
3584 if( hr == DS_OK ) {
\r
3585 if (caps.dwChannels > 0 && caps.dwFormats > 0)
\r
3586 info->isValid = true;
\r
3588 object->Release();
\r
3592 LPDIRECTSOUND object;
\r
3593 hr = DirectSoundCreate( lpguid, &object, NULL );
\r
3594 if( hr != DS_OK ) return true;
\r
3596 caps.dwSize = sizeof(caps);
\r
3597 hr = object->GetCaps( &caps );
\r
3598 if( hr == DS_OK ) {
\r
3599 if ( caps.dwFlags & DSCAPS_PRIMARYMONO || caps.dwFlags & DSCAPS_PRIMARYSTEREO )
\r
3600 info->isValid = true;
\r
3602 object->Release();
\r
3608 static char* getErrorString(int code)
\r
3612 case DSERR_ALLOCATED:
\r
3613 return "Direct Sound already allocated";
\r
3615 case DSERR_CONTROLUNAVAIL:
\r
3616 return "Direct Sound control unavailable";
\r
3618 case DSERR_INVALIDPARAM:
\r
3619 return "Direct Sound invalid parameter";
\r
3621 case DSERR_INVALIDCALL:
\r
3622 return "Direct Sound invalid call";
\r
3624 case DSERR_GENERIC:
\r
3625 return "Direct Sound generic error";
\r
3627 case DSERR_PRIOLEVELNEEDED:
\r
3628 return "Direct Sound Priority level needed";
\r
3630 case DSERR_OUTOFMEMORY:
\r
3631 return "Direct Sound out of memory";
\r
3633 case DSERR_BADFORMAT:
\r
3634 return "Direct Sound bad format";
\r
3636 case DSERR_UNSUPPORTED:
\r
3637 return "Direct Sound unsupported error";
\r
3639 case DSERR_NODRIVER:
\r
3640 return "Direct Sound no driver error";
\r
3642 case DSERR_ALREADYINITIALIZED:
\r
3643 return "Direct Sound already initialized";
\r
3645 case DSERR_NOAGGREGATION:
\r
3646 return "Direct Sound no aggregation";
\r
3648 case DSERR_BUFFERLOST:
\r
3649 return "Direct Sound buffer lost";
\r
3651 case DSERR_OTHERAPPHASPRIO:
\r
3652 return "Direct Sound other app has priority";
\r
3654 case DSERR_UNINITIALIZED:
\r
3655 return "Direct Sound uninitialized";
\r
3658 return "Direct Sound unknown error";
\r
3662 //******************** End of __WINDOWS_DS__ *********************//
\r
3664 #elif defined(__IRIX_AL__) // SGI's AL API for IRIX
\r
3666 #include <unistd.h>
\r
3667 #include <errno.h>
\r
3669 void RtAudio :: initialize(void)
\r
3672 // Count cards and devices
\r
3675 // Determine the total number of input and output devices.
\r
3676 nDevices = alQueryValues(AL_SYSTEM, AL_DEVICES, 0, 0, 0, 0);
\r
3677 if (nDevices < 0) {
\r
3678 sprintf(message, "RtAudio: AL error counting devices: %s.",
\r
3679 alGetErrorString(oserror()));
\r
3680 error(RtError::DRIVER_ERROR);
\r
3683 if (nDevices <= 0) return;
\r
3685 ALvalue *vls = (ALvalue *) new ALvalue[nDevices];
\r
3687 // Add one for our default input/output devices.
\r
3690 // Allocate the RTAUDIO_DEVICE structures.
\r
3691 devices = (RTAUDIO_DEVICE *) calloc(nDevices, sizeof(RTAUDIO_DEVICE));
\r
3692 if (devices == NULL) {
\r
3693 sprintf(message, "RtAudio: memory allocation error!");
\r
3694 error(RtError::MEMORY_ERROR);
\r
3697 // Write device ascii identifiers to device info structure.
\r
3701 pvs[0].param = AL_NAME;
\r
3702 pvs[0].value.ptr = name;
\r
3703 pvs[0].sizeIn = 32;
\r
3705 strcpy(devices[0].name, "Default Input/Output Devices");
\r
3707 outs = alQueryValues(AL_SYSTEM, AL_DEFAULT_OUTPUT, vls, nDevices-1, 0, 0);
\r
3709 sprintf(message, "RtAudio: AL error getting output devices: %s.",
\r
3710 alGetErrorString(oserror()));
\r
3711 error(RtError::DRIVER_ERROR);
\r
3714 for (i=0; i<outs; i++) {
\r
3715 if (alGetParams(vls[i].i, pvs, 1) < 0) {
\r
3716 sprintf(message, "RtAudio: AL error querying output devices: %s.",
\r
3717 alGetErrorString(oserror()));
\r
3718 error(RtError::DRIVER_ERROR);
\r
3720 strncpy(devices[i+1].name, name, 32);
\r
3721 devices[i+1].id[0] = vls[i].i;
\r
3724 ins = alQueryValues(AL_SYSTEM, AL_DEFAULT_INPUT, &vls[outs], nDevices-outs-1, 0, 0);
\r
3726 sprintf(message, "RtAudio: AL error getting input devices: %s.",
\r
3727 alGetErrorString(oserror()));
\r
3728 error(RtError::DRIVER_ERROR);
\r
3731 for (i=outs; i<ins+outs; i++) {
\r
3732 if (alGetParams(vls[i].i, pvs, 1) < 0) {
\r
3733 sprintf(message, "RtAudio: AL error querying input devices: %s.",
\r
3734 alGetErrorString(oserror()));
\r
3735 error(RtError::DRIVER_ERROR);
\r
3737 strncpy(devices[i+1].name, name, 32);
\r
3738 devices[i+1].id[1] = vls[i].i;
\r
3746 void RtAudio :: probeDeviceInfo(RTAUDIO_DEVICE *info)
\r
3748 int resource, result, i;
\r
3750 ALparamInfo pinfo;
\r
3752 // Get output resource ID if it exists.
\r
3753 if ( !strncmp(info->name, "Default Input/Output Devices", 28) ) {
\r
3754 result = alQueryValues(AL_SYSTEM, AL_DEFAULT_OUTPUT, &value, 1, 0, 0);
\r
3756 sprintf(message, "RtAudio: AL error getting default output device id: %s.",
\r
3757 alGetErrorString(oserror()));
\r
3758 error(RtError::WARNING);
\r
3761 resource = value.i;
\r
3764 resource = info->id[0];
\r
3766 if (resource > 0) {
\r
3768 // Probe output device parameters.
\r
3769 result = alQueryValues(resource, AL_CHANNELS, &value, 1, 0, 0);
\r
3771 sprintf(message, "RtAudio: AL error getting device (%s) channels: %s.",
\r
3772 info->name, alGetErrorString(oserror()));
\r
3773 error(RtError::WARNING);
\r
3776 info->maxOutputChannels = value.i;
\r
3777 info->minOutputChannels = 1;
\r
3780 result = alGetParamInfo(resource, AL_RATE, &pinfo);
\r
3782 sprintf(message, "RtAudio: AL error getting device (%s) rates: %s.",
\r
3783 info->name, alGetErrorString(oserror()));
\r
3784 error(RtError::WARNING);
\r
3787 info->nSampleRates = 0;
\r
3788 for (i=0; i<MAX_SAMPLE_RATES; i++) {
\r
3789 if ( SAMPLE_RATES[i] >= pinfo.min.i && SAMPLE_RATES[i] <= pinfo.max.i ) {
\r
3790 info->sampleRates[info->nSampleRates] = SAMPLE_RATES[i];
\r
3791 info->nSampleRates++;
\r
3796 // The AL library supports all our formats, except 24-bit and 32-bit ints.
\r
3797 info->nativeFormats = (RTAUDIO_FORMAT) 51;
\r
3800 // Now get input resource ID if it exists.
\r
3801 if ( !strncmp(info->name, "Default Input/Output Devices", 28) ) {
\r
3802 result = alQueryValues(AL_SYSTEM, AL_DEFAULT_INPUT, &value, 1, 0, 0);
\r
3804 sprintf(message, "RtAudio: AL error getting default input device id: %s.",
\r
3805 alGetErrorString(oserror()));
\r
3806 error(RtError::WARNING);
\r
3809 resource = value.i;
\r
3812 resource = info->id[1];
\r
3814 if (resource > 0) {
\r
3816 // Probe input device parameters.
\r
3817 result = alQueryValues(resource, AL_CHANNELS, &value, 1, 0, 0);
\r
3819 sprintf(message, "RtAudio: AL error getting device (%s) channels: %s.",
\r
3820 info->name, alGetErrorString(oserror()));
\r
3821 error(RtError::WARNING);
\r
3824 info->maxInputChannels = value.i;
\r
3825 info->minInputChannels = 1;
\r
3828 result = alGetParamInfo(resource, AL_RATE, &pinfo);
\r
3830 sprintf(message, "RtAudio: AL error getting device (%s) rates: %s.",
\r
3831 info->name, alGetErrorString(oserror()));
\r
3832 error(RtError::WARNING);
\r
3835 // In the case of the default device, these values will
\r
3836 // overwrite the rates determined for the output device. Since
\r
3837 // the input device is most likely to be more limited than the
\r
3838 // output device, this is ok.
\r
3839 info->nSampleRates = 0;
\r
3840 for (i=0; i<MAX_SAMPLE_RATES; i++) {
\r
3841 if ( SAMPLE_RATES[i] >= pinfo.min.i && SAMPLE_RATES[i] <= pinfo.max.i ) {
\r
3842 info->sampleRates[info->nSampleRates] = SAMPLE_RATES[i];
\r
3843 info->nSampleRates++;
\r
3848 // The AL library supports all our formats, except 24-bit and 32-bit ints.
\r
3849 info->nativeFormats = (RTAUDIO_FORMAT) 51;
\r
3852 if ( info->maxInputChannels == 0 && info->maxOutputChannels == 0 )
\r
3854 if ( info->nSampleRates == 0 )
\r
3857 // Determine duplex status.
\r
3858 if (info->maxInputChannels < info->maxOutputChannels)
\r
3859 info->maxDuplexChannels = info->maxInputChannels;
\r
3861 info->maxDuplexChannels = info->maxOutputChannels;
\r
3862 if (info->minInputChannels < info->minOutputChannels)
\r
3863 info->minDuplexChannels = info->minInputChannels;
\r
3865 info->minDuplexChannels = info->minOutputChannels;
\r
3867 if ( info->maxDuplexChannels > 0 ) info->hasDuplexSupport = true;
\r
3868 else info->hasDuplexSupport = false;
\r
3870 info->probed = true;
\r
3875 bool RtAudio :: probeDeviceOpen(int device, RTAUDIO_STREAM *stream,
\r
3876 STREAM_MODE mode, int channels,
\r
3877 int sampleRate, RTAUDIO_FORMAT format,
\r
3878 int *bufferSize, int numberOfBuffers)
\r
3880 int result, resource, nBuffers;
\r
3881 ALconfig al_config;
\r
3885 // Get a new ALconfig structure.
\r
3886 al_config = alNewConfig();
\r
3887 if ( !al_config ) {
\r
3888 sprintf(message,"RtAudio: can't get AL config: %s.",
\r
3889 alGetErrorString(oserror()));
\r
3890 error(RtError::WARNING);
\r
3894 // Set the channels.
\r
3895 result = alSetChannels(al_config, channels);
\r
3896 if ( result < 0 ) {
\r
3897 sprintf(message,"RtAudio: can't set %d channels in AL config: %s.",
\r
3898 channels, alGetErrorString(oserror()));
\r
3899 error(RtError::WARNING);
\r
3903 // Set the queue (buffer) size.
\r
3904 if ( numberOfBuffers < 1 )
\r
3907 nBuffers = numberOfBuffers;
\r
3908 long buffer_size = *bufferSize * nBuffers;
\r
3909 result = alSetQueueSize(al_config, buffer_size); // in sample frames
\r
3910 if ( result < 0 ) {
\r
3911 sprintf(message,"RtAudio: can't set buffer size (%ld) in AL config: %s.",
\r
3912 buffer_size, alGetErrorString(oserror()));
\r
3913 error(RtError::WARNING);
\r
3917 // Set the data format.
\r
3918 stream->userFormat = format;
\r
3919 stream->deviceFormat[mode] = format;
\r
3920 if (format == RTAUDIO_SINT8) {
\r
3921 result = alSetSampFmt(al_config, AL_SAMPFMT_TWOSCOMP);
\r
3922 result = alSetWidth(al_config, AL_SAMPLE_8);
\r
3924 else if (format == RTAUDIO_SINT16) {
\r
3925 result = alSetSampFmt(al_config, AL_SAMPFMT_TWOSCOMP);
\r
3926 result = alSetWidth(al_config, AL_SAMPLE_16);
\r
3928 else if (format == RTAUDIO_SINT24) {
\r
3929 // Our 24-bit format assumes the upper 3 bytes of a 4 byte word.
\r
3930 // The AL library uses the lower 3 bytes, so we'll need to do our
\r
3931 // own conversion.
\r
3932 result = alSetSampFmt(al_config, AL_SAMPFMT_FLOAT);
\r
3933 stream->deviceFormat[mode] = RTAUDIO_FLOAT32;
\r
3935 else if (format == RTAUDIO_SINT32) {
\r
3936 // The AL library doesn't seem to support the 32-bit integer
\r
3937 // format, so we'll need to do our own conversion.
\r
3938 result = alSetSampFmt(al_config, AL_SAMPFMT_FLOAT);
\r
3939 stream->deviceFormat[mode] = RTAUDIO_FLOAT32;
\r
3941 else if (format == RTAUDIO_FLOAT32)
\r
3942 result = alSetSampFmt(al_config, AL_SAMPFMT_FLOAT);
\r
3943 else if (format == RTAUDIO_FLOAT64)
\r
3944 result = alSetSampFmt(al_config, AL_SAMPFMT_DOUBLE);
\r
3946 if ( result == -1 ) {
\r
3947 sprintf(message,"RtAudio: AL error setting sample format in AL config: %s.",
\r
3948 alGetErrorString(oserror()));
\r
3949 error(RtError::WARNING);
\r
3953 if (mode == PLAYBACK) {
\r
3955 // Set our device.
\r
3957 resource = AL_DEFAULT_OUTPUT;
\r
3959 resource = devices[device].id[0];
\r
3960 result = alSetDevice(al_config, resource);
\r
3961 if ( result == -1 ) {
\r
3962 sprintf(message,"RtAudio: AL error setting device (%s) in AL config: %s.",
\r
3963 devices[device].name, alGetErrorString(oserror()));
\r
3964 error(RtError::WARNING);
\r
3969 port = alOpenPort("RtAudio Output Port", "w", al_config);
\r
3971 sprintf(message,"RtAudio: AL error opening output port: %s.",
\r
3972 alGetErrorString(oserror()));
\r
3973 error(RtError::WARNING);
\r
3977 // Set the sample rate
\r
3978 pvs[0].param = AL_MASTER_CLOCK;
\r
3979 pvs[0].value.i = AL_CRYSTAL_MCLK_TYPE;
\r
3980 pvs[1].param = AL_RATE;
\r
3981 pvs[1].value.ll = alDoubleToFixed((double)sampleRate);
\r
3982 result = alSetParams(resource, pvs, 2);
\r
3983 if ( result < 0 ) {
\r
3984 alClosePort(port);
\r
3985 sprintf(message,"RtAudio: AL error setting sample rate (%d) for device (%s): %s.",
\r
3986 sampleRate, devices[device].name, alGetErrorString(oserror()));
\r
3987 error(RtError::WARNING);
\r
3991 else { // mode == RECORD
\r
3993 // Set our device.
\r
3995 resource = AL_DEFAULT_INPUT;
\r
3997 resource = devices[device].id[1];
\r
3998 result = alSetDevice(al_config, resource);
\r
3999 if ( result == -1 ) {
\r
4000 sprintf(message,"RtAudio: AL error setting device (%s) in AL config: %s.",
\r
4001 devices[device].name, alGetErrorString(oserror()));
\r
4002 error(RtError::WARNING);
\r
4007 port = alOpenPort("RtAudio Output Port", "r", al_config);
\r
4009 sprintf(message,"RtAudio: AL error opening input port: %s.",
\r
4010 alGetErrorString(oserror()));
\r
4011 error(RtError::WARNING);
\r
4015 // Set the sample rate
\r
4016 pvs[0].param = AL_MASTER_CLOCK;
\r
4017 pvs[0].value.i = AL_CRYSTAL_MCLK_TYPE;
\r
4018 pvs[1].param = AL_RATE;
\r
4019 pvs[1].value.ll = alDoubleToFixed((double)sampleRate);
\r
4020 result = alSetParams(resource, pvs, 2);
\r
4021 if ( result < 0 ) {
\r
4022 alClosePort(port);
\r
4023 sprintf(message,"RtAudio: AL error setting sample rate (%d) for device (%s): %s.",
\r
4024 sampleRate, devices[device].name, alGetErrorString(oserror()));
\r
4025 error(RtError::WARNING);
\r
4030 alFreeConfig(al_config);
\r
4032 stream->nUserChannels[mode] = channels;
\r
4033 stream->nDeviceChannels[mode] = channels;
\r
4035 // Set handle and flags for buffer conversion
\r
4036 stream->handle[mode] = port;
\r
4037 stream->doConvertBuffer[mode] = false;
\r
4038 if (stream->userFormat != stream->deviceFormat[mode])
\r
4039 stream->doConvertBuffer[mode] = true;
\r
4041 // Allocate necessary internal buffers
\r
4042 if ( stream->nUserChannels[0] != stream->nUserChannels[1] ) {
\r
4044 long buffer_bytes;
\r
4045 if (stream->nUserChannels[0] >= stream->nUserChannels[1])
\r
4046 buffer_bytes = stream->nUserChannels[0];
\r
4048 buffer_bytes = stream->nUserChannels[1];
\r
4050 buffer_bytes *= *bufferSize * formatBytes(stream->userFormat);
\r
4051 if (stream->userBuffer) free(stream->userBuffer);
\r
4052 stream->userBuffer = (char *) calloc(buffer_bytes, 1);
\r
4053 if (stream->userBuffer == NULL)
\r
4054 goto memory_error;
\r
4057 if ( stream->doConvertBuffer[mode] ) {
\r
4059 long buffer_bytes;
\r
4060 bool makeBuffer = true;
\r
4061 if ( mode == PLAYBACK )
\r
4062 buffer_bytes = stream->nDeviceChannels[0] * formatBytes(stream->deviceFormat[0]);
\r
4063 else { // mode == RECORD
\r
4064 buffer_bytes = stream->nDeviceChannels[1] * formatBytes(stream->deviceFormat[1]);
\r
4065 if ( stream->mode == PLAYBACK ) {
\r
4066 long bytes_out = stream->nDeviceChannels[0] * formatBytes(stream->deviceFormat[0]);
\r
4067 if ( buffer_bytes > bytes_out )
\r
4068 buffer_bytes = (buffer_bytes > bytes_out) ? buffer_bytes : bytes_out;
\r
4070 makeBuffer = false;
\r
4074 if ( makeBuffer ) {
\r
4075 buffer_bytes *= *bufferSize;
\r
4076 if (stream->deviceBuffer) free(stream->deviceBuffer);
\r
4077 stream->deviceBuffer = (char *) calloc(buffer_bytes, 1);
\r
4078 if (stream->deviceBuffer == NULL)
\r
4079 goto memory_error;
\r
4083 stream->device[mode] = device;
\r
4084 stream->state = STREAM_STOPPED;
\r
4085 if ( stream->mode == PLAYBACK && mode == RECORD )
\r
4086 // We had already set up an output stream.
\r
4087 stream->mode = DUPLEX;
\r
4089 stream->mode = mode;
\r
4090 stream->nBuffers = nBuffers;
\r
4091 stream->bufferSize = *bufferSize;
\r
4092 stream->sampleRate = sampleRate;
\r
4097 if (stream->handle[0]) {
\r
4098 alClosePort(stream->handle[0]);
\r
4099 stream->handle[0] = 0;
\r
4101 if (stream->handle[1]) {
\r
4102 alClosePort(stream->handle[1]);
\r
4103 stream->handle[1] = 0;
\r
4105 if (stream->userBuffer) {
\r
4106 free(stream->userBuffer);
\r
4107 stream->userBuffer = 0;
\r
4109 sprintf(message, "RtAudio: ALSA error allocating buffer memory for device (%s).",
\r
4110 devices[device].name);
\r
4111 error(RtError::WARNING);
\r
4115 void RtAudio :: cancelStreamCallback(int streamId)
\r
4117 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
4119 if (stream->usingCallback) {
\r
4120 stream->usingCallback = false;
\r
4121 pthread_cancel(stream->thread);
\r
4122 pthread_join(stream->thread, NULL);
\r
4123 stream->thread = 0;
\r
4124 stream->callback = NULL;
\r
4125 stream->userData = NULL;
\r
4129 void RtAudio :: closeStream(int streamId)
\r
4131 // We don't want an exception to be thrown here because this
\r
4132 // function is called by our class destructor. So, do our own
\r
4133 // streamId check.
\r
4134 if ( streams.find( streamId ) == streams.end() ) {
\r
4135 sprintf(message, "RtAudio: invalid stream identifier!");
\r
4136 error(RtError::WARNING);
\r
4140 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) streams[streamId];
\r
4142 if (stream->usingCallback) {
\r
4143 pthread_cancel(stream->thread);
\r
4144 pthread_join(stream->thread, NULL);
\r
4147 pthread_mutex_destroy(&stream->mutex);
\r
4149 if (stream->handle[0])
\r
4150 alClosePort(stream->handle[0]);
\r
4152 if (stream->handle[1])
\r
4153 alClosePort(stream->handle[1]);
\r
4155 if (stream->userBuffer)
\r
4156 free(stream->userBuffer);
\r
4158 if (stream->deviceBuffer)
\r
4159 free(stream->deviceBuffer);
\r
4162 streams.erase(streamId);
\r
4165 void RtAudio :: startStream(int streamId)
\r
4167 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
4169 if (stream->state == STREAM_RUNNING)
\r
4172 // The AL port is ready as soon as it is opened.
\r
4173 stream->state = STREAM_RUNNING;
\r
4176 void RtAudio :: stopStream(int streamId)
\r
4178 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
4180 MUTEX_LOCK(&stream->mutex);
\r
4182 if (stream->state == STREAM_STOPPED)
\r
4186 int buffer_size = stream->bufferSize * stream->nBuffers;
\r
4188 if (stream->mode == PLAYBACK || stream->mode == DUPLEX)
\r
4189 alZeroFrames(stream->handle[0], buffer_size);
\r
4191 if (stream->mode == RECORD || stream->mode == DUPLEX) {
\r
4192 result = alDiscardFrames(stream->handle[1], buffer_size);
\r
4193 if (result == -1) {
\r
4194 sprintf(message, "RtAudio: AL error draining stream device (%s): %s.",
\r
4195 devices[stream->device[1]].name, alGetErrorString(oserror()));
\r
4196 error(RtError::DRIVER_ERROR);
\r
4199 stream->state = STREAM_STOPPED;
\r
4202 MUTEX_UNLOCK(&stream->mutex);
\r
4205 void RtAudio :: abortStream(int streamId)
\r
4207 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
4209 MUTEX_LOCK(&stream->mutex);
\r
4211 if (stream->state == STREAM_STOPPED)
\r
4214 if (stream->mode == PLAYBACK || stream->mode == DUPLEX) {
\r
4216 int buffer_size = stream->bufferSize * stream->nBuffers;
\r
4217 int result = alDiscardFrames(stream->handle[0], buffer_size);
\r
4218 if (result == -1) {
\r
4219 sprintf(message, "RtAudio: AL error aborting stream device (%s): %s.",
\r
4220 devices[stream->device[0]].name, alGetErrorString(oserror()));
\r
4221 error(RtError::DRIVER_ERROR);
\r
4225 // There is no clear action to take on the input stream, since the
\r
4226 // port will continue to run in any event.
\r
4227 stream->state = STREAM_STOPPED;
\r
4230 MUTEX_UNLOCK(&stream->mutex);
\r
4233 int RtAudio :: streamWillBlock(int streamId)
\r
4235 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
4237 MUTEX_LOCK(&stream->mutex);
\r
4240 if (stream->state == STREAM_STOPPED)
\r
4244 if (stream->mode == PLAYBACK || stream->mode == DUPLEX) {
\r
4245 err = alGetFillable(stream->handle[0]);
\r
4247 sprintf(message, "RtAudio: AL error getting available frames for stream (%s): %s.",
\r
4248 devices[stream->device[0]].name, alGetErrorString(oserror()));
\r
4249 error(RtError::DRIVER_ERROR);
\r
4255 if (stream->mode == RECORD || stream->mode == DUPLEX) {
\r
4256 err = alGetFilled(stream->handle[1]);
\r
4258 sprintf(message, "RtAudio: AL error getting available frames for stream (%s): %s.",
\r
4259 devices[stream->device[1]].name, alGetErrorString(oserror()));
\r
4260 error(RtError::DRIVER_ERROR);
\r
4262 if (frames > err) frames = err;
\r
4265 frames = stream->bufferSize - frames;
\r
4266 if (frames < 0) frames = 0;
\r
4269 MUTEX_UNLOCK(&stream->mutex);
\r
4273 void RtAudio :: tickStream(int streamId)
\r
4275 RTAUDIO_STREAM *stream = (RTAUDIO_STREAM *) verifyStream(streamId);
\r
4277 int stopStream = 0;
\r
4278 if (stream->state == STREAM_STOPPED) {
\r
4279 if (stream->usingCallback) usleep(50000); // sleep 50 milliseconds
\r
4282 else if (stream->usingCallback) {
\r
4283 stopStream = stream->callback(stream->userBuffer, stream->bufferSize, stream->userData);
\r
4286 MUTEX_LOCK(&stream->mutex);
\r
4288 // The state might change while waiting on a mutex.
\r
4289 if (stream->state == STREAM_STOPPED)
\r
4294 RTAUDIO_FORMAT format;
\r
4295 if (stream->mode == PLAYBACK || stream->mode == DUPLEX) {
\r
4297 // Setup parameters and do buffer conversion if necessary.
\r
4298 if (stream->doConvertBuffer[0]) {
\r
4299 convertStreamBuffer(stream, PLAYBACK);
\r
4300 buffer = stream->deviceBuffer;
\r
4301 channels = stream->nDeviceChannels[0];
\r
4302 format = stream->deviceFormat[0];
\r
4305 buffer = stream->userBuffer;
\r
4306 channels = stream->nUserChannels[0];
\r
4307 format = stream->userFormat;
\r
4310 // Do byte swapping if necessary.
\r
4311 if (stream->doByteSwap[0])
\r
4312 byteSwapBuffer(buffer, stream->bufferSize * channels, format);
\r
4314 // Write interleaved samples to device.
\r
4315 alWriteFrames(stream->handle[0], buffer, stream->bufferSize);
\r
4318 if (stream->mode == RECORD || stream->mode == DUPLEX) {
\r
4320 // Setup parameters.
\r
4321 if (stream->doConvertBuffer[1]) {
\r
4322 buffer = stream->deviceBuffer;
\r
4323 channels = stream->nDeviceChannels[1];
\r
4324 format = stream->deviceFormat[1];
\r
4327 buffer = stream->userBuffer;
\r
4328 channels = stream->nUserChannels[1];
\r
4329 format = stream->userFormat;
\r
4332 // Read interleaved samples from device.
\r
4333 alReadFrames(stream->handle[1], buffer, stream->bufferSize);
\r
4335 // Do byte swapping if necessary.
\r
4336 if (stream->doByteSwap[1])
\r
4337 byteSwapBuffer(buffer, stream->bufferSize * channels, format);
\r
4339 // Do buffer conversion if necessary.
\r
4340 if (stream->doConvertBuffer[1])
\r
4341 convertStreamBuffer(stream, RECORD);
\r
4345 MUTEX_UNLOCK(&stream->mutex);
\r
4347 if (stream->usingCallback && stopStream)
\r
4348 this->stopStream(streamId);
\r
4351 extern "C" void *callbackHandler(void *ptr)
\r
4353 RtAudio *object = thread_info.object;
\r
4354 int stream = thread_info.streamId;
\r
4355 bool *usingCallback = (bool *) ptr;
\r
4357 while ( *usingCallback ) {
\r
4358 pthread_testcancel();
\r
4360 object->tickStream(stream);
\r
4362 catch (RtError &exception) {
\r
4363 fprintf(stderr, "\nCallback thread error (%s) ... closing thread.\n\n",
\r
4364 exception.getMessage());
\r
4372 //******************** End of __IRIX_AL__ *********************//
\r
4377 // *************************************************** //
\r
4379 // Private common (OS-independent) RtAudio methods.
\r
4381 // *************************************************** //
\r
4383 // This method can be modified to control the behavior of error
\r
4384 // message reporting and throwing.
\r
4385 void RtAudio :: error(RtError::TYPE type)
\r
4387 if (type == RtError::WARNING) {
\r
4388 #if defined(RTAUDIO_DEBUG)
\r
4389 fprintf(stderr, "\n%s\n\n", message);
\r
4390 else if (type == RtError::DEBUG_WARNING) {
\r
4391 fprintf(stderr, "\n%s\n\n", message);
\r
4395 fprintf(stderr, "\n%s\n\n", message);
\r
4396 throw RtError(message, type);
\r
4400 void *RtAudio :: verifyStream(int streamId)
\r
4402 // Verify the stream key.
\r
4403 if ( streams.find( streamId ) == streams.end() ) {
\r
4404 sprintf(message, "RtAudio: invalid stream identifier!");
\r
4405 error(RtError::INVALID_STREAM);
\r
4408 return streams[streamId];
\r
4411 void RtAudio :: clearDeviceInfo(RTAUDIO_DEVICE *info)
\r
4413 // Don't clear the name or DEVICE_ID fields here ... they are
\r
4414 // typically set prior to a call of this function.
\r
4415 info->probed = false;
\r
4416 info->maxOutputChannels = 0;
\r
4417 info->maxInputChannels = 0;
\r
4418 info->maxDuplexChannels = 0;
\r
4419 info->minOutputChannels = 0;
\r
4420 info->minInputChannels = 0;
\r
4421 info->minDuplexChannels = 0;
\r
4422 info->hasDuplexSupport = false;
\r
4423 info->nSampleRates = 0;
\r
4424 for (int i=0; i<MAX_SAMPLE_RATES; i++)
\r
4425 info->sampleRates[i] = 0;
\r
4426 info->nativeFormats = 0;
\r
4429 int RtAudio :: formatBytes(RTAUDIO_FORMAT format)
\r
4431 if (format == RTAUDIO_SINT16)
\r
4433 else if (format == RTAUDIO_SINT24 || format == RTAUDIO_SINT32 ||
\r
4434 format == RTAUDIO_FLOAT32)
\r
4436 else if (format == RTAUDIO_FLOAT64)
\r
4438 else if (format == RTAUDIO_SINT8)
\r
4441 sprintf(message,"RtAudio: undefined format in formatBytes().");
\r
4442 error(RtError::WARNING);
\r
4447 void RtAudio :: convertStreamBuffer(RTAUDIO_STREAM *stream, STREAM_MODE mode)
\r
4449 // This method does format conversion, input/output channel compensation, and
\r
4450 // data interleaving/deinterleaving. 24-bit integers are assumed to occupy
\r
4451 // the upper three bytes of a 32-bit integer.
\r
4453 int j, channels_in, channels_out, channels;
\r
4454 RTAUDIO_FORMAT format_in, format_out;
\r
4455 char *input, *output;
\r
4457 if (mode == RECORD) { // convert device to user buffer
\r
4458 input = stream->deviceBuffer;
\r
4459 output = stream->userBuffer;
\r
4460 channels_in = stream->nDeviceChannels[1];
\r
4461 channels_out = stream->nUserChannels[1];
\r
4462 format_in = stream->deviceFormat[1];
\r
4463 format_out = stream->userFormat;
\r
4465 else { // convert user to device buffer
\r
4466 input = stream->userBuffer;
\r
4467 output = stream->deviceBuffer;
\r
4468 channels_in = stream->nUserChannels[0];
\r
4469 channels_out = stream->nDeviceChannels[0];
\r
4470 format_in = stream->userFormat;
\r
4471 format_out = stream->deviceFormat[0];
\r
4473 // clear our device buffer when in/out duplex device channels are different
\r
4474 if ( stream->mode == DUPLEX &&
\r
4475 stream->nDeviceChannels[0] != stream->nDeviceChannels[1] )
\r
4476 memset(output, 0, stream->bufferSize * channels_out * formatBytes(format_out));
\r
4479 channels = (channels_in < channels_out) ? channels_in : channels_out;
\r
4481 // Set up the interleave/deinterleave offsets
\r
4482 std::vector<int> offset_in(channels);
\r
4483 std::vector<int> offset_out(channels);
\r
4484 if (mode == RECORD && stream->deInterleave[1]) {
\r
4485 for (int k=0; k<channels; k++) {
\r
4486 offset_in[k] = k * stream->bufferSize;
\r
4487 offset_out[k] = k;
\r
4490 else if (mode == PLAYBACK && stream->deInterleave[0]) {
\r
4491 for (int k=0; k<channels; k++) {
\r
4493 offset_out[k] = k * stream->bufferSize;
\r
4497 for (int k=0; k<channels; k++) {
\r
4499 offset_out[k] = k;
\r
4503 if (format_out == RTAUDIO_FLOAT64) {
\r
4505 FLOAT64 *out = (FLOAT64 *)output;
\r
4507 if (format_in == RTAUDIO_SINT8) {
\r
4508 signed char *in = (signed char *)input;
\r
4509 scale = 1.0 / 128.0;
\r
4510 for (int i=0; i<stream->bufferSize; i++) {
\r
4511 for (j=0; j<channels; j++) {
\r
4512 out[offset_out[j]] = (FLOAT64) in[offset_in[j]];
\r
4513 out[offset_out[j]] *= scale;
\r
4515 in += channels_in;
\r
4516 out += channels_out;
\r
4519 else if (format_in == RTAUDIO_SINT16) {
\r
4520 INT16 *in = (INT16 *)input;
\r
4521 scale = 1.0 / 32768.0;
\r
4522 for (int i=0; i<stream->bufferSize; i++) {
\r
4523 for (j=0; j<channels; j++) {
\r
4524 out[offset_out[j]] = (FLOAT64) in[offset_in[j]];
\r
4525 out[offset_out[j]] *= scale;
\r
4527 in += channels_in;
\r
4528 out += channels_out;
\r
4531 else if (format_in == RTAUDIO_SINT24) {
\r
4532 INT32 *in = (INT32 *)input;
\r
4533 scale = 1.0 / 2147483648.0;
\r
4534 for (int i=0; i<stream->bufferSize; i++) {
\r
4535 for (j=0; j<channels; j++) {
\r
4536 out[offset_out[j]] = (FLOAT64) (in[offset_in[j]] & 0xffffff00);
\r
4537 out[offset_out[j]] *= scale;
\r
4539 in += channels_in;
\r
4540 out += channels_out;
\r
4543 else if (format_in == RTAUDIO_SINT32) {
\r
4544 INT32 *in = (INT32 *)input;
\r
4545 scale = 1.0 / 2147483648.0;
\r
4546 for (int i=0; i<stream->bufferSize; i++) {
\r
4547 for (j=0; j<channels; j++) {
\r
4548 out[offset_out[j]] = (FLOAT64) in[offset_in[j]];
\r
4549 out[offset_out[j]] *= scale;
\r
4551 in += channels_in;
\r
4552 out += channels_out;
\r
4555 else if (format_in == RTAUDIO_FLOAT32) {
\r
4556 FLOAT32 *in = (FLOAT32 *)input;
\r
4557 for (int i=0; i<stream->bufferSize; i++) {
\r
4558 for (j=0; j<channels; j++) {
\r
4559 out[offset_out[j]] = (FLOAT64) in[offset_in[j]];
\r
4561 in += channels_in;
\r
4562 out += channels_out;
\r
4565 else if (format_in == RTAUDIO_FLOAT64) {
\r
4566 // Channel compensation and/or (de)interleaving only.
\r
4567 FLOAT64 *in = (FLOAT64 *)input;
\r
4568 for (int i=0; i<stream->bufferSize; i++) {
\r
4569 for (j=0; j<channels; j++) {
\r
4570 out[offset_out[j]] = in[offset_in[j]];
\r
4572 in += channels_in;
\r
4573 out += channels_out;
\r
4577 else if (format_out == RTAUDIO_FLOAT32) {
\r
4579 FLOAT32 *out = (FLOAT32 *)output;
\r
4581 if (format_in == RTAUDIO_SINT8) {
\r
4582 signed char *in = (signed char *)input;
\r
4583 scale = 1.0 / 128.0;
\r
4584 for (int i=0; i<stream->bufferSize; i++) {
\r
4585 for (j=0; j<channels; j++) {
\r
4586 out[offset_out[j]] = (FLOAT32) in[offset_in[j]];
\r
4587 out[offset_out[j]] *= scale;
\r
4589 in += channels_in;
\r
4590 out += channels_out;
\r
4593 else if (format_in == RTAUDIO_SINT16) {
\r
4594 INT16 *in = (INT16 *)input;
\r
4595 scale = 1.0 / 32768.0;
\r
4596 for (int i=0; i<stream->bufferSize; i++) {
\r
4597 for (j=0; j<channels; j++) {
\r
4598 out[offset_out[j]] = (FLOAT32) in[offset_in[j]];
\r
4599 out[offset_out[j]] *= scale;
\r
4601 in += channels_in;
\r
4602 out += channels_out;
\r
4605 else if (format_in == RTAUDIO_SINT24) {
\r
4606 INT32 *in = (INT32 *)input;
\r
4607 scale = 1.0 / 2147483648.0;
\r
4608 for (int i=0; i<stream->bufferSize; i++) {
\r
4609 for (j=0; j<channels; j++) {
\r
4610 out[offset_out[j]] = (FLOAT32) (in[offset_in[j]] & 0xffffff00);
\r
4611 out[offset_out[j]] *= scale;
\r
4613 in += channels_in;
\r
4614 out += channels_out;
\r
4617 else if (format_in == RTAUDIO_SINT32) {
\r
4618 INT32 *in = (INT32 *)input;
\r
4619 scale = 1.0 / 2147483648.0;
\r
4620 for (int i=0; i<stream->bufferSize; i++) {
\r
4621 for (j=0; j<channels; j++) {
\r
4622 out[offset_out[j]] = (FLOAT32) in[offset_in[j]];
\r
4623 out[offset_out[j]] *= scale;
\r
4625 in += channels_in;
\r
4626 out += channels_out;
\r
4629 else if (format_in == RTAUDIO_FLOAT32) {
\r
4630 // Channel compensation and/or (de)interleaving only.
\r
4631 FLOAT32 *in = (FLOAT32 *)input;
\r
4632 for (int i=0; i<stream->bufferSize; i++) {
\r
4633 for (j=0; j<channels; j++) {
\r
4634 out[offset_out[j]] = in[offset_in[j]];
\r
4636 in += channels_in;
\r
4637 out += channels_out;
\r
4640 else if (format_in == RTAUDIO_FLOAT64) {
\r
4641 FLOAT64 *in = (FLOAT64 *)input;
\r
4642 for (int i=0; i<stream->bufferSize; i++) {
\r
4643 for (j=0; j<channels; j++) {
\r
4644 out[offset_out[j]] = (FLOAT32) in[offset_in[j]];
\r
4646 in += channels_in;
\r
4647 out += channels_out;
\r
4651 else if (format_out == RTAUDIO_SINT32) {
\r
4652 INT32 *out = (INT32 *)output;
\r
4653 if (format_in == RTAUDIO_SINT8) {
\r
4654 signed char *in = (signed char *)input;
\r
4655 for (int i=0; i<stream->bufferSize; i++) {
\r
4656 for (j=0; j<channels; j++) {
\r
4657 out[offset_out[j]] = (INT32) in[offset_in[j]];
\r
4658 out[offset_out[j]] <<= 24;
\r
4660 in += channels_in;
\r
4661 out += channels_out;
\r
4664 else if (format_in == RTAUDIO_SINT16) {
\r
4665 INT16 *in = (INT16 *)input;
\r
4666 for (int i=0; i<stream->bufferSize; i++) {
\r
4667 for (j=0; j<channels; j++) {
\r
4668 out[offset_out[j]] = (INT32) in[offset_in[j]];
\r
4669 out[offset_out[j]] <<= 16;
\r
4671 in += channels_in;
\r
4672 out += channels_out;
\r
4675 else if (format_in == RTAUDIO_SINT24) {
\r
4676 INT32 *in = (INT32 *)input;
\r
4677 for (int i=0; i<stream->bufferSize; i++) {
\r
4678 for (j=0; j<channels; j++) {
\r
4679 out[offset_out[j]] = (INT32) in[offset_in[j]];
\r
4681 in += channels_in;
\r
4682 out += channels_out;
\r
4685 else if (format_in == RTAUDIO_SINT32) {
\r
4686 // Channel compensation and/or (de)interleaving only.
\r
4687 INT32 *in = (INT32 *)input;
\r
4688 for (int i=0; i<stream->bufferSize; i++) {
\r
4689 for (j=0; j<channels; j++) {
\r
4690 out[offset_out[j]] = in[offset_in[j]];
\r
4692 in += channels_in;
\r
4693 out += channels_out;
\r
4696 else if (format_in == RTAUDIO_FLOAT32) {
\r
4697 FLOAT32 *in = (FLOAT32 *)input;
\r
4698 for (int i=0; i<stream->bufferSize; i++) {
\r
4699 for (j=0; j<channels; j++) {
\r
4700 out[offset_out[j]] = (INT32) (in[offset_in[j]] * 2147483647.0);
\r
4702 in += channels_in;
\r
4703 out += channels_out;
\r
4706 else if (format_in == RTAUDIO_FLOAT64) {
\r
4707 FLOAT64 *in = (FLOAT64 *)input;
\r
4708 for (int i=0; i<stream->bufferSize; i++) {
\r
4709 for (j=0; j<channels; j++) {
\r
4710 out[offset_out[j]] = (INT32) (in[offset_in[j]] * 2147483647.0);
\r
4712 in += channels_in;
\r
4713 out += channels_out;
\r
4717 else if (format_out == RTAUDIO_SINT24) {
\r
4718 INT32 *out = (INT32 *)output;
\r
4719 if (format_in == RTAUDIO_SINT8) {
\r
4720 signed char *in = (signed char *)input;
\r
4721 for (int i=0; i<stream->bufferSize; i++) {
\r
4722 for (j=0; j<channels; j++) {
\r
4723 out[offset_out[j]] = (INT32) in[offset_in[j]];
\r
4724 out[offset_out[j]] <<= 24;
\r
4726 in += channels_in;
\r
4727 out += channels_out;
\r
4730 else if (format_in == RTAUDIO_SINT16) {
\r
4731 INT16 *in = (INT16 *)input;
\r
4732 for (int i=0; i<stream->bufferSize; i++) {
\r
4733 for (j=0; j<channels; j++) {
\r
4734 out[offset_out[j]] = (INT32) in[offset_in[j]];
\r
4735 out[offset_out[j]] <<= 16;
\r
4737 in += channels_in;
\r
4738 out += channels_out;
\r
4741 else if (format_in == RTAUDIO_SINT24) {
\r
4742 // Channel compensation and/or (de)interleaving only.
\r
4743 INT32 *in = (INT32 *)input;
\r
4744 for (int i=0; i<stream->bufferSize; i++) {
\r
4745 for (j=0; j<channels; j++) {
\r
4746 out[offset_out[j]] = in[offset_in[j]];
\r
4748 in += channels_in;
\r
4749 out += channels_out;
\r
4752 else if (format_in == RTAUDIO_SINT32) {
\r
4753 INT32 *in = (INT32 *)input;
\r
4754 for (int i=0; i<stream->bufferSize; i++) {
\r
4755 for (j=0; j<channels; j++) {
\r
4756 out[offset_out[j]] = (INT32) (in[offset_in[j]] & 0xffffff00);
\r
4758 in += channels_in;
\r
4759 out += channels_out;
\r
4762 else if (format_in == RTAUDIO_FLOAT32) {
\r
4763 FLOAT32 *in = (FLOAT32 *)input;
\r
4764 for (int i=0; i<stream->bufferSize; i++) {
\r
4765 for (j=0; j<channels; j++) {
\r
4766 out[offset_out[j]] = (INT32) (in[offset_in[j]] * 2147483647.0);
\r
4768 in += channels_in;
\r
4769 out += channels_out;
\r
4772 else if (format_in == RTAUDIO_FLOAT64) {
\r
4773 FLOAT64 *in = (FLOAT64 *)input;
\r
4774 for (int i=0; i<stream->bufferSize; i++) {
\r
4775 for (j=0; j<channels; j++) {
\r
4776 out[offset_out[j]] = (INT32) (in[offset_in[j]] * 2147483647.0);
\r
4778 in += channels_in;
\r
4779 out += channels_out;
\r
4783 else if (format_out == RTAUDIO_SINT16) {
\r
4784 INT16 *out = (INT16 *)output;
\r
4785 if (format_in == RTAUDIO_SINT8) {
\r
4786 signed char *in = (signed char *)input;
\r
4787 for (int i=0; i<stream->bufferSize; i++) {
\r
4788 for (j=0; j<channels; j++) {
\r
4789 out[offset_out[j]] = (INT16) in[offset_in[j]];
\r
4790 out[offset_out[j]] <<= 8;
\r
4792 in += channels_in;
\r
4793 out += channels_out;
\r
4796 else if (format_in == RTAUDIO_SINT16) {
\r
4797 // Channel compensation and/or (de)interleaving only.
\r
4798 INT16 *in = (INT16 *)input;
\r
4799 for (int i=0; i<stream->bufferSize; i++) {
\r
4800 for (j=0; j<channels; j++) {
\r
4801 out[offset_out[j]] = in[offset_in[j]];
\r
4803 in += channels_in;
\r
4804 out += channels_out;
\r
4807 else if (format_in == RTAUDIO_SINT24) {
\r
4808 INT32 *in = (INT32 *)input;
\r
4809 for (int i=0; i<stream->bufferSize; i++) {
\r
4810 for (j=0; j<channels; j++) {
\r
4811 out[offset_out[j]] = (INT16) ((in[offset_in[j]] >> 16) & 0x0000ffff);
\r
4813 in += channels_in;
\r
4814 out += channels_out;
\r
4817 else if (format_in == RTAUDIO_SINT32) {
\r
4818 INT32 *in = (INT32 *)input;
\r
4819 for (int i=0; i<stream->bufferSize; i++) {
\r
4820 for (j=0; j<channels; j++) {
\r
4821 out[offset_out[j]] = (INT16) ((in[offset_in[j]] >> 16) & 0x0000ffff);
\r
4823 in += channels_in;
\r
4824 out += channels_out;
\r
4827 else if (format_in == RTAUDIO_FLOAT32) {
\r
4828 FLOAT32 *in = (FLOAT32 *)input;
\r
4829 for (int i=0; i<stream->bufferSize; i++) {
\r
4830 for (j=0; j<channels; j++) {
\r
4831 out[offset_out[j]] = (INT16) (in[offset_in[j]] * 32767.0);
\r
4833 in += channels_in;
\r
4834 out += channels_out;
\r
4837 else if (format_in == RTAUDIO_FLOAT64) {
\r
4838 FLOAT64 *in = (FLOAT64 *)input;
\r
4839 for (int i=0; i<stream->bufferSize; i++) {
\r
4840 for (j=0; j<channels; j++) {
\r
4841 out[offset_out[j]] = (INT16) (in[offset_in[j]] * 32767.0);
\r
4843 in += channels_in;
\r
4844 out += channels_out;
\r
4848 else if (format_out == RTAUDIO_SINT8) {
\r
4849 signed char *out = (signed char *)output;
\r
4850 if (format_in == RTAUDIO_SINT8) {
\r
4851 // Channel compensation and/or (de)interleaving only.
\r
4852 signed char *in = (signed char *)input;
\r
4853 for (int i=0; i<stream->bufferSize; i++) {
\r
4854 for (j=0; j<channels; j++) {
\r
4855 out[offset_out[j]] = in[offset_in[j]];
\r
4857 in += channels_in;
\r
4858 out += channels_out;
\r
4861 if (format_in == RTAUDIO_SINT16) {
\r
4862 INT16 *in = (INT16 *)input;
\r
4863 for (int i=0; i<stream->bufferSize; i++) {
\r
4864 for (j=0; j<channels; j++) {
\r
4865 out[offset_out[j]] = (signed char) ((in[offset_in[j]] >> 8) & 0x00ff);
\r
4867 in += channels_in;
\r
4868 out += channels_out;
\r
4871 else if (format_in == RTAUDIO_SINT24) {
\r
4872 INT32 *in = (INT32 *)input;
\r
4873 for (int i=0; i<stream->bufferSize; i++) {
\r
4874 for (j=0; j<channels; j++) {
\r
4875 out[offset_out[j]] = (signed char) ((in[offset_in[j]] >> 24) & 0x000000ff);
\r
4877 in += channels_in;
\r
4878 out += channels_out;
\r
4881 else if (format_in == RTAUDIO_SINT32) {
\r
4882 INT32 *in = (INT32 *)input;
\r
4883 for (int i=0; i<stream->bufferSize; i++) {
\r
4884 for (j=0; j<channels; j++) {
\r
4885 out[offset_out[j]] = (signed char) ((in[offset_in[j]] >> 24) & 0x000000ff);
\r
4887 in += channels_in;
\r
4888 out += channels_out;
\r
4891 else if (format_in == RTAUDIO_FLOAT32) {
\r
4892 FLOAT32 *in = (FLOAT32 *)input;
\r
4893 for (int i=0; i<stream->bufferSize; i++) {
\r
4894 for (j=0; j<channels; j++) {
\r
4895 out[offset_out[j]] = (signed char) (in[offset_in[j]] * 127.0);
\r
4897 in += channels_in;
\r
4898 out += channels_out;
\r
4901 else if (format_in == RTAUDIO_FLOAT64) {
\r
4902 FLOAT64 *in = (FLOAT64 *)input;
\r
4903 for (int i=0; i<stream->bufferSize; i++) {
\r
4904 for (j=0; j<channels; j++) {
\r
4905 out[offset_out[j]] = (signed char) (in[offset_in[j]] * 127.0);
\r
4907 in += channels_in;
\r
4908 out += channels_out;
\r
4914 void RtAudio :: byteSwapBuffer(char *buffer, int samples, RTAUDIO_FORMAT format)
\r
4916 register char val;
\r
4917 register char *ptr;
\r
4920 if (format == RTAUDIO_SINT16) {
\r
4921 for (int i=0; i<samples; i++) {
\r
4922 // Swap 1st and 2nd bytes.
\r
4924 *(ptr) = *(ptr+1);
\r
4927 // Increment 2 bytes.
\r
4931 else if (format == RTAUDIO_SINT24 ||
\r
4932 format == RTAUDIO_SINT32 ||
\r
4933 format == RTAUDIO_FLOAT32) {
\r
4934 for (int i=0; i<samples; i++) {
\r
4935 // Swap 1st and 4th bytes.
\r
4937 *(ptr) = *(ptr+3);
\r
4940 // Swap 2nd and 3rd bytes.
\r
4943 *(ptr) = *(ptr+1);
\r
4946 // Increment 4 bytes.
\r
4950 else if (format == RTAUDIO_FLOAT64) {
\r
4951 for (int i=0; i<samples; i++) {
\r
4952 // Swap 1st and 8th bytes
\r
4954 *(ptr) = *(ptr+7);
\r
4957 // Swap 2nd and 7th bytes
\r
4960 *(ptr) = *(ptr+5);
\r
4963 // Swap 3rd and 6th bytes
\r
4966 *(ptr) = *(ptr+3);
\r
4969 // Swap 4th and 5th bytes
\r
4972 *(ptr) = *(ptr+1);
\r
4975 // Increment 8 bytes.
\r
4982 // *************************************************** //
\r
4984 // RtError class definition.
\r
4986 // *************************************************** //
\r
4988 RtError :: RtError(const char *p, TYPE tipe)
\r
4991 strncpy(error_message, p, 256);
\r
4994 RtError :: ~RtError()
\r
4998 void RtError :: printMessage()
\r
5000 printf("\n%s\n\n", error_message);
\r