aboutsummaryrefslogtreecommitdiffstats
path: root/test/TestPortAudioDevice.pas
blob: 5afc02e8a5ef8ea4900695b9262a8b4812ce81b1 (plain) (blame)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
{* UltraStar Deluxe - Karaoke Game
 *
 * UltraStar Deluxe is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING. If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *}

program TestPortAudioDevice;

{* TestPortAudioDevice does some basic tests of the portaudio libs.
 * If all works, it lists all audio input and output devices and their
 * characteristics. Compile and run with simple commands.
 *}

{$IFDEF FPC}
  {$MODE Delphi}
{$ENDIF}

uses
  SysUtils,
  ctypes,
  crt,
  math,
  PortAudio in '../src/lib/portaudio/portaudio.pas';

const
  paDefaultApi = -1;

  ApiPreferenceOrder:
{$IF Defined(MSWINDOWS)}
    // Note1: Portmixer has no mixer support for paASIO and paWASAPI at the moment
    // Note2: Windows Default-API is MME, but DirectSound is faster
    array[0..0] of TPaHostApiTypeId = ( paDirectSound );
{$ELSEIF Defined(DARWIN)}
    array[0..0] of TPaHostApiTypeId = ( paDefaultApi ); // paCoreAudio
{$ELSEIF Defined(UNIX)}
    // Note: Portmixer has no mixer support for JACK at the moment
    array[0..2] of TPaHostApiTypeId = ( paALSA, paJACK, paOSS );
{$ELSE}
    array[0..0] of TPaHostApiTypeId = ( paDefaultApi );
{$IFEND}

  standardSampleRates: array[1..13] of cdouble =
    ( 8000.0,  9600.0,  11025.0,  12000.0,  16000.0,
     22050.0, 24000.0,  32000.0,  44100.0,  48000.0,
     88200.0, 96000.0, 192000.0
    );

  SampleFormat: array[1..8] of culong =
    (paFloat32, paInt32, paInt24,        paInt16,
     paInt8,    paUInt8, paCustomFormat, paNonInterleaved
    );
  SampleFormatName: array[1..8] of string =
    ('paFloat32', 'paInt32', 'paInt24',        'paInt16',
     'paInt8',    'paUInt8', 'paCustomFormat', 'paNonInterleaved'
    );

var
  i, j:        integer;
  PaError:     TPaError;
  paApiIndex:  TPaHostApiIndex;
  paApiInfo:   PPaHostApiInfo;
  deviceIndex: TPaDeviceIndex;
  deviceInfo:  PPaDeviceInfo;
  inputParameters:  PPaStreamParameters;
  outputParameters: PPaStreamParameters;
  sampleRate:       cdouble;
  stream:           PPaStream;
  framesPerBuffer:  culong;
  streamFlags:      TPaStreamFlags;
  streamCallback:   PPaStreamCallback;
  callbackStartTime: TDateTime;
  callbackWorks:    boolean;
  userData:         Pointer;


function GetPreferredApiIndex(): TPaHostApiIndex;
var
  i:        integer;
  apiIndex: TPaHostApiIndex;
  apiInfo:  PPaHostApiInfo;
begin
  result := -1;

  // select preferred sound-API
  for i:= 0 to High(ApiPreferenceOrder) do
  begin
    if (ApiPreferenceOrder[i] <> paDefaultApi) then
    begin
      // check if API is available
      apiIndex := Pa_HostApiTypeIdToHostApiIndex(ApiPreferenceOrder[i]);
      if apiIndex >= 0 then
      begin
        // we found an API but we must check if it works
        // (on linux portaudio might detect OSS but does not provide
        // any devices if ALSA is enabled)
        apiInfo := Pa_GetHostApiInfo(apiIndex);
        if apiInfo^.deviceCount > 0 then
        begin
          Result := apiIndex;
          break;
        end;
      end;
    end;
  end;

  // None of the preferred APIs is available -> use default
  if (result < 0) then
  begin
    result := Pa_GetDefaultHostApi();
  end;
end;

