File size: 117,768 Bytes
83418c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
/*
 * $Id:$
 * PortAudio Portable Real-Time Audio Library
 * Latest Version at: http://www.portaudio.com
 * AudioScience HPI implementation by Fred Gleason, Ludwig Schwardt and
 * Eliot Blennerhassett
 *
 * Copyright (c) 2003 Fred Gleason <[email protected]>
 * Copyright (c) 2005,2006 Ludwig Schwardt <[email protected]>
 * Copyright (c) 2011 Eliot Blennerhassett <[email protected]>
 *
 * Based on the Open Source API proposed by Ross Bencina
 * Copyright (c) 1999-2008 Ross Bencina, Phil Burk
 *
 * 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
 * 12/2003 - Initial version
 * 09/2005 - v19 version [rewrite]
 */

/** @file
 @ingroup hostapi_src
 @brief Host API implementation supporting AudioScience cards
        via the Linux HPI interface.

 <h3>Overview</h3>

 This is a PortAudio implementation for the AudioScience HPI Audio API
 on the Linux platform. AudioScience makes a range of audio adapters customised
 for the broadcasting industry, with support for both Windows and Linux.
 More information on their products can be found on their website:

     http://www.audioscience.com

 Documentation for the HPI API can be found at:

     http://www.audioscience.com/internet/download/sdk/hpi_usermanual_html/html/index.html

 The Linux HPI driver itself (a kernel module + library) can be downloaded from:

     http://www.audioscience.com/internet/download/linux_drivers.htm

 <h3>Implementation strategy</h3>

 *Note* Ideally, AudioScience cards should be handled by the PortAudio ALSA
 implementation on Linux, as ALSA is the preferred Linux soundcard API. The existence
 of this host API implementation might therefore seem a bit flawed. Unfortunately, at
 the time of the creation of this implementation (June 2006), the PA ALSA implementation
 could not make use of the existing AudioScience ALSA driver. PA ALSA uses the
 "memory-mapped" (mmap) ALSA access mode to interact with the ALSA library, while the
 AudioScience ALSA driver only supports the "read-write" access mode. The appropriate
 solution to this problem is to add "read-write" support to PortAudio ALSA, thereby
 extending the range of soundcards it supports (AudioScience cards are not the only
 ones with this problem). Given the author's limited knowledge of ALSA and the
 simplicity of the HPI API, the second-best solution was born...

 The following mapping between HPI and PA was followed:
 HPI subsystem => PortAudio host API
 HPI adapter => nothing specific
 HPI stream => PortAudio device

 Each HPI stream is either input or output (not both), and can support
 different channel counts, sampling rates and sample formats. It is therefore
 a more natural fit to a PA device. A PA stream can therefore combine two
 HPI streams (one input and one output) into a "full-duplex" stream. These
 HPI streams can even be on different physical adapters. The two streams ought to be
 sample-synchronised when they reside on the same adapter, as most AudioScience adapters
 derive their ADC and DAC clocks from one master clock. When combining two adapters
 into one full-duplex stream, however, the use of a word clock connection between the
 adapters is strongly recommended.

 The HPI interface is inherently blocking, making use of read and write calls to
 transfer data between user buffers and driver buffers. The callback interface therefore
 requires a helper thread ("callback engine") which periodically transfers data (one thread
 per PA stream, in fact). The current implementation explicitly sleeps via Pa_Sleep() until
 enough samples can be transferred (select() or poll() would be better, but currently seems
 impossible...). The thread implementation makes use of the Unix thread helper functions
 and some pthread calls here and there. If a unified PA thread exists, this host API
 implementation might also compile on Windows, as this is the only real Linux-specific
 part of the code.

 There is no inherent fixed buffer size in the HPI interface, as in some other host APIs.
 The PortAudio implementation contains a buffer that is allocated during OpenStream and
 used to transfer data between the callback and the HPI driver buffer. The size of this
 buffer is quite flexible and is derived from latency suggestions and matched to the
 requested callback buffer size as far as possible. It can become quite huge, as the
 AudioScience cards are typically geared towards higher-latency applications and contain
 large hardware buffers.

 The HPI interface natively supports most common sample formats and sample rates (some
 conversion is done on the adapter itself).

 Stream time is measured based on the number of processed frames, which is adjusted by the
 number of frames currently buffered by the HPI driver.

 There is basic support for detecting overflow and underflow. The HPI interface does not
 explicitly indicate this, so thresholds on buffer levels are used in combination with
 stream state. Recovery from overflow and underflow is left to the PA client.

 Blocking streams are also implemented. It makes use of the same polling routines that
 the callback interface uses, in order to prevent the allocation of variable-sized
 buffers during reading and writing. The framesPerBuffer parameter is therefore still
 relevant, and this can be increased in the blocking case to improve efficiency.

 The implementation contains extensive reporting macros (slightly modified PA_ENSURE and
 PA_UNLESS versions) and a useful stream dump routine to provide debugging feedback.

 Output buffer priming via the user callback (i.e. paPrimeOutputBuffersUsingStreamCallback
 and friends) is not implemented yet. All output is primed with silence.
 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>          /* strlen() */
#include <pthread.h>         /* pthreads and friends */
#include <assert.h>          /* assert */
#include <math.h>            /* ceil, floor */

#include <asihpi/hpi.h>      /* HPI API */

#include "portaudio.h"       /* PortAudio API */
#include "pa_util.h"         /* PA_DEBUG, other small utilities */
#include "pa_unix_util.h"    /* Unix threading utilities */
#include "pa_allocation.h"   /* Group memory allocation */
#include "pa_hostapi.h"      /* Host API structs */
#include "pa_stream.h"       /* Stream interface structs */
#include "pa_cpuload.h"      /* CPU load measurer */
#include "pa_process.h"      /* Buffer processor */
#include "pa_converters.h"   /* PaUtilZeroer */
#include "pa_debugprint.h"

/* -------------------------------------------------------------------------- */

/*
 * Defines
 */

/* Error reporting and assertions */

