File: teststops.cpp

package info (click to toggle)
rtaudio 4.1.1~ds0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 832 kB
  • ctags: 555
  • sloc: cpp: 9,188; sh: 2,494; makefile: 32
file content (271 lines) | stat: -rw-r--r-- 7,829 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/******************************************/
/*
  teststop.cpp
  by Gary P. Scavone, 2011

  This program starts and stops an RtAudio
  stream many times in succession and in
  different ways to to test its functionality.
*/
/******************************************/

#include "RtAudio.h"
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>

#define PULSE_RATE 0.01  // seconds
#define RUNTIME    0.4   // seconds
#define PAUSETIME  0.1   // seconds
#define REPETITIONS 10

// Platform-dependent sleep routines.
#if defined( __WINDOWS_ASIO__ ) || defined( __WINDOWS_DS__ ) || defined( __WINDOWS_WASAPI__ )
  #include <windows.h>
  #define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds ) 
#else // Unix variants
  #include <unistd.h>
  #define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )
#endif

void usage( void ) {
  // Error function in case of incorrect command-line
  // argument specifications
  std::cout << "\nuseage: teststops N fs <iDevice> <oDevice> <iChannelOffset> <oChannelOffset>\n";
  std::cout << "    where N = number of channels,\n";
  std::cout << "    fs = the sample rate,\n";
  std::cout << "    iDevice = optional input device to use (default = 0),\n";
  std::cout << "    oDevice = optional output device to use (default = 0),\n";
  std::cout << "    iChannelOffset = an optional input channel offset (default = 0),\n";
  std::cout << "    and oChannelOffset = optional output channel offset (default = 0).\n\n";
  exit( 0 );
}

struct MyData {
  unsigned int channels;
  unsigned int pulseCount;
  unsigned int frameCounter;
  unsigned int nFrames;
  unsigned int returnValue;
};

// Interleaved buffers
int pulse( void *outputBuffer, void * /*inputBuffer*/, unsigned int nBufferFrames,
           double /*streamTime*/, RtAudioStreamStatus status, void *mydata )
{
  // Write out a pulse signal and ignore the input buffer.
  unsigned int i, j;
  float sample;
  float *buffer = (float *) outputBuffer;
  MyData *data = (MyData *) mydata;

  if ( status ) std::cout << "Stream over/underflow detected!" << std::endl;

  for ( i=0; i<nBufferFrames; i++ ) {
    if ( data->frameCounter % data->pulseCount == 0 ) sample = 0.9;
    else sample = 0.0;
    for ( j=0; j<data->channels; j++ )
      *buffer++ = sample;

    data->frameCounter++;
  }

  if ( data->frameCounter >= data->nFrames )
    return data->returnValue;
  else
    return 0;
}