{
type
  TAudioSampleFormat = (
    asfU8, asfS8,         // unsigned/signed  8 bits
    asfU16LSB, asfS16LSB, // unsigned/signed 16 bits (endianness: LSB)
    asfU16MSB, asfS16MSB, // unsigned/signed 16 bits (endianness: MSB)
    asfU16, asfS16,       // unsigned/signed 16 bits (endianness: System)
    asfS32,               // signed 32 bits (endianness: System)
    asfFloat,             // float
    asfDouble             // double
  );
  TAudioFormatInfo = ;
  TAudioInputDevice = record
      AudioFormat:     TAudioFormatInfo; // capture format info (e.g. 44.1kHz SInt16 stereo)
      CaptureChannel:  array of TCaptureBuffer; // sound-buffer references used for mono or stereo channel's capture data
  end;

procedure HandleMicrophoneData(Buffer: PByteArray; Size: integer; InputDevice: TAudioInputDevice);
var
  MultiChannelBuffer:      PByteArray;  // buffer handled as array of bytes (offset relative to channel)
  SingleChannelBuffer:     PByteArray;  // temporary buffer for new samples per channel
  SingleChannelBufferSize: integer;
  ChannelIndex:            integer;
  CaptureChannel:          TCaptureBuffer;
  AudioFormat:             TAudioFormatInfo;
  SampleSize:              integer;
  SamplesPerChannel:       integer;
  i:                       integer;
begin
  AudioFormat := InputDevice.AudioFormat;
  SampleSize := AudioSampleSize[AudioFormat.Format];
  SamplesPerChannel := Size div AudioFormat.FrameSize;

  SingleChannelBufferSize := SamplesPerChannel * SampleSize;
  GetMem(SingleChannelBuffer, SingleChannelBufferSize);

  // process channels
  for ChannelIndex := 0 to High(InputDevice.CaptureChannel) do
  begin
    CaptureChannel := InputDevice.CaptureChannel[ChannelIndex];
    // check if a capture buffer was assigned, otherwise there is nothing to do
    if (CaptureChannel <> nil) then
    begin
      // set offset according to channel index
      MultiChannelBuffer := @Buffer[ChannelIndex * SampleSize];
      // separate channel-data from interleaved multi-channel (e.g. stereo) data
      for i := 0 to SamplesPerChannel-1 do
      begin
        Move(MultiChannelBuffer[i*AudioFormat.FrameSize],
             SingleChannelBuffer[i*SampleSize],
             SampleSize);
      end;
      CaptureChannel.ProcessNewBuffer(SingleChannelBuffer, SingleChannelBufferSize);
    end;
  end;

  FreeMem(SingleChannelBuffer);
end;
}

procedure TestInitTerminate();
begin
  writeln ('*** Test of Pa_Initialize and Pa_Terminate ***');
  PaError := Pa_Initialize;
  if PaError = paNoError then
    writeln ('Pa_Initialize: No error')
  else
    writeln ('Pa_Initialize: Error No ', PaError);

  PaError := Pa_Terminate; 
  if PaError = paNoError then
    writeln ('Pa_Terminate:  No error')
  else
    writeln ('Pa_Terminate:  Error No: ', PaError);
  writeln;
end;

procedure TestErrorText();
begin
  writeln ('*** Test of Pa_GetErrorText ***');
  PaError := Pa_Initialize;
  writeln ('paNoError (0): ', Pa_GetErrorText(PaError));
  writeln;
  writeln ('Code   Text');
  writeln ('------------------------------------');
  i := paNotInitialized;
  repeat
    writeln (i:6, ' ', Pa_GetErrorText(i));
    i := succ(i);
  until SameText(Pa_GetErrorText(i), 'Invalid error code') or (i = paNotInitialized + 100);
  writeln (i:6, ' ', Pa_GetErrorText(i));
  PaError := Pa_Terminate; 
  writeln;
end;

procedure TestVersion();
begin
  writeln ('*** Test of Pa_GetVersion and Pa_GetVersionText ***');
  PaError := Pa_Initialize;
  writeln ('Pa_GetVersion:     ', Pa_GetVersion);
  writeln ('Pa_GetVersionText: ', Pa_GetVersionText);
  PaError := Pa_Terminate; 
  writeln;
