comparison rtaudio-test.cpp @ 1:9cadc470e3da

reorganize, add more
author pmeerw@pan
date Fri, 30 Oct 2009 23:07:52 +0100
parents
children
comparison
equal deleted inserted replaced
0:deadffdf5d60 1:9cadc470e3da
1 #include <cmath>
2 #include <stdlib.h>
3 #include <iostream>
4 #include <vector>
5 #include "RtAudio.h"
6
7 int myCallback(void *outputBuffer, void *inputBuffer, unsigned int bufferFrames, double streamTime, RtAudioStreamStatus status, void *userData) {
8 unsigned int i, j;
9 float *buffer = (float *) outputBuffer;
10
11 if ( status )
12 std::cout << "Stream underflow detected!" << std::endl;
13
14 if (!buffer)
15 return 0;
16
17 // Write interleaved audio data.
18 static int t = 0;
19 for ( i=0; i<bufferFrames/2; i++ ) {
20 for ( j=0; j<2; j++ ) {
21 *buffer++ = 0; // 0.25*cosf((t/(float)1000));
22
23 }
24 if (++t > 1000) t = 0;
25 }
26
27 return 0;
28 }
29
30 int main() {
31 RtAudio audio;
32
33 // Determine the number of devices available
34 unsigned int devices = audio.getDeviceCount();
35 std::cout << devices << " devices found\n";
36
37 // Scan through devices for various capabilities
38 RtAudio::DeviceInfo info;
39 for ( unsigned int i=0; i<devices; i++ ) {
40
41 info = audio.getDeviceInfo( i );
42
43 if ( info.probed == true ) {
44 // Print, for example, the maximum number of output channels for each device
45 std::cout << "device: #" << i << " - " << info.name << "\n";
46 std::cout << " output channels: " << info.outputChannels << "\n";
47 std::cout << " input channels: " << info.inputChannels << "\n";
48 std::cout << " duplex channels: " << info.duplexChannels << "\n";
49 std::cout << " default input/output: " << info.isDefaultInput << " / " << info.isDefaultOutput << "\n";
50
51 std::cout << " sample rates:\n";
52 for (std::vector<unsigned int>::iterator i = info.sampleRates.begin(); i != info.sampleRates.end(); i++) {
53 std::cout << " " << *i << "\n";
54 }
55 std::cout << " data formats:\n";
56 if (info.nativeFormats & RTAUDIO_SINT8) std::cout << " signed 8-bit\n";
57 if (info.nativeFormats & RTAUDIO_SINT16) std::cout << " signed 16-bit\n";
58 if (info.nativeFormats & RTAUDIO_SINT24) std::cout << " signed 24-bit\n";
59 if (info.nativeFormats & RTAUDIO_SINT32) std::cout << " signed 32-bit\n";
60 if (info.nativeFormats & RTAUDIO_FLOAT32) std::cout << " 32-bit float [-1,1]\n";
61 if (info.nativeFormats & RTAUDIO_FLOAT64) std::cout << " 64-bit float [-1,1]\n";
62 }
63 else
64 std::cout << "device: #" << i << " - not probed\n";
65 }
66
67 RtAudio::StreamParameters oparam;
68 oparam.deviceId = audio.getDefaultOutputDevice();
69 oparam.nChannels = 1;
70 oparam.firstChannel = 0;
71
72 RtAudio::StreamParameters iparam;
73 iparam.deviceId = audio.getDefaultInputDevice();
74 iparam.nChannels = 1;
75 iparam.firstChannel = 0;
76
77
78 unsigned int sampleRate = 44100;
79 unsigned int bufferFrames = 256; // 256 sample frames
80
81 RtAudio::StreamOptions options;
82 options.flags = 0;
83 options.numberOfBuffers = 2;
84 options.streamName = "test";
85
86
87 try {
88 audio.openStream( &oparam, &iparam, RTAUDIO_FLOAT32,
89 sampleRate, &bufferFrames, &myCallback, (void*) 0, &options );
90 audio.startStream();
91 }
92 catch ( RtError& e ) {
93 std::cout << '\n' << e.getMessage() << '\n' << std::endl;
94 exit( EXIT_FAILURE );
95 }
96
97 char input;
98 std::cout << "\nPlaying ... press <enter> to quit.\n";
99 std::cin.get( input );
100
101 try {
102 // Stop the stream
103 audio.stopStream();
104 }
105 catch (RtError& e) {
106 e.printMessage();
107 }
108
109 if ( audio.isStreamOpen() )
110 audio.closeStream();
111
112 return EXIT_SUCCESS;
113 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.