int main( int argc, char *argv[] )
{
  unsigned int bufferFrames, fs, oDevice = 0, iDevice = 0, iOffset = 0, oOffset = 0;
  unsigned int runtime, pausetime;
  char input;

  // minimal command-line checking
  if (argc < 3 || argc > 7 ) usage();

  RtAudio *adc = new RtAudio();
  if ( adc->getDeviceCount() < 1 ) {
    std::cout << "\nNo audio devices found!\n";
    exit( 1 );
  }

  MyData mydata;
  mydata.channels = (unsigned int) atoi( argv[1] );
  fs = (unsigned int) atoi( argv[2] );
  if ( argc > 3 )
    iDevice = (unsigned int) atoi( argv[3] );
  if ( argc > 4 )
    oDevice = (unsigned int) atoi(argv[4]);
  if ( argc > 5 )
    iOffset = (unsigned int) atoi(argv[5]);
  if ( argc > 6 )
    oOffset = (unsigned int) atoi(argv[6]);

  // Let RtAudio print messages to stderr.
  adc->showWarnings( true );

  runtime = RUNTIME * 1000;
  pausetime = PAUSETIME * 1000;

  // Set our stream parameters for a duplex stream.
  bufferFrames = 512;
  RtAudio::StreamParameters oParams, iParams;
  oParams.deviceId = oDevice;
  oParams.nChannels = mydata.channels;
  oParams.firstChannel = oOffset;

  iParams.deviceId = iDevice;
  iParams.nChannels = mydata.channels;
  iParams.firstChannel = iOffset;

  if ( iDevice == 0 )
    iParams.deviceId = adc->getDefaultInputDevice();
  if ( oDevice == 0 )
    oParams.deviceId = adc->getDefaultOutputDevice();

  // First, test external stopStream() calls.
  mydata.pulseCount = PULSE_RATE * fs;
  mydata.nFrames = 50 * fs;
  mydata.returnValue = 0;
  try {
    adc->openStream( &oParams, &iParams, RTAUDIO_SINT32, fs, &bufferFrames, &pulse, (void *)&mydata );

    std::cout << "Press <enter> to start test.\n";
    std::cin.get( input );

    for (int i=0; i<REPETITIONS; i++ ) {
      mydata.frameCounter = 0;
      adc->startStream();
      std::cout << "Stream started ... ";
      SLEEP( runtime );
      adc->stopStream();
      std::cout << "stream externally stopped.\n";
      SLEEP( pausetime );
    }
  }
  catch ( RtAudioError& e ) {
    e.printMessage();
    goto cleanup;
  }

  adc->closeStream();

  // Next, test internal stopStream() calls.
  mydata.nFrames = (unsigned int) (RUNTIME * fs);
  mydata.returnValue = 1;
  try {
    adc->openStream( &oParams, &iParams, RTAUDIO_SINT32, fs, &bufferFrames, &pulse, (void *)&mydata );

    std::cin.clear();
    fflush(stdin);
    std::cout << "\nPress <enter> to continue test.\n";
    std::cin.get( input );

    for (int i=0; i<REPETITIONS; i++ ) {
      mydata.frameCounter = 0;
      adc->startStream();
      std::cout << "Stream started ... ";
      while ( adc->isStreamRunning() ) SLEEP( 5 );
      std::cout << "stream stopped via callback return value = 1.\n";
      SLEEP( pausetime );
    }
  }
  catch ( RtAudioError& e ) {
    e.printMessage();
    goto cleanup;
  }

  adc->closeStream();

  // Test internal abortStream() calls.
  mydata.returnValue = 2;
  try {
    adc->openStream( &oParams, &iParams, RTAUDIO_SINT32, fs, &bufferFrames, &pulse, (void *)&mydata );
    std::cin.clear();
    fflush(stdin);
    std::cout << "\nPress <enter> to continue test.\n";
    std::cin.get( input );

    for (int i=0; i<REPETITIONS; i++ ) {
      mydata.frameCounter = 0;
      adc->startStream();
      std::cout << "Stream started ... ";
      while ( adc->isStreamRunning() ) SLEEP( 5 );
      std::cout << "stream aborted via callback return value = 2.\n";
      SLEEP( pausetime );
    }
  }
  catch ( RtAudioError& e ) {
    e.printMessage();
    goto cleanup;
  }

  adc->closeStream();

  // Test consecutive stream re-opening.
  mydata.returnValue = 0;
  mydata.nFrames = 50 * fs;
  try {

    std::cin.clear();
    fflush(stdin);
    std::cout << "\nPress <enter> to continue test.\n";
    std::cin.get( input );

    for (int i=0; i<REPETITIONS; i++ ) {
      adc->openStream( &oParams, &iParams, RTAUDIO_SINT32, fs, &bufferFrames, &pulse, (void *)&mydata );
      mydata.frameCounter = 0;
      adc->startStream();
      std::cout << "New stream started ... ";
      SLEEP( runtime );
      adc->stopStream();
      adc->closeStream();
      std::cout << "stream stopped externally and closed.\n";
      SLEEP( pausetime );
    }
  }
  catch ( RtAudioError& e ) {
    e.printMessage();
    goto cleanup;
  }

  delete adc;
  adc = 0;

  // Test consecutive RtAudio creating and deletion.
  try {

    std::cin.clear();
    fflush(stdin);
    std::cout << "\nPress <enter> to continue test.\n";
    std::cin.get( input );

    for (int i=0; i<REPETITIONS; i++ ) {
      adc = new RtAudio();      
      adc->openStream( &oParams, &iParams, RTAUDIO_SINT32, fs, &bufferFrames, &pulse, (void *)&mydata );
      mydata.frameCounter = 0;
      adc->startStream();
      std::cout << "New instance and stream started ... ";
      SLEEP( runtime );
      adc->stopStream();
      adc->closeStream();
      delete adc;
      adc = 0;
      std::cout << "stream stopped and instance deleted.\n";
      SLEEP( pausetime );
    }
  }
  catch ( RtAudioError& e ) {
    e.printMessage();
    goto cleanup;
  }

 cleanup:
  if ( adc && adc->isStreamOpen() ) adc->closeStream();
  if ( adc ) delete adc;

  return 0;
}