File: sndfile.c

package info (click to toggle)
rat 4.2.22-2.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,896 kB
  • ctags: 3,717
  • sloc: ansic: 36,542; tcl: 2,740; sh: 2,675; makefile: 295
file content (342 lines) | stat: -rw-r--r-- 8,669 bytes parent folder | download | duplicates (5)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 * FILE:    sndfile.c
 * PROGRAM: RAT
 * AUTHOR:  Orion Hodson
 *
 * Copyright (c) 1998-2001 University College London
 * All rights reserved.
 */
 
#ifndef HIDE_SOURCE_STRINGS
static const char cvsid[] = 
	"$Id: sndfile.c,v 1.30 2001/01/08 20:30:08 ucaccsp Exp $";
#endif /* HIDE_SOURCE_STRINGS */

#include "config_unix.h"
#include "config_win32.h"
#include "debug.h"
#include "memory.h"
#include "audio_types.h"
#include "codec_types.h"
#include "codec_g711.h"
#include "sndfile_types.h"
#include "sndfile_au.h"
#include "sndfile_raw.h"
#include "sndfile_wav.h"
#include "sndfile.h"

/* This code uses the same function pointer arrangement as other files in RAT.
 * It is basically just a cumbersome way of implementing virtual functions
 */

/* Generic file handling *********************************************************/

typedef int (*pf_open_hdr)    (FILE *, char **state, sndfile_fmt_t *fmt);
typedef int (*pf_read_audio)  (FILE *, char * state, sample *buf, int samples);
typedef int (*pf_write_hdr)   (FILE *, char **state, const sndfile_fmt_t *);
typedef int (*pf_write_audio) (FILE *, char * state, sample *buf, int samples);
typedef int (*pf_write_end)   (FILE *, char *state);
typedef int (*pf_free_state)  (char **state);
typedef int (*pf_get_format)  (char *state, sndfile_fmt_t *fmt);

typedef struct s_file_handler {
        char name[12];           /* Sound handler name        */
        char extension[12];      /* Recognized file extension */
        pf_open_hdr    open_hdr;
        pf_read_audio  read_audio;
        pf_write_hdr   write_hdr;
        pf_write_audio write_audio;
        pf_write_end   write_end;
        pf_free_state  free_state;
        pf_get_format  get_format;
} sndfile_handler_t;

/* Sound file handlers */

static sndfile_handler_t snd_handlers[] = {
        {"NeXT/Sun", 
         "au", 
         sun_read_hdr, 
         sun_read_audio,
         sun_write_hdr,
         sun_write_audio,
         NULL, /* No post write handling required */
         sun_free_state,
         sun_get_format
        },
        {"MS RIFF",
         "wav",
         riff_read_hdr,
         riff_read_audio,
         riff_write_hdr,
         riff_write_audio,
         riff_write_end,
         riff_free_state,
         riff_get_format
        },
        /* RAW should go last because it'll always succeed if we open a file
         * using this handler and have specified format.
         */
        {"Raw",
         "raw",
         raw_read_hdr,
         raw_read_audio,
         raw_write_hdr,
         raw_write_audio,
         NULL, /* No post write handling required */
         raw_free_state,
         raw_get_format
        }
};

#define NUM_SND_HANDLERS (int)(sizeof(snd_handlers)/sizeof(snd_handlers[0]))

#define SND_ACTION_PLAYING    1
#define SND_ACTION_RECORDING  2
#define SND_ACTION_PAUSED     4

typedef struct s_sndfile {
        FILE *fp;
        char *state;
        sndfile_handler_t *sfh;
        uint32_t action; /* Playing, recording, paused */
} sndfile_t;

int  
snd_read_open (sndfile_t **sndfile, char *path, sndfile_fmt_t *fmt) 
{
        sndfile_t *s;
        FILE       *fp;
        int         i;

        if (*sndfile) {
                debug_msg("File not closed before opening\n");
                snd_read_close(sndfile);
        }
#ifdef WIN32
        fp = fopen(path, "rb");
#else
        fp = fopen(path, "r");
#endif
        if (!fp) {
                debug_msg("Could not open %s\n",path);
                return FALSE;
        }
        
        s     = (sndfile_t*)xmalloc(sizeof(sndfile_t));
        if (!s) return FALSE;
        
        s->fp = fp;
        
        for(i = 0; i < NUM_SND_HANDLERS; i++) {
                if (snd_handlers[i].open_hdr(fp,&s->state, fmt)) {
                        s->sfh    = snd_handlers + i;
                        s->action = SND_ACTION_PLAYING;
                        *sndfile = s;
                        return TRUE;
                }
                rewind(fp);
        }

        xfree(s);
        
        return FALSE;
}

