/src/libsndfile/src/gsm610.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ** Copyright (C) 1999-2019 Erik de Castro Lopo <erikd@mega-nerd.com> |
3 | | ** |
4 | | ** This program is free software; you can redistribute it and/or modify |
5 | | ** it under the terms of the GNU Lesser General Public License as published by |
6 | | ** the Free Software Foundation; either version 2.1 of the License, or |
7 | | ** (at your option) any later version. |
8 | | ** |
9 | | ** This program is distributed in the hope that it will be useful, |
10 | | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | ** GNU Lesser General Public License for more details. |
13 | | ** |
14 | | ** You should have received a copy of the GNU Lesser General Public License |
15 | | ** along with this program; if not, write to the Free Software |
16 | | ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
17 | | */ |
18 | | |
19 | | #include "sfconfig.h" |
20 | | |
21 | | #include <stdio.h> |
22 | | #include <stdlib.h> |
23 | | #include <string.h> |
24 | | #include <math.h> |
25 | | |
26 | | #include "sndfile.h" |
27 | | #include "sfendian.h" |
28 | | #include "common.h" |
29 | | #include "wavlike.h" |
30 | | #include "GSM610/gsm.h" |
31 | | |
32 | 349k | #define GSM610_BLOCKSIZE 33 |
33 | 401 | #define GSM610_SAMPLES 160 |
34 | | |
35 | | typedef struct gsm610_tag |
36 | | { int blocks ; |
37 | | int blockcount, samplecount ; |
38 | | int samplesperblock, blocksize ; |
39 | | |
40 | | int (*decode_block) (SF_PRIVATE *psf, struct gsm610_tag *pgsm610) ; |
41 | | int (*encode_block) (SF_PRIVATE *psf, struct gsm610_tag *pgsm610) ; |
42 | | |
43 | | short samples [WAVLIKE_GSM610_SAMPLES] ; |
44 | | unsigned char block [WAVLIKE_GSM610_BLOCKSIZE] ; |
45 | | |
46 | | /* Damn I hate typedef-ed pointers; yes, gsm is a pointer type. */ |
47 | | gsm gsm_data ; |
48 | | } GSM610_PRIVATE ; |
49 | | |
50 | | static sf_count_t gsm610_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ; |
51 | | static sf_count_t gsm610_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ; |
52 | | static sf_count_t gsm610_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ; |
53 | | static sf_count_t gsm610_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ; |
54 | | |
55 | | static sf_count_t gsm610_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ; |
56 | | static sf_count_t gsm610_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ; |
57 | | static sf_count_t gsm610_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ; |
58 | | static sf_count_t gsm610_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ; |
59 | | |
60 | | static int gsm610_read_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610, short *ptr, int len) ; |
61 | | static int gsm610_write_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610, const short *ptr, int len) ; |
62 | | |
63 | | static int gsm610_decode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) ; |
64 | | static int gsm610_encode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) ; |
65 | | |
66 | | static int gsm610_wav_decode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) ; |
67 | | static int gsm610_wav_encode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) ; |
68 | | |
69 | | static sf_count_t gsm610_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ; |
70 | | |
71 | | static int gsm610_close (SF_PRIVATE *psf) ; |
72 | | |
73 | | /*============================================================================================ |
74 | | ** WAV GSM610 initialisation function. |
75 | | */ |
76 | | |
77 | | int |
78 | | gsm610_init (SF_PRIVATE *psf) |
79 | 1.03k | { GSM610_PRIVATE *pgsm610 ; |
80 | 1.03k | int true_flag = 1 ; |
81 | | |
82 | 1.03k | if (psf->codec_data != NULL) |
83 | 0 | { psf_log_printf (psf, "*** psf->codec_data is not NULL.\n") ; |
84 | 0 | return SFE_INTERNAL ; |
85 | 1.03k | } ; |
86 | | |
87 | 1.03k | if (psf->file.mode == SFM_RDWR) |
88 | 0 | return SFE_BAD_MODE_RW ; |
89 | | |
90 | 1.03k | psf->sf.seekable = SF_FALSE ; |
91 | | |
92 | 1.03k | if ((pgsm610 = calloc (1, sizeof (GSM610_PRIVATE))) == NULL) |
93 | 0 | return SFE_MALLOC_FAILED ; |
94 | | |
95 | 1.03k | psf->codec_data = pgsm610 ; |
96 | | |
97 | 1.03k | memset (pgsm610, 0, sizeof (GSM610_PRIVATE)) ; |
98 | | |
99 | | /*============================================================ |
100 | | |
101 | | Need separate gsm_data structs for encode and decode. |
102 | | |
103 | | ============================================================*/ |
104 | | |
105 | 1.03k | if ((pgsm610->gsm_data = gsm_create ()) == NULL) |
106 | 0 | return SFE_MALLOC_FAILED ; |
107 | | |
108 | 1.03k | switch (SF_CONTAINER (psf->sf.format)) |
109 | 1.03k | { case SF_FORMAT_WAV : |
110 | 636 | case SF_FORMAT_WAVEX : |
111 | 638 | case SF_FORMAT_W64 : |
112 | 638 | gsm_option (pgsm610->gsm_data, GSM_OPT_WAV49, &true_flag) ; |
113 | | |
114 | 638 | pgsm610->encode_block = gsm610_wav_encode_block ; |
115 | 638 | pgsm610->decode_block = gsm610_wav_decode_block ; |
116 | | |
117 | 638 | pgsm610->samplesperblock = WAVLIKE_GSM610_SAMPLES ; |
118 | 638 | pgsm610->blocksize = WAVLIKE_GSM610_BLOCKSIZE ; |
119 | 638 | break ; |
120 | | |
121 | 401 | case SF_FORMAT_AIFF : |
122 | 401 | case SF_FORMAT_RAW : |
123 | 401 | pgsm610->encode_block = gsm610_encode_block ; |
124 | 401 | pgsm610->decode_block = gsm610_decode_block ; |
125 | | |
126 | 401 | pgsm610->samplesperblock = GSM610_SAMPLES ; |
127 | 401 | pgsm610->blocksize = GSM610_BLOCKSIZE ; |
128 | 401 | break ; |
129 | | |
130 | 0 | default : |
131 | 0 | return SFE_INTERNAL ; |
132 | 0 | break ; |
133 | 1.03k | } ; |
134 | | |
135 | 1.03k | if (psf->file.mode == SFM_READ) |
136 | 1.03k | { if (psf->datalength % pgsm610->blocksize == 0) |
137 | 29 | pgsm610->blocks = psf->datalength / pgsm610->blocksize ; |
138 | 1.01k | else if (psf->datalength % pgsm610->blocksize == 1 && pgsm610->blocksize == GSM610_BLOCKSIZE) |
139 | 4 | { /* |
140 | | ** Weird AIFF specific case. |
141 | | ** AIFF chunks must be at an even offset from the start of file and |
142 | | ** GSM610_BLOCKSIZE is odd which can result in an odd length SSND |
143 | | ** chunk. The SSND chunk then gets padded on write which means that |
144 | | ** when it is read the datalength is too big by 1. |
145 | | */ |
146 | 4 | pgsm610->blocks = psf->datalength / pgsm610->blocksize ; |
147 | 4 | } |
148 | 1.00k | else |
149 | 1.00k | { psf_log_printf (psf, "*** Warning : data chunk seems to be truncated.\n") ; |
150 | 1.00k | pgsm610->blocks = psf->datalength / pgsm610->blocksize + 1 ; |
151 | 1.00k | } ; |
152 | | |
153 | 1.03k | psf->sf.frames = (sf_count_t) pgsm610->samplesperblock * pgsm610->blocks ; |
154 | | |
155 | 1.03k | psf_fseek (psf, psf->dataoffset, SEEK_SET) ; |
156 | | |
157 | 1.03k | pgsm610->decode_block (psf, pgsm610) ; /* Read first block. */ |
158 | | |
159 | 1.03k | psf->read_short = gsm610_read_s ; |
160 | 1.03k | psf->read_int = gsm610_read_i ; |
161 | 1.03k | psf->read_float = gsm610_read_f ; |
162 | 1.03k | psf->read_double = gsm610_read_d ; |
163 | 1.03k | } ; |
164 | | |
165 | 1.03k | if (psf->file.mode == SFM_WRITE) |
166 | 0 | { pgsm610->blockcount = 0 ; |
167 | 0 | pgsm610->samplecount = 0 ; |
168 | |
|
169 | 0 | psf->write_short = gsm610_write_s ; |
170 | 0 | psf->write_int = gsm610_write_i ; |
171 | 0 | psf->write_float = gsm610_write_f ; |
172 | 0 | psf->write_double = gsm610_write_d ; |
173 | 0 | } ; |
174 | | |
175 | 1.03k | psf->codec_close = gsm610_close ; |
176 | | |
177 | 1.03k | psf->seek = gsm610_seek ; |
178 | | |
179 | 1.03k | psf->filelength = psf_get_filelen (psf) ; |
180 | 1.03k | psf->datalength = psf->filelength - psf->dataoffset ; |
181 | | |
182 | 1.03k | return 0 ; |
183 | 1.03k | } /* gsm610_init */ |
184 | | |
185 | | /*============================================================================================ |
186 | | ** GSM 6.10 Read Functions. |
187 | | */ |
188 | | |
189 | | static int |
190 | | gsm610_wav_decode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) |
191 | 11.9k | { int k ; |
192 | | |
193 | 11.9k | pgsm610->blockcount ++ ; |
194 | 11.9k | pgsm610->samplecount = 0 ; |
195 | | |
196 | 11.9k | if (pgsm610->blockcount > pgsm610->blocks) |
197 | 8 | { memset (pgsm610->samples, 0, sizeof (pgsm610->samples)) ; |
198 | 8 | return 1 ; |
199 | 11.9k | } ; |
200 | | |
201 | 11.9k | if ((k = (int) psf_fread (pgsm610->block, 1, WAVLIKE_GSM610_BLOCKSIZE, psf)) != WAVLIKE_GSM610_BLOCKSIZE) |
202 | 623 | psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, WAVLIKE_GSM610_BLOCKSIZE) ; |
203 | | |
204 | 11.9k | if (gsm_decode (pgsm610->gsm_data, pgsm610->block, pgsm610->samples) < 0) |
205 | 0 | { psf_log_printf (psf, "Error from WAV gsm_decode() on frame : %d\n", pgsm610->blockcount) ; |
206 | 0 | return 0 ; |
207 | 11.9k | } ; |
208 | | |
209 | 11.9k | if (gsm_decode (pgsm610->gsm_data, pgsm610->block + (WAVLIKE_GSM610_BLOCKSIZE + 1) / 2, pgsm610->samples + WAVLIKE_GSM610_SAMPLES / 2) < 0) |
210 | 0 | { psf_log_printf (psf, "Error from WAV gsm_decode() on frame : %d.5\n", pgsm610->blockcount) ; |
211 | 0 | return 0 ; |
212 | 11.9k | } ; |
213 | | |
214 | 11.9k | return 1 ; |
215 | 11.9k | } /* gsm610_wav_decode_block */ |
216 | | |
217 | | static int |
218 | | gsm610_decode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) |
219 | 174k | { int k ; |
220 | | |
221 | 174k | pgsm610->blockcount ++ ; |
222 | 174k | pgsm610->samplecount = 0 ; |
223 | | |
224 | 174k | if (pgsm610->blockcount > pgsm610->blocks) |
225 | 120 | { memset (pgsm610->samples, 0, sizeof (pgsm610->samples)) ; |
226 | 120 | return 1 ; |
227 | 174k | } ; |
228 | | |
229 | 174k | if ((k = (int) psf_fread (pgsm610->block, 1, GSM610_BLOCKSIZE, psf)) != GSM610_BLOCKSIZE) |
230 | 209 | psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, GSM610_BLOCKSIZE) ; |
231 | | |
232 | 174k | if (gsm_decode (pgsm610->gsm_data, pgsm610->block, pgsm610->samples) < 0) |
233 | 167k | { psf_log_printf (psf, "Error from standard gsm_decode() on frame : %d\n", pgsm610->blockcount) ; |
234 | 167k | return 0 ; |
235 | 167k | } ; |
236 | | |
237 | 6.97k | return 1 ; |
238 | 174k | } /* gsm610_decode_block */ |
239 | | |
240 | | static int |
241 | | gsm610_read_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610, short *ptr, int len) |
242 | 760k | { int count, total = 0, indx = 0 ; |
243 | | |
244 | 1.70M | while (indx < len) |
245 | 943k | { if (pgsm610->blockcount >= pgsm610->blocks && pgsm610->samplecount >= pgsm610->samplesperblock) |
246 | 991 | { memset (ptr + indx, 0, (len - indx) * sizeof (short)) ; |
247 | 991 | return total ; |
248 | 942k | } ; |
249 | | |
250 | 942k | if (pgsm610->samplecount >= pgsm610->samplesperblock) |
251 | 185k | pgsm610->decode_block (psf, pgsm610) ; |
252 | | |
253 | 942k | count = pgsm610->samplesperblock - pgsm610->samplecount ; |
254 | 942k | count = (len - indx > count) ? count : len - indx ; |
255 | | |
256 | 942k | memcpy (&(ptr [indx]), &(pgsm610->samples [pgsm610->samplecount]), count * sizeof (short)) ; |
257 | 942k | indx += count ; |
258 | 942k | pgsm610->samplecount += count ; |
259 | 942k | total = indx ; |
260 | 942k | } ; |
261 | | |
262 | 759k | return total ; |
263 | 760k | } /* gsm610_read_block */ |
264 | | |
265 | | static sf_count_t |
266 | | gsm610_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) |
267 | 552 | { GSM610_PRIVATE *pgsm610 ; |
268 | 552 | int readcount, count ; |
269 | 552 | sf_count_t total = 0 ; |
270 | | |
271 | 552 | if (psf->codec_data == NULL) |
272 | 0 | return 0 ; |
273 | 552 | pgsm610 = (GSM610_PRIVATE*) psf->codec_data ; |
274 | | |
275 | 1.00k | while (len > 0) |
276 | 552 | { readcount = (len > 0x10000000) ? 0x1000000 : (int) len ; |
277 | | |
278 | 552 | count = gsm610_read_block (psf, pgsm610, ptr, readcount) ; |
279 | | |
280 | 552 | total += count ; |
281 | 552 | len -= count ; |
282 | | |
283 | 552 | if (count != readcount) |
284 | 103 | break ; |
285 | 552 | } ; |
286 | | |
287 | 552 | return total ; |
288 | 552 | } /* gsm610_read_s */ |
289 | | |
290 | | static sf_count_t |
291 | | gsm610_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) |
292 | 925 | { GSM610_PRIVATE *pgsm610 ; |
293 | 925 | BUF_UNION ubuf ; |
294 | 925 | short *sptr ; |
295 | 925 | int k, bufferlen, readcount = 0, count ; |
296 | 925 | sf_count_t total = 0 ; |
297 | | |
298 | 925 | if (psf->codec_data == NULL) |
299 | 0 | return 0 ; |
300 | 925 | pgsm610 = (GSM610_PRIVATE*) psf->codec_data ; |
301 | | |
302 | 925 | sptr = ubuf.sbuf ; |
303 | 925 | bufferlen = ARRAY_LEN (ubuf.sbuf) ; |
304 | 2.90k | while (len > 0) |
305 | 1.98k | { readcount = (len >= bufferlen) ? bufferlen : (int) len ; |
306 | 1.98k | count = gsm610_read_block (psf, pgsm610, sptr, readcount) ; |
307 | 4.64M | for (k = 0 ; k < readcount ; k++) |
308 | 4.64M | ptr [total + k] = arith_shift_left (sptr [k], 16) ; |
309 | | |
310 | 1.98k | total += count ; |
311 | 1.98k | len -= readcount ; |
312 | 1.98k | } ; |
313 | 925 | return total ; |
314 | 925 | } /* gsm610_read_i */ |
315 | | |
316 | | static sf_count_t |
317 | | gsm610_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) |
318 | 755k | { GSM610_PRIVATE *pgsm610 ; |
319 | 755k | BUF_UNION ubuf ; |
320 | 755k | short *sptr ; |
321 | 755k | int k, bufferlen, readcount = 0, count ; |
322 | 755k | sf_count_t total = 0 ; |
323 | 755k | float normfact ; |
324 | | |
325 | 755k | if (psf->codec_data == NULL) |
326 | 0 | return 0 ; |
327 | 755k | pgsm610 = (GSM610_PRIVATE*) psf->codec_data ; |
328 | | |
329 | 755k | normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ; |
330 | | |
331 | 755k | sptr = ubuf.sbuf ; |
332 | 755k | bufferlen = ARRAY_LEN (ubuf.sbuf) ; |
333 | 1.51M | while (len > 0) |
334 | 755k | { readcount = (len >= bufferlen) ? bufferlen : (int) len ; |
335 | 755k | count = gsm610_read_block (psf, pgsm610, sptr, readcount) ; |
336 | 1.85M | for (k = 0 ; k < readcount ; k++) |
337 | 1.09M | ptr [total + k] = normfact * sptr [k] ; |
338 | | |
339 | 755k | total += count ; |
340 | 755k | len -= readcount ; |
341 | 755k | } ; |
342 | 755k | return total ; |
343 | 755k | } /* gsm610_read_f */ |
344 | | |
345 | | static sf_count_t |
346 | | gsm610_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) |
347 | 833 | { GSM610_PRIVATE *pgsm610 ; |
348 | 833 | BUF_UNION ubuf ; |
349 | 833 | short *sptr ; |
350 | 833 | int k, bufferlen, readcount = 0, count ; |
351 | 833 | sf_count_t total = 0 ; |
352 | 833 | double normfact ; |
353 | | |
354 | 833 | normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ; |
355 | | |
356 | 833 | if (psf->codec_data == NULL) |
357 | 0 | return 0 ; |
358 | 833 | pgsm610 = (GSM610_PRIVATE*) psf->codec_data ; |
359 | | |
360 | 833 | sptr = ubuf.sbuf ; |
361 | 833 | bufferlen = ARRAY_LEN (ubuf.sbuf) ; |
362 | 3.62k | while (len > 0) |
363 | 2.78k | { readcount = (len >= bufferlen) ? bufferlen : (int) len ; |
364 | 2.78k | count = gsm610_read_block (psf, pgsm610, sptr, readcount) ; |
365 | 8.66M | for (k = 0 ; k < readcount ; k++) |
366 | 8.66M | ptr [total + k] = normfact * sptr [k] ; |
367 | | |
368 | 2.78k | total += count ; |
369 | 2.78k | len -= readcount ; |
370 | 2.78k | } ; |
371 | 833 | return total ; |
372 | 833 | } /* gsm610_read_d */ |
373 | | |
374 | | static sf_count_t |
375 | | gsm610_seek (SF_PRIVATE *psf, int UNUSED (mode), sf_count_t offset) |
376 | 0 | { GSM610_PRIVATE *pgsm610 ; |
377 | 0 | int newblock, newsample ; |
378 | |
|
379 | 0 | if (psf->codec_data == NULL) |
380 | 0 | return 0 ; |
381 | 0 | pgsm610 = (GSM610_PRIVATE*) psf->codec_data ; |
382 | |
|
383 | 0 | if (psf->dataoffset < 0) |
384 | 0 | { psf->error = SFE_BAD_SEEK ; |
385 | 0 | return PSF_SEEK_ERROR ; |
386 | 0 | } ; |
387 | |
|
388 | 0 | if (offset == 0) |
389 | 0 | { int true_flag = 1 ; |
390 | |
|
391 | 0 | psf_fseek (psf, psf->dataoffset, SEEK_SET) ; |
392 | 0 | pgsm610->blockcount = 0 ; |
393 | |
|
394 | 0 | gsm_init (pgsm610->gsm_data) ; |
395 | 0 | if ((SF_CONTAINER (psf->sf.format)) == SF_FORMAT_WAV || |
396 | 0 | (SF_CONTAINER (psf->sf.format)) == SF_FORMAT_W64) |
397 | 0 | gsm_option (pgsm610->gsm_data, GSM_OPT_WAV49, &true_flag) ; |
398 | |
|
399 | 0 | pgsm610->decode_block (psf, pgsm610) ; |
400 | 0 | pgsm610->samplecount = 0 ; |
401 | 0 | return 0 ; |
402 | 0 | } ; |
403 | |
|
404 | 0 | if (offset < 0 || offset > pgsm610->blocks * pgsm610->samplesperblock) |
405 | 0 | { psf->error = SFE_BAD_SEEK ; |
406 | 0 | return PSF_SEEK_ERROR ; |
407 | 0 | } ; |
408 | |
|
409 | 0 | newblock = offset / pgsm610->samplesperblock ; |
410 | 0 | newsample = offset % pgsm610->samplesperblock ; |
411 | |
|
412 | 0 | if (psf->file.mode == SFM_READ) |
413 | 0 | { if (psf->read_current != newblock * pgsm610->samplesperblock + newsample) |
414 | 0 | { psf_fseek (psf, psf->dataoffset + newblock * pgsm610->samplesperblock, SEEK_SET) ; |
415 | 0 | pgsm610->blockcount = newblock ; |
416 | 0 | pgsm610->decode_block (psf, pgsm610) ; |
417 | 0 | pgsm610->samplecount = newsample ; |
418 | 0 | } ; |
419 | |
|
420 | 0 | return newblock * pgsm610->samplesperblock + newsample ; |
421 | 0 | } ; |
422 | | |
423 | | /* What to do about write??? */ |
424 | 0 | psf->error = SFE_BAD_SEEK ; |
425 | 0 | return PSF_SEEK_ERROR ; |
426 | 0 | } /* gsm610_seek */ |
427 | | |
428 | | /*========================================================================================== |
429 | | ** GSM 6.10 Write Functions. |
430 | | */ |
431 | | |
432 | | static int |
433 | | gsm610_encode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) |
434 | 0 | { int k ; |
435 | | |
436 | | /* Encode the samples. */ |
437 | 0 | gsm_encode (pgsm610->gsm_data, pgsm610->samples, pgsm610->block) ; |
438 | | |
439 | | /* Write the block to disk. */ |
440 | 0 | if ((k = (int) psf_fwrite (pgsm610->block, 1, GSM610_BLOCKSIZE, psf)) != GSM610_BLOCKSIZE) |
441 | 0 | psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, GSM610_BLOCKSIZE) ; |
442 | |
|
443 | 0 | pgsm610->samplecount = 0 ; |
444 | 0 | pgsm610->blockcount ++ ; |
445 | | |
446 | | /* Set samples to zero for next block. */ |
447 | 0 | memset (pgsm610->samples, 0, sizeof (pgsm610->samples)) ; |
448 | |
|
449 | 0 | return 1 ; |
450 | 0 | } /* gsm610_encode_block */ |
451 | | |
452 | | static int |
453 | | gsm610_wav_encode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) |
454 | 0 | { int k ; |
455 | | |
456 | | /* Encode the samples. */ |
457 | 0 | gsm_encode (pgsm610->gsm_data, pgsm610->samples, pgsm610->block) ; |
458 | 0 | gsm_encode (pgsm610->gsm_data, pgsm610->samples+WAVLIKE_GSM610_SAMPLES / 2, pgsm610->block+WAVLIKE_GSM610_BLOCKSIZE / 2) ; |
459 | | |
460 | | /* Write the block to disk. */ |
461 | 0 | if ((k = (int) psf_fwrite (pgsm610->block, 1, WAVLIKE_GSM610_BLOCKSIZE, psf)) != WAVLIKE_GSM610_BLOCKSIZE) |
462 | 0 | psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, WAVLIKE_GSM610_BLOCKSIZE) ; |
463 | |
|
464 | 0 | pgsm610->samplecount = 0 ; |
465 | 0 | pgsm610->blockcount ++ ; |
466 | | |
467 | | /* Set samples to zero for next block. */ |
468 | 0 | memset (pgsm610->samples, 0, sizeof (pgsm610->samples)) ; |
469 | |
|
470 | 0 | return 1 ; |
471 | 0 | } /* gsm610_wav_encode_block */ |
472 | | |
473 | | static int |
474 | | gsm610_write_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610, const short *ptr, int len) |
475 | 0 | { int count, total = 0, indx = 0 ; |
476 | |
|
477 | 0 | while (indx < len) |
478 | 0 | { count = pgsm610->samplesperblock - pgsm610->samplecount ; |
479 | |
|
480 | 0 | if (count > len - indx) |
481 | 0 | count = len - indx ; |
482 | |
|
483 | 0 | memcpy (&(pgsm610->samples [pgsm610->samplecount]), &(ptr [indx]), count * sizeof (short)) ; |
484 | 0 | indx += count ; |
485 | 0 | pgsm610->samplecount += count ; |
486 | 0 | total = indx ; |
487 | |
|
488 | 0 | if (pgsm610->samplecount >= pgsm610->samplesperblock) |
489 | 0 | pgsm610->encode_block (psf, pgsm610) ; |
490 | 0 | } ; |
491 | |
|
492 | 0 | return total ; |
493 | 0 | } /* gsm610_write_block */ |
494 | | |
495 | | static sf_count_t |
496 | | gsm610_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len) |
497 | 0 | { GSM610_PRIVATE *pgsm610 ; |
498 | 0 | int writecount, count ; |
499 | 0 | sf_count_t total = 0 ; |
500 | |
|
501 | 0 | if (psf->codec_data == NULL) |
502 | 0 | return 0 ; |
503 | 0 | pgsm610 = (GSM610_PRIVATE*) psf->codec_data ; |
504 | |
|
505 | 0 | while (len > 0) |
506 | 0 | { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ; |
507 | |
|
508 | 0 | count = gsm610_write_block (psf, pgsm610, ptr, writecount) ; |
509 | |
|
510 | 0 | total += count ; |
511 | 0 | len -= count ; |
512 | |
|
513 | 0 | if (count != writecount) |
514 | 0 | break ; |
515 | 0 | } ; |
516 | |
|
517 | 0 | return total ; |
518 | 0 | } /* gsm610_write_s */ |
519 | | |
520 | | static sf_count_t |
521 | | gsm610_write_i (SF_PRIVATE *psf, const int *ptr, sf_count_t len) |
522 | 0 | { GSM610_PRIVATE *pgsm610 ; |
523 | 0 | BUF_UNION ubuf ; |
524 | 0 | short *sptr ; |
525 | 0 | int k, bufferlen, writecount = 0, count ; |
526 | 0 | sf_count_t total = 0 ; |
527 | |
|
528 | 0 | if (psf->codec_data == NULL) |
529 | 0 | return 0 ; |
530 | 0 | pgsm610 = (GSM610_PRIVATE*) psf->codec_data ; |
531 | |
|
532 | 0 | sptr = ubuf.sbuf ; |
533 | 0 | bufferlen = ARRAY_LEN (ubuf.sbuf) ; |
534 | 0 | while (len > 0) |
535 | 0 | { writecount = (len >= bufferlen) ? bufferlen : (int) len ; |
536 | 0 | for (k = 0 ; k < writecount ; k++) |
537 | 0 | sptr [k] = ptr [total + k] >> 16 ; |
538 | 0 | count = gsm610_write_block (psf, pgsm610, sptr, writecount) ; |
539 | |
|
540 | 0 | total += count ; |
541 | 0 | len -= writecount ; |
542 | 0 | } ; |
543 | 0 | return total ; |
544 | 0 | } /* gsm610_write_i */ |
545 | | |
546 | | static sf_count_t |
547 | | gsm610_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) |
548 | 0 | { GSM610_PRIVATE *pgsm610 ; |
549 | 0 | BUF_UNION ubuf ; |
550 | 0 | short *sptr ; |
551 | 0 | int k, bufferlen, writecount = 0, count ; |
552 | 0 | sf_count_t total = 0 ; |
553 | 0 | float normfact ; |
554 | |
|
555 | 0 | if (psf->codec_data == NULL) |
556 | 0 | return 0 ; |
557 | 0 | pgsm610 = (GSM610_PRIVATE*) psf->codec_data ; |
558 | |
|
559 | 0 | normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ; |
560 | |
|
561 | 0 | sptr = ubuf.sbuf ; |
562 | 0 | bufferlen = ARRAY_LEN (ubuf.sbuf) ; |
563 | 0 | while (len > 0) |
564 | 0 | { writecount = (len >= bufferlen) ? bufferlen : (int) len ; |
565 | 0 | for (k = 0 ; k < writecount ; k++) |
566 | 0 | sptr [k] = psf_lrintf (normfact * ptr [total + k]) ; |
567 | 0 | count = gsm610_write_block (psf, pgsm610, sptr, writecount) ; |
568 | |
|
569 | 0 | total += count ; |
570 | 0 | len -= writecount ; |
571 | 0 | if (count != writecount) |
572 | 0 | break ; |
573 | 0 | } ; |
574 | 0 | return total ; |
575 | 0 | } /* gsm610_write_f */ |
576 | | |
577 | | static sf_count_t |
578 | | gsm610_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len) |
579 | 0 | { GSM610_PRIVATE *pgsm610 ; |
580 | 0 | BUF_UNION ubuf ; |
581 | 0 | short *sptr ; |
582 | 0 | int k, bufferlen, writecount = 0, count ; |
583 | 0 | sf_count_t total = 0 ; |
584 | 0 | double normfact ; |
585 | |
|
586 | 0 | if (psf->codec_data == NULL) |
587 | 0 | return 0 ; |
588 | 0 | pgsm610 = (GSM610_PRIVATE*) psf->codec_data ; |
589 | |
|
590 | 0 | normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ; |
591 | |
|
592 | 0 | sptr = ubuf.sbuf ; |
593 | 0 | bufferlen = ARRAY_LEN (ubuf.sbuf) ; |
594 | 0 | while (len > 0) |
595 | 0 | { writecount = (len >= bufferlen) ? bufferlen : (int) len ; |
596 | 0 | for (k = 0 ; k < writecount ; k++) |
597 | 0 | sptr [k] = psf_lrint (normfact * ptr [total + k]) ; |
598 | 0 | count = gsm610_write_block (psf, pgsm610, sptr, writecount) ; |
599 | |
|
600 | 0 | total += count ; |
601 | 0 | len -= writecount ; |
602 | 0 | } ; |
603 | 0 | return total ; |
604 | 0 | } /* gsm610_write_d */ |
605 | | |
606 | | static int |
607 | | gsm610_close (SF_PRIVATE *psf) |
608 | 1.03k | { GSM610_PRIVATE *pgsm610 ; |
609 | | |
610 | 1.03k | if (psf->codec_data == NULL) |
611 | 0 | return 0 ; |
612 | | |
613 | 1.03k | pgsm610 = (GSM610_PRIVATE*) psf->codec_data ; |
614 | | |
615 | 1.03k | if (psf->file.mode == SFM_WRITE) |
616 | 0 | { /* If a block has been partially assembled, write it out |
617 | | ** as the final block. |
618 | | */ |
619 | |
|
620 | 0 | if (pgsm610->samplecount && pgsm610->samplecount < pgsm610->samplesperblock) |
621 | 0 | pgsm610->encode_block (psf, pgsm610) ; |
622 | 0 | } ; |
623 | | |
624 | 1.03k | if (pgsm610->gsm_data) |
625 | 1.03k | gsm_destroy (pgsm610->gsm_data) ; |
626 | | |
627 | 1.03k | return 0 ; |
628 | 1.03k | } /* gsm610_close */ |
629 | | |