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