File: gvPS3Headset.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 (1141 lines) | stat: -rw-r--r-- 33,499 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
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#include "gvPS3Headset.h"
#if !defined(GV_NO_PS3_HEADSET)
#include "gvDevice.h"
#include "gvCodec.h"
#include "gvSource.h"
#include "gvUtil.h"
#include <types.h>
#include <sys/event.h>
#include <cell/audio.h>
#include <cell/mic.h>
#include <cell/sysmodule.h>
#include <cell/mixer.h>


#if !defined(_PS3)
#error This file should only be used with the PlayStation3
#endif

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// definitions
#define GVI_PLAYBACK_STAYAHEAD_MILLISECONDS  50

// Used as a starting value for the audio port queue key
#define GVI_AUDIO_QUEUE_KEY_BASE	0x0000998877660000ULL

// Used as a starting value for the capture event queue key
#define GVI_CAPTURE_QUEUE_KEY_BASE  0x0000000072110700UL 

#define GVI_AUDIO_QUEUE_DEPTH		4
#define GVI_CAPTURE_VOLUME_MAX      241
#define GVI_PLAYBACK_NUM_BLOCKS     CELL_AUDIO_BLOCK_32
#define GVI_PLAYBACK_BLOCK_SAMPLES  CELL_AUDIO_BLOCK_SAMPLES
#define GVI_PLAYBACK_SAMPLE_RATE    48000 //Hz
#define GVI_PS3_MIC_BUFFER_MS       1000
#define GVI_LOCAL_TALK_MAX          10
#define GVI_PLAYBACK_SAMPLE_FACTOR  (GVI_PLAYBACK_SAMPLE_RATE / gviGetSampleRate())
#define GVI_NUM_CHANNELS            CELL_AUDIO_PORT_2CH


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// structs
typedef struct
{
	sys_event_t         m_captureCallbackEvent;
	sys_event_queue_t   m_captureCallbackQueue;
	uint64_t            m_captureEventQueueKey;
	GVBool              m_capturing;
	GVScalar            m_captureVolume;
	GVFrameStamp        m_captureClock;
	GVScalar            m_captureThreshold;
	GVFrameStamp        m_captureLastCrossedThresholdTime;
	float              *m_capturePreConvertBuffer;
	size_t              m_capturePreConvertBufferLen;
	GVSample           *m_captureBuffer;

	int                 m_captureBufferBytes;
	int                 m_deviceNum; // used to keep the microphone device number
	GVBool              m_captureMicOpen;

	GVBool              m_playing;
	GVScalar            m_playbackVolume;
	GVFrameStamp        m_playbackClock;
	GVISourceList       m_playbackSources;
	GVSample           *m_playbackBuffer;
	gsi_u32             m_playbackCellAudioPortNum;
	CellAudioPortConfig m_playbackCellAudioConfig;
	sys_event_queue_t   m_playbackQueue;
	uint64_t            m_playbackQueueKey;
	int                 m_playbackPortPos;
} GVIPS3HeadsetData;



