39 package com.portaudio;
41 import junit.framework.TestCase;
52 public void testDeviceCount()
63 public void testListDevices()
67 assertTrue(
"getDeviceCount", (count > 0) );
68 for(
int i = 0; i < count; i++ )
71 System.out.println(
"------------------ #" + i );
72 System.out.println(
" name = " + info.name );
73 System.out.println(
" hostApi = " + info.hostApi );
74 System.out.println(
" maxOutputChannels = "
75 + info.maxOutputChannels );
76 System.out.println(
" maxInputChannels = "
77 + info.maxInputChannels );
78 System.out.println(
" defaultSampleRate = "
79 + info.defaultSampleRate );
80 System.out.printf(
" defaultLowInputLatency = %3d msec\n",
81 ((
int) (info.defaultLowInputLatency * 1000)) );
82 System.out.printf(
" defaultHighInputLatency = %3d msec\n",
83 ((
int) (info.defaultHighInputLatency * 1000)) );
84 System.out.printf(
" defaultLowOutputLatency = %3d msec\n",
85 ((
int) (info.defaultLowOutputLatency * 1000)) );
86 System.out.printf(
" defaultHighOutputLatency = %3d msec\n",
87 ((
int) (info.defaultHighOutputLatency * 1000)) );
89 assertTrue(
"some channels",
90 (info.maxOutputChannels + info.maxInputChannels) > 0 );
91 assertTrue(
"not too many channels", (info.maxInputChannels < 64) );
92 assertTrue(
"not too many channels", (info.maxOutputChannels < 64) );
95 System.out.println(
"defaultInput = "
97 System.out.println(
"defaultOutput = "
103 public void testHostApis()
106 int validApiCount = 0;
107 for(
int hostApiType = 0; hostApiType <
PortAudio.HOST_API_TYPE_COUNT; hostApiType++ )
111 if( hostApiIndex >= 0 )
114 System.out.println(
"Checking Host API: " + info.name );
115 for(
int apiDeviceIndex = 0; apiDeviceIndex < info.deviceCount; apiDeviceIndex++ )
121 .getDeviceInfo( deviceIndex );
122 assertEquals(
"host api must match up", hostApiIndex,
123 deviceInfo.hostApi );
133 public void testListHostApis()
137 assertTrue(
"getHostApiCount", (count > 0) );
138 for(
int i = 0; i < count; i++ )
141 System.out.println(
"------------------ #" + i );
142 System.out.println(
" version = " + info.version );
143 System.out.println(
" name = " + info.name );
144 System.out.println(
" type = " + info.type );
145 System.out.println(
" deviceCount = " + info.deviceCount );
146 System.out.println(
" defaultInputDevice = "
147 + info.defaultInputDevice );
148 System.out.println(
" defaultOutputDevice = "
149 + info.defaultOutputDevice );
150 assertTrue(
"some devices", info.deviceCount > 0 );
153 System.out.println(
"------\ndefaultHostApi = "
158 public void testCheckFormat()
162 streamParameters.device =
PortAudio.getDefaultOutputDevice();
165 System.out.println(
"isFormatSupported returns " + result );
166 assertEquals(
"default output format", 0, result );
168 streamParameters.channelCount = 8765;
170 System.out.println(
"crazy isFormatSupported returns " + result );
171 assertTrue(
"default output format", (result < 0) );
175 static class SineOscillator
178 double phaseIncrement = 0.01;
180 SineOscillator(
double freq,
int sampleRate)
182 phaseIncrement = freq * Math.PI * 2.0 / sampleRate;
187 double value = Math.sin( phase );
188 phase += phaseIncrement;
189 if( phase > Math.PI )
191 phase -= Math.PI * 2.0;
197 public void testStreamError()
202 streamParameters.channelCount = 2;
203 streamParameters.device =
PortAudio.getDefaultOutputDevice();
204 int framesPerBuffer = 256;
207 44100, framesPerBuffer, flags );
210 Throwable caught = null;
213 float[] buffer =
new float[framesPerBuffer
214 * streamParameters.channelCount];
215 stream.
write( buffer, framesPerBuffer );
216 }
catch( Throwable e )
222 assertTrue(
"caught no expection", (caught != null) );
223 assertTrue(
"exception should say stream is stopped", caught
224 .getMessage().contains(
"stopped" ) );
230 stream.
write( (
float[]) null, framesPerBuffer );
231 }
catch( Throwable e )
236 assertTrue(
"caught no expection", (caught != null) );
237 assertTrue(
"exception should say stream is stopped", caught
238 .getMessage().contains(
"null" ) );
245 short[] buffer =
new short[framesPerBuffer
246 * streamParameters.channelCount];
247 stream.
write( buffer, framesPerBuffer );
248 }
catch( Throwable e )
254 assertTrue(
"caught no expection", (caught != null) );
255 assertTrue(
"exception should say tried to", caught.getMessage()
256 .contains(
"Tried to write short" ) );
263 public void checkBlockingWriteFloat(
int deviceId,
double sampleRate )
266 streamParameters.channelCount = 2;
267 streamParameters.device = deviceId;
268 streamParameters.suggestedLatency =
PortAudio
269 .getDeviceInfo( streamParameters.device ).defaultLowOutputLatency;
270 System.out.println(
"suggestedLatency = "
271 + streamParameters.suggestedLatency );
273 int framesPerBuffer = 256;
276 (
int) sampleRate, framesPerBuffer, flags );
277 assertTrue(
"got default stream", stream != null );
279 assertEquals(
"stream isStopped",
true, stream.isStopped() );
280 assertEquals(
"stream isActive",
false, stream.isActive() );
282 int numFrames = 80000;
283 double expected = ((double)numFrames) / sampleRate;
285 long startTime = System.currentTimeMillis();
286 double startStreamTime = stream.
getTime();
287 assertEquals(
"stream isStopped",
false, stream.isStopped() );
288 assertEquals(
"stream isActive",
true, stream.isActive() );
290 writeSineData( stream, framesPerBuffer, numFrames, (
int) sampleRate );
293 System.out.println(
"inputLatency of a stream = "+ streamInfo.inputLatency );
294 System.out.println(
"outputLatency of a stream = "+streamInfo.outputLatency );
295 System.out.println(
"sampleRate of a stream = "+ streamInfo.sampleRate );
297 assertEquals(
"inputLatency of a stream ", 0.0, streamInfo.inputLatency, 0.000001 );
298 assertTrue(
"outputLatency of a stream ",(streamInfo.outputLatency > 0) );
299 assertEquals(
"sampleRate of a stream ", sampleRate, streamInfo.sampleRate, 3 );
301 double endStreamTime = stream.
getTime();
303 long stopTime = System.currentTimeMillis();
305 System.out.println(
"startStreamTime = " + startStreamTime );
306 System.out.println(
"endStreamTime = " + endStreamTime );
307 double elapsedStreamTime = endStreamTime - startStreamTime;
308 System.out.println(
"elapsedStreamTime = " + elapsedStreamTime );
309 assertTrue(
"elapsedStreamTime: " + elapsedStreamTime,
310 (elapsedStreamTime > 0.0) );
311 assertEquals(
"elapsedStreamTime: ", expected, elapsedStreamTime, 0.10 );
313 assertEquals(
"stream isStopped",
true, stream.isStopped() );
314 assertEquals(
"stream isActive",
false, stream.isActive() );
317 double elapsed = (stopTime - startTime) / 1000.0;
318 assertEquals(
"elapsed time to play", expected, elapsed, 0.20 );
321 public void testBlockingWriteFloat()
324 checkBlockingWriteFloat(
PortAudio.getDefaultOutputDevice(), 44100 );
328 public void ZtestWriteEachHostAPI()
334 System.out.println(
"-------------\nWriting using Host API: " + hostInfo.name );
335 int deviceId = hostInfo.defaultOutputDevice;
336 System.out.println(
" Device ID =" + deviceId );
338 System.out.println(
" sampleRate =" + deviceInfo.defaultSampleRate );
339 checkBlockingWriteFloat( deviceId,
340 (
int) deviceInfo.defaultSampleRate );
341 System.out.println(
"Finished with " + hostInfo.name );
346 private void writeSineData(
BlockingStream stream,
int framesPerBuffer,
347 int numFrames,
int sampleRate )
349 float[] buffer =
new float[framesPerBuffer * 2];
350 SineOscillator osc1 =
new SineOscillator( 200.0, sampleRate );
351 SineOscillator osc2 =
new SineOscillator( 300.0, sampleRate );
352 int framesLeft = numFrames;
353 while( framesLeft > 0 )
356 int framesToWrite = (framesLeft > framesPerBuffer) ? framesPerBuffer
358 for(
int j = 0; j < framesToWrite; j++ )
360 buffer[index++] = (float) osc1.next();
361 buffer[index++] = (float) osc2.next();
363 stream.
write( buffer, framesToWrite );
364 framesLeft -= framesToWrite;
369 int framesPerBuffer,
int numFrames )
371 short[] buffer =
new short[framesPerBuffer * 2];
372 SineOscillator osc1 =
new SineOscillator( 200.0, 44100 );
373 SineOscillator osc2 =
new SineOscillator( 300.0, 44100 );
374 int framesLeft = numFrames;
375 while( framesLeft > 0 )
378 int framesToWrite = (framesLeft > framesPerBuffer) ? framesPerBuffer
380 for(
int j = 0; j < framesToWrite; j++ )
382 buffer[index++] = (short) (osc1.next() * 32767);
383 buffer[index++] = (short) (osc2.next() * 32767);
385 stream.
write( buffer, framesToWrite );
386 framesLeft -= framesToWrite;
390 public void testBlockingWriteShort()
395 streamParameters.sampleFormat =
PortAudio.FORMAT_INT_16;
396 streamParameters.channelCount = 2;
397 streamParameters.device =
PortAudio.getDefaultOutputDevice();
398 streamParameters.suggestedLatency =
PortAudio
399 .getDeviceInfo( streamParameters.device ).defaultLowOutputLatency;
400 System.out.println(
"suggestedLatency = "
401 + streamParameters.suggestedLatency );
403 int framesPerBuffer = 256;
406 44100, framesPerBuffer, flags );
407 assertTrue(
"got default stream", stream != null );
409 int numFrames = 80000;
411 long startTime = System.currentTimeMillis();
412 writeSineDataShort( stream, framesPerBuffer, numFrames );
414 long stopTime = System.currentTimeMillis();
417 double elapsed = (stopTime - startTime) / 1000.0;
418 double expected = numFrames / 44100.0;
419 assertEquals(
"elapsed time to play", expected, elapsed, 0.20 );
423 public void testRecordPlayFloat()
throws InterruptedException
428 public void testRecordPlayShort()
throws InterruptedException
430 checkRecordPlay(
PortAudio.FORMAT_INT_16 );
433 public void checkRecordPlay(
int sampleFormat )
throws InterruptedException
435 int framesPerBuffer = 256;
437 int sampleRate = 44100;
438 int numFrames = sampleRate * 3;
439 float[] floatBuffer = null;
440 short[] shortBuffer = null;
444 inParameters.sampleFormat = sampleFormat;
445 inParameters.device =
PortAudio.getDefaultInputDevice();
448 inParameters.channelCount = (info.maxInputChannels > 2) ? 2
449 : info.maxInputChannels;
450 System.out.println(
"channelCount = " + inParameters.channelCount );
451 inParameters.suggestedLatency =
PortAudio
452 .getDeviceInfo( inParameters.device ).defaultLowInputLatency;
456 floatBuffer =
new float[numFrames * inParameters.channelCount];
458 else if( sampleFormat ==
PortAudio.FORMAT_INT_16 )
460 shortBuffer =
new short[numFrames * inParameters.channelCount];
464 sampleRate, framesPerBuffer, flags );
466 System.out.println(
"RECORDING - say something like testing 1,2,3..." );
471 inStream.
read( floatBuffer, numFrames );
473 else if( sampleFormat ==
PortAudio.FORMAT_INT_16 )
475 inStream.
read( shortBuffer, numFrames );
479 System.out.println(
"availableToRead = " + availableToRead );
480 assertTrue(
"getReadAvailable ", availableToRead > 0 );
484 System.out.println(
"Finished recording. Begin Playback." );
488 outParameters.sampleFormat = sampleFormat;
489 outParameters.channelCount = inParameters.channelCount;
490 outParameters.device =
PortAudio.getDefaultOutputDevice();
491 outParameters.suggestedLatency =
PortAudio
492 .getDeviceInfo( outParameters.device ).defaultLowOutputLatency;
495 sampleRate, framesPerBuffer, flags );
496 assertTrue(
"got default stream", outStream != null );
498 assertEquals(
"inStream isActive",
false, inStream.isActive() );
503 System.out.println(
"availableToWrite = " + availableToWrite );
504 assertTrue(
"getWriteAvailable ", availableToWrite > 0 );
506 System.out.println(
"inStream = " + inStream );
507 System.out.println(
"outStream = " + outStream );
508 assertEquals(
"inStream isActive",
false, inStream.isActive() );
509 assertEquals(
"outStream isActive",
true, outStream.isActive() );
512 outStream.
write( floatBuffer, numFrames );
514 else if( sampleFormat ==
PortAudio.FORMAT_INT_16 )
516 outStream.
write( shortBuffer, numFrames );