]> code.delx.au - pulseaudio/blob - polyp/sound-file.c
Make the whole stuff LGPL only
[pulseaudio] / polyp / sound-file.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <assert.h>
28
29 #include <sndfile.h>
30
31 #include "sound-file.h"
32 #include "sample.h"
33 #include "log.h"
34
35 #define MAX_FILE_SIZE (1024*1024)
36
37 int pa_sound_file_load(const char *fname, struct pa_sample_spec *ss, struct pa_memchunk *chunk, struct pa_memblock_stat *s) {
38 SNDFILE*sf = NULL;
39 SF_INFO sfinfo;
40 int ret = -1;
41 size_t l;
42 sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames);
43 assert(fname && ss && chunk);
44
45 chunk->memblock = NULL;
46 chunk->index = chunk->length = 0;
47
48 memset(&sfinfo, 0, sizeof(sfinfo));
49
50 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
51 pa_log(__FILE__": Failed to open file %s\n", fname);
52 goto finish;
53 }
54
55 switch (sfinfo.format & 0xFF) {
56 case SF_FORMAT_PCM_16:
57 case SF_FORMAT_PCM_U8:
58 case SF_FORMAT_ULAW:
59 case SF_FORMAT_ALAW:
60 ss->format = PA_SAMPLE_S16NE;
61 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
62 break;
63 case SF_FORMAT_FLOAT:
64 default:
65 ss->format = PA_SAMPLE_FLOAT32NE;
66 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
67 break;
68 }
69
70 ss->rate = sfinfo.samplerate;
71 ss->channels = sfinfo.channels;
72
73 if (!pa_sample_spec_valid(ss)) {
74 pa_log(__FILE__": Unsupported sample format in file %s\n", fname);
75 goto finish;
76 }
77
78 if ((l = pa_frame_size(ss)*sfinfo.frames) > MAX_FILE_SIZE) {
79 pa_log(__FILE__": File to large\n");
80 goto finish;
81 }
82
83 chunk->memblock = pa_memblock_new(l, s);
84 assert(chunk->memblock);
85 chunk->index = 0;
86 chunk->length = l;
87
88 if (readf_function(sf, chunk->memblock->data, sfinfo.frames) != sfinfo.frames) {
89 pa_log(__FILE__": Premature file end\n");
90 goto finish;
91 }
92
93 ret = 0;
94
95 finish:
96
97 if (sf)
98 sf_close(sf);
99
100 if (ret != 0 && chunk->memblock)
101 pa_memblock_unref(chunk->memblock);
102
103 return ret;
104
105 }