annotate rtaudio-test.cpp @ 1:9cadc470e3da

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

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