annotate audiodelay.cpp @ 1:9cadc470e3da

reorganize, add more
author pmeerw@pan
date Fri, 30 Oct 2009 23:07:52 +0100
parents audiodelay/audiodelay.cpp@deadffdf5d60
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
1 /******************************************/
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
2 /*
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
3 audiodelay.cpp
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
4 by Peter Meerwald, pmeerw@pmeerw.net, 2009.
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
5
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
6 This program tests the delay between audio output and input.
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
7 */
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
8 /******************************************/
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
9
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
10 #include "RtAudio.h"
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
11 #include <stdlib.h>
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
12 #include <stdio.h>
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
13 #include <math.h>
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
14 #include <string.h>
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
15 #include <sys/time.h>
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
16 #include <time.h>
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
17
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
18 const char *progname = "audiodelay";
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
19
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
20 typedef signed short AUDIOSAMPLE_TYPE;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
21 #define FORMAT RTAUDIO_SINT16
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
22
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
23 static volatile unsigned int nmeasurements = 0;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
24 static float accum_measurements = 0.0f;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
25
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
26 static int inout(void *out_buf, void *in_buf, unsigned int nbuf_frames,
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
27 double stream_time, RtAudioStreamStatus status, void *data) {
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
28 if (status)
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
29 printf("%s: stream over/underflow detected\n", progname);
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
30
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
31 static unsigned int count_inout = 0;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
32 static struct timeval tv_start;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
33 static struct timeval tv_stop;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
34
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
35 AUDIOSAMPLE_TYPE *pout = (AUDIOSAMPLE_TYPE *)out_buf, *pin = (AUDIOSAMPLE_TYPE *)in_buf;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
36
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
37 memset(out_buf, 0, nbuf_frames*sizeof(AUDIOSAMPLE_TYPE) * 2);
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
38 if ((count_inout % (512*128/nbuf_frames)) == 0) {
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
39 for (unsigned int i = nbuf_frames/2-4; i < nbuf_frames/2+4; i++) {
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
40 pout[2*i] = 32000;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
41 pout[2*i+1] = 32000;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
42 }
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
43
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
44 gettimeofday(&tv_start, 0);
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
45 }
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
46
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
47 if (1) {
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
48 for (unsigned int i = 0; i < nbuf_frames; i++) {
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
49 if (pin[2*i] > 15000 || pin[2*i+1] > 15000) {
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
50
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
51 gettimeofday(&tv_stop, 0);
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
52
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
53 unsigned int msec = (tv_stop.tv_sec - tv_start.tv_sec) * 1000;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
54 if (tv_stop.tv_usec < tv_start.tv_usec)
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
55 msec -= (tv_start.tv_usec - tv_stop.tv_usec) / 1000;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
56 else
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
57 msec += (tv_stop.tv_usec - tv_start.tv_usec) / 1000;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
58
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
59 accum_measurements += msec;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
60 nmeasurements++;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
61 printf("%s: measured delay %u ms, offset %u, average %.2f\n", progname, msec, i, accum_measurements / nmeasurements);
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
62
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
63 break;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
64 }
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
65 }
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
66 }
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
67
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
68 count_inout++;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
69
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
70 return 0;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
71 }
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
72
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
73 int main(int argc, char *argv[]) {
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
74 unsigned int channels = 2, sample_rate = 44100, buf_bytes, odevice = 0, idevice = 0, ioffset = 0, ooffset = 0;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
75
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
76 RtAudio adac;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
77 if (adac.getDeviceCount() < 1) {
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
78 std::cout << "\nNo audio devices found!\n";
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
79 exit(EXIT_FAILURE);
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
80 }
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
81
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
82 // Let RtAudio print messages to stderr.
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
83 adac.showWarnings(true);
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
84
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
85 // Set the same number of channels for both input and output.
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
86 unsigned int buf_frames = 128;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
87 RtAudio::StreamParameters iparams, oparams;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
88 iparams.deviceId = idevice;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
89 iparams.nChannels = channels;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
90 iparams.firstChannel = ioffset;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
91 oparams.deviceId = odevice;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
92 oparams.nChannels = channels;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
93 oparams.firstChannel = ooffset;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
94
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
95 RtAudio::StreamOptions options;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
96 options.flags = 0; // RTAUDIO_MINIMIZE_LATENCY, RTAUDIO_HOG_DEVICE, RTAUDIO_SCHEDULE_REALTIME
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
97 options.numberOfBuffers = 2;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
98 options.priority = 0; // for RTAUDIO_SCHEDULE_REALTIME
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
99
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
100 try {
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
101 adac.openStream(&oparams, &iparams, FORMAT, sample_rate, &buf_frames, &inout, (void *)&buf_bytes, &options);
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
102 }
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
103 catch (RtError& e) {
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
104 printf("%s: %s\n", progname, e.getMessage().c_str());
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
105 exit(EXIT_FAILURE);
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
106 }
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
107
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
108 buf_bytes = buf_frames * channels * sizeof(AUDIOSAMPLE_TYPE);
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
109
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
110 try {
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
111 adac.startStream();
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
112
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
113 printf("%s: stream latency %ld frames, %d sample rate\n", progname, adac.getStreamLatency(), adac.getStreamSampleRate());
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
114
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
115 printf("%s: running ... press <enter> to quit (%d buffer frames)\n", progname, buf_frames);
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
116 getchar();
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
117
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
118 // Stop the stream.
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
119 adac.stopStream();
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
120 }
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
121 catch (RtError& e) {
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
122 printf("%s: %s\n", progname, e.getMessage().c_str());
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
123 goto cleanup;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
124 }
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
125
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
126 cleanup:
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
127 if (adac.isStreamOpen())
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
128 adac.closeStream();
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
129
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
130 return 0;
deadffdf5d60 import audiodelay
pmeerw@pan
parents:
diff changeset
131 }

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