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
35 #include <pulse/sample.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/macro.h>
38 #include <pulsecore/core-error.h>
39 #include <pulsecore/core-util.h>
41 #include "sound-file.h"
42 #include "core-scache.h"
44 int pa_sound_file_load(
55 sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames) = NULL;
63 pa_memchunk_reset(chunk);
64 memset(&sfinfo, 0, sizeof(sfinfo));
66 if ((fd = open(fname, O_RDONLY|O_NOCTTY)) < 0) {
67 pa_log("Failed to open file %s: %s", fname, pa_cstrerror(errno));
71 #ifdef HAVE_POSIX_FADVISE
72 if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL) < 0) {
73 pa_log_warn("POSIX_FADV_SEQUENTIAL failed: %s", pa_cstrerror(errno));
76 pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
79 if (!(sf = sf_open_fd(fd, SFM_READ, &sfinfo, 1))) {
80 pa_log("Failed to open file %s", fname);
85 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
86 case SF_FORMAT_PCM_16:
87 case SF_FORMAT_PCM_U8:
88 case SF_FORMAT_PCM_S8:
89 ss->format = PA_SAMPLE_S16NE;
90 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
94 ss->format = PA_SAMPLE_ULAW;
98 ss->format = PA_SAMPLE_ALAW;
101 case SF_FORMAT_FLOAT:
102 case SF_FORMAT_DOUBLE:
104 ss->format = PA_SAMPLE_FLOAT32NE;
105 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
109 ss->rate = sfinfo.samplerate;
110 ss->channels = sfinfo.channels;
112 if (!pa_sample_spec_valid(ss)) {
113 pa_log("Unsupported sample format in file %s", fname);
118 pa_channel_map_init_auto(map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
120 if ((l = pa_frame_size(ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
121 pa_log("File too large");
125 chunk->memblock = pa_memblock_new(pool, l);
129 ptr = pa_memblock_acquire(chunk->memblock);
131 if ((readf_function && readf_function(sf, ptr, sfinfo.frames) != sfinfo.frames) ||
132 (!readf_function && sf_read_raw(sf, ptr, l) != (sf_count_t) l)) {
133 pa_log("Premature file end");
145 pa_memblock_release(chunk->memblock);
147 if (ret != 0 && chunk->memblock)
148 pa_memblock_unref(chunk->memblock);
153 int pa_sound_file_too_big_to_cache(const char *fname) {
161 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
162 pa_log("Failed to open file %s", fname);
168 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
169 case SF_FORMAT_PCM_16:
170 case SF_FORMAT_PCM_U8:
171 case SF_FORMAT_PCM_S8:
172 ss.format = PA_SAMPLE_S16NE;
176 ss.format = PA_SAMPLE_ULAW;
180 ss.format = PA_SAMPLE_ALAW;
183 case SF_FORMAT_DOUBLE:
184 case SF_FORMAT_FLOAT:
186 ss.format = PA_SAMPLE_FLOAT32NE;
190 ss.rate = sfinfo.samplerate;
191 ss.channels = sfinfo.channels;
193 if (!pa_sample_spec_valid(&ss)) {
194 pa_log("Unsupported sample format in file %s", fname);
198 if ((pa_frame_size(&ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
199 pa_log("File too large: %s", fname);