File: gvOSXAudio.c

package info (click to toggle)
openmohaa 0.82.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 34,192 kB
  • sloc: cpp: 315,720; ansic: 275,789; sh: 312; xml: 246; asm: 141; makefile: 7
file content (1465 lines) | stat: -rw-r--r-- 38,665 bytes parent folder | download | duplicates (2)
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
#include "gvOSXAudio.h"
#include "gvDevice.h"
#include "gvCodec.h"
#include "gvSource.h"
#include "gvUtil.h"
#include <AudioToolbox/AudioConverter.h>
#include <CoreAudio/CoreAudio.h>
#include <pthread.h>

#if !defined(_MACOSX)
#error This file should only be used with MacOS X
#endif

#if !defined(GVI_VOLUME_IN_SOFTWARE)
	#define GVI_VOLUME_IN_SOFTWARE 1
#endif

/**********
** TYPES **
**********/
// when allocated, enough memory is allocated to fit an entire
// captured frame into the m_frame array
typedef struct GVICapturedFrame
{
	GVFrameStamp m_frameStamp;
	struct GVICapturedFrame * m_next;
	// m_frame must be the last member of this struct
	GVSample m_frame[1];
} GVICapturedFrame;

typedef struct
{
	GVBool m_playing;
	AudioConverterRef m_playbackConverter;
	AudioStreamBasicDescription m_playbackStreamDescriptor;
	GVFrameStamp m_playbackClock;
	GVSample * m_playbackBuffer;
	GVISourceList m_playbackSources;
	int m_playbackChannel;
#if GVI_VOLUME_IN_SOFTWARE
	GVScalar m_playbackVolume;
#endif

	GVBool m_capturing;
	AudioConverterRef m_captureConverter;
	AudioStreamBasicDescription m_captureStreamDescriptor;
	GVFrameStamp m_captureClock;
	GVSample * m_captureBuffer;
	GVICapturedFrame m_capturedFrames;
	GVICapturedFrame m_captureAvailableFrames;
	GVScalar m_captureThreshold;
	GVFrameStamp m_captureLastCrossedThresholdTime;
	int m_captureChannel;
#if GVI_VOLUME_IN_SOFTWARE
	GVScalar m_captureVolume;
#endif

	pthread_mutex_t m_mutex;
	GVBool m_unplugged;
} GVIHardwareData;

typedef struct
{
	GVIDevice * m_device;
	AudioBufferList * m_capturedAudio;
	GVBool m_used;
} GVICaptureConverterData;

/************
** GLOBALS **
************/
static GVIDeviceList GVIDevices;
static AudioStreamBasicDescription GVIVoiceFormat;

/**************
** FUNCTIONS **
**************/
static void gviFreeCapturedFrames(GVICapturedFrame * frameHead)
{
	GVICapturedFrame * frame;
	while(frameHead->m_next)
	{
		frame = frameHead->m_next;
		frameHead->m_next = frame->m_next;
		gsifree(frame);
	}
}

static GVICapturedFrame * gviPopFirstFrame(GVICapturedFrame * frameHead)
{
	GVICapturedFrame * frame = frameHead->m_next;
	if(frame)
	{
		frameHead->m_next = frame->m_next;
		frame->m_next = NULL;
	}
	return frame;
}

static void gviPushFirstFrame(GVICapturedFrame * frameHead, GVICapturedFrame * frame)
{
	frame->m_next = frameHead->m_next;
	frameHead->m_next = frame;
}

static void gviPushLastFrame(GVICapturedFrame * frameHead, GVICapturedFrame * frame)
{
	GVICapturedFrame * lastFrame;

	// find the last frame in the list
	lastFrame = frameHead;
	while(lastFrame->m_next)
		lastFrame = lastFrame->m_next;

	// add this frame to the end of the capture list
	lastFrame->m_next = frame;
}

static GVBool gviLockDevice(GVIHardwareData * data)
{
	int rcode;
	rcode = pthread_mutex_lock(&data->m_mutex);
	return (rcode == 0)?GVTrue:GVFalse;
}

static void gviUnlockDevice(GVIHardwareData * data)
{
	pthread_mutex_unlock(&data->m_mutex);
}

static void gviCleanupPlayback(GVIHardwareData * data)
{
	if(data->m_playbackSources)
		gviFreeSourceList(data->m_playbackSources);
	if(data->m_playbackConverter)
		AudioConverterDispose(data->m_playbackConverter);
	gsifree(data->m_playbackBuffer);
}

static void gviCleanupCapture(GVIHardwareData * data)
{
	if(data->m_captureConverter)
		AudioConverterDispose(data->m_captureConverter);
	gviFreeCapturedFrames(&data->m_captureAvailableFrames);
	gviFreeCapturedFrames(&data->m_capturedFrames);
	gsifree(data->m_captureBuffer);
}

static void gviFreeArrayDevice(void * elem)
{
	GVIDevice * device = *(GVIDevice **)elem;
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;

	// stop the device
	device->m_methods.m_stopDevice(device, device->m_types);

	// free the mutex
	pthread_mutex_destroy(&data->m_mutex);

	// playback specific cleanup
	if(device->m_types & GV_PLAYBACK)
	{
		gviCleanupPlayback(data);
	}

	// capture specific cleanup
	if(device->m_types & GV_CAPTURE)
	{
		gviCleanupCapture(data);
	}

	gviFreeDevice(device);
}

