comparison host.c @ 0:9008dbc8ca74

import original C code from GSM 06.06
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 14 Jun 2024 23:27:16 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:9008dbc8ca74
1 /***************************************************************************
2 *
3 * File Name: host.c
4 *
5 * Purpose: Contains functions for file I/O and formatting, no signal
6 * processing.
7 *
8 * The functions in this file are listed below. All are level 2
9 * fuctions, where level 0 is main(), except for fillBitAlloc() which
10 * is level 3. The two "Interface" routines perform truncation of the
11 * three least significant bits of the 16 bit linear input. The others
12 * are simply file I/O functions and data reformatters.
13 *
14 * fillBitAlloc()
15 * hostEncoderInterface()
16 * readDecfile()
17 * speechDecoderHostInterface()
18 * writeEncfile()
19 *
20 **************************************************************************/
21
22 /*_________________________________________________________________________
23 | |
24 | Include Files |
25 |_________________________________________________________________________|
26 */
27
28 #include <stdio.h>
29 #include "typedefs.h"
30
31 /***************************************************************************
32 *
33 * FUNCTION NAME: fillBitAlloc
34 *
35 * PURPOSE:
36 *
37 * Arrange speech parameters for encoder output
38 *
39 * INPUTS:
40 *
41 * The speechcoders codewords:
42 * iR0 - Frame energy
43 * piVqIndeces[0:2] - LPC vector quantizer codewords
44 * iSoftInterp - Soft interpolation bit 1 or 0
45 * iVoicing - voicing mode 0,1,2,3
46 * piLags[0:3] - Frame and delta lag codewords
47 * piCodeWrdsA[0:3] - VSELP codevector 1
48 * piCodeWrdsB[0:3] - VSELP codevector 2 (n/a for voiced modes)
49 * piGsp0s[0:3] - GSP0 codewords
50 * swVadFlag - voice activity detection flag
51 * swSP - Speech flag
52 *
53 * OUTPUTS:
54 *
55 * pswBAlloc[0:20] - an array into which the coded data is moved
56 *
57 * RETURN VALUE:
58 *
59 * none
60 *
61 * REFERENCES: Sub-clause 2.1 and 4.1.12 of GSM Recomendation 06.20
62 *
63 **************************************************************************/
64
65 void fillBitAlloc(int iVoicing, int iR0, int *piVqIndeces,
66 int iSoftInterp, int *piLags,
67 int *piCodeWrdsA, int *piCodeWrdsB,
68 int *piGsp0s, Shortword swVadFlag,
69 Shortword swSP, Shortword *pswBAlloc)
70 {
71
72 /*_________________________________________________________________________
73 | |
74 | Automatic Variables |
75 |_________________________________________________________________________|
76 */
77
78 int i;
79 Shortword *pswNxt;
80
81 /*_________________________________________________________________________
82 | |
83 | Executable Code |
84 |_________________________________________________________________________|
85 */
86
87 pswNxt = pswBAlloc;
88 *pswNxt++ = iR0;
89 for (i = 0; i < 3; i++)
90 *pswNxt++ = *piVqIndeces++;
91 *pswNxt++ = iSoftInterp;
92 *pswNxt++ = iVoicing;
93
94 /* check voicing mode */
95 if (iVoicing)
96 {
97 /* voiced mode */
98 for (i = 0; i < N_SUB; i++)
99 {
100 *pswNxt++ = *piLags++;
101 *pswNxt++ = *piCodeWrdsA++;
102 *pswNxt++ = *piGsp0s++;
103 }
104 }
105 else
106 { /* unvoiced frame */
107 for (i = 0; i < N_SUB; i++)
108 {
109 *pswNxt++ = *piCodeWrdsA++;
110 *pswNxt++ = *piCodeWrdsB++;
111 *pswNxt++ = *piGsp0s++;
112 }
113 }
114 *pswNxt++ = swVadFlag;
115 *pswNxt++ = swSP;
116 }
117
118 /***************************************************************************
119 *
120 * FUNCTION NAME: hostEncoderInterface
121 *
122 * PURPOSE:
123 *
124 * Read in speech data from a file. Zero the least significant 3 bits.
125 *
126 *
127 * INPUTS:
128 *
129 * pfileInSpeech
130 * FILE pointer to the binary input file
131 *
132 * iNumToRead
133 * Number of samples to read from the file, typically
134 * 160 (20 ms of speech).
135 *
136 *
137 * OUTPUTS:
138 *
139 * pswSamplesRead[]
140 * The speech samples read in from the file.
141 *
142 *
143 * RETURN VALUE:
144 *
145 * iNumRead
146 * The number of samples actually read.
147 *
148 * IMPLEMENTATION:
149 *
150 * The input speech file should be in "native" format. This means that
151 * the file is to be read (by this program) and written (by another
152 * program) as short ints (not chars).
153 *
154 * If not enough samples are available in the file, the number actually
155 * read is returned. If the read fails to fill the requested iNumToRead
156 * samples, then the rest are zeroed.
157 *
158 * In all cases the least significant 3 bits of all speech samples are
159 * zeroed.
160 *
161 * REFERENCES: Sub-clause 4.1 of GSM Recomendation 06.20
162 *
163 * KEYWORDS: read, read speech, get speech data
164 *
165 **************************************************************************/
166
167 int hostEncoderInterface(FILE *pfileInSpeech, int iNumToRead,
168 Shortword pswSamplesRead[])
169 {
170
171 /*_________________________________________________________________________
172 | |
173 | Automatic Variables |
174 |_________________________________________________________________________|
175 */
176 int iNumRead,
177 i;
178
179 /*_________________________________________________________________________
180 | |
181 | Executable Code |
182 |_________________________________________________________________________|
183 */
184
185 iNumRead = fread((char *) pswSamplesRead, sizeof (Shortword),
186 iNumToRead, pfileInSpeech);
187
188 /* Delete the 3 LSB's - 13 bit speech input */
189 /*------------------------------------------*/
190
191 for (i = 0; i < iNumRead; i++)
192 {
193 pswSamplesRead[i] &= 0xfff8;
194 }
195
196
197 /* Fill out the iNumToRead buffer with zeroes */
198 /*--------------------------------------------*/
199
200 if (iNumRead != iNumToRead)
201 {
202 for (i = iNumRead; i < iNumToRead; i++)
203 {
204 pswSamplesRead[i] = 0;
205 }
206 }
207 return (iNumRead);
208 }
209
210 /***************************************************************************
211 *
212 * FUNCTION NAME: readDecfile
213 *
214 * PURPOSE:
215 * Reads decoder parameter input file
216 *
217 * INPUT:
218 * infile decoder parameter input file.
219 *
220 * OUTPUT:
221 * pswSpeechPara array of received 16-bit values
222 *
223 * RETURN:
224 * 0 successfully read a complete frame of data
225 *
226 * REFERENCES: Sub-clause 4.2 of GSM Recomendation 06.20
227 *
228 * KEYWORDS: pswSpeechPara
229 *
230 **************************************************************************/
231
232 int readDecfile(FILE *infile, Shortword pswSpeechPara[])
233 {
234 if (fread((char *) pswSpeechPara, sizeof (Shortword), 22, infile) == 0)
235 return (1);
236 else
237 return (0);
238 }
239
240 /***************************************************************************
241 *
242 * FUNCTION NAME: speechDecoderHostInterface
243 *
244 * PURPOSE:
245 * The purpose of this function is to truncate the linear pcm and write
246 * it into the output file
247 *
248 * INPUTS:
249 *
250 * F_LEN
251 *
252 * 160 = linear pcm output frame size
253 *
254 * pswDecodedSpeechFrame[0:F_LEN-1]
255 *
256 * 16 bit linear pcm
257 *
258 * OUTPUTS:
259 *
260 * fpfileSpeechOut
261 *
262 * 13 bit linear pcm stored to file given by this pointer
263 *
264 * RETURN VALUE:
265 *
266 * none
267 *
268 * IMPLEMENTATION:
269 *
270 * REFERENCES: Sub-clause 4.2 of GSM Recomendation 06.20
271 *
272 * KEYWORDS: synthesis, speechdecoder, decoding, truncation
273 *
274 **************************************************************************/
275
276 void speechDecoderHostInterface(Shortword pswDecodedSpeechFrame[],
277 FILE *fpfileSpeechOut)
278 {
279
280 /*_________________________________________________________________________
281 | |
282 | Local Constants |
283 |_________________________________________________________________________|
284 */
285
286 #define PCM_MASK 0xfff8 /* 16 to 13 bit linear PCM mask */
287
288 /*_________________________________________________________________________
289 | |
290 | Automatic Variables |
291 |_________________________________________________________________________|
292 */
293
294 short int i;
295
296 /*_________________________________________________________________________
297 | |
298 | Executable Code |
299 |_________________________________________________________________________|
300 */
301
302 /* truncate the 16 bit linear pcm to 13 bits */
303 /* ----------------------------------------- */
304
305 for (i = 0; i < F_LEN; i++)
306 {
307 pswDecodedSpeechFrame[i] = pswDecodedSpeechFrame[i] & PCM_MASK;
308 }
309
310 /* F_LEN samples of linear pcm to output file */
311 /* ------------------------------------------ */
312
313 fwrite((char *) pswDecodedSpeechFrame, sizeof (Shortword),
314 F_LEN, fpfileSpeechOut);
315 }
316
317 /***************************************************************************
318 *
319 * FUNCTION NAME: writeEncfile
320 *
321 * PURPOSE:
322 * Writes encoded parameters to ouput file
323 *
324 * INPUT:
325 * pswSpeechPara array of encoded parameter words.
326 *
327 * OUTPUT:
328 * fpfileEnc 16-bit encoded values.
329 *
330 * RETURN:
331 * i number of bytes written
332 *
333 * REFERENCES: Sub-clause 4.1 of GSM Recomendation 06.20
334 *
335 * KEYWORDS: pswSpeechPara, fpfileEnc
336 *
337 **************************************************************************
338 */
339
340 int writeEncfile(Shortword pswSpeechPara[], FILE *fpfileEnc)
341 {
342 int i;
343
344 i = fwrite((char *) pswSpeechPara, sizeof (Shortword), 20, fpfileEnc);
345
346 return (i);
347 }