aboutsummaryrefslogtreecommitdiff
path: root/pd/portaudio_v18/pa_mac_core/pa_mac_core.c
blob: 983d0e4fda6292088f5c888a0a6ec79cbd494370 (plain)
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
/*
 * $Id: pa_mac_core.c,v 1.8.4.13 2003/12/09 19:04:54 philburk Exp $
 * pa_mac_core.c
 * Implementation of PortAudio for Mac OS X Core Audio
 *
 * PortAudio Portable Real-Time Audio Library
 * Latest Version at: http://www.portaudio.com
 *
 * Authors: Ross Bencina and Phil Burk
 * Copyright (c) 1999-2002 Ross Bencina and Phil Burk
 *
 * Theory of Operation
 *
 * This code uses the HAL (Hardware Access Layer) of the Apple CoreAudio library.
 * This is the layer closes to the hardware.
 * The HAL layer only supports the native HW supported sample rates.
 * So if the chip only supports 44100 Hz, then the HAL only supports 44100.
 * To provide other rates we use the handy Apple AudioConverter which provides
 * sample rate conversion, mono-to-stereo conversion, and buffer size adaptation.
 *
 * There are four modes of operation:
 *    PA_MODE_OUTPUT_ONLY,
 *    PA_MODE_INPUT_ONLY,
 *    PA_MODE_IO_ONE_DEVICE,
 *    PA_MODE_IO_TWO_DEVICES
 *    
 * The processing pipeline for PA_MODE_IO_ONE_DEVICE is in one thread:
 *
 * PaOSX_CoreAudioIOCallback() input buffers -> RingBuffer -> input.AudioConverter ->
 *    PortAudio callback -> output.AudioConverter -> PaOSX_CoreAudioIOCallback() output buffers
 *
 * For two separate devices, we have to use two separate callbacks.
 * We pass data between them using a RingBuffer FIFO.
 * The processing pipeline for PA_MODE_IO_TWO_DEVICES is split into two threads:
 *
 * PaOSX_CoreAudioInputCallback() input buffers -> RingBuffer
 *
 * RingBuffer -> input.AudioConverter ->
 *    PortAudio callback -> output.AudioConverter -> PaOSX_CoreAudioIOCallback() output buffers
 *
 * License
 *
 * 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.
 *
 * 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.
 *
 * 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.
 *
 * CHANGE HISTORY:
 
 3.29.2001 - Phil Burk - First pass... converted from Window MME code with help from Darren.
 3.30.2001 - Darren Gibbs - Added more support for dynamically querying device info.
 12.7.2001 - Gord Peters - Tweaks to compile on PA V17 and OS X 10.1
 2.7.2002 - Darren and Phil - fixed isInput so GetProperty works better, 
             fixed device queries for numChannels and sampleRates,
            one CoreAudio device now maps to separate input and output PaDevices,
            audio input works if using same CoreAudio device (some HW devices make separate CoreAudio devices).
 2.22.2002 - Stephane Letz - Explicit cast needed for compilation with Code Warrior 7
 3.19.2002 - Phil Burk - Added paInt16, paInt8, format using new "pa_common/pa_convert.c" file.
            Return error if opened in mono mode cuz not supported. [Supported 10.12.2002]
            Add support for Pa_GetCPULoad();
            Fixed timestamp in callback and Pa_StreamTime() (Thanks n++k for the advice!)
            Check for invalid sample rates and return an error.
            Check for getenv("PA_MIN_LATENCY_MSEC") to set latency externally.
            Better error checking for invalid channel counts and invalid devices.
 3.29.2002 - Phil Burk - Fixed Pa_GetCPULoad() for small buffers.
 3.31.2002 - Phil Burk - Use getrusage() instead of gettimeofday() for CPU Load calculation.
 10.12.2002 - Phil Burk - Use AudioConverter to allow wide range of sample rates, and mono.
              Use FIFO (from pablio/rinbuffer.h) so that we can pull data through converter.
              Added PaOSX_FixVolumeScalar() to make iMic audible.
 10.17.2002 - Phil Burk - Support full duplex between two different devices.
              Name internal functions PaOSX_*
              Dumped useless PA_MIN_LATENCY_MSEC environment variable.
              Use kAudioDevicePropertyStreamFormatMatch to determine max channels.
 02.03.2003 - Phil Burk - always use AudioConverters so that we can adapt when format changes.
              Synchronize with device when format changes.
 02.13.2003 - Phil Burk - scan for maxChannels because FormatMatch won't tell us.
 03.05.2003 - Phil Burk and Dominic Mazzoni - interleave and deinterleave multiple
              CoreAudio buffers. Needed for MOTU828 and some other N>2 channel devices.
              See code related to "streamInterleavingBuffer".
 03.06.2003 - Phil Burk and Ryan Francesconi - fixed numChannels query for MOTU828.
              Handle fact that MOTU828 gives you 8 channels even when you ask for 2!
 04.06.2003 - Phil Burk - Combine Dominic Mazzoni's technique of using Configuration to query maxChannels
              with old technique of scanning for mormat.
              Increase channel scan by 1 to handle mono USB microphones.
              Do not merge or split channels in AudioConverter to handle 2+2 channels
              of Quattro which has a format of 2 channels.
 04.07.2003 - Phil Burk - use AudioGetCurrentHostTime instead of getrusage() which can lock threads.
 04.10.2003 - Phil Burk - fixed pointer bug with input deinterleaving loop.
              Detect and ignore NULL inputData and outputData in CodeAudio callback.
              Overlap creation and deletion of AudioConverters to prevent thread death when device rate changes.
 04.16.2003 - Phil Burk - Fixed input channel scrambling when numChannels != 2^N. Caused by alignment
              error when filling RingBuffer with 2^N zero bytes.
 04.26.2003 - Phil Burk - Removed code to turn up volume and unmute to prevent blown eardrums.
 12.08.2003 - Phil Burk - Move declaration of oldConverter to top of PAOSX_DevicePropertyListener()
 12.08.2003 - Phil Burk - Removed need for #include "pa_trace.h", just for debug
 12.09.2003 - Phil Burk - Only change sampleRate or numChannels if we need to improve over
              current setting.
*/

#include <CoreServices/CoreServices.h>
#include <CoreAudio/CoreAudio.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <AudioUnit/AudioUnit.h>
#include <AudioToolbox/DefaultAudioOutput.h>
#include <AudioToolbox/AudioConverter.h>
#include <CoreAudio/HostTime.h>

#include "portaudio.h"
#include "pa_host.h"
#include "ringbuffer.h"

/************************************************* Configuration ********/
#define PA_ENABLE_LOAD_MEASUREMENT  (1)

/************************************************* Constants ********/
#define SET_DEVICE_BUFFER_SIZE   (1)

/* To trace program, enable TRACE_REALTIME_EVENTS in pa_trace.h */
#define PA_TRACE_RUN             (0)
#define PA_TRACE_START_STOP      (0)

#define PA_MIN_LATENCY_MSEC      (20) /* FIXME */
#define MIN_TIMEOUT_MSEC         (3000)

#define PRINT(x) { printf x; fflush(stdout); }
#define PRINT_ERR( msg, err ) PRINT(( msg ": error = 0x%0lX = '%s'\n", (err), ErrorToString(err)) )
#define DBUG(x)    /* PRINT(x) */
#define DBUGBACK(x) /* if( sMaxBackgroundErrorMessages-- > 0 ) PRINT(x) */
#define DBUGX(x)

// define value of isInput passed to CoreAudio routines
#define IS_INPUT    (true)
#define IS_OUTPUT   (false)

typedef enum PaDeviceMode
{
    PA_MODE_OUTPUT_ONLY,
    PA_MODE_INPUT_ONLY,
    PA_MODE_IO_ONE_DEVICE,
    PA_MODE_IO_TWO_DEVICES
} PaDeviceMode;

#define PA_USING_OUTPUT   (pahsc->mode != PA_MODE_INPUT_ONLY)
#define PA_USING_INPUT    (pahsc->mode != PA_MODE_OUTPUT_ONLY)

/**************************************************************
 * Information needed by PortAudio specific to a CoreAudio device.
 */
typedef struct PaHostInOut_s
{
    AudioDeviceID      audioDeviceID; /* CoreAudio specific ID */
    int                bytesPerUserNativeBuffer; /* User buffer size in native host format. Depends on numChannels. */
    AudioConverterRef  converter;
    void              *converterBuffer;
    int                numChannels;
    /** Used for interleaving or de-interleaving multiple streams for devices like MOTU828. */
    int                streamInterleavingBufferLen; /**< size in bytes */
    Float32           *streamInterleavingBuffer; 
} PaHostInOut;

/**************************************************************
 * Structure for internal host specific stream data.
 * This is allocated on a per stream basis.
 */
typedef struct PaHostSoundControl
{
    PaHostInOut        input;
    PaHostInOut        output;
    AudioDeviceID      primaryDeviceID;
    PaDeviceMode       mode;
    RingBuffer         ringBuffer;
    char              *ringBufferData;
    Boolean            formatListenerCalled;
    /* For measuring CPU utilization. */
    UInt64             entryTime;
    double             inverseHostTicksPerBuffer; /* 1/Ticks of real-time audio per user buffer. */
} PaHostSoundControl;

/**************************************************************
 * Structure for internal extended device info query.
 * There will be one or two PortAudio devices for each Core Audio device:
 *   one input and or one output.
 */
typedef struct PaHostDeviceInfo
{
    PaDeviceInfo      paInfo;
    AudioDeviceID     audioDeviceID;
}
PaHostDeviceInfo;

/************************************************* Shared Data ********/
/* FIXME - put Mutex around this shared data. */
static int sNumPaDevices = 0;   /* Total number of PaDeviceInfos */
static int sNumInputDevices = 0; /* Total number of input PaDeviceInfos */
static int sNumOutputDevices = 0;
static int sNumCoreDevices = 0;
static AudioDeviceID *sCoreDeviceIDs;   // Array of Core AudioDeviceIDs
static PaHostDeviceInfo *sDeviceInfos = NULL;
static int sDefaultInputDeviceID = paNoDevice;
static int sDefaultOutputDeviceID = paNoDevice;
static int sSavedHostError = 0;

