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;
45 assert(fname && ss && chunk);
47 chunk->memblock = NULL;
48 chunk->index = chunk->length = 0;
50 memset(&sfinfo, 0, sizeof(sfinfo));
52 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
53 pa_log("Failed to open file %s", fname);
57 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
58 case SF_FORMAT_PCM_16:
59 case SF_FORMAT_PCM_U8:
60 case SF_FORMAT_PCM_S8:
61 ss->format = PA_SAMPLE_S16NE;
62 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
66 ss->format = PA_SAMPLE_ULAW;
70 ss->format = PA_SAMPLE_ALAW;
74 case SF_FORMAT_DOUBLE:
76 ss->format = PA_SAMPLE_FLOAT32NE;
77 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
81 ss->rate = sfinfo.samplerate;
82 ss->channels = sfinfo.channels;
84 if (!pa_sample_spec_valid(ss)) {
85 pa_log("Unsupported sample format in file %s", fname);
90 pa_channel_map_init_auto(map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
92 if ((l = pa_frame_size(ss)*sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
93 pa_log("File too large");
97 chunk->memblock = pa_memblock_new(pool, l);
98 assert(chunk->memblock);
102 if ((readf_function && readf_function(sf, chunk->memblock->data, sfinfo.frames) != sfinfo.frames) ||
103 (!readf_function && sf_read_raw(sf, chunk->memblock->data, l) != l)) {
104 pa_log("Premature file end");
115 if (ret != 0 && chunk->memblock)
116 pa_memblock_unref(chunk->memblock);
122 int pa_sound_file_too_big_to_cache(const char *fname) {
127 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
128 pa_log("Failed to open file %s", fname);
134 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
135 case SF_FORMAT_PCM_16:
136 case SF_FORMAT_PCM_U8:
137 case SF_FORMAT_PCM_S8:
138 ss.format = PA_SAMPLE_S16NE;
142 ss.format = PA_SAMPLE_ULAW;
146 ss.format = PA_SAMPLE_ALAW;
149 case SF_FORMAT_DOUBLE:
150 case SF_FORMAT_FLOAT:
152 ss.format = PA_SAMPLE_FLOAT32NE;
156 ss.rate = sfinfo.samplerate;
157 ss.channels = sfinfo.channels;
159 if ((pa_frame_size(&ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
160 pa_log("File too large %s", fname);