Coverage Report

Created: 2026-01-09 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libsndfile/src/gsm610.c
Line
Count
Source
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
130k
#define GSM610_BLOCKSIZE    33
33
246
#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
635
{ GSM610_PRIVATE  *pgsm610 ;
80
635
  int   true_flag = 1 ;
81
82
635
  if (psf->codec_data != NULL)
83
0
  { psf_log_printf (psf, "*** psf->codec_data is not NULL.\n") ;
84
0
    return SFE_INTERNAL ;
85
635
    } ;
86
87
635
  if (psf->file.mode == SFM_RDWR)
88
0
    return SFE_BAD_MODE_RW ;
89
90
635
  psf->sf.seekable = SF_FALSE ;
91
92
635
  if ((pgsm610 = calloc (1, sizeof (GSM610_PRIVATE))) == NULL)
93
0
    return SFE_MALLOC_FAILED ;
94
95
635
  psf->codec_data = pgsm610 ;
96
97
635
  memset (pgsm610, 0, sizeof (GSM610_PRIVATE)) ;
98
99
/*============================================================
100
101
Need separate gsm_data structs for encode and decode.
102
103
============================================================*/
104
105
635
  if ((pgsm610->gsm_data = gsm_create ()) == NULL)
106
0
    return SFE_MALLOC_FAILED ;
107
108
635
  switch (SF_CONTAINER (psf->sf.format))
109
635
  { case SF_FORMAT_WAV :
110
356
    case SF_FORMAT_WAVEX :
111
389
    case SF_FORMAT_W64 :
112
389
      gsm_option (pgsm610->gsm_data, GSM_OPT_WAV49, &true_flag) ;
113
114
389
      pgsm610->encode_block = gsm610_wav_encode_block ;
115
389
      pgsm610->decode_block = gsm610_wav_decode_block ;
116
117
389
      pgsm610->samplesperblock = WAVLIKE_GSM610_SAMPLES ;
118
389
      pgsm610->blocksize = WAVLIKE_GSM610_BLOCKSIZE ;
119
389
      break ;
120
121
246
    case SF_FORMAT_AIFF :
122
246
    case SF_FORMAT_RAW :
123
246
      pgsm610->encode_block = gsm610_encode_block ;
124
246
      pgsm610->decode_block = gsm610_decode_block ;
125
126
246
      pgsm610->samplesperblock = GSM610_SAMPLES ;
127
246
      pgsm610->blocksize = GSM610_BLOCKSIZE ;
128
246
      break ;
129
130
0
    default :
131
0
      return SFE_INTERNAL ;
132
0
      break ;
133
635
    } ;
134
135
635
  if (psf->file.mode == SFM_READ)
136
635
  { if (psf->datalength % pgsm610->blocksize == 0)
137
16
      pgsm610->blocks = psf->datalength / pgsm610->blocksize ;
138
619
    else if (psf->datalength % pgsm610->blocksize == 1 && pgsm610->blocksize == GSM610_BLOCKSIZE)
139
6
    { /*
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
6
      pgsm610->blocks = psf->datalength / pgsm610->blocksize ;
147
6
      }
148
613
    else
149
613
    { psf_log_printf (psf, "*** Warning : data chunk seems to be truncated.\n") ;
150
613
      pgsm610->blocks = psf->datalength / pgsm610->blocksize + 1 ;
151
613
      } ;
152
153
635
    psf->sf.frames = (sf_count_t) pgsm610->samplesperblock * pgsm610->blocks ;
154
155
635
    psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
156
157
635
    pgsm610->decode_block (psf, pgsm610) ;  /* Read first block. */
158
159
635
    psf->read_short   = gsm610_read_s ;
160
635
    psf->read_int   = gsm610_read_i ;
161
635
    psf->read_float   = gsm610_read_f ;
162
635
    psf->read_double  = gsm610_read_d ;
163
635
    } ;
164
165
635
  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
635
  psf->codec_close = gsm610_close ;
176
177
635
  psf->seek = gsm610_seek ;
178
179
635
  psf->filelength = psf_get_filelen (psf) ;
180
635
  psf->datalength = psf->filelength - psf->dataoffset ;
181
182
635
  return 0 ;
183
635
} /* 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
57.6k
{ int k ;
192
193
57.6k
  pgsm610->blockcount ++ ;
194
57.6k
  pgsm610->samplecount = 0 ;
195
196
57.6k
  if (pgsm610->blockcount > pgsm610->blocks)
197
35
  { memset (pgsm610->samples, 0, sizeof (pgsm610->samples)) ;
198
35
    return 1 ;
199
57.5k
    } ;
200
201
57.5k
  if ((k = (int) psf_fread (pgsm610->block, 1, WAVLIKE_GSM610_BLOCKSIZE, psf)) != WAVLIKE_GSM610_BLOCKSIZE)
202
351
    psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, WAVLIKE_GSM610_BLOCKSIZE) ;
203
204
57.5k
  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
57.5k
    } ;
208
209
57.5k
  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
57.5k
    } ;
213
214
57.5k
  return 1 ;
215
57.5k
} /* gsm610_wav_decode_block */
216
217
static int
218
gsm610_decode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610)
219
65.1k
{ int k ;
220
221
65.1k
  pgsm610->blockcount ++ ;
222
65.1k
  pgsm610->samplecount = 0 ;
223
224
65.1k
  if (pgsm610->blockcount > pgsm610->blocks)
225
65
  { memset (pgsm610->samples, 0, sizeof (pgsm610->samples)) ;
226
65
    return 1 ;
227
65.0k
    } ;
228
229
65.0k
  if ((k = (int) psf_fread (pgsm610->block, 1, GSM610_BLOCKSIZE, psf)) != GSM610_BLOCKSIZE)
230
124
    psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, GSM610_BLOCKSIZE) ;
