]> code.delx.au - pulseaudio/blob - src/pulsecore/sound-file.c
minor optimization for cacheing in of samples by using posix_fadvise
[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 if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL) < 0) {
71 pa_log_warn("POSIX_FADV_SEQUENTIAL failed: %s", pa_cstrerror(errno));
72 goto finish;
73 } else
74 pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
75
76 if (!(sf = sf_open_fd(fd, SFM_READ, &sfinfo, 1))) {
77 pa_log("Failed to open file %s", fname);
78 close(fd);
79 goto finish;
80 }
81
82 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
83 case SF_FORMAT_PCM_16:
84 case SF_FORMAT_PCM_U8:
85 case SF_FORMAT_PCM_S8:
86 ss->format = PA_SAMPLE_S16NE;
87 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
88 break;
89
90 case SF_FORMAT_ULAW:
91 ss->format = PA_SAMPLE_ULAW;
92 break;
93
94 case SF_FORMAT_ALAW:
95 ss->format = PA_SAMPLE_ALAW;
96 break;
97
98 case SF_FORMAT_FLOAT:
99 case SF_FORMAT_DOUBLE:
100 default:
101 ss->format = PA_SAMPLE_FLOAT32NE;
102 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
103 break;
104 }
105
106 ss->rate = sfinfo.samplerate;
107 ss->channels = sfinfo.channels;
108
109 if (!pa_sample_spec_valid(ss)) {
110 pa_log("Unsupported sample format in file %s", fname);
111 goto finish;
112 }
113
114 if (map)
115 pa_channel_map_init_auto(map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
116
117 if ((l = pa_frame_size(ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
118 pa_log("File too large");
119 goto finish;
120 }
121
122 chunk->memblock = pa_memblock_new(pool, l);
123 chunk->index = 0;
124 chunk->length = l;
125
126 ptr = pa_memblock_acquire(chunk->memblock);
127
128 if ((readf_function && readf_function(sf, ptr, sfinfo.frames) != sfinfo.frames) ||
129 (!readf_function && sf_read_raw(sf, ptr, l) != l)) {
130 pa_log("Premature file end");
131 goto finish;
132 }
133
134 ret = 0;
135
136 finish:
137
138 if (sf)
139 sf_close(sf);
140
141 if (ptr)
142 pa_memblock_release(chunk->memblock);
143
144 if (ret != 0 && chunk->memblock)
145 pa_memblock_unref(chunk->memblock);
146
147 return ret;
148 }
149
150 int pa_sound_file_too_big_to_cache(const char *fname) {
151
152 SNDFILE*sf = NULL;
153 SF_INFO sfinfo;
154 pa_sample_spec ss;
155
156 pa_assert(fname);
157
158 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
159 pa_log("Failed to open file %s", fname);
160 return -1;
161 }
162
163 sf_close(sf);
164
165 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
166 case SF_FORMAT_PCM_16:
167 case SF_FORMAT_PCM_U8:
168 case SF_FORMAT_PCM_S8:
169 ss.format = PA_SAMPLE_S16NE;
170 break;
171
172 case SF_FORMAT_ULAW:
173 ss.format = PA_SAMPLE_ULAW;
174 break;
175
176 case SF_FORMAT_ALAW:
177 ss.format = PA_SAMPLE_ALAW;
178 break;
179
180 case SF_FORMAT_DOUBLE:
181 case SF_FORMAT_FLOAT:
182 default:
183 ss.format = PA_SAMPLE_FLOAT32NE;
184 break;
185 }
186
187 ss.rate = sfinfo.samplerate;
188 ss.channels = sfinfo.channels;
189
190 if (!pa_sample_spec_valid(&ss)) {
191 pa_log("Unsupported sample format in file %s", fname);
192 return -1;
193 }
194
195 if ((pa_frame_size(&ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
196 pa_log("File too large: %s", fname);
197 return 1;
198 }
199
200 return 0;
201 }