end;

procedure TestApiInfo();
begin
  writeln ('*** Test of GetPreferredApiIndex ***');
  PaError    := Pa_Initialize;
  paApiIndex := GetPreferredApiIndex();
  if paApiIndex = -1 then
    writeln ('GetPreferredApiIndex: No working Audio-API found.')
  else
    writeln ('GetPreferredApiIndex: working Audio-API found. No: ', paApiIndex);
  PaError := Pa_Terminate; 
  writeln;

  writeln ('*** Test of Pa_GetHostApiInfo ***');
  PaError    := Pa_Initialize;
  paApiIndex := GetPreferredApiIndex();
  paApiInfo  := Pa_GetHostApiInfo(paApiIndex);
  writeln ('Pa_GetHostApiInfo:');
  writeln ('paApiInfo.structVersion:       ', paApiInfo.structVersion);
  writeln ('paApiInfo._type:               ', paApiInfo._type);
  writeln ('paApiInfo.name:                ', paApiInfo.name);
  writeln ('paApiInfo.deviceCount:         ', paApiInfo.deviceCount);
  writeln ('paApiInfo.defaultInputDevice:  ', paApiInfo.defaultInputDevice);
  writeln ('paApiInfo.defaultOutputDevice: ', paApiInfo.defaultOutputDevice);
  PaError := Pa_Terminate; 
  writeln;

  writeln ('*** Test of Pa_HostApiDeviceIndexToDeviceIndex ***');
  PaError    := Pa_Initialize;
  paApiIndex := GetPreferredApiIndex();
  paApiInfo  := Pa_GetHostApiInfo(paApiIndex);
  for i:= 0 to paApiInfo^.deviceCount-1 do
  begin
    deviceIndex := Pa_HostApiDeviceIndexToDeviceIndex(paApiIndex, i);
    writeln ('deviceIndex[', i, ']: ', deviceIndex);
  end;
  PaError := Pa_Terminate; 
  writeln;
end;

procedure TestDeviceInfo();
begin
  writeln ('*** Test of Pa_GetDeviceCount ***');
  PaError := Pa_Initialize;
  writeln ('Pa_GetDeviceCount: ', Pa_GetDeviceCount);
  PaError := Pa_Terminate; 
  writeln;

  writeln ('*** Test of Pa_GetDefaultInputDevice ***');
  PaError := Pa_Initialize;
  writeln ('Pa_GetDefaultInputDevice: ', Pa_GetDefaultInputDevice);
  PaError := Pa_Terminate; 
  writeln;

  writeln ('*** Test of Pa_GetDefaultOutputDevice ***');
  PaError := Pa_Initialize;
  writeln ('Pa_GetDefaultOutputDevice: ', Pa_GetDefaultOutputDevice);
  PaError := Pa_Terminate; 
  writeln;

  writeln ('*** Test of Pa_GetDeviceInfo ***');
// Note: the fields of deviceInfo can also be used without the '^'.
// deviceInfo.name works as well as deviceInfo^.name
  PaError    := Pa_Initialize;
  paApiIndex := GetPreferredApiIndex();
  paApiInfo  := Pa_GetHostApiInfo(paApiIndex);
  for i:= 0 to paApiInfo^.deviceCount - 1 do
  begin
    deviceIndex := Pa_HostApiDeviceIndexToDeviceIndex(paApiIndex, i);
    deviceInfo  := Pa_GetDeviceInfo(deviceIndex);
    writeln ('deviceInfo[', i, '].name:                     ', deviceInfo^.name);
    writeln ('deviceInfo[', i, '].structVersion:            ', deviceInfo^.structVersion, ' (should be 2)');
    writeln ('deviceInfo[', i, '].hostApi:                  ', deviceInfo^.hostApi);
    writeln ('deviceInfo[', i, '].maxInputChannels:         ', deviceInfo^.maxInputChannels);
    writeln ('deviceInfo[', i, '].maxOutputChannels:        ', deviceInfo^.maxOutputChannels);
    writeln ('deviceInfo[', i, '].defaultLowInputLatency:   ', deviceInfo^.defaultLowInputLatency:6:4);
    writeln ('deviceInfo[', i, '].defaultLowOutputLatency:  ', deviceInfo^.defaultLowOutputLatency:6:4);
    writeln ('deviceInfo[', i, '].defaultHighInputLatency:  ', deviceInfo^.defaultHighInputLatency:6:4);
    writeln ('deviceInfo[', i, '].defaultHighOutputLatency: ', deviceInfo^.defaultHighOutputLatency:6:4);
    writeln ('deviceInfo[', i, '].defaultSampleRate:        ', deviceInfo^.defaultSampleRate:5:0);
    writeln;
  end;
  PaError := Pa_Terminate; 
