File: gvCustomDevice.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 (385 lines) | stat: -rw-r--r-- 10,305 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
#include "gvCustomDevice.h"
#include "gvSource.h"
#include "gvCodec.h"
#include "gvUtil.h"

#define GVI_DEFAULT_THRESHOLD   ((GVScalar)0.0)

#if defined(_WIN32)
static GVDeviceID GVICustomDeviceID = {0};
#else
static GVDeviceID GVICustomDeviceID = 0;
#endif 

typedef struct
{
	GVBool        m_captureStarted;
	GVScalar      m_captureThreshold;
	GVFrameStamp  m_captureClock;
	GVFrameStamp  m_captureLastCrossedThresholdTime;
	GVScalar      m_captureVolume;

	GVBool        m_playbackStarted;
	GVISourceList m_playbackSources;
	GVFrameStamp  m_playbackClock;
	GVScalar      m_playbackVolume;
} GVICustomData;

static void gviCustomFreeDevice(GVIDevice * device)
{
	GVICustomData * data = (GVICustomData *)device->m_data;

	if(device->m_types & GV_PLAYBACK)
	{
		data->m_playbackStarted = GVFalse;
		gviFreeSourceList(data->m_playbackSources);
	}
	if(device->m_types & GV_CAPTURE)
	{
		data->m_captureStarted = GVFalse;
	}

	gviFreeDevice(device);
}

static GVBool gviCustomStartDevice(GVIDevice * device, GVDeviceType type)
{
	GVICustomData * data = (GVICustomData *)device->m_data;

	if(type & GV_PLAYBACK)
	{
		data->m_playbackStarted = GVTrue;
		data->m_playbackClock = 0;
	}
	if(type & GV_CAPTURE)
	{
		data->m_captureStarted = GVTrue;
	}
	return GVTrue;
}

static void gviCustomStopDevice(GVIDevice * device, GVDeviceType type)
{
	GVICustomData * data = (GVICustomData *)device->m_data;

	if(type & GV_PLAYBACK)
	{
		data->m_playbackStarted = GVFalse;
		gviClearSourceList(data->m_playbackSources);
	}
	if(type & GV_CAPTURE)
	{
		data->m_captureStarted = GVFalse;
		data->m_captureClock++;
	}
}

static GVBool gviCustomIsDeviceStarted(GVIDevice * device, GVDeviceType type)
{
	GVICustomData * data = (GVICustomData *)device->m_data;

	if(type == GV_PLAYBACK)
		return data->m_playbackStarted;
	else if(type == GV_CAPTURE)
		return data->m_captureStarted;
	return (data->m_playbackStarted && data->m_captureStarted);
}

static void gviCustomSetDeviceVolume(GVIDevice * device, GVDeviceType type, GVScalar volume)
{
	GVICustomData * data = (GVICustomData *)device->m_data;

	if(type & GV_PLAYBACK)
		data->m_playbackVolume = volume;
	if(type & GV_CAPTURE)
		data->m_captureVolume = volume;
}

