]> code.delx.au - pulseaudio/blob - src/pulsecore/sound-file.c
posix_madvise and posix_fadvise aren't present on all systems.
[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
40 #include "sound-file.h"
41 #include "core-scache.h"
42
43 int pa_sound_file_load(
44 pa_mempool *pool,
45 const char *fname,
46 pa_sample_spec *ss,
47 pa_channel_map *map,
48 pa_memchunk *chunk) {
49
50 SNDFILE *sf = NULL;
51 SF_INFO sfinfo;
52 int ret = -1;
53 size_t l;
54 sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames) = NULL;
55 void *ptr = NULL;
56 int fd;
57
58 pa_assert(fname);
59 pa_assert(ss);
60 pa_assert(chunk);
61
62 pa_memchunk_reset(chunk);
63 memset(&sfinfo, 0, sizeof(sfinfo));
64
65 if ((fd = open(fname, O_RDONLY|O_NOCTTY)) < 0) {
66 pa_log("Failed to open file %s: %s", fname, pa_cstrerror(errno));
67 goto finish;
68 }
69
70 #ifdef HAVE_POSIX_FADVISE
71 if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL) < 0) {
72 pa_log_warn("POSIX_FADV_SEQUENTIAL failed: %s", pa_cstrerror(errno));
73 goto finish;
74 } else
75 pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
76 #endif
77
78 if (!(sf = sf_open_fd(fd, SFM_READ, &sfinfo, 1))) {
79 pa_log("Failed to open file %s", fname);
80 close(fd);
81 goto finish;
82 }
83
84 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
85 case SF_FORMAT_PCM_16:
86 case SF_FORMAT_PCM_U8:
87 case SF_FORMAT_PCM_S8:
88 ss->format = PA_SAMPLE_S16NE;
89 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
90 break;
91
92 case SF_FORMAT_ULAW:
93 ss->format = PA_SAMPLE_ULAW;
94 break;
95
96 case SF_FORMAT_ALAW:
97 ss->format = PA_SAMPLE_ALAW;
98 break;
99
100 case SF_FORMAT_FLOAT:
101 case SF_FORMAT_DOUBLE:
102 default:
103 ss->format = PA_SAMPLE_FLOAT32NE;
104 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
105 break;
106 }
107
108 ss->rate = sfinfo.samplerate;
109 ss->channels = sfinfo.channels;
110
111 if (!pa_sample_spec_valid(ss)) {
112 pa_log("Unsupported sample format in file %s", fname);
113 goto finish;
114 }
115
116 if (map)
117 pa_channel_map_init_auto(map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
118
119 if ((l = pa_frame_size(ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
120 pa_log("File too large");
121 goto finish;
122 }
123
124 chunk->memblock = pa_memblock_new(pool, l);
125 chunk->index = 0;
126 chunk->length = l;
127
128 ptr = pa_memblock_acquire(chunk->memblock);
129
130 if ((readf_function && readf_function(sf, ptr, sfinfo.frames) != sfinfo.frames) ||
131 (!readf_function && sf_read_raw(sf, ptr, l) != (sf_count_t) l)) {
132 pa_log("Premature file end");
133 goto finish;
134 }
135
136 ret = 0;
137
138 finish:
139
140 if (sf)
141 sf_close(sf);
142
143 if (ptr)
144 pa_memblock_release(chunk->memblock);
145
146 if (ret != 0 && chunk->memblock)
147 pa_memblock_unref(chunk->memblock);
148
149 return ret;
150 }
151
152 int pa_sound_file_too_big_to_cache(const char *fname) {
153
154 SNDFILE*sf = NULL;
155 SF_INFO sfinfo;
156 pa_sample_spec ss;
157
158 pa_assert(fname);
159
160 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
161 pa_log("Failed to open file %s", fname);
162 return -1;
163 }
164
165 sf_close(sf);
166
167 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
168 case SF_FORMAT_PCM_16:
169 case SF_FORMAT_PCM_U8:
170 case SF_FORMAT_PCM_S8:
171 ss.format = PA_SAMPLE_S16NE;
172 break;
173
174 case SF_FORMAT_ULAW:
175 ss.format = PA_SAMPLE_ULAW;
176 break;
177
178 case SF_FORMAT_ALAW:
179 ss.format = PA_SAMPLE_ALAW;
180 break;
181
182 case SF_FORMAT_DOUBLE:
183 case SF_FORMAT_FLOAT:
184 default:
185 ss.format = PA_SAMPLE_FLOAT32NE;
186 break;
187 }
188
189 ss.rate = sfinfo.samplerate;
190 ss.channels = sfinfo.channels;
191
192 if (!pa_sample_spec_valid(&ss)) {
193 pa_log("Unsupported sample format in file %s", fname);
194 return -1;
195 }
196
197 if ((pa_frame_size(&ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
198 pa_log("File too large: %s", fname);
199 return 1;
200 }
201
202 return 0;
203 }