GVBool gviHardwareStartup(void)
{
	// create the devices array
	GVIDevices = gviNewDeviceList(gviFreeArrayDevice);
	if(!GVIDevices)
		return GVFalse;

	// setup the format descriptor for the GV format
	memset(&GVIVoiceFormat, 0, sizeof(AudioStreamBasicDescription));
	GVIVoiceFormat.mSampleRate = (Float64)GV_SAMPLES_PER_SECOND;
	GVIVoiceFormat.mFormatID = kAudioFormatLinearPCM;
	GVIVoiceFormat.mFormatFlags = kAudioFormatFlagsNativeEndian|kAudioFormatFlagIsSignedInteger|kAudioFormatFlagIsPacked;
	GVIVoiceFormat.mBytesPerPacket = GV_BYTES_PER_SAMPLE;
	GVIVoiceFormat.mFramesPerPacket = 1;
	GVIVoiceFormat.mBytesPerFrame = GV_BYTES_PER_SAMPLE;
	GVIVoiceFormat.mChannelsPerFrame = 1;
	GVIVoiceFormat.mBitsPerChannel = GV_BITS_PER_SAMPLE;

	return GVTrue;
}

void gviHardwareCleanup(void)
{
	// cleanup the devices
	if(GVIDevices)
	{
		gviFreeDeviceList(GVIDevices);
		GVIDevices = NULL;
	}
}

void gviHardwareThink(void)
{
	GVIDevice * device;
	int num;
	int i;

	if(!GVIDevices)
		return;

	// loop through backwards so that we can remove devices as we go
	num = gviGetNumDevices(GVIDevices);
	for(i = (num - 1) ; i >= 0 ; i--)
	{
		// get the device
		device = gviGetDevice(GVIDevices, i);

		// has it been unplugged?
		if(((GVIHardwareData*)device->m_data)->m_unplugged)
		{
			gviDeviceUnplugged(device);
		}
	}
}

static GVBool gviCFStringToString(CFStringRef ref, gsi_char str[], int strLen)
{
#if !defined(GSI_UNICODE)

	Boolean result = CFStringGetCString(ref, str, strLen, kCFStringEncodingASCII);
	return (result == true)?GVTrue:GVFalse;

#else
	
	CFRange range;

	range.location = 0;
	range.length = CFStringGetLength(ref);
	range.length = min(range.length, strLen - 1);

	CFStringGetCharacters(ref, range, str);
	str[range.length] = '\0';

	return GVTrue;

#endif
}

static int gviCountChannels(AudioDeviceID deviceID, bool input)
{
	OSStatus result;
	UInt32 size;
	AudioBufferList * list;
	int numChannels;
	int i;

	// get the size of the buffer list
	result = AudioDeviceGetPropertyInfo(deviceID, 0, input, kAudioDevicePropertyStreamConfiguration, &size, NULL);
	if(result != noErr)
		return 0;

	// allocate the buffer list
	list = (AudioBufferList *)gsimalloc(size);
	if(list == NULL)
		return 0;

	// fill the buffer list
	result = AudioDeviceGetProperty(deviceID, 0, input, kAudioDevicePropertyStreamConfiguration, &size, list);
	if(result != noErr)
	{
		gsifree(list);
		return 0;
	}

	// count the number of channels
	numChannels = 0;
	for(i = 0 ; i < list->mNumberBuffers ; i++)
		numChannels += list->mBuffers[i].mNumberChannels;

	// free the list
	gsifree(list);

	return numChannels;
}

static GVBool gviFillDeviceInfo(AudioDeviceID deviceID, GVDeviceInfo * device, GVDeviceType types)
{
	OSStatus result;
	UInt32 size;
	UInt32 isAlive;
	GVDeviceType supportedTypes;
	CFStringRef nameRef;

	// make sure the device is alive
	size = sizeof(UInt32);
	result = AudioDeviceGetProperty(deviceID, 0, true, kAudioDevicePropertyDeviceIsAlive, &size, &isAlive);
	if((result != noErr) || (isAlive != 1))
		return GVFalse;

	// set the hardware type
	device->m_hardwareType = GVHardwareMacOSX;

	// store the id
	device->m_id = deviceID;

	// figure out what types it supports
	supportedTypes = (GVDeviceType)0;
	if(gviCountChannels(deviceID, true) > 0)
		supportedTypes |= GV_CAPTURE;
	if(gviCountChannels(deviceID, false) > 0)
		supportedTypes |= GV_PLAYBACK;

	// if there's nothing in common, return
	if(!(supportedTypes & types))
		return GVFalse;

	// store the supported types
	device->m_deviceType = supportedTypes;

	// get the name
	size = sizeof(nameRef);
	result = AudioDeviceGetProperty(deviceID, 0, (supportedTypes & GV_CAPTURE)?true:false, kAudioDevicePropertyDeviceNameCFString, &size, &nameRef);
	if(result != noErr)
		return GVFalse;

	// if there's a blank name, we'll supply our own
	if(CFStringGetLength(nameRef) <= 0)
	{
		CFRelease(nameRef);
		nameRef = NULL;
	}

	// if there's no name, give a default
	if(nameRef == NULL)
	{
		if(supportedTypes == GV_CAPTURE)
			nameRef = CFSTR("Capture Device");
		else if(supportedTypes == GV_PLAYBACK)
			nameRef = CFSTR("Playback Device");
		else
			nameRef = CFSTR("Capture & Playback Device");
	}

	// convert it to an array of gsi_char
	gviCFStringToString(nameRef, device->m_name, GV_DEVICE_NAME_LEN);

	// release the CFString
	CFRelease(nameRef);

	return GVTrue;
}