231
232
65.0k
  if (gsm_decode (pgsm610->gsm_data, pgsm610->block, pgsm610->samples) < 0)
233
60.6k
  { psf_log_printf (psf, "Error from standard gsm_decode() on frame : %d\n", pgsm610->blockcount) ;
234
60.6k
    return 0 ;
235
60.6k
    } ;
236
237
4.48k
  return 1 ;
238
65.0k
} /* gsm610_decode_block */
239
240
static int
241
gsm610_read_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610, short *ptr, int len)
242
4.34k
{ int count, total = 0, indx = 0 ;
243
244
130k
  while (indx < len)
245
126k
  { if (pgsm610->blockcount >= pgsm610->blocks && pgsm610->samplecount >= pgsm610->samplesperblock)
246
1.08k
    { memset (ptr + indx, 0, (len - indx) * sizeof (short)) ;
247
1.08k
      return total ;
248
125k
      } ;
249
250
125k
    if (pgsm610->samplecount >= pgsm610->samplesperblock)
251
122k
      pgsm610->decode_block (psf, pgsm610) ;
252
253
125k
    count = pgsm610->samplesperblock - pgsm610->samplecount ;
254
125k
    count = (len - indx > count) ? count : len - indx ;
255
256
125k
    memcpy (&(ptr [indx]), &(pgsm610->samples [pgsm610->samplecount]), count * sizeof (short)) ;
257
125k
    indx += count ;
258
125k
    pgsm610->samplecount += count ;
259
125k
    total = indx ;
260
125k
    } ;
261
262
3.25k
  return total ;
263
4.34k
} /* gsm610_read_block */
264
265
static sf_count_t
266
gsm610_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
267
704
{ GSM610_PRIVATE  *pgsm610 ;
268
704
  int     readcount, count ;
269
704
  sf_count_t  total = 0 ;
270
271
704
  if (psf->codec_data == NULL)
272
0
    return 0 ;
273
704
  pgsm610 = (GSM610_PRIVATE*) psf->codec_data ;
274
275
1.27k
  while (len > 0)
276
704
  { readcount = (len > 0x10000000) ? 0x1000000 : (int) len ;
277
278
704
    count = gsm610_read_block (psf, pgsm610, ptr, readcount) ;
279
280
704
    total += count ;
281
704
    len -= count ;
282
283
704
    if (count != readcount)
284
136
      break ;
285
704
    } ;
286
287
704
  return total ;
288
704
} /* gsm610_read_s */
289
290
static sf_count_t
291
gsm610_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
292
846
{ GSM610_PRIVATE *pgsm610 ;
293
846
  BUF_UNION ubuf ;
294
846
  short   *sptr ;
295
846
  int     k, bufferlen, readcount = 0, count ;
296
846
  sf_count_t  total = 0 ;
297
298
846
  if (psf->codec_data == NULL)
299
0
    return 0 ;
300
846
  pgsm610 = (GSM610_PRIVATE*) psf->codec_data ;
301
302
846
  sptr = ubuf.sbuf ;
303
846
  bufferlen = ARRAY_LEN (ubuf.sbuf) ;
304
2.62k
  while (len > 0)
305
1.78k
  { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
306
1.78k
    count = gsm610_read_block (psf, pgsm610, sptr, readcount) ;
307
4.14M
    for (k = 0 ; k < readcount ; k++)
308
4.14M
      ptr [total + k] = arith_shift_left (sptr [k], 16) ;
309
310
1.78k
    total += count ;
311
1.78k
    len -= readcount ;
312
1.78k
    } ;
313
846
  return total ;
314
846
} /* gsm610_read_i */
315
316
static sf_count_t
317
gsm610_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
318
0
{ GSM610_PRIVATE *pgsm610 ;
319
0
  BUF_UNION ubuf ;
320
0
  short   *sptr ;
321
0
  int     k, bufferlen, readcount = 0, count ;
322
0
  sf_count_t  total = 0 ;
323
0
  float   normfact ;
324
325
0
  if (psf->codec_data == NULL)
326
0
    return 0 ;
327
0
  pgsm610 = (GSM610_PRIVATE*) psf->codec_data ;
328
329
0
  normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
330
331
0
  sptr = ubuf.sbuf ;
332
0
  bufferlen = ARRAY_LEN (ubuf.sbuf) ;
333
0
  while (len > 0)
334
0
  { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
335
0
    count = gsm610_read_block (psf, pgsm610, sptr, readcount) ;
336
0
    for (k = 0 ; k < readcount ; k++)
337
0
      ptr [total + k] = normfact * sptr [k] ;
338
339
0
    total += count ;
340
0
    len -= readcount ;
341
0
    } ;
342
0
  return total ;
343
0
} /* gsm610_read_f */
344
345
static sf_count_t
346
gsm610_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
347
958
{ GSM610_PRIVATE *pgsm610 ;
348
958
  BUF_UNION ubuf ;
349
958
  short   *sptr ;
350
958
  int     k, bufferlen, readcount = 0, count ;
351
958
  sf_count_t  total = 0 ;
352
958
  double    normfact ;
353
354
958
  normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
355
356
958
  if (psf->codec_data == NULL)
357
0
    return 0 ;
358
958
  pgsm610 = (GSM610_PRIVATE*) psf->codec_data ;
359
360
958
  sptr = ubuf.sbuf ;
361
958
  bufferlen = ARRAY_LEN (ubuf.sbuf) ;
362
2.81k
  while (len > 0)
363
1.85k
  { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
364
1.85k
    count = gsm610_read_block (psf, pgsm610, sptr, readcount) ;
365
3.97M
    for (k = 0 ; k < readcount ; k++)
366
3.97M
      ptr [total + k] = normfact * sptr [k] ;
367
368
1.85k
    total += count ;
369
1.85k
    len -= readcount ;
370
1.85k
    } ;
371
958
  return total ;
372
958
} /* 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
635
{ GSM610_PRIVATE *pgsm610 ;
609
610
635
  if (psf->codec_data == NULL)
611
0
    return 0 ;
612
613
635
  pgsm610 = (GSM610_PRIVATE*) psf->codec_data ;
614
615
635
  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
635
  if (pgsm610->gsm_data)
625
635
    gsm_destroy (pgsm610->gsm_data) ;
626
627
635
  return 0 ;
628
635
} /* gsm610_close */
629