2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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
33 #include <pulse/sample.h>
34 #include <pulsecore/log.h>
35 #include <pulsecore/macro.h>
36 #include <pulsecore/core-error.h>
37 #include <pulsecore/core-util.h>
39 #include "sound-file.h"
40 #include "core-scache.h"
42 int pa_sound_file_load(
53 sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames) = NULL;
61 pa_memchunk_reset(chunk);
62 memset(&sfinfo, 0, sizeof(sfinfo));
64 if ((fd = open(fname, O_RDONLY
69 pa_log("Failed to open file %s: %s", fname, pa_cstrerror(errno));
73 #ifdef HAVE_POSIX_FADVISE
74 if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL) < 0) {
75 pa_log_warn("POSIX_FADV_SEQUENTIAL failed: %s", pa_cstrerror(errno));
78 pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
81 if (!(sf = sf_open_fd(fd, SFM_READ, &sfinfo, 1))) {
82 pa_log("Failed to open file %s", fname);
87 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
88 case SF_FORMAT_PCM_16:
89 case SF_FORMAT_PCM_U8:
90 case SF_FORMAT_PCM_S8:
91 ss->format = PA_SAMPLE_S16NE;
92 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
96 ss->format = PA_SAMPLE_ULAW;
100 ss->format = PA_SAMPLE_ALAW;
103 case SF_FORMAT_FLOAT:
104 case SF_FORMAT_DOUBLE:
106 ss->format = PA_SAMPLE_FLOAT32NE;
107 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
111 ss->rate = sfinfo.samplerate;
112 ss->channels = sfinfo.channels;
114 if (!pa_sample_spec_valid(ss)) {
115 pa_log("Unsupported sample format in file %s", fname);
120 pa_channel_map_init_extend(map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
122 if ((l = pa_frame_size(ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
123 pa_log("File too large");
127 chunk->memblock = pa_memblock_new(pool, l);
131 ptr = pa_memblock_acquire(chunk->memblock);
133 if ((readf_function && readf_function(sf, ptr, sfinfo.frames) != sfinfo.frames) ||
134 (!readf_function && sf_read_raw(sf, ptr, l) != (sf_count_t) l)) {
135 pa_log("Premature file end");
147 pa_memblock_release(chunk->memblock);
149 if (ret != 0 && chunk->memblock)
150 pa_memblock_unref(chunk->memblock);
155 int pa_sound_file_too_big_to_cache(const char *fname) {
163 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
164 pa_log("Failed to open file %s", fname);
170 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
171 case SF_FORMAT_PCM_16:
172 case SF_FORMAT_PCM_U8:
173 case SF_FORMAT_PCM_S8:
174 ss.format = PA_SAMPLE_S16NE;
178 ss.format = PA_SAMPLE_ULAW;
182 ss.format = PA_SAMPLE_ALAW;
185 case SF_FORMAT_DOUBLE:
186 case SF_FORMAT_FLOAT:
188 ss.format = PA_SAMPLE_FLOAT32NE;
192 ss.rate = sfinfo.samplerate;
193 ss.channels = sfinfo.channels;
195 if (!pa_sample_spec_valid(&ss)) {
196 pa_log("Unsupported sample format in file %s", fname);
200 if ((pa_frame_size(&ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
201 pa_log("File too large: %s", fname);