static const double supportedSampleRateRange[] = { 8000.0, 96000.0 }; /* FIXME - go to double HW rate. */
static const char sMapperSuffixInput[] = " - Input";
static const char sMapperSuffixOutput[] = " - Output";

/* Debug support. */
//static int sMaxBackgroundErrorMessages = 100;
//static int sCoverageCounter = 1; // used to check code coverage during validation

/* We index the input devices first, then the output devices. */
#define LOWEST_INPUT_DEVID     (0)
#define HIGHEST_INPUT_DEVID    (sNumInputDevices - 1)
#define LOWEST_OUTPUT_DEVID    (sNumInputDevices)
#define HIGHEST_OUTPUT_DEVID   (sNumPaDevices - 1)

/************************************************* Macros ********/

/************************************************* Prototypes **********/

static PaError PaOSX_QueryDevices( void );
static int PaOSX_ScanDevices( Boolean isInput );
static int PaOSX_QueryDeviceInfo( PaHostDeviceInfo *hostDeviceInfo, int coreDeviceIndex, Boolean isInput );
static PaDeviceID PaOSX_QueryDefaultInputDevice( void );
static PaDeviceID PaOSX_QueryDefaultOutputDevice( void );
static void PaOSX_CalcHostBufferSize( internalPortAudioStream *past );

static OSStatus PAOSX_DevicePropertyListener (AudioDeviceID					inDevice,
								UInt32							inChannel,
								Boolean							isInput,
								AudioDevicePropertyID			inPropertyID,
								void*							inClientData);
                                
/**********************************************************************/
/* OS X errors are 4 character ID that can be printed.
 * Note that uses a static pad so result must be printed immediately.
 */
static OSStatus statusText[2] = { 0, 0 };
static const char *ErrorToString( OSStatus err )
{
    const char *str;

    switch (err)
    {
    case kAudioHardwareUnspecifiedError:
        str = "kAudioHardwareUnspecifiedError";
        break;
    case kAudioHardwareNotRunningError:
        str = "kAudioHardwareNotRunningError";
        break;
    case kAudioHardwareUnknownPropertyError:
        str = "kAudioHardwareUnknownPropertyError";
        break;
    case kAudioDeviceUnsupportedFormatError:
        str = "kAudioDeviceUnsupportedFormatError";
        break;
    case kAudioHardwareBadPropertySizeError:
        str = "kAudioHardwareBadPropertySizeError";
        break;
    case kAudioHardwareIllegalOperationError:
        str = "kAudioHardwareIllegalOperationError";
        break;
    default:
        statusText[0] = err;
    	str = (const char *)statusText;
        break;
    }

    return str;
}

/**********************************************************************/
static unsigned long RoundUpToNextPowerOf2( unsigned long n )
{
    long numBits = 0;
    if( ((n-1) & n) == 0) return n; /* Already Power of two. */
    while( n > 0 )
    {
        n= n>>1;
        numBits++;
    }
    return (1<<numBits);
}

/********************************* BEGIN CPU UTILIZATION MEASUREMENT ****/
static void Pa_StartUsageCalculation( internalPortAudioStream   *past )
{
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    if( pahsc == NULL ) return;
    /* Query user CPU timer for usage analysis and to prevent overuse of CPU. */
    pahsc->entryTime = AudioGetCurrentHostTime();
}

/******************************************************************************
** Measure fractional CPU load based on real-time it took to calculate
** buffers worth of output.
*/
static void Pa_EndUsageCalculation( internalPortAudioStream   *past )
{
    UInt64   exitTime;
    UInt64   ticksElapsed;
    double   newUsage;
    
#define LOWPASS_COEFFICIENT_0   (0.95)
#define LOWPASS_COEFFICIENT_1   (0.99999 - LOWPASS_COEFFICIENT_0)

    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    if( pahsc == NULL ) return;
    
    exitTime = AudioGetCurrentHostTime();
    
    ticksElapsed = exitTime - pahsc->entryTime;

    /* Use inverse because it is faster than the divide. */
	newUsage =  ticksElapsed * pahsc->inverseHostTicksPerBuffer;
	/* Low pass filter result. */
    past->past_Usage = (LOWPASS_COEFFICIENT_0 * past->past_Usage) +
                           (LOWPASS_COEFFICIENT_1 * newUsage);
}
/****************************************** END CPU UTILIZATION *******/

/************************************************************************/
static PaDeviceID PaOSX_QueryDefaultInputDevice( void )
{
    OSStatus      err = noErr;
    UInt32        count;
    int           i;
    AudioDeviceID tempDeviceID = kAudioDeviceUnknown;
    PaDeviceID    defaultDeviceID = paNoDevice;

    // get the default output device for the HAL
    // it is required to pass the size of the data to be returned
    count = sizeof(tempDeviceID);
    err = AudioHardwareGetProperty( kAudioHardwarePropertyDefaultInputDevice,  &count, (void *) &tempDeviceID);
    if (err != noErr) goto error;
    
    // scan input devices to see which one matches this device
    defaultDeviceID = paNoDevice;
    for( i=LOWEST_INPUT_DEVID; i<=HIGHEST_INPUT_DEVID; i++ )
    {
        DBUG(("PaOSX_QueryDefaultInputDevice: i = %d, aDevId = %ld\n", i, sDeviceInfos[i].audioDeviceID ));
        if( sDeviceInfos[i].audioDeviceID == tempDeviceID )
        {
            defaultDeviceID = i;
            break;
        }
    }
error:
    return defaultDeviceID;
}

/************************************************************************/
static PaDeviceID PaOSX_QueryDefaultOutputDevice( void )
{
    OSStatus      err = noErr;
    UInt32        count;
    int           i;
    AudioDeviceID tempDeviceID = kAudioDeviceUnknown;
    PaDeviceID    defaultDeviceID = paNoDevice;

    // get the default output device for the HAL
    // it is required to pass the size of the data to be returned
    count = sizeof(tempDeviceID);
    err = AudioHardwareGetProperty( kAudioHardwarePropertyDefaultOutputDevice,  &count, (void *) &tempDeviceID);
    if (err != noErr) goto error;
    
    // scan output devices to see which one matches this device
    defaultDeviceID = paNoDevice;
    for( i=LOWEST_OUTPUT_DEVID; i<=HIGHEST_OUTPUT_DEVID; i++ )
    {
        DBUG(("PaOSX_QueryDefaultOutputDevice: i = %d, aDevId = %ld\n", i, sDeviceInfos[i].audioDeviceID ));
        if( sDeviceInfos[i].audioDeviceID == tempDeviceID )
        {
            defaultDeviceID = i;
            break;
        }
    }
error:
    return defaultDeviceID;
}

/******************************************************************/
static PaError PaOSX_QueryDevices( void )
{
    OSStatus err = noErr;
    UInt32   outSize;
    Boolean  outWritable;
    int      numBytes;

    // find out how many Core Audio devices there are, if any
    outSize = sizeof(outWritable);
    err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &outSize, &outWritable);
    if (err != noErr)
    {
        PRINT_ERR("Couldn't get info about list of audio devices", err);
        sSavedHostError = err;
        return paHostError;
    }
        
    // calculate the number of device available
    sNumCoreDevices = outSize / sizeof(AudioDeviceID);

    // Bail if there aren't any devices
    if (sNumCoreDevices < 1)
    {
        PRINT(("No Devices Available"));
        return paHostError;
    }
    
    // make space for the devices we are about to get
    sCoreDeviceIDs =  (AudioDeviceID *)malloc(outSize);

    // get an array of AudioDeviceIDs
    err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &outSize, (void *)sCoreDeviceIDs);
    if (err != noErr)
    {
        PRINT_ERR("Couldn't get list of audio device IDs", err);
        sSavedHostError = err;
        return paHostError;
    }

    // Allocate structures to hold device info pointers.
    // There will be a maximum of two Pa devices per Core Audio device, input and/or output.
    numBytes = sNumCoreDevices * 2 * sizeof(PaHostDeviceInfo);
    sDeviceInfos = (PaHostDeviceInfo *) PaHost_AllocateFastMemory( numBytes );
    if( sDeviceInfos == NULL ) return paInsufficientMemory;

    // Scan all the Core Audio devices to see which support input and allocate a
    // PaHostDeviceInfo structure for each one.
    DBUG(("PaOSX_QueryDevices: scan for input ======================\n"));
    PaOSX_ScanDevices( IS_INPUT );
    sNumInputDevices = sNumPaDevices;
    // Now scan all the output devices.
    DBUG(("PaOSX_QueryDevices: scan for output ======================\n"));
    PaOSX_ScanDevices( IS_OUTPUT );
    sNumOutputDevices = sNumPaDevices - sNumInputDevices;

    // Figure out which of the devices that we scanned is the default device.
    sDefaultInputDeviceID = PaOSX_QueryDefaultInputDevice();
    sDefaultOutputDeviceID = PaOSX_QueryDefaultOutputDevice();

    return paNoError;
}


/*************************************************************************/
/* Query a device for its sample rate.
 * @return positive rate or 0.0 on error.
 */
static Float64 PaOSX_GetDeviceSampleRate( AudioDeviceID deviceID, Boolean isInput )
{
    OSStatus  err = noErr;
    AudioStreamBasicDescription formatDesc;
    UInt32    dataSize;
    dataSize = sizeof(formatDesc);
    err = AudioDeviceGetProperty( deviceID, 0, isInput,
        kAudioDevicePropertyStreamFormat, &dataSize, &formatDesc);
    if( err != noErr ) return 0.0;
    else return formatDesc.mSampleRate;
}

/*************************************************************************/
/* Allocate a string containing the device name. */
static char *PaOSX_DeviceNameFromID(AudioDeviceID deviceID, Boolean isInput )
{
    OSStatus err = noErr;
    UInt32  outSize;
    Boolean  outWritable;
    char     *deviceName = nil;
    
    // query size of name
    err =  AudioDeviceGetPropertyInfo(deviceID, 0, isInput, kAudioDevicePropertyDeviceName, &outSize, &outWritable);
    if (err == noErr)
    {
        deviceName =  (char*)malloc( outSize + 1);
        if( deviceName )
        {
            err = AudioDeviceGetProperty(deviceID, 0, isInput, kAudioDevicePropertyDeviceName, &outSize, deviceName);
            if (err != noErr)
                PRINT_ERR("Couldn't get audio device name", err);
        }
    }

    return deviceName;
}