end;

procedure TestFormatInfo();
begin
  writeln ('*** Test of Pa_IsFormatSupported ***');
  PaError    := Pa_Initialize;
  paApiIndex := GetPreferredApiIndex();
  paApiInfo  := Pa_GetHostApiInfo(paApiIndex);
  for i:= 0 to paApiInfo^.deviceCount - 1 do
  begin
    deviceIndex := Pa_HostApiDeviceIndexToDeviceIndex(paApiIndex, i);
    deviceInfo  := Pa_GetDeviceInfo(deviceIndex);
    writeln ('Device[', i, '] ', deviceInfo^.name, ':');
    New(inputParameters);
    New(outputParameters);

    if deviceInfo^.maxInputChannels > 0 then
    begin
      inputParameters^.device                    := deviceIndex;
      inputParameters^.channelCount              := deviceInfo^.maxInputChannels;
      inputParameters^.sampleFormat              := paInt16;
      inputParameters^.suggestedLatency          := 0;
      inputParameters^.hostApiSpecificStreamInfo := nil;
      outputParameters := nil;
    end
    else
    begin
      inputParameters := nil;
      outputParameters^.device                    := deviceIndex;
      outputParameters^.channelCount              := deviceInfo^.maxOutputChannels;
      outputParameters^.sampleFormat              := paInt16;
      outputParameters^.suggestedLatency          := 0;
      outputParameters^.hostApiSpecificStreamInfo := nil;
    end;

    sampleRate := deviceInfo^.defaultSampleRate;
    PaError    := Pa_IsFormatSupported(inputParameters, outputParameters, sampleRate);
    if PaError = paFormatIsSupported then
      writeln ('Sample rate: ', sampleRate:5:0, ' : supported')
    else
      writeln ('Sample rate: ', sampleRate:5:0, ' : Error: ', Pa_GetErrorText(PaError));

    for j := low(standardSampleRates) to high(standardSampleRates) do
    begin 
      sampleRate := standardSampleRates[j];
      PaError    := Pa_IsFormatSupported(inputParameters, outputParameters, sampleRate);
      if PaError = paFormatIsSupported then
        writeln ('Sample rate: ', sampleRate:5:0, ' : supported')
      else
        writeln ('Sample rate: ', sampleRate:5:0, ' : Error: ', PaError);
    end;

    writeln;
    for j := low(SampleFormat) to high(SampleFormat) do
    begin 
      if inputParameters <> nil then
        inputParameters^.sampleFormat := SampleFormat[j]
      else
        outputParameters^.sampleFormat := SampleFormat[j];
      PaError := Pa_IsFormatSupported(inputParameters, outputParameters, sampleRate);
      if PaError = paFormatIsSupported then
        writeln ('Sample Format ', SampleFormatName[j], ': supported')
      else
        writeln ('Sample Format ', SampleFormatName[j], ': ', Pa_GetErrorText(PaError));
    end;

    Dispose(inputParameters);
    Dispose(outputParameters);
    writeln;
  end;
  PaError := Pa_Terminate; 
end;

function AudioCallback(input: pointer; output: pointer; frameCount: culong;
      timeInfo: PPaStreamCallbackTimeInfo; statusFlags: TPaStreamCallbackFlags;
      inputDevice: pointer): cint; cdecl;
var
  duration: real;
begin
  duration := (Now() - callbackStartTime) * 24 * 3600;
  if duration < 2.0 then
    result := paContinue
  else
  begin
    callbackWorks := true;
    result := paComplete;
  end;
