4 This file is part of PulseAudio.
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.
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.
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
31 #include <pulse/sample.h>
32 #include <pulsecore/log.h>
34 #include "sound-file.h"
35 #include "core-scache.h"
37 int pa_sound_file_load(pa_mempool *pool, const char *fname, pa_sample_spec *ss, pa_channel_map *map, pa_memchunk *chunk) {
42 sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames) = NULL;
49 chunk->memblock = NULL;
50 chunk->index = chunk->length = 0;
52 memset(&sfinfo, 0, sizeof(sfinfo));
54 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
55 pa_log("Failed to open file %s", fname);
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;
68 ss->format = PA_SAMPLE_ULAW;
72 ss->format = PA_SAMPLE_ALAW;
76 case SF_FORMAT_DOUBLE:
78 ss->format = PA_SAMPLE_FLOAT32NE;
79 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
83 ss->rate = sfinfo.samplerate;
84 ss->channels = sfinfo.channels;
86 if (!pa_sample_spec_valid(ss)) {
87 pa_log("Unsupported sample format in file %s", fname);
92 pa_channel_map_init_auto(map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
94 if ((l = pa_frame_size(ss)*sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
95 pa_log("File too large");
99 chunk->memblock = pa_memblock_new(pool, l);
100 assert(chunk->memblock);
104 ptr = pa_memblock_acquire(chunk->memblock);
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");
120 pa_memblock_release(chunk->memblock);
122 if (ret != 0 && chunk->memblock)
123 pa_memblock_unref(chunk->memblock);
129 int pa_sound_file_too_big_to_cache(const char *fname) {
134 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
135 pa_log("Failed to open file %s", fname);
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;
149 ss.format = PA_SAMPLE_ULAW;
153 ss.format = PA_SAMPLE_ALAW;
156 case SF_FORMAT_DOUBLE:
157 case SF_FORMAT_FLOAT:
159 ss.format = PA_SAMPLE_FLOAT32NE;
163 ss.rate = sfinfo.samplerate;
164 ss.channels = sfinfo.channels;
166 if ((pa_frame_size(&ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
167 pa_log("File too large %s", fname);