/*************************************************************************
** Scan all of the Core Audio devices to see which support selected
** input or output mode.
** Changes sNumDevices, and fills in sDeviceInfos.
*/
static int PaOSX_ScanDevices( Boolean isInput )
{
    int coreDeviceIndex;
    int result;
    PaHostDeviceInfo  *hostDeviceInfo;
    int numAdded = 0;

    for(  coreDeviceIndex=0; coreDeviceIndex<sNumCoreDevices; coreDeviceIndex++ )
    {
        // try to fill in next PaHostDeviceInfo
        hostDeviceInfo = &sDeviceInfos[sNumPaDevices];
        result = PaOSX_QueryDeviceInfo( hostDeviceInfo, coreDeviceIndex, isInput );
        DBUG(("PaOSX_ScanDevices: paDevId = %d, coreDevId = %d, result = %d\n", sNumPaDevices, coreDeviceIndex, result ));
        if( result > 0 )
        {
            sNumPaDevices += 1;  // bump global counter if we got one
            numAdded += 1;
        }
        else if( result < 0 ) return result;
    }
    return numAdded;
}

/*************************************************************************
** Determine the maximum number of channels based on the configuration.
** @return maxChannels or negative error.
*/
static int PaOSX_GetMaxChannels_Config( AudioDeviceID devID, Boolean isInput )
{
    OSStatus         err;
    UInt32           outSize;
    Boolean          outWritable;
    AudioBufferList *list;
    int              numChannels;
    int              i;

    // Determine maximum number of channels supported.
    // dmazzoni: new method

    outSize = 0;
    err = AudioDeviceGetPropertyInfo(devID, 0, isInput,
                                     kAudioDevicePropertyStreamConfiguration,
                                     &outSize, &outWritable);
    if ( err != noErr )
    {
        PRINT_ERR("PaOSX_GetMaxChannels_Config: Could not get stream configuration info", err);
        sSavedHostError = err;
        return paHostError;
    }

    list = (AudioBufferList *)PaHost_AllocateFastMemory( outSize );
    err = AudioDeviceGetProperty(devID, 0, isInput,
                                 kAudioDevicePropertyStreamConfiguration,
                                 &outSize, list);
    if ( err != noErr )
    {
        PRINT_ERR("PaOSX_GetMaxChannels_Config: Could not get stream configuration", err);
        sSavedHostError = err;
        return paHostError;
    }

    numChannels = 0;
    for( i=0; i<list->mNumberBuffers; i++ )
    {
        int bufChannels = list->mBuffers[i].mNumberChannels;
        DBUG(("PaOSX_GetMaxChannels_Config: buffer %d has %d channels.\n", i, bufChannels ));
        numChannels += bufChannels;
    }
    
    PaHost_FreeFastMemory( list, outSize );

    return numChannels;
}

/*************************************************************************
** Determine the maximum number of channels a device will support based on scanning the format.
** @return maxChannels or negative error.
*/
static int PaOSX_GetMaxChannels_Format( AudioDeviceID devID, Boolean isInput )
{
    OSStatus         err;
    UInt32           outSize;
    AudioStreamBasicDescription formatDesc;
    int              maxChannels;
    int              numChannels;
    Boolean          gotMax;

	// Scan to find highest matching format.
    // Unfortunately some devices won't just return maxChannels for the match.
    // For example, some 8 channel devices return 2 when given 256 as input.
    gotMax = false;
    maxChannels = 0;
    numChannels = 0;
    while( !gotMax )
    {
    
        memset( &formatDesc, 0, sizeof(formatDesc));
        numChannels = numChannels + 1;
        DBUG(("PaOSX_GetMaxChannels: try numChannels = %d = %d + 1\n",
            numChannels, numChannels ));
        formatDesc.mChannelsPerFrame = numChannels;
        outSize = sizeof(formatDesc);

        err = AudioDeviceGetProperty( devID, 0,
            isInput, kAudioDevicePropertyStreamFormatMatch, &outSize, &formatDesc);

        DBUG(("PaOSX_GetMaxChannels: err 0x%0x, formatDesc.mChannelsPerFrame= %d\n",
            err, formatDesc.mChannelsPerFrame ));
        if( err != noErr )
        {
            if (numChannels > (maxChannels + 4)) // Try several possibilities above current max
			{
                gotMax = true;
            }
        }
        else
        {
            // This value worked so we have a new candidate for maxChannels.
            if (formatDesc.mChannelsPerFrame > numChannels)
            {
            	maxChannels =  formatDesc.mChannelsPerFrame;
            }
            else if(formatDesc.mChannelsPerFrame < numChannels)
            {
                if (numChannels > (maxChannels + 4)) // Try several possibilities above current max
                {
                    gotMax = true;
                }
            }
            else
            {
            	maxChannels = numChannels;            	
            }
        }
    }
    return maxChannels;
}



/*************************************************************************
** Determine the maximum number of channels a device will support.
** It is not clear at this point which the better technique so
** we do both and use the biggest result.
**
** @return maxChannels or negative error.
*/
static int PaOSX_GetMaxChannels( AudioDeviceID devID, Boolean isInput )
{
    int maxChannelsFormat;
    int maxChannelsConfig;
    maxChannelsFormat = PaOSX_GetMaxChannels_Format( devID, isInput );
    maxChannelsConfig = PaOSX_GetMaxChannels_Config( devID, isInput );
    return (maxChannelsFormat > maxChannelsConfig) ? maxChannelsFormat : maxChannelsConfig;
}

/*************************************************************************
** Try to fill in the device info for this device.
** Return 1 if a good device that PA can use.
** Return 0 if not appropriate
** or return negative error.
**
*/
static int PaOSX_QueryDeviceInfo( PaHostDeviceInfo *hostDeviceInfo, int coreDeviceIndex, Boolean isInput )
{
    OSStatus         err;
    UInt32           outSize;
    AudioStreamBasicDescription formatDesc;
    AudioDeviceID    devID;
    PaDeviceInfo    *deviceInfo = &hostDeviceInfo->paInfo;
    int              maxChannels;

    deviceInfo->structVersion = 1;
    deviceInfo->maxInputChannels = 0;
    deviceInfo->maxOutputChannels = 0;

    deviceInfo->sampleRates = supportedSampleRateRange; // because we use sample rate converter to get continuous rates
    deviceInfo->numSampleRates = -1;

    devID = sCoreDeviceIDs[ coreDeviceIndex ];
    hostDeviceInfo->audioDeviceID = devID;
    DBUG(("PaOSX_QueryDeviceInfo: coreDeviceIndex = %d, devID = %d, isInput = %d\n",
        coreDeviceIndex, (int) devID, isInput ));
        
    // Get data format info from the device.
    outSize = sizeof(formatDesc);
    err = AudioDeviceGetProperty(devID, 0, isInput, kAudioDevicePropertyStreamFormat, &outSize, &formatDesc);
    // This just may not be an appropriate device for input or output so leave quietly.
    if( (err != noErr)  || (formatDesc.mChannelsPerFrame == 0) ) goto error;
    
    DBUG(("PaOSX_QueryDeviceInfo: mFormatID = 0x%x\n", (unsigned int) formatDesc.mFormatID));
    DBUG(("PaOSX_QueryDeviceInfo: mFormatFlags = 0x%x\n",(unsigned int)  formatDesc.mFormatFlags));

    // Right now the Core Audio headers only define one formatID: LinearPCM
    // Apparently LinearPCM must be Float32 for now.
    if( (formatDesc.mFormatID == kAudioFormatLinearPCM) &&
        ((formatDesc.mFormatFlags & kLinearPCMFormatFlagIsFloat) != 0) )
    {
        deviceInfo->nativeSampleFormats = paFloat32;
    }
    else
    {
        PRINT(("PaOSX_QueryDeviceInfo: ERROR - not LinearPCM & Float32!!!\n"));
        return paSampleFormatNotSupported;
    }
    
    maxChannels = PaOSX_GetMaxChannels( devID, isInput );
    if( maxChannels <= 0 ) goto error;
    if( isInput )
    {
        deviceInfo->maxInputChannels = maxChannels;
    }
    else
    {
        deviceInfo->maxOutputChannels = maxChannels;
    }
    
    // Get the device name
    deviceInfo->name = PaOSX_DeviceNameFromID( devID, isInput );
    DBUG(("PaOSX_QueryDeviceInfo: name = %s\n", deviceInfo->name ));
    return 1;

error:
    return 0;
}

/**********************************************************************/
static PaError PaOSX_MaybeQueryDevices( void )
{
    if( sNumPaDevices == 0 )
    {
        return PaOSX_QueryDevices();
    }
    return 0;
}

static char zeroPad[256] = { 0 };

/**********************************************************************
** This is the proc that supplies the data to the AudioConverterFillBuffer call.
** We can pass back arbitrarily sized blocks so if the FIFO region is split
** just pass back the first half.
*/
static OSStatus PaOSX_InputConverterCallbackProc (AudioConverterRef			inAudioConverter,
								UInt32*						outDataSize,
								void**						outData,
								void*						inUserData)
{
    internalPortAudioStream   *past = (internalPortAudioStream *) inUserData;
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    void *dataPtr1;
    long size1;
    void *dataPtr2;
    long size2;
           
    /* Pass contiguous region from FIFO directly to converter. */
    RingBuffer_GetReadRegions( &pahsc->ringBuffer, *outDataSize,
            &dataPtr1, &size1, &dataPtr2, &size2 );

    if( size1 > 0 )
    {
        *outData = dataPtr1;
        *outDataSize = size1;
        RingBuffer_AdvanceReadIndex( &pahsc->ringBuffer, size1 );
        DBUGX(("PaOSX_InputConverterCallbackProc: read %ld bytes from FIFO.\n", size1 ));
    }
    else
    {
        DBUGBACK(("PaOSX_InputConverterCallbackProc: got no data!\n"));
        *outData = zeroPad; /* Give it zero data to keep it happy. */
        *outDataSize = sizeof(zeroPad);
    }
	return noErr;
}

