-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharsc_asio.c
More file actions
1090 lines (929 loc) · 39.1 KB
/
Copy patharsc_asio.c
File metadata and controls
1090 lines (929 loc) · 39.1 KB
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
#ifdef ASIO
#include "arsc_asio.h"
#include "arsc_asio_wrappers.h"
#include "../arsclib.h"
#include <Windows.h>
#include <stdio.h>
enum {
max_registry_key_length = 255,
max_drivers = 32, // PortAudio allows a max of 32, so we will too
max_driver_name_length = 40
};
#define ASIO_PATH "software\\asio"
typedef struct {
CLSID clsid;
char name[max_driver_name_length];
char description[max_driver_name_length];
int valid;
int32_t devices;
} TAsioDriver;
// ASIO loads and intializes only one valid driver at a time. To
// make a full list of valid "devices" (in terms of waveopen functionality)
// I created this structure to hold possible devices.
typedef struct {
char name[ARSC_NAMLEN];
int32_t driver; // index back to TAsioDriver struct
int32_t good_sampling_rates;
int IsOutput;
} TVirtualDevice;
ASIOBufferInfo *global_asio_buffer_info = NULL;
ArAsioOutputAudio *global_ar_asio_output_audio;
ArAsioInputAudio *global_ar_asio_input_audio;
ARDEV *global_ar_asio_current_device;
int (*SDKAsioSetSampleRate)(
ASIOSampleRate aSampleRate) = SDKAsioSetSampleRateImpl;
int (*SDKAsioGetBufferSize)(long *min_buffer_size, long *max_buffer_size,
long *preferred_buffer_size, long *granularity) = SDKAsioGetBufferSizeImpl;
int (*SDKAsioOutputReady)() = SDKAsioOutputReadyImpl;
int (*SDKAsioCreateBuffers)(ASIOBufferInfo *bufferInfos, long channels,
long buffer_size, ASIOCallbacks *callbacks) = SDKAsioCreateBuffersImpl;
int (*SDKAsioGetLatencies)(
long *inputLatency, long *outputLatency) = SDKAsioGetLatenciesImpl;
int (*SDKAsioStart)(void) = SDKAsioStartImpl;
// device identifier offset
// for multiple platforms such as when WIND and ASIO are both set in the
// compilation. The WIND devices are listed first, for example 0 to 5, then
// the ASIO devices are listed, e.g. 6.
static int32_t device_identifier_offset = 0;
static TAsioDriver drivers[max_drivers];
static TVirtualDevice VirtualDevice[MAXDEV]; // To hold the device names and the
// driver to which they associate
// Index into the AsioDriverList[] for the loaded ASIO driver
static int32_t initialized_driver = -1;
static int32_t device_count = 0; // Number of valid ASIO devices
static int32_t max_input_channels = -1; // Maximum as reported by the driver
static int32_t max_output_channels = -1; // Maximum as reported by the driver
static ASIOChannelInfo *channelInfos = NULL;
static ArAsioOutputAudio *first_output_audio_of_current_segment;
static ArAsioInputAudio *first_input_audio_of_current_segment;
static long preferred_buffer_size; // Returned by the driver
static long total_input_and_output_channels = 0;
static long input_latency, output_latency; // Latencies as polled from the card.
// flag - 1 if driver uses ASIOOutputReady optimization
static int output_ready_optimization = 0;
static int buffers_have_been_created = 0;
static int driver_has_started = 0; // flag - 1 if driver is started
static int32_t total_samples_processed = 0; // Total samples processed
static long latency_offset = 0; // LatencyOffset specified by app
static int32_t segment_has_finished = 0;
static int32_t good_sample_rates;
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 int32_t pPollAsioDrivers(void);
static int32_t pLockAndLoadImpl(int32_t aintDevice);
static int32_t pBuildVirtualDevices(int32_t aintDriver);
static int32_t ar_asio_devices_impl() {
// Has the number of devices already been polled?
if (device_count > 0) {
return device_count;
} else {
// Need to load the ASIO driver and get some details.
if (!pPollAsioDrivers())
return 0;
}
return device_count;
}
int32_t (*ar_asio_devices)() = ar_asio_devices_impl;
/*
_ar_asio_dev_name - return name of I/O device
The original assumption was to just return the driver name.
This doesn't work for Echo ASIO WDM, which is the same driver
name for a number of Echo cards.
*/
static char *_ar_asio_dev_name(int32_t di) {
// In case this is called before we are ready
if (device_count == 0) {
if (!pPollAsioDrivers())
return NULL;
}
for (int32_t i = 0; i < MAXDEV; i++) {
if (i == (di - device_identifier_offset)) {
return VirtualDevice[i].name;
}
}
return NULL;
}
char *(*ar_asio_device_name)(int32_t) = _ar_asio_dev_name;
/*
_ar_asio_list_rates - return good sampling rates
*/
static int32_t _ar_asio_list_rates(int32_t di) {
// In case this is called before we are ready
if (device_count == 0) {
if (!pPollAsioDrivers())
return 0;
}
for (int32_t i = 0; i < MAXDEV; i++) {
if (i == (di - device_identifier_offset)) {
return VirtualDevice[i].good_sampling_rates;
}
}
return 0;
}
int32_t (*ar_asio_list_rates)(int32_t) = _ar_asio_list_rates;
static void _ar_asio_io_stop(int32_t di) {
global_ar_asio_current_device = _ardev[di];
if (driver_has_started) {
SDKAsioStop();
driver_has_started = 0;
}
}
void (*ar_asio_io_stop)(int32_t) = _ar_asio_io_stop;
static void _ar_asio_close(int32_t di) {
// Stop the driver if it is running
if (driver_has_started) {
_ar_asio_io_stop(di);
driver_has_started = 0;
Sleep(1); // 1-ms delay delay before freeing buffers
}
SDKAsioDisposeBuffers();
buffers_have_been_created = 0;
// Clear the channels
if (channelInfos != NULL) {
free(channelInfos);
channelInfos = NULL;
}
// Clear the buffers
if (global_asio_buffer_info != NULL) {
free(global_asio_buffer_info);
global_asio_buffer_info = NULL;
}
// Clear stim blocks
if (global_ar_asio_output_audio != NULL) {
free(global_ar_asio_output_audio);
global_ar_asio_output_audio = NULL;
}
// Clear resp blocks
if (global_ar_asio_output_audio != NULL) {
free(global_ar_asio_input_audio);
global_ar_asio_input_audio = NULL;
}
// Unload the ASIO driver
if (initialized_driver != -1) {
SDKAsioExit();
}
initialized_driver = -1;
}
void (*ar_asio_close)(int32_t) = _ar_asio_close;
static ASIOCallbacks asioCallbacks;
int32_t _ar_asio_open(int32_t di) {
int32_t intChannelOffset = 0;
global_ar_asio_current_device = _ardev[di];
// Loon test
if (intChannelOffset + global_ar_asio_current_device->ncda >
max_output_channels) {
goto err;
}
if (intChannelOffset + global_ar_asio_current_device->ncad >
max_input_channels) {
goto err;
}
int bolOutput = !(_arsc_find & ARSC_PREF_IN);
// Load the driver and initialize
if (!pLockAndLoad(di))
goto err;
// This is always 1 for ASIO. This lets the API code use the
// segments as is because of the ef (effective) flag.
global_ar_asio_current_device->nbps = 4;
global_ar_asio_current_device->ntlv = 0;
/*
Ensure a good sample rate, then set it.
gdsr is a bitwise combintation of our list of 27 sample rates.
Once known, we don't have to look it up again.
*/
if (!global_ar_asio_current_device->gdsr)
global_ar_asio_current_device->gdsr = ar_asio_list_rates(di);
global_ar_asio_current_device->rate =
_ar_adjust_rate(di, global_ar_asio_current_device->a_rate);
if (!SDKAsioSetSampleRate((double)global_ar_asio_current_device->rate))
goto err;
// check whether the driver requires the ASIOOutputReady() optimization
// (can be used by the driver to reduce output latency by one block)
output_ready_optimization = SDKAsioOutputReady();
/*
Get the buffer size information from the driver. This
value is set in the ASIO control panel. So far I haven't
found a need to deviate from the Preferred . . . .
*/
long max_buffer_size;
long min_buffer_size;
long granularity;
if (!SDKAsioGetBufferSize(&min_buffer_size, &max_buffer_size,
&preferred_buffer_size, &granularity))
goto err;
/*
Allocate bufferInfos
*/
total_input_and_output_channels = global_ar_asio_current_device->a_ncad +
global_ar_asio_current_device->a_ncda;
if ((global_asio_buffer_info = (ASIOBufferInfo *)calloc(
total_input_and_output_channels, sizeof(ASIOBufferInfo))) == NULL)
goto err;
// Figure out the channel offset in case there are multiple ASIO cards.
// Count all "devices" up to the card in question.
for (int32_t i = 0; i < initialized_driver; i++)
if (drivers[i].valid)
intChannelOffset += drivers[i].devices;
intChannelOffset = (di - device_identifier_offset) - intChannelOffset;
ASIOBufferInfo *bufferInfo = global_asio_buffer_info;
for (int32_t i = 0; i < global_ar_asio_current_device->a_ncda; i++) {
bufferInfo->isInput = ASIOFalse;
bufferInfo->channelNum = i;
bufferInfo->buffers[0] = NULL;
bufferInfo->buffers[1] = NULL;
bufferInfo++;
}
for (int32_t i = 0; i < global_ar_asio_current_device->a_ncad;
i++) { // loop over output channels
bufferInfo->isInput = ASIOTrue; // create an input buffer
bufferInfo->channelNum = i; // (di - dio) handles channel offsets
bufferInfo->buffers[0] = NULL; // clear buffer 1/2 channels
bufferInfo->buffers[1] = NULL; // clear buffer 1/2 channels
bufferInfo++;
}
// set up the asioCallback structure and create the ASIO data buffer
asioCallbacks.bufferSwitch = &bufferSwitch;
asioCallbacks.sampleRateDidChange = &sampleRateChanged;
asioCallbacks.asioMessage = &asioMessages;
asioCallbacks.bufferSwitchTimeInfo = &bufferSwitchTimeInfo;
/*
Create the ASIO buffers, both input and output. Also set up the callbacks.
*/
buffers_have_been_created = SDKAsioCreateBuffers(global_asio_buffer_info,
total_input_and_output_channels, preferred_buffer_size, &asioCallbacks);
if (!buffers_have_been_created) {
goto err;
}
// get the input and output latencies
// Latencies often are only valid after ASIOCreateBuffers()
// (input latency is the age of the first sample in the currently returned
// audio block) (output latency is the time the first sample in the
// currently returned audio block requires to get to the output)
if (!SDKAsioGetLatencies(&input_latency, &output_latency))
goto err;
// Clear the buffers because CardDeluxe has known issues
bufferInfo = global_asio_buffer_info;
for (int32_t i = 0; i < total_input_and_output_channels; i++) {
memset(
bufferInfo->buffers[0], 0, preferred_buffer_size * sizeof(int32_t));
memset(
bufferInfo->buffers[1], 0, preferred_buffer_size * sizeof(int32_t));
bufferInfo++;
}
total_samples_processed = 0; // Total samples processed
latency_offset = 0; // LatencyOffset specified by app
segment_has_finished =
0; // semaphore-like variable to tell when segment is finished
return (0);
err:
return 120; // ASIO open error
}
int32_t (*ar_asio_open)(int32_t) = _ar_asio_open;
int32_t _ar_asio_io_prepare(int32_t di) {
global_ar_asio_current_device = _ardev[di];
/*
Set up stimulus (OUTPUT) blocks
This is just a contiguous array of ArAsioChannelBuffers. The bufferSwitch
only loops over channels, so the contiguous array should (for stereo) look
like this:
SEGMENT CHANNEL
0 0
0 1
1 0
1 1
2 0
. . .
The pointer "first_output_audio_of_current_segment" will point to the first
(channel 0) global_output_audio for the current segment.
*/
size_t segments = global_ar_asio_current_device->segswp;
if ((global_ar_asio_output_audio =
calloc(global_ar_asio_current_device->ncda * segments,
sizeof(ArAsioOutputAudio))) == NULL)
return -1;
if ((global_ar_asio_input_audio =
calloc(global_ar_asio_current_device->ncad * segments,
sizeof(ArAsioInputAudio))) == NULL)
return -1;
ArAsioOutputAudio *output_audio = global_ar_asio_output_audio;
first_output_audio_of_current_segment = global_ar_asio_output_audio;
for (int32_t i = 0; i < (int32_t)(global_ar_asio_current_device->ncda * segments);
i++) {
output_audio->channel = i % global_ar_asio_current_device->ncda;
output_audio->segment = i / global_ar_asio_current_device->ncda;
output_audio->data = global_ar_asio_current_device->o_data[i];
output_audio->Index = 0;
output_audio->size =
global_ar_asio_current_device
->sizptr[i / global_ar_asio_current_device->ncda];
output_audio++;
}
ArAsioInputAudio *ptrResponseData = global_ar_asio_input_audio;
first_input_audio_of_current_segment = global_ar_asio_input_audio;
for (int32_t i = 0; i < (int32_t)(global_ar_asio_current_device->ncad * segments);
i++) {
ptrResponseData->channel = i % global_ar_asio_current_device->ncad;
ptrResponseData->segment = i / global_ar_asio_current_device->ncad;
ptrResponseData->data = global_ar_asio_current_device->i_data[i];
ptrResponseData->Index = 0;
ptrResponseData->size =
global_ar_asio_current_device
->sizptr[i / global_ar_asio_current_device->ncad];
// Only segment 0 need be concerned with latency
ptrResponseData->LatencyReached = (ptrResponseData->segment != 0);
ptrResponseData++;
}
return (0);
}
int32_t (*ar_asio_io_prepare)(int32_t) = _ar_asio_io_prepare;
/* _ar_asio_xfer_seg - this segment is ready to go */
static int32_t _ar_asio_xfer_seg(int32_t di, int32_t b) { return 0; }
int32_t (*ar_asio_transfer_segment)(int32_t, int32_t) = _ar_asio_xfer_seg;
int32_t _ar_asio_chk_seg(int32_t di, int32_t b) {
/*
This function will tell the current segment that is running.
Should be for both input and output.
Polling sort of worked. It was interesting in that printf() statements
on the ASIO side and the calling program were combined onscreen. Also,
it ends one segment early.
It's a threading synchronization thing. My thought was to use
Windows Messages to handle this, but then I am necessarily creating
a windows application even if the Window is invisible. PortAudio
didn't require this, and it also means that the examples written
for MME/WDM will need modification to use ASIO.
The problem, then, is that the polling function ar_io_cur_seg() needs
to go through the chk_seg() function in the API. This is not only
extra processing, but it doesn't really yield the result, i.e. the
current segment that is needed.
*/
global_ar_asio_current_device = _ardev[di];
if (!driver_has_started) {
return -1;
}
switch (segment_has_finished) {
case 0:
break;
case 1:
// The segment has just finished. If this routine is called frequently
// enough, we should get this within moments of it happening.
segment_has_finished--;
return 1;
break;
default:
// Segment overrun
// This can happen when clicking to another window while the app is
// running.
global_ar_asio_current_device->xrun++;
return 1;
}
return 0; // 0 means the API xfer function isn't run
}
int32_t (*ar_asio_check_segment)(int32_t, int32_t) = _ar_asio_chk_seg;
void _ar_asio_io_start(int32_t di) {
total_samples_processed = 0;
driver_has_started = SDKAsioStart();
}
void (*ar_asio_io_start)(int32_t) = _ar_asio_io_start;
/* _ar_asio_latency - set and get latency */
static int32_t _ar_asio_latency(int32_t di, int32_t nsmp) {
long max_latency;
global_ar_asio_current_device =
_ardev[di]; // get access to application parameters
if (nsmp != ARSC_GET_LATENCY) {
max_latency = input_latency +
output_latency; // sum of driver input/output latencies
max_latency -= max_latency % 256; // subtract impulse latency, if any
if (nsmp > max_latency)
nsmp = max_latency;
latency_offset = nsmp;
}
return latency_offset;
}
int32_t (*ar_asio_latency)(int32_t, int32_t) = _ar_asio_latency;
static ARDVT *device_type(int32_t n) { return &_ardvt[n]; }
int32_t _ar_asio_bind(int32_t ndt, int32_t tnd) {
// Get the number of ASIO devices. This is not the same as the
// number of ASIO Drivers in the registry. This will either be
// 1 or 0.
int32_t devices = ar_asio_devices();
if (devices > 0) {
device_type(ndt)->num_dev = ar_asio_devices;
device_type(ndt)->dev_name = ar_asio_device_name;
device_type(ndt)->io_stop = ar_asio_io_stop;
device_type(ndt)->close = ar_asio_close;
device_type(ndt)->open = ar_asio_open;
device_type(ndt)->io_prepare = ar_asio_io_prepare;
device_type(ndt)->io_start = ar_asio_io_start;
device_type(ndt)->xfer_seg = ar_asio_transfer_segment;
device_type(ndt)->chk_seg = ar_asio_check_segment;
device_type(ndt)->latency = ar_asio_latency;
device_type(ndt)->list_rates = ar_asio_list_rates;
device_identifier_offset = tnd;
}
return devices;
}
static int is_last_output_segment(ArAsioOutputAudio *audio) {
return audio->segment + 1 == global_ar_asio_current_device->segswp;
}
static int is_last_output_channel(ArAsioOutputAudio *audio) {
int32_t output_channels = global_ar_asio_current_device->a_ncda;
int32_t last_channel = output_channels - 1;
return audio->channel == last_channel;
}
static int is_last_input_channel(ArAsioInputAudio *audio) {
int32_t input_channels = global_ar_asio_current_device->a_ncad;
int32_t last_channel = input_channels - 1;
return audio->channel == last_channel;
}
static int output_is_exhausted(ArAsioOutputAudio *audio) {
return audio->Index == audio->size;
}
static int input_is_exhausted(ArAsioInputAudio *audio) {
return audio->Index == audio->size;
}
static int is_last_input_segment(ArAsioInputAudio *audio) {
return audio->segment + 1 == global_ar_asio_current_device->segswp;
}
static int32_t minimum(int32_t a, int32_t b) { return a < b ? a : b; }
static void copy(int32_t *destination, int32_t *source, int32_t count) {
memcpy(destination, source, count * sizeof(int32_t));
}
static void update_if_last_output_channel(ArAsioOutputAudio *audio) {
if (is_last_output_channel(audio)) {
// If there are no input channels, the out channel determines the
// segment end
if (!global_ar_asio_current_device->a_ncad)
segment_has_finished++;
first_output_audio_of_current_segment = is_last_output_segment(audio)
? global_ar_asio_output_audio
: audio + 1;
}
}
int32_t ar_asio_write_device_buffer(
int32_t *destination, int32_t size, ArAsioOutputAudio *audio) {
int32_t copied = 0;
while (1) {
int32_t *source = audio->data + audio->Index;
int32_t to_copy = minimum(size - copied, audio->size - audio->Index);
copy(destination + copied, source, to_copy);
copied += to_copy;
audio->Index += to_copy;
if (output_is_exhausted(audio)) {
update_if_last_output_channel(audio);
int32_t output_channels = global_ar_asio_current_device->a_ncda;
audio += is_last_output_segment(audio)
? -output_channels * (global_ar_asio_current_device->segswp - 1)
: output_channels;
audio->Index = 0;
} else
return 1;
}
}
int32_t ar_asio_read_device_buffer(
int32_t *source, int32_t size, ArAsioInputAudio *audio) {
/*
Check to see that the latency between ouput and input has been reached.
Each channel has the same latency (one would hope), and we only check
the latency once for each channel of segment 0. This was accomplished
by filling the LatencyReached flag to 1 for segments 1..N
*/
if (!audio->LatencyReached) {
// An E-mail to the ASIO listserv suggested the calculation for the
// offset to be Input + Output latency. These values are usually quite
// close.
// Changed default LoopbackLatecy to match WDM latency [STN:Jun-2007]
int32_t loopback_latency = input_latency + output_latency;
int32_t impulse_latency = loopback_latency % 256;
loopback_latency -= latency_offset + impulse_latency;
int32_t samples_skipped =
minimum(loopback_latency - audio->SkippedSamples, size);
audio->SkippedSamples += samples_skipped;
size -= samples_skipped;
source += samples_skipped;
audio->LatencyReached = audio->SkippedSamples == loopback_latency;
}
int32_t copied = 0;
while (1) {
int32_t *destination = audio->data + audio->Index;
int32_t to_copy = minimum(size - copied, audio->size - audio->Index);
copy(destination, source + copied, to_copy);
copied += to_copy;
audio->Index += to_copy;
if (input_is_exhausted(audio)) {
if (is_last_input_channel(audio)) {
segment_has_finished++;
first_input_audio_of_current_segment =
is_last_input_segment(audio) ? global_ar_asio_input_audio
: audio + 1;
}
int32_t input_channels = global_ar_asio_current_device->a_ncad;
audio += is_last_input_segment(audio)
? -input_channels * (global_ar_asio_current_device->segswp - 1)
: input_channels;
audio->Index = 0;
} else
return 1;
}
}
static long check_rates(void) {
/*
Get a list of sample rates the card can handle. The list will be
used later to ensure the closest valid sample rate to the requested
rate is used.
*/
good_sample_rates = 0;
for (int i = 0; i < SRLSTSZ; i++) {
if (SDKAsioCanSampleRate(_ar_SRlist[i]) == 1) {
good_sample_rates |= 1 << i;
}
}
return (good_sample_rates);
}
// The sound card name is contained within the channel information.
// This is opposed to the old method of just using the driver name.
// The SYNC routine has "devices" which more closely resemble these
// ASIO channels.
//
// The channel information is more useful than the driver name, even
// though only one driver can be used at a time, because all Echo cards
// have the same driver name of "Echo ASIO WDM." This is a way to
// differentiate between cards.
//
// The SYNC code only gets the OUT devices, unless only input is given.
int32_t pGetChannelDetails(int32_t aintDriver) {
ASIOChannelInfo *ptrChannelInfo;
// Are we interested in input or output channels? Usually output.
int bolOutput = !(_arsc_find & ARSC_PREF_IN);
// Hold onto the number of "devices" for this driver. This is used
// later in calculating the channel offset from the device number.
if (bolOutput)
drivers[aintDriver].devices = max_output_channels;
else
drivers[aintDriver].devices = max_input_channels;
;
// Channel details
ptrChannelInfo = channelInfos;
for (int32_t i = 0; i < max_output_channels; i++) {
ptrChannelInfo->isInput = ASIOFalse;
ptrChannelInfo->channel = i;
// Gets channel information from the driver
if (!SDKAsioGetChannelInfo(ptrChannelInfo))
return -1;
if (bolOutput) {
// Set the VirtualDevice information
VirtualDevice[device_count].driver = aintDriver;
sprintf(VirtualDevice[device_count].name, "%s ASIO",
ptrChannelInfo->name);
VirtualDevice[device_count].IsOutput = 1;
VirtualDevice[device_count].good_sampling_rates = check_rates();
device_count++;
}
ptrChannelInfo++;
}
for (int32_t i = 0; i < max_input_channels; i++) {
ptrChannelInfo->isInput = ASIOTrue;
ptrChannelInfo->channel = i;
// Gets channel information from the driver
if (!SDKAsioGetChannelInfo(ptrChannelInfo))
return -1;
if (!bolOutput) {
// Set the VirtualDevice information
VirtualDevice[device_count].driver = aintDriver;
sprintf(VirtualDevice[device_count].name, "%s (%s) ASIO %s",
ptrChannelInfo->name, "In",
ptrChannelInfo->isActive == ASIOTrue ? "*" : "");
VirtualDevice[device_count].IsOutput = 0;
VirtualDevice[device_count].good_sampling_rates = check_rates();
device_count++;
}
ptrChannelInfo++;
}
return 1;
}
// This loads the ASIO driver and acquires some details.
int32_t pPollAsioDrivers(void) {
long lRet;
HKEY hkEnum = 0;
HKEY hkDriver = 0;
char keyname[max_registry_key_length];
DWORD index = 0;
DWORD type, size;
char strFullKeyPath[max_registry_key_length];
char value[max_registry_key_length]; // Arbitrary value type
int32_t intDriver; // looping variable
ASIODriverInfo asioDriverInfo; // needed for ASIOInit()
// Open the main key to the ASIO drivers
// http://msdn.microsoft.com/library/en-us/sysinfo/base/regopenkey.asp
lRet = RegOpenKey(HKEY_LOCAL_MACHINE, ASIO_PATH, &hkEnum);
if (lRet != ERROR_SUCCESS) {
return 0;
}
while (lRet == ERROR_SUCCESS) {
// http://msdn.microsoft.com/library/en-us/sysinfo/base/regenumkey.asp
if ((lRet = RegEnumKey(hkEnum, index, (LPTSTR)keyname,
max_registry_key_length)) == ERROR_SUCCESS) {
// Print out the ASIO card name
// Get the subkey that this keyname represents
sprintf(strFullKeyPath, "%s\\%s", ASIO_PATH, keyname);
lRet = RegOpenKey(HKEY_LOCAL_MACHINE, strFullKeyPath, &hkDriver);
if (lRet != ERROR_SUCCESS) {
return 0;
}
// name
// The name is the key in the registry.
strcpy(drivers[index].name, keyname);
// CLSID
size = max_registry_key_length;
lRet = RegQueryValueEx(
hkDriver, "CLSID", 0, &type, (LPBYTE)value, &size);
if (lRet == ERROR_SUCCESS) {
char *ptrClsid = (char *)value; // Shorthand
WORD wData[100];
// Convert to CLSID
if (strlen(ptrClsid) == 38) { // Must be 38 long, including { }
MultiByteToWideChar(
CP_ACP, 0, (LPCSTR)ptrClsid, -1, (LPWSTR)wData, 100);
if (CLSIDFromString((LPOLESTR)wData,
(LPCLSID) & (drivers[index].clsid)) != S_OK) {
}
}
} else {
// All ASIO drivers should have a CLSID
return 0;
}
// Description
// The description is not necessarily the same as the name of the
// driver.
size = max_registry_key_length;
if (RegQueryValueEx(hkDriver, "Description", 0, &type,
(LPBYTE)value, &size) == ERROR_SUCCESS) {
strcpy(drivers[index].description, value);
} else {
// Some ASIO drivers don't have a description
strcpy(drivers[index].description, keyname);
}
index++;
} // fi RegEnumKey
} // elihw
if (hkEnum)
RegCloseKey(hkEnum);
/*
The number of available drivers is not the same as the available ASIO
devices. Need to test each one out to see if it opens.
*/
device_count = 0;
for (intDriver = 0; intDriver < max_drivers; intDriver++) {
if (strlen(drivers[intDriver].name) > 0) {
// In case multiple ASIO sound cards exist, terminate the
// AudioStreamIO. This may not be necessary.
if (initialized_driver > 0)
SDKAsioExit();
if (SDKLoadAsioDriver(drivers[intDriver].name)) {
// But loading isn't enough. The Echo Gina will load even if it
// isn't installed. Initialize the AudioStreamIO.
asioDriverInfo.driverVersion = 2; // ASIO 2.0
asioDriverInfo.sysRef =
GetDesktopWindow(); // Application main window handle
if (SDKAsioInit(&asioDriverInfo) == 1) {
// This ASIO driver can be used. Although we can use only
// one ASIO driver at a time, there may be multiple sound
// cards in the same box, so we aren't done yet.
drivers[intDriver].valid = 1;
// For now, this sets the last ASIO driver as the one used.
initialized_driver = intDriver;
// Get the device information
if (!pBuildVirtualDevices(intDriver))
return 0;
} else {
}
} else {
} // fi SDKLoadAsioDriver
} else {
// Get out when there are no more named drivers
break;
} // fi .name
} // rof intDriver
// Terminate AudioStreamIO. The open function will reload the correct
// driver based upon the application's device choice.
if (initialized_driver > 0) {
SDKAsioExit();
initialized_driver = -1;
}
return 1;
}
/*
Run only after a driver is loaded -and- initialized. This function gets
channel information to derive virtual device ids (for the SYNC analog).
The total possible channels is used to get full channel information from the
card.
*/
int32_t pBuildVirtualDevices(int32_t aintDriver) {
// Gets the number of channels for this card
if (!SDKAsioGetChannels(&max_input_channels, &max_output_channels)) {
return 0;
}
long slngTotalPossibleChannels = max_input_channels + max_output_channels;
// Allocate enough memory for every channel info, but only until we get the
// device information
if ((channelInfos = (ASIOChannelInfo *)calloc(
slngTotalPossibleChannels, sizeof(ASIOChannelInfo))) == NULL)
return 0;
// Get the channel details. This should be done after the buffers are
// created otherwise it is not known which channels are active.
if (!pGetChannelDetails(aintDriver))
return 0;
// Get rid of the channelInfos allocated in this function
if (channelInfos != NULL) {
free(channelInfos);
channelInfos = NULL;
}
return 1;
}
/*
The driver is now known from the application's request. Load
the appropriate driver and initialize.
*/
int32_t pLockAndLoadImpl(int32_t aintDevice) {
int32_t intDriver =
VirtualDevice[aintDevice - device_identifier_offset].driver;
ASIODriverInfo asioDriverInfo; // needed for ASIOInit()
ASIOChannelInfo *ptrChannelInfo; // handy pointer
int32_t i;
if (SDKLoadAsioDriver(drivers[intDriver].name)) {
asioDriverInfo.driverVersion = 2; // ASIO 2.0
asioDriverInfo.sysRef =
GetDesktopWindow(); // Application main window handle
if (SDKAsioInit(&asioDriverInfo) == 1) {
// Hold for later
initialized_driver = intDriver;
} else {
return 0;
}
} else {
return 0;
} // fi SDKLoadAsioDriver
// Gets the number of channels for this card
if (!SDKAsioGetChannels(&max_input_channels, &max_output_channels)) {
} else {
}
long slngTotalPossibleChannels = max_input_channels + max_output_channels;
// Allocate enough memory for every channel info
if ((channelInfos = (ASIOChannelInfo *)calloc(
slngTotalPossibleChannels, sizeof(ASIOChannelInfo))) == NULL)
return 0;
// Initialize channels
ptrChannelInfo = channelInfos;
for (i = 0; i < max_output_channels; i++) {
ptrChannelInfo->isInput = ASIOFalse;
ptrChannelInfo->channel = i;
// Gets channel information from the driver
if (!SDKAsioGetChannelInfo(ptrChannelInfo))
return -1;
ptrChannelInfo++;
}
for (i = 0; i < max_input_channels; i++) {
ptrChannelInfo->isInput = ASIOTrue;
ptrChannelInfo->channel = i;
// Gets channel information from the driver
if (!SDKAsioGetChannelInfo(ptrChannelInfo))
return -1;
ptrChannelInfo++;
}
return 1;
}
int32_t (*pLockAndLoad)(int32_t aintDevice) = pLockAndLoadImpl;
/*
SDK Note:
Beware that this is normally in a seperate thread, hence be sure that
you take care about thread synchronization. This is omitted here for simplicity.
*/
ASIOTime *bufferSwitchTimeInfo(
ASIOTime *timeInfo, long index, ASIOBool processNow) {
long lngAsioBufferSize =
preferred_buffer_size; // shorthand to buffer size in samples
ArAsioOutputAudio *output_audio =
first_output_audio_of_current_segment; // pointer to channel 0 of
// current segment stimulus
ArAsioInputAudio *input_audio =
first_input_audio_of_current_segment; // pointer to channel 0 of current
// segment response
if (!driver_has_started)
return 0L;
for (int32_t i = 0; i < total_input_and_output_channels; i++) {
if (global_asio_buffer_info[i].isInput == 0) {
switch (channelInfos[i].type) {
case ASIOSTInt32LSB:
/*
Tone - all of the cards tested here are Int32LSB, including:
CardDeluxe, Gina24, Layla24, and M-Audio Delta Audiophile 2496
*/
if (!ar_asio_write_device_buffer(
global_asio_buffer_info[i].buffers[index],
lngAsioBufferSize, output_audio))
break;
break;
default:
break;
}
output_audio++;
} else {
switch (channelInfos[i].type) {
case ASIOSTInt32LSB:
if (!ar_asio_read_device_buffer(
global_asio_buffer_info[i].buffers[index],
lngAsioBufferSize, input_audio))
break;
break;
default:
break;
}
input_audio++;
}
}