int gviHardwareListDevices(GVDeviceInfo devices[], int maxDevices, GVDeviceType types)
{
	OSStatus result;
	UInt32 size;
	int numDevices;
	int deviceCount;
	AudioDeviceID * deviceIDs;
	AudioDeviceID defaultCaptureDeviceID;
	AudioDeviceID defaultPlaybackDeviceID;
	GVDeviceType defaultTypes;
	int i;

	// get the default device ids
	size = sizeof(AudioDeviceID);
	result = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &size, &defaultCaptureDeviceID);
	if(result != noErr)
		defaultCaptureDeviceID = kAudioDeviceUnknown;
	result = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &size, &defaultPlaybackDeviceID);
	if(result != noErr)
		defaultPlaybackDeviceID = kAudioDeviceUnknown;

	// get the size of the device ids array
	result = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &size, NULL);
	if(result != noErr)
		return 0;

	// calc the number of devices
	numDevices = (size / sizeof(AudioDeviceID));

	// allocate the array of device ids
	deviceIDs = (AudioDeviceID *)gsimalloc(size);
	if(deviceIDs == NULL)
		return 0;

	// get the device ids
	result = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &size, deviceIDs);
	if(result != noErr)
	{
		gsifree(deviceIDs);
		return 0;
	}

	// fill the array of devices with info
	deviceCount = 0;
	for(i = 0 ; (i < maxDevices) && (deviceCount < numDevices) ; i++)
	{
		// try to add the device to the list
		GVBool addedDevice = gviFillDeviceInfo(deviceIDs[i], &devices[deviceCount], types);

		// check if it was added successfully
		if(addedDevice)
		{
			// check it against the defaults
			defaultTypes = (GVDeviceType)0;
			if(deviceIDs[i] == defaultCaptureDeviceID)
				defaultTypes |= GV_CAPTURE;
			if(deviceIDs[i] == defaultPlaybackDeviceID)
				defaultTypes |= GV_PLAYBACK;
			devices[deviceCount].m_defaultDevice = defaultTypes;

			// increment the count
			deviceCount++;
		}
	}

	// free the array
	gsifree(deviceIDs);

	return deviceCount;
}

static void gviHardwareFreeDevice(GVDevice device)
{
	// delete it from the array
	// it will clear out internal data in the array's free function
	gviDeleteDeviceFromList(GVIDevices, device);
}

static OSStatus gviAudioConverterCaptureProc(AudioConverterRef inAudioConverter,
                                             UInt32 * ioNumberDataPackets,
                                             AudioBufferList * ioData,
                                             AudioStreamPacketDescription ** outDataPacketDescription,
                                             void * inUserData)
{
	GVICaptureConverterData * converterData = (GVICaptureConverterData *)inUserData;
	GVIDevice * device = converterData->m_device;
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;
	AudioBufferList * bufferList = converterData->m_capturedAudio;

	// check if this data has already been used
	if(converterData->m_used)
		return (OSStatus)1;

	// it only wants one buffer
	ioData->mBuffers[0].mNumberChannels = bufferList->mBuffers[data->m_captureChannel].mNumberChannels;
	ioData->mBuffers[0].mData = bufferList->mBuffers[data->m_captureChannel].mData;
	ioData->mBuffers[0].mDataByteSize = bufferList->mBuffers[data->m_captureChannel].mDataByteSize;

	// fill in the number of samples we provided
	*ioNumberDataPackets = (ioData->mBuffers[0].mDataByteSize / data->m_captureStreamDescriptor.mBytesPerPacket);

	// we've used the buffer
	converterData->m_used = GVTrue;

	return noErr;
}

static OSStatus gviHardwareCaptureIOProc(AudioDeviceID inDevice,
                                         const AudioTimeStamp * inNow,
                                         const AudioBufferList * inInputData, 
                                         const AudioTimeStamp * inInputTime,
                                         AudioBufferList * outOutputData, 
                                         const AudioTimeStamp * inOutputTime,
                                         void * inClientData)
{
	GVIDevice * device = (GVIDevice *)inClientData;
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;
	OSStatus result;
	UInt32 size;
	GVICaptureConverterData converterData;
	AudioBufferList bufferList;
	GVICapturedFrame * frame;

	// get a lock on the device
	if(!gviLockDevice(data))
		return (OSStatus)1;

	// make sure we are capturing
	if(data->m_capturing)
	{
		// setup the buffer list
		bufferList.mNumberBuffers = 1;
		bufferList.mBuffers[0].mNumberChannels = 1;
		bufferList.mBuffers[0].mDataByteSize = GVIBytesPerFrame;
		bufferList.mBuffers[0].mData = data->m_captureBuffer;

		// setup the converter data struct
		converterData.m_device = device;
		converterData.m_capturedAudio = (AudioBufferList *)inInputData;
		converterData.m_used = GVFalse;

		// loop while it is converting data
		do
		{
			// request one frame
			size = GVISamplesPerFrame;

			// convert the captured data into our format
			result = AudioConverterFillComplexBuffer(data->m_captureConverter, gviAudioConverterCaptureProc, &converterData, &size, &bufferList, NULL);

			// was there enough to fill a buffer
			if(result == noErr)
			{
				// get a frame
				frame = gviPopFirstFrame(&data->m_captureAvailableFrames);

				// if there aren't any available frames, repurpose the oldest captured frame
				if(!frame)
				{
					frame = gviPopFirstFrame(&data->m_capturedFrames);
					assert(frame);
					if(!frame)
						break;
				}

				// setup the frame
				memcpy(frame->m_frame, data->m_captureBuffer, GVIBytesPerFrame);
				frame->m_frameStamp = data->m_captureClock;

				// increment the capture clock
				data->m_captureClock++;

				// add this frame to the end of the capture list
				gviPushLastFrame(&data->m_capturedFrames, frame);
			}
		}
		while(result == noErr);
	}

	// release the device lock
	gviUnlockDevice(data);

	return noErr;
}

