]> code.delx.au - pulseaudio/blob - src/pulsecore/sound-file.c
deal with a possibly failing pa_channel_map_init_auto() correctly
[pulseaudio] / src / pulsecore / sound-file.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <string.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <errno.h>
32
33 #include <sndfile.h>
34
35 #include <pulse/sample.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/macro.h>
38 #include <pulsecore/core-error.h>
39 #include <pulsecore/core-util.h>
40
41 #include "sound-file.h"
42 #include "core-scache.h"
43
44 int pa_sound_file_load(
45 pa_mempool *pool,
46 const char *fname,
47 pa_sample_spec *ss,
48 pa_channel_map *map,
49 pa_memchunk *chunk) {
50
51 SNDFILE *sf = NULL;
52 SF_INFO sfinfo;
53 int ret = -1;
54 size_t l;
55 sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames) = NULL;
56 void *ptr = NULL;
57 int fd;
58
59 pa_assert(fname);
60 pa_assert(ss);
61 pa_assert(chunk);
62
63 pa_memchunk_reset(chunk);
64 memset(&sfinfo, 0, sizeof(sfinfo));
65
66 if ((fd = open(fname, O_RDONLY
67 #ifdef O_NOCTTY
68 |O_NOCTTY
69 #endif
70 )) < 0) {
71 pa_log("Failed to open file %s: %s", fname, pa_cstrerror(errno));
72 goto finish;
73 }
74
75 #ifdef HAVE_POSIX_FADVISE
76 if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL) < 0) {
77 pa_log_warn("POSIX_FADV_SEQUENTIAL failed: %s", pa_cstrerror(errno));
78 goto finish;
79 } else
80 pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
81 #endif
82
83 if (!(sf = sf_open_fd(fd, SFM_READ, &sfinfo, 1))) {
84 pa_log("Failed to open file %s", fname);
85 pa_close(fd);
86 goto finish;
87 }
88
89 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
90 case SF_FORMAT_PCM_16:
91 case SF_FORMAT_PCM_U8:
92 case SF_FORMAT_PCM_S8:
93 ss->format = PA_SAMPLE_S16NE;
94 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
95 break;
96
97 case SF_FORMAT_ULAW:
98 ss->format = PA_SAMPLE_ULAW;
99 break;
100
101 case SF_FORMAT_ALAW:
102 ss->format = PA_SAMPLE_ALAW;
103 break;
104
105 case SF_FORMAT_FLOAT:
106 case SF_FORMAT_DOUBLE:
107 default:
108 ss->format = PA_SAMPLE_FLOAT32NE;
109 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
110 break;
111 }
112
113 ss->rate = sfinfo.samplerate;
114 ss->channels = sfinfo.channels;
115
116 if (!pa_sample_spec_valid(ss)) {
117 pa_log("Unsupported sample format in file %s", fname);
118 goto finish;
119 }
120
121 if (map)
122 if (!pa_channel_map_init_auto(map, ss->channels, PA_CHANNEL_MAP_DEFAULT)) {
123 pa_log("Unsupported channel map in file %s", fname);
124 goto finish;
125 }
126
127 if ((l = pa_frame_size(ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
128 pa_log("File too large");
129 goto finish;
130 }
131
132 chunk->memblock = pa_memblock_new(pool, l);
133 chunk->index = 0;
134 chunk->length = l;
135
136 ptr = pa_memblock_acquire(chunk->memblock);
137
138 if ((readf_function && readf_function(sf, ptr, sfinfo.frames) != sfinfo.frames) ||
139 (!readf_function && sf_read_raw(sf, ptr, l) != (sf_count_t) l)) {
140 pa_log("Premature file end");
141 goto finish;
142 }
143
144 ret = 0;
145
146 finish:
147
148 if (sf)
149 sf_close(sf);
150
151 if (ptr)
152 pa_memblock_release(chunk->memblock);
153
154 if (ret != 0 && chunk->memblock)
155 pa_memblock_unref(chunk->memblock);
156
157 return ret;
158 }
159
160 int pa_sound_file_too_big_to_cache(const char *fname) {
161
162 SNDFILE*sf = NULL;
163 SF_INFO sfinfo;
164 pa_sample_spec ss;
165
166 pa_assert(fname);
167
168 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
169 pa_log("Failed to open file %s", fname);
170 return -1;
171 }
172
173 sf_close(sf);
174
175 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
176 case SF_FORMAT_PCM_16:
177 case SF_FORMAT_PCM_U8:
178 case SF_FORMAT_PCM_S8:
179 ss.format = PA_SAMPLE_S16NE;
180 break;
181
182 case SF_FORMAT_ULAW:
183 ss.format = PA_SAMPLE_ULAW;
184 break;
185
186 case SF_FORMAT_ALAW:
187 ss.format = PA_SAMPLE_ALAW;
188 break;
189
190 case SF_FORMAT_DOUBLE:
191 case SF_FORMAT_FLOAT:
192 default:
193 ss.format = PA_SAMPLE_FLOAT32NE;
194 break;
195 }
196
197 ss.rate = sfinfo.samplerate;
198 ss.channels = sfinfo.channels;
199
200 if (!pa_sample_spec_valid(&ss)) {
201 pa_log("Unsupported sample format in file %s", fname);
202 return -1;
203 }
204
205 if ((pa_frame_size(&ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
206 pa_log("File too large: %s", fname);
207 return 1;
208 }
209
210 return 0;
211 }