///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// globals
static GVIDeviceList GVIDevices;

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// gets the device by the deviceID
static GVIDevice * gviFindDeviceByID(GVDeviceID deviceID)
{
	GVIDevice * device;
	int num;
	int i;

	num = gviGetNumDevices(GVIDevices);
	for(i = 0 ; i < num ; i++)
	{
		device = gviGetDevice(GVIDevices, i);
		if(device->m_deviceID == deviceID)
			return device;
	}

	return NULL;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// frees the device
static void gviFreeArrayDevice(void * elem)
{
	GS_ASSERT(elem);
	GVIDevice * device = *(GVIDevice **)elem;
	GVIPS3HeadsetData * data = (GVIPS3HeadsetData *)device->m_data;
	int result;

	// turn off mic if its a capture device and if the mic port is open
	if (data->m_capturing && cellMicIsOpen(data->m_deviceNum))
		result = cellMicClose(data->m_deviceNum);

	// Destroy the callback and playback queues
	if (data->m_playbackQueue)
		cellAudioRemoveNotifyEventQueue(data->m_playbackQueueKey);
	if (data->m_captureCallbackQueue)
		cellMicRemoveNotifyEventQueue(data->m_captureEventQueueKey);
	if (data->m_playbackQueue)
		sys_event_queue_destroy(data->m_playbackQueue, 0);
	if (data->m_captureCallbackQueue)
		sys_event_queue_destroy(data->m_captureCallbackQueue, 0);

	// close audio port
	if (data->m_playing)
		cellAudioPortClose(data->m_playbackCellAudioPortNum);

	// playback specific cleanup
	if(device->m_types & GV_PLAYBACK)
	{
		if(data->m_playbackSources)
			gviFreeSourceList(data->m_playbackSources);
		gsifree(data->m_playbackBuffer);
	}

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

	// free the device
	gviFreeDevice(device);
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// starts up the headset
GVBool gviPS3HeadsetStartup(void)
{
	int result;

	// create the array of devices
	GVIDevices = gviNewDeviceList(gviFreeArrayDevice);
	if(!GVIDevices)
		return GVFalse;

	// initialize the mic library
	result = cellSysmoduleLoadModule(CELL_SYSMODULE_MIC);
	if(result != CELL_OK)
	{
		gviFreeDeviceList(GVIDevices);
		GVIDevices = NULL;
		return GVFalse;
	}

	result = cellMicInit();
	if(result != CELL_OK)
	{
		gviFreeDeviceList(GVIDevices);
		GVIDevices = NULL;
		return GVFalse;
	}

	// initialize the audio library
	result = cellSysmoduleLoadModule(CELL_SYSMODULE_AUDIO);
	if(result != CELL_OK)
	{
		gviFreeDeviceList(GVIDevices);
		GVIDevices = NULL;
		return GVFalse;
	}

	result = cellAudioInit();
	if(result != CELL_OK && result != CELL_AUDIO_ERROR_ALREADY_INIT)
	{
		gviFreeDeviceList(GVIDevices);
		cellMicEnd();
		GVIDevices = NULL;
		return GVFalse;
	}

	return GVTrue;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// unloads the headset
void gviPS3HeadsetCleanup(void)
{
	// free the device array
	if(GVIDevices)
	{
		gviFreeDeviceList(GVIDevices);
		GVIDevices = NULL;
	}

	cellMicEnd();
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// process playback if there is any in the queue
static GVBool gviPlaybackDeviceThink(GVIDevice * device)
{
	GVIPS3HeadsetData * data = (GVIPS3HeadsetData *)device->m_data;
	int remainingSamples;
	int numFrames;
	GVBool wroteToBuffer;
	int i, j, k;
	sys_event_t playbackQueueEvent;
	int result;
	int playbackReadPos;
	unsigned int currentBlock;
	int totalSamples;

	// don't do anything if we're not playing
	if(!data->m_playing)
		return GVTrue;

	// check the queue
	result = sys_event_queue_receive(data->m_playbackQueue, &playbackQueueEvent, 1);
	if(result == ETIMEDOUT)
	{
		return GVTrue;
	}
	if(result != CELL_OK)
	{
		return GVFalse;
	}

	totalSamples = (GVI_PLAYBACK_NUM_BLOCKS * GVI_PLAYBACK_BLOCK_SAMPLES);

	currentBlock = (unsigned int)*(uint64_t *)(data->m_playbackCellAudioConfig.readIndexAddr);
	playbackReadPos = (currentBlock * GVI_PLAYBACK_BLOCK_SAMPLES);

	if(data->m_playbackPortPos == -1)
	{
		unsigned int nextBlock = (currentBlock + 1) % GVI_PLAYBACK_NUM_BLOCKS; // write target is next block
		data->m_playbackPortPos = (nextBlock * GVI_PLAYBACK_BLOCK_SAMPLES);
	}

	remainingSamples = (((playbackReadPos + totalSamples) - data->m_playbackPortPos) % totalSamples);

	// figure out the number of frames that we can write
	numFrames = ((remainingSamples / GVI_PLAYBACK_SAMPLE_FACTOR) / GVISamplesPerFrame);

	// write the frames
	for(i = 0 ; i < numFrames ; i++)
	{
		// write a frame of sources to our buffer
		wroteToBuffer = gviWriteSourcesToBuffer(data->m_playbackSources, data->m_playbackClock, data->m_playbackBuffer, 1);

		// clear it if nothing was written
		if(!wroteToBuffer)
			memset(data->m_playbackBuffer, 0, (unsigned int)GVIBytesPerFrame);

		// filter
		if(device->m_playbackFilterCallback)
			device->m_playbackFilterCallback(device, data->m_playbackBuffer, data->m_playbackClock);
		
		// write to port buffer from m_playbackBuffer
		// converting from sample to float
		// converting from 8khz or 16KHz to 48khz and mono to stereo (write each sample 6-12 
		// times depending on sample rate)
		for(j = 0 ; j < GVISamplesPerFrame ; j++)
		{
			float sample = ((float)data->m_playbackBuffer[j] / (float)SHRT_MAX);
			for(k = 0; k < GVI_PLAYBACK_SAMPLE_FACTOR; k++)
			{
				float *dest = (float *)(data->m_playbackCellAudioConfig.portAddr +
					(data->m_playbackPortPos * GVI_NUM_CHANNELS * sizeof(float)));
				*dest++ = sample;
				*dest = sample;
				data->m_playbackPortPos++;
				data->m_playbackPortPos %= totalSamples;
			}
		}

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

	return GVTrue;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// called every once in a while to process playback
void gviPS3HeadsetThink(void)
{
	GVIDevice * device;
	GVBool rcode;
	int num;
	int i;

	if(!GVIDevices)
		return;

	// loop through the devices backwards to 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);

		// // check if playback is setup on the device
		if(device->m_types & GV_PLAYBACK)
		{
			// let it think
			rcode = gviPlaybackDeviceThink(device);

			// check if the device was unplugged
			if(!rcode)
				gviDeviceUnplugged(device);
		}
	}
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// gets the devices detected
int gviPS3HeadsetListDevices(GVDeviceInfo devices[], int maxDevices, GVDeviceType types)
{
	int index;
	int numDevices = 0;

	maxDevices = min(maxDevices, CELL_MAX_MICS);

	for(index = 0 ; index < maxDevices ; index++)
	{
		if(cellMicIsAttached(index))
		{
			int deviceType;
			if (cellMicGetType(index, &deviceType) == CELL_OK)
			{
				if (deviceType == CELLMIC_TYPE_USBAUDIO)
				{
					devices[numDevices].m_id = index;
					devices[numDevices].m_deviceType = GV_CAPTURE | GV_PLAYBACK;
					devices[numDevices].m_defaultDevice = (GVDeviceType)0; 
					_tcscpy(devices[numDevices].m_name, _T("USB Headset"));
					devices[numDevices].m_hardwareType = GVHardwarePS3Headset;
				}
				else if (deviceType == CELLMIC_TYPE_BLUETOOTH)
				{
					devices[numDevices].m_id = index;
					devices[numDevices].m_deviceType = GV_CAPTURE | GV_PLAYBACK;
					devices[numDevices].m_defaultDevice = (GVDeviceType)0; 
					_tcscpy(devices[numDevices].m_name, _T("Bluetooth Headset"));
					devices[numDevices].m_hardwareType = GVHardwarePS3Headset;
				}
			}

			numDevices++;
		}
	}
	return numDevices;
}

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

static GVBool gviPS3HeadsetInitHeadphone(GVIPS3HeadsetData *data)
{
	int result;
	CellAudioPortParam  audioParam;
	sys_event_queue_attribute_t	aQueueAttr;
	int aCount;	

	audioParam.attr = CELL_AUDIO_PORTATTR_OUT_SECONDARY;
	audioParam.nBlock = GVI_PLAYBACK_NUM_BLOCKS;
	audioParam.nChannel = GVI_NUM_CHANNELS;

	// set the audio port value to something really abnormal
	// so that if the open function fails, the gviPS3HeadsetClose
	// will close the port only if a valid port number is assigned
	data->m_playbackCellAudioPortNum = 0xFFFFFFFF;

	// Playback
	///////////

	result = cellAudioPortOpen(&audioParam, &data->m_playbackCellAudioPortNum);
	if (result != CELL_OK)
	{	
		return GVFalse;
	}

	// get the config for the audio port so we can use it to write data to the audio port ring buffer
	result = cellAudioGetPortConfig(data->m_playbackCellAudioPortNum, &data->m_playbackCellAudioConfig);
	if (result != CELL_OK)
	{
		return GVFalse;
	}

	// Event queue notify tells us when the system is ready to play new data
	aCount = 0;
	sys_event_queue_attribute_initialize(aQueueAttr);
	aQueueAttr.attr_protocol  = SYS_SYNC_FIFO;
	data->m_playbackQueueKey = GVI_AUDIO_QUEUE_KEY_BASE;

	while (aCount < 10)
	{
		result = sys_event_queue_create(&data->m_playbackQueue, &aQueueAttr, 
			data->m_playbackQueueKey, GVI_AUDIO_QUEUE_DEPTH);
		if (result == CELL_OK)
		{
			break;
		}
		// search unused key
		data->m_playbackQueueKey = GVI_AUDIO_QUEUE_KEY_BASE | (rand() & 0x0ffff);
		aCount++;
	}

	if (result != CELL_OK)
	{
		return GVFalse;
	}

	// register event queue to libaudio
	result = cellAudioSetNotifyEventQueue(data->m_playbackQueueKey);
	if (result < 0)
	{
		return GVFalse;
	}

	return GVTrue;
}

GVBool gviPS3HeadsetInitMic(GVIPS3HeadsetData *data)
{
	int result = 0;
	int aMsg = 0;
	int aDevNum=0;

	int aCount = 0; 
	sys_event_queue_attribute_t  equeue_attr = {SYS_SYNC_FIFO, SYS_PPU_QUEUE, ""};

	// Event queue key for libmic
	// checks for an event occurrence 
	//create event queue to recv "MicIn" callback from MIOS
	data->m_captureEventQueueKey = GVI_CAPTURE_QUEUE_KEY_BASE;

	while ( (aCount++) < 100 )
	{
		result = sys_event_queue_create(&data->m_captureCallbackQueue, 
			&equeue_attr, data->m_captureEventQueueKey, 32);
		if (result == CELL_OK) break;
		data->m_captureEventQueueKey =  GVI_CAPTURE_QUEUE_KEY_BASE | ( rand() & 0xffff);
	}
	if (result != CELL_OK)
	{		
		return GVFalse;
	}

	//install "MicIn" system-callback(with devnum == -1) to recv attach/detach event
	result = cellMicSetNotifyEventQueue(data->m_captureEventQueueKey);
	if (result != CELL_OK)
	{
		return GVFalse;
	}

	// Wait up to 10 ms before checking if the mike is attached
	result = sys_event_queue_receive(data->m_captureCallbackQueue, 
		&data->m_captureCallbackEvent, 10000);
	if(result == ETIMEDOUT)
		return GVFalse;

	aMsg = (int)data->m_captureCallbackEvent.data1;
	aDevNum = (int)data->m_captureCallbackEvent.data2;

	if (aMsg == CELLMIC_ATTACH && aDevNum == data->m_deviceNum)
	{
		// start with the default audio device
		result = cellMicOpenEx(data->m_deviceNum, GVI_PLAYBACK_SAMPLE_RATE, 1, 
			GV_SAMPLES_PER_SECOND, GVI_PS3_MIC_BUFFER_MS, CELLMIC_SIGTYPE_DSP);

		if (result != CELL_OK)
		{
			return GVFalse;
		}
		data->m_captureMicOpen = GVTrue;
	}
	return GVTrue;
}

void gviPS3HeadsetCloseHeadphone(GVIPS3HeadsetData *data)
{
	// Remove and Destroy the callback and playback queues
	if (data->m_playbackQueue)
		cellAudioRemoveNotifyEventQueue(data->m_playbackQueue);
	if (data->m_playbackQueue)
		sys_event_queue_destroy(data->m_playbackQueue, 0);

	// the audio port needs to be closed if a device wasn't initialized properly
	if (data->m_playbackCellAudioPortNum != 0xFFFFFFFF)
		cellAudioPortClose(data->m_playbackCellAudioPortNum);
}

void gviPS3HeadsetCloseMic(GVIPS3HeadsetData *data)
{
	// Remove and Destroy the callback and playback queues
	if (data->m_captureCallbackQueue)
		cellAudioRemoveNotifyEventQueue(data->m_captureCallbackQueue);
	if (data->m_captureCallbackQueue)
		sys_event_queue_destroy(data->m_captureCallbackQueue, 0);

	// The mic should be closed if it is open 
	if (cellMicIsOpen(data->m_deviceNum))
		cellMicClose(data->m_deviceNum);
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// initializes the device for playback
static GVBool gviStartPlaybackDevice(GVIPS3HeadsetData * data)
{
	int result;

	sys_event_queue_drain(data->m_playbackQueue);

	data->m_playbackPortPos = -1;

	result = cellAudioPortStart(data->m_playbackCellAudioPortNum);
	if (result != CELL_OK)
	{
		return GVFalse;
	}

	// clear the clock
	data->m_playbackClock = 0;

	// started playing
	data->m_playing = GVTrue;

	return GVTrue;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// initializes the device for capture
static GVBool gviStartCaptureDevice(GVIPS3HeadsetData * data)
{
	int result;
	// start the mic capture
	if (data->m_captureMicOpen)
	{		
		result = cellMicStart(data->m_deviceNum);
		if (result != CELL_OK)
			return GVFalse;
	}
	else 
		return GVFalse;

	cellMicReset(data->m_deviceNum);
	// no data in the capture buffer
	data->m_captureBufferBytes = 0;
	data->m_capturePreConvertBufferLen = 0;

	// started capturing
	data->m_capturing = GVTrue;

	return GVTrue;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// stops the device
static void gviPS3HeadsetStopDevice(GVIDevice * device, GVDeviceType type)
{
	GVIPS3HeadsetData * data = (GVIPS3HeadsetData *)device->m_data;

	if(type & GV_PLAYBACK)
	{
		cellAudioPortStop(data->m_playbackCellAudioPortNum);

		// clear the playback buffer
		memset(data->m_playbackBuffer, 0, GVIBytesPerFrame);

		// stopped playing
		data->m_playing = GVFalse;

		// clear any pending sources & buffers
		gviClearSourceList(data->m_playbackSources);
	}
	if(type & GV_CAPTURE)
	{
		// stop the capture buffer
		cellMicStop(data->m_deviceNum);

		// clear capture buffer
		memset(data->m_captureBuffer, 0, GVIBytesPerFrame);

		// stopped capturing
		data->m_capturing = GVFalse;

		// so a stop then start isn't continuous
		data->m_captureClock++;
	}

}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// starts the device
static GVBool gviPS3HeadsetStartDevice(GVIDevice * device, GVDeviceType type)
{
	GVIPS3HeadsetData * data = (GVIPS3HeadsetData *)device->m_data;

	if(type == GV_PLAYBACK)
	{
		return gviStartPlaybackDevice(data);
	}
	if(type == GV_CAPTURE)
	{
		return gviStartCaptureDevice(data);
	}
	if(type == GV_CAPTURE_AND_PLAYBACK)
	{
		if(!gviStartPlaybackDevice(data))
			return GVFalse;
		if(!gviStartCaptureDevice(data))
		{
			gviPS3HeadsetStopDevice(device, GV_PLAYBACK);
			return GVFalse;
		}
		return GVTrue;
	}
	return GVFalse;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// checks to see if the device is ready
static GVBool gviPS3HeadsetIsDeviceStarted(GVIDevice * device, GVDeviceType type)
{
	// NULL device means not even created or started
	GS_ASSERT(device);
	if (!device)
		return GVFalse;

	GVIPS3HeadsetData * data = (GVIPS3HeadsetData *)device->m_data;

	if(type == GV_PLAYBACK)
		return data->m_playing;
	if(type == GV_CAPTURE)
		return data->m_capturing;
	if(type == GV_CAPTURE_AND_PLAYBACK)
		return (data->m_playing && data->m_capturing);
	return GVFalse;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// sets the volume for the device
static void gviPS3HeadsetSetDeviceVolume(GVIDevice * device, GVDeviceType type, GVScalar volume)
{
	GVIPS3HeadsetData * data = (GVIPS3HeadsetData *)device->m_data;

	if(type & GV_PLAYBACK)
	{
		cellAudioSetPortLevel(data->m_playbackCellAudioPortNum, volume);
		data->m_playbackVolume = volume;
	}
	if(type & GV_CAPTURE)
	{
		cellMicSetDeviceAttr(data->m_deviceNum, CELLMIC_DEVATTR_VOLUME, (int)(volume * GVI_CAPTURE_VOLUME_MAX), 0);
		data->m_captureVolume = volume;
	}
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// gets the device volume
static GVScalar gviPS3HeadsetGetDeviceVolume(GVIDevice * device, GVDeviceType type)
{
	GVIPS3HeadsetData * data = (GVIPS3HeadsetData *)device->m_data;

	if(type & GV_PLAYBACK)
		return data->m_playbackVolume;
	return data->m_captureVolume;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// sets the capture threshold
static void gviPS3HeadsetSetCaptureThreshold(GVIDevice * device, GVScalar threshold)
{
	GVIPS3HeadsetData * data = (GVIPS3HeadsetData *)device->m_data;
	data->m_captureThreshold = threshold;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// gets the capture threshold
static GVScalar gviPS3HeadsetGetCaptureThreshold(GVIDevice * device)
{
	GVIPS3HeadsetData * data = (GVIPS3HeadsetData *)device->m_data;
	return data->m_captureThreshold;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// gets the available capture bytes
static int gviPS3HeadsetGetAvailableCaptureBytes(GVDevice device)
{
	GVIPS3HeadsetData * data = (GVIPS3HeadsetData *)device->m_data;

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

	// no call listed in the Sony documentation, so just return 1
	return 1;
}

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//
GVBool gviPS3HeadsetHandleMicAttach(GVIPS3HeadsetData *data)
{
	int result;

	// start with the default audio device
	result = cellMicOpenEx(data->m_deviceNum, GVI_PLAYBACK_SAMPLE_RATE, 1, 
		GV_SAMPLES_PER_SECOND, GVI_PS3_MIC_BUFFER_MS, CELLMIC_SIGTYPE_DSP);

	if (result != CELL_OK)
	{
		return GVFalse;
	}
	data->m_captureMicOpen = GVTrue;
	result = cellMicStart(data->m_deviceNum);
	if (result != CELL_OK)
	{
		return GVFalse;
	}
	result = cellMicReset(data->m_deviceNum);
	if (result != CELL_OK)
	{
		return GVFalse;
	}
	return GVTrue;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// processes the captured frame
static void gviProcessCapturedFrame(GVDevice device, GVSample *frameIn, GVByte* frameOut, GVScalar *volume, GVBool *threshold)
{
	GVIPS3HeadsetData * data = (GVIPS3HeadsetData *)device->m_data;
	GVScalar frameVolume;

	// get the volume if requested
	if(volume)
	{
		frameVolume = gviGetSamplesVolume(frameIn, GVISamplesPerFrame);
		if(frameVolume > *volume)
			*volume = frameVolume;
	}

	// check against the threshold
	if(threshold && !*threshold)
	{
		if(volume)
		{
			// we already got the volume, so use that to check
			*threshold = (*volume >= data->m_captureThreshold);
		}
		else
		{
			// we didn't get a volume, so check the samples directly
			*threshold = gviIsOverThreshold(frameIn, GVISamplesPerFrame, data->m_captureThreshold);
		}
	}

	// filter
	if(device->m_captureFilterCallback)
		device->m_captureFilterCallback(device, frameIn, data->m_captureClock);

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

	// encode the buffer into the packet
	gviEncode(frameOut, frameIn);	
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// captures a packet
static GVBool gviPS3HeadsetCapturePacket(GVDevice device, GVByte * packet, int * len, GVFrameStamp * frameStamp, GVScalar * volume)
{
	GVIPS3HeadsetData * data = (GVIPS3HeadsetData *)device->m_data;
	GVBool overThreshold;
	int result;
	int numFrames;
	int readSize;
	int lenAvailable;
	int framesAvailable;
	GVByte * frameOut;
	float *frameIn;
	int aCaptureQueueMsg;
	int aDeviceIndex;
	int sample;
	int localtalk;
	// figure out how many encoded bytes they can handle
	lenAvailable = *len;

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

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

	// set the frameStamp
	*frameStamp = data->m_captureClock;

	// figure out how many frames they can handle
	framesAvailable = (lenAvailable / GVIEncodedFrameSize);
	overThreshold = GVFalse;

	frameOut = packet;
	//frameIn = data->m_capturePreConvertBuffer;
	// handle the data one frame at a time
	for(numFrames = 0 ; numFrames < framesAvailable ; numFrames++)
	{
		// Wait up to 1 us before checking if the mike is attached
		result = sys_event_queue_receive(data->m_captureCallbackQueue, 
			&data->m_captureCallbackEvent, 1);
		if(result == ETIMEDOUT)
		{
			break;
		}	

		aCaptureQueueMsg = (int)data->m_captureCallbackEvent.data1;
		aDeviceIndex = (int)data->m_captureCallbackEvent.data2;
		if (aDeviceIndex != data->m_deviceNum)
			continue;

		if (aCaptureQueueMsg == CELLMIC_ATTACH)
		{
			if (!gviPS3HeadsetHandleMicAttach(data))
			{
				break;
			}
		}	
		else if (aCaptureQueueMsg == CELLMIC_DETACH)
		{
			gviDeviceUnplugged(device);
			return GVFalse;
		}
		else if (aCaptureQueueMsg == CELLMIC_DATA)
		{
			// read this frame
			//readSize = (GVIBytesPerFrame - data->m_captureBufferBytes);
			readSize = ((GVISamplesPerFrame - data->m_capturePreConvertBufferLen) * sizeof(float));
			frameIn = data->m_capturePreConvertBuffer + data->m_capturePreConvertBufferLen;
			readSize = cellMicRead(data->m_deviceNum, frameIn, readSize);

			cellMicGetSignalState(0, CELLMIC_SIGSTATE_LOCTALK, &localtalk);
			if (localtalk < (int)(GVI_LOCAL_TALK_MAX * data->m_captureThreshold))
			{
				return GVFalse;
			}

			if (readSize == CELL_MICIN_ERROR_DEVICE_NOT_FOUND)
			{
				gviDeviceUnplugged(device);
				return GVFalse;
			}

			data->m_capturePreConvertBufferLen += (readSize / sizeof(float));
			if(data->m_capturePreConvertBufferLen < GVISamplesPerFrame)
			{
				break;
			}

			// convert the data from 32 bit Big Endian Floats [-1.0,1.0]
			// to 16 bit short and write the values into the buffer
			for (sample = 0; sample < GVISamplesPerFrame; sample++)
			{
				data->m_captureBuffer[sample] = (GVSample)((SHRT_MAX)*data->m_capturePreConvertBuffer[sample]); 
			}

			// process the frame
			gviProcessCapturedFrame(device, data->m_captureBuffer, frameOut, volume, &overThreshold);

			// we got a full frame, so there's no leftover
			data->m_captureBufferBytes = 0;

			// update the frame pointer
			frameOut += GVIEncodedFrameSize;

			data->m_capturePreConvertBufferLen = 0;
		}
	}

	// check if this packet crossed the threshold
	if(overThreshold)
	{
		// store the time we crossed it
		data->m_captureLastCrossedThresholdTime = data->m_captureClock;
	}
	else
	{
		// check if we are still on the overhang from a previous crossing
		overThreshold = ((GVFrameStamp)(*frameStamp - data->m_captureLastCrossedThresholdTime) < GVI_HOLD_THRESHOLD_FRAMES);
	}

	// set the len
	*len = (numFrames * GVIEncodedFrameSize);

	// return false if we didn't get a packet
	if(!overThreshold || (*len == 0))
		return GVFalse;

	return GVTrue;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// sends the packet to the mixer
static void gviPS3HeadsetPlayPacket(GVIDevice * device, const GVByte * packet, int len, GVSource source, GVFrameStamp frameStamp, GVBool mute)
{
	GVIPS3HeadsetData * data = (GVIPS3HeadsetData *)device->m_data;

	// don't do anything if we're not playing
	if(!data->m_playing)
		return;

	//add it
	gviAddPacketToSourceList(data->m_playbackSources, packet, len, source, frameStamp, mute, data->m_playbackClock);
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// checks to see if we're talking
static GVBool gviPS3HeadsetIsSourceTalking(GVDevice device, GVSource source)
{
	GVIPS3HeadsetData * data = (GVIPS3HeadsetData *)device->m_data;

	// don't do anything if we're not playing
	if(!data->m_playing)
		return GVFalse;

	return gviIsSourceTalking(data->m_playbackSources, source);
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// lists the talking sources
static int gviPS3HeadsetListTalkingSources(GVDevice device, GVSource sources[], int maxSources)
{
	GVIPS3HeadsetData * data = (GVIPS3HeadsetData *)device->m_data;

	// don't do anything if we're not playing
	if(!data->m_playing)
		return GVFalse;

	return gviListTalkingSources(data->m_playbackSources, sources, maxSources);
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// initializes the device
static GVBool gviPS3HeadsetInitDevice(GVIDevice * device, int deviceIndex, GVDeviceType type)
{
	GVIPS3HeadsetData * data;

	// get a pointer to the data
	data = (GVIPS3HeadsetData *)device->m_data;
	data->m_deviceNum = deviceIndex;

	// handle playback specific stuff
	if(type & GV_PLAYBACK)
	{
		// create the array of sources
		data->m_playbackSources = gviNewSourceList();
		if(!data->m_playbackSources)
		{
			return NULL;
		}

		// allocate the buffer to hold one frame
		data->m_playbackBuffer = (GVSample *)gsimemalign(16, (unsigned int)(GVIBytesPerFrame));
		if(!data->m_playbackBuffer)
		{
			gviFreeSourceList(data->m_playbackSources);
			return NULL;
		}
		if (!gviPS3HeadsetInitHeadphone(data))
		{
			gviFreeSourceList(data->m_playbackSources);
			gsifree(data->m_playbackBuffer);
			gviPS3HeadsetCloseHeadphone(data);
			return NULL;
		}
		data->m_playbackVolume = 1.0;
	}

	// handle capture specific stuff
	if(type & GV_CAPTURE)
	{
		// set some data vars
		data->m_captureClock = 0;
		data->m_captureVolume = 1.0;
		data->m_capturePreConvertBufferLen = 0;

		data->m_captureLastCrossedThresholdTime = (GVFrameStamp)(data->m_captureClock - GVI_HOLD_THRESHOLD_FRAMES - 1);

		// allocate the buffer
		data->m_captureBuffer = (GVSample *)gsimemalign(16, (unsigned int)(GVIBytesPerFrame));
		if(!data->m_captureBuffer)
		{
			// Need to free any resources in data for playback
			// Also library needs to close audio port and mic
			// The library still needs to remain
			if(type & GV_PLAYBACK)
			{
				gviFreeSourceList(data->m_playbackSources);
				gsifree(data->m_playbackBuffer);
				gviPS3HeadsetCloseHeadphone(data);
			}
			return NULL;
		}

		data->m_capturePreConvertBuffer = (float *)gsimemalign(16,(unsigned int)(GVISamplesPerFrame * sizeof(float)));
		if(!data->m_capturePreConvertBuffer)
		{
			// Need to free any resources in data for playback
			// Also library needs to close audio port and mic
			// The library still needs to remain
			if(type & GV_PLAYBACK)
			{
				gviFreeSourceList(data->m_playbackSources);
				gsifree(data->m_playbackBuffer);
				gviPS3HeadsetCloseHeadphone(data);
			}

			// Still need to free capture buffer
			gsifree(data->m_captureBuffer);
			return NULL;
		}

		if (!gviPS3HeadsetInitMic(data))
		{
			// Need to free any resources in data for playback
			// Also library needs to close audio port and mic
			// The library still needs to remain
			if(type & GV_PLAYBACK)
			{
				gviFreeSourceList(data->m_playbackSources);
				gsifree(data->m_playbackBuffer);
				gviPS3HeadsetCloseHeadphone(data);
			}
			gviPS3HeadsetCloseMic(data);
			gsifree(data->m_captureBuffer);
			gsifree(data->m_capturePreConvertBuffer);
			return NULL;
		}
	}

	return GVTrue;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// initializes a new device
GVDevice gviPS3HeadsetNewDevice(GVDeviceID deviceID, GVDeviceType type)
{
	GVIDevice * device;
	GVBool result;

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

	// check if the device already exists
	if(gviFindDeviceByID(deviceID))
		return NULL;

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

	// init the device
	result = gviPS3HeadsetInitDevice(device, deviceID, type);
	if(result == GVFalse)
	{
		gviFreeDevice(device);
		return NULL;
	}

	// store the pointers
	device->m_methods.m_freeDevice = gviPS3HeadsetFreeDevice;
	device->m_methods.m_startDevice = gviPS3HeadsetStartDevice;
	device->m_methods.m_stopDevice = gviPS3HeadsetStopDevice;
	device->m_methods.m_isDeviceStarted = gviPS3HeadsetIsDeviceStarted;
	device->m_methods.m_setDeviceVolume = gviPS3HeadsetSetDeviceVolume;
	device->m_methods.m_getDeviceVolume = gviPS3HeadsetGetDeviceVolume;
	device->m_methods.m_setCaptureThreshold = gviPS3HeadsetSetCaptureThreshold;
	device->m_methods.m_getCaptureThreshold = gviPS3HeadsetGetCaptureThreshold;
	device->m_methods.m_getAvailableCaptureBytes = gviPS3HeadsetGetAvailableCaptureBytes;
	device->m_methods.m_capturePacket = gviPS3HeadsetCapturePacket;
	device->m_methods.m_playPacket = gviPS3HeadsetPlayPacket;
	device->m_methods.m_isSourceTalking = gviPS3HeadsetIsSourceTalking;
	device->m_methods.m_listTalkingSources = gviPS3HeadsetListTalkingSources;

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

#endif //!defined(GV_NO_PS3_HEADSET)