end;

procedure TestStreams();
begin
  writeln ('*** Test of Pa_OpenStream and Pa_CloseStream ***');
  PaError    := Pa_Initialize;
  paApiIndex := GetPreferredApiIndex();
  paApiInfo  := Pa_GetHostApiInfo(paApiIndex);
  for i:= 0 to paApiInfo^.deviceCount - 1 do
  begin
    deviceIndex := Pa_HostApiDeviceIndexToDeviceIndex(paApiIndex, i);
    deviceInfo  := Pa_GetDeviceInfo(deviceIndex);
    writeln ('Device[', i, '] ', deviceInfo^.name, ':');
    New(inputParameters);
    New(outputParameters);
    if deviceInfo^.maxInputChannels > 0 then
    begin
      inputParameters^.device                    := deviceIndex;
      inputParameters^.channelCount              := deviceInfo^.maxInputChannels;
      inputParameters^.sampleFormat              := paInt16;
      inputParameters^.suggestedLatency          := deviceInfo.defaultHighInputLatency;
      inputParameters^.hostApiSpecificStreamInfo := nil;
      outputParameters := nil;
    end
    else
    begin
      inputParameters := nil;
      outputParameters^.device                    := deviceIndex;
      outputParameters^.channelCount              := deviceInfo^.maxOutputChannels;
      outputParameters^.sampleFormat              := paInt16;
      outputParameters^.suggestedLatency          := deviceInfo.defaultLowOutputLatency;
      outputParameters^.hostApiSpecificStreamInfo := nil;
    end;
      
    sampleRate      := deviceInfo^.defaultSampleRate;
    framesPerBuffer := paFramesPerBufferUnspecified;
    streamFlags     := paNoFlag;
    streamCallback  := @AudioCallback;
    userData        := nil;

    PaError := Pa_OpenStream(
                     stream,
                     inputParameters,
                     outputParameters,
                     sampleRate,
                     framesPerBuffer,
                     streamFlags,
                     streamCallback,
                     userData 
         );
    if (PaError = paNoError) and (stream <> nil) then
      writeln ('Pa_OpenStream: success')
    else
      writeln ('Pa_OpenStream: ', Pa_GetErrorText(PaError));

    if (PaError = paNoError) and (stream <> nil) then
    begin
      callbackStartTime := Now();

      PaError := Pa_StartStream(stream);
      if PaError = paNoError then
        writeln ('Pa_StartStream: success')
      else
        writeln ('Pa_StartStream: ', Pa_GetErrorText(PaError));

      callbackWorks := false;

      // wait twice the time a successful callback would need for termination
      writeln('Wait for callback');
      delay(4000);

      if callbackWorks and (Pa_IsStreamStopped(stream) = 0) then
      begin
        writeln ('Success: Device works');
        PaError := Pa_StopStream(stream);
        if (PaError = paNoError) then
          writeln ('Pa_StopStream: success')
        else
          writeln ('Pa_StopStream: ', Pa_GetErrorText(PaError));
      end
      else
      begin
        writeln ('Error: Non working device');
        PaError := Pa_AbortStream(stream);
        if (PaError = paNoError) then
          writeln ('Pa_AbortStream: success')
        else
          writeln ('Pa_AbortStream: ', Pa_GetErrorText(PaError));

      end;
    end;

    PaError := Pa_CloseStream(stream);
    if PaError = paNoError then
      writeln ('Pa_CloseStream: success')
    else
      writeln ('Pa_CloseStream: ', Pa_GetErrorText(PaError));

    Dispose(inputParameters);
    Dispose(outputParameters);
    
    writeln;    
  end;
  PaError := Pa_Terminate; 
end;

begin
  // floating point exceptions are raised. Therefore, set the exception mask.
  SetExceptionMask([exZeroDivide, exPrecision]);

  writeln ('Start: Test of Portaudio libs');
  writeln;

  TestInitTerminate();
  TestErrorText(); 
  TestVersion();
  TestApiInfo();
  TestDeviceInfo();
  TestFormatInfo();
  TestStreams();
  
  writeln ('End: Test of Portaudio libs');
end.