~ubuntu-branches/ubuntu/gutsy/audacity/gutsy-backports

« back to all changes in this revision

Viewing changes to lib-src/libsndfile/src/gsm610.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2006-11-21 02:38:07 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20061121023807-flsqb0p8gb5a942v
Tags: 1.2.6-0ubuntu1
* New upstream release.
* debian/{control,rules}:
  - Use system portaudio 19 to gain native ALSA and JACK support,
  - Use system soundtouch and samplerate,
  - Suggest the LAME libs (Closes Ubuntu: #51063),
  - Disable resample support.
* {configure,lib-src/Makefile}.in: Build & link against system
  portaudio 19.
* Reinvoke autoconf to fix FTBFS.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
 
2
** Copyright (C) 1999-2006 Erik de Castro Lopo <erikd@mega-nerd.com>
3
3
**
4
4
** This program is free software; you can redistribute it and/or modify
5
5
** it under the terms of the GNU Lesser General Public License as published by
16
16
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
17
*/
18
18
 
19
 
#include "config.h"
 
19
#include "sfconfig.h"
20
20
 
21
21
#include <stdio.h>
22
22
#include <stdlib.h>
43
43
        short                   samples [WAV_W64_GSM610_SAMPLES] ;
44
44
        unsigned char   block [WAV_W64_GSM610_BLOCKSIZE] ;
45
45
 
 
46
        /* Damn I hate typedef-ed pointers; yes, gsm is a pointer type. */
46
47
        gsm                             gsm_data ;
47
48
} GSM610_PRIVATE ;
48
49
 