static GVScalar gviCustomGetDeviceVolume(GVIDevice * device, GVDeviceType type)
{
	GVICustomData * data = (GVICustomData *)device->m_data;

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

static void gviCustomSetCaptureThreshold(GVIDevice * device, GVScalar threshold)
{
	GVICustomData * data = (GVICustomData *)device->m_data;

	data->m_captureThreshold = threshold;
}

static GVScalar gviCustomGetCaptureThreshold(GVIDevice * device)
{
	GVICustomData * data = (GVICustomData *)device->m_data;

	return data->m_captureThreshold;
}

static int gviCustomGetAvailableCaptureBytes(GVDevice device)
{
	// not supported with custom devices
	assert(0);
	GSI_UNUSED(device);
	return 0;
}

static GVBool gviCustomCapturePacket(GVDevice device, GVByte * packet, int * len, GVFrameStamp * frameStamp, GVScalar * volume)
{
	// not supported with custom devices
	assert(0);
	GSI_UNUSED(device);
	GSI_UNUSED(packet);
	GSI_UNUSED(len);
	GSI_UNUSED(frameStamp);
	GSI_UNUSED(volume);
	return GVFalse;
}

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

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

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

static GVBool gviCustomIsSourceTalking(GVDevice device, GVSource source)
{
	GVICustomData * data = (GVICustomData *)device->m_data;

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

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

static int gviCustomListTalkingSources(GVDevice device, GVSource sources[], int maxSources)
{
	GVICustomData * data = (GVICustomData *)device->m_data;

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

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

GVDevice gviCustomNewDevice(GVDeviceType type)
{
	GVIDevice * device;
	GVICustomData * data;

	// create a new device
	device = gviNewDevice(GVICustomDeviceID, GVHardwareCustom, type, sizeof(GVICustomData));
	if(!device)
		return NULL;

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

	// store the pointers
	device->m_methods.m_freeDevice = gviCustomFreeDevice;
	device->m_methods.m_startDevice = gviCustomStartDevice;
	device->m_methods.m_stopDevice = gviCustomStopDevice;
	device->m_methods.m_isDeviceStarted = gviCustomIsDeviceStarted;
	device->m_methods.m_setDeviceVolume = gviCustomSetDeviceVolume;
	device->m_methods.m_getDeviceVolume = gviCustomGetDeviceVolume;
	device->m_methods.m_setCaptureThreshold = gviCustomSetCaptureThreshold;
	device->m_methods.m_getCaptureThreshold = gviCustomGetCaptureThreshold;
	device->m_methods.m_getAvailableCaptureBytes = gviCustomGetAvailableCaptureBytes;
	device->m_methods.m_capturePacket = gviCustomCapturePacket;
	device->m_methods.m_playPacket = gviCustomPlayPacket;
	device->m_methods.m_isSourceTalking = gviCustomIsSourceTalking;
	device->m_methods.m_listTalkingSources = gviCustomListTalkingSources;

	// init the data
	if(type & GV_PLAYBACK)
	{
		// create the array of sources
		data->m_playbackSources = gviNewSourceList();
		if(!data->m_playbackSources)
			return GVFalse;

		// setup the struct
		data->m_playbackStarted = GVFalse;
		data->m_playbackClock = 0;
		data->m_playbackVolume = 1.0;
	}
	if(type & GV_CAPTURE)
	{
		data->m_captureStarted = GVFalse;
		data->m_captureThreshold = GVI_DEFAULT_THRESHOLD;
		data->m_captureClock = 0;
		data->m_captureLastCrossedThresholdTime = (GVFrameStamp)(data->m_captureClock - GVI_HOLD_THRESHOLD_FRAMES - 1);
		data->m_captureVolume = 1.0;
	}

	return device;
}

GVBool gviGetCustomPlaybackAudio(GVIDevice * device, GVSample * audio, int numSamples)
{
	GVICustomData * data = (GVICustomData *)device->m_data;
	GVBool wroteToBuffer;
	int numFrames;
	int i;
	GVBool result = GVFalse;

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

	// the len must be a multiple of the frame size
	assert(!(numSamples % GVISamplesPerFrame));

	// calc the number of frames
	numFrames = (numSamples / GVISamplesPerFrame);

	// fill it	
	wroteToBuffer = gviWriteSourcesToBuffer(data->m_playbackSources, data->m_playbackClock,
                                            audio, numFrames);

	// check if anything was written
	if(!wroteToBuffer)
	{
		// no, so clear it
		memset(audio, 0, numSamples * GV_BYTES_PER_SAMPLE);
	}
	else
	{
		// check if we need to adjust the volume
		if(data->m_playbackVolume != 1.0)
		{
			for(i = 0 ; i < numSamples ; i++)
				audio[i] = (GVSample)(audio[i] * data->m_playbackVolume);
		}
		// set the output flag to true because samples were decoded this pass
		result = GVTrue;
	}

	// filter
	if(device->m_playbackFilterCallback)
	{
		for(i = 0 ; i < numFrames ; i++)
			device->m_playbackFilterCallback(device, audio + (GVISamplesPerFrame * i), (GVFrameStamp)(data->m_playbackClock + i));
	}

	// update the clock
	// Expanded to remove warnings in VS2K5
	data->m_playbackClock = data->m_playbackClock + (GVFrameStamp)numFrames;

	return result;
}

GVBool gviSetCustomCaptureAudio(GVDevice device, const GVSample * audio, int numSamples,
                               GVByte * packet, int * packetLen, GVFrameStamp * frameStamp, GVScalar * volume)
{
	GVICustomData * data = (GVICustomData *)device->m_data;
	int numBytes;
	GVBool overThreshold = GVFalse;
	int numFrames;
	int i;
	int len;

	// store the len
	len = *packetLen;

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

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

	// the len must be a multiple of the frame size
	assert(!(numSamples % GVISamplesPerFrame));

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

	// figure out the number of frames
	numFrames = (numSamples / GVISamplesPerFrame);

	// if audio is NULL, this is simply telling us to advance the clock
	if(audio)
	{
		// get the number of new bytes
		numBytes = (int)(numSamples * GV_BYTES_PER_SAMPLE);

		// make sure they have enough space in the packet buffer
		assert(len >= numBytes);
		if(len < numBytes)
			return GVFalse;

		// get the volume if requested
		if(volume)
			*volume = gviGetSamplesVolume(audio, numSamples);

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

		// did the audio cross the threshold?
		if(overThreshold)
		{
			// update the time at which we crossed
			data->m_captureLastCrossedThresholdTime = data->m_captureClock;
		}
		else
		{
			// check if we are still within the hold time
			overThreshold = ((GVFrameStamp)(data->m_captureClock - data->m_captureLastCrossedThresholdTime) < GVI_HOLD_THRESHOLD_FRAMES);
		}

		if(overThreshold)
		{
			// check if we need to adjust the volume
			if(data->m_captureVolume != 1.0)
			{
				for(i = 0 ; i < numSamples ; i++)
					((GVSample *)audio)[i] = (GVSample)(audio[i] * data->m_captureVolume);
			}

			// handle the data one frame at a time
			for(i = 0 ; i < numFrames ; i++)
			{
				// filter
				if(device->m_captureFilterCallback)
					device->m_captureFilterCallback(device, (GVSample *)audio + (i * GVISamplesPerFrame), (GVFrameStamp)(data->m_captureClock + i));

				// encode the buffer into the packet
				gviEncode(packet + (i * GVIEncodedFrameSize), audio + (i * GVISamplesPerFrame));
			}
		}

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

	// increment the clock
	// Expanded to remove warnings in VS2K5
	data->m_captureClock = data->m_captureClock + (GVFrameStamp)numFrames;

	// return false if we didn't get a packet
	if(!overThreshold)
		return GVFalse;

	return GVTrue;
}