static GVBool gviHardwareStartCapture(GVDevice device)
{
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;
	OSStatus result;

	// now capturing
	data->m_capturing = GVTrue;

	// add the IO proc
	result = AudioDeviceAddIOProc((AudioDeviceID)device->m_deviceID, gviHardwareCaptureIOProc, device);
	if(result != noErr)
	{
		data->m_capturing = GVFalse;
		return GVFalse;
	}

	// start it
	result = AudioDeviceStart((AudioDeviceID)device->m_deviceID, gviHardwareCaptureIOProc);
	if(result != noErr)
	{
		data->m_capturing = GVFalse;
		return GVFalse;
	}

	return GVTrue;
}

static OSStatus gviAudioConverterPlaybackProc(AudioConverterRef inAudioConverter,
                                              UInt32 * ioNumberDataPackets,
                                              AudioBufferList * ioData,
                                              AudioStreamPacketDescription ** outDataPacketDescription,
                                              void * inUserData)
{
	GVIDevice * device = (GVIDevice *)inUserData;
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;
	GVBool wroteToBuffer = GVFalse;

	// make sure we are playing
	if(data->m_playing)
	{
		// write sources to the playback buffer
		wroteToBuffer = gviWriteSourcesToBuffer(data->m_playbackSources, data->m_playbackClock, data->m_playbackBuffer, 1);
	}

	// clear it if nothing was written
	if(!data->m_playing || !wroteToBuffer)
	{
		memset(data->m_playbackBuffer, 0, (size_t)GVIBytesPerFrame);
	}
	// check if we need to adjust the volume
	else if(data->m_playbackVolume < 1.0)
	{
		int i;
		for(i = 0 ; i < GVISamplesPerFrame ; i++)
			data->m_playbackBuffer[i] = (GVSample)(data->m_playbackBuffer[i] * data->m_playbackVolume);
	}

	// filter
	if(device->m_playbackFilterCallback)
		device->m_playbackFilterCallback(device, data->m_playbackBuffer, data->m_playbackClock);

	// setup the output data
	ioData->mBuffers[0].mNumberChannels = 1;
	ioData->mBuffers[0].mDataByteSize = GVIBytesPerFrame;
	ioData->mBuffers[0].mData = (void *)data->m_playbackBuffer;

	// we wrote one frame
	*ioNumberDataPackets = GVISamplesPerFrame;

	// update the clock
	data->m_playbackClock++;

	return noErr;
}

static OSStatus gviHardwarePlaybackIOProc(AudioDeviceID inDevice,
                                          const AudioTimeStamp * inNow,
                                          const AudioBufferList * inInputData, 
                                          const AudioTimeStamp * inInputTime,
                                          AudioBufferList * outOutputData, 
                                          const AudioTimeStamp * inOutputTime,
                                          void * inClientData)
{
	GVIDevice * device = (GVIDevice *)inClientData;
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;
	AudioBufferList bufferList;
	OSStatus result;
	UInt32 size;

	// get a lock on the device
	if(!gviLockDevice(data))
		return (OSStatus)1;

	// calculate the number of samples the proc wants
	size = (outOutputData->mBuffers[0].mDataByteSize / data->m_playbackStreamDescriptor.mBytesPerFrame);

	// setup our own buffer list, pointing at the channel (buffer) we want
	bufferList.mNumberBuffers = 1;
	bufferList.mBuffers[0] = outOutputData->mBuffers[data->m_playbackChannel];

	// fill the buffer using the callback
	result = AudioConverterFillComplexBuffer(data->m_playbackConverter, gviAudioConverterPlaybackProc, device, &size, &bufferList, NULL);
	if(result != noErr)
		return (OSStatus)1;

	// release the device lock
	gviUnlockDevice(data);

	return noErr;
}

static GVBool gviHardwareStartPlayback(GVDevice device)
{
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;
	OSStatus result;

	// now playing
	data->m_playing = GVTrue;

	// reset the playback clock
	data->m_playbackClock = 0;

	// add the IO proc
	result = AudioDeviceAddIOProc((AudioDeviceID)device->m_deviceID, gviHardwarePlaybackIOProc, device);
	if(result != noErr)
	{
		data->m_playing = GVFalse;
		return GVFalse;
	}

	// start it
	result = AudioDeviceStart((AudioDeviceID)device->m_deviceID, gviHardwarePlaybackIOProc);
	if(result != noErr)
	{
		data->m_playing = GVFalse;
		return GVFalse;
	}

	return GVTrue;
}

static GVBool gviHardwareStartDevice(GVDevice device, GVDeviceType type)
{
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;
	GVBool result;

	// get a lock on the device
	if(!gviLockDevice(data))
		return GVFalse;

	// capture
	if((type & GV_CAPTURE) && !data->m_capturing)
	{
		result = gviHardwareStartCapture(device);
		if(!result)
		{
			gviUnlockDevice(data);
			return GVFalse;
		}
	}

	// playback
	if((type & GV_PLAYBACK) && !data->m_playing)
	{
		result = gviHardwareStartPlayback(device);
		if(!result)
		{
			if(type & GV_CAPTURE)
				device->m_methods.m_stopDevice(device, GV_CAPTURE);
			gviUnlockDevice(data);
			return GVFalse;
		}
	}

	// release the device lock
	gviUnlockDevice(data);

	return GVTrue;
}

