-
Notifications
You must be signed in to change notification settings - Fork 317
/
pa_asio.cpp
4243 lines (3500 loc) · 159 KB
/
pa_asio.cpp
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $Id$
* Portable Audio I/O Library for ASIO Drivers
*
* Author: Stephane Letz
* Based on the Open Source API proposed by Ross Bencina
* Copyright (c) 2000-2002 Stephane Letz, Phil Burk, Ross Bencina
* Blocking i/o implementation by Sven Fischer, Institute of Hearing
* Technology and Audiology (www.hoertechnik-audiologie.de)
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* The text above constitutes the entire PortAudio license; however,
* the PortAudio community also makes the following non-binding requests:
*
* Any person wishing to distribute modifications to the Software is
* requested to send the modifications to the original developer so that
* they can be incorporated into the canonical version. It is also
* requested that these non-binding requests be included along with the
* license above.
*/
/* Modification History
08-03-01 First version : Stephane Letz
08-06-01 Tweaks for PC, use C++, buffer allocation, Float32 to Int32 conversion : Phil Burk
08-20-01 More conversion, PA_StreamTime, Pa_GetHostError : Stephane Letz
08-21-01 PaUInt8 bug correction, implementation of ASIOSTFloat32LSB and ASIOSTFloat32MSB native formats : Stephane Letz
08-24-01 MAX_INT32_FP hack, another Uint8 fix : Stephane and Phil
08-27-01 Implementation of hostBufferSize < userBufferSize case, better management of the output buffer when
the stream is stopped : Stephane Letz
08-28-01 Check the stream pointer for null in bufferSwitchTimeInfo, correct bug in bufferSwitchTimeInfo when
the stream is stopped : Stephane Letz
10-12-01 Correct the PaHost_CalcNumHostBuffers function: computes FramesPerHostBuffer to be the lowest that
respect requested FramesPerUserBuffer and userBuffersPerHostBuffer : Stephane Letz
10-26-01 Management of hostBufferSize and userBufferSize of any size : Stephane Letz
10-27-01 Improve calculus of hostBufferSize to be multiple or divisor of userBufferSize if possible : Stephane and Phil
10-29-01 Change MAX_INT32_FP to (2147483520.0f) to prevent roundup to 0x80000000 : Phil Burk
10-31-01 Clear the output buffer and user buffers in PaHost_StartOutput, correct bug in GetFirstMultiple : Stephane Letz
11-06-01 Rename functions : Stephane Letz
11-08-01 New Pa_ASIO_Adaptor_Init function to init Callback adpatation variables, cleanup of Pa_ASIO_Callback_Input: Stephane Letz
11-29-01 Break apart device loading to debug random failure in Pa_ASIO_QueryDeviceInfo ; Phil Burk
01-03-02 Deallocate all resources in PaHost_Term for cases where Pa_CloseStream is not called properly : Stephane Letz
02-01-02 Cleanup, test of multiple-stream opening : Stephane Letz
19-02-02 New Pa_ASIO_loadDriver that calls CoInitialize on each thread on Windows : Stephane Letz
09-04-02 Correct error code management in PaHost_Term, removes various compiler warning : Stephane Letz
12-04-02 Add Mac includes for <Devices.h> and <Timer.h> : Phil Burk
13-04-02 Removes another compiler warning : Stephane Letz
30-04-02 Pa_ASIO_QueryDeviceInfo bug correction, memory allocation checking, better error handling : D Viens, P Burk, S Letz
12-06-02 Rehashed into new multi-api infrastructure, added support for all ASIO sample formats : Ross Bencina
18-06-02 Added pa_asio.h, PaAsio_GetAvailableLatencyValues() : Ross B.
21-06-02 Added SelectHostBufferSize() which selects host buffer size based on user latency parameters : Ross Bencina
** NOTE maintenance history is now stored in CVS **
*/
/** @file
@ingroup hostapi_src
Note that specific support for paInputUnderflow, paOutputOverflow and
paNeverDropInput is not necessary or possible with this driver due to the
synchronous full duplex double-buffered architecture of ASIO.
*/
#include <stdio.h>
#include <assert.h>
#include <string.h>
//#include <values.h>
#include <new>
#include <windows.h>
#include <mmsystem.h>
#include "portaudio.h"
#include "pa_asio.h"
#include "pa_util.h"
#include "pa_allocation.h"
#include "pa_hostapi.h"
#include "pa_stream.h"
#include "pa_cpuload.h"
#include "pa_process.h"
#include "pa_debugprint.h"
#include "pa_ringbuffer.h"
#include "pa_win_coinitialize.h"
#include "pa_win_util.h"
/* This version of pa_asio.cpp is currently only targeted at Win32,
It would require a few tweaks to work with pre-OS X Macintosh.
To make configuration easier, we define WIN32 here to make sure
that the ASIO SDK knows this is Win32.
*/
#ifndef WIN32
#define WIN32
#endif
#include "asiosys.h"
#include "asio.h"
#include "asiodrivers.h"
#include "iasiothiscallresolver.h"
/*
#if MAC
#include <Devices.h>
#include <Timer.h>
#include <Math64.h>
#else
*/
/*
#include <math.h>
#include <windows.h>
#include <mmsystem.h>
*/
/*
#endif
*/
/* winmm.lib is needed for timeGetTime() (this is in winmm.a if you're using gcc) */
#if (defined(WIN32) && (defined(_MSC_VER) && (_MSC_VER >= 1200))) /* MSC version 6 and above */
#pragma comment(lib, "winmm.lib")
#endif
/* external reference to ASIO SDK's asioDrivers.
This is a bit messy because we want to explicitly manage
allocation/deallocation of this structure, but some layers of the SDK
which we currently use (eg the implementation in asio.cpp) still
use this global version.
For now we keep it in sync with our local instance in the host
API representation structure, but later we should be able to remove
all dependence on it.
*/
extern AsioDrivers* asioDrivers;
/* We are trying to be compatible with CARBON but this has not been thoroughly tested. */
/* not tested at all since new V19 code was introduced. */
#define CARBON_COMPATIBLE (0)
/* prototypes for functions declared in this file */
extern "C" PaError PaAsio_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiIndex hostApiIndex );
static void Terminate( struct PaUtilHostApiRepresentation *hostApi );
static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi,
PaStream** s,
const PaStreamParameters *inputParameters,
const PaStreamParameters *outputParameters,
double sampleRate,
unsigned long framesPerBuffer,
PaStreamFlags streamFlags,
PaStreamCallback *streamCallback,
void *userData );
static PaError IsFormatSupported( struct PaUtilHostApiRepresentation *hostApi,
const PaStreamParameters *inputParameters,
const PaStreamParameters *outputParameters,
double sampleRate );
static PaError CloseStream( PaStream* stream );
static PaError StartStream( PaStream *stream );
static PaError StopStream( PaStream *stream );
static PaError AbortStream( PaStream *stream );
static PaError IsStreamStopped( PaStream *s );
static PaError IsStreamActive( PaStream *stream );
static PaTime GetStreamTime( PaStream *stream );
static double GetStreamCpuLoad( PaStream* stream );
static PaError ReadStream( PaStream* stream, void *buffer, unsigned long frames );
static PaError WriteStream( PaStream* stream, const void *buffer, unsigned long frames );
static signed long GetStreamReadAvailable( PaStream* stream );
static signed long GetStreamWriteAvailable( PaStream* stream );
/* Blocking i/o callback function. */
static int BlockingIoPaCallback(const void *inputBuffer ,
void *outputBuffer ,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo *timeInfo ,
PaStreamCallbackFlags statusFlags ,
void *userData );
/* our ASIO callback functions */
static void bufferSwitch(long index, ASIOBool processNow);
static ASIOTime *bufferSwitchTimeInfo(ASIOTime *timeInfo, long index, ASIOBool processNow);
static void sampleRateChanged(ASIOSampleRate sRate);
static long asioMessages(long selector, long value, void* message, double* opt);
static ASIOCallbacks asioCallbacks_ =
{ bufferSwitch, sampleRateChanged, asioMessages, bufferSwitchTimeInfo };
#define PA_ASIO_SET_LAST_HOST_ERROR( errorCode, errorText ) \
PaUtil_SetLastHostErrorInfo( paASIO, errorCode, errorText )
static void PaAsio_SetLastSystemError( DWORD errorCode )
{
PaWinUtil_SetLastSystemErrorInfo( paASIO, errorCode );
}
#define PA_ASIO_SET_LAST_SYSTEM_ERROR( errorCode ) \
PaAsio_SetLastSystemError( errorCode )
static const char* PaAsio_GetAsioErrorText( ASIOError asioError )
{
const char *result;
switch( asioError ){
case ASE_OK:
case ASE_SUCCESS: result = "Success"; break;
case ASE_NotPresent: result = "Hardware input or output is not present or available"; break;
case ASE_HWMalfunction: result = "Hardware is malfunctioning"; break;
case ASE_InvalidParameter: result = "Input parameter invalid"; break;
case ASE_InvalidMode: result = "Hardware is in a bad mode or used in a bad mode"; break;
case ASE_SPNotAdvancing: result = "Hardware is not running when sample position is inquired"; break;
case ASE_NoClock: result = "Sample clock or rate cannot be determined or is not present"; break;
case ASE_NoMemory: result = "Not enough memory for completing the request"; break;
default: result = "Unknown ASIO error"; break;
}
return result;
}
#define PA_ASIO_SET_LAST_ASIO_ERROR( asioError ) \
PaUtil_SetLastHostErrorInfo( paASIO, asioError, PaAsio_GetAsioErrorText( asioError ) )
// Atomic increment and decrement operations
#if MAC
/* need to be implemented on Mac */
inline long PaAsio_AtomicIncrement(volatile long* v) {return ++(*const_cast<long*>(v));}
inline long PaAsio_AtomicDecrement(volatile long* v) {return --(*const_cast<long*>(v));}
#elif WINDOWS
inline long PaAsio_AtomicIncrement(volatile long* v) {return InterlockedIncrement(const_cast<long*>(v));}
inline long PaAsio_AtomicDecrement(volatile long* v) {return InterlockedDecrement(const_cast<long*>(v));}
#endif
typedef struct PaAsioDriverInfo
{
ASIODriverInfo asioDriverInfo;
long inputChannelCount, outputChannelCount;
long bufferMinSize, bufferMaxSize, bufferPreferredSize, bufferGranularity;
bool postOutput;
}
PaAsioDriverInfo;
/* PaAsioHostApiRepresentation - host api datastructure specific to this implementation */
typedef struct
{
PaUtilHostApiRepresentation inheritedHostApiRep;
PaUtilStreamInterface callbackStreamInterface;
PaUtilStreamInterface blockingStreamInterface;
PaUtilAllocationGroup *allocations;
PaWinUtilComInitializationResult comInitializationResult;
AsioDrivers *asioDrivers;
void *systemSpecific;
/* the ASIO C API only allows one ASIO driver to be open at a time,
so we keep track of whether we have the driver open here, and
use this information to return errors from OpenStream if the
driver is already open.
openAsioDeviceIndex will be PaNoDevice if there is no device open
and a valid pa_asio (not global) device index otherwise.
openAsioDriverInfo is populated with the driver info for the
currently open device (if any)
*/
PaDeviceIndex openAsioDeviceIndex;
PaAsioDriverInfo openAsioDriverInfo;
}
PaAsioHostApiRepresentation;
/*
Retrieve <driverCount> driver names from ASIO, returned in a char**
allocated in <group>.
*/
static char **GetAsioDriverNames( PaAsioHostApiRepresentation *asioHostApi, PaUtilAllocationGroup *group, long driverCount )
{
char **result = 0;
int i;
result =(char**)PaUtil_GroupAllocateZeroInitializedMemory(
group, sizeof(char*) * driverCount );
if( !result )
goto error;
result[0] = (char*)PaUtil_GroupAllocateZeroInitializedMemory(
group, 32 * driverCount );
if( !result[0] )
goto error;
for( i=0; i<driverCount; ++i )
result[i] = result[0] + (32 * i);
asioHostApi->asioDrivers->getDriverNames( result, driverCount );
error:
return result;
}
static PaSampleFormat AsioSampleTypeToPaNativeSampleFormat(ASIOSampleType type)
{
switch (type) {
case ASIOSTInt16MSB:
case ASIOSTInt16LSB:
return paInt16;
case ASIOSTFloat32MSB:
case ASIOSTFloat32LSB:
case ASIOSTFloat64MSB:
case ASIOSTFloat64LSB:
return paFloat32;
case ASIOSTInt32MSB:
case ASIOSTInt32LSB:
case ASIOSTInt32MSB16:
case ASIOSTInt32LSB16:
case ASIOSTInt32MSB18:
case ASIOSTInt32MSB20:
case ASIOSTInt32MSB24:
case ASIOSTInt32LSB18:
case ASIOSTInt32LSB20:
case ASIOSTInt32LSB24:
return paInt32;
case ASIOSTInt24MSB:
case ASIOSTInt24LSB:
return paInt24;
default:
return paCustomFormat;
}
}
void AsioSampleTypeLOG(ASIOSampleType type)
{
switch (type) {
case ASIOSTInt16MSB: PA_DEBUG(("ASIOSTInt16MSB\n")); break;
case ASIOSTInt16LSB: PA_DEBUG(("ASIOSTInt16LSB\n")); break;
case ASIOSTFloat32MSB:PA_DEBUG(("ASIOSTFloat32MSB\n"));break;
case ASIOSTFloat32LSB:PA_DEBUG(("ASIOSTFloat32LSB\n"));break;
case ASIOSTFloat64MSB:PA_DEBUG(("ASIOSTFloat64MSB\n"));break;
case ASIOSTFloat64LSB:PA_DEBUG(("ASIOSTFloat64LSB\n"));break;
case ASIOSTInt32MSB: PA_DEBUG(("ASIOSTInt32MSB\n")); break;
case ASIOSTInt32LSB: PA_DEBUG(("ASIOSTInt32LSB\n")); break;
case ASIOSTInt32MSB16:PA_DEBUG(("ASIOSTInt32MSB16\n"));break;
case ASIOSTInt32LSB16:PA_DEBUG(("ASIOSTInt32LSB16\n"));break;
case ASIOSTInt32MSB18:PA_DEBUG(("ASIOSTInt32MSB18\n"));break;
case ASIOSTInt32MSB20:PA_DEBUG(("ASIOSTInt32MSB20\n"));break;
case ASIOSTInt32MSB24:PA_DEBUG(("ASIOSTInt32MSB24\n"));break;
case ASIOSTInt32LSB18:PA_DEBUG(("ASIOSTInt32LSB18\n"));break;
case ASIOSTInt32LSB20:PA_DEBUG(("ASIOSTInt32LSB20\n"));break;
case ASIOSTInt32LSB24:PA_DEBUG(("ASIOSTInt32LSB24\n"));break;
case ASIOSTInt24MSB: PA_DEBUG(("ASIOSTInt24MSB\n")); break;
case ASIOSTInt24LSB: PA_DEBUG(("ASIOSTInt24LSB\n")); break;
default: PA_DEBUG(("Custom Format%d\n",type));break;
}
}
static int BytesPerAsioSample( ASIOSampleType sampleType )
{
switch (sampleType) {
case ASIOSTInt16MSB:
case ASIOSTInt16LSB:
return 2;
case ASIOSTFloat64MSB:
case ASIOSTFloat64LSB:
return 8;
case ASIOSTFloat32MSB:
case ASIOSTFloat32LSB:
case ASIOSTInt32MSB:
case ASIOSTInt32LSB:
case ASIOSTInt32MSB16:
case ASIOSTInt32LSB16:
case ASIOSTInt32MSB18:
case ASIOSTInt32MSB20:
case ASIOSTInt32MSB24:
case ASIOSTInt32LSB18:
case ASIOSTInt32LSB20:
case ASIOSTInt32LSB24:
return 4;
case ASIOSTInt24MSB:
case ASIOSTInt24LSB:
return 3;
default:
return 0;
}
}
static void Swap16( void *buffer, long shift, long count )
{
unsigned short *p = (unsigned short*)buffer;
unsigned short temp;
(void) shift; /* unused parameter */
while( count-- )
{
temp = *p;
*p++ = (unsigned short)((temp<<8) | (temp>>8));
}
}
static void Swap24( void *buffer, long shift, long count )
{
unsigned char *p = (unsigned char*)buffer;
unsigned char temp;
(void) shift; /* unused parameter */
while( count-- )
{
temp = *p;
*p = *(p+2);
*(p+2) = temp;
p += 3;
}
}
#define PA_SWAP32_( x ) ((x>>24) | ((x>>8)&0xFF00) | ((x<<8)&0xFF0000) | (x<<24));
static void Swap32( void *buffer, long shift, long count )
{
unsigned long *p = (unsigned long*)buffer;
unsigned long temp;
(void) shift; /* unused parameter */
while( count-- )
{
temp = *p;
*p++ = PA_SWAP32_( temp);
}
}
static void SwapShiftLeft32( void *buffer, long shift, long count )
{
unsigned long *p = (unsigned long*)buffer;
unsigned long temp;
while( count-- )
{
temp = *p;
temp = PA_SWAP32_( temp);
*p++ = temp << shift;
}
}
static void ShiftRightSwap32( void *buffer, long shift, long count )
{
unsigned long *p = (unsigned long*)buffer;
unsigned long temp;
while( count-- )
{
temp = *p >> shift;
*p++ = PA_SWAP32_( temp);
}
}
static void ShiftLeft32( void *buffer, long shift, long count )
{
unsigned long *p = (unsigned long*)buffer;
unsigned long temp;
while( count-- )
{
temp = *p;
*p++ = temp << shift;
}
}
static void ShiftRight32( void *buffer, long shift, long count )
{
unsigned long *p = (unsigned long*)buffer;
unsigned long temp;
while( count-- )
{
temp = *p;
*p++ = temp >> shift;
}
}
#define PA_SWAP_( x, y ) temp=x; x = y; y = temp;
static void Swap64ConvertFloat64ToFloat32( void *buffer, long shift, long count )
{
double *in = (double*)buffer;
float *out = (float*)buffer;
unsigned char *p;
unsigned char temp;
(void) shift; /* unused parameter */
while( count-- )
{
p = (unsigned char*)in;
PA_SWAP_( p[0], p[7] );
PA_SWAP_( p[1], p[6] );
PA_SWAP_( p[2], p[5] );
PA_SWAP_( p[3], p[4] );
*out++ = (float) (*in++);
}
}
static void ConvertFloat64ToFloat32( void *buffer, long shift, long count )
{
double *in = (double*)buffer;
float *out = (float*)buffer;
(void) shift; /* unused parameter */
while( count-- )
*out++ = (float) (*in++);
}
static void ConvertFloat32ToFloat64Swap64( void *buffer, long shift, long count )
{
float *in = ((float*)buffer) + (count-1);
double *out = ((double*)buffer) + (count-1);
unsigned char *p;
unsigned char temp;
(void) shift; /* unused parameter */
while( count-- )
{
*out = *in--;
p = (unsigned char*)out;
PA_SWAP_( p[0], p[7] );
PA_SWAP_( p[1], p[6] );
PA_SWAP_( p[2], p[5] );
PA_SWAP_( p[3], p[4] );
out--;
}
}
static void ConvertFloat32ToFloat64( void *buffer, long shift, long count )
{
float *in = ((float*)buffer) + (count-1);
double *out = ((double*)buffer) + (count-1);
(void) shift; /* unused parameter */
while( count-- )
*out-- = *in--;
}
#ifdef MAC
#define PA_MSB_IS_NATIVE_
#undef PA_LSB_IS_NATIVE_
#endif
#ifdef WINDOWS
#undef PA_MSB_IS_NATIVE_
#define PA_LSB_IS_NATIVE_
#endif
typedef void PaAsioBufferConverter( void *, long, long );
static void SelectAsioToPaConverter( ASIOSampleType type, PaAsioBufferConverter **converter, long *shift )
{
*shift = 0;
*converter = 0;
switch (type) {
case ASIOSTInt16MSB:
/* dest: paInt16, no conversion necessary, possible byte swap*/
#ifdef PA_LSB_IS_NATIVE_
*converter = Swap16;
#endif
break;
case ASIOSTInt16LSB:
/* dest: paInt16, no conversion necessary, possible byte swap*/
#ifdef PA_MSB_IS_NATIVE_
*converter = Swap16;
#endif
break;
case ASIOSTFloat32MSB:
/* dest: paFloat32, no conversion necessary, possible byte swap*/
#ifdef PA_LSB_IS_NATIVE_
*converter = Swap32;
#endif
break;
case ASIOSTFloat32LSB:
/* dest: paFloat32, no conversion necessary, possible byte swap*/
#ifdef PA_MSB_IS_NATIVE_
*converter = Swap32;
#endif
break;
case ASIOSTFloat64MSB:
/* dest: paFloat32, in-place conversion to/from float32, possible byte swap*/
#ifdef PA_LSB_IS_NATIVE_
*converter = Swap64ConvertFloat64ToFloat32;
#else
*converter = ConvertFloat64ToFloat32;
#endif
break;
case ASIOSTFloat64LSB:
/* dest: paFloat32, in-place conversion to/from float32, possible byte swap*/
#ifdef PA_MSB_IS_NATIVE_
*converter = Swap64ConvertFloat64ToFloat32;
#else
*converter = ConvertFloat64ToFloat32;
#endif
break;
case ASIOSTInt32MSB:
/* dest: paInt32, no conversion necessary, possible byte swap */
#ifdef PA_LSB_IS_NATIVE_
*converter = Swap32;
#endif
break;
case ASIOSTInt32LSB:
/* dest: paInt32, no conversion necessary, possible byte swap */
#ifdef PA_MSB_IS_NATIVE_
*converter = Swap32;
#endif
break;
case ASIOSTInt32MSB16:
/* dest: paInt32, 16 bit shift, possible byte swap */
#ifdef PA_LSB_IS_NATIVE_
*converter = SwapShiftLeft32;
#else
*converter = ShiftLeft32;
#endif
*shift = 16;
break;
case ASIOSTInt32MSB18:
/* dest: paInt32, 14 bit shift, possible byte swap */
#ifdef PA_LSB_IS_NATIVE_
*converter = SwapShiftLeft32;
#else
*converter = ShiftLeft32;
#endif
*shift = 14;
break;
case ASIOSTInt32MSB20:
/* dest: paInt32, 12 bit shift, possible byte swap */
#ifdef PA_LSB_IS_NATIVE_
*converter = SwapShiftLeft32;
#else
*converter = ShiftLeft32;
#endif
*shift = 12;
break;
case ASIOSTInt32MSB24:
/* dest: paInt32, 8 bit shift, possible byte swap */
#ifdef PA_LSB_IS_NATIVE_
*converter = SwapShiftLeft32;
#else
*converter = ShiftLeft32;
#endif
*shift = 8;
break;
case ASIOSTInt32LSB16:
/* dest: paInt32, 16 bit shift, possible byte swap */
#ifdef PA_MSB_IS_NATIVE_
*converter = SwapShiftLeft32;
#else
*converter = ShiftLeft32;
#endif
*shift = 16;
break;
case ASIOSTInt32LSB18:
/* dest: paInt32, 14 bit shift, possible byte swap */
#ifdef PA_MSB_IS_NATIVE_
*converter = SwapShiftLeft32;
#else
*converter = ShiftLeft32;
#endif
*shift = 14;
break;
case ASIOSTInt32LSB20:
/* dest: paInt32, 12 bit shift, possible byte swap */
#ifdef PA_MSB_IS_NATIVE_
*converter = SwapShiftLeft32;
#else
*converter = ShiftLeft32;
#endif
*shift = 12;
break;
case ASIOSTInt32LSB24:
/* dest: paInt32, 8 bit shift, possible byte swap */
#ifdef PA_MSB_IS_NATIVE_
*converter = SwapShiftLeft32;
#else
*converter = ShiftLeft32;
#endif
*shift = 8;
break;
case ASIOSTInt24MSB:
/* dest: paInt24, no conversion necessary, possible byte swap */
#ifdef PA_LSB_IS_NATIVE_
*converter = Swap24;
#endif
break;
case ASIOSTInt24LSB:
/* dest: paInt24, no conversion necessary, possible byte swap */
#ifdef PA_MSB_IS_NATIVE_
*converter = Swap24;
#endif
break;
}
}
static void SelectPaToAsioConverter( ASIOSampleType type, PaAsioBufferConverter **converter, long *shift )
{
*shift = 0;
*converter = 0;
switch (type) {
case ASIOSTInt16MSB:
/* src: paInt16, no conversion necessary, possible byte swap*/
#ifdef PA_LSB_IS_NATIVE_
*converter = Swap16;
#endif
break;
case ASIOSTInt16LSB:
/* src: paInt16, no conversion necessary, possible byte swap*/
#ifdef PA_MSB_IS_NATIVE_
*converter = Swap16;
#endif
break;
case ASIOSTFloat32MSB:
/* src: paFloat32, no conversion necessary, possible byte swap*/
#ifdef PA_LSB_IS_NATIVE_
*converter = Swap32;
#endif
break;
case ASIOSTFloat32LSB:
/* src: paFloat32, no conversion necessary, possible byte swap*/
#ifdef PA_MSB_IS_NATIVE_
*converter = Swap32;
#endif
break;
case ASIOSTFloat64MSB:
/* src: paFloat32, in-place conversion to/from float32, possible byte swap*/
#ifdef PA_LSB_IS_NATIVE_
*converter = ConvertFloat32ToFloat64Swap64;
#else
*converter = ConvertFloat32ToFloat64;
#endif
break;
case ASIOSTFloat64LSB:
/* src: paFloat32, in-place conversion to/from float32, possible byte swap*/
#ifdef PA_MSB_IS_NATIVE_
*converter = ConvertFloat32ToFloat64Swap64;
#else
*converter = ConvertFloat32ToFloat64;
#endif
break;
case ASIOSTInt32MSB:
/* src: paInt32, no conversion necessary, possible byte swap */
#ifdef PA_LSB_IS_NATIVE_
*converter = Swap32;
#endif
break;
case ASIOSTInt32LSB:
/* src: paInt32, no conversion necessary, possible byte swap */
#ifdef PA_MSB_IS_NATIVE_
*converter = Swap32;
#endif
break;
case ASIOSTInt32MSB16:
/* src: paInt32, 16 bit shift, possible byte swap */
#ifdef PA_LSB_IS_NATIVE_
*converter = ShiftRightSwap32;
#else
*converter = ShiftRight32;
#endif
*shift = 16;
break;
case ASIOSTInt32MSB18:
/* src: paInt32, 14 bit shift, possible byte swap */
#ifdef PA_LSB_IS_NATIVE_
*converter = ShiftRightSwap32;
#else
*converter = ShiftRight32;
#endif
*shift = 14;
break;
case ASIOSTInt32MSB20:
/* src: paInt32, 12 bit shift, possible byte swap */
#ifdef PA_LSB_IS_NATIVE_
*converter = ShiftRightSwap32;
#else
*converter = ShiftRight32;
#endif
*shift = 12;
break;
case ASIOSTInt32MSB24:
/* src: paInt32, 8 bit shift, possible byte swap */
#ifdef PA_LSB_IS_NATIVE_
*converter = ShiftRightSwap32;
#else
*converter = ShiftRight32;
#endif
*shift = 8;
break;
case ASIOSTInt32LSB16:
/* src: paInt32, 16 bit shift, possible byte swap */
#ifdef PA_MSB_IS_NATIVE_
*converter = ShiftRightSwap32;
#else
*converter = ShiftRight32;
#endif
*shift = 16;
break;
case ASIOSTInt32LSB18:
/* src: paInt32, 14 bit shift, possible byte swap */
#ifdef PA_MSB_IS_NATIVE_
*converter = ShiftRightSwap32;
#else
*converter = ShiftRight32;
#endif
*shift = 14;
break;
case ASIOSTInt32LSB20:
/* src: paInt32, 12 bit shift, possible byte swap */
#ifdef PA_MSB_IS_NATIVE_
*converter = ShiftRightSwap32;
#else
*converter = ShiftRight32;
#endif
*shift = 12;
break;
case ASIOSTInt32LSB24:
/* src: paInt32, 8 bit shift, possible byte swap */
#ifdef PA_MSB_IS_NATIVE_
*converter = ShiftRightSwap32;
#else
*converter = ShiftRight32;
#endif
*shift = 8;
break;
case ASIOSTInt24MSB:
/* src: paInt24, no conversion necessary, possible byte swap */
#ifdef PA_LSB_IS_NATIVE_
*converter = Swap24;
#endif
break;
case ASIOSTInt24LSB:
/* src: paInt24, no conversion necessary, possible byte swap */
#ifdef PA_MSB_IS_NATIVE_
*converter = Swap24;
#endif
break;
}
}
typedef struct PaAsioDeviceInfo
{
PaDeviceInfo commonDeviceInfo;
long minBufferSize;
long maxBufferSize;
long preferredBufferSize;
long bufferGranularity;
ASIOChannelInfo *asioChannelInfos;
}
PaAsioDeviceInfo;
PaError PaAsio_GetAvailableBufferSizes( PaDeviceIndex device,
long *minBufferSizeFrames, long *maxBufferSizeFrames, long *preferredBufferSizeFrames, long *granularity )
{
PaError result;
PaUtilHostApiRepresentation *hostApi;
PaDeviceIndex hostApiDevice;
result = PaUtil_GetHostApiRepresentation( &hostApi, paASIO );
if( result == paNoError )
{
result = PaUtil_DeviceIndexToHostApiDeviceIndex( &hostApiDevice, device, hostApi );
if( result == paNoError )
{
PaAsioDeviceInfo *asioDeviceInfo =
(PaAsioDeviceInfo*)hostApi->deviceInfos[hostApiDevice];
*minBufferSizeFrames = asioDeviceInfo->minBufferSize;
*maxBufferSizeFrames = asioDeviceInfo->maxBufferSize;
*preferredBufferSizeFrames = asioDeviceInfo->preferredBufferSize;
*granularity = asioDeviceInfo->bufferGranularity;
}
}
return result;
}
/* Unload whatever we loaded in LoadAsioDriver().
*/
static void UnloadAsioDriver( void )
{
ASIOExit();
}
/*
load the asio driver named by <driverName> and return statistics about
the driver in info. If no error occurred, the driver will remain open
and must be closed by the called by calling UnloadAsioDriver() - if an error
is returned the driver will already be unloaded.
*/
static PaError LoadAsioDriver( PaAsioHostApiRepresentation *asioHostApi, const char *driverName,
PaAsioDriverInfo *driverInfo, void *systemSpecific )
{
PaError result = paNoError;
ASIOError asioError;
int asioIsInitialized = 0;
if( !asioHostApi->asioDrivers->loadDriver( const_cast<char*>(driverName) ) )
{
result = paUnanticipatedHostError;
PA_ASIO_SET_LAST_HOST_ERROR( 0, "Failed to load ASIO driver" );
goto error;
}
memset( &driverInfo->asioDriverInfo, 0, sizeof(ASIODriverInfo) );
driverInfo->asioDriverInfo.asioVersion = 2;
driverInfo->asioDriverInfo.sysRef = systemSpecific;
if( (asioError = ASIOInit( &driverInfo->asioDriverInfo )) != ASE_OK )
{
result = paUnanticipatedHostError;
PA_ASIO_SET_LAST_ASIO_ERROR( asioError );
goto error;
}
else
{
asioIsInitialized = 1;
}
if( (asioError = ASIOGetChannels(&driverInfo->inputChannelCount,
&driverInfo->outputChannelCount)) != ASE_OK )
{
result = paUnanticipatedHostError;
PA_ASIO_SET_LAST_ASIO_ERROR( asioError );
goto error;
}
if( (asioError = ASIOGetBufferSize(&driverInfo->bufferMinSize,
&driverInfo->bufferMaxSize, &driverInfo->bufferPreferredSize,
&driverInfo->bufferGranularity)) != ASE_OK )
{
result = paUnanticipatedHostError;
PA_ASIO_SET_LAST_ASIO_ERROR( asioError );
goto error;
}
if( ASIOOutputReady() == ASE_OK )
driverInfo->postOutput = true;
else
driverInfo->postOutput = false;
return result;
error:
if( asioIsInitialized )
{
ASIOExit();
}