aboutsummaryrefslogtreecommitdiff
path: root/pd/portaudio_v18/pa_mac/pa_mac.c
blob: 118ea5101f7e5acb6bc4c4f3a5cadef0509a907a (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
/*
 * $Id: pa_mac.c,v 1.4.4.2 2002/10/15 03:14:08 dmazzoni Exp $
 * Portable Audio I/O Library for Macintosh
 *
 * Based on the Open Source API proposed by Ross Bencina
 * Copyright (c) 1999-2000 Phil Burk
 *
 * Special thanks to Chris Rolfe for his many helpful suggestions, bug fixes,
 * and code contributions.
 * Thanks also to Tue Haste Andersen, Alberto Ricci, Nico Wald,
 * Roelf Toxopeus and Tom Erbe for testing the code and making
 * numerous suggestions.
 *
 * 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.
 */
/* Modification History
   PLB20010415 - ScanInputDevices was setting sDefaultOutputDeviceID instead of sDefaultInputDeviceID
   PLB20010415 - Device Scan was crashing for anything other than siBadSoundInDevice, but some Macs may return other errors!
   PLB20010420 - Fix TIMEOUT in record mode.
   PLB20010420 - Change CARBON_COMPATIBLE to TARGET_API_MAC_CARBON
   PLB20010907 - Pass unused event to WaitNextEvent to prevent Mac OSX crash. Thanks Dominic Mazzoni.
   PLB20010908 - Use requested number of input channels. Thanks Dominic Mazzoni.
   PLB20011009 - Use NewSndCallBackUPP() for CARBON
   PLB20020417 - I used to call Pa_GetMinNumBuffers() which doesn't take into account the
                 variable minFramesPerHostBuffer. Now I call PaMac_GetMinNumBuffers() which will
                 give lower latency when virtual memory is turned off.
                 Thanks Kristoffer Jensen and Georgios Marentakis for spotting this bug.
   PLB20020423 - Use new method to calculate CPU load similar to other ports. Based on num frames calculated.
                 Fixed Pa_StreamTime(). Now estimates how many frames have played based on MicroSecond timer.
                 Added PA_MAX_USAGE_ALLOWED to prevent Mac from hanging when CPU load approaches 100%.
   PLB20020424 - Fixed return value in Pa_StreamTime
   PLB20020612 - Fix allocation error on Mac 8600 by casting *nameH as uchar* so that we get a proper Str255 length.
*/

/*
COMPATIBILITY
This Macintosh implementation is designed for use with Mac OS 7, 8 and
9 on PowerMacs, and OS X if compiled with CARBON
 
OUTPUT
A circular array of CmpSoundHeaders is used as a queue. For low latency situations
there will only be two small buffers used. For higher latency, more and larger buffers
may be used.
To play the sound we use SndDoCommand() with bufferCmd. Each buffer is followed
by a callbackCmd which informs us when the buffer has been processsed.
 
INPUT
The SndInput Manager SPBRecord call is used for sound input. If only
input is used, then the PA user callback is called from the Input completion proc.
For full-duplex, or output only operation, the PA callback is called from the
HostBuffer output completion proc. In that case, input sound is passed to the
callback by a simple FIFO.
 
TODO:
O- Add support for native sample data formats other than int16.
O- Review buffer sizing. Should it be based on result of siDeviceBufferInfo query?
O- Determine default devices somehow.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>

/* Mac specific includes */
#include "OSUtils.h"
#include <MacTypes.h>
#include <Math64.h>
#include <Errors.h>
#include <Sound.h>
#include <SoundInput.h>
#include <SoundComponents.h>
#include <Devices.h>
#include <DateTimeUtils.h>
#include <Timer.h>
#include <Gestalt.h>

#include "portaudio.h"
#include "pa_host.h"
#include "pa_trace.h"

#ifndef FALSE
 #define FALSE  (0)
 #define TRUE   (!FALSE)
#endif

/* #define TARGET_API_MAC_CARBON (1) */

/*
 * Define maximum CPU load that will be allowed. User callback will
 * be skipped if load exceeds this limit. This is to prevent the Mac
 * from hanging when the CPU is hogged by the sound thread.
 * On my PowerBook G3, the mac hung when I used 94% of CPU ( usage = 0.94 ).
 */
#define PA_MAX_USAGE_ALLOWED    (0.92)

/* Debugging output macros. */
#define PRINT(x) { printf x; fflush(stdout); }
#define ERR_RPT(x) PRINT(x)
#define DBUG(x)   /* PRINT(x) */
#define DBUGX(x)  /* PRINT(x) */

#define MAC_PHYSICAL_FRAMES_PER_BUFFER   (512)  /* Minimum number of stereo frames per SoundManager double buffer. */
#define MAC_VIRTUAL_FRAMES_PER_BUFFER   (4096) /* Need this many when using Virtual Memory for recording. */
#define PA_MIN_NUM_HOST_BUFFERS            (2)
#define PA_MAX_NUM_HOST_BUFFERS           (16)   /* Do not exceed!! */
#define PA_MAX_DEVICE_INFO                (32)

/* Conversions for 16.16 fixed point code. */
#define DoubleToUnsignedFixed(x) ((UnsignedFixed) ((x) * 65536.0))
#define UnsignedFixedToDouble(fx) (((double)(fx)) * (1.0/(1<<16)))

/************************************************************************************/
/****************** Structures ******************************************************/
/************************************************************************************/
/* Use for passing buffers from input callback to output callback for processing. */
typedef struct MultiBuffer
{
    char    *buffers[PA_MAX_NUM_HOST_BUFFERS];
    int      numBuffers;
    int      nextWrite;
    int      nextRead;
}
MultiBuffer;

/* Define structure to contain all Macintosh specific data. */
typedef struct PaHostSoundControl
{
    UInt64                  pahsc_EntryCount;
	double                  pahsc_InverseMicrosPerHostBuffer; /* 1/Microseconds of real-time audio per user buffer. */

    /* Use char instead of Boolean for atomic operation. */
    volatile char           pahsc_IsRecording;   /* Recording in progress. Set by foreground. Cleared by background. */
    volatile char           pahsc_StopRecording; /* Signal sent to background. */
    volatile char           pahsc_IfInsideCallback;
    /* Input */
    SPB                     pahsc_InputParams;
    SICompletionUPP         pahsc_InputCompletionProc;
    MultiBuffer             pahsc_InputMultiBuffer;
    int32                   pahsc_BytesPerInputHostBuffer;
    int32                   pahsc_InputRefNum;
    /* Output */
    CmpSoundHeader          pahsc_SoundHeaders[PA_MAX_NUM_HOST_BUFFERS];
    int32                   pahsc_BytesPerOutputHostBuffer;
    SndChannelPtr           pahsc_Channel;
    SndCallBackUPP          pahsc_OutputCompletionProc;
    int32                   pahsc_NumOutsQueued;
    int32                   pahsc_NumOutsPlayed;
    PaTimestamp             pahsc_NumFramesDone;
    UInt64                  pahsc_WhenFramesDoneIncremented;
    /* Init Time -------------- */
    int32                   pahsc_NumHostBuffers;
    int32                   pahsc_FramesPerHostBuffer;
    int32                   pahsc_UserBuffersPerHostBuffer;
    int32                   pahsc_MinFramesPerHostBuffer; /* Can vary depending on virtual memory usage. */
}
PaHostSoundControl;

/* Mac specific device information. */
typedef struct internalPortAudioDevice
{
    long                    pad_DeviceRefNum;
    long                    pad_DeviceBufferSize;
    Component               pad_Identifier;
    PaDeviceInfo            pad_Info;
}
internalPortAudioDevice;

/************************************************************************************/
/****************** Data ************************************************************/
/************************************************************************************/
static int                 sNumDevices = 0;
static internalPortAudioDevice sDevices[PA_MAX_DEVICE_INFO] = { 0 };
static int32               sPaHostError = 0;
static int                 sDefaultOutputDeviceID;
static int                 sDefaultInputDeviceID;

/************************************************************************************/
/****************** Prototypes ******************************************************/
/************************************************************************************/
static PaError PaMac_TimeSlice( internalPortAudioStream   *past,  int16 *macOutputBufPtr );
static PaError PaMac_CallUserLoop( internalPortAudioStream   *past, int16 *outPtr );
static PaError PaMac_RecordNext( internalPortAudioStream   *past );
static void    PaMac_StartLoadCalculation( internalPortAudioStream   *past );
static int     PaMac_GetMinNumBuffers( int minFramesPerHostBuffer, int framesPerBuffer, double sampleRate );
static double *PaMac_GetSampleRatesFromHandle ( int numRates, Handle h );
static PaError PaMac_ScanInputDevices( void );
static PaError PaMac_ScanOutputDevices( void );
static PaError PaMac_QueryOutputDeviceInfo( Component identifier, internalPortAudioDevice *ipad );
static PaError PaMac_QueryInputDeviceInfo( Str255 deviceName, internalPortAudioDevice *ipad );
static void    PaMac_InitSoundHeader( internalPortAudioStream   *past, CmpSoundHeader *sndHeader );
static void    PaMac_EndLoadCalculation( internalPortAudioStream   *past );
static void    PaMac_PlayNext ( internalPortAudioStream *past, int index );
static long    PaMac_FillNextOutputBuffer( internalPortAudioStream   *past, int index );
static pascal void PaMac_InputCompletionProc(SPBPtr recParams);
static pascal void PaMac_OutputCompletionProc (SndChannelPtr theChannel, SndCommand * theCmd);
static PaError PaMac_BackgroundManager( internalPortAudioStream   *past, int index );
long PaHost_GetTotalBufferFrames( internalPortAudioStream   *past );
static int     Mac_IsVirtualMemoryOn( void );
static void    PToCString(unsigned char* inString, char* outString);
static void    CToPString(char *inString, unsigned char* outString);
char *MultiBuffer_GetNextWriteBuffer( MultiBuffer *mbuf );
char *MultiBuffer_GetNextReadBuffer( MultiBuffer *mbuf );
int   MultiBuffer_GetNextReadIndex( MultiBuffer *mbuf );
int   MultiBuffer_GetNextWriteIndex( MultiBuffer *mbuf );
int   MultiBuffer_IsWriteable(  MultiBuffer *mbuf );
int   MultiBuffer_IsReadable(  MultiBuffer *mbuf );
void  MultiBuffer_AdvanceReadIndex(  MultiBuffer *mbuf );
void  MultiBuffer_AdvanceWriteIndex(  MultiBuffer *mbuf );

/*************************************************************************
** Simple FIFO index control for multiple buffers.
** Read and Write indices range from 0 to 2N-1.
** This allows us to distinguish between full and empty.
*/
char *MultiBuffer_GetNextWriteBuffer( MultiBuffer *mbuf )
{
    return mbuf->buffers[mbuf->nextWrite % mbuf->numBuffers];
}
char *MultiBuffer_GetNextReadBuffer( MultiBuffer *mbuf )
{
    return mbuf->buffers[mbuf->nextRead % mbuf->numBuffers];
}
int MultiBuffer_GetNextReadIndex( MultiBuffer *mbuf )
{
    return mbuf->nextRead % mbuf->numBuffers;
}
int MultiBuffer_GetNextWriteIndex( MultiBuffer *mbuf )
{
    return mbuf->nextWrite % mbuf->numBuffers;
}

int MultiBuffer_IsWriteable(  MultiBuffer *mbuf )
{
    int bufsFull = mbuf->nextWrite - mbuf->nextRead;
    if( bufsFull < 0 ) bufsFull += (2 * mbuf->numBuffers);
    return (bufsFull < mbuf->numBuffers);
}
int MultiBuffer_IsReadable(  MultiBuffer *mbuf )
{
    int bufsFull = mbuf->nextWrite - mbuf->nextRead;
    if( bufsFull < 0 ) bufsFull += (2 * mbuf->numBuffers);
    return (bufsFull > 0);
}
void MultiBuffer_AdvanceReadIndex(  MultiBuffer *mbuf )
{
    int temp = mbuf->nextRead + 1;
    mbuf->nextRead = (temp >= (2 * mbuf->numBuffers)) ? 0 : temp;
}
void MultiBuffer_AdvanceWriteIndex(  MultiBuffer *mbuf )
{
    int temp = mbuf->nextWrite + 1;
    mbuf->nextWrite = (temp >= (2 * mbuf->numBuffers)) ? 0 : temp;
}

/*************************************************************************
** String Utility by Chris Rolfe
*/
static void PToCString(unsigned char* inString, char* outString)
{
    long i;
    for(i=0; i<inString[0]; i++)  /* convert Pascal to C string */
        outString[i] = inString[i+1];
    outString[i]=0;
}

/*************************************************************************
** String Utility by Dominic Mazzoni
*/
static void CToPString(char* inString, unsigned char* outString)
{
    long len = strlen(inString);
    long i;

    if (len > 255)
        len = 255;

    /* Length is stored in first char of Pascal string */
    outString[0] = (unsigned char)len;
    for(i=0; i<len; i++)
        outString[i+1] = inString[i];
}

/*************************************************************************/
PaError PaHost_Term( void )
{
    int           i;
    PaDeviceInfo *dev;
    double       *rates;
    /* Free any allocated sample rate arrays. */
    for( i=0; i<sNumDevices; i++ )
    {
        dev =  &sDevices[i].pad_Info;
        rates = (double *) dev->sampleRates;
        if( (rates != NULL) ) free( rates ); /* MEM_011 */
        dev->sampleRates = NULL;
        if( dev->name != NULL ) free( (void *) dev->name ); /* MEM_010 */
        dev->name = NULL;
    }
    sNumDevices = 0;
    return paNoError;
}

/*************************************************************************
 PaHost_Init() is the library initialization function - call this before
    using the library.
*/
PaError PaHost_Init( void )
{
    PaError err;
    NumVersionVariant version;

    version.parts = SndSoundManagerVersion();
    DBUG(("SndSoundManagerVersion = 0x%x\n", version.whole));

    /* Have we already initialized the device info? */
    err = (PaError) Pa_CountDevices();
    if( err < 0 ) return err;
    else return paNoError;
}

/*************************************************************************
 PaMac_ScanOutputDevices() queries the properties of all output devices.
*/
static PaError PaMac_ScanOutputDevices( void )
{
    PaError       err;
    Component     identifier=0;
    ComponentDescription criteria = { kSoundOutputDeviceType, 0, 0, 0, 0 };
    long       numComponents, i;

    /* Search the system linked list for output components  */
    numComponents = CountComponents (&criteria);
    identifier = 0;
    sDefaultOutputDeviceID = sNumDevices; /* FIXME - query somehow */
    for (i = 0; i < numComponents; i++)
    {
        /* passing nil returns first matching component. */
        identifier = FindNextComponent( identifier, &criteria);
        sDevices[sNumDevices].pad_Identifier = identifier;

        /* Set up for default OUTPUT devices. */
        err = PaMac_QueryOutputDeviceInfo( identifier, &sDevices[sNumDevices] );
        if( err < 0 ) return err;
        else  sNumDevices++;

    }

    return paNoError;
}

/*************************************************************************
 PaMac_ScanInputDevices() queries the properties of all input devices.
*/
static PaError PaMac_ScanInputDevices( void )
{
    Str255     deviceName;
    int        count;
    Handle     iconHandle;
    PaError    err;
    OSErr      oserr;
    count = 1;
    sDefaultInputDeviceID = sNumDevices; /* FIXME - query somehow */ /* PLB20010415 - was setting sDefaultOutputDeviceID */
    while(true)
    {
        /* Thanks Chris Rolfe and Alberto Ricci for this trick. */
        oserr = SPBGetIndexedDevice(count++, deviceName, &iconHandle);
        DBUG(("PaMac_ScanInputDevices: SPBGetIndexedDevice returned %d\n", oserr ));
#if 1
        /* PLB20010415 - was giving error for anything other than siBadSoundInDevice, but some Macs may return other errors! */
        if(oserr != noErr) break; /* Some type of error is expected when count > devices */
#else
        if(oserr == siBadSoundInDevice)
        {  /* it's expected when count > devices */
            oserr = noErr;
            break;
        }
        if(oserr != noErr)
        {
            ERR_RPT(("ERROR: SPBGetIndexedDevice(%d,,) returned %d\n", count-1, oserr ));
            sPaHostError = oserr;
            return paHostError;
        }
#endif
        DisposeHandle(iconHandle);   /* Don't need the icon */

        err = PaMac_QueryInputDeviceInfo( deviceName, &sDevices[sNumDevices] );
        DBUG(("PaMac_ScanInputDevices: PaMac_QueryInputDeviceInfo returned %d\n", err ));
        if( err < 0 ) return err;
        else if( err == 1 ) sNumDevices++;
    }

    return paNoError;
}

/* Sample rate info returned by using siSampleRateAvailable selector in SPBGetDeviceInfo() */
/* Thanks to Chris Rolfe for help with this query. */
#pragma options align=mac68k
typedef  struct
{
    int16                   numRates;
    UnsignedFixed           (**rates)[]; /* Handle created by SPBGetDeviceInfo */
}
SRateInfo;
#pragma options align=reset

/*************************************************************************
** PaMac_QueryOutputDeviceInfo()
** Query information about a named output device.
** Clears contents of ipad and writes info based on queries.
** Return one if OK,
**        zero if device cannot be used,
**        or negative error.
*/
static PaError PaMac_QueryOutputDeviceInfo( Component identifier, internalPortAudioDevice *ipad )
{
    int     len;
    OSErr   err;
    PaDeviceInfo *dev =  &ipad->pad_Info;
    SRateInfo srinfo = {0};
    int     numRates;
    ComponentDescription tempD;
    Handle nameH=nil, infoH=nil, iconH=nil;

    memset( ipad, 0, sizeof(internalPortAudioDevice) );

    dev->structVersion = 1;
    dev->maxInputChannels = 0;
    dev->maxOutputChannels = 2;
    dev->nativeSampleFormats = paInt16; /* FIXME - query to see if 24 or 32 bit data can be handled. */

    /* Get sample rates supported. */
    err = GetSoundOutputInfo(identifier, siSampleRateAvailable, (Ptr) &srinfo);
    if(err != noErr)
    {
        ERR_RPT(("Error in PaMac_QueryOutputDeviceInfo: GetSoundOutputInfo siSampleRateAvailable returned %d\n", err ));
        goto error;
    }
    numRates = srinfo.numRates;
    DBUG(("PaMac_QueryOutputDeviceInfo: srinfo.numRates = %d\n", srinfo.numRates ));
    if( numRates == 0 )
    {
        dev->numSampleRates = -1;
        numRates = 2;
    }
    else
    {
        dev->numSampleRates = numRates;
    }
    dev->sampleRates = PaMac_GetSampleRatesFromHandle( numRates, (Handle) srinfo.rates );
    if(dev->sampleRates == NULL)
    {
    	DBUG(("PaMac_QueryOutputDeviceInfo: PaMac_GetSampleRatesFromHandle alloc failed.\n"));
    	return paInsufficientMemory;
    }
    
    /* SPBGetDeviceInfo created the handle, but it's OUR job to release it. */
    DisposeHandle((Handle) srinfo.rates);

    /* Device name */
    /*  we pass an existing handle for the component name;
     we don't care about the info (type, subtype, etc.) or icon, so set them to nil */
	DBUG(("PaMac_QueryOutputDeviceInfo: get component name.\n"));
    infoH = nil;
    iconH = nil;
    nameH = NewHandle(0);
    if(nameH == nil) return paInsufficientMemory;
    err = GetComponentInfo(identifier, &tempD, nameH, infoH, iconH);
    if (err)
    {
        ERR_RPT(("Error in PaMac_QueryOutputDeviceInfo: GetComponentInfo returned %d\n", err ));
        goto error;
    }
    /* Cast as uchar* so that we get a proper pascal string length. */
	len = ((unsigned char *)(*nameH))[0] + 1;  /* PLB20020612 - fix allocation error on Mac 8600 */
	DBUG(("PaMac_QueryOutputDeviceInfo: new len = %d\n", len ));

    dev->name = (char *) malloc(len);  /* MEM_010 */
    if( dev->name == NULL )
    {
        DisposeHandle(nameH);
        return paInsufficientMemory;
    }
    else
    {
        PToCString((unsigned char *)(*nameH), (char *) dev->name);
        DisposeHandle(nameH);
    }

    DBUG(("PaMac_QueryOutputDeviceInfo: dev->name = %s\n", dev->name ));
    return paNoError;

error:
    sPaHostError = err;
    return paHostError;

}

/*************************************************************************
** PaMac_QueryInputDeviceInfo()
** Query information about a named input device.
** Clears contents of ipad and writes info based on queries.
** Return one if OK,
**        zero if device cannot be used,
**        or negative error.
*/
static PaError PaMac_QueryInputDeviceInfo( Str255 deviceName, internalPortAudioDevice *ipad )
{
    PaError result = paNoError;
    int     len;
    OSErr   err;
    long    mRefNum = 0;
    long    tempL;
    int16   tempS;
    Fixed   tempF;
    PaDeviceInfo *dev =  &ipad->pad_Info;
    SRateInfo srinfo = {0};
    int     numRates;

    memset( ipad, 0, sizeof(internalPortAudioDevice) );
    dev->maxOutputChannels = 0;

    /* Open device based on name. If device is in use, it may not be able to open in write mode. */
    err = SPBOpenDevice( deviceName, siWritePermission, &mRefNum);
    if (err)
    {
        /* If device is in use, it may not be able to open in write mode so try read mode. */
        err = SPBOpenDevice( deviceName, siReadPermission, &mRefNum);
        if (err)
        {
            ERR_RPT(("Error in PaMac_QueryInputDeviceInfo: SPBOpenDevice returned %d\n", err ));
            sPaHostError = err;
            return paHostError;
        }
    }

    /* Define macros for printing out device info. */
#define PrintDeviceInfo(selector,var) \
    err = SPBGetDeviceInfo(mRefNum, selector, (Ptr) &var); \
    if (err) { \
        DBUG(("query %s failed\n", #selector )); \
    }\
    else { \
        DBUG(("query %s = 0x%x\n", #selector, var )); \
    }

    PrintDeviceInfo( siContinuous, tempS );
    PrintDeviceInfo( siAsync, tempS );
    PrintDeviceInfo( siNumberChannels, tempS );
    PrintDeviceInfo( siSampleSize, tempS );
    PrintDeviceInfo( siSampleRate, tempF );
    PrintDeviceInfo( siChannelAvailable, tempS );
    PrintDeviceInfo( siActiveChannels, tempL );
    PrintDeviceInfo( siDeviceBufferInfo, tempL );

    err = SPBGetDeviceInfo(mRefNum, siActiveChannels, (Ptr) &tempL);
    if (err == 0) DBUG(("%s = 0x%x\n", "siActiveChannels", tempL ));
    /* Can we use this device? */
    err = SPBGetDeviceInfo(mRefNum, siAsync, (Ptr) &tempS);
    if (err)
    {
        ERR_RPT(("Error in PaMac_QueryInputDeviceInfo: SPBGetDeviceInfo siAsync returned %d\n", err ));
        goto error;
    }
    if( tempS == 0 ) goto useless; /* Does not support async recording so forget about it. */

    err = SPBGetDeviceInfo(mRefNum, siChannelAvailable, (Ptr) &tempS);
    if (err)
    {
        ERR_RPT(("Error in PaMac_QueryInputDeviceInfo: SPBGetDeviceInfo siChannelAvailable returned %d\n", err ));
        goto error;
    }
    dev->maxInputChannels = tempS;

    /* Get sample rates supported. */
    err = SPBGetDeviceInfo(mRefNum, siSampleRateAvailable, (Ptr) &srinfo);
    if (err)
    {
        ERR_RPT(("Error in PaMac_QueryInputDeviceInfo: SPBGetDeviceInfo siSampleRateAvailable returned %d\n", err ));
        goto error;
    }

    numRates = srinfo.numRates;
    DBUG(("numRates = 0x%x\n", numRates ));
    if( numRates == 0 )
    {
        dev->numSampleRates = -1;
        numRates = 2;
    }
    else
    {
        dev->numSampleRates = numRates;
    }
    dev->sampleRates = PaMac_GetSampleRatesFromHandle( numRates, (Handle) srinfo.rates );
    /* SPBGetDeviceInfo created the handle, but it's OUR job to release it. */
    DisposeHandle((Handle) srinfo.rates);

    /* Get size of device buffer. */
    err = SPBGetDeviceInfo(mRefNum, siDeviceBufferInfo, (Ptr) &tempL);
    if (err)
    {
        ERR_RPT(("Error in PaMac_QueryInputDeviceInfo: SPBGetDeviceInfo siDeviceBufferInfo returned %d\n", err ));
        goto error;
    }
    ipad->pad_DeviceBufferSize = tempL;
    DBUG(("siDeviceBufferInfo = %d\n", tempL ));

    /* Set format based on sample size. */
    err = SPBGetDeviceInfo(mRefNum, siSampleSize, (Ptr) &tempS);
    if (err)
    {
        ERR_RPT(("Error in PaMac_QueryInputDeviceInfo: SPBGetDeviceInfo siSampleSize returned %d\n", err ));
        goto error;
    }
    switch( tempS )
    {
    case 0x0020:
        dev->nativeSampleFormats = paInt32;  /* FIXME - warning, code probably won't support this! */
        break;
    case 0x0010:
    default: /* FIXME - What about other formats? */
        dev->nativeSampleFormats = paInt16;
        break;
    }
    DBUG(("nativeSampleFormats = %d\n", dev->nativeSampleFormats ));

    /* Device name */
    len = deviceName[0] + 1;  /* Get length of Pascal string */
    dev->name = (char *) malloc(len);  /* MEM_010 */
    if( dev->name == NULL )
    {
        result = paInsufficientMemory;
        goto cleanup;
    }
    PToCString(deviceName, (char *) dev->name);
    DBUG(("deviceName = %s\n", dev->name ));
    result = (PaError) 1;
    /* All done so close up device. */
cleanup:
    if( mRefNum )  SPBCloseDevice(mRefNum);
    return result;

error:
    if( mRefNum )  SPBCloseDevice(mRefNum);
    sPaHostError = err;
    return paHostError;

useless:
    if( mRefNum )  SPBCloseDevice(mRefNum);
    return (PaError) 0;
}

/*************************************************************************
** Allocate a double array and fill it with listed sample rates.
*/
static double * PaMac_GetSampleRatesFromHandle ( int numRates, Handle h )
{
    OSErr   err = noErr;
    SInt8   hState;
    int     i;
    UnsignedFixed *fixedRates;
    double *rates = (double *) malloc( numRates * sizeof(double) ); /* MEM_011 */
    if( rates == NULL ) return NULL;
    /* Save and restore handle state as suggested by TechNote at:
            http://developer.apple.com/technotes/tn/tn1122.html
    */
    hState = HGetState (h);
    if (!(err = MemError ()))
    {
        HLock (h);
        if (!(err = MemError ( )))
        {
            fixedRates = (UInt32 *) *h;
            for( i=0; i<numRates; i++ )
            {
                rates[i] = UnsignedFixedToDouble(fixedRates[i]);
            }

            HSetState (h,hState);
            err = MemError ( );
        }
    }
    if( err )
    {
        free( rates );
        ERR_RPT(("Error in PaMac_GetSampleRatesFromHandle = %d\n", err ));
    }
    return rates;
}

/*************************************************************************/
int Pa_CountDevices()
{
    PaError err;
    DBUG(("Pa_CountDevices()\n"));
    /* If no devices, go find some. */
    if( sNumDevices <= 0 )
    {
        err = PaMac_ScanOutputDevices();
        if( err != paNoError ) goto error;
        err = PaMac_ScanInputDevices();
        if( err != paNoError ) goto error;
    }
    return sNumDevices;

error:
    PaHost_Term();
    DBUG(("Pa_CountDevices: returns %d\n", err ));
    return err;

}

/*************************************************************************/
const PaDeviceInfo* Pa_GetDeviceInfo( PaDeviceID id )
{
    if( (id < 0) || ( id >= Pa_CountDevices()) ) return NULL;
    return &sDevices[id].pad_Info;
}
/*************************************************************************/
PaDeviceID Pa_GetDefaultInputDeviceID( void )
{
    return sDefaultInputDeviceID;
}

/*************************************************************************/
PaDeviceID Pa_GetDefaultOutputDeviceID( void )
{
    return sDefaultOutputDeviceID;
}

/********************************* BEGIN CPU UTILIZATION MEASUREMENT ****/
static void PaMac_StartLoadCalculation( internalPortAudioStream   *past )
{
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    UnsignedWide widePad;
    if( pahsc == NULL ) return;
    /* Query system timer for usage analysis and to prevent overuse of CPU. */
    Microseconds( &widePad );
    pahsc->pahsc_EntryCount = UnsignedWideToUInt64( widePad );
}

/******************************************************************************
** Measure fractional CPU load based on real-time it took to calculate
** buffers worth of output.
*/
/**************************************************************************/
static void PaMac_EndLoadCalculation( internalPortAudioStream   *past )
{
    UnsignedWide widePad;
    UInt64    currentCount;
    long      usecsElapsed;
    double    newUsage;
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    if( pahsc == NULL ) return;
    
    /* Measure CPU utilization during this callback. Note that this calculation
    ** assumes that we had the processor the whole time.
    */
#define LOWPASS_COEFFICIENT_0   (0.95)
#define LOWPASS_COEFFICIENT_1   (0.99999 - LOWPASS_COEFFICIENT_0)
    Microseconds( &widePad );
    currentCount = UnsignedWideToUInt64( widePad );

	usecsElapsed = (long) U64Subtract(currentCount, pahsc->pahsc_EntryCount);
	
        /* Use inverse because it is faster than the divide. */
	newUsage =  usecsElapsed * pahsc->pahsc_InverseMicrosPerHostBuffer;
	
	past->past_Usage = (LOWPASS_COEFFICIENT_0 * past->past_Usage) +
                           (LOWPASS_COEFFICIENT_1 * newUsage);
 
}

/***********************************************************************
** Called by Pa_StartStream()
*/
PaError PaHost_StartInput( internalPortAudioStream   *past )
{
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    pahsc->pahsc_IsRecording = 0;
    pahsc->pahsc_StopRecording = 0;
    pahsc->pahsc_InputMultiBuffer.nextWrite = 0;
    pahsc->pahsc_InputMultiBuffer.nextRead = 0;
    return PaMac_RecordNext( past );
}

/***********************************************************************
** Called by Pa_StopStream().
** May be called during error recovery or cleanup code
** so protect against NULL pointers.
*/
PaError PaHost_StopInput( internalPortAudioStream   *past, int abort )
{
    int32   timeOutMsec;
    PaError result = paNoError;
    OSErr   err = 0;
    long    mRefNum;
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    if( pahsc == NULL ) return paNoError;

    (void) abort;

    mRefNum = pahsc->pahsc_InputRefNum;

    DBUG(("PaHost_StopInput: mRefNum = %d\n", mRefNum ));
    if( mRefNum )
    {
        DBUG(("PaHost_StopInput: pahsc_IsRecording = %d\n", pahsc->pahsc_IsRecording ));
        if( pahsc->pahsc_IsRecording )
        {
            /* PLB20010420 - Fix TIMEOUT in record mode. */
            pahsc->pahsc_StopRecording = 1; /* Request that we stop recording. */
            err = SPBStopRecording(mRefNum);
            DBUG(("PaHost_StopInput: then pahsc_IsRecording = %d\n", pahsc->pahsc_IsRecording ));

            /* Calculate timeOut longer than longest time it could take to play one buffer. */
            timeOutMsec = (int32) ((1500.0 * pahsc->pahsc_FramesPerHostBuffer) / past->past_SampleRate);
            /* Keep querying sound channel until it is no longer busy playing. */
            while( !err && pahsc->pahsc_IsRecording && (timeOutMsec > 0))
            {
                Pa_Sleep(20);
                timeOutMsec -= 20;
            }
            if( timeOutMsec <= 0 )
            {
                ERR_RPT(("PaHost_StopInput: timed out!\n"));
                return paTimedOut;
            }
        }
    }
    if( err )
    {
        sPaHostError = err;
        result = paHostError;
    }

    DBUG(("PaHost_StopInput: finished.\n", mRefNum ));
    return result;
}

/***********************************************************************/
static void PaMac_InitSoundHeader( internalPortAudioStream   *past, CmpSoundHeader *sndHeader )
{
    sndHeader->numChannels = past->past_NumOutputChannels;
    sndHeader->sampleRate = DoubleToUnsignedFixed(past->past_SampleRate);
    sndHeader->loopStart = 0;
    sndHeader->loopEnd = 0;
    sndHeader->encode = cmpSH;
    sndHeader->baseFrequency = kMiddleC;
    sndHeader->markerChunk = nil;
    sndHeader->futureUse2 = nil;
    sndHeader->stateVars = nil;
    sndHeader->leftOverSamples = nil;
    sndHeader->compressionID = 0;
    sndHeader->packetSize = 0;
    sndHeader->snthID = 0;
    sndHeader->sampleSize = 8 * sizeof(int16); // FIXME - might be 24 or 32 bits some day;
    sndHeader->sampleArea[0] = 0;
    sndHeader->format = kSoundNotCompressed;
}

static void SetFramesDone( PaHostSoundControl *pahsc, PaTimestamp framesDone )
{
	UnsignedWide     now;
 	Microseconds( &now );
 	pahsc->pahsc_NumFramesDone = framesDone;
 	pahsc->pahsc_WhenFramesDoneIncremented = UnsignedWideToUInt64( now );
}

/***********************************************************************/
PaError PaHost_StartOutput( internalPortAudioStream   *past )
{
    SndCommand  pauseCommand;
    SndCommand  resumeCommand;
    int          i;
    OSErr         error;
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    if( pahsc == NULL ) return paInternalError;
    if( pahsc->pahsc_Channel == NULL ) return paInternalError;

    past->past_StopSoon = 0;
    past->past_IsActive = 1;
    pahsc->pahsc_NumOutsQueued = 0;
    pahsc->pahsc_NumOutsPlayed = 0;
    
    SetFramesDone( pahsc, 0.0 );

    /* Pause channel so it does not do back ground processing while we are still filling the queue. */
    pauseCommand.cmd = pauseCmd;
    pauseCommand.param1 = pauseCommand.param2 = 0;
    error = SndDoCommand (pahsc->pahsc_Channel, &pauseCommand, true);
    if (noErr != error) goto exit;

    /* Queue all of the buffers so we start off full. */
    for (i = 0; i<pahsc->pahsc_NumHostBuffers; i++)
    {
        PaMac_PlayNext( past, i );
    }
    
    /* Resume channel now that the queue is full. */
    resumeCommand.cmd = resumeCmd;
    resumeCommand.param1 = resumeCommand.param2 = 0;
    error = SndDoImmediate( pahsc->pahsc_Channel, &resumeCommand );
    if (noErr != error) goto exit;

    return paNoError;
exit:
    past->past_IsActive = 0;
    sPaHostError = error;
    ERR_RPT(("Error in PaHost_StartOutput: SndDoCommand returned %d\n", error ));
    return paHostError;
}

/*******************************************************************/
long PaHost_GetTotalBufferFrames( internalPortAudioStream   *past )
{
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    return (long) (pahsc->pahsc_NumHostBuffers * pahsc->pahsc_FramesPerHostBuffer);
}

/***********************************************************************
** Called by Pa_StopStream().
** May be called during error recovery or cleanup code
** so protect against NULL pointers.
*/
PaError PaHost_StopOutput( internalPortAudioStream   *past, int abort )
{
    int32 timeOutMsec;
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    if( pahsc == NULL ) return paNoError;
    if( pahsc->pahsc_Channel == NULL ) return paNoError;

    DBUG(("PaHost_StopOutput()\n"));
    if( past->past_IsActive == 0 ) return paNoError;

    /* Set flags for callback function to see. */
    if( abort ) past->past_StopNow = 1;
    past->past_StopSoon = 1;
    /* Calculate timeOut longer than longest time it could take to play all buffers. */
    timeOutMsec = (int32) ((1500.0 * PaHost_GetTotalBufferFrames( past )) / past->past_SampleRate);
    /* Keep querying sound channel until it is no longer busy playing. */
    while( past->past_IsActive && (timeOutMsec > 0))
    {
        Pa_Sleep(20);
        timeOutMsec -= 20;
    }
    if( timeOutMsec <= 0 )
    {
        ERR_RPT(("PaHost_StopOutput: timed out!\n"));
        return paTimedOut;
    }
    else return paNoError;
}

/***********************************************************************/
PaError PaHost_StartEngine( internalPortAudioStream   *past )
{
    (void) past; /* Prevent unused variable warnings. */
    return paNoError;
}

/***********************************************************************/
PaError PaHost_StopEngine( internalPortAudioStream   *past, int abort )
{
    (void) past; /* Prevent unused variable warnings. */
    (void) abort; /* Prevent unused variable warnings. */
    return paNoError;
}
/***********************************************************************/
PaError PaHost_StreamActive( internalPortAudioStream   *past )
{
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    return (PaError) ( past->past_IsActive + pahsc->pahsc_IsRecording );
}
int Mac_IsVirtualMemoryOn( void )
{
    long  attr;
    OSErr result = Gestalt( gestaltVMAttr, &attr );
    DBUG(("gestaltVMAttr : 0x%x\n", attr ));
    return ((attr >> gestaltVMHasPagingControl ) & 1);
}

/*******************************************************************
* Determine number of host Buffers
* and how many User Buffers we can put into each host buffer.
*/
static void PaHost_CalcNumHostBuffers( internalPortAudioStream *past )
{
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    int32  minNumBuffers;
    int32  minFramesPerHostBuffer;
    int32  minTotalFrames;
    int32  userBuffersPerHostBuffer;
    int32  framesPerHostBuffer;
    int32  numHostBuffers;
    
    minFramesPerHostBuffer = pahsc->pahsc_MinFramesPerHostBuffer;
    minFramesPerHostBuffer = (minFramesPerHostBuffer + 7) & ~7;
    DBUG(("PaHost_CalcNumHostBuffers: minFramesPerHostBuffer = %d\n", minFramesPerHostBuffer ));
    
    /* Determine number of user buffers based on minimum latency. */
	/* PLB20020417 I used to call Pa_GetMinNumBuffers() which doesn't take into account the
	**    variable minFramesPerHostBuffer. Now I call PaMac_GetMinNumBuffers() which will
	**    gove lower latency when virtual memory is turned off. */
    /* minNumBuffers = Pa_GetMinNumBuffers( past->past_FramesPerUserBuffer, past->past_SampleRate ); WRONG */
    minNumBuffers = PaMac_GetMinNumBuffers( minFramesPerHostBuffer, past->past_FramesPerUserBuffer, past->past_SampleRate );
    
    past->past_NumUserBuffers = ( minNumBuffers > past->past_NumUserBuffers ) ? minNumBuffers : past->past_NumUserBuffers;
    DBUG(("PaHost_CalcNumHostBuffers: min past_NumUserBuffers = %d\n", past->past_NumUserBuffers ));
    minTotalFrames = past->past_NumUserBuffers * past->past_FramesPerUserBuffer;
    
    /* We cannot make the buffers too small because they may not get serviced quickly enough. */
    if( (int32) past->past_FramesPerUserBuffer < minFramesPerHostBuffer )
    {
        userBuffersPerHostBuffer =
            (minFramesPerHostBuffer + past->past_FramesPerUserBuffer - 1) /
            past->past_FramesPerUserBuffer;
    }
    else
    {
        userBuffersPerHostBuffer = 1;
    }
    framesPerHostBuffer = past->past_FramesPerUserBuffer * userBuffersPerHostBuffer;
    
    /* Calculate number of host buffers needed. Round up to cover minTotalFrames. */
    numHostBuffers = (minTotalFrames + framesPerHostBuffer - 1) / framesPerHostBuffer;
    /* Make sure we have enough host buffers. */
    if( numHostBuffers < PA_MIN_NUM_HOST_BUFFERS)
    {
        numHostBuffers = PA_MIN_NUM_HOST_BUFFERS;
    }
    else
    {
        /* If we have too many host buffers, try to put more user buffers in a host buffer. */
        while(numHostBuffers > PA_MAX_NUM_HOST_BUFFERS)
        {
            userBuffersPerHostBuffer += 1;
            framesPerHostBuffer = past->past_FramesPerUserBuffer * userBuffersPerHostBuffer;
            numHostBuffers = (minTotalFrames + framesPerHostBuffer - 1) / framesPerHostBuffer;
        }
    }

    pahsc->pahsc_UserBuffersPerHostBuffer = userBuffersPerHostBuffer;
    pahsc->pahsc_FramesPerHostBuffer = framesPerHostBuffer;
    pahsc->pahsc_NumHostBuffers = numHostBuffers;
    DBUG(("PaHost_CalcNumHostBuffers: pahsc_UserBuffersPerHostBuffer = %d\n", pahsc->pahsc_UserBuffersPerHostBuffer ));
    DBUG(("PaHost_CalcNumHostBuffers: pahsc_NumHostBuffers = %d\n", pahsc->pahsc_NumHostBuffers ));
    DBUG(("PaHost_CalcNumHostBuffers: pahsc_FramesPerHostBuffer = %d\n", pahsc->pahsc_FramesPerHostBuffer ));
    DBUG(("PaHost_CalcNumHostBuffers: past_NumUserBuffers = %d\n", past->past_NumUserBuffers ));
}

/*******************************************************************/
PaError PaHost_OpenStream( internalPortAudioStream   *past )
{
    OSErr             err;
    PaError             result = paHostError;
    PaHostSoundControl *pahsc;
    int                 i;
    /* Allocate and initialize host data. */
    pahsc = (PaHostSoundControl *) PaHost_AllocateFastMemory(sizeof(PaHostSoundControl));
    if( pahsc == NULL )
    {
        return paInsufficientMemory;
    }
    past->past_DeviceData = (void *) pahsc;

    /* If recording, and virtual memory is turned on, then use bigger buffers to prevent glitches. */
    if( (past->past_NumInputChannels > 0)  && Mac_IsVirtualMemoryOn() )
    {
        pahsc->pahsc_MinFramesPerHostBuffer = MAC_VIRTUAL_FRAMES_PER_BUFFER;
    }
    else
    {
        pahsc->pahsc_MinFramesPerHostBuffer = MAC_PHYSICAL_FRAMES_PER_BUFFER;
    }

    PaHost_CalcNumHostBuffers( past );
    
    /* Setup constants for CPU load measurement. */
    pahsc->pahsc_InverseMicrosPerHostBuffer = past->past_SampleRate / (1000000.0 * 	pahsc->pahsc_FramesPerHostBuffer);

    /* ------------------ OUTPUT */
    if( past->past_NumOutputChannels > 0 )
    {
        /* Create sound channel to which we can send commands. */
        pahsc->pahsc_Channel = 0L;
        err = SndNewChannel(&pahsc->pahsc_Channel, sampledSynth, 0, nil); /* FIXME - use kUseOptionalOutputDevice if not default. */
        if(err != 0)
        {
            ERR_RPT(("Error in PaHost_OpenStream: SndNewChannel returned 0x%x\n", err ));
            goto error;
        }

        /* Install our callback function pointer straight into the sound channel structure */
        /* Use new CARBON name for callback procedure. */
#if TARGET_API_MAC_CARBON
        pahsc->pahsc_OutputCompletionProc = NewSndCallBackUPP(PaMac_OutputCompletionProc);
#else
        pahsc->pahsc_OutputCompletionProc = NewSndCallBackProc(PaMac_OutputCompletionProc);
#endif

        pahsc->pahsc_Channel->callBack = pahsc->pahsc_OutputCompletionProc;

        pahsc->pahsc_BytesPerOutputHostBuffer = pahsc->pahsc_FramesPerHostBuffer * past->past_NumOutputChannels * sizeof(int16);
        for (i = 0; i<pahsc->pahsc_NumHostBuffers; i++)
        {
            char *buf = (char *)PaHost_AllocateFastMemory(pahsc->pahsc_BytesPerOutputHostBuffer);
            if (buf == NULL)
            {
                ERR_RPT(("Error in PaHost_OpenStream: could not allocate output buffer. Size = \n", pahsc->pahsc_BytesPerOutputHostBuffer ));
                goto memerror;
            }

            PaMac_InitSoundHeader( past, &pahsc->pahsc_SoundHeaders[i] );
            pahsc->pahsc_SoundHeaders[i].samplePtr = buf;
            pahsc->pahsc_SoundHeaders[i].numFrames = (unsigned long) pahsc->pahsc_FramesPerHostBuffer;

        }
    }
#ifdef SUPPORT_AUDIO_CAPTURE
    /* ------------------ INPUT */
    /* Use double buffer scheme that matches output. */
    if( past->past_NumInputChannels > 0 )
    {
        int16   tempS;
        long    tempL;
        Fixed   tempF;
        long    mRefNum;
        Str255 namePString;
#if TARGET_API_MAC_CARBON
        pahsc->pahsc_InputCompletionProc = NewSICompletionUPP((SICompletionProcPtr)PaMac_InputCompletionProc);
#else
        pahsc->pahsc_InputCompletionProc = NewSICompletionProc((ProcPtr)PaMac_InputCompletionProc);
#endif
        pahsc->pahsc_BytesPerInputHostBuffer = pahsc->pahsc_FramesPerHostBuffer * past->past_NumInputChannels * sizeof(int16);
        for (i = 0; i<pahsc->pahsc_NumHostBuffers; i++)
        {
            char *buf = (char *) PaHost_AllocateFastMemory(pahsc->pahsc_BytesPerInputHostBuffer);
            if ( buf == NULL )
            {
                ERR_RPT(("PaHost_OpenStream: could not allocate input  buffer. Size = \n", pahsc->pahsc_BytesPerInputHostBuffer ));
                goto memerror;
            }
            pahsc->pahsc_InputMultiBuffer.buffers[i] = buf;
        }
        pahsc->pahsc_InputMultiBuffer.numBuffers = pahsc->pahsc_NumHostBuffers;

        // err = SPBOpenDevice( (const unsigned char *) &noname, siWritePermission, &mRefNum);
        CToPString((char *)sDevices[past->past_InputDeviceID].pad_Info.name, namePString);
        err = SPBOpenDevice(namePString, siWritePermission, &mRefNum);

        if (err) goto error;
        pahsc->pahsc_InputRefNum = mRefNum;
        DBUG(("PaHost_OpenStream: mRefNum = %d\n", mRefNum ));

        /* Set input device characteristics. */
        tempS = 1;
        err = SPBSetDeviceInfo(mRefNum, siContinuous, (Ptr) &tempS);
        if (err)
        {
            ERR_RPT(("Error in PaHost_OpenStream: SPBSetDeviceInfo siContinuous returned %d\n", err ));
            goto error;
        }

        tempL = 0x03;
        err = SPBSetDeviceInfo(mRefNum, siActiveChannels, (Ptr) &tempL);
        if (err)
        {
            DBUG(("PaHost_OpenStream: setting siActiveChannels returned 0x%x. Error ignored.\n", err ));
        }

        /* PLB20010908 - Use requested number of input channels. Thanks Dominic Mazzoni. */
        tempS = past->past_NumInputChannels;
        err = SPBSetDeviceInfo(mRefNum, siNumberChannels, (Ptr) &tempS);
        if (err)
        {
            ERR_RPT(("Error in PaHost_OpenStream: SPBSetDeviceInfo siNumberChannels returned %d\n", err ));
            goto error;
        }

        tempF = ((unsigned long)past->past_SampleRate) << 16;
        err = SPBSetDeviceInfo(mRefNum, siSampleRate, (Ptr) &tempF);
        if (err)
        {
            ERR_RPT(("Error in PaHost_OpenStream: SPBSetDeviceInfo siSampleRate returned %d\n", err ));
            goto error;
        }

        /* Setup record-parameter block */
        pahsc->pahsc_InputParams.inRefNum          = mRefNum;
        pahsc->pahsc_InputParams.milliseconds      = 0;   // not used
        pahsc->pahsc_InputParams.completionRoutine = pahsc->pahsc_InputCompletionProc;
        pahsc->pahsc_InputParams.interruptRoutine  = 0;
        pahsc->pahsc_InputParams.userLong          = (long) past;
        pahsc->pahsc_InputParams.unused1           = 0;
    }
#endif /* SUPPORT_AUDIO_CAPTURE */
    DBUG(("PaHost_OpenStream: complete.\n"));
    return paNoError;

error:
    PaHost_CloseStream( past );
    ERR_RPT(("PaHost_OpenStream: sPaHostError =  0x%x.\n", err ));
    sPaHostError = err;
    return paHostError;

memerror:
    PaHost_CloseStream( past );
    return paInsufficientMemory;
}

/***********************************************************************
** Called by Pa_CloseStream().
** May be called during error recovery or cleanup code
** so protect against NULL pointers.
*/
PaError PaHost_CloseStream( internalPortAudioStream   *past )
{
    PaError result = paNoError;
    OSErr   err = 0;
    int     i;
    PaHostSoundControl *pahsc;

    DBUG(("PaHost_CloseStream( 0x%x )\n", past ));

    if( past == NULL ) return paBadStreamPtr;

    pahsc = (PaHostSoundControl *) past->past_DeviceData;
    if( pahsc == NULL ) return paNoError;

    if( past->past_NumOutputChannels > 0 )
    {
        /* TRUE means flush now instead of waiting for quietCmd to be processed. */
        if( pahsc->pahsc_Channel != NULL ) SndDisposeChannel(pahsc->pahsc_Channel, TRUE);
        {
            for (i = 0; i<pahsc->pahsc_NumHostBuffers; i++)
            {
                Ptr p = (Ptr) pahsc->pahsc_SoundHeaders[i].samplePtr;
                if( p != NULL ) PaHost_FreeFastMemory( p, pahsc->pahsc_BytesPerOutputHostBuffer );
            }
        }
    }

    if( past->past_NumInputChannels > 0 )
    {
        if( pahsc->pahsc_InputRefNum )
        {
            err = SPBCloseDevice(pahsc->pahsc_InputRefNum);
            pahsc->pahsc_InputRefNum = 0;
            if( err )
            {
                sPaHostError = err;
                result = paHostError;
            }
        }
        {
            for (i = 0; i<pahsc->pahsc_InputMultiBuffer.numBuffers; i++)
            {
                Ptr p = (Ptr) pahsc->pahsc_InputMultiBuffer.buffers[i];
                if( p != NULL ) PaHost_FreeFastMemory( p, pahsc->pahsc_BytesPerInputHostBuffer );
            }
        }
    }

    past->past_DeviceData = NULL;
    PaHost_FreeFastMemory( pahsc, sizeof(PaHostSoundControl) );

    DBUG(("PaHost_CloseStream: complete.\n", past ));
    return result;
}
/*************************************************************************/
int Pa_GetMinNumBuffers( int framesPerUserBuffer, double sampleRate )
{
/* We use the MAC_VIRTUAL_FRAMES_PER_BUFFER because we might be recording.
** This routine doesn't have enough information to determine the best value
** and is being depracated. */
    return PaMac_GetMinNumBuffers( MAC_VIRTUAL_FRAMES_PER_BUFFER, framesPerUserBuffer, sampleRate );
}
/*************************************************************************/
static int PaMac_GetMinNumBuffers( int minFramesPerHostBuffer, int framesPerUserBuffer, double sampleRate )
{
    int minUserPerHost = ( minFramesPerHostBuffer + framesPerUserBuffer - 1) / framesPerUserBuffer;
    int numBufs = PA_MIN_NUM_HOST_BUFFERS * minUserPerHost;
    if( numBufs < PA_MIN_NUM_HOST_BUFFERS ) numBufs = PA_MIN_NUM_HOST_BUFFERS;
    (void) sampleRate;
    return numBufs;
}

/*************************************************************************/
void Pa_Sleep( int32 msec )
{
    EventRecord   event;
    int32 sleepTime, endTime;
    /* Convert to ticks. Round up so we sleep a MINIMUM of msec time. */
    sleepTime = ((msec * 60) + 999) / 1000;
    if( sleepTime < 1 ) sleepTime = 1;
    endTime = TickCount() + sleepTime;
    do
    {
        DBUGX(("Sleep for %d ticks.\n", sleepTime ));
        /* Use WaitNextEvent() to sleep without getting events. */
        /* PLB20010907 - Pass unused event to WaitNextEvent instead of NULL to prevent
         * Mac OSX crash. Thanks Dominic Mazzoni. */
        WaitNextEvent( 0, &event, sleepTime, NULL );
        sleepTime = endTime - TickCount();
    }
    while( sleepTime > 0 );
}
/*************************************************************************/
int32 Pa_GetHostError( void )
{
    int32 err = sPaHostError;
    sPaHostError = 0;
    return err;
}

/*************************************************************************
 * 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 = NewPtrClear( numBytes );
    if( (addr == NULL) || (MemError () != 0) ) return NULL;

#if (TARGET_API_MAC_CARBON == 0)
    if( HoldMemory( addr, numBytes ) != noErr )
    {
        DisposePtr( (Ptr) addr );
        return NULL;
    }
#endif
    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 ) return;
#if TARGET_API_MAC_CARBON
    (void) numBytes;
#else
    UnholdMemory( addr, numBytes );
#endif
    DisposePtr( (Ptr) addr );
}

/*************************************************************************/
PaTimestamp Pa_StreamTime( PortAudioStream *stream )
{
	PaTimestamp   framesDone1;
	PaTimestamp   framesDone2;
	UInt64        whenIncremented;
	UnsignedWide  now;
	UInt64        now64;
	long          microsElapsed;
	long          framesElapsed;
	
    PaHostSoundControl *pahsc;
    internalPortAudioStream   *past = (internalPortAudioStream *) stream;
    if( past == NULL ) return paBadStreamPtr;
    pahsc = (PaHostSoundControl *) past->past_DeviceData;
    
/* Capture information from audio thread.
 * We have to be careful that we don't get interrupted in the middle.
 * So we grab the pahsc_NumFramesDone twice and make sure it didn't change.
 */
 	do
 	{
 		framesDone1 = pahsc->pahsc_NumFramesDone;
 		whenIncremented = pahsc->pahsc_WhenFramesDoneIncremented;
 		framesDone2 = pahsc->pahsc_NumFramesDone;
 	} while( framesDone1 != framesDone2 );
 	
 /* Calculate how many microseconds have elapsed and convert to frames. */
 	Microseconds( &now );
 	now64 = UnsignedWideToUInt64( now );
 	microsElapsed = U64Subtract( now64, whenIncremented );
 	framesElapsed = microsElapsed * past->past_SampleRate * 0.000001;
 	
	return framesDone1 + framesElapsed;
}

/**************************************************************************
** Callback for Input, SPBRecord()
*/
int gRecordCounter = 0;
int gPlayCounter = 0;
pascal void PaMac_InputCompletionProc(SPBPtr recParams)
{
    PaError                    result = paNoError;
    int                        finished = 1;
    internalPortAudioStream   *past;
    PaHostSoundControl        *pahsc;

    gRecordCounter += 1; /* debug hack to see if engine running */

    /* Get our PA data from Mac structure. */
    past = (internalPortAudioStream *) recParams->userLong;
    if( past == NULL ) return;

    if( past->past_Magic != PA_MAGIC )
    {
        AddTraceMessage("PaMac_InputCompletionProc: bad MAGIC, past", (long) past );
        AddTraceMessage("PaMac_InputCompletionProc: bad MAGIC, magic", (long) past->past_Magic );
        goto error;
    }
    pahsc = (PaHostSoundControl *) past->past_DeviceData;
    past->past_NumCallbacks += 1;

    /* Have we been asked to stop recording? */
    if( (recParams->error == abortErr) || pahsc->pahsc_StopRecording ) goto error;

    /* If there are no output channels, then we need to call the user callback function from here.
     * Otherwise we will call the user code during the output completion routine.
     */
    if(past->past_NumOutputChannels == 0)
    {
		SetFramesDone( pahsc,
			pahsc->pahsc_NumFramesDone + pahsc->pahsc_FramesPerHostBuffer );
        result = PaMac_CallUserLoop( past, NULL );
    }

    /* Did user code ask us to stop? If not, issue another recording request. */
    if( (result == paNoError) && (pahsc->pahsc_StopRecording == 0) )
    {
        result = PaMac_RecordNext( past );
        if( result != paNoError ) pahsc->pahsc_IsRecording = 0;
    }
    else goto error;

    return;

error:
    pahsc->pahsc_IsRecording = 0;
    pahsc->pahsc_StopRecording = 0;
    return;
}

/***********************************************************************
** Called by either input or output completion proc.
** Grabs input data if any present, and calls PA conversion code,
** that in turn calls user code.
*/
static PaError PaMac_CallUserLoop( internalPortAudioStream   *past, int16 *outPtr )
{
    PaError             result = paNoError;
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    int16              *inPtr = NULL;
    int                 i;
    

    /* Advance read index for sound input FIFO here, independantly of record/write process. */
    if(past->past_NumInputChannels > 0)
    {
        if( MultiBuffer_IsReadable( &pahsc->pahsc_InputMultiBuffer ) )
        {
            inPtr = (int16 *)  MultiBuffer_GetNextReadBuffer( &pahsc->pahsc_InputMultiBuffer );
            MultiBuffer_AdvanceReadIndex( &pahsc->pahsc_InputMultiBuffer );
        }
    }

    /* Call user code enough times to fill buffer. */
    if( (inPtr != NULL) || (outPtr != NULL) )
    {
        PaMac_StartLoadCalculation( past ); /* CPU usage */

#ifdef PA_MAX_USAGE_ALLOWED
	/* If CPU usage exceeds limit, skip user callback to prevent hanging CPU. */
		if( past->past_Usage > PA_MAX_USAGE_ALLOWED )
	    {
	    	past->past_FrameCount += (PaTimestamp) pahsc->pahsc_FramesPerHostBuffer;
		}
		else
#endif
		{
		
	        for( i=0; i<pahsc->pahsc_UserBuffersPerHostBuffer; i++ )
	        {
	            result = (PaError) Pa_CallConvertInt16( past, inPtr, outPtr );
	            if( result != 0)
	            {
	                /* Recording might be in another process, so tell it to stop with a flag. */
	                pahsc->pahsc_StopRecording = pahsc->pahsc_IsRecording;
	                break;
	            }
	            /* Advance sample pointers. */
	            if(inPtr != NULL) inPtr += past->past_FramesPerUserBuffer * past->past_NumInputChannels;
	            if(outPtr != NULL) outPtr += past->past_FramesPerUserBuffer * past->past_NumOutputChannels;
	        }
	    }
		
        PaMac_EndLoadCalculation( past );
    }
    return result;
}

/***********************************************************************
** Setup next recording buffer in FIFO and issue recording request to Snd Input Manager.
*/
static PaError PaMac_RecordNext( internalPortAudioStream   *past )
{
    PaError  result = paNoError;
    OSErr     err;
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    /* Get pointer to next buffer to record into. */
    pahsc->pahsc_InputParams.bufferPtr  = MultiBuffer_GetNextWriteBuffer( &pahsc->pahsc_InputMultiBuffer );

    /* Advance write index if there is room. Otherwise keep writing same buffer. */
    if( MultiBuffer_IsWriteable( &pahsc->pahsc_InputMultiBuffer ) )
    {
        MultiBuffer_AdvanceWriteIndex( &pahsc->pahsc_InputMultiBuffer );
    }

    AddTraceMessage("PaMac_RecordNext: bufferPtr", (long) pahsc->pahsc_InputParams.bufferPtr );
    AddTraceMessage("PaMac_RecordNext: nextWrite", pahsc->pahsc_InputMultiBuffer.nextWrite );

    /* Setup parameters and issue an asynchronous recording request. */
    pahsc->pahsc_InputParams.bufferLength      = pahsc->pahsc_BytesPerInputHostBuffer;
    pahsc->pahsc_InputParams.count             = pahsc->pahsc_BytesPerInputHostBuffer;
    err = SPBRecord(&pahsc->pahsc_InputParams, true);
    if( err )
    {
        AddTraceMessage("PaMac_RecordNext: SPBRecord error ", err );
        sPaHostError = err;
        result = paHostError;
    }
    else
    {
        pahsc->pahsc_IsRecording = 1;
    }
    return result;
}

/**************************************************************************
** Callback for Output Playback()
** Return negative error, 0 to continue, 1 to stop.
*/
long    PaMac_FillNextOutputBuffer( internalPortAudioStream   *past, int index )
{
    PaHostSoundControl  *pahsc;
    long                 result = 0;
    int                  finished = 1;
    char                *outPtr;

    gPlayCounter += 1; /* debug hack */

    past->past_NumCallbacks += 1;
    pahsc = (PaHostSoundControl *) past->past_DeviceData;
    if( pahsc == NULL ) return -1;
    /* Are we nested?! */
    if( pahsc->pahsc_IfInsideCallback ) return 0;
    pahsc->pahsc_IfInsideCallback = 1;
    /* Get pointer to buffer to fill. */
    outPtr = pahsc->pahsc_SoundHeaders[index].samplePtr;
    /* Combine with any sound input, and call user callback. */
    result = PaMac_CallUserLoop( past, (int16 *) outPtr );

    pahsc->pahsc_IfInsideCallback = 0;
    return result;
}

/*************************************************************************************
** Called by SoundManager when ready for another buffer.
*/
static pascal void PaMac_OutputCompletionProc (SndChannelPtr theChannel, SndCommand * theCallBackCmd)
{
    internalPortAudioStream *past;
    PaHostSoundControl      *pahsc;
    (void) theChannel;
    (void) theCallBackCmd;

    /* Get our data from Mac structure. */
    past = (internalPortAudioStream *) theCallBackCmd->param2;
    if( past == NULL ) return;

    pahsc = (PaHostSoundControl *) past->past_DeviceData;
    pahsc->pahsc_NumOutsPlayed += 1;

	SetFramesDone( pahsc,
			pahsc->pahsc_NumFramesDone + pahsc->pahsc_FramesPerHostBuffer );
			
    PaMac_BackgroundManager( past, theCallBackCmd->param1 );
}

/*******************************************************************/
static PaError PaMac_BackgroundManager( internalPortAudioStream   *past, int index )
{
    PaError      result = paNoError;
    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;
    /* Has someone asked us to abort by calling Pa_AbortStream()? */
    if( past->past_StopNow )
    {
        SndCommand  command;
        /* Clear the queue of any pending commands. */
        command.cmd = flushCmd;
        command.param1 = command.param2 = 0;
        SndDoImmediate( pahsc->pahsc_Channel, &command );
        /* Then stop currently playing buffer, if any. */
        command.cmd = quietCmd;
        SndDoImmediate( pahsc->pahsc_Channel, &command );
        past->past_IsActive = 0;
    }
    /* 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 )
    {
        if( (pahsc->pahsc_NumOutsQueued - pahsc->pahsc_NumOutsPlayed) <= 0 )
        {
            past->past_IsActive = 0; /* We're finally done. */
        }
    }
    else
    {
        PaMac_PlayNext( past, index );
    }
    return result;
}

/*************************************************************************************
** Fill next buffer with sound and queue it for playback.
*/
static void PaMac_PlayNext ( internalPortAudioStream *past, int index )
{
    OSErr                  error;
    long                     result;
    SndCommand               playCmd;
    SndCommand           callbackCmd;
    PaHostSoundControl      *pahsc = (PaHostSoundControl *) past->past_DeviceData;

    /* If this was the last buffer, or abort requested, then just be done. */
    if ( past->past_StopSoon ) goto done;
    
    /* Load buffer with sound. */
    result = PaMac_FillNextOutputBuffer ( past, index );
    if( result > 0 ) past->past_StopSoon = 1; /* Stop generating audio but wait until buffers play. */
    else if( result < 0 ) goto done;
    
    /* Play the next buffer. */
    playCmd.cmd = bufferCmd;
    playCmd.param1 = 0;
    playCmd.param2 = (long) &pahsc->pahsc_SoundHeaders[ index ];
    error = SndDoCommand (pahsc->pahsc_Channel, &playCmd, true );
    if( error != noErr ) goto gotError;
    
    /* Ask for a callback when it is done. */
    callbackCmd.cmd = callBackCmd;
    callbackCmd.param1 = index;
    callbackCmd.param2 = (long)past;
    error = SndDoCommand (pahsc->pahsc_Channel, &callbackCmd, true );
    if( error != noErr ) goto gotError;
    pahsc->pahsc_NumOutsQueued += 1;

    return;

gotError:
    sPaHostError = error;
done:
    return;
}