51
52
static sf_count_t       gsm610_read_f   (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
52
53
static sf_count_t       gsm610_read_d   (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
53
54
 
54
 
static sf_count_t       gsm610_write_s  (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
55
 
static sf_count_t       gsm610_write_i  (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
56
 
static sf_count_t       gsm610_write_f  (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
57
 
static sf_count_t       gsm610_write_d  (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
 
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) ;
58
59
 
59
60
static  int gsm610_read_block   (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610, short *ptr, int len) ;
60
 
static  int gsm610_write_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) ;
61
62
 
62
63
static  int     gsm610_decode_block     (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) ;
63
64
static  int     gsm610_encode_block     (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) ;
78
79
{       GSM610_PRIVATE  *pgsm610 ;
79
80
        int             true_flag = 1 ;
80
81
 
 
82
        if (psf->fdata != NULL)
 
83
        {       psf_log_printf (psf, "*** psf->fdata is not NULL.\n") ;
 
84
                return SFE_INTERNAL ;
 
85
                } ;
 
86
 
81
87
        if (psf->mode == SFM_RDWR)
82
88
                return SFE_BAD_MODE_RW ;
83
89
 
84
90
        psf->sf.seekable = SF_FALSE ;
85
91
 
86
 
        if (! (pgsm610 = malloc (sizeof (GSM610_PRIVATE))))
 
92
        if ((pgsm610 = calloc (1, sizeof (GSM610_PRIVATE))) == NULL)
87
93
                return SFE_MALLOC_FAILED ;
88
94
 
89
95
        psf->fdata = (void*) pgsm610 ;
96
102
 
97
103
============================================================*/
98
104
 
99
 
        if (! (pgsm610->gsm_data = gsm_create ()))
 
105
        if ((pgsm610->gsm_data = gsm_create ()) == NULL)
100
106
                return SFE_MALLOC_FAILED ;
101
107
 
102
 
        if ((psf->sf.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV ||
103
 
                                (psf->sf.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_W64)
104
 
        {       gsm_option (pgsm610->gsm_data, GSM_OPT_WAV49, &true_flag) ;
105
 
 
106
 
                pgsm610->encode_block = gsm610_wav_encode_block ;
107
 
                pgsm610->decode_block = gsm610_wav_decode_block ;
108
 
 
109
 
                pgsm610->samplesperblock = WAV_W64_GSM610_SAMPLES ;
110
 
                pgsm610->blocksize = WAV_W64_GSM610_BLOCKSIZE ;
111
 
                }
112
 
        else
113
 
        {       pgsm610->encode_block = gsm610_encode_block ;
114
 
                pgsm610->decode_block = gsm610_decode_block ;
115
 
 
116
 
                pgsm610->samplesperblock = GSM610_SAMPLES ;
117
 
                pgsm610->blocksize = GSM610_BLOCKSIZE ;
 
108
        switch (psf->sf.format & SF_FORMAT_TYPEMASK)
 
109
        {       case SF_FORMAT_WAV :
 
110
                case SF_FORMAT_WAVEX :
 
111
                case SF_FORMAT_W64 :
 
112
                        gsm_option (pgsm610->gsm_data, GSM_OPT_WAV49, &true_flag) ;
 
113
 
 
114
                        pgsm610->encode_block = gsm610_wav_encode_block ;
 
115
                        pgsm610->decode_block = gsm610_wav_decode_block ;
 
116
 
 
117
                        pgsm610->samplesperblock = WAV_W64_GSM610_SAMPLES ;
 
118
                        pgsm610->blocksize = WAV_W64_GSM610_BLOCKSIZE ;
 
119
                        break ;
 
120
 
 
121
                case SF_FORMAT_AIFF :
 
122
                case SF_FORMAT_RAW :
 
123
                        pgsm610->encode_block = gsm610_encode_block ;
 
124
                        pgsm610->decode_block = gsm610_decode_block ;
 
125
 
 
126
                        pgsm610->samplesperblock = GSM610_SAMPLES ;
 
127
                        pgsm610->blocksize = GSM610_BLOCKSIZE ;
 
128
                        break ;
 
129
 
 
130
                default :
 
131
                        return SFE_INTERNAL ;
 
132
                        break ;
118
133
                } ;
119
134
 
120
135
        if (psf->mode == SFM_READ)
121
 
        {       if (psf->datalength % pgsm610->blocksize)
 
136
        {       if (psf->datalength % pgsm610->blocksize == 0)
 
137
                        pgsm610->blocks = psf->datalength / pgsm610->blocksize ;
 
138
                else if (psf->datalength % pgsm610->blocksize == 1 && pgsm610->blocksize == GSM610_BLOCKSIZE)
 
139
                {       /*
 
140
                        **      Weird AIFF specific case.
 
141
                        **      AIFF chunks must be at an odd 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
                        pgsm610->blocks = psf->datalength / pgsm610->blocksize ;
 
147
                        }
 
148
                else
122
149
                {       psf_log_printf (psf, "*** Warning : data chunk seems to be truncated.\n") ;
123
150
                        pgsm610->blocks = psf->datalength / pgsm610->blocksize + 1 ;
124
 
                        }
125
 
                else
126
 
                        pgsm610->blocks = psf->datalength / pgsm610->blocksize ;
 
151
                        } ;
127
152
 
128
153
                psf->sf.frames = pgsm610->samplesperblock * pgsm610->blocks ;
129
154
 
145
170
                psf->write_double       = gsm610_write_d ;
146
171
                } ;
147
172
 
148
 
        psf->close = gsm610_close ;
 
173
        psf->codec_close = gsm610_close ;
 
174
 
149
175
        psf->seek = gsm610_seek ;
150
176
 
151
177
        psf->filelength = psf_get_filelen (psf) ;
215
241
 
216
242
        while (indx < len)
217
243
        {       if (pgsm610->blockcount >= pgsm610->blocks && pgsm610->samplecount >= pgsm610->samplesperblock)
218
 
                {       memset (&(ptr [indx]), 0, (size_t) ((len - indx) * sizeof (short))) ;
 
244
                {       memset (&(ptr [indx]), 0, (len - indx) * sizeof (short)) ;
219
245
                        return total ;
220
246
                        } ;
221
247
 
240
266
        int                     readcount, count ;
241
267
        sf_count_t      total = 0 ;
242
268
 
243
 
        if (! psf->fdata)
 
269
        if (psf->fdata == NULL)
244
270
                return 0 ;
245
271
        pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
246
272
 
266
292
        int                     k, bufferlen, readcount = 0, count ;
267
293
        sf_count_t      total = 0 ;
268
294
 
269
 
        if (! psf->fdata)
 
295
        if (psf->fdata == NULL)
270
296
                return 0 ;
271
297
        pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
272
298
 
292
318
        sf_count_t      total = 0 ;
293
319
        float           normfact ;
294
320
 
295
 
        if (! psf->fdata)
 
321
        if (psf->fdata == NULL)
296
322
                return 0 ;
297
323
        pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
298
324
 
322
348
 
323
349
        normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
324
350
 
325
 
        if (! psf->fdata)
 
351
        if (psf->fdata == NULL)
326
352
                return 0 ;
327
353
        pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
328
354
 
347
373
 
348
374
        mode = mode ;
349
375
 
350
 
        if (! psf->fdata)
 
376
        if (psf->fdata == NULL)
351
377
                return 0 ;
352
378
        pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
353
379
 
354
380
        if (psf->dataoffset < 0)
355
381
        {       psf->error = SFE_BAD_SEEK ;
356
 
                return  ((sf_count_t) -1) ;
 
382
                return  PSF_SEEK_ERROR ;
357
383
                } ;
358
384
 
359
385
        if (offset == 0)
374
400
 
375
401
        if (offset < 0 || offset > pgsm610->blocks * pgsm610->samplesperblock)
376
402
        {       psf->error = SFE_BAD_SEEK ;
377
 
                return  ((sf_count_t) -1) ;
 
403
                return  PSF_SEEK_ERROR ;
378
404
                } ;
379
405
 
380
406
        newblock        = offset / pgsm610->samplesperblock ;
393
419
 
394
420
        /* What to do about write??? */
395
421
        psf->error = SFE_BAD_SEEK ;
396
 
        return  ((sf_count_t) -1) ;
 
422
        return  PSF_SEEK_ERROR ;
397
423
} /* gsm610_seek */
398
424
 
399
425
/*==========================================================================================
442
468
} /* gsm610_wav_encode_block */
443
469
 
444
470
static int
445
 
gsm610_write_block      (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610, short *ptr, int len)
 
471
gsm610_write_block      (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610, const short *ptr, int len)
446
472
{       int             count, total = 0, indx = 0 ;
447
473
 
448
474
        while (indx < len)
464
490
} /* gsm610_write_block */
465
491
 
466
492
static sf_count_t
467
 
gsm610_write_s  (SF_PRIVATE *psf, short *ptr, sf_count_t len)
 
493
gsm610_write_s  (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
468
494
{       GSM610_PRIVATE  *pgsm610 ;
469
495
        int                     writecount, count ;
470
496
        sf_count_t      total = 0 ;
471
497
 
472
 
        if (! psf->fdata)
 
498
        if (psf->fdata == NULL)
473
499
                return 0 ;
474
500
        pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
475
501
 
489
515
} /* gsm610_write_s */
490
516
 
491
517
static sf_count_t
492
 
gsm610_write_i  (SF_PRIVATE *psf, int *ptr, sf_count_t len)
 
518
gsm610_write_i  (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
493
519
{       GSM610_PRIVATE *pgsm610 ;
494
520
        short           *sptr ;
495
521
        int                     k, bufferlen, writecount = 0, count ;
496
522
        sf_count_t      total = 0 ;
497
523
 
498
 
        if (! psf->fdata)
 
524
        if (psf->fdata == NULL)
499
525
                return 0 ;
500
526
        pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
501
527
 
514
540
} /* gsm610_write_i */
515
541
 
516
542
static sf_count_t
517
 
gsm610_write_f  (SF_PRIVATE *psf, float *ptr, sf_count_t len)
 
543
gsm610_write_f  (SF_PRIVATE *psf, const float *ptr, sf_count_t len)
518
544
{       GSM610_PRIVATE *pgsm610 ;
519
545
        short           *sptr ;
520
546
        int                     k, bufferlen, writecount = 0, count ;
521
547
        sf_count_t      total = 0 ;
522
548
        float           normfact ;
523
549
 
524
 
        if (! psf->fdata)
 
550
        if (psf->fdata == NULL)
525
551
                return 0 ;
526
552
        pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
527
553
 
542
568
} /* gsm610_write_f */
543
569
 
544
570
static sf_count_t
545
 
gsm610_write_d  (SF_PRIVATE *psf, double *ptr, sf_count_t len)
 
571
gsm610_write_d  (SF_PRIVATE *psf, const double *ptr, sf_count_t len)
546
572
{       GSM610_PRIVATE *pgsm610 ;
547
573
        short           *sptr ;
548
574
        int                     k, bufferlen, writecount = 0, count ;
549
575
        sf_count_t      total = 0 ;
550
576
        double          normfact ;
551
577
 
552
 
        if (! psf->fdata)
 
578
        if (psf->fdata == NULL)
553
579
                return 0 ;
554
580
        pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
555
581
 
573
599
gsm610_close    (SF_PRIVATE *psf)
574
600
{       GSM610_PRIVATE *pgsm610 ;
575
601
 
576
 
        if (! psf->fdata)
 
602
        if (psf->fdata == NULL)
577
603
                return 0 ;
578
604
 
579
605
        pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
585
611
 
586
612
                if (pgsm610->samplecount && pgsm610->samplecount < pgsm610->samplesperblock)
587
613
                        pgsm610->encode_block (psf, pgsm610) ;
588
 
 
589
 
                if (psf->write_header)
590
 
                        psf->write_header (psf, SF_TRUE) ;
591
614
                } ;
592
615
 
593
616
        if (pgsm610->gsm_data)