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