]> code.delx.au - pulseaudio/blob - src/polypcore/sound-file.c
bd0cf5961fce4b1c05edafa72a8b1aeac216b4d1
[pulseaudio] / src / polypcore / 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 <polyp/sample.h>
32 #include <polypcore/log.h>
33
34 #include "sound-file.h"
35 #include "core-scache.h"
36
37 int pa_sound_file_load(const char *fname, pa_sample_spec *ss, pa_channel_map *map, pa_memchunk *chunk, 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) = NULL;
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", fname);
52 goto finish;
53 }
54
55 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
56 case SF_FORMAT_PCM_16:
57 case SF_FORMAT_PCM_U8:
58 case SF_FORMAT_PCM_S8:
59 ss->format = PA_SAMPLE_S16NE;
60 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
61 break;
62
63 case SF_FORMAT_ULAW:
64 ss->format = PA_SAMPLE_ULAW;
65 break;
66
67 case SF_FORMAT_ALAW:
68 ss->format = PA_SAMPLE_ALAW;
69 break;
70
71 case SF_FORMAT_FLOAT:
72 case SF_FORMAT_DOUBLE:
73 default:
74 ss->format = PA_SAMPLE_FLOAT32NE;
75 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
76 break;
77 }
78
79 ss->rate = sfinfo.samplerate;
80 ss->channels = sfinfo.channels;
81
82 if (!pa_sample_spec_valid(ss)) {
83 pa_log(__FILE__": Unsupported sample format in file %s", fname);
84 goto finish;
85 }
86
87 if (map)
88 pa_channel_map_init_auto(map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
89
90 if ((l = pa_frame_size(ss)*sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
91 pa_log(__FILE__": File too large");
92 goto finish;
93 }
94
95 chunk->memblock = pa_memblock_new(l, s);
96 assert(chunk->memblock);
97 chunk->index = 0;
98 chunk->length = l;
99
100 if ((readf_function && readf_function(sf, chunk->memblock->data, sfinfo.frames) != sfinfo.frames) ||
101 (!readf_function && sf_read_raw(sf, chunk->memblock->data, l) != l)) {
102 pa_log(__FILE__": Premature file end");
103 goto finish;
104 }
105
106 ret = 0;
107
108 finish:
109
110 if (sf)
111 sf_close(sf);
112
113 if (ret != 0 && chunk->memblock)
114 pa_memblock_unref(chunk->memblock);
115
116 return ret;
117
118 }
119
120 int pa_sound_file_too_big_to_cache(const char *fname) {
121 SNDFILE*sf = NULL;
122 SF_INFO sfinfo;
123 pa_sample_spec ss;
124
125 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
126 pa_log(__FILE__": Failed to open file %s", fname);
127 return 0;
128 }
129
130 sf_close(sf);
131
132 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
133 case SF_FORMAT_PCM_16:
134 case SF_FORMAT_PCM_U8:
135 case SF_FORMAT_PCM_S8:
136 ss.format = PA_SAMPLE_S16NE;
137 break;
138
139 case SF_FORMAT_ULAW:
140 ss.format = PA_SAMPLE_ULAW;
141 break;
142
143 case SF_FORMAT_ALAW:
144 ss.format = PA_SAMPLE_ALAW;
145 break;
146
147 case SF_FORMAT_DOUBLE:
148 case SF_FORMAT_FLOAT:
149 default:
150 ss.format = PA_SAMPLE_FLOAT32NE;
151 break;
152 }
153
154 ss.rate = sfinfo.samplerate;
155 ss.channels = sfinfo.channels;
156
157 if ((pa_frame_size(&ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
158 pa_log(__FILE__": File too large %s", fname);
159 return 1;
160 }
161
162 return 0;
163 }