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;
43 assert(fname && ss && chunk);
45 chunk->memblock = NULL;
46 chunk->index = chunk->length = 0;
48 memset(&sfinfo, 0, sizeof(sfinfo));
50 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
51 pa_log("Failed to open file %s", fname);
55 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
56 case SF_FORMAT_PCM_16:
57 case SF_FORMAT_PCM_U8:
58 case SF_FORMAT_PCM_S8:
59 ss->format = PA_SAMPLE_S16NE;
60 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
64 ss->format = PA_SAMPLE_ULAW;
68 ss->format = PA_SAMPLE_ALAW;
72 case SF_FORMAT_DOUBLE:
74 ss->format = PA_SAMPLE_FLOAT32NE;
75 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
79 ss->rate = sfinfo.samplerate;
80 ss->channels = sfinfo.channels;
82 if (!pa_sample_spec_valid(ss)) {
83 pa_log("Unsupported sample format in file %s", fname);
88 pa_channel_map_init_auto(map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
90 if ((l = pa_frame_size(ss)*sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
91 pa_log("File too large");
95 chunk->memblock = pa_memblock_new(pool, l);
96 assert(chunk->memblock);
100 if ((readf_function && readf_function(sf, chunk->memblock->data, sfinfo.frames) != sfinfo.frames) ||
101 (!readf_function && sf_read_raw(sf, chunk->memblock->data, l) != l)) {
102 pa_log("Premature file end");
113 if (ret != 0 && chunk->memblock)
114 pa_memblock_unref(chunk->memblock);
120 int pa_sound_file_too_big_to_cache(const char *fname) {
125 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
126 pa_log("Failed to open file %s", fname);
132 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
133 case SF_FORMAT_PCM_16:
134 case SF_FORMAT_PCM_U8:
135 case SF_FORMAT_PCM_S8:
136 ss.format = PA_SAMPLE_S16NE;
140 ss.format = PA_SAMPLE_ULAW;
144 ss.format = PA_SAMPLE_ALAW;
147 case SF_FORMAT_DOUBLE:
148 case SF_FORMAT_FLOAT:
150 ss.format = PA_SAMPLE_FLOAT32NE;
154 ss.rate = sfinfo.samplerate;
155 ss.channels = sfinfo.channels;
157 if ((pa_frame_size(&ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
158 pa_log("File too large %s", fname);