]> code.delx.au - pulseaudio/blob - src/pulsecore/sound-file.c
merge 'lennart' branch back into trunk.
[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 pa_channel_map_init_auto(map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
123
124 if ((l = pa_frame_size(ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
125 pa_log("File too large");
126 goto finish;
127 }
128
129 chunk->memblock = pa_memblock_new(pool, l);
130 chunk->index = 0;
131 chunk->length = l;
132
133 ptr = pa_memblock_acquire(chunk->memblock);
134
135 if ((readf_function && readf_function(sf, ptr, sfinfo.frames) != sfinfo.frames) ||
136 (!readf_function && sf_read_raw(sf, ptr, l) != (sf_count_t) l)) {
137 pa_log("Premature file end");
138 goto finish;
139 }
140
141 ret = 0;
142
143 finish:
144
145 if (sf)
146 sf_close(sf);
147
148 if (ptr)
149 pa_memblock_release(chunk->memblock);
150
151 if (ret != 0 && chunk->memblock)
152 pa_memblock_unref(chunk->memblock);
153
154 return ret;
155 }
156
157 int pa_sound_file_too_big_to_cache(const char *fname) {
158
159 SNDFILE*sf = NULL;
160 SF_INFO sfinfo;
161 pa_sample_spec ss;
162
163 pa_assert(fname);
164
165 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
166 pa_log("Failed to open file %s", fname);
167 return -1;
168 }
169
170 sf_close(sf);
171
172 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
173 case SF_FORMAT_PCM_16:
174 case SF_FORMAT_PCM_U8:
175 case SF_FORMAT_PCM_S8:
176 ss.format = PA_SAMPLE_S16NE;
177 break;
178
179 case SF_FORMAT_ULAW:
180 ss.format = PA_SAMPLE_ULAW;
181 break;
182
183 case SF_FORMAT_ALAW:
184 ss.format = PA_SAMPLE_ALAW;
185 break;
186
187 case SF_FORMAT_DOUBLE:
188 case SF_FORMAT_FLOAT:
189 default:
190 ss.format = PA_SAMPLE_FLOAT32NE;
191 break;
192 }
193
194 ss.rate = sfinfo.samplerate;
195 ss.channels = sfinfo.channels;
196
197 if (!pa_sample_spec_valid(&ss)) {
198 pa_log("Unsupported sample format in file %s", fname);
199 return -1;
200 }
201
202 if ((pa_frame_size(&ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
203 pa_log("File too large: %s", fname);
204 return 1;
205 }
206
207 return 0;
208 }