static void gviHardwareStopDevice(GVDevice device, GVDeviceType type)
{
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;
	GVICapturedFrame * frame;

	// lock the device
	gviLockDevice(data);

	// capture
	if((type & GV_CAPTURE) && data->m_capturing)
	{
		// unlock so the ioproc can stop
		gviUnlockDevice(data);

		// stop io
		AudioDeviceStop((AudioDeviceID)device->m_deviceID, gviHardwareCaptureIOProc);

		// relock
		gviLockDevice(data);

		// remove the IO proc
		AudioDeviceRemoveIOProc((AudioDeviceID)device->m_deviceID, gviHardwareCaptureIOProc);

		// move captured frames back to the available frames list
		while((frame = gviPopFirstFrame(&data->m_capturedFrames)) != NULL)
			gviPushFirstFrame(&data->m_captureAvailableFrames, frame);

		// no longer capturing
		data->m_capturing = GVFalse;
	}

	// playback
	if((type & GV_PLAYBACK) && data->m_playing)
	{
		// unlock so the ioproc can stop
		gviUnlockDevice(data);

		// stop io
		AudioDeviceStop((AudioDeviceID)device->m_deviceID, gviHardwarePlaybackIOProc);

		// relock
		gviLockDevice(data);

		// remove the IO proc
		AudioDeviceRemoveIOProc((AudioDeviceID)device->m_deviceID, gviHardwarePlaybackIOProc);

		// clear any pending sources & buffers
		gviClearSourceList(data->m_playbackSources);

		// no longer playing
		data->m_playing = GVFalse;
	}

	// unlock the device
	gviUnlockDevice(data);
}

static GVBool gviHardwareIsDeviceStarted(GVDevice device, GVDeviceType type)
{
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;
	GVBool result = GVFalse;

	// get a lock on the device
	if(!gviLockDevice(data))
		return GVFalse;

	// figure out the result
	if(type == GV_PLAYBACK)
		result = data->m_playing;
	else if(type == GV_CAPTURE)
		result = data->m_capturing;
	else if (type == GV_CAPTURE_AND_PLAYBACK)
		result = (data->m_playing && data->m_capturing)?GVTrue:GVFalse;

	// release the device lock
	gviUnlockDevice(data);

	return result;
}

#if GVI_VOLUME_IN_SOFTWARE

	static void gviSetDeviceVolume(GVDevice device, GVBool isInput, GVScalar volume)
	{
		GVIHardwareData * data = (GVIHardwareData *)device->m_data;

		if(isInput)
			data->m_captureVolume = volume;
		else
			data->m_playbackVolume = volume;
	}

	static GVScalar gviGetDeviceVolume(GVDevice device, GVBool isInput)
	{
		GVIHardwareData * data = (GVIHardwareData *)device->m_data;

		if(isInput)
			return data->m_captureVolume;
		else
			return data->m_playbackVolume;
	}

#else

	static void gviSetDeviceVolume(GVDevice device, GVBool isInput, GVScalar volume)
	{
		Float32 vol = volume;
		int channel;

		// set the volume on all three channels
		// this covers mono and stereo devices
		for(channel = 0 ; channel < 3 ; channel++)
			AudioDeviceSetProperty(device->m_deviceID, NULL, channel, isInput, kAudioDevicePropertyVolumeScalar, sizeof(Float32), &vol);
	}

	static GVBool gviGetChannelVolume(GVDevice device, GVBool isInput, int channel, Float32 * volume)
	{
		OSStatus result;
		UInt32 size = sizeof(Float32);

		// get the volume for a specific channel
		result = AudioDeviceGetProperty(device->m_deviceID, channel, isInput, kAudioDevicePropertyVolumeScalar, &size, volume);

		return (result == noErr)?GVTrue:GVFalse;
	}

	static GVScalar gviGetDeviceVolume(GVDevice device, GVBool isInput)
	{
		GVBool result;
		Float32 channels[2];

		// check for mono
		result = gviGetChannelVolume(device, isInput, 0, &channels[0]);
		if(result)
			return (GVScalar)channels[0];

		// get left
		result = gviGetChannelVolume(device, isInput, 1, &channels[0]);
		if(!result)
			return 0;

		// get right
		result = gviGetChannelVolume(device, isInput, 2, &channels[1]);
		if(!result)
			return (GVScalar)channels[0];

		// return a mix of left and right
		return (GVScalar)((channels[0] + channels[1]) / 2);
	}

#endif

static void gviHardwareSetDeviceVolume(GVDevice device, GVDeviceType type, GVScalar volume)
{
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;

	// get a lock on the device
	if(!gviLockDevice(data))
		return;

	// set the volume
	if(type & GV_PLAYBACK)
		gviSetDeviceVolume(device, GVFalse, volume);
	if(type & GV_CAPTURE)
		gviSetDeviceVolume(device, GVTrue, volume);

	// release the device lock
	gviUnlockDevice(data);
}

static GVScalar gviHardwareGetDeviceVolume(GVDevice device, GVDeviceType type)
{
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;
	GVScalar volume = 0;

	// get a lock on the device
	if(!gviLockDevice(data))
		return 0;

	// get the volume
	if(type & GV_PLAYBACK)
		volume = gviGetDeviceVolume(device, GVFalse);
	else if(type & GV_CAPTURE)
		volume = gviGetDeviceVolume(device, GVTrue);

	// release the device lock
	gviUnlockDevice(data);

	return volume;
}

static void gviHardwareSetCaptureThreshold(GVDevice device, GVScalar threshold)
{
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;

	// get a lock on the device
	if(!gviLockDevice(data))
		return;

	// store the threshold
	data->m_captureThreshold = threshold;

	// release the device lock
	gviUnlockDevice(data);
}