/*****************************************************************************
** Get audio input, if any, from passed in buffer, or from converter or from FIFO,
** then run PA callback and output data.
*/
static OSStatus PaOSX_LoadAndProcess( internalPortAudioStream   *past,
    void *inputBuffer, void *outputBuffer )
{
    OSStatus err = noErr;
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    
    if( past->past_StopSoon )
    {
        if( outputBuffer )
        {
            /* Clear remainder of audio buffer if we are waiting for stop. */
            memset( outputBuffer, 0, pahsc->output.bytesPerUserNativeBuffer );
        }
    }
    else
    {
        /* Do we need data from the converted input? */
        if( PA_USING_INPUT )
        {
            UInt32 size = pahsc->input.bytesPerUserNativeBuffer;
            err = AudioConverterFillBuffer(
                pahsc->input.converter,
                PaOSX_InputConverterCallbackProc,
                past,
                &size,
                pahsc->input.converterBuffer);
            if( err != noErr ) return err;
            inputBuffer = pahsc->input.converterBuffer;
        }
        
        /* Measure CPU load. */
#if PA_ENABLE_LOAD_MEASUREMENT
        Pa_StartUsageCalculation( past );
#endif

        /* Fill part of audio converter buffer by converting input to user format,
        * calling user callback, then converting output to native format. */
        if( PaConvert_Process( past, inputBuffer, outputBuffer ))
        {
            past->past_StopSoon = 1;
        }
        
#if PA_ENABLE_LOAD_MEASUREMENT
        Pa_EndUsageCalculation( past );
#endif

    }
    return err;
}

/*****************************************************************************
** This is the proc that supplies the data to the AudioConverterFillBuffer call
*/
static OSStatus PaOSX_OutputConverterCallbackProc (AudioConverterRef			inAudioConverter,
								UInt32*						outDataSize,
								void**						outData,
								void*						inUserData)
{
    internalPortAudioStream   *past = (internalPortAudioStream *) inUserData;
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    
	*outData = pahsc->output.converterBuffer;
	*outDataSize = pahsc->output.bytesPerUserNativeBuffer;
    
	return PaOSX_LoadAndProcess ( past, pahsc->input.converterBuffer, pahsc->output.converterBuffer );
}

/**********************************************************************
** If data available, write it to the Ring Buffer so we can
** pull it from the other side.
*/
static OSStatus PaOSX_WriteInputRingBuffer( internalPortAudioStream   *past,
        const AudioBufferList*  inInputData  )
{
    int   numBytes = 0;
    int   currentInterleavedChannelIndex;
    int   numFramesInInputBuffer;
    int   numInterleavedChannels;
    int   numChannelsRemaining;
	int   i;
    long  writeRoom;
    char *inputNativeBufferfPtr = NULL;
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;

    /* Do we need to deinterleave the buffers first? */
    if( past->past_NumInputChannels != inInputData->mBuffers[0].mNumberChannels )
    {
		numFramesInInputBuffer = inInputData->mBuffers[0].mDataByteSize /
            (sizeof(float) * inInputData->mBuffers[0].mNumberChannels);
		
		numBytes = numFramesInInputBuffer * sizeof(float) * past->past_NumInputChannels;
	
		/* Allocate temporary buffer if needed. */
		if ( (pahsc->input.streamInterleavingBuffer != NULL) &&
			 (pahsc->input.streamInterleavingBufferLen < numBytes) )
		{
			PaHost_FreeFastMemory( pahsc->input.streamInterleavingBuffer, pahsc->input.streamInterleavingBufferLen );
			pahsc->input.streamInterleavingBuffer = NULL;
		}
		if ( pahsc->input.streamInterleavingBuffer == NULL )
		{
			pahsc->input.streamInterleavingBufferLen = numBytes;
			pahsc->input.streamInterleavingBuffer = (float *)PaHost_AllocateFastMemory( pahsc->input.streamInterleavingBufferLen );
		}
	
		/* Perform interleaving by writing to temp buffer. */
		currentInterleavedChannelIndex = 0;
		numInterleavedChannels = past->past_NumInputChannels;
		numChannelsRemaining = numInterleavedChannels;
		
		for( i=0; i<inInputData->mNumberBuffers; i++ )
		{
			int j;
			int numBufChannels = inInputData->mBuffers[i].mNumberChannels;
			/* Don't use more than we need or more than we have. */
			int numChannelsUsedInThisBuffer = (numChannelsRemaining < numBufChannels ) ?
				  numChannelsRemaining : numBufChannels;
			for( j=0; j<numChannelsUsedInThisBuffer; j++ )
			{
				int k;
                float *dest = &pahsc->input.streamInterleavingBuffer[ currentInterleavedChannelIndex ];
                float *src = &((float *)inInputData->mBuffers[i].mData)[ j ];
				/* Move one channel from CoreAudio buffer to interleaved buffer. */
				for( k=0; k<numFramesInInputBuffer; k++ )
				{
					*dest = *src;
                    src += numBufChannels;
                    dest += numInterleavedChannels;
				}
				currentInterleavedChannelIndex++;
			}
			numChannelsRemaining -= numChannelsUsedInThisBuffer;
			if( numChannelsRemaining <= 0 ) break;
		}

        inputNativeBufferfPtr = (char *)pahsc->input.streamInterleavingBuffer;
    }
    else
    {
        inputNativeBufferfPtr = (char*)inInputData->mBuffers[0].mData;
        numBytes = inInputData->mBuffers[0].mDataByteSize;
    }

    writeRoom = RingBuffer_GetWriteAvailable( &pahsc->ringBuffer );
    
    if( numBytes <= writeRoom )
    {
        RingBuffer_Write(  &pahsc->ringBuffer, inputNativeBufferfPtr, numBytes );
        DBUGBACK(("PaOSX_WriteInputRingBuffer: wrote %ld bytes to FIFO.\n", numBytes));
    } // FIXME else drop samples on floor, remember overflow???            

    return noErr;
}

/**********************************************************************
** Use any available input buffers by writing to RingBuffer.
** Process input if PA_MODE_INPUT_ONLY.
*/
static OSStatus PaOSX_HandleInput( internalPortAudioStream   *past,
        const AudioBufferList*  inInputData )
{
    OSStatus            err = noErr;
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;

    if( inInputData == NULL )
    {
        DBUG(("PaOSX_HandleInput: inInputData == NULL\n"));
        return noErr;
    }
    
    if(  inInputData->mNumberBuffers > 0  )
    {    
        /* Write to FIFO here if we are only using this callback. */
        if( (pahsc->mode == PA_MODE_INPUT_ONLY) || (pahsc->mode == PA_MODE_IO_ONE_DEVICE) )
        {
            err = PaOSX_WriteInputRingBuffer( past, inInputData  );
            if( err != noErr ) goto error;
        }
    }
    
    if( pahsc->mode == PA_MODE_INPUT_ONLY )
    {
        /* Generate user buffers as long as we have a half full input FIFO. */
        long halfSize = pahsc->ringBuffer.bufferSize / 2;
        while( (RingBuffer_GetReadAvailable( &pahsc->ringBuffer ) >= halfSize) &&
            (past->past_StopSoon == 0) )
        {
            err = PaOSX_LoadAndProcess ( past, NULL, NULL );
            if( err != noErr ) goto error;
        }
    }
        
error:
    return err;
}

/**********************************************************************
** Fill any available output buffers.
*/
static OSStatus PaOSX_HandleOutput( internalPortAudioStream   *past,
            AudioBufferList*  outOutputData )
{
    OSStatus            err = noErr;
    void               *outputNativeBufferfPtr = NULL;
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    UInt32              numBytes = 0;
    int                 numChannelsRemaining;
    Boolean             deinterleavingNeeded;
    int                 numFramesInOutputBuffer;
    
    if( outOutputData == NULL )
    {
        DBUG(("PaOSX_HandleOutput: outOutputData == NULL\n"));
        return noErr;
    }

    deinterleavingNeeded = past->past_NumOutputChannels != outOutputData->mBuffers[0].mNumberChannels;
    
	numFramesInOutputBuffer = outOutputData->mBuffers[0].mDataByteSize /
        (sizeof(float) * outOutputData->mBuffers[0].mNumberChannels);
    
    if( pahsc->mode != PA_MODE_INPUT_ONLY )
    {
        /* If we are using output, then we need an empty output buffer. */
        if( outOutputData->mNumberBuffers > 0 )
        {
        	
            /* If we have multiple CoreAudio buffers, then we will need to deinterleave after conversion. */
            if( deinterleavingNeeded )
            {
            	numBytes = numFramesInOutputBuffer * sizeof(float) * past->past_NumOutputChannels;
                
                /* Free old buffer if we are allocating new one. */
                if ( (pahsc->output.streamInterleavingBuffer != NULL) &&
                     (pahsc->output.streamInterleavingBufferLen < numBytes) )
                {
                    PaHost_FreeFastMemory( pahsc->output.streamInterleavingBuffer, pahsc->output.streamInterleavingBufferLen );
                    pahsc->output.streamInterleavingBuffer = NULL;
                }
                /* Allocate interleaving buffer if needed. */
                if ( pahsc->output.streamInterleavingBuffer == NULL )
                {
                    pahsc->output.streamInterleavingBufferLen = numBytes;
                    pahsc->output.streamInterleavingBuffer = (float *)PaHost_AllocateFastMemory( pahsc->output.streamInterleavingBufferLen );
                }

                outputNativeBufferfPtr =  (void*)pahsc->output.streamInterleavingBuffer;
            }
            else
            {
                numBytes = outOutputData->mBuffers[0].mDataByteSize;
                outputNativeBufferfPtr =  (void*)outOutputData->mBuffers[0].mData;
            }
    
            /* Pull data from PA user through converter. */
            err = AudioConverterFillBuffer(
                pahsc->output.converter,
                PaOSX_OutputConverterCallbackProc,
                past,
                &numBytes,
                outputNativeBufferfPtr);
            if( err != noErr )
            {
                PRINT_ERR("PaOSX_HandleOutput: AudioConverterFillBuffer failed", err);
                goto error;
            }
            
            /* Deinterleave data from PortAudio and write to multiple CoreAudio buffers. */
            if( deinterleavingNeeded )
            {
                int numInterleavedChannels = past->past_NumOutputChannels;
                int i, currentInterleavedChannelIndex = 0;
                numChannelsRemaining = numInterleavedChannels;
                
                for( i=0; i<outOutputData->mNumberBuffers; i++ )
                {
                    int numBufChannels = outOutputData->mBuffers[i].mNumberChannels;
                    int j;            
                    /* Don't use more than we need or more than we have. */
                	int numChannelsUsedInThisBuffer = (numChannelsRemaining < numBufChannels ) ?
                	      numChannelsRemaining : numBufChannels;
                	
                    for( j=0; j<numChannelsUsedInThisBuffer; j++ )
                    {
                        int k;
                        float *dest = &((float *)outOutputData->mBuffers[i].mData)[ j ];
                        float *src = &pahsc->output.streamInterleavingBuffer[ currentInterleavedChannelIndex ];
                        /* Move one channel from interleaved buffer to CoreAudio buffer. */
                        for( k=0; k<numFramesInOutputBuffer; k++ )
                        {
                            *dest = *src;
                            dest += numBufChannels;
                            src += numInterleavedChannels;
                        }
                        currentInterleavedChannelIndex++;
                    }
                    
                    numChannelsRemaining -= numChannelsUsedInThisBuffer;
                    if( numChannelsRemaining <= 0 ) break;
                }
            }
        }
    }
    
error:
    return err;
}