int  
snd_read_close(sndfile_t **sf)
{
        sndfile_handler_t *sfh = (*sf)->sfh;

        /* Release state */
        sfh->free_state(&(*sf)->state);

        /* Close file */
        fclose((*sf)->fp);

        /* Release memory */
        xfree(*sf);
        *sf = NULL;

        return TRUE;
}

int
snd_read_audio(sndfile_t **sf, sample *buf, uint16_t samples)
{
        sndfile_handler_t *sfh;
        int samples_read;

        if ((*sf)->action & SND_ACTION_PAUSED) {
		return 0;
	}

        sfh = (*sf)->sfh;
        
        samples_read = sfh->read_audio((*sf)->fp, (*sf)->state, buf, samples);

        if (samples_read != samples) {
                /* Looks like the end of the line */
                memset(buf+samples_read, 0, sizeof(sample) * (samples - samples_read));
                snd_read_close(sf);
        }

        return samples_read;
}

static char *
snd_get_extension(char *path)
{
        if (path) {
                char *ext = path + strlen(path) - 1;
                while(ext > path) {
                        if (*ext == '.') return ext + 1;
                        ext--;
                }
        }
        return NULL;
}

int
snd_write_open (sndfile_t **sf, char *path, char *default_extension, const sndfile_fmt_t *fmt)
{
        sndfile_t *s;
        FILE       *fp;
        int         i;
        char *extension;

        if (*sf) {
                debug_msg("File not closed before opening\n");
                snd_write_close(sf);
        }

        if (fmt == NULL) {
                debug_msg("No format specified\n");
                snd_write_close(sf);
        }

        extension = snd_get_extension(path);
        if (extension == NULL) {
                debug_msg("No extension in file name (%s),using default %s\n", path, default_extension);
                extension = default_extension;
        }

#ifdef WIN32
        fp = fopen(path, "wb");
#else
        fp = fopen(path, "w");
#endif
        if (!fp) {
                debug_msg("Could not open %s\n",path);
                return FALSE;
        }
        
        s     = (sndfile_t*)xmalloc(sizeof(sndfile_t));
        if (!s) return FALSE;

        s->fp = fp;
        
        for(i = 0; i < NUM_SND_HANDLERS; i++) {
                if (!strcasecmp(extension, snd_handlers[i].extension)) {
                        s->sfh    = snd_handlers + i;
                        s->action = SND_ACTION_RECORDING;
                        s->sfh->write_hdr(fp, &s->state, fmt);
                        *sf = s;
                        return TRUE;
                }
        }
        
        xfree(s);
        
        return FALSE;
}

int  
snd_write_close(sndfile_t **pps)
{
        sndfile_t *ps = *pps;

        if (ps->sfh->write_end) ps->sfh->write_end(ps->fp, ps->state);
        
        ps->sfh->free_state(&ps->state);
        
        fclose(ps->fp);
        
        xfree(ps);
        *pps = NULL;
        
        return TRUE;
}

int  
snd_write_audio(sndfile_t **pps, sample *buf, uint16_t buf_len)
{
        sndfile_t *ps = *pps;
        int success;

        if (ps->action & SND_ACTION_PAUSED) return FALSE;

        if (buf_len == 0) {
                /* We succeeded although we didn't write anything */
                return TRUE;
        }

        success = ps->sfh->write_audio(ps->fp, ps->state, buf, buf_len);
        if (!success) {
                debug_msg("Closing file\n");
                snd_write_close(pps);        
                return FALSE;
        }

        return TRUE;
}

int 
snd_get_format(sndfile_t *sf, sndfile_fmt_t *fmt)
{
        return sf->sfh->get_format(sf->state, fmt);
} 

int
snd_valid_format(sndfile_fmt_t *fmt)
{
        if (fmt->channels != 1 && 
            fmt->channels != 2) {
                debug_msg("Invalid channels %d\n", fmt->channels);
                return FALSE;
        }

        if ((fmt->sample_rate % 8000)  != 0 &&
            (fmt->sample_rate % 11025) != 0) {
                debug_msg("Invalid rate %d\n", fmt->sample_rate);
                return FALSE;
        }

        switch (fmt->encoding) {
        case SNDFILE_ENCODING_PCMU:
        case SNDFILE_ENCODING_PCMA:
        case SNDFILE_ENCODING_L8:
        case SNDFILE_ENCODING_L16:
                break;
        default:
                debug_msg("Invalid encoding %d\n", fmt->encoding);
                return FALSE;
        }

        return TRUE;
}

int 
snd_pause(sndfile_t  *sf)
{
        sf->action = sf->action | SND_ACTION_PAUSED;
        return TRUE;
}

int
snd_resume(sndfile_t *sf)
{
        sf->action = sf->action & ~SND_ACTION_PAUSED;
        return TRUE;
}