static GVScalar gviHardwareGetCaptureThreshold(GVDevice device)
{
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;
	GVScalar threshold;

	// get a lock on the device
	if(!gviLockDevice(data))
		return GVFalse;

	// store the threshold
	threshold = data->m_captureThreshold;
	
	// release the device lock
	gviUnlockDevice(data);
	
	return threshold;
}

static GVBool gviHardwareCapturePacket(GVDevice device, GVByte * packet, int * len, GVFrameStamp * frameStamp, GVScalar * volume)
{
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;
	int numFrames;
	GVICapturedFrame * frame;
	GVScalar frameVolume;
	GVBool overThreshold;

	// get a lock on the device
	if(!gviLockDevice(data))
		return GVFalse;

	// calculate the number of frames we can put in the packet
	numFrames = (*len / GVIEncodedFrameSize);

	// clear the len and volume
	*len = 0;
	if(volume)
		*volume = 0;

	// don't do anything if we're not capturing
	if(!data->m_capturing)
	{
		gviUnlockDevice(data);
		return GVFalse;
	}

	// don't do anything if there isn't room for a frame
	if(numFrames < 1)
	{
		gviUnlockDevice(data);
		return GVFalse;
	}

	do
	{
		// get a frame
		frame = gviPopFirstFrame(&data->m_capturedFrames);
		if(!frame)
		{
			gviUnlockDevice(data);
			return GVFalse;
		}

		// calculate the volume
		frameVolume = gviGetSamplesVolume(frame->m_frame, GVISamplesPerFrame);

		// check against the threshold
		overThreshold = (frameVolume >= data->m_captureThreshold);
		if(overThreshold)
		{
			// update the time at which we crossed the threshold
			data->m_captureLastCrossedThresholdTime = frame->m_frameStamp;
		}
		else
		{
			// check if we are still within the hold time
			overThreshold = ((GVFrameStamp)(frame->m_frameStamp - data->m_captureLastCrossedThresholdTime) < GVI_HOLD_THRESHOLD_FRAMES);
		}

		// check if this should be captured
		if(overThreshold)
		{
			// scale the data
			if(data->m_captureVolume < 1.0)
			{
				int j;
				for(j = 0 ; j < GVISamplesPerFrame ; j++)
					frame->m_frame[j] = (GVSample)(frame->m_frame[j] * data->m_captureVolume);
			}

			// filter
			if(device->m_captureFilterCallback)
				device->m_captureFilterCallback(device, frame->m_frame, frame->m_frameStamp);

			// encode the frame into the packet
			gviEncode(packet, frame->m_frame);
			*len = GVIEncodedFrameSize;
			*frameStamp = frame->m_frameStamp;
			if(volume)
				*volume = frameVolume;
		}

		// put the frame back in the available list
		gviPushFirstFrame(&data->m_captureAvailableFrames, frame);
	}
	while(!overThreshold);
	
	// release the device lock
	gviUnlockDevice(data);

	return GVTrue;
}

static int gviHardwareGetAvailableCaptureBytes(GVDevice device)
{
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;
	GVICapturedFrame * frame;
	int count = 0;

	// don't do anything if we're not capturing
	if(!data->m_capturing)
		return 0;

	// count the number of captured frames
	for(frame = data->m_capturedFrames.m_next ; frame ; frame = frame->m_next)
		count++;

	// return the number of bytes
	return (count * GVIEncodedFrameSize);
}

static void gviHardwarePlayPacket(GVDevice device, const GVByte * packet, int len, GVSource source, GVFrameStamp frameStamp, GVBool mute)
{
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;

	// get a lock on the device
	if(!gviLockDevice(data))
		return;

	// check if we're not playing
	if(data->m_playing)
	{
		//add it
		gviAddPacketToSourceList(data->m_playbackSources, packet, len, source, frameStamp, mute, data->m_playbackClock);
	}

	// release the device lock
	gviUnlockDevice(data);
}

static GVBool gviHardwareIsSourceTalking(GVDevice device, GVSource source)
{
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;
	GVBool isTalking = GVFalse;
	
	// get a lock on the device
	if(!gviLockDevice(data))
		return GVFalse;

	// check if we're playing
	if(data->m_playing)
	{
		// check if the source is talking
		isTalking = gviIsSourceTalking(data->m_playbackSources, source);
	}
	
	// release the device lock
	gviUnlockDevice(data);
	
	return isTalking;
}

static int gviHardwareListTalkingSources(GVDevice device, GVSource sources[], int maxSources)
{
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;
	int numTalking = 0;

	// get a lock on the device
	if(!gviLockDevice(data))
		return 0;

	// check if we're not playing
	if(data->m_playing)
	{
		// list them
		numTalking = gviListTalkingSources(data->m_playbackSources, sources, maxSources);
	}
	
	// release the device lock
	gviUnlockDevice(data);
	
	return numTalking;
}

static int gviHardwareGetNumChannels(GVDevice device, GVDeviceType type)
{
	OSStatus result;
	UInt32 size;
	int numStreams;

	result = AudioDeviceGetPropertyInfo(device->m_deviceID, 0, (type & GV_CAPTURE)?true:false, kAudioDevicePropertyStreams, &size, NULL);
	if(result != noErr)
		return 1;

	numStreams = (size / sizeof(AudioStreamID));

	return numStreams;
}

