File: voice2bench.c

package info (click to toggle)
openmohaa 0.81.1%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: trixie
  • size: 29,124 kB
  • sloc: ansic: 270,865; cpp: 250,173; sh: 234; asm: 141; xml: 64; makefile: 7
file content (261 lines) | stat: -rw-r--r-- 5,384 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
#include "../gv.h"
#include "voicesample.h"
#include <stdio.h>

GVByte voice_sample[80000];

#if defined(_PS2)

#include "../gvLogitechPS2Codecs.h"

static GVBool Init(GVCodec codec)
{
	const char * name;

	// figure out the quality
	// the mapping of codec to quality is copied from gvCodec.c
	if(codec == GVCodecSuperHighQuality)
		name = "uLaw";
	else if(codec == GVCodecHighQuality)
		name = "G723.24";
	else if(codec == GVCodecAverage)
		name = "GSM";
	else if(codec == GVCodecLowBandwidth)
		name = "SPEEX";
	else if(codec == GVCodecSuperLowBandwidth)
		name = "LPC10";
	else
		return GVFalse;

	return gviLGCodecInitialize(name);
}

static void Cleanup(void)
{
	gviLGCodecCleanup();
}

static int GetSamplesPerFrame(void)
{
	return gviLGCodecGetSamplesPerFrame();
}

static GVBool NewDecoder(GVDecoderData * data)
{
	data = NULL;
	return GVTrue;
}

static void FreeDecoder(GVDecoderData data)
{
}

static void Encode(GVByte * out, const GVSample * in)
{
	gviLGCodecEncode(out, in);
}

static void Decode(GVSample * out, const GVByte * in, GVDecoderData data)
{
	gviLGCodecDecodeAdd(out, in, data);
}

#elif defined(_PSP) || defined(_WIN32)

#include "../gsm-1.0-pl12/inc/gsm.h"

gsm EncoderState;

static GVBool Init(GVCodec codec)
{
	int gsm_ltp = 1;
	EncoderState = gsm_create();
	gsm_option(EncoderState, GSM_OPT_LTP_CUT, &gsm_ltp);
	
	GSI_UNUSED(codec);
	return (EncoderState != NULL)?GVTrue:GVFalse;	
}

static void Cleanup(void)
{
	gsm_destroy(EncoderState);
}

static int GetSamplesPerFrame(void)
{
	return 160;
}

static GVBool NewDecoder(GVDecoderData * data)
{
	*data = (GVDecoderData)gsm_create();
	return (*data != NULL)?GVTrue:GVFalse;
}

static void FreeDecoder(GVDecoderData data)
{
	gsm_destroy((gsm)data);
}

static void Encode(GVByte * out, const GVSample * in)
{
	gsm_encode((gsm)EncoderState, (gsm_signal*)in, (gsm_byte*)out);
}

static void Decode(GVSample * out, const GVByte * in, GVDecoderData data)
{
	gsm_decode((gsm)data, (gsm_byte*)in, (gsm_signal*)out);
}

#else

#include "../gvSpeex.h"

static GVBool Init(GVCodec codec, GVRate sampleRate)
{
	int quality;

	// figure out the quality
	// the mapping of codec to quality is copied from gvCodec.c
	if(codec == GVCodecSuperHighQuality)
		quality = 10;
	else if(codec == GVCodecHighQuality)
		quality = 7;
	else if(codec == GVCodecAverage)
		quality = 4;
	else if(codec == GVCodecLowBandwidth)
		quality = 2;
	else if(codec == GVCodecSuperLowBandwidth)
		quality = 1;
	else
		return GVFalse;
	
	gvSetSampleRate(sampleRate);
	return gviSpeexInitialize(quality, sampleRate);
}

static void Cleanup(void)
{
	gviSpeexCleanup();
}

static int GetSamplesPerFrame(void)
{
	return gviSpeexGetSamplesPerFrame();
}

static GVBool NewDecoder(GVDecoderData * data)
{
	return gviSpeexNewDecoder(data);
}

static void FreeDecoder(GVDecoderData data)
{
	gviSpeexFreeDecoder(data);
}

static void Encode(GVByte * out, const GVSample * in)
{
	gviSpeexEncode(out, in);
}

static void Decode(GVSample * out, const GVByte * in, GVDecoderData data)
{
	gviSpeexDecodeAdd(out, in, data);
}

#endif

static void TestCodec(GVCodec codec, GVRate sampleRate, const char * name)
{
	int samplesPerFrame;
	GVSample * samples;
	int numFrames;
	GVByte encodeOut[1000];
	GVSample decodeOut[1000];
	int i;
	unsigned long startTime;
	unsigned long endTime;
	GVDecoderData decoderData;
	unsigned long sampleTime = 0;
	unsigned long totalEncodeTime = 0;
	unsigned long totalDecodeTime = 0;
	double encodeFraction;
	double decodeFraction;

	printf("Testing %s\n", name);


	if(!Init(codec, sampleRate))
	{
		printf("Failed to init\n");
		return;
	}

	sampleTime = (unsigned long)((double)sizeof(voice_sample) / ((int)(sampleRate) * GV_BYTES_PER_SAMPLE) * 1000000);

	if(!NewDecoder(&decoderData))
	{
		printf("Failed to allocate decoder data\n");
		return;
	}

	samplesPerFrame = GetSamplesPerFrame();
	numFrames = (sizeof(voice_sample) / GV_BYTES_PER_SAMPLE / samplesPerFrame);

	samples = (GVSample *)voice_sample;

	totalEncodeTime = 0;
	for(i = 0 ; i < numFrames ; i++)
	{
		startTime = current_time_hires();
		Encode(encodeOut, samples);
		endTime = current_time_hires();
		totalEncodeTime += (endTime - startTime);
		samples += samplesPerFrame;
		msleep(0);

		startTime = current_time_hires();
		Decode(decodeOut, encodeOut, decoderData);
		endTime = current_time_hires();
		totalDecodeTime += (endTime - startTime);
		msleep(0);
	}

	encodeFraction = ((double)totalEncodeTime / sampleTime);
	decodeFraction = ((double)totalDecodeTime / sampleTime);

	printf("Encode: %0.1f%%\n", encodeFraction * 100);
	printf("Decode: %0.1f%%\n", decodeFraction * 100);

	FreeDecoder(decoderData);

	Cleanup();
}

#if defined(_PS2) || defined(_PSP)
	#ifdef __MWERKS__ // CodeWarrior will warn if not prototyped
		int test_main(int argc, char **argp);
	#endif	
int test_main(int argc, char **argp)
#else
int main(int argc, char **argp)
#endif // _PS2
{
	printf("Testing codecs\n");

	TestCodec(GVCodecSuperLowBandwidth, GVRate_8KHz, "GVCodecSuperLowBandwidth");
#if !defined(_PS2)
	TestCodec(GVCodecLowBandwidth, GVRate_8KHz, "GVCodecLowBandwidth");
#endif
	TestCodec(GVCodecAverage, GVRate_8KHz, "GVCodecAverage");
	TestCodec(GVCodecHighQuality, GVRate_8KHz, "GVCodecHighQuality");
	TestCodec(GVCodecSuperHighQuality,  GVRate_8KHz, "GVCodecSuperHighQuality");

	printf("Testing complete\n");

	GSI_UNUSED(argc);
	GSI_UNUSED(argp);
	
	return 0;
}