/******************************************************************
 * This callback is used when two separate devices are used for input and output.
 * This often happens when using USB devices which present as two devices: input and output.
 * It just writes its data to a FIFO so that it can be read by the main callback
 * proc PaOSX_CoreAudioIOCallback().
 */
static OSStatus PaOSX_CoreAudioInputCallback (AudioDeviceID  inDevice, const AudioTimeStamp*  inNow,
                    const AudioBufferList*  inInputData, const AudioTimeStamp*  inInputTime,
                    AudioBufferList*  outOutputData, const AudioTimeStamp* inOutputTime,
                    void* contextPtr)
{
    OSStatus      err = noErr;
    internalPortAudioStream *past = (internalPortAudioStream *) contextPtr;
    PaHostSoundControl *pahsc;
    pahsc = (PaHostSoundControl *) past->past_DeviceData;
   
    /* If there is a FIFO for input then write to it. */
    if( (pahsc->ringBufferData != NULL) && (inInputData != NULL) )
    {
        err = PaOSX_WriteInputRingBuffer( past, inInputData  );
        if( err != noErr ) goto error;
    }
error:
    return err;
}

/******************************************************************
 * This is the primary callback for CoreAudio.
 * It can handle input and/or output for a single device.
 * It takes input from CoreAudio, converts it and passes it to the
 * PortAudio user callback. Then takes the PA results and passes it
 * back to CoreAudio.
 */
static OSStatus PaOSX_CoreAudioIOCallback (AudioDeviceID  inDevice, const AudioTimeStamp*  inNow,
                    const AudioBufferList*  inInputData, const AudioTimeStamp*  inInputTime,
                    AudioBufferList*  outOutputData, const AudioTimeStamp* inOutputTime,
                    void* contextPtr)
{
    OSStatus      err = noErr;
    internalPortAudioStream *past;
    PaHostSoundControl *pahsc;
    past = (internalPortAudioStream *) contextPtr;
    pahsc = (PaHostSoundControl *) past->past_DeviceData;

    /* Has someone asked us to abort by calling Pa_AbortStream()? */
    if( past->past_StopNow )
    {
        past->past_IsActive = 0; /* Will cause thread to return. */
    }
    /* Has someone asked us to stop by calling Pa_StopStream()
     * OR has a user callback returned '1' to indicate finished.
     */
    else if( past->past_StopSoon )
    {
        // FIXME - Pretend all done. Should wait for audio to play out but CoreAudio latency very low.
        past->past_IsActive = 0; /* Will cause thread to return. */
    }
    else
    {
        /* use time stamp from CoreAudio if valid */
        if( inOutputTime->mFlags & kAudioTimeStampSampleTimeValid) 
        {
            past->past_FrameCount = inOutputTime->mSampleTime;
        }
        else if( inInputTime->mFlags & kAudioTimeStampSampleTimeValid) 
        {
            past->past_FrameCount = inInputTime->mSampleTime;
        }
        
        past->past_NumCallbacks += 1;
        
        /* Process full input buffer. */
        err = PaOSX_HandleInput( past, inInputData );
        if( err != 0 ) goto error;
        
        /* Fill up empty output buffers. */
        err = PaOSX_HandleOutput( past, outOutputData );
        if( err != 0 ) goto error;
    }

    if( err != 0 ) DBUG(("PaOSX_CoreAudioIOCallback: returns %ld.\n", err ));
    
error:
    return err;
}

/*******************************************************************/
/** Attempt to set device sample rate.
 * This is not critical because we use an AudioConverter but we may
 * get better fidelity if we can avoid resampling.
 *
 * Only set format once because some devices take time to settle.
 * Return flag indicating whether format changed so we know whether to wait
 * for DevicePropertyListener to get called.
 *
 * @return negative error, zero if no change, or one if changed successfully.
 */
static PaError PaOSX_SetFormat( AudioDeviceID devID, Boolean isInput,
        double desiredRate, int desiredNumChannels )
{
    AudioStreamBasicDescription formatDesc;
    PaError  result = 0;
    OSStatus err;
    UInt32   dataSize;
    Float64  originalRate;
    int      originalChannels;
    
    /* Get current device format. This is critical because if we pass
     * zeros for unspecified fields then the iMic device gets switched to a 16 bit
     * integer format!!! I don't know if this is a Mac bug or not. But it only
     * started happening when I upgraded from OS X V10.1 to V10.2 (Jaguar).
     */
    dataSize = sizeof(formatDesc);
    err = AudioDeviceGetProperty( devID, 0, isInput,
        kAudioDevicePropertyStreamFormat, &dataSize, &formatDesc);
    if( err != noErr )
    {
        PRINT_ERR("PaOSX_SetFormat: Could not get format.", err);
        sSavedHostError = err;
        return paHostError;
    }
    
    originalRate = formatDesc.mSampleRate;
    originalChannels = formatDesc.mChannelsPerFrame;
        
    // Changing the format can mess up other apps.
    // So only change the format if the original format
    // has a lower sample rate, or fewer channels, than the desired format.
    if( (originalRate < desiredRate) || (originalChannels < desiredNumChannels) )
    {
        DBUG(("PaOSX_SetFormat: try to change sample rate from %f to %f.\n", originalRate, desiredRate ));
        DBUG(("PaOSX_SetFormat: try to set number of channels %d to %d\n", originalChannels, desiredNumChannels));

        formatDesc.mSampleRate = desiredRate;
        formatDesc.mChannelsPerFrame = desiredNumChannels;
        formatDesc.mBytesPerFrame = formatDesc.mChannelsPerFrame * sizeof(float);
        formatDesc.mBytesPerPacket = formatDesc.mBytesPerFrame * formatDesc.mFramesPerPacket;

        err = AudioDeviceSetProperty( devID, 0, 0,
            isInput, kAudioDevicePropertyStreamFormat, sizeof(formatDesc), &formatDesc);
        if (err != noErr)
        {
            /* Could not set to desired rate so query for closest match. */
            dataSize = sizeof(formatDesc);
            err = AudioDeviceGetProperty( devID, 0,
                isInput, kAudioDevicePropertyStreamFormatMatch, &dataSize, &formatDesc);
                
            DBUG(("PaOSX_SetFormat: closest rate is %f.\n", formatDesc.mSampleRate ));
            DBUG(("PaOSX_SetFormat: closest numChannels is %d.\n", (int)formatDesc.mChannelsPerFrame ));
            // Set to closest if different from original.
            if( (err == noErr) &&
                ((originalRate != formatDesc.mSampleRate) ||
                 (originalChannels != formatDesc.mChannelsPerFrame)) )
            {
                err = AudioDeviceSetProperty( devID, 0, 0,
                    isInput, kAudioDevicePropertyStreamFormat, sizeof(formatDesc), &formatDesc);
                if( err == noErr ) result = 1;
            }
        }
        else result = 1;
    }
    
    return result;
}

#if 0
static void PaOSX_DumpDeviceInfo( AudioDeviceID devID, Boolean isInput )
{
    OSStatus err = noErr;
    UInt32    dataSize;
    UInt32    uidata32;
    Float32   fdata32;
    AudioValueRange audioRange;
    
    dataSize = sizeof( uidata32 );
    err = AudioDeviceGetProperty( devID, 0, isInput, 
        kAudioDevicePropertyLatency, &dataSize, &uidata32 );
    if( err != noErr )
    {
        PRINT_ERR("Error reading kAudioDevicePropertyLatency", err);
        return;
    }
    PRINT(("kAudioDevicePropertyLatency = %d\n", (int)uidata32 ));
    
    dataSize = sizeof( fdata32 );
    err = AudioDeviceGetProperty( devID, 1, isInput, 
        kAudioDevicePropertyVolumeScalar, &dataSize, &fdata32 );
    if( err != noErr )
    {
        PRINT_ERR("Error reading kAudioDevicePropertyVolumeScalar", err);
        return;
    }
    PRINT(("kAudioDevicePropertyVolumeScalar = %f\n", fdata32 ));
    
    dataSize = sizeof( uidata32 );
    err = AudioDeviceGetProperty( devID, 0, isInput, 
        kAudioDevicePropertyBufferSize, &dataSize, &uidata32 );
    if( err != noErr )
    {
        PRINT_ERR("Error reading buffer size", err);
        return;
    }
    PRINT(("kAudioDevicePropertyBufferSize = %d bytes\n", (int)uidata32 ));

    dataSize = sizeof( audioRange );
    err = AudioDeviceGetProperty( devID, 0, isInput, 
        kAudioDevicePropertyBufferSizeRange, &dataSize, &audioRange );
    if( err != noErr )
    {
        PRINT_ERR("Error reading buffer size range", err);
        return;
    }
    PRINT(("kAudioDevicePropertyBufferSizeRange = %g to %g bytes\n", audioRange.mMinimum, audioRange.mMaximum ));
    
    dataSize = sizeof( uidata32 );
    err = AudioDeviceGetProperty( devID, 0, isInput, 
        kAudioDevicePropertyBufferFrameSize, &dataSize, &uidata32 );
    if( err != noErr )
    {
        PRINT_ERR("Error reading buffer size", err);
        return;
    }
    PRINT(("kAudioDevicePropertyBufferFrameSize = %d frames\n", (int)uidata32 ));
    
    dataSize = sizeof( audioRange );
    err = AudioDeviceGetProperty( devID, 0, isInput, 
        kAudioDevicePropertyBufferFrameSizeRange, &dataSize, &audioRange );
    if( err != noErr )
    {
        PRINT_ERR("Error reading buffer size range", err);
        return;
    }
    PRINT(("kAudioDevicePropertyBufferFrameSizeRange = %g to %g frames\n", audioRange.mMinimum, audioRange.mMaximum ));

    return;
}
#endif