static void gviHardwareGetChannelName(GVDevice device, GVDeviceType type, int index, gsi_char name[GV_CHANNEL_NAME_LEN])
{
	OSStatus result;
	UInt32 size;
	CFStringRef nameRef;
	AudioStreamID * streamIDs;
	int num;

	// clear the name in case we abort
	name[0] = '\0';

	// figure out how many channels there are
	num = gviHardwareGetNumChannels(device, type);

	// make sure the index we have is valid
	if((index < 0) || (index >= num))
		return;

	// allocate memory for the stream IDs
	size = (num * sizeof(AudioStreamID));
	streamIDs = (AudioStreamID*)gsimalloc(size);

	// get the stream IDs
	result = AudioDeviceGetProperty(device->m_deviceID, 0, (type & GV_CAPTURE)?true:false, kAudioDevicePropertyStreams, &size, streamIDs);
	if(result == noErr)
	{
		// get the CFString for this stream
		size = sizeof(CFStringRef);
		result = AudioStreamGetProperty(streamIDs[index], 0, kAudioDevicePropertyDeviceNameCFString, &size, &nameRef);
		if(result != noErr)
			nameRef = NULL;
	}

	// if there's a blank name, we'll supply our own
	if(nameRef && (CFStringGetLength(nameRef) <= 0))
	{
		CFRelease(nameRef);
		nameRef = NULL;
	}

	// if there's no name, give a default
	if(nameRef == NULL)
	{
		CFStringRef format;

		if(type & GV_CAPTURE)
			format = CFSTR("Input Channel %d");
		else
			format = CFSTR("Output Channel %d");

		nameRef = CFStringCreateWithFormat(NULL, NULL, format, index);

		CFRelease(format);
	}

	// convert it to an array of gsi_char
	gviCFStringToString(nameRef, name, GV_CHANNEL_NAME_LEN);

	// release the CFString
	CFRelease(nameRef);

	// free the streamIDs
	gsifree(streamIDs);
}

static void gviHardwareSetChannel(GVDevice device, GVDeviceType type, int index)
{
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;
	int num;
	
	num = gviHardwareGetNumChannels(device, type);

	if((index < 0) || (index >= num))
		return;

	if(type & GV_CAPTURE)
		data->m_captureChannel = index;
	else
		data->m_playbackChannel = index;
}

static int gviHardwareGetChannel(GVDevice device, GVDeviceType type)
{
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;

	if(type & GV_CAPTURE)
		return data->m_captureChannel;
	else
		return data->m_playbackChannel;
}

OSStatus gviPropertyListener(AudioDeviceID           inDevice,
                             UInt32                  inChannel,
                             Boolean                 isInput,
                             AudioDevicePropertyID   inPropertyID,
                             void*                   inClientData)
{
	GVIDevice * device = (GVIDevice*)inClientData;
	GVIHardwareData * data = (GVIHardwareData*)device->m_data;
	UInt32 value;
	UInt32 len = sizeof(UInt32);
	OSStatus result;

	result = AudioDeviceGetProperty(device->m_deviceID, 0, isInput, kAudioDevicePropertyDeviceIsAlive, &len, &value);
	if((result == noErr) && (value == 0))
	{
		// the device has been unplugged
		gviLockDevice(device->m_data);
		data->m_unplugged = GVTrue;
		gviUnlockDevice(device->m_data);
	}

	return (OSStatus)0;
}

static GVBool gviHardwareInitCapture(GVIDevice * device)
{
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;
	UInt32 size;
	OSStatus result;
	GVICapturedFrame * frame;
	int numCaptureBufferBytes;
	int numCaptureBufferFrames;
	int i;

	// get the capture format
	size = sizeof(AudioStreamBasicDescription);
	result = AudioDeviceGetProperty(device->m_deviceID, 0, true, kAudioDevicePropertyStreamFormat, &size, &data->m_captureStreamDescriptor);
	if(result != noErr)
		return GVFalse;

	// create a converter from the capture format to the GV format
	result = AudioConverterNew(&data->m_captureStreamDescriptor, &GVIVoiceFormat, &data->m_captureConverter);
	if(result != noErr)
		return GVFalse;

	// allocate a capture buffer
	data->m_captureBuffer = (GVSample *)gsimalloc(GVIBytesPerFrame);
	if(!data->m_captureBuffer)
	{
		AudioConverterDispose(data->m_captureConverter);
		return GVFalse;		
	}

	// allocate space for holding captured frames
	numCaptureBufferBytes = gviMultiplyByBytesPerMillisecond(GVI_CAPTURE_BUFFER_MILLISECONDS);
	numCaptureBufferBytes = gviRoundUpToNearestMultiple(numCaptureBufferBytes, GVIBytesPerFrame);
	numCaptureBufferFrames = (numCaptureBufferBytes / GVIBytesPerFrame);
	for(i = 0 ; i < numCaptureBufferFrames ; i++)
	{
		frame = (GVICapturedFrame *)gsimalloc(sizeof(GVICapturedFrame) + GVIBytesPerFrame - sizeof(GVSample));
		if(!frame)
		{
			gviFreeCapturedFrames(&data->m_captureAvailableFrames);
			gsifree(data->m_captureBuffer);
			AudioConverterDispose(data->m_captureConverter);
			return GVFalse;
		}
		gviPushFirstFrame(&data->m_captureAvailableFrames, frame);
	}

	// init the last crossed time
	data->m_captureLastCrossedThresholdTime = (data->m_captureClock - GVI_HOLD_THRESHOLD_FRAMES - 1);

	// add property listener
	AudioDeviceAddPropertyListener(device->m_deviceID, 0, true, kAudioDevicePropertyDeviceIsAlive, gviPropertyListener, device);

#if GVI_VOLUME_IN_SOFTWARE
	// init volume
	data->m_captureVolume = (GVScalar)1.0;
#endif

	return GVTrue;
}