/** Evaluate expression, and return on any PortAudio errors */
#define PA_ENSURE_(expr) \
    do { \
        PaError paError = (expr); \
        if( UNLIKELY( paError < paNoError ) ) \
        { \
            PA_DEBUG(( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" )); \
            result = paError; \
            goto error; \
        } \
    } while (0);

/** Assert expression, else return the provided PaError */
#define PA_UNLESS_(expr, paError) \
    do { \
        if( UNLIKELY( (expr) == 0 ) ) \
        { \
            PA_DEBUG(( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" )); \
            result = (paError); \
            goto error; \
        } \
    } while( 0 );

/** Check return value of HPI function, and map it to PaError */
#define PA_ASIHPI_UNLESS_(expr, paError) \
    do { \
        hpi_err_t hpiError = (expr); \
        /* If HPI error occurred */ \
        if( UNLIKELY( hpiError ) ) \
        { \
        char szError[256]; \
        HPI_GetErrorText( hpiError, szError ); \
        PA_DEBUG(( "HPI error %d occurred: %s\n", hpiError, szError )); \
        /* This message will always be displayed, even if debug info is disabled */ \
            PA_DEBUG(( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" )); \
            if( (paError) == paUnanticipatedHostError ) \
        { \
            PA_DEBUG(( "Host error description: %s\n", szError )); \
            /* PaUtil_SetLastHostErrorInfo should only be used in the main thread */ \
            if( pthread_equal( pthread_self(), paUnixMainThread ) ) \
                { \
            PaUtil_SetLastHostErrorInfo( paInDevelopment, hpiError, szError ); \
                } \
        } \
        /* If paNoError is specified, continue as usual */ \
            /* (useful if you only want to print out the debug messages above) */ \
        if( (paError) < 0 ) \
        { \
            result = (paError); \
            goto error; \
        } \
        } \
    } while( 0 );

/** Report HPI error code and text */
#define PA_ASIHPI_REPORT_ERROR_(hpiErrorCode) \
    do { \
        char szError[256]; \
        HPI_GetErrorText( hpiError, szError ); \
        PA_DEBUG(( "HPI error %d occurred: %s\n", hpiError, szError )); \
        /* PaUtil_SetLastHostErrorInfo should only be used in the main thread */ \
        if( pthread_equal( pthread_self(), paUnixMainThread ) ) \
    { \
        PaUtil_SetLastHostErrorInfo( paInDevelopment, (hpiErrorCode), szError ); \
    } \
    } while( 0 );

/* Defaults */

/** Sample formats available natively on AudioScience hardware */
#define PA_ASIHPI_AVAILABLE_FORMATS_ (paFloat32 | paInt32 | paInt24 | paInt16 | paUInt8)
/** Enable background bus mastering (BBM) for buffer transfers, if available (see HPI docs) */
#define PA_ASIHPI_USE_BBM_ 1
/** Minimum number of frames in HPI buffer (for either data or available space).
 If buffer contains less data/space, it indicates xrun or completion. */
#define PA_ASIHPI_MIN_FRAMES_ 1152
/** Minimum polling interval in milliseconds, which determines minimum host buffer size */
#define PA_ASIHPI_MIN_POLLING_INTERVAL_ 10

/* -------------------------------------------------------------------------- */

/*
 * Structures
 */

/** Host API global data */
typedef struct PaAsiHpiHostApiRepresentation
{
    /* PortAudio "base class" - keep the baseRep first! (C-style inheritance) */
    PaUtilHostApiRepresentation baseHostApiRep;
    PaUtilStreamInterface callbackStreamInterface;
    PaUtilStreamInterface blockingStreamInterface;

    PaUtilAllocationGroup *allocations;

    /* implementation specific data goes here */

    PaHostApiIndex hostApiIndex;
}
PaAsiHpiHostApiRepresentation;


/** Device data */
typedef struct PaAsiHpiDeviceInfo
{
    /* PortAudio "base class" - keep the baseRep first! (C-style inheritance) */
    /** Common PortAudio device information */
    PaDeviceInfo baseDeviceInfo;

    /* implementation specific data goes here */

    /** Adapter index */
    uint16_t adapterIndex;
    /** Adapter model number (hex) */
    uint16_t adapterType;
    /** Adapter HW/SW version */
    uint16_t adapterVersion;
    /** Adapter serial number */
    uint32_t adapterSerialNumber;
    /** Stream number */
    uint16_t streamIndex;
    /** 0=Input, 1=Output (HPI streams are either input or output but not both) */
    uint16_t streamIsOutput;
}
PaAsiHpiDeviceInfo;


/** Stream state as defined by PortAudio.
 It seems that the host API implementation has to keep track of the PortAudio stream state.
 Please note that this is NOT the same as the state of the underlying HPI stream. By separating
 these two concepts, a lot of flexibility is gained. There is a rough match between the two,
 of course, but forcing a precise match is difficult. For example, HPI_STATE_DRAINED can occur
 during the Active state of PortAudio (due to underruns) and also during CallBackFinished in
 the case of an output stream. Similarly, HPI_STATE_STOPPED mostly coincides with the Stopped
 PortAudio state, by may also occur in the CallbackFinished state when recording is finished.

 Here is a rough match-up:

 PortAudio state   =>     HPI state
 ---------------          ---------
 Active            =>     HPI_STATE_RECORDING, HPI_STATE_PLAYING, (HPI_STATE_DRAINED)
 Stopped           =>     HPI_STATE_STOPPED
 CallbackFinished  =>     HPI_STATE_STOPPED, HPI_STATE_DRAINED */
typedef enum PaAsiHpiStreamState
{
    paAsiHpiStoppedState=0,
    paAsiHpiActiveState=1,
    paAsiHpiCallbackFinishedState=2
}
PaAsiHpiStreamState;


/** Stream component data (associated with one direction, i.e. either input or output) */
typedef struct PaAsiHpiStreamComponent
{
    /** Device information (HPI handles, etc) */
    PaAsiHpiDeviceInfo *hpiDevice;
    /** Stream handle, as passed to HPI interface. */
    hpi_handle_t hpiStream;
    /** Stream format, as passed to HPI interface */
    struct hpi_format hpiFormat;
    /** Number of bytes per frame, derived from hpiFormat and saved for convenience */
    uint32_t bytesPerFrame;
    /** Size of hardware (on-card) buffer of stream in bytes */
    uint32_t hardwareBufferSize;
    /** Size of host (BBM) buffer of stream in bytes (if used) */
    uint32_t hostBufferSize;
    /** Upper limit on the utilization of output stream buffer (both hardware and host).
     This prevents large latencies in an output-only stream with a potentially huge buffer
     and a fast data generator, which would otherwise keep the hardware buffer filled to
     capacity. See also the "Hardware Buffering=off" option in the AudioScience WAV driver. */
    uint32_t outputBufferCap;
    /** Sample buffer (halfway station between HPI and buffer processor) */
    uint8_t *tempBuffer;
    /** Sample buffer size, in bytes */
    uint32_t tempBufferSize;
}
PaAsiHpiStreamComponent;


/** Stream data */
typedef struct PaAsiHpiStream
{
    /* PortAudio "base class" - keep the baseRep first! (C-style inheritance) */
    PaUtilStreamRepresentation baseStreamRep;
    PaUtilCpuLoadMeasurer cpuLoadMeasurer;
    PaUtilBufferProcessor bufferProcessor;

    PaUtilAllocationGroup *allocations;

    /* implementation specific data goes here */

    /** Separate structs for input and output sides of stream */
    PaAsiHpiStreamComponent *input, *output;

    /** Polling interval (in milliseconds) */
    uint32_t pollingInterval;
    /** Are we running in callback mode? */
    int callbackMode;
    /** Number of frames to transfer at a time to/from HPI */
    unsigned long maxFramesPerHostBuffer;
    /** Indicates that the stream is in the paNeverDropInput mode */
    int neverDropInput;
    /** Contains copy of user buffers, used by blocking interface to transfer non-interleaved data.
     It went here instead of to each stream component, as the stream component buffer setup in
     PaAsiHpi_SetupBuffers doesn't know the stream details such as callbackMode.
     (Maybe a problem later if ReadStream and WriteStream happens concurrently on same stream.) */
    void **blockingUserBufferCopy;

    /* Thread-related variables */

    /** Helper thread which will deliver data to user callback */
    PaUnixThread thread;
    /** PortAudio stream state (Active/Stopped/CallbackFinished) */
    volatile sig_atomic_t state;
    /** Hard abort, i.e. drop frames? */
    volatile sig_atomic_t callbackAbort;
    /** True if stream stopped via exiting callback with paComplete/paAbort flag
     (as opposed to explicit call to StopStream/AbortStream) */
    volatile sig_atomic_t callbackFinished;
}
PaAsiHpiStream;


/** Stream state information, collected together for convenience */
typedef struct PaAsiHpiStreamInfo
{
    /** HPI stream state (HPI_STATE_STOPPED, HPI_STATE_PLAYING, etc.) */
    uint16_t state;
    /** Size (in bytes) of recording/playback data buffer in HPI driver */
    uint32_t bufferSize;
    /** Amount of data (in bytes) available in the buffer */
    uint32_t dataSize;
    /** Number of frames played/recorded since last stream reset */
    uint32_t frameCounter;
    /** Amount of data (in bytes) in hardware (on-card) buffer.
     This differs from dataSize if bus mastering (BBM) is used, which introduces another
     driver-level buffer to which dataSize/bufferSize then refers. */
    uint32_t auxDataSize;
    /** Total number of data frames currently buffered by HPI driver (host + hw buffers) */
    uint32_t totalBufferedData;
    /** Size of immediately available data (for input) or space (for output) in frames.
     This only checks the first-level buffer (typically host buffer). This amount can be
     transferred immediately. */
    uint32_t availableFrames;
    /** Indicates that hardware buffer is getting too full */
    int overflow;
    /** Indicates that hardware buffer is getting too empty */
    int underflow;
}
PaAsiHpiStreamInfo;

/* -------------------------------------------------------------------------- */

/*
 * Function prototypes
 */

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */

    /* The only exposed function in the entire host API implementation */
    PaError PaAsiHpi_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiIndex index );

#ifdef __cplusplus
}
#endif /* __cplusplus */

static void Terminate( struct PaUtilHostApiRepresentation *hostApi );
static PaError IsFormatSupported( struct PaUtilHostApiRepresentation *hostApi,
                                  const PaStreamParameters *inputParameters,
                                  const PaStreamParameters *outputParameters,
                                  double sampleRate );

/* Stream prototypes */
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 CloseStream( PaStream *s );
static PaError StartStream( PaStream *s );
static PaError StopStream( PaStream *s );
static PaError AbortStream( PaStream *s );
static PaError IsStreamStopped( PaStream *s );
static PaError IsStreamActive( PaStream *s );
static PaTime GetStreamTime( PaStream *s );
static double GetStreamCpuLoad( PaStream *s );

/* Blocking prototypes */
static PaError ReadStream( PaStream *s, void *buffer, unsigned long frames );
static PaError WriteStream( PaStream *s, const void *buffer, unsigned long frames );
static signed long GetStreamReadAvailable( PaStream *s );
static signed long GetStreamWriteAvailable( PaStream *s );

/* Callback prototypes */
static void *CallbackThreadFunc( void *userData );

/* Functions specific to this API */
static PaError PaAsiHpi_BuildDeviceList( PaAsiHpiHostApiRepresentation *hpiHostApi );
static uint16_t PaAsiHpi_PaToHpiFormat( PaSampleFormat paFormat );
static PaSampleFormat PaAsiHpi_HpiToPaFormat( uint16_t hpiFormat );
static PaError PaAsiHpi_CreateFormat( struct PaUtilHostApiRepresentation *hostApi,
                                      const PaStreamParameters *parameters, double sampleRate,
                                      PaAsiHpiDeviceInfo **hpiDevice, struct hpi_format *hpiFormat );
static PaError PaAsiHpi_OpenInput( struct PaUtilHostApiRepresentation *hostApi,
                                   const PaAsiHpiDeviceInfo *hpiDevice, const struct hpi_format *hpiFormat,
                                   hpi_handle_t *hpiStream );
static PaError PaAsiHpi_OpenOutput( struct PaUtilHostApiRepresentation *hostApi,
                                    const PaAsiHpiDeviceInfo *hpiDevice, const struct hpi_format *hpiFormat,
                                    hpi_handle_t *hpiStream );
static PaError PaAsiHpi_GetStreamInfo( PaAsiHpiStreamComponent *streamComp, PaAsiHpiStreamInfo *info );
static void PaAsiHpi_StreamComponentDump( PaAsiHpiStreamComponent *streamComp, PaAsiHpiStream *stream );
static void PaAsiHpi_StreamDump( PaAsiHpiStream *stream );
static PaError PaAsiHpi_SetupBuffers( PaAsiHpiStreamComponent *streamComp, uint32_t pollingInterval,
                                      unsigned long framesPerPaHostBuffer, PaTime suggestedLatency );
static PaError PaAsiHpi_PrimeOutputWithSilence( PaAsiHpiStream *stream );
static PaError PaAsiHpi_StartStream( PaAsiHpiStream *stream, int outputPrimed );
static PaError PaAsiHpi_StopStream( PaAsiHpiStream *stream, int abort );
static PaError PaAsiHpi_ExplicitStop( PaAsiHpiStream *stream, int abort );
static void PaAsiHpi_OnThreadExit( void *userData );
static PaError PaAsiHpi_WaitForFrames( PaAsiHpiStream *stream, unsigned long *framesAvail,
                                       PaStreamCallbackFlags *cbFlags );
static void PaAsiHpi_CalculateTimeInfo( PaAsiHpiStream *stream, PaStreamCallbackTimeInfo *timeInfo );
static PaError PaAsiHpi_BeginProcessing( PaAsiHpiStream* stream, unsigned long* numFrames,
        PaStreamCallbackFlags *cbFlags );
static PaError PaAsiHpi_EndProcessing( PaAsiHpiStream *stream, unsigned long numFrames,
                                       PaStreamCallbackFlags *cbFlags );

/* ==========================================================================
 * ============================= IMPLEMENTATION =============================
 * ========================================================================== */

/* --------------------------- Host API Interface --------------------------- */

/** Enumerate all PA devices (= HPI streams).
 This compiles a list of all HPI adapters, and registers a PA device for each input and
 output stream it finds. Most errors are ignored, as missing or erroneous devices are
 simply skipped.

 @param hpiHostApi Pointer to HPI host API struct

 @return PortAudio error code (only paInsufficientMemory in practice)
 */
static PaError PaAsiHpi_BuildDeviceList( PaAsiHpiHostApiRepresentation *hpiHostApi )
{
    PaError result = paNoError;
    PaUtilHostApiRepresentation *hostApi = &hpiHostApi->baseHostApiRep;
    PaHostApiInfo *baseApiInfo = &hostApi->info;
    PaAsiHpiDeviceInfo *hpiDeviceList;
    int numAdapters;
    hpi_err_t hpiError = 0;
    int i, j, deviceCount = 0, deviceIndex = 0;

    assert( hpiHostApi );

    /* Errors not considered critical here (subsystem may report 0 devices), but report them */
    /* in debug mode. */
    PA_ASIHPI_UNLESS_( HPI_SubSysGetNumAdapters( NULL, &numAdapters), paNoError );

    for( i=0; i < numAdapters; ++i )
    {
        uint16_t inStreams, outStreams;
        uint16_t version;
        uint32_t serial;
        uint16_t type;
        uint32_t idx;

        hpiError = HPI_SubSysGetAdapter(NULL, i, &idx, &type);
        if (hpiError)
            continue;

        /* Try to open adapter */
        hpiError = HPI_AdapterOpen( NULL, idx );
        /* Report error and skip to next device on failure */
        if( hpiError )
        {
            PA_ASIHPI_REPORT_ERROR_( hpiError );
            continue;
        }
        hpiError = HPI_AdapterGetInfo( NULL, idx, &outStreams, &inStreams,
                                       &version, &serial, &type );
        /* Skip to next device on failure */
        if( hpiError )
        {
            PA_ASIHPI_REPORT_ERROR_( hpiError );
            continue;
        }
        else
        {
            /* Assign default devices if available and increment device count */
            if( (baseApiInfo->defaultInputDevice == paNoDevice) && (inStreams > 0) )
                baseApiInfo->defaultInputDevice = deviceCount;
            deviceCount += inStreams;
            if( (baseApiInfo->defaultOutputDevice == paNoDevice) && (outStreams > 0) )
                baseApiInfo->defaultOutputDevice = deviceCount;
            deviceCount += outStreams;
        }
    }

    /* Register any discovered devices */
    if( deviceCount > 0 )
    {
        /* Memory allocation */
        PA_UNLESS_( hostApi->deviceInfos = (PaDeviceInfo**) PaUtil_GroupAllocateMemory(
                                               hpiHostApi->allocations, sizeof(PaDeviceInfo*) * deviceCount ),
                    paInsufficientMemory );
        /* Allocate all device info structs in a contiguous block */
        PA_UNLESS_( hpiDeviceList = (PaAsiHpiDeviceInfo*) PaUtil_GroupAllocateMemory(
                                        hpiHostApi->allocations, sizeof(PaAsiHpiDeviceInfo) * deviceCount ),
                    paInsufficientMemory );

        /* Now query devices again for information */
        for( i=0; i < numAdapters; ++i )
        {
            uint16_t inStreams, outStreams;
            uint16_t version;
            uint32_t serial;
            uint16_t type;
            uint32_t idx;

            hpiError = HPI_SubSysGetAdapter( NULL, i, &idx, &type );
            if (hpiError)
                continue;

            /* Assume adapter is still open from previous round */
            hpiError = HPI_AdapterGetInfo( NULL, idx,
                                           &outStreams, &inStreams, &version, &serial, &type );
            /* Report error and skip to next device on failure */
            if( hpiError )
            {
                PA_ASIHPI_REPORT_ERROR_( hpiError );
                continue;
            }
            else
            {
                PA_DEBUG(( "Found HPI Adapter ID=%4X Idx=%d #In=%d #Out=%d S/N=%d HWver=%c%d DSPver=%03d\n",
                           type, idx, inStreams, outStreams, serial,
                           ((version>>3)&0xf)+'A',                  /* Hw version major */
                           version&0x7,                             /* Hw version minor */
                           ((version>>13)*100)+((version>>7)&0x3f)  /* DSP code version */
                         ));
            }

            /* First add all input streams as devices */
            for( j=0; j < inStreams; ++j )
            {
                PaAsiHpiDeviceInfo *hpiDevice = &hpiDeviceList[deviceIndex];
                PaDeviceInfo *baseDeviceInfo = &hpiDevice->baseDeviceInfo;
                char srcName[72];
                char *deviceName;

                memset( hpiDevice, 0, sizeof(PaAsiHpiDeviceInfo) );
                /* Set implementation-specific device details */
                hpiDevice->adapterIndex = idx;
                hpiDevice->adapterType = type;
                hpiDevice->adapterVersion = version;
                hpiDevice->adapterSerialNumber = serial;
                hpiDevice->streamIndex = j;
                hpiDevice->streamIsOutput = 0;
                /* Set common PortAudio device stats */
                baseDeviceInfo->structVersion = 2;
                /* Make sure name string is owned by API info structure */
                sprintf( srcName,
                         "Adapter %d (%4X) - Input Stream %d", i+1, type, j+1 );
                PA_UNLESS_( deviceName = (char *) PaUtil_GroupAllocateMemory(
                                             hpiHostApi->allocations, strlen(srcName) + 1 ), paInsufficientMemory );
                strcpy( deviceName, srcName );
                baseDeviceInfo->name = deviceName;
                baseDeviceInfo->hostApi = hpiHostApi->hostApiIndex;
                baseDeviceInfo->maxInputChannels = HPI_MAX_CHANNELS;
                baseDeviceInfo->maxOutputChannels = 0;
                /* Default latency values for interactive performance */
                baseDeviceInfo->defaultLowInputLatency = 0.01;
                baseDeviceInfo->defaultLowOutputLatency = -1.0;
                /* Default latency values for robust non-interactive applications (eg. playing sound files) */
                baseDeviceInfo->defaultHighInputLatency = 0.2;
                baseDeviceInfo->defaultHighOutputLatency = -1.0;
                /* HPI interface can actually handle any sampling rate to 1 Hz accuracy,
                * so this default is as good as any */
                baseDeviceInfo->defaultSampleRate = 44100;

                /* Store device in global PortAudio list */
                hostApi->deviceInfos[deviceIndex++] = (PaDeviceInfo *) hpiDevice;
            }

            /* Now add all output streams as devices (I know, the repetition is painful) */
            for( j=0; j < outStreams; ++j )
            {
                PaAsiHpiDeviceInfo *hpiDevice = &hpiDeviceList[deviceIndex];
                PaDeviceInfo *baseDeviceInfo = &hpiDevice->baseDeviceInfo;
                char srcName[72];
                char *deviceName;

                memset( hpiDevice, 0, sizeof(PaAsiHpiDeviceInfo) );
                /* Set implementation-specific device details */
                hpiDevice->adapterIndex = idx;
                hpiDevice->adapterType = type;
                hpiDevice->adapterVersion = version;
                hpiDevice->adapterSerialNumber = serial;
                hpiDevice->streamIndex = j;
                hpiDevice->streamIsOutput = 1;
                /* Set common PortAudio device stats */
                baseDeviceInfo->structVersion = 2;
                /* Make sure name string is owned by API info structure */
                sprintf( srcName,
                         "Adapter %d (%4X) - Output Stream %d", i+1, type, j+1 );
                PA_UNLESS_( deviceName = (char *) PaUtil_GroupAllocateMemory(
                                             hpiHostApi->allocations, strlen(srcName) + 1 ), paInsufficientMemory );
                strcpy( deviceName, srcName );
                baseDeviceInfo->name = deviceName;
                baseDeviceInfo->hostApi = hpiHostApi->hostApiIndex;
                baseDeviceInfo->maxInputChannels = 0;
                baseDeviceInfo->maxOutputChannels = HPI_MAX_CHANNELS;
                /* Default latency values for interactive performance. */
                baseDeviceInfo->defaultLowInputLatency = -1.0;
                baseDeviceInfo->defaultLowOutputLatency = 0.01;
                /* Default latency values for robust non-interactive applications (eg. playing sound files). */
                baseDeviceInfo->defaultHighInputLatency = -1.0;
                baseDeviceInfo->defaultHighOutputLatency = 0.2;
                /* HPI interface can actually handle any sampling rate to 1 Hz accuracy,
                * so this default is as good as any */
                baseDeviceInfo->defaultSampleRate = 44100;

                /* Store device in global PortAudio list */
                hostApi->deviceInfos[deviceIndex++] = (PaDeviceInfo *) hpiDevice;
            }
        }
    }

    /* Finally acknowledge checked devices */
    baseApiInfo->deviceCount = deviceIndex;

error:
    return result;
}


/** Initialize host API implementation.
 This is the only function exported beyond this file. It is called by PortAudio to initialize
 the host API. It stores API info, finds and registers all devices, and sets up callback and
 blocking interfaces.

 @param hostApi Pointer to host API struct

 @param hostApiIndex Index of current (HPI) host API

 @return PortAudio error code
 */
PaError PaAsiHpi_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiIndex hostApiIndex )
{
    PaError result = paNoError;
    PaAsiHpiHostApiRepresentation *hpiHostApi = NULL;
    PaHostApiInfo *baseApiInfo;

    /* Try to initialize HPI subsystem */
    if (!HPI_SubSysCreate())
    {
        /* the V19 development docs say that if an implementation
         * detects that it cannot be used, it should return a NULL
         * interface and paNoError */
        PA_DEBUG(( "Could not open HPI interface\n" ));

        *hostApi = NULL;
        return paNoError;
    }
    else
    {
        uint32_t hpiVersion;
        PA_ASIHPI_UNLESS_( HPI_SubSysGetVersionEx( NULL, &hpiVersion ), paUnanticipatedHostError );
        PA_DEBUG(( "HPI interface v%d.%02d.%02d\n",
                   hpiVersion >> 16,  (hpiVersion >> 8) & 0x0F, (hpiVersion & 0x0F) ));
    }

    /* Allocate host API structure */
    PA_UNLESS_( hpiHostApi = (PaAsiHpiHostApiRepresentation*) PaUtil_AllocateMemory(
                                 sizeof(PaAsiHpiHostApiRepresentation) ), paInsufficientMemory );
    PA_UNLESS_( hpiHostApi->allocations = PaUtil_CreateAllocationGroup(), paInsufficientMemory );

    hpiHostApi->hostApiIndex = hostApiIndex;

    *hostApi = &hpiHostApi->baseHostApiRep;
    baseApiInfo = &((*hostApi)->info);
    /* Fill in common API details */
    baseApiInfo->structVersion = 1;
    baseApiInfo->type = paAudioScienceHPI;
    baseApiInfo->name = "AudioScience HPI";
    baseApiInfo->deviceCount = 0;
    baseApiInfo->defaultInputDevice = paNoDevice;
    baseApiInfo->defaultOutputDevice = paNoDevice;

    PA_ENSURE_( PaAsiHpi_BuildDeviceList( hpiHostApi ) );

    (*hostApi)->Terminate = Terminate;
    (*hostApi)->OpenStream = OpenStream;
    (*hostApi)->IsFormatSupported = IsFormatSupported;

    PaUtil_InitializeStreamInterface( &hpiHostApi->callbackStreamInterface, CloseStream, StartStream,
                                      StopStream, AbortStream, IsStreamStopped, IsStreamActive,
                                      GetStreamTime, GetStreamCpuLoad,
                                      PaUtil_DummyRead, PaUtil_DummyWrite,
                                      PaUtil_DummyGetReadAvailable, PaUtil_DummyGetWriteAvailable );

    PaUtil_InitializeStreamInterface( &hpiHostApi->blockingStreamInterface, CloseStream, StartStream,
                                      StopStream, AbortStream, IsStreamStopped, IsStreamActive,
                                      GetStreamTime, PaUtil_DummyGetCpuLoad,
                                      ReadStream, WriteStream, GetStreamReadAvailable, GetStreamWriteAvailable );

    /* Store identity of main thread */
    PA_ENSURE_( PaUnixThreading_Initialize() );

    return result;
error:
    if (hpiHostApi)
        PaUtil_FreeMemory( hpiHostApi );
    return result;
}


/** Terminate host API implementation.
 This closes all HPI adapters and frees the HPI subsystem. It also frees the host API struct
 memory. It should be called once for every PaAsiHpi_Initialize call.

 @param Pointer to host API struct
 */
static void Terminate( struct PaUtilHostApiRepresentation *hostApi )
{
    PaAsiHpiHostApiRepresentation *hpiHostApi = (PaAsiHpiHostApiRepresentation*)hostApi;
    int i;
    PaError result = paNoError;

    if( hpiHostApi )
    {
        /* Get rid of HPI-specific structures */
        uint16_t lastAdapterIndex = HPI_MAX_ADAPTERS;
        /* Iterate through device list and close adapters */
        for( i=0; i < hostApi->info.deviceCount; ++i )
        {
            PaAsiHpiDeviceInfo *hpiDevice = (PaAsiHpiDeviceInfo *) hostApi->deviceInfos[ i ];
            /* Close adapter only if it differs from previous one */
            if( hpiDevice->adapterIndex != lastAdapterIndex )
            {
                /* Ignore errors (report only during debugging) */
                PA_ASIHPI_UNLESS_( HPI_AdapterClose( NULL,
                                                     hpiDevice->adapterIndex ), paNoError );
                lastAdapterIndex = hpiDevice->adapterIndex;
            }
        }
        /* Finally dismantle HPI subsystem */
        HPI_SubSysFree( NULL );

        if( hpiHostApi->allocations )
        {
            PaUtil_FreeAllAllocations( hpiHostApi->allocations );
            PaUtil_DestroyAllocationGroup( hpiHostApi->allocations );
        }

        PaUtil_FreeMemory( hpiHostApi );
    }
error:
    return;
}


/** Converts PortAudio sample format to equivalent HPI format.

 @param paFormat PortAudio sample format

 @return HPI sample format
 */
static uint16_t PaAsiHpi_PaToHpiFormat( PaSampleFormat paFormat )
{
    /* Ignore interleaving flag */
    switch( paFormat & ~paNonInterleaved )
    {
    case paFloat32:
        return HPI_FORMAT_PCM32_FLOAT;

    case paInt32:
        return HPI_FORMAT_PCM32_SIGNED;

    case paInt24:
        return HPI_FORMAT_PCM24_SIGNED;

    case paInt16:
        return HPI_FORMAT_PCM16_SIGNED;

    case paUInt8:
        return HPI_FORMAT_PCM8_UNSIGNED;

        /* Default is 16-bit signed */
    case paInt8:
    default:
        return HPI_FORMAT_PCM16_SIGNED;
    }
}


/** Converts HPI sample format to equivalent PortAudio format.

 @param paFormat HPI sample format

 @return PortAudio sample format
 */
static PaSampleFormat PaAsiHpi_HpiToPaFormat( uint16_t hpiFormat )
{
    switch( hpiFormat )
    {
    case HPI_FORMAT_PCM32_FLOAT:
        return paFloat32;

    case HPI_FORMAT_PCM32_SIGNED:
        return paInt32;

    case HPI_FORMAT_PCM24_SIGNED:
        return paInt24;

    case HPI_FORMAT_PCM16_SIGNED:
        return paInt16;

    case HPI_FORMAT_PCM8_UNSIGNED:
        return paUInt8;

        /* Default is custom format (e.g. for HPI MP3 format) */
    default:
        return paCustomFormat;
    }
}


/** Creates HPI format struct based on PortAudio parameters.
 This also does some checks to see whether the desired format is valid, and whether
 the device allows it. This only checks the format of one half (input or output) of the
 PortAudio stream.

 @param hostApi Pointer to host API struct

 @param parameters Pointer to stream parameter struct

 @param sampleRate Desired sample rate

 @param hpiDevice Pointer to HPI device struct

 @param hpiFormat Resulting HPI format returned here

 @return PortAudio error code (typically indicating a problem with stream format)
 */
static PaError PaAsiHpi_CreateFormat( struct PaUtilHostApiRepresentation *hostApi,
                                      const PaStreamParameters *parameters, double sampleRate,
                                      PaAsiHpiDeviceInfo **hpiDevice, struct hpi_format *hpiFormat )
{
    int maxChannelCount = 0;
    PaSampleFormat hostSampleFormat = 0;
    hpi_err_t hpiError = 0;

    /* Unless alternate device specification is supported, reject the use of
       paUseHostApiSpecificDeviceSpecification */
    if( parameters->device == paUseHostApiSpecificDeviceSpecification )
        return paInvalidDevice;
    else
    {
        assert( parameters->device < hostApi->info.deviceCount );
        *hpiDevice = (PaAsiHpiDeviceInfo*) hostApi->deviceInfos[ parameters->device ];
    }

    /* Validate streamInfo - this implementation doesn't use custom stream info */
    if( parameters->hostApiSpecificStreamInfo )
        return paIncompatibleHostApiSpecificStreamInfo;

    /* Check that device can support channel count */
    if( (*hpiDevice)->streamIsOutput )
    {
        maxChannelCount = (*hpiDevice)->baseDeviceInfo.maxOutputChannels;
    }
    else
    {
        maxChannelCount = (*hpiDevice)->baseDeviceInfo.maxInputChannels;
    }
    if( (maxChannelCount == 0) || (parameters->channelCount > maxChannelCount) )
        return paInvalidChannelCount;

    /* All standard sample formats are supported by the buffer adapter,
       and this implementation doesn't support any custom sample formats */
    if( parameters->sampleFormat & paCustomFormat )
        return paSampleFormatNotSupported;

    /* Switch to closest HPI native format */
    hostSampleFormat = PaUtil_SelectClosestAvailableFormat(PA_ASIHPI_AVAILABLE_FORMATS_,
                       parameters->sampleFormat );
    /* Setup format + info objects */
    hpiError = HPI_FormatCreate( hpiFormat, (uint16_t)parameters->channelCount,
                                 PaAsiHpi_PaToHpiFormat( hostSampleFormat ),
                                 (uint32_t)sampleRate, 0, 0 );
    if( hpiError )
    {
        PA_ASIHPI_REPORT_ERROR_( hpiError );
        switch( hpiError )
        {
        case HPI_ERROR_INVALID_FORMAT:
            return paSampleFormatNotSupported;

        case HPI_ERROR_INVALID_SAMPLERATE:
        case HPI_ERROR_INCOMPATIBLE_SAMPLERATE:
            return paInvalidSampleRate;

        case HPI_ERROR_INVALID_CHANNELS:
            return paInvalidChannelCount;
        }
    }

    return paNoError;
}


/** Open HPI input stream with given format.
 This attempts to open HPI input stream with desired format. If the format is not supported
 or the device is unavailable, the stream is closed and a PortAudio error code is returned.

 @param hostApi Pointer to host API struct

 @param hpiDevice Pointer to HPI device struct

 @param hpiFormat Pointer to HPI format struct

 @return PortAudio error code (typically indicating a problem with stream format or device)
*/
static PaError PaAsiHpi_OpenInput( struct PaUtilHostApiRepresentation *hostApi,
                                   const PaAsiHpiDeviceInfo *hpiDevice, const struct hpi_format *hpiFormat,
                                   hpi_handle_t *hpiStream )
{
    PaAsiHpiHostApiRepresentation *hpiHostApi = (PaAsiHpiHostApiRepresentation*)hostApi;
    PaError result = paNoError;
    hpi_err_t hpiError = 0;

    /* Catch misplaced output devices, as they typically have 0 input channels */
    PA_UNLESS_( !hpiDevice->streamIsOutput, paInvalidChannelCount );
    /* Try to open input stream */
    PA_ASIHPI_UNLESS_( HPI_InStreamOpen( NULL, hpiDevice->adapterIndex,
                                         hpiDevice->streamIndex, hpiStream ), paDeviceUnavailable );
    /* Set input format (checking it in the process) */
    /* Could also use HPI_InStreamQueryFormat, but this economizes the process */
    hpiError = HPI_InStreamSetFormat( NULL, *hpiStream, (struct hpi_format*)hpiFormat );
    if( hpiError )
    {
        PA_ASIHPI_REPORT_ERROR_( hpiError );
        PA_ASIHPI_UNLESS_( HPI_InStreamClose( NULL, *hpiStream ), paNoError );
        switch( hpiError )
        {
        case HPI_ERROR_INVALID_FORMAT:
            return paSampleFormatNotSupported;

        case HPI_ERROR_INVALID_SAMPLERATE:
        case HPI_ERROR_INCOMPATIBLE_SAMPLERATE:
            return paInvalidSampleRate;

        case HPI_ERROR_INVALID_CHANNELS:
            return paInvalidChannelCount;

        default:
            /* In case anything else went wrong */
            return paInvalidDevice;
        }
    }

error:
    return result;
}


/** Open HPI output stream with given format.
 This attempts to open HPI output stream with desired format. If the format is not supported
 or the device is unavailable, the stream is closed and a PortAudio error code is returned.

 @param hostApi Pointer to host API struct

 @param hpiDevice Pointer to HPI device struct

 @param hpiFormat Pointer to HPI format struct

 @return PortAudio error code (typically indicating a problem with stream format or device)
*/
static PaError PaAsiHpi_OpenOutput( struct PaUtilHostApiRepresentation *hostApi,
                                    const PaAsiHpiDeviceInfo *hpiDevice, const struct hpi_format *hpiFormat,
                                    hpi_handle_t *hpiStream )
{
    PaAsiHpiHostApiRepresentation *hpiHostApi = (PaAsiHpiHostApiRepresentation*)hostApi;
    PaError result = paNoError;
    hpi_err_t hpiError = 0;

    /* Catch misplaced input devices, as they typically have 0 output channels */
    PA_UNLESS_( hpiDevice->streamIsOutput, paInvalidChannelCount );
    /* Try to open output stream */
    PA_ASIHPI_UNLESS_( HPI_OutStreamOpen( NULL, hpiDevice->adapterIndex,
                                          hpiDevice->streamIndex, hpiStream ), paDeviceUnavailable );

    /* Check output format (format is set on first write to output stream) */
    hpiError = HPI_OutStreamQueryFormat( NULL, *hpiStream, (struct hpi_format*)hpiFormat );
    if( hpiError )
    {
        PA_ASIHPI_REPORT_ERROR_( hpiError );
        PA_ASIHPI_UNLESS_( HPI_OutStreamClose( NULL, *hpiStream ), paNoError );
        switch( hpiError )
        {
        case HPI_ERROR_INVALID_FORMAT:
            return paSampleFormatNotSupported;

        case HPI_ERROR_INVALID_SAMPLERATE:
        case HPI_ERROR_INCOMPATIBLE_SAMPLERATE:
            return paInvalidSampleRate;

        case HPI_ERROR_INVALID_CHANNELS:
            return paInvalidChannelCount;

        default:
            /* In case anything else went wrong */
            return paInvalidDevice;
        }
    }

error:
    return result;
}


/** Checks whether the desired stream formats and devices are supported
 (for both input and output).
 This is done by actually opening the appropriate HPI streams and closing them again.

 @param hostApi Pointer to host API struct

 @param inputParameters Pointer to stream parameter struct for input side of stream

 @param outputParameters Pointer to stream parameter struct for output side of stream

 @param sampleRate Desired sample rate

 @return PortAudio error code (paFormatIsSupported on success)
 */
static PaError IsFormatSupported( struct PaUtilHostApiRepresentation *hostApi,
                                  const PaStreamParameters *inputParameters,
                                  const PaStreamParameters *outputParameters,
                                  double sampleRate )
{
    PaError result = paFormatIsSupported;
    PaAsiHpiHostApiRepresentation *hpiHostApi = (PaAsiHpiHostApiRepresentation*)hostApi;
    PaAsiHpiDeviceInfo *hpiDevice = NULL;
    struct hpi_format hpiFormat;

    /* Input stream */
    if( inputParameters )
    {
        hpi_handle_t hpiStream;
        PA_DEBUG(( "%s: Checking input params: dev=%d, sr=%d, chans=%d, fmt=%d\n",
                   __FUNCTION__, inputParameters->device, (int)sampleRate,
                   inputParameters->channelCount, inputParameters->sampleFormat ));
        /* Create and validate format */
        PA_ENSURE_( PaAsiHpi_CreateFormat( hostApi, inputParameters, sampleRate,
                                           &hpiDevice, &hpiFormat ) );
        /* Open stream to further check format */
        PA_ENSURE_( PaAsiHpi_OpenInput( hostApi, hpiDevice, &hpiFormat, &hpiStream ) );
        /* Close stream again */
        PA_ASIHPI_UNLESS_( HPI_InStreamClose( NULL, hpiStream ), paNoError );
    }

    /* Output stream */
    if( outputParameters )
    {
        hpi_handle_t hpiStream;
        PA_DEBUG(( "%s: Checking output params: dev=%d, sr=%d, chans=%d, fmt=%d\n",
                   __FUNCTION__, outputParameters->device, (int)sampleRate,
                   outputParameters->channelCount, outputParameters->sampleFormat ));
        /* Create and validate format */
        PA_ENSURE_( PaAsiHpi_CreateFormat( hostApi, outputParameters, sampleRate,
                                           &hpiDevice, &hpiFormat ) );
        /* Open stream to further check format */
        PA_ENSURE_( PaAsiHpi_OpenOutput( hostApi, hpiDevice, &hpiFormat, &hpiStream ) );
        /* Close stream again */
        PA_ASIHPI_UNLESS_( HPI_OutStreamClose( NULL, hpiStream ), paNoError );
    }

error:
    return result;
}

/* ---------------------------- Stream Interface ---------------------------- */

/** Obtain HPI stream information.
 This obtains info such as stream state and available data/space in buffers. It also
 estimates whether an underflow or overflow occurred.

 @param streamComp Pointer to stream component (input or output) to query

 @param info Pointer to stream info struct that will contain result

 @return PortAudio error code (either paNoError, paDeviceUnavailable or paUnanticipatedHostError)
 */
static PaError PaAsiHpi_GetStreamInfo( PaAsiHpiStreamComponent *streamComp, PaAsiHpiStreamInfo *info )
{
    PaError result = paDeviceUnavailable;
    uint16_t state;
    uint32_t bufferSize, dataSize, frameCounter, auxDataSize, threshold;
    uint32_t hwBufferSize, hwDataSize;

    assert( streamComp );
    assert( info );

    /* First blank the stream info struct, in case something goes wrong below.
       This saves the caller from initializing the struct. */
    info->state = 0;
    info->bufferSize = 0;
    info->dataSize = 0;
    info->frameCounter = 0;
    info->auxDataSize = 0;
    info->totalBufferedData = 0;
    info->availableFrames = 0;
    info->underflow = 0;
    info->overflow = 0;

    if( streamComp->hpiDevice && streamComp->hpiStream )
    {
        /* Obtain detailed stream info (either input or output) */
        if( streamComp->hpiDevice->streamIsOutput )
        {
            PA_ASIHPI_UNLESS_( HPI_OutStreamGetInfoEx( NULL,
                               streamComp->hpiStream,
                               &state, &bufferSize, &dataSize, &frameCounter,
                               &auxDataSize ), paUnanticipatedHostError );
        }
        else
        {
            PA_ASIHPI_UNLESS_( HPI_InStreamGetInfoEx( NULL,
                               streamComp->hpiStream,
                               &state, &bufferSize, &dataSize, &frameCounter,
                               &auxDataSize ), paUnanticipatedHostError );
        }
        /* Load stream info */
        info->state = state;
        info->bufferSize = bufferSize;
        info->dataSize = dataSize;
        info->frameCounter = frameCounter;
        info->auxDataSize = auxDataSize;
        /* Determine total buffered data */
        info->totalBufferedData = dataSize;
        if( streamComp->hostBufferSize > 0 )
            info->totalBufferedData += auxDataSize;
        info->totalBufferedData /= streamComp->bytesPerFrame;
        /* Determine immediately available frames */
        info->availableFrames = streamComp->hpiDevice->streamIsOutput ?
                                bufferSize - dataSize : dataSize;
        info->availableFrames /= streamComp->bytesPerFrame;
        /* Minimum space/data required in buffers */
        threshold = PA_MIN( streamComp->tempBufferSize,
                            streamComp->bytesPerFrame * PA_ASIHPI_MIN_FRAMES_ );
        /* Obtain hardware buffer stats first, to simplify things */
        hwBufferSize = streamComp->hardwareBufferSize;
        hwDataSize = streamComp->hostBufferSize > 0 ? auxDataSize : dataSize;
        /* Underflow is a bit tricky */
        info->underflow = streamComp->hpiDevice->streamIsOutput ?
                          /* Stream seems to start in drained state sometimes, so ignore initial underflow */
                          (frameCounter > 0) && ( (state == HPI_STATE_DRAINED) || (hwDataSize == 0) ) :
                          /* Input streams check the first-level (host) buffer for underflow */
                          (state != HPI_STATE_STOPPED) && (dataSize < threshold);
        /* Check for overflow in second-level (hardware) buffer for both input and output */
        info->overflow = (state != HPI_STATE_STOPPED) && (hwBufferSize - hwDataSize < threshold);

        return paNoError;
    }

error:
    return result;
}


/** Display stream component information for debugging purposes.

 @param streamComp Pointer to stream component (input or output) to query

 @param stream Pointer to stream struct which contains the component above
 */
static void PaAsiHpi_StreamComponentDump( PaAsiHpiStreamComponent *streamComp,
        PaAsiHpiStream *stream )
{
    PaAsiHpiStreamInfo streamInfo;

    assert( streamComp );
    assert( stream );

    /* Name of soundcard/device used by component */
    PA_DEBUG(( "device: %s\n", streamComp->hpiDevice->baseDeviceInfo.name ));
    /* Unfortunately some overlap between input and output here */
    if( streamComp->hpiDevice->streamIsOutput )
    {
        /* Settings on the user side (as experienced by user callback) */
        PA_DEBUG(( "user: %d-bit, %d ",
                   8*stream->bufferProcessor.bytesPerUserOutputSample,
                   stream->bufferProcessor.outputChannelCount));
        if( stream->bufferProcessor.userOutputIsInterleaved )
        {
            PA_DEBUG(( "interleaved channels, " ));
        }
        else
        {
            PA_DEBUG(( "non-interleaved channels, " ));
        }
        PA_DEBUG(( "%d frames/buffer, latency = %5.1f ms\n",
                   stream->bufferProcessor.framesPerUserBuffer,
                   1000*stream->baseStreamRep.streamInfo.outputLatency ));
        /* Settings on the host side (internal to PortAudio host API) */
        PA_DEBUG(( "host: %d-bit, %d interleaved channels, %d frames/buffer ",
                   8*stream->bufferProcessor.bytesPerHostOutputSample,
                   stream->bufferProcessor.outputChannelCount,
                   stream->bufferProcessor.framesPerHostBuffer ));
    }
    else
    {
        /* Settings on the user side (as experienced by user callback) */
        PA_DEBUG(( "user: %d-bit, %d ",
                   8*stream->bufferProcessor.bytesPerUserInputSample,
                   stream->bufferProcessor.inputChannelCount));
        if( stream->bufferProcessor.userInputIsInterleaved )
        {
            PA_DEBUG(( "interleaved channels, " ));
        }
        else
        {
            PA_DEBUG(( "non-interleaved channels, " ));
        }
        PA_DEBUG(( "%d frames/buffer, latency = %5.1f ms\n",
                   stream->bufferProcessor.framesPerUserBuffer,
                   1000*stream->baseStreamRep.streamInfo.inputLatency ));
        /* Settings on the host side (internal to PortAudio host API) */
        PA_DEBUG(( "host: %d-bit, %d interleaved channels, %d frames/buffer ",
                   8*stream->bufferProcessor.bytesPerHostInputSample,
                   stream->bufferProcessor.inputChannelCount,
                   stream->bufferProcessor.framesPerHostBuffer ));
    }
    switch( stream->bufferProcessor.hostBufferSizeMode )
    {
    case paUtilFixedHostBufferSize:
        PA_DEBUG(( "[fixed] " ));
        break;
    case paUtilBoundedHostBufferSize:
        PA_DEBUG(( "[bounded] " ));
        break;
    case paUtilUnknownHostBufferSize:
        PA_DEBUG(( "[unknown] " ));
        break;
    case paUtilVariableHostBufferSizePartialUsageAllowed:
        PA_DEBUG(( "[variable] " ));
        break;
    }
    PA_DEBUG(( "(%d max)\n", streamComp->tempBufferSize / streamComp->bytesPerFrame ));
    /* HPI hardware settings */
    PA_DEBUG(( "HPI: adapter %d stream %d, %d-bit, %d-channel, %d Hz\n",
               streamComp->hpiDevice->adapterIndex, streamComp->hpiDevice->streamIndex,
               8 * streamComp->bytesPerFrame / streamComp->hpiFormat.wChannels,
               streamComp->hpiFormat.wChannels,
               streamComp->hpiFormat.dwSampleRate ));
    /* Stream state and buffer levels */
    PA_DEBUG(( "HPI: " ));
    PaAsiHpi_GetStreamInfo( streamComp, &streamInfo );
    switch( streamInfo.state )
    {
    case HPI_STATE_STOPPED:
        PA_DEBUG(( "[STOPPED] " ));
        break;
    case HPI_STATE_PLAYING:
        PA_DEBUG(( "[PLAYING] " ));
        break;
    case HPI_STATE_RECORDING:
        PA_DEBUG(( "[RECORDING] " ));
        break;
    case HPI_STATE_DRAINED:
        PA_DEBUG(( "[DRAINED] " ));
        break;
    default:
        PA_DEBUG(( "[unknown state] " ));
        break;
    }
    if( streamComp->hostBufferSize )
    {
        PA_DEBUG(( "host = %d/%d B, ", streamInfo.dataSize, streamComp->hostBufferSize ));
        PA_DEBUG(( "hw = %d/%d (%d) B, ", streamInfo.auxDataSize,
                   streamComp->hardwareBufferSize, streamComp->outputBufferCap ));
    }
    else
    {
        PA_DEBUG(( "hw = %d/%d B, ", streamInfo.dataSize, streamComp->hardwareBufferSize ));
    }
    PA_DEBUG(( "count = %d", streamInfo.frameCounter ));
    if( streamInfo.overflow )
    {
        PA_DEBUG(( " [overflow]" ));
    }
    else if( streamInfo.underflow )
    {
        PA_DEBUG(( " [underflow]" ));
    }
    PA_DEBUG(( "\n" ));
}


/** Display stream information for debugging purposes.

 @param stream Pointer to stream to query
 */
static void PaAsiHpi_StreamDump( PaAsiHpiStream *stream )
{
    assert( stream );

    PA_DEBUG(( "\n------------------------- STREAM INFO FOR %p ---------------------------\n", stream ));
    /* General stream info (input+output) */
    if( stream->baseStreamRep.streamCallback )
    {
        PA_DEBUG(( "[callback] " ));
    }
    else
    {
        PA_DEBUG(( "[blocking] " ));
    }
    PA_DEBUG(( "sr=%d Hz, poll=%d ms, max %d frames/buf ",
               (int)stream->baseStreamRep.streamInfo.sampleRate,
               stream->pollingInterval, stream->maxFramesPerHostBuffer ));
    switch( stream->state )
    {
    case paAsiHpiStoppedState:
        PA_DEBUG(( "[stopped]\n" ));
        break;
    case paAsiHpiActiveState:
        PA_DEBUG(( "[active]\n" ));
        break;
    case paAsiHpiCallbackFinishedState:
        PA_DEBUG(( "[cb fin]\n" ));
        break;
    default:
        PA_DEBUG(( "[unknown state]\n" ));
        break;
    }
    if( stream->callbackMode )
    {
        PA_DEBUG(( "cb info: thread=%p, cbAbort=%d, cbFinished=%d\n",
                   stream->thread.thread, stream->callbackAbort, stream->callbackFinished ));
    }

    PA_DEBUG(( "----------------------------------- Input  ------------------------------------\n" ));
    if( stream->input )
    {
        PaAsiHpi_StreamComponentDump( stream->input, stream );
    }
    else
    {
        PA_DEBUG(( "*none*\n" ));
    }

    PA_DEBUG(( "----------------------------------- Output ------------------------------------\n" ));
    if( stream->output )
    {
        PaAsiHpi_StreamComponentDump( stream->output, stream );
    }
    else
    {
        PA_DEBUG(( "*none*\n" ));
    }
    PA_DEBUG(( "-------------------------------------------------------------------------------\n\n" ));

}


/** Determine buffer sizes and allocate appropriate stream buffers.
 This attempts to allocate a BBM (host) buffer for the HPI stream component (either input
 or output, as both have similar buffer needs). Not all AudioScience adapters support BBM,
 in which case the hardware buffer has to suffice. The size of the HPI host buffer is chosen
 as a multiple of framesPerPaHostBuffer, and also influenced by the suggested latency and the
 estimated minimum polling interval. The HPI host and hardware buffer sizes are stored, and an
 appropriate cap for the hardware buffer is also calculated. Finally, the temporary stream
 buffer which serves as the PortAudio host buffer for this implementation is allocated.
 This buffer contains an integer number of user buffers, to simplify buffer adaption in the
 buffer processor. The function returns paBufferTooBig if the HPI interface cannot allocate
 an HPI host buffer of the desired size.

 @param streamComp Pointer to stream component struct

 @param pollingInterval Polling interval for stream, in milliseconds

 @param framesPerPaHostBuffer Size of PortAudio host buffer, in frames

 @param suggestedLatency Suggested latency for stream component, in seconds

 @return PortAudio error code (possibly paBufferTooBig or paInsufficientMemory)
 */
static PaError PaAsiHpi_SetupBuffers( PaAsiHpiStreamComponent *streamComp, uint32_t pollingInterval,
                                      unsigned long framesPerPaHostBuffer, PaTime suggestedLatency )
{
    PaError result = paNoError;
    PaAsiHpiStreamInfo streamInfo;
    unsigned long hpiBufferSize = 0, paHostBufferSize = 0;

    assert( streamComp );
    assert( streamComp->hpiDevice );

    /* Obtain size of hardware buffer of HPI stream, since we will be activating BBM shortly
       and afterwards the buffer size will refer to the BBM (host-side) buffer.
       This is necessary to enable reliable detection of xruns. */
    PA_ENSURE_( PaAsiHpi_GetStreamInfo( streamComp, &streamInfo ) );
    streamComp->hardwareBufferSize = streamInfo.bufferSize;
    hpiBufferSize = streamInfo.bufferSize;

    /* Check if BBM (background bus mastering) is to be enabled */
    if( PA_ASIHPI_USE_BBM_ )
    {
        uint32_t bbmBufferSize = 0, preLatencyBufferSize = 0;
        hpi_err_t hpiError = 0;
        PaTime pollingOverhead;

        /* Check overhead of Pa_Sleep() call (minimum sleep duration in ms -> OS dependent) */
        pollingOverhead = PaUtil_GetTime();
        Pa_Sleep( 0 );
        pollingOverhead = 1000*(PaUtil_GetTime() - pollingOverhead);
        PA_DEBUG(( "polling overhead = %f ms (length of 0-second sleep)\n", pollingOverhead ));
        /* Obtain minimum recommended size for host buffer (in bytes) */
        PA_ASIHPI_UNLESS_( HPI_StreamEstimateBufferSize( &streamComp->hpiFormat,
                           pollingInterval + (uint32_t)ceil( pollingOverhead ),
                           &bbmBufferSize ), paUnanticipatedHostError );
        /* BBM places more stringent requirements on buffer size (see description */
        /* of HPI_StreamEstimateBufferSize in HPI API document) */
        bbmBufferSize *= 3;
        /* Make sure the BBM buffer contains multiple PA host buffers */
        if( bbmBufferSize < 3 * streamComp->bytesPerFrame * framesPerPaHostBuffer )
            bbmBufferSize = 3 * streamComp->bytesPerFrame * framesPerPaHostBuffer;
        /* Try to honor latency suggested by user by growing buffer (no decrease possible) */
        if( suggestedLatency > 0.0 )
        {
            PaTime bufferDuration = ((PaTime)bbmBufferSize) / streamComp->bytesPerFrame
                                    / streamComp->hpiFormat.dwSampleRate;
            /* Don't decrease buffer */
            if( bufferDuration < suggestedLatency )
            {
                /* Save old buffer size, to be retried if new size proves too big */
                preLatencyBufferSize = bbmBufferSize;
                bbmBufferSize = (uint32_t)ceil( suggestedLatency * streamComp->bytesPerFrame
                                            * streamComp->hpiFormat.dwSampleRate );
            }
        }
        /* Choose closest memory block boundary (HPI API document states that
        "a buffer size of Nx4096 - 20 makes the best use of memory"
        (under the entry for HPI_StreamEstimateBufferSize)) */
        bbmBufferSize = ((uint32_t)ceil((bbmBufferSize + 20)/4096.0))*4096 - 20;
        streamComp->hostBufferSize = bbmBufferSize;
        /* Allocate BBM host buffer (this enables bus mastering transfers in background) */
        if( streamComp->hpiDevice->streamIsOutput )
            hpiError = HPI_OutStreamHostBufferAllocate( NULL,
                       streamComp->hpiStream,
                       bbmBufferSize );
        else
            hpiError = HPI_InStreamHostBufferAllocate( NULL,
                       streamComp->hpiStream,
                       bbmBufferSize );
        if( hpiError )
        {
            /* Indicate that BBM is disabled */
            streamComp->hostBufferSize = 0;
            /* Retry with smaller buffer size (transfers will still work, but not via BBM) */
            if( hpiError == HPI_ERROR_INVALID_DATASIZE )
            {
                /* Retry BBM allocation with smaller size if requested latency proved too big */
                if( preLatencyBufferSize > 0 )
                {
                    PA_DEBUG(( "Retrying BBM allocation with smaller size (%d vs. %d bytes)\n",
                               preLatencyBufferSize, bbmBufferSize ));
                    bbmBufferSize = preLatencyBufferSize;
                    if( streamComp->hpiDevice->streamIsOutput )
                        hpiError = HPI_OutStreamHostBufferAllocate( NULL,
                                   streamComp->hpiStream,
                                   bbmBufferSize );
                    else
                        hpiError = HPI_InStreamHostBufferAllocate( NULL,
                                   streamComp->hpiStream,
                                   bbmBufferSize );
                    /* Another round of error checking */
                    if( hpiError )
                    {
                        PA_ASIHPI_REPORT_ERROR_( hpiError );
                        /* No escapes this time */
                        if( hpiError == HPI_ERROR_INVALID_DATASIZE )
                        {
                            result = paBufferTooBig;
                            goto error;
                        }
                        else if( hpiError != HPI_ERROR_INVALID_OPERATION )
                        {
                            result = paUnanticipatedHostError;
                            goto error;
                        }
                    }
                    else
                    {
                        streamComp->hostBufferSize = bbmBufferSize;
                        hpiBufferSize = bbmBufferSize;
                    }
                }
                else
                {
                    result = paBufferTooBig;
                    goto error;
                }
            }
            /* If BBM not supported, foreground transfers will be used, but not a show-stopper */
            /* Anything else is an error */
            else if (( hpiError != HPI_ERROR_INVALID_OPERATION ) &&
                    ( hpiError != HPI_ERROR_INVALID_FUNC ))
            {
                PA_ASIHPI_REPORT_ERROR_( hpiError );
                result = paUnanticipatedHostError;
                goto error;
            }
        }
        else
        {
            hpiBufferSize = bbmBufferSize;
        }
    }

    /* Final check of buffer size */
    paHostBufferSize = streamComp->bytesPerFrame * framesPerPaHostBuffer;
    if( hpiBufferSize < 3*paHostBufferSize )
    {
        result = paBufferTooBig;
        goto error;
    }
    /* Set cap on output buffer size, based on latency suggestions */
    if( streamComp->hpiDevice->streamIsOutput )
    {
        PaTime latency = suggestedLatency > 0.0 ? suggestedLatency :
                         streamComp->hpiDevice->baseDeviceInfo.defaultHighOutputLatency;
        streamComp->outputBufferCap =
            (uint32_t)ceil( latency * streamComp->bytesPerFrame * streamComp->hpiFormat.dwSampleRate );
        /* The cap should not be too small, to prevent underflow */
        if( streamComp->outputBufferCap < 4*paHostBufferSize )
            streamComp->outputBufferCap = 4*paHostBufferSize;
    }
    else
    {
        streamComp->outputBufferCap = 0;
    }
    /* Temp buffer size should be multiple of PA host buffer size (or 1x, if using fixed blocks) */
    streamComp->tempBufferSize = paHostBufferSize;
    /* Allocate temp buffer */
    PA_UNLESS_( streamComp->tempBuffer = (uint8_t *)PaUtil_AllocateMemory( streamComp->tempBufferSize ),
                paInsufficientMemory );
error:
    return result;
}


/** Opens PortAudio stream.
 This determines a suitable value for framesPerBuffer, if the user didn't specify it,
 based on the suggested latency. It then opens each requested stream direction with the
 appropriate stream format, and allocates the required stream buffers. It sets up the
 various PortAudio structures dealing with streams, and estimates the stream latency.

 See pa_hostapi.h for a list of validity guarantees made about OpenStream parameters.

 @param hostApi Pointer to host API struct

 @param s List of open streams, where successfully opened stream will go

 @param inputParameters Pointer to stream parameter struct for input side of stream

 @param outputParameters Pointer to stream parameter struct for output side of stream

 @param sampleRate Desired sample rate

 @param framesPerBuffer Desired number of frames per buffer passed to user callback
                        (or chunk size for blocking stream)

 @param streamFlags Stream flags

 @param streamCallback Pointer to user callback function (zero for blocking interface)

 @param userData Pointer to user data that will be passed to callback function along with data

 @return PortAudio error code
*/
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 )
{
    PaError result = paNoError;
    PaAsiHpiHostApiRepresentation *hpiHostApi = (PaAsiHpiHostApiRepresentation*)hostApi;
    PaAsiHpiStream *stream = NULL;
    unsigned long framesPerHostBuffer = framesPerBuffer;
    int inputChannelCount = 0, outputChannelCount = 0;
    PaSampleFormat inputSampleFormat = 0, outputSampleFormat = 0;
    PaSampleFormat hostInputSampleFormat = 0, hostOutputSampleFormat = 0;
    PaTime maxSuggestedLatency = 0.0;

    /* Validate platform-specific flags -> none expected for HPI */
    if( (streamFlags & paPlatformSpecificFlags) != 0 )
        return paInvalidFlag; /* unexpected platform-specific flag */

    /* Create blank stream structure */
    PA_UNLESS_( stream = (PaAsiHpiStream *)PaUtil_AllocateMemory( sizeof(PaAsiHpiStream) ),
                paInsufficientMemory );
    memset( stream, 0, sizeof(PaAsiHpiStream) );

    /* If the number of frames per buffer is unspecified, we have to come up with one. */
    if( framesPerHostBuffer == paFramesPerBufferUnspecified )
    {
        if( inputParameters )
            maxSuggestedLatency = inputParameters->suggestedLatency;
        if( outputParameters && (outputParameters->suggestedLatency > maxSuggestedLatency) )
            maxSuggestedLatency = outputParameters->suggestedLatency;
        /* Use suggested latency if available */
        if( maxSuggestedLatency > 0.0 )
            framesPerHostBuffer = (unsigned long)ceil( maxSuggestedLatency * sampleRate );
        else
            /* AudioScience cards like BIG buffers by default */
            framesPerHostBuffer = 4096;
    }
    /* Lower bounds on host buffer size, due to polling and HPI constraints */
    if( 1000.0*framesPerHostBuffer/sampleRate < PA_ASIHPI_MIN_POLLING_INTERVAL_ )
        framesPerHostBuffer = (unsigned long)ceil( sampleRate * PA_ASIHPI_MIN_POLLING_INTERVAL_ / 1000.0 );
    /*    if( framesPerHostBuffer < PA_ASIHPI_MIN_FRAMES_ )
            framesPerHostBuffer = PA_ASIHPI_MIN_FRAMES_; */
    /* Efficient if host buffer size is integer multiple of user buffer size */
    if( framesPerBuffer > 0 )
        framesPerHostBuffer = (unsigned long)ceil( (double)framesPerHostBuffer / framesPerBuffer ) * framesPerBuffer;
    /* Buffer should always be a multiple of 4 bytes to facilitate 32-bit PCI transfers.
     By keeping the frames a multiple of 4, this is ensured even for 8-bit mono sound. */
    framesPerHostBuffer = (framesPerHostBuffer / 4) * 4;
    /* Polling is based on time length (in milliseconds) of user-requested block size */
    stream->pollingInterval = (uint32_t)ceil( 1000.0*framesPerHostBuffer/sampleRate );
    assert( framesPerHostBuffer > 0 );

    /* Open underlying streams, check formats and allocate buffers */
    if( inputParameters )
    {
        /* Create blank stream component structure */
        PA_UNLESS_( stream->input = (PaAsiHpiStreamComponent *)PaUtil_AllocateMemory( sizeof(PaAsiHpiStreamComponent) ),
                    paInsufficientMemory );
        memset( stream->input, 0, sizeof(PaAsiHpiStreamComponent) );
        /* Create/validate format */
        PA_ENSURE_( PaAsiHpi_CreateFormat( hostApi, inputParameters, sampleRate,
                                           &stream->input->hpiDevice, &stream->input->hpiFormat ) );
        /* Open stream and set format */
        PA_ENSURE_( PaAsiHpi_OpenInput( hostApi, stream->input->hpiDevice, &stream->input->hpiFormat,
                                        &stream->input->hpiStream ) );
        inputChannelCount = inputParameters->channelCount;
        inputSampleFormat = inputParameters->sampleFormat;
        hostInputSampleFormat = PaAsiHpi_HpiToPaFormat( stream->input->hpiFormat.wFormat );
        stream->input->bytesPerFrame = inputChannelCount * Pa_GetSampleSize( hostInputSampleFormat );
        assert( stream->input->bytesPerFrame > 0 );
        /* Allocate host and temp buffers of appropriate size */
        PA_ENSURE_( PaAsiHpi_SetupBuffers( stream->input, stream->pollingInterval,
                                           framesPerHostBuffer, inputParameters->suggestedLatency ) );
    }
    if( outputParameters )
    {
        /* Create blank stream component structure */
        PA_UNLESS_( stream->output = (PaAsiHpiStreamComponent *)PaUtil_AllocateMemory( sizeof(PaAsiHpiStreamComponent) ),
                    paInsufficientMemory );
        memset( stream->output, 0, sizeof(PaAsiHpiStreamComponent) );
        /* Create/validate format */
        PA_ENSURE_( PaAsiHpi_CreateFormat( hostApi, outputParameters, sampleRate,
                                           &stream->output->hpiDevice, &stream->output->hpiFormat ) );
        /* Open stream and check format */
        PA_ENSURE_( PaAsiHpi_OpenOutput( hostApi, stream->output->hpiDevice,
                                         &stream->output->hpiFormat,
                                         &stream->output->hpiStream ) );
        outputChannelCount = outputParameters->channelCount;
        outputSampleFormat = outputParameters->sampleFormat;
        hostOutputSampleFormat = PaAsiHpi_HpiToPaFormat( stream->output->hpiFormat.wFormat );
        stream->output->bytesPerFrame = outputChannelCount * Pa_GetSampleSize( hostOutputSampleFormat );
        /* Allocate host and temp buffers of appropriate size */
        PA_ENSURE_( PaAsiHpi_SetupBuffers( stream->output, stream->pollingInterval,
                                           framesPerHostBuffer, outputParameters->suggestedLatency ) );
    }

    /* Determine maximum frames per host buffer (least common denominator of input/output) */
    if( inputParameters && outputParameters )
    {
        stream->maxFramesPerHostBuffer = PA_MIN( stream->input->tempBufferSize / stream->input->bytesPerFrame,
                                         stream->output->tempBufferSize / stream->output->bytesPerFrame );
    }
    else
    {
        stream->maxFramesPerHostBuffer = inputParameters ? stream->input->tempBufferSize / stream->input->bytesPerFrame
                                         : stream->output->tempBufferSize / stream->output->bytesPerFrame;
    }
    assert( stream->maxFramesPerHostBuffer > 0 );
    /* Initialize various other stream parameters */
    stream->neverDropInput = streamFlags & paNeverDropInput;
    stream->state = paAsiHpiStoppedState;

    /* Initialize either callback or blocking interface */
    if( streamCallback )
    {
        PaUtil_InitializeStreamRepresentation( &stream->baseStreamRep,
                                               &hpiHostApi->callbackStreamInterface,
                                               streamCallback, userData );
        stream->callbackMode = 1;
    }
    else
    {
        PaUtil_InitializeStreamRepresentation( &stream->baseStreamRep,
                                               &hpiHostApi->blockingStreamInterface,
                                               streamCallback, userData );
        /* Pre-allocate non-interleaved user buffer pointers for blocking interface */
        PA_UNLESS_( stream->blockingUserBufferCopy =
                        PaUtil_AllocateMemory( sizeof(void *) * PA_MAX( inputChannelCount, outputChannelCount ) ),
                    paInsufficientMemory );
        stream->callbackMode = 0;
    }
    PaUtil_InitializeCpuLoadMeasurer( &stream->cpuLoadMeasurer, sampleRate );

    /* Following pa_linux_alsa's lead, we operate with fixed host buffer size by default, */
    /* since other modes will invariably lead to block adaption (maybe Bounded better?) */
    PA_ENSURE_( PaUtil_InitializeBufferProcessor( &stream->bufferProcessor,
                inputChannelCount, inputSampleFormat, hostInputSampleFormat,
                outputChannelCount, outputSampleFormat, hostOutputSampleFormat,
                sampleRate, streamFlags,
                framesPerBuffer, framesPerHostBuffer, paUtilFixedHostBufferSize,
                streamCallback, userData ) );

    stream->baseStreamRep.streamInfo.structVersion = 1;
    stream->baseStreamRep.streamInfo.sampleRate = sampleRate;
    /* Determine input latency from buffer processor and buffer sizes */
    if( stream->input )
    {
        PaTime bufferDuration = ( stream->input->hostBufferSize + stream->input->hardwareBufferSize )
                                / sampleRate / stream->input->bytesPerFrame;
        stream->baseStreamRep.streamInfo.inputLatency =
            bufferDuration +
            ((PaTime)PaUtil_GetBufferProcessorInputLatencyFrames( &stream->bufferProcessor ) -
                stream->maxFramesPerHostBuffer) / sampleRate;
        assert( stream->baseStreamRep.streamInfo.inputLatency > 0.0 );
    }
    /* Determine output latency from buffer processor and buffer sizes */
    if( stream->output )
    {
        PaTime bufferDuration = ( stream->output->hostBufferSize + stream->output->hardwareBufferSize )
                                / sampleRate / stream->output->bytesPerFrame;
        /* Take buffer size cap into account (see PaAsiHpi_WaitForFrames) */
        if( !stream->input && (stream->output->outputBufferCap > 0) )
        {
            bufferDuration = PA_MIN( bufferDuration,
                                     stream->output->outputBufferCap / sampleRate / stream->output->bytesPerFrame );
        }
        stream->baseStreamRep.streamInfo.outputLatency =
            bufferDuration +
            ((PaTime)PaUtil_GetBufferProcessorOutputLatencyFrames( &stream->bufferProcessor ) -
                stream->maxFramesPerHostBuffer) / sampleRate;
        assert( stream->baseStreamRep.streamInfo.outputLatency > 0.0 );
    }

    /* Report stream info, for debugging purposes */
    PaAsiHpi_StreamDump( stream );

    /* Save initialized stream to PA stream list */
    *s = (PaStream*)stream;
    return result;

error:
    CloseStream( (PaStream*)stream );
    return result;
}


/** Close PortAudio stream.
 When CloseStream() is called, the multi-api layer ensures that the stream has already
 been stopped or aborted. This closes the underlying HPI streams and deallocates stream
 buffers and structs.

 @param s Pointer to PortAudio stream

 @return PortAudio error code
*/
static PaError CloseStream( PaStream *s )
{
    PaError result = paNoError;
    PaAsiHpiStream *stream = (PaAsiHpiStream*)s;

    /* If stream is already gone, all is well */
    if( stream == NULL )
        return paNoError;

    /* Generic stream cleanup */
    PaUtil_TerminateBufferProcessor( &stream->bufferProcessor );
    PaUtil_TerminateStreamRepresentation( &stream->baseStreamRep );

    /* Implementation-specific details - close internal streams */
    if( stream->input )
    {
        /* Close HPI stream (freeing BBM host buffer in the process, if used) */
        if( stream->input->hpiStream )
        {
            PA_ASIHPI_UNLESS_( HPI_InStreamClose( NULL,
                                                  stream->input->hpiStream ), paUnanticipatedHostError );
        }
        /* Free temp buffer and stream component */
        PaUtil_FreeMemory( stream->input->tempBuffer );
        PaUtil_FreeMemory( stream->input );
    }
    if( stream->output )
    {
        /* Close HPI stream (freeing BBM host buffer in the process, if used) */
        if( stream->output->hpiStream )
        {
            PA_ASIHPI_UNLESS_( HPI_OutStreamClose( NULL,
                                                   stream->output->hpiStream ), paUnanticipatedHostError );
        }
        /* Free temp buffer and stream component */
        PaUtil_FreeMemory( stream->output->tempBuffer );
        PaUtil_FreeMemory( stream->output );
    }

    PaUtil_FreeMemory( stream->blockingUserBufferCopy );
    PaUtil_FreeMemory( stream );

error:
    return result;
}


/** Prime HPI output stream with silence.
 This resets the output stream and uses PortAudio helper routines to fill the
 temp buffer with silence. It then writes two host buffers to the stream. This is supposed
 to be called before the stream is started. It has no effect on input-only streams.

 @param stream Pointer to stream struct

 @return PortAudio error code
 */
static PaError PaAsiHpi_PrimeOutputWithSilence( PaAsiHpiStream *stream )
{
    PaError result = paNoError;
    PaAsiHpiStreamComponent *out;
    PaUtilZeroer *zeroer;
    PaSampleFormat outputFormat;
    assert( stream );
    out = stream->output;
    /* Only continue if stream has output channels */
    if( !out )
        return result;
    assert( out->tempBuffer );

    /* Clear all existing data in hardware playback buffer */
    PA_ASIHPI_UNLESS_( HPI_OutStreamReset( NULL,
                                           out->hpiStream ), paUnanticipatedHostError );
    /* Fill temp buffer with silence */
    outputFormat = PaAsiHpi_HpiToPaFormat( out->hpiFormat.wFormat );
    zeroer = PaUtil_SelectZeroer( outputFormat );
    zeroer(out->tempBuffer, 1, out->tempBufferSize / Pa_GetSampleSize(outputFormat) );
    /* Write temp buffer to hardware fifo twice, to get started */
    PA_ASIHPI_UNLESS_( HPI_OutStreamWriteBuf( NULL, out->hpiStream,
                                              out->tempBuffer, out->tempBufferSize, &out->hpiFormat),
                                              paUnanticipatedHostError );
    PA_ASIHPI_UNLESS_( HPI_OutStreamWriteBuf( NULL, out->hpiStream,
                                              out->tempBuffer, out->tempBufferSize, &out->hpiFormat),
                                              paUnanticipatedHostError );
error:
    return result;
}


/** Start HPI streams (both input + output).
 This starts all HPI streams in the PortAudio stream. Output streams are first primed with
 silence, if required. After this call the PA stream is in the Active state.

 @todo Implement priming via the user callback

 @param stream Pointer to stream struct

 @param outputPrimed True if output is already primed (if false, silence will be loaded before starting)

 @return PortAudio error code
 */
static PaError PaAsiHpi_StartStream( PaAsiHpiStream *stream, int outputPrimed )
{
    PaError result = paNoError;

    if( stream->input )
    {
        PA_ASIHPI_UNLESS_( HPI_InStreamStart( NULL,
                                              stream->input->hpiStream ), paUnanticipatedHostError );
    }
    if( stream->output )
    {
        if( !outputPrimed )
        {
            /* Buffer isn't primed, so load stream with silence */
            PA_ENSURE_( PaAsiHpi_PrimeOutputWithSilence( stream ) );
        }
        PA_ASIHPI_UNLESS_( HPI_OutStreamStart( NULL,
                                               stream->output->hpiStream ), paUnanticipatedHostError );
    }
    stream->state = paAsiHpiActiveState;
    stream->callbackFinished = 0;

    /* Report stream info for debugging purposes */
    /*    PaAsiHpi_StreamDump( stream );   */

error:
    return result;
}


/** Start PortAudio stream.
 If the stream has a callback interface, this starts a helper thread to feed the user callback.
 The thread will then take care of starting the HPI streams, and this function will block
 until the streams actually start. In the case of a blocking interface, the HPI streams
 are simply started.

 @param s Pointer to PortAudio stream

 @return PortAudio error code
*/
static PaError StartStream( PaStream *s )
{
    PaError result = paNoError;
    PaAsiHpiStream *stream = (PaAsiHpiStream*)s;

    assert( stream );

    /* Ready the processor */
    PaUtil_ResetBufferProcessor( &stream->bufferProcessor );

    if( stream->callbackMode )
    {
        /* Create and start callback engine thread */
        /* Also waits 1 second for stream to be started by engine thread (otherwise aborts) */
        PA_ENSURE_( PaUnixThread_New( &stream->thread, &CallbackThreadFunc, stream, 1., 0 /*rtSched*/ ) );
    }
    else
    {
        PA_ENSURE_( PaAsiHpi_StartStream( stream, 0 ) );
    }

error:
    return result;
}


/** Stop HPI streams (input + output), either softly or abruptly.
 If abort is false, the function blocks until the output stream is drained, otherwise it
 stops immediately and discards data in the stream hardware buffers.

 This function is safe to call from the callback engine thread as well as the main thread.

 @param stream Pointer to stream struct

 @param abort True if samples in output buffer should be discarded (otherwise blocks until stream is done)

 @return PortAudio error code

 */
static PaError PaAsiHpi_StopStream( PaAsiHpiStream *stream, int abort )
{
    PaError result = paNoError;

    assert( stream );

    /* Input channels */
    if( stream->input )
    {
        PA_ASIHPI_UNLESS_( HPI_InStreamReset( NULL,
                                              stream->input->hpiStream ), paUnanticipatedHostError );
    }
    /* Output channels */
    if( stream->output )
    {
        if( !abort )
        {
            /* Wait until HPI output stream is drained */
            while( 1 )
            {
                PaAsiHpiStreamInfo streamInfo;
                PaTime timeLeft;

                /* Obtain number of samples waiting to be played */
                PA_ENSURE_( PaAsiHpi_GetStreamInfo( stream->output, &streamInfo ) );
                /* Check if stream is drained */
                if( (streamInfo.state != HPI_STATE_PLAYING) &&
                        (streamInfo.dataSize < stream->output->bytesPerFrame * PA_ASIHPI_MIN_FRAMES_) )
                    break;
                /* Sleep amount of time represented by remaining samples */
                timeLeft = 1000.0 * streamInfo.dataSize / stream->output->bytesPerFrame
                           / stream->baseStreamRep.streamInfo.sampleRate;
                Pa_Sleep( (long)ceil( timeLeft ) );
            }
        }
        PA_ASIHPI_UNLESS_( HPI_OutStreamReset( NULL,
                                               stream->output->hpiStream ), paUnanticipatedHostError );
    }

    /* Report stream info for debugging purposes */
    /*    PaAsiHpi_StreamDump( stream ); */

error:
    return result;
}


/** Stop or abort PortAudio stream.

 This function is used to explicitly stop the PortAudio stream (via StopStream/AbortStream),
 as opposed to the situation when the callback finishes with a result other than paContinue.
 If a stream is in callback mode we will have to inspect whether the background thread has
 finished, or we will have to take it out. In either case we join the thread before returning.
 In blocking mode, we simply tell HPI to stop abruptly (abort) or finish buffers (drain).
 The PortAudio stream will be in the Stopped state after a call to this function.

 Don't call this from the callback engine thread!

 @param stream Pointer to stream struct

 @param abort True if samples in output buffer should be discarded (otherwise blocks until stream is done)

 @return PortAudio error code
*/
static PaError PaAsiHpi_ExplicitStop( PaAsiHpiStream *stream, int abort )
{
    PaError result = paNoError;

    /* First deal with the callback thread, cancelling and/or joining it if necessary */
    if( stream->callbackMode )
    {
        PaError threadRes;
        stream->callbackAbort = abort;
        if( abort )
        {
            PA_DEBUG(( "Aborting callback\n" ));
        }
        else
        {
            PA_DEBUG(( "Stopping callback\n" ));
        }
        PA_ENSURE_( PaUnixThread_Terminate( &stream->thread, !abort, &threadRes ) );
        if( threadRes != paNoError )
        {
            PA_DEBUG(( "Callback thread returned: %d\n", threadRes ));
        }
    }
    else
    {
        PA_ENSURE_( PaAsiHpi_StopStream( stream, abort ) );
    }

    stream->state = paAsiHpiStoppedState;

error:
    return result;
}


/** Stop PortAudio stream.
 This blocks until the output buffers are drained.

 @param s Pointer to PortAudio stream

 @return PortAudio error code
*/
static PaError StopStream( PaStream *s )
{
    return PaAsiHpi_ExplicitStop( (PaAsiHpiStream *) s, 0 );
}


/** Abort PortAudio stream.
 This discards any existing data in output buffers and stops the stream immediately.

 @param s Pointer to PortAudio stream

 @return PortAudio error code
*/
static PaError AbortStream( PaStream *s )
{
    return PaAsiHpi_ExplicitStop( (PaAsiHpiStream * ) s, 1 );
}


/** Determine whether the stream is stopped.
 A stream is considered to be stopped prior to a successful call to StartStream and after
 a successful call to StopStream or AbortStream. If a stream callback returns a value other
 than paContinue the stream is NOT considered to be stopped (it is in CallbackFinished state).

 @param s Pointer to PortAudio stream

 @return Returns one (1) when the stream is stopped, zero (0) when the stream is running, or
         a PaErrorCode (which are always negative) if PortAudio is not initialized or an
         error is encountered.
*/
static PaError IsStreamStopped( PaStream *s )
{
    PaAsiHpiStream *stream = (PaAsiHpiStream*)s;

    assert( stream );
    return stream->state == paAsiHpiStoppedState ? 1 : 0;
}


/** Determine whether the stream is active.
 A stream is active after a successful call to StartStream(), until it becomes inactive either
 as a result of a call to StopStream() or AbortStream(), or as a result of a return value
 other than paContinue from the stream callback. In the latter case, the stream is considered
 inactive after the last buffer has finished playing.

 @param s Pointer to PortAudio stream

 @return Returns one (1) when the stream is active (i.e. playing or recording audio),
         zero (0) when not playing, or a PaErrorCode (which are always negative)
         if PortAudio is not initialized or an error is encountered.
*/
static PaError IsStreamActive( PaStream *s )
{
    PaAsiHpiStream *stream = (PaAsiHpiStream*)s;

    assert( stream );
    return stream->state == paAsiHpiActiveState ? 1 : 0;
}


/** Returns current stream time.
 This corresponds to the system clock. The clock should run continuously while the stream
 is open, i.e. between calls to OpenStream() and CloseStream(), therefore a frame counter
 is not good enough.

 @param s Pointer to PortAudio stream

 @return Stream time, in seconds
 */
static PaTime GetStreamTime( PaStream *s )
{
    return PaUtil_GetTime();
}


/** Returns CPU load.

 @param s Pointer to PortAudio stream

 @return CPU load (0.0 if blocking interface is used)
 */
static double GetStreamCpuLoad( PaStream *s )
{
    PaAsiHpiStream *stream = (PaAsiHpiStream*)s;

    return stream->callbackMode ? PaUtil_GetCpuLoad( &stream->cpuLoadMeasurer ) : 0.0;
}

/* --------------------------- Callback Interface --------------------------- */

/** Exit routine which is called when callback thread quits.
 This takes care of stopping the HPI streams (either waiting for output to finish, or
 abruptly). It also calls the user-supplied StreamFinished callback, and sets the
 stream state to CallbackFinished if it was reached via a non-paContinue return from
 the user callback function.

 @param userData A pointer to an open stream previously created with Pa_OpenStream
 */
static void PaAsiHpi_OnThreadExit( void *userData )
{
    PaAsiHpiStream *stream = (PaAsiHpiStream *) userData;

    assert( stream );

    PaUtil_ResetCpuLoadMeasurer( &stream->cpuLoadMeasurer );

    PA_DEBUG(( "%s: Stopping HPI streams\n", __FUNCTION__ ));
    PaAsiHpi_StopStream( stream, stream->callbackAbort );
    PA_DEBUG(( "%s: Stoppage\n", __FUNCTION__ ));

    /* Eventually notify user all buffers have played */
    if( stream->baseStreamRep.streamFinishedCallback )
    {
        stream->baseStreamRep.streamFinishedCallback( stream->baseStreamRep.userData );
    }

    /* Unfortunately both explicit calls to Stop/AbortStream (leading to Stopped state)
     and implicit stops via paComplete/paAbort (leading to CallbackFinished state)
     end up here - need another flag to remind us which is the case */
    if( stream->callbackFinished )
        stream->state = paAsiHpiCallbackFinishedState;
}


/** Wait until there is enough frames to fill a host buffer.
 The routine attempts to sleep until at least a full host buffer can be retrieved from the
 input HPI stream and passed to the output HPI stream. It will first sleep until enough
 output space is available, as this is usually easily achievable. If it is an output-only
 stream, it will also sleep if the hardware buffer is too full, thereby throttling the
 filling of the output buffer and reducing output latency. The routine then blocks until
 enough input samples are available, unless this will cause an output underflow. In the
 process, input overflows and output underflows are indicated.

 @param stream Pointer to stream struct

 @param framesAvail Returns the number of available frames

 @param cbFlags Overflows and underflows indicated in here

 @return PortAudio error code (only paUnanticipatedHostError expected)
 */
static PaError PaAsiHpi_WaitForFrames( PaAsiHpiStream *stream, unsigned long *framesAvail,
                                       PaStreamCallbackFlags *cbFlags )
{
    PaError result = paNoError;
    double sampleRate;
    unsigned long framesTarget;
    uint32_t outputData = 0, outputSpace = 0, inputData = 0, framesLeft = 0;

    assert( stream );
    assert( stream->input || stream->output );

    sampleRate = stream->baseStreamRep.streamInfo.sampleRate;
    /* We have to come up with this much frames on both input and output */
    framesTarget = stream->bufferProcessor.framesPerHostBuffer;
    assert( framesTarget > 0 );

    while( 1 )
    {
        PaAsiHpiStreamInfo info;
        /* Check output first, as this takes priority in the default full-duplex mode */
        if( stream->output )
        {
            PA_ENSURE_( PaAsiHpi_GetStreamInfo( stream->output, &info ) );
            /* Wait until enough space is available in output buffer to receive a full block */
            if( info.availableFrames < framesTarget )
            {
                framesLeft = framesTarget - info.availableFrames;
                Pa_Sleep( (long)ceil( 1000 * framesLeft / sampleRate ) );
                continue;
            }
            /* Wait until the data in hardware buffer has dropped to a sensible level.
             Without this, the hardware buffer quickly fills up in the absence of an input
             stream to regulate its data rate (if data generation is fast). This leads to
             large latencies, as the AudioScience hardware buffers are humongous.
             This is similar to the default "Hardware Buffering=off" option in the
             AudioScience WAV driver. */
            if( !stream->input && (stream->output->outputBufferCap > 0) &&
                    ( info.totalBufferedData > stream->output->outputBufferCap / stream->output->bytesPerFrame ) )
            {
                framesLeft = info.totalBufferedData - stream->output->outputBufferCap / stream->output->bytesPerFrame;
                Pa_Sleep( (long)ceil( 1000 * framesLeft / sampleRate ) );
                continue;
            }
            outputData = info.totalBufferedData;
            outputSpace = info.availableFrames;
            /* Report output underflow to callback */
            if( info.underflow )
            {
                *cbFlags |= paOutputUnderflow;
            }
        }

        /* Now check input side */
        if( stream->input )
        {
            PA_ENSURE_( PaAsiHpi_GetStreamInfo( stream->input, &info ) );
            /* If a full block of samples hasn't been recorded yet, wait for it if possible */
            if( info.availableFrames < framesTarget )
            {
                framesLeft = framesTarget - info.availableFrames;
                /* As long as output is not disrupted in the process, wait for a full
                block of input samples */
                if( !stream->output || (outputData > framesLeft) )
                {
                    Pa_Sleep( (long)ceil( 1000 * framesLeft / sampleRate ) );
                    continue;
                }
            }
            inputData = info.availableFrames;
            /** @todo The paInputOverflow flag should be set in the callback containing the
             first input sample following the overflow. That means the block currently sitting
             at the fore-front of recording, i.e. typically the one containing the newest (last)
             sample in the HPI buffer system. This is most likely not the same as the current
             block of data being passed to the callback. The current overflow should ideally
             be noted in an overflow list of sorts, with an indication of when it should be
             reported. The trouble starts if there are several separate overflow incidents,
             given a big input buffer. Oh well, something to try out later... */
            if( info.overflow )
            {
                *cbFlags |= paInputOverflow;
            }
        }
        break;
    }
    /* Full-duplex stream */
    if( stream->input && stream->output )
    {
        if( outputSpace >= framesTarget )
            *framesAvail = outputSpace;
        /* If input didn't make the target, keep the output count instead (input underflow) */
        if( (inputData >= framesTarget) && (inputData < outputSpace) )
            *framesAvail = inputData;
    }
    else
    {
        *framesAvail = stream->input ? inputData : outputSpace;
    }

error:
    return result;
}


/** Obtain recording, current and playback timestamps of stream.
 The current time is determined by the system clock. This "now" timestamp occurs at the
 forefront of recording (and playback in the full-duplex case), which happens later than the
 input timestamp by an amount equal to the total number of recorded frames in the input buffer.
 The output timestamp indicates when the next generated sample will actually be played. This
 happens after all the samples currently in the output buffer are played. The output timestamp
 therefore follows the current timestamp by an amount equal to the number of frames yet to be
 played back in the output buffer.

 If the current timestamp is the present, the input timestamp is in the past and the output
 timestamp is in the future.

 @param stream Pointer to stream struct

 @param timeInfo Pointer to timeInfo struct that will contain timestamps
 */
static void PaAsiHpi_CalculateTimeInfo( PaAsiHpiStream *stream, PaStreamCallbackTimeInfo *timeInfo )
{
    PaAsiHpiStreamInfo streamInfo;
    double sampleRate;

    assert( stream );
    assert( timeInfo );
    sampleRate = stream->baseStreamRep.streamInfo.sampleRate;

    /* The current time ("now") is at the forefront of both recording and playback */
    timeInfo->currentTime = GetStreamTime( (PaStream *)stream );
    /* The last sample in the input buffer was recorded just now, so the first sample
     happened (number of recorded samples)/sampleRate ago */
    timeInfo->inputBufferAdcTime = timeInfo->currentTime;
    if( stream->input )
    {
        PaAsiHpi_GetStreamInfo( stream->input, &streamInfo );
        timeInfo->inputBufferAdcTime -= streamInfo.totalBufferedData / sampleRate;
    }
    /* The first of the outgoing samples will be played after all the samples in the output
     buffer is done */
    timeInfo->outputBufferDacTime = timeInfo->currentTime;
    if( stream->output )
    {
        PaAsiHpi_GetStreamInfo( stream->output, &streamInfo );
        timeInfo->outputBufferDacTime += streamInfo.totalBufferedData / sampleRate;
    }
}


/** Read from HPI input stream and register buffers.
 This reads data from the HPI input stream (if it exists) and registers the temp stream
 buffers of both input and output streams with the buffer processor. In the process it also
 handles input underflows in the full-duplex case.

 @param stream Pointer to stream struct

 @param numFrames On entrance the number of available frames, on exit the number of
                  received frames

 @param cbFlags Indicates overflows and underflows

 @return PortAudio error code
 */
static PaError PaAsiHpi_BeginProcessing( PaAsiHpiStream *stream, unsigned long *numFrames,
        PaStreamCallbackFlags *cbFlags )
{
    PaError result = paNoError;

    assert( stream );
    if( *numFrames > stream->maxFramesPerHostBuffer )
        *numFrames = stream->maxFramesPerHostBuffer;

    if( stream->input )
    {
        PaAsiHpiStreamInfo info;

        uint32_t framesToGet = *numFrames;

        /* Check for overflows and underflows yet again */
        PA_ENSURE_( PaAsiHpi_GetStreamInfo( stream->input, &info ) );
        if( info.overflow )
        {
            *cbFlags |= paInputOverflow;
        }
        /* Input underflow if less than expected number of samples pitch up */
        if( framesToGet > info.availableFrames )
        {
            PaUtilZeroer *zeroer;
            PaSampleFormat inputFormat;

            /* Never call an input-only stream with InputUnderflow set */
            if( stream->output )
                *cbFlags |= paInputUnderflow;
            framesToGet = info.availableFrames;
            /* Fill temp buffer with silence (to make up for missing input samples) */
            inputFormat = PaAsiHpi_HpiToPaFormat( stream->input->hpiFormat.wFormat );
            zeroer = PaUtil_SelectZeroer( inputFormat );
            zeroer(stream->input->tempBuffer, 1,
                   stream->input->tempBufferSize / Pa_GetSampleSize(inputFormat) );
        }

        /* Read block of data into temp buffer */
        PA_ASIHPI_UNLESS_( HPI_InStreamReadBuf( NULL,
                                             stream->input->hpiStream,
                                             stream->input->tempBuffer,
                                             framesToGet * stream->input->bytesPerFrame),
                           paUnanticipatedHostError );
        /* Register temp buffer with buffer processor (always FULL buffer) */
        PaUtil_SetInputFrameCount( &stream->bufferProcessor, *numFrames );
        /* HPI interface only allows interleaved channels */
        PaUtil_SetInterleavedInputChannels( &stream->bufferProcessor,
                                            0, stream->input->tempBuffer,
                                            stream->input->hpiFormat.wChannels );
    }
    if( stream->output )
    {
        /* Register temp buffer with buffer processor */
        PaUtil_SetOutputFrameCount( &stream->bufferProcessor, *numFrames );
        /* HPI interface only allows interleaved channels */
        PaUtil_SetInterleavedOutputChannels( &stream->bufferProcessor,
                                             0, stream->output->tempBuffer,
                                             stream->output->hpiFormat.wChannels );
    }

error:
    return result;
}


/** Flush output buffers to HPI output stream.
 This completes the processing cycle by writing the temp buffer to the HPI interface.
 Additional output underflows are caught before data is written to the stream, as this
 action typically remedies the underflow and hides it in the process.

 @param stream Pointer to stream struct

 @param numFrames The number of frames to write to the output stream

 @param cbFlags Indicates overflows and underflows
 */
static PaError PaAsiHpi_EndProcessing( PaAsiHpiStream *stream, unsigned long numFrames,
                                       PaStreamCallbackFlags *cbFlags )
{
    PaError result = paNoError;

    assert( stream );

    if( stream->output )
    {
        PaAsiHpiStreamInfo info;
        /* Check for underflows after the (potentially time-consuming) callback */
        PA_ENSURE_( PaAsiHpi_GetStreamInfo( stream->output, &info ) );
        if( info.underflow )
        {
            *cbFlags |= paOutputUnderflow;
        }

        /* Write temp buffer to HPI stream */
        PA_ASIHPI_UNLESS_( HPI_OutStreamWriteBuf( NULL,
                                           stream->output->hpiStream,
                                           stream->output->tempBuffer,
                                           numFrames * stream->output->bytesPerFrame,
                                           &stream->output->hpiFormat),
                           paUnanticipatedHostError );
    }

error:
    return result;
}


/** Main callback engine.
 This function runs in a separate thread and does all the work of fetching audio data from
 the AudioScience card via the HPI interface, feeding it to the user callback via the buffer
 processor, and delivering the resulting output data back to the card via HPI calls.
 It is started and terminated when the PortAudio stream is started and stopped, and starts
 the HPI streams on startup.

 @param userData A pointer to an open stream previously created with Pa_OpenStream.
*/
static void *CallbackThreadFunc( void *userData )
{
    PaError result = paNoError;
    PaAsiHpiStream *stream = (PaAsiHpiStream *) userData;
    int callbackResult = paContinue;

    assert( stream );

    /* Cleanup routine stops streams on thread exit */
    pthread_cleanup_push( &PaAsiHpi_OnThreadExit, stream );

    /* Start HPI streams and notify parent when we're done */
    PA_ENSURE_( PaUnixThread_PrepareNotify( &stream->thread ) );
    /* Buffer will be primed with silence */
    PA_ENSURE_( PaAsiHpi_StartStream( stream, 0 ) );
    PA_ENSURE_( PaUnixThread_NotifyParent( &stream->thread ) );

    /* MAIN LOOP */
    while( 1 )
    {
        PaStreamCallbackFlags cbFlags = 0;
        unsigned long framesAvail, framesGot;

        pthread_testcancel();

        /** @concern StreamStop if the main thread has requested a stop and the stream has not
        * been effectively stopped we signal this condition by modifying callbackResult
        * (we'll want to flush buffered output). */
        if( PaUnixThread_StopRequested( &stream->thread ) && (callbackResult == paContinue) )
        {
            PA_DEBUG(( "Setting callbackResult to paComplete\n" ));
            callbackResult = paComplete;
        }

        /* Start winding down thread if requested */
        if( callbackResult != paContinue )
        {
            stream->callbackAbort = (callbackResult == paAbort);
            if( stream->callbackAbort ||
                    /** @concern BlockAdaption: Go on if adaption buffers are empty */
                    PaUtil_IsBufferProcessorOutputEmpty( &stream->bufferProcessor ) )
            {
                goto end;
            }
            PA_DEBUG(( "%s: Flushing buffer processor\n", __FUNCTION__ ));
            /* There is still buffered output that needs to be processed */
        }

        /* SLEEP */
        /* Wait for data (or buffer space) to become available. This basically sleeps and
        polls the HPI interface until a full block of frames can be moved. */
        PA_ENSURE_( PaAsiHpi_WaitForFrames( stream, &framesAvail, &cbFlags ) );

        /* Consume buffer space. Once we have a number of frames available for consumption we
        must retrieve the data from the HPI interface and pass it to the PA buffer processor.
        We should be prepared to process several chunks successively. */
        while( framesAvail > 0 )
        {
            PaStreamCallbackTimeInfo timeInfo = {0, 0, 0};

            pthread_testcancel();

            framesGot = framesAvail;
            if( stream->bufferProcessor.hostBufferSizeMode == paUtilFixedHostBufferSize )
            {
                /* We've committed to a fixed host buffer size, stick to that */
                framesGot = framesGot >= stream->maxFramesPerHostBuffer ? stream->maxFramesPerHostBuffer : 0;
            }
            else
            {
                /* We've committed to an upper bound on the size of host buffers */
                assert( stream->bufferProcessor.hostBufferSizeMode == paUtilBoundedHostBufferSize );
                framesGot = PA_MIN( framesGot, stream->maxFramesPerHostBuffer );
            }

            /* Obtain buffer timestamps */
            PaAsiHpi_CalculateTimeInfo( stream, &timeInfo );
            PaUtil_BeginBufferProcessing( &stream->bufferProcessor, &timeInfo, cbFlags );
            /* CPU load measurement should include processing activivity external to the stream callback */
            PaUtil_BeginCpuLoadMeasurement( &stream->cpuLoadMeasurer );
            if( framesGot > 0 )
            {
                /* READ FROM HPI INPUT STREAM */
                PA_ENSURE_( PaAsiHpi_BeginProcessing( stream, &framesGot, &cbFlags ) );
                /* Input overflow in a full-duplex stream makes for interesting times */
                if( stream->input && stream->output && (cbFlags & paInputOverflow) )
                {
                    /* Special full-duplex paNeverDropInput mode */
                    if( stream->neverDropInput )
                    {
                        PaUtil_SetNoOutput( &stream->bufferProcessor );
                        cbFlags |= paOutputOverflow;
                    }
                }
                /* CALL USER CALLBACK WITH INPUT DATA, AND OBTAIN OUTPUT DATA */
                PaUtil_EndBufferProcessing( &stream->bufferProcessor, &callbackResult );
                /* Clear overflow and underflow information (but PaAsiHpi_EndProcessing might
                still show up output underflow that will carry over to next round) */
                cbFlags = 0;
                /*  WRITE TO HPI OUTPUT STREAM */
                PA_ENSURE_( PaAsiHpi_EndProcessing( stream, framesGot, &cbFlags ) );
                /* Advance frame counter */
                framesAvail -= framesGot;
            }
            PaUtil_EndCpuLoadMeasurement( &stream->cpuLoadMeasurer, framesGot );

            if( framesGot == 0 )
            {
                /* Go back to polling for more frames */
                break;

            }
            if( callbackResult != paContinue )
                break;
        }
    }

    /* This code is unreachable, but important to include regardless because it
     * is possibly a macro with a closing brace to match the opening brace in
     * pthread_cleanup_push() above.  The documentation states that they must
     * always occur in pairs. */
    pthread_cleanup_pop( 1 );

end:
    /* Indicates normal exit of callback, as opposed to the thread getting killed explicitly */
    stream->callbackFinished = 1;
    PA_DEBUG(( "%s: Thread %d exiting (callbackResult = %d)\n ",
               __FUNCTION__, pthread_self(), callbackResult ));
    /* Exit from thread and report any PortAudio error in the process */
    PaUnixThreading_EXIT( result );
error:
    goto end;
}

/* --------------------------- Blocking Interface --------------------------- */

/* As separate stream interfaces are used for blocking and callback streams, the following
 functions can be guaranteed to only be called for blocking streams. */

/** Read data from input stream.
 This reads the indicated number of frames into the supplied buffer from an input stream,
 and blocks until this is done.

 @param s Pointer to PortAudio stream

 @param buffer Pointer to buffer that will receive interleaved data (or an array of pointers
               to a buffer for each non-interleaved channel)

 @param frames Number of frames to read from stream

 @return PortAudio error code (also indicates overflow via paInputOverflowed)
 */
static PaError ReadStream( PaStream *s,
                           void *buffer,
                           unsigned long frames )
{
    PaError result = paNoError;
    PaAsiHpiStream *stream = (PaAsiHpiStream*)s;
    PaAsiHpiStreamInfo info;
    void *userBuffer;

    assert( stream );
    PA_UNLESS_( stream->input, paCanNotReadFromAnOutputOnlyStream );

    /* Check for input overflow since previous call to ReadStream */
    PA_ENSURE_( PaAsiHpi_GetStreamInfo( stream->input, &info ) );
    if( info.overflow )
    {
        result = paInputOverflowed;
    }

    /* NB Make copy of user buffer pointers, since they are advanced by buffer processor */
    if( stream->bufferProcessor.userInputIsInterleaved )
    {
        userBuffer = buffer;
    }
    else
    {
        /* Copy channels into local array */
        userBuffer = stream->blockingUserBufferCopy;
        memcpy( userBuffer, buffer, sizeof (void *) * stream->input->hpiFormat.wChannels );
    }

    while( frames > 0 )
    {
        unsigned long framesGot, framesAvail;
        PaStreamCallbackFlags cbFlags = 0;

        PA_ENSURE_( PaAsiHpi_WaitForFrames( stream, &framesAvail, &cbFlags ) );
        framesGot = PA_MIN( framesAvail, frames );
        PA_ENSURE_( PaAsiHpi_BeginProcessing( stream, &framesGot, &cbFlags ) );

        if( framesGot > 0 )
        {
            framesGot = PaUtil_CopyInput( &stream->bufferProcessor, &userBuffer, framesGot );
            PA_ENSURE_( PaAsiHpi_EndProcessing( stream, framesGot, &cbFlags ) );
            /* Advance frame counter */
            frames -= framesGot;
        }
    }

error:
    return result;
}


/** Write data to output stream.
 This writes the indicated number of frames from the supplied buffer to an output stream,
 and blocks until this is done.

 @param s Pointer to PortAudio stream

 @param buffer Pointer to buffer that provides interleaved data (or an array of pointers
               to a buffer for each non-interleaved channel)

 @param frames Number of frames to write to stream

 @return PortAudio error code (also indicates underflow via paOutputUnderflowed)
 */
static PaError WriteStream( PaStream *s,
                            const void *buffer,
                            unsigned long frames )
{
    PaError result = paNoError;
    PaAsiHpiStream *stream = (PaAsiHpiStream*)s;
    PaAsiHpiStreamInfo info;
    const void *userBuffer;

    assert( stream );
    PA_UNLESS_( stream->output, paCanNotWriteToAnInputOnlyStream );

    /* Check for output underflow since previous call to WriteStream */
    PA_ENSURE_( PaAsiHpi_GetStreamInfo( stream->output, &info ) );
    if( info.underflow )
    {
        result = paOutputUnderflowed;
    }

    /* NB Make copy of user buffer pointers, since they are advanced by buffer processor */
    if( stream->bufferProcessor.userOutputIsInterleaved )
    {
        userBuffer = buffer;
    }
    else
    {
        /* Copy channels into local array */
        userBuffer = stream->blockingUserBufferCopy;
        memcpy( (void *)userBuffer, buffer, sizeof (void *) * stream->output->hpiFormat.wChannels );
    }

    while( frames > 0 )
    {
        unsigned long framesGot, framesAvail;
        PaStreamCallbackFlags cbFlags = 0;

        PA_ENSURE_( PaAsiHpi_WaitForFrames( stream, &framesAvail, &cbFlags ) );
        framesGot = PA_MIN( framesAvail, frames );
        PA_ENSURE_( PaAsiHpi_BeginProcessing( stream, &framesGot, &cbFlags ) );

        if( framesGot > 0 )
        {
            framesGot = PaUtil_CopyOutput( &stream->bufferProcessor, &userBuffer, framesGot );
            PA_ENSURE_( PaAsiHpi_EndProcessing( stream, framesGot, &cbFlags ) );
            /* Advance frame counter */
            frames -= framesGot;
        }
    }

error:
    return result;
}


/** Number of frames that can be read from input stream without blocking.

 @param s Pointer to PortAudio stream

 @return Number of frames, or PortAudio error code
 */
static signed long GetStreamReadAvailable( PaStream *s )
{
    PaError result = paNoError;
    PaAsiHpiStream *stream = (PaAsiHpiStream*)s;
    PaAsiHpiStreamInfo info;

    assert( stream );
    PA_UNLESS_( stream->input, paCanNotReadFromAnOutputOnlyStream );

    PA_ENSURE_( PaAsiHpi_GetStreamInfo( stream->input, &info ) );
    /* Round down to the nearest host buffer multiple */
    result = (info.availableFrames / stream->maxFramesPerHostBuffer) * stream->maxFramesPerHostBuffer;
    if( info.overflow )
    {
        result = paInputOverflowed;
    }

error:
    return result;
}


/** Number of frames that can be written to output stream without blocking.

 @param s Pointer to PortAudio stream

 @return Number of frames, or PortAudio error code
 */
static signed long GetStreamWriteAvailable( PaStream *s )
{
    PaError result = paNoError;
    PaAsiHpiStream *stream = (PaAsiHpiStream*)s;
    PaAsiHpiStreamInfo info;

    assert( stream );
    PA_UNLESS_( stream->output, paCanNotWriteToAnInputOnlyStream );

    PA_ENSURE_( PaAsiHpi_GetStreamInfo( stream->output, &info ) );
    /* Round down to the nearest host buffer multiple */
    result = (info.availableFrames / stream->maxFramesPerHostBuffer) * stream->maxFramesPerHostBuffer;
    if( info.underflow )
    {
        result = paOutputUnderflowed;
    }

error:
    return result;
}