/*******************************************************************/
static OSStatus PAOSX_DevicePropertyListener (AudioDeviceID					inDevice,
								UInt32							inChannel,
								Boolean							isInput,
								AudioDevicePropertyID			inPropertyID,
								void*							inClientData)
{
    PaHostSoundControl       *pahsc;
    internalPortAudioStream  *past;
    UInt32                    dataSize;
    OSStatus                  err = noErr;
	AudioStreamBasicDescription userStreamFormat, hardwareStreamFormat;
    PaHostInOut              *hostInOut;
	AudioStreamBasicDescription *destFormatPtr, *srcFormatPtr;
    AudioConverterRef         oldConverter = NULL; // PLB 20031208 - Declare here for standard 'C'.

    past = (internalPortAudioStream *) inClientData;
    pahsc = (PaHostSoundControl *) past->past_DeviceData;

    DBUG(("PAOSX_DevicePropertyListener: called with propertyID = 0x%0X\n", (unsigned int) inPropertyID ));

    if(inPropertyID == kAudioDevicePropertyStreamFormat)
    {    
        /* Get target device format */
        dataSize = sizeof(hardwareStreamFormat);
        err = AudioDeviceGetProperty(inDevice, 0, isInput,
            kAudioDevicePropertyStreamFormat, &dataSize, &hardwareStreamFormat);
        if( err != noErr )
        {
            PRINT_ERR("PAOSX_DevicePropertyListener: Could not get device format", err);
            sSavedHostError = err;
            goto error;
        }

        DBUG(("PAOSX_DevicePropertyListener: HW mChannelsPerFrame = %d\n", (int)hardwareStreamFormat.mChannelsPerFrame ));
        
        /* Set source user format. */
        userStreamFormat = hardwareStreamFormat;
        userStreamFormat.mSampleRate = past->past_SampleRate;	// sample rate of the user synthesis code
        userStreamFormat.mChannelsPerFrame = (isInput) ? past->past_NumInputChannels : past->past_NumOutputChannels;	//	the number of channels in each frame
        DBUG(("PAOSX_DevicePropertyListener: User mChannelsPerFrame = %d\n", (int)userStreamFormat.mChannelsPerFrame ));
    
        userStreamFormat.mBytesPerFrame = userStreamFormat.mChannelsPerFrame * sizeof(float);
        userStreamFormat.mBytesPerPacket = userStreamFormat.mBytesPerFrame * userStreamFormat.mFramesPerPacket;
    
    	/* Don't use AudioConverter for merging or splitting channels. */
        hardwareStreamFormat.mChannelsPerFrame = userStreamFormat.mChannelsPerFrame;
        hardwareStreamFormat.mBytesPerFrame = userStreamFormat.mBytesPerFrame;
        hardwareStreamFormat.mBytesPerPacket = userStreamFormat.mBytesPerPacket;
    	        
        if( isInput )
        {
            hostInOut = &pahsc->input;
            srcFormatPtr = &hardwareStreamFormat;
            destFormatPtr = &userStreamFormat;
        }
        else
        {
            hostInOut = &pahsc->output;
            srcFormatPtr = &userStreamFormat;
            destFormatPtr = &hardwareStreamFormat;
        }
        DBUG(("PAOSX_DevicePropertyListener: source rate = %f\n", srcFormatPtr->mSampleRate ));
        DBUG(("PAOSX_DevicePropertyListener: dest rate = %f\n", destFormatPtr->mSampleRate ));
        
        // Don't delete old converter until we create new one so we don't pull
        // the rug out from under other audio threads.
        oldConverter = hostInOut->converter;
        
        // Make converter to change sample rate.
        err = AudioConverterNew (
            srcFormatPtr, 
            destFormatPtr, 
            &hostInOut->converter );
        if( err != noErr )
        {
            PRINT_ERR("Could not create format converter", err);
            sSavedHostError = err;
            goto error;
        }
        
        if( oldConverter != NULL )
        {
            verify_noerr( AudioConverterDispose( oldConverter ) );
        }
    }
    
error:
    pahsc->formatListenerCalled = true;
    return err;
}

/* Allocate FIFO between Device callback and Converter callback so that device can push data
* and converter can pull data.
*/
static PaError PaOSX_CreateInputRingBuffer( internalPortAudioStream   *past )
{
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    OSStatus  err = noErr;
    UInt32    dataSize;
    double    sampleRateRatio;
    long      numBytes;
    UInt32    framesPerHostBuffer;
    UInt32    bytesForDevice;
    UInt32    bytesForUser;
    UInt32    bytesPerFrame;
    AudioStreamBasicDescription formatDesc;
    
    dataSize = sizeof(formatDesc);
    err = AudioDeviceGetProperty( pahsc->input.audioDeviceID, 0, IS_INPUT,
        kAudioDevicePropertyStreamFormat, &dataSize, &formatDesc);
    if( err != noErr )
    {
        PRINT_ERR("PaOSX_CreateInputRingBuffer: Could not get input format.\n", err);
        sSavedHostError = err;
        return paHostError;
    }

    // If device is delivering audio faster than being consumed then buffer must be bigger.
    sampleRateRatio = formatDesc.mSampleRate / past->past_SampleRate;
    
    // Get size of CoreAudio IO buffers.
    dataSize = sizeof(framesPerHostBuffer);
    err = AudioDeviceGetProperty( pahsc->input.audioDeviceID, 0, IS_INPUT,
                                kAudioDevicePropertyBufferFrameSize, &dataSize,
                                &framesPerHostBuffer);
    if( err != noErr )
    {
        PRINT_ERR("PaOSX_CreateInputRingBuffer: Could not get input buffer size.\n", err);
        sSavedHostError = err;
        return paHostError;
    }
    
    bytesPerFrame = past->past_NumInputChannels * sizeof(Float32);
    
    bytesForDevice = framesPerHostBuffer * bytesPerFrame * 2;
    bytesForUser = past->past_FramesPerUserBuffer * bytesPerFrame * 3 * sampleRateRatio;
        
    // Ring buffer should be large enough to consume audio input from device,
    // and to deliver a complete user buffer.
    numBytes = (bytesForDevice > bytesForUser) ? bytesForDevice : bytesForUser;
            
    numBytes = RoundUpToNextPowerOf2( numBytes );

    DBUG(("PaOSX_CreateInputRingBuffer: FIFO numBytes = %ld\n", numBytes));
    pahsc->ringBufferData = PaHost_AllocateFastMemory( numBytes );
    if( pahsc->ringBufferData == NULL )
    {
        return paInsufficientMemory;
    }
    RingBuffer_Init( &pahsc->ringBuffer, numBytes, pahsc->ringBufferData );
    // Make it look almost full at beginning. We must advance by an integral number of frames
    // so that the channels don't get scrambled when numChannels is not a power of 2.
    {
        int numZeroFrames = numBytes / bytesPerFrame;
        int numZeroBytes = numZeroFrames * bytesPerFrame;
        RingBuffer_AdvanceWriteIndex( &pahsc->ringBuffer, numZeroBytes );
    }
    
    return paNoError;
}

/******************************************************************
 * Try to set the I/O bufferSize of the device.
 * Scale the size by the ratio of the sample rates so that the converter will have
 * enough data to operate on.
 */
static OSStatus PaOSX_SetDeviceBufferSize( AudioDeviceID devID, Boolean isInput, int framesPerUserBuffer, Float64 sampleRateRatio )
{
    UInt32    dataSize;
    UInt32    ioBufferSize;
    int       scaler;    
    
	scaler = (int) sampleRateRatio;
    if( sampleRateRatio > (Float64) scaler ) scaler += 1;
    DBUG(("PaOSX_SetDeviceBufferSize: buffer size scaler = %d\n", scaler ));
    ioBufferSize = framesPerUserBuffer * scaler;
    
    // Limit buffer size to reasonable value.
    if( ioBufferSize < 128 ) ioBufferSize = 128;

    dataSize = sizeof(ioBufferSize);
    return AudioDeviceSetProperty( devID, 0, 0, isInput,
                            kAudioDevicePropertyBufferFrameSize, dataSize,
                            &ioBufferSize);
}


/*******************************************************************/
static PaError PaOSX_OpenCommonDevice( internalPortAudioStream   *past,
        PaHostInOut *inOut, Boolean isInput )
{
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    PaError          result = paNoError;
    OSStatus         err = noErr;
    Float64          deviceRate;


    // The HW device format changes are asynchronous.
    // So we don't know when or if the PAOSX_DevicePropertyListener() will
    // get called. To be safe, call the listener now to forcibly create the converter.
    if( inOut->converter == NULL )
    {
        err = PAOSX_DevicePropertyListener (inOut->audioDeviceID,
                                0, isInput, kAudioDevicePropertyStreamFormat, past);
        if (err != kAudioHardwareNoError)
        {
            PRINT_ERR("PaOSX_OpenCommonDevice: PAOSX_DevicePropertyListener failed.\n", err);
            sSavedHostError = err;
            return paHostError;
        }
    }

    // Add listener for when format changed by other apps.
    DBUG(("PaOSX_OpenCommonDevice: call AudioDeviceAddPropertyListener()\n" ));
	err = AudioDeviceAddPropertyListener( inOut->audioDeviceID, 0, isInput,
        kAudioDevicePropertyStreamFormat,
        (AudioDevicePropertyListenerProc) PAOSX_DevicePropertyListener, past );
    if (err != noErr)
    {
        return -1; // FIXME
    }

    // Only change format if current HW format is different.
    // Don't bother to check result because we are going to use an AudioConverter anyway.
    pahsc->formatListenerCalled = false;
    result = PaOSX_SetFormat( inOut->audioDeviceID, isInput, past->past_SampleRate, inOut->numChannels );
    // Synchronize with device because format changes put some devices into unusable mode.
    if( result > 0 )
    {
        const int sleepDurMsec = 50;
        int spinCount = MIN_TIMEOUT_MSEC / sleepDurMsec;
        while( !pahsc->formatListenerCalled && (spinCount > 0) )
        {
            Pa_Sleep( sleepDurMsec ); // FIXME - use a semaphore or signal
            spinCount--;
        }
        if( !pahsc->formatListenerCalled )
        {
            PRINT(("PaOSX_OpenCommonDevice: timed out waiting for device format to settle.\n"));
        }
        result = 0;
    }
    
#if SET_DEVICE_BUFFER_SIZE
    // Try to set the I/O bufferSize of the device.
    {
        Float64   ratio;
        deviceRate = PaOSX_GetDeviceSampleRate( inOut->audioDeviceID, isInput );
        if( deviceRate <= 0.0 ) deviceRate =  past->past_SampleRate;
        ratio = deviceRate / past->past_SampleRate ;
        err = PaOSX_SetDeviceBufferSize( inOut->audioDeviceID, isInput,
            past->past_FramesPerUserBuffer, ratio );
        if( err != noErr )
        {
            DBUG(("PaOSX_OpenCommonDevice: Could not set I/O buffer size.\n"));
        }
    }
#endif

    /* Allocate an input buffer because we need it between the user callback and the converter. */
    inOut->converterBuffer = PaHost_AllocateFastMemory( inOut->bytesPerUserNativeBuffer );
    if( inOut->converterBuffer == NULL )
    {
        return paInsufficientMemory;
    }

    return result;
}

