4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
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.
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.
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
33 #include <pulse/sample.h>
34 #include <pulsecore/log.h>
36 #include "sound-file.h"
37 #include "core-scache.h"
39 int pa_sound_file_load(pa_mempool *pool, const char *fname, pa_sample_spec *ss, pa_channel_map *map, pa_memchunk *chunk) {
44 sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames) = NULL;
51 chunk->memblock = NULL;
52 chunk->index = chunk->length = 0;
54 memset(&sfinfo, 0, sizeof(sfinfo));
56 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
57 pa_log("Failed to open file %s", fname);
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;
70 ss->format = PA_SAMPLE_ULAW;
74 ss->format = PA_SAMPLE_ALAW;
78 case SF_FORMAT_DOUBLE:
80 ss->format = PA_SAMPLE_FLOAT32NE;
81 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
85 ss->rate = sfinfo.samplerate;
86 ss->channels = sfinfo.channels;
88 if (!pa_sample_spec_valid(ss)) {
89 pa_log("Unsupported sample format in file %s", fname);
94 pa_channel_map_init_auto(map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
96 if ((l = pa_frame_size(ss)*sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
97 pa_log("File too large");
101 chunk->memblock = pa_memblock_new(pool, l);
102 assert(chunk->memblock);
106 ptr = pa_memblock_acquire(chunk->memblock);
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");
122 pa_memblock_release(chunk->memblock);
124 if (ret != 0 && chunk->memblock)
125 pa_memblock_unref(chunk->memblock);
131 int pa_sound_file_too_big_to_cache(const char *fname) {
136 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
137 pa_log("Failed to open file %s", fname);
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;
151 ss.format = PA_SAMPLE_ULAW;
155 ss.format = PA_SAMPLE_ALAW;
158 case SF_FORMAT_DOUBLE:
159 case SF_FORMAT_FLOAT:
161 ss.format = PA_SAMPLE_FLOAT32NE;
165 ss.rate = sfinfo.samplerate;
166 ss.channels = sfinfo.channels;
168 if ((pa_frame_size(&ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
169 pa_log("File too large %s", fname);