static GVBool gviHardwareInitPlayback(GVIDevice * device)
{
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;
	UInt32 size;
	OSStatus result;
	UInt32 primeMethod;
	SInt32 channelMap[100];
	int i;

	// create the array of sources
	data->m_playbackSources = gviNewSourceList();
	if(!data->m_playbackSources)
		return GVFalse;

	// get the playback format
	size = sizeof(AudioStreamBasicDescription);
	result = AudioDeviceGetProperty(device->m_deviceID, 0, false, kAudioDevicePropertyStreamFormat, &size, &data->m_playbackStreamDescriptor);
	if(result != noErr)
	{
		gviFreeSourceList(data->m_playbackSources);
		return GVFalse;
	}

	// create a converter from the GV format to the playback format
	result = AudioConverterNew(&GVIVoiceFormat, &data->m_playbackStreamDescriptor, &data->m_playbackConverter);
	if(result != noErr)
	{
		gviFreeSourceList(data->m_playbackSources);
		return GVFalse;
	}

	// set it to do no priming
	primeMethod = kConverterPrimeMethod_None;
	result = AudioConverterSetProperty(data->m_playbackConverter, kAudioConverterPrimeMethod, sizeof(UInt32), &primeMethod);
	if(result != noErr)
	{
		AudioConverterDispose(data->m_playbackConverter);
		gviFreeSourceList(data->m_playbackSources);
		return GVFalse;
	}

	// setup the converter to map the input channel to all output channels
	result = AudioConverterGetPropertyInfo(data->m_playbackConverter, kAudioConverterChannelMap, &size, NULL);
	if(result == noErr)
	{
		result = AudioConverterGetProperty(data->m_playbackConverter, kAudioConverterChannelMap, &size, channelMap);
		if(result == noErr)
		{
			for(i = 0 ; i < (size / sizeof(SInt32)) ; i++)
				channelMap[i] = 0;

			AudioConverterSetProperty(data->m_playbackConverter, kAudioConverterChannelMap, size, channelMap);
		}
	}

	// allocate the playback buffer
	data->m_playbackBuffer = (GVSample *)gsimalloc(GVIBytesPerFrame);
	if(!data->m_playbackBuffer)
	{
		AudioConverterDispose(data->m_playbackConverter);
		gviFreeSourceList(data->m_playbackSources);
		return GVFalse;
	}

	// add property listener
	AudioDeviceAddPropertyListener(device->m_deviceID, 0, false, kAudioDevicePropertyDeviceIsAlive, gviPropertyListener, device);

#if GVI_VOLUME_IN_SOFTWARE
	// init volume
	data->m_playbackVolume = (GVScalar)1.0;
#endif

	return GVTrue;
}

static GVBool gviHardwareInitDevice(GVIDevice * device)
{
	GVIHardwareData * data = (GVIHardwareData *)device->m_data;
	int result;

	// init the mutex
	result = pthread_mutex_init(&data->m_mutex, NULL);
	if(result != 0)
		return GVFalse;

	// handle playback specific stuff
	if(device->m_types & GV_PLAYBACK)
	{
		if(!gviHardwareInitPlayback(device))
		{
			pthread_mutex_destroy(&data->m_mutex);
			return GVFalse;
		}
	}

	// handle capture specific stuff
	if(device->m_types & GV_CAPTURE)
	{
		if(!gviHardwareInitCapture(device))
		{
			pthread_mutex_destroy(&data->m_mutex);
			gviCleanupPlayback(data);
			return GVFalse;
		}
	}

	return GVTrue;
}

GVDevice gviHardwareNewDevice(GVDeviceID deviceID, GVDeviceType type)
{
	GVIDevice * device;
	GVBool result;

	// check for no type
	if(!(type & GV_CAPTURE_AND_PLAYBACK))
		return NULL;

	// create a new device
	device = gviNewDevice(deviceID, GVHardwareMacOSX, type, sizeof(GVIHardwareData));
	if(!device)
		return NULL;

	// init the device
	result = gviHardwareInitDevice(device);
	if(result == GVFalse)
	{
		gviFreeDevice(device);
		return NULL;
	}

	// store the pointers
	device->m_methods.m_freeDevice = gviHardwareFreeDevice;
	device->m_methods.m_startDevice = gviHardwareStartDevice;
	device->m_methods.m_stopDevice = gviHardwareStopDevice;
	device->m_methods.m_isDeviceStarted = gviHardwareIsDeviceStarted;
	device->m_methods.m_setDeviceVolume = gviHardwareSetDeviceVolume;
	device->m_methods.m_getDeviceVolume = gviHardwareGetDeviceVolume;
	device->m_methods.m_setCaptureThreshold = gviHardwareSetCaptureThreshold;
	device->m_methods.m_getCaptureThreshold = gviHardwareGetCaptureThreshold;
	device->m_methods.m_getAvailableCaptureBytes = gviHardwareGetAvailableCaptureBytes;
	device->m_methods.m_capturePacket = gviHardwareCapturePacket;
	device->m_methods.m_playPacket = gviHardwarePlayPacket;
	device->m_methods.m_isSourceTalking = gviHardwareIsSourceTalking;
	device->m_methods.m_listTalkingSources = gviHardwareListTalkingSources;
	device->m_methods.m_getNumChannels = gviHardwareGetNumChannels;
	device->m_methods.m_getChannelName = gviHardwareGetChannelName;
	device->m_methods.m_setChannel= gviHardwareSetChannel;
	device->m_methods.m_getChannel = gviHardwareGetChannel;

	// add it to the list
	gviAppendDeviceToList(GVIDevices, device);

	return device;
}