/*******************************************************************/
static PaError PaOSX_OpenInputDevice( internalPortAudioStream   *past )
{
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    const PaHostDeviceInfo *hostDeviceInfo;
    PaError          result = paNoError;
        
    DBUG(("PaOSX_OpenInputDevice: -------------\n"));
    
    if( (past->past_InputDeviceID < LOWEST_INPUT_DEVID) ||
        (past->past_InputDeviceID > HIGHEST_INPUT_DEVID) )
    {
        return paInvalidDeviceId;
    }
    hostDeviceInfo = &sDeviceInfos[past->past_InputDeviceID];

    if( past->past_NumInputChannels > hostDeviceInfo->paInfo.maxInputChannels )
    {
        return paInvalidChannelCount; /* Too many channels! */
    }
    pahsc->input.numChannels = past->past_NumInputChannels;

    // setup PA conversion procedure
    result = PaConvert_SetupInput( past, paFloat32 );

    result = PaOSX_OpenCommonDevice( past, &pahsc->input, IS_INPUT );
    if( result != paNoError ) goto error;
    
    // Allocate a ring buffer so we can push in data from device, and pull through AudioConverter.
    result = PaOSX_CreateInputRingBuffer( past );
    if( result != paNoError ) goto error;
            
error:
    return result;
}

/*******************************************************************/
static PaError PaOSX_OpenOutputDevice( internalPortAudioStream *past )
{
    PaHostSoundControl *pahsc;
    const PaHostDeviceInfo *hostDeviceInfo;
    PaError          result = paNoError;

    DBUG(("PaOSX_OpenOutputDevice: -------------\n"));
    pahsc = (PaHostSoundControl *) past->past_DeviceData;
    
    // Validate DeviceID
    DBUG(("PaOSX_OpenOutputDevice: deviceID = 0x%x\n", past->past_OutputDeviceID));
    if( (past->past_OutputDeviceID < LOWEST_OUTPUT_DEVID) ||
        (past->past_OutputDeviceID > HIGHEST_OUTPUT_DEVID) )
    {
        return paInvalidDeviceId;
    }
    hostDeviceInfo = &sDeviceInfos[past->past_OutputDeviceID];
    
    // Validate number of channels.
    if( past->past_NumOutputChannels > hostDeviceInfo->paInfo.maxOutputChannels )
    {
        return paInvalidChannelCount; /* Too many channels! */
    }
    pahsc->output.numChannels = past->past_NumOutputChannels;
    
    // setup conversion procedure
    result = PaConvert_SetupOutput( past, paFloat32 );
	if( result != paNoError ) goto error;
        
    result = PaOSX_OpenCommonDevice( past, &pahsc->output, IS_OUTPUT );
    if( result != paNoError ) goto error;
    
error:
    return result;
}

/*******************************************************************
* Determine how many User Buffers we can put into our CoreAudio stream buffer.
* Uses:
*    past->past_FramesPerUserBuffer, etc.
* Sets:
*    past->past_NumUserBuffers
*    pahsc->input.bytesPerUserNativeBuffer
*    pahsc->output.bytesPerUserNativeBuffer
*/
static void PaOSX_CalcHostBufferSize( internalPortAudioStream *past )
{
    PaHostSoundControl *pahsc = ( PaHostSoundControl *)past->past_DeviceData;

    // Determine number of user buffers based strictly on minimum reasonable buffer size.
    // We ignore the Pa_OpenStream numBuffer parameter because CoreAudio has a big
    // mix buffer and handles latency automatically.
    past->past_NumUserBuffers = Pa_GetMinNumBuffers( past->past_FramesPerUserBuffer, past->past_SampleRate );

    // calculate buffer sizes in bytes
    pahsc->input.bytesPerUserNativeBuffer = past->past_FramesPerUserBuffer *
        Pa_GetSampleSize(paFloat32) * past->past_NumInputChannels;
    pahsc->output.bytesPerUserNativeBuffer = past->past_FramesPerUserBuffer *
        Pa_GetSampleSize(paFloat32) * past->past_NumOutputChannels;

    DBUG(("PaOSX_CalcNumHostBuffers: past_NumUserBuffers = %ld\n", past->past_NumUserBuffers ));
    DBUG(("PaOSX_CalcNumHostBuffers: input.bytesPerUserNativeBuffer = %d\n", pahsc->input.bytesPerUserNativeBuffer ));
    DBUG(("PaOSX_CalcNumHostBuffers: output.bytesPerUserNativeBuffer = %d\n", pahsc->output.bytesPerUserNativeBuffer ));
}

/*****************************************************************************/
/************** Internal Host API ********************************************/
/*****************************************************************************/
PaError PaHost_OpenStream( internalPortAudioStream   *past )
{
    PaError             result = paNoError;
    PaHostSoundControl *pahsc;
    Boolean             useInput;  
    Boolean             useOutput;          

    assert( past->past_Magic == PA_MAGIC );
    
    /* Allocate and initialize host data. */
    pahsc = (PaHostSoundControl *) malloc(sizeof(PaHostSoundControl));
    if( pahsc == NULL )
    {
        result = paInsufficientMemory;
        goto error;
    }
    memset( pahsc, 0, sizeof(PaHostSoundControl) );
    past->past_DeviceData = (void *) pahsc;
    pahsc->primaryDeviceID = kAudioDeviceUnknown;
    pahsc->input.audioDeviceID = kAudioDeviceUnknown;
    pahsc->output.audioDeviceID = kAudioDeviceUnknown;
    
    PaOSX_CalcHostBufferSize( past );

    useOutput = (past->past_OutputDeviceID != paNoDevice) && (past->past_NumOutputChannels > 0);
    useInput = (past->past_InputDeviceID != paNoDevice) && (past->past_NumInputChannels > 0);
    
    // Set device IDs and determine IO Device mode.
    if( useOutput )
    {
        pahsc->output.audioDeviceID = sDeviceInfos[past->past_OutputDeviceID].audioDeviceID;
        pahsc->primaryDeviceID = pahsc->output.audioDeviceID;
        if( useInput )
        {
            pahsc->input.audioDeviceID = sDeviceInfos[past->past_InputDeviceID].audioDeviceID;
            pahsc->mode = ( pahsc->input.audioDeviceID != pahsc->primaryDeviceID ) ?
                PA_MODE_IO_TWO_DEVICES : PA_MODE_IO_ONE_DEVICE;
        }
        else
        {
            pahsc->mode = PA_MODE_OUTPUT_ONLY;
        }
    }
    else
    {
        /* Just input, not output. */
        pahsc->input.audioDeviceID = sDeviceInfos[past->past_InputDeviceID].audioDeviceID;
        pahsc->primaryDeviceID = pahsc->input.audioDeviceID;
        pahsc->mode = PA_MODE_INPUT_ONLY;
    }
    
	DBUG(("outputDeviceID = %ld\n", pahsc->output.audioDeviceID ));
	DBUG(("inputDeviceID = %ld\n", pahsc->input.audioDeviceID ));
	DBUG(("primaryDeviceID = %ld\n", pahsc->primaryDeviceID ));
        
    /* ------------------ OUTPUT */
    if( useOutput )
    {
        result = PaOSX_OpenOutputDevice( past );
        if( result < 0 ) goto error;
    }

    /* ------------------ INPUT */
    if( useInput )
    {
        result = PaOSX_OpenInputDevice( past );
        if( result < 0 ) goto error;
    }
    
#if PA_ENABLE_LOAD_MEASUREMENT    
    pahsc->inverseHostTicksPerBuffer = past->past_SampleRate /
        (AudioGetHostClockFrequency() * past->past_FramesPerUserBuffer);
        DBUG(("inverseHostTicksPerBuffer based on buffer size of %d frames.\n", past->past_FramesPerUserBuffer ));
#else
    PRINT(("WARNING - Pa_GetCPULoad() mesaurement disabled in pa_mac_core.c.\n"));
#endif

    return result;

error:
    PaHost_CloseStream( past );
    return result;
}

/*************************************************************************/
PaError PaHost_StartOutput( internalPortAudioStream *past )
{
    return 0;
}

/*************************************************************************/
PaError PaHost_StartInput( internalPortAudioStream *past )
{
    return 0;
}

/*************************************************************************/
PaError PaHost_StartEngine( internalPortAudioStream *past )
{
    OSStatus            err = noErr;
    PaError             result = paNoError;
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;

    past->past_StopSoon = 0;
    past->past_StopNow = 0;
    past->past_IsActive = 1;
    
/* If full duplex and using two separate devices then start input device. */
    if( pahsc->mode == PA_MODE_IO_TWO_DEVICES )
    {
        // Associate an IO proc with the device and pass a pointer to the audio data context
        err = AudioDeviceAddIOProc(pahsc->input.audioDeviceID, (AudioDeviceIOProc)PaOSX_CoreAudioInputCallback, past);
        if (err != noErr)
        {            
            PRINT_ERR("PaHost_StartEngine: AudioDeviceAddIOProc secondary failed", err );
            goto error;
        }
        
        // start playing sound through the device
        err = AudioDeviceStart(pahsc->input.audioDeviceID, (AudioDeviceIOProc)PaOSX_CoreAudioInputCallback);
        if (err != noErr)
        {            
            PRINT_ERR("PaHost_StartEngine: AudioDeviceStart secondary failed", err );
            PRINT(("The program may succeed if you run it again!\n"));
            goto error;
        }
    }
        
    // Associate an IO proc with the device and pass a pointer to the audio data context
    err = AudioDeviceAddIOProc(pahsc->primaryDeviceID, (AudioDeviceIOProc)PaOSX_CoreAudioIOCallback, past);
    if (err != noErr)
    {            
        PRINT_ERR("PaHost_StartEngine: AudioDeviceAddIOProc primary failed", err );
        goto error;
    }

    // start playing sound through the device
    err = AudioDeviceStart(pahsc->primaryDeviceID, (AudioDeviceIOProc)PaOSX_CoreAudioIOCallback);
    if (err != noErr)
    {            
        PRINT_ERR("PaHost_StartEngine: AudioDeviceStart primary failed", err );
        PRINT(("The program may succeed if you run it again!\n"));
        goto error;
    }
    
    return result;

error:
    sSavedHostError = err;
    return paHostError;
}

/*************************************************************************/
PaError PaHost_StopEngine( internalPortAudioStream *past, int abort )
{
    OSStatus  err = noErr;
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    if( pahsc == NULL ) return paNoError;
    (void) abort;

    /* Tell background thread to stop generating more data and to let current data play out. */
    past->past_StopSoon = 1;
    /* If aborting, tell background thread to stop NOW! */
    if( abort ) past->past_StopNow = 1;
    past->past_IsActive = 0;

#if PA_TRACE_START_STOP
    AddTraceMessage( "PaHost_StopOutput: pahsc_HWaveOut ", (int) pahsc->pahsc_HWaveOut );
#endif

    // FIXME - we should ask proc to stop instead of stopping abruptly
    err = AudioDeviceStop(pahsc->primaryDeviceID, (AudioDeviceIOProc)PaOSX_CoreAudioIOCallback);
    if (err != noErr)
    {
        goto error;
    }

    err = AudioDeviceRemoveIOProc(pahsc->primaryDeviceID, (AudioDeviceIOProc)PaOSX_CoreAudioIOCallback);
    if (err != noErr) goto error;
    
/* If full duplex and using two separate devices then stop second input device. */
    if( pahsc->mode == PA_MODE_IO_TWO_DEVICES )
    {
        err = AudioDeviceStop(pahsc->input.audioDeviceID, (AudioDeviceIOProc)PaOSX_CoreAudioInputCallback);
        if (err != noErr) goto error;
        err = AudioDeviceRemoveIOProc(pahsc->input.audioDeviceID, (AudioDeviceIOProc)PaOSX_CoreAudioInputCallback);
        if (err != noErr) goto error;
    }

    return paNoError;

error:
    sSavedHostError = err;
    return paHostError;
}

/*************************************************************************/
PaError PaHost_StopInput( internalPortAudioStream *past, int abort )
{
    return paNoError;
}

/*************************************************************************/
PaError PaHost_StopOutput( internalPortAudioStream *past, int abort )
{
    return paNoError;
}

/*******************************************************************/
PaError PaHost_CloseStream( internalPortAudioStream   *past )
{
    PaHostSoundControl *pahsc;

    if( past == NULL ) return paBadStreamPtr;
    pahsc = (PaHostSoundControl *) past->past_DeviceData;
    if( pahsc == NULL ) return paNoError;
        
    //PaOSX_DumpDeviceInfo( sDeviceInfos[past->past_OutputDeviceID].audioDeviceID, IS_OUTPUT );

#if PA_TRACE_START_STOP
    AddTraceMessage( "PaHost_CloseStream: pahsc_HWaveOut ", (int) pahsc->pahsc_HWaveOut );
#endif
    // Stop Listener callbacks ASAP before dismantling stream.
    if( PA_USING_INPUT )
    {
        AudioDeviceRemovePropertyListener( pahsc->input.audioDeviceID, 0, IS_INPUT,
            kAudioDevicePropertyStreamFormat,
            (AudioDevicePropertyListenerProc) PAOSX_DevicePropertyListener );
    }

    if( PA_USING_OUTPUT )
    {
        AudioDeviceRemovePropertyListener( pahsc->output.audioDeviceID, 0, IS_OUTPUT,
            kAudioDevicePropertyStreamFormat,
            (AudioDevicePropertyListenerProc) PAOSX_DevicePropertyListener );
    }

    if( pahsc->output.converterBuffer != NULL )
    {
        PaHost_FreeFastMemory( pahsc->output.converterBuffer, pahsc->output.bytesPerUserNativeBuffer );
    }
    if( pahsc->input.converterBuffer != NULL )
    {
        PaHost_FreeFastMemory( pahsc->input.converterBuffer, pahsc->input.bytesPerUserNativeBuffer );
    }
    if( pahsc->ringBufferData != NULL )
    {
        PaHost_FreeFastMemory( pahsc->ringBufferData, pahsc->ringBuffer.bufferSize );
    }
    if( pahsc->output.converter != NULL )
    {
        verify_noerr(AudioConverterDispose (pahsc->output.converter));
    }
    if( pahsc->input.converter != NULL )
    {
        verify_noerr(AudioConverterDispose (pahsc->input.converter));
    }
	if ( pahsc->input.streamInterleavingBuffer != NULL )
    {
        PaHost_FreeFastMemory( pahsc->input.streamInterleavingBuffer, pahsc->input.streamInterleavingBufferLen );
    }
	if ( pahsc->output.streamInterleavingBuffer != NULL )
    {
        PaHost_FreeFastMemory( pahsc->output.streamInterleavingBuffer, pahsc->output.streamInterleavingBufferLen );
    }

    free( pahsc );
    past->past_DeviceData = NULL;

    return paNoError;
}

/**********************************************************************
** Initialize Host dependant part of API.
*/
PaError PaHost_Init( void )
{
    return PaOSX_MaybeQueryDevices();
}

/*************************************************************************
** Cleanup device info.
*/
PaError PaHost_Term( void )
{
    int i;

    if( sDeviceInfos != NULL )
    {
        for( i=0; i<sNumPaDevices; i++ )
        {
            if( sDeviceInfos[i].paInfo.name != NULL )
            {
                free( (char*)sDeviceInfos[i].paInfo.name );
            }
        }
        free( sDeviceInfos );
        sDeviceInfos = NULL;
    }

    sNumPaDevices = 0;
    return paNoError;
}

/*************************************************************************
 * Allocate memory that can be accessed in real-time.
 * This may need to be held in physical memory so that it is not
 * paged to virtual memory.
 * This call MUST be balanced with a call to PaHost_FreeFastMemory().
 */
void *PaHost_AllocateFastMemory( long numBytes )
{
    void *addr = malloc( numBytes ); /* FIXME - do we need physical memory, not virtual memory? */
    if( addr != NULL ) memset( addr, 0, numBytes );
    return addr;
}

/*************************************************************************
 * Free memory that could be accessed in real-time.
 * This call MUST be balanced with a call to PaHost_AllocateFastMemory().
 */
void PaHost_FreeFastMemory( void *addr, long numBytes )
{
    if( addr != NULL ) free( addr );
}


/***********************************************************************/
PaError PaHost_StreamActive( internalPortAudioStream   *past )
{
    PaHostSoundControl *pahsc;
    if( past == NULL ) return paBadStreamPtr;
    pahsc = (PaHostSoundControl *) past->past_DeviceData;
    if( pahsc == NULL ) return paInternalError;
    return (PaError) past->past_IsActive;
}

/*****************************************************************************/
/************** External User API ********************************************/
/*****************************************************************************/

/**********************************************************************
** Query devices and use result.
*/
PaDeviceID Pa_GetDefaultInputDeviceID( void )
{
    PaError result = PaOSX_MaybeQueryDevices();
	if( result < 0 ) return result;
	return sDefaultInputDeviceID;
}

PaDeviceID Pa_GetDefaultOutputDeviceID( void )
{
    PaError result = PaOSX_MaybeQueryDevices();
	if( result < 0 ) return result;
	return sDefaultOutputDeviceID;
}


/*************************************************************************
** Determine minimum number of buffers required for this host based
** on minimum latency. Because CoreAudio manages latency, this just selects
** a reasonably small number of buffers.
*/
int Pa_GetMinNumBuffers( int framesPerBuffer, double framesPerSecond )
{
    int minBuffers;
    double denominator;
    int minLatencyMsec = PA_MIN_LATENCY_MSEC;
    denominator =  1000.0 * framesPerBuffer;
    minBuffers = (int) (((minLatencyMsec * framesPerSecond) + denominator - 1) / denominator );
    if( minBuffers < 1 ) minBuffers = 1;
    return minBuffers;
}

/*************************************************************************/
void Pa_Sleep( long msec )
{
    usleep( msec * 1000 );
}

/*************************************************************************/
PaTimestamp Pa_StreamTime( PortAudioStream *stream )
{
    AudioTimeStamp timeStamp;
    PaTimestamp streamTime;
    PaHostSoundControl *pahsc;
    internalPortAudioStream   *past = (internalPortAudioStream *) stream;
    if( past == NULL ) return paBadStreamPtr;
    pahsc = (PaHostSoundControl *) past->past_DeviceData;
  
    AudioDeviceGetCurrentTime(pahsc->primaryDeviceID, &timeStamp);
  
    streamTime = ( timeStamp.mFlags & kAudioTimeStampSampleTimeValid) ?
            timeStamp.mSampleTime : past->past_FrameCount;

    return streamTime;
}

/************************************************************************************/
long Pa_GetHostError()
{
    return sSavedHostError;
}

/*************************************************************************/
int Pa_CountDevices()
{
    if( sNumPaDevices <= 0 ) Pa_Initialize();
    return sNumPaDevices;
}

/*************************************************************************
** PaDeviceInfo structures have already been created
** so just return the pointer.
**
*/
const PaDeviceInfo* Pa_GetDeviceInfo( PaDeviceID id )
{
    if( id < 0 || id >= sNumPaDevices )
        return NULL;

    return &sDeviceInfos[id].paInfo;
}