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
71 pa_log("Failed to open file %s: %s", fname, pa_cstrerror(errno));
75 #ifdef HAVE_POSIX_FADVISE
76 if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL) < 0) {
77 pa_log_warn("POSIX_FADV_SEQUENTIAL failed: %s", pa_cstrerror(errno));
80 pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
83 if (!(sf = sf_open_fd(fd, SFM_READ, &sfinfo, 1))) {
84 pa_log("Failed to open file %s", fname);
89 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
90 case SF_FORMAT_PCM_16:
91 case SF_FORMAT_PCM_U8:
92 case SF_FORMAT_PCM_S8:
93 ss->format = PA_SAMPLE_S16NE;
94 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
98 ss->format = PA_SAMPLE_ULAW;
102 ss->format = PA_SAMPLE_ALAW;
105 case SF_FORMAT_FLOAT:
106 case SF_FORMAT_DOUBLE:
108 ss->format = PA_SAMPLE_FLOAT32NE;
109 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
113 ss->rate = sfinfo.samplerate;
114 ss->channels = sfinfo.channels;
116 if (!pa_sample_spec_valid(ss)) {
117 pa_log("Unsupported sample format in file %s", fname);
122 if (!pa_channel_map_init_auto(map, ss->channels, PA_CHANNEL_MAP_DEFAULT)) {
123 pa_log("Unsupported channel map in file %s", fname);
127 if ((l = pa_frame_size(ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
128 pa_log("File too large");
132 chunk->memblock = pa_memblock_new(pool, l);
136 ptr = pa_memblock_acquire(chunk->memblock);
138 if ((readf_function && readf_function(sf, ptr, sfinfo.frames) != sfinfo.frames) ||
139 (!readf_function && sf_read_raw(sf, ptr, l) != (sf_count_t) l)) {
140 pa_log("Premature file end");
152 pa_memblock_release(chunk->memblock);
154 if (ret != 0 && chunk->memblock)
155 pa_memblock_unref(chunk->memblock);
160 int pa_sound_file_too_big_to_cache(const char *fname) {
168 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
169 pa_log("Failed to open file %s", fname);
175 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
176 case SF_FORMAT_PCM_16:
177 case SF_FORMAT_PCM_U8:
178 case SF_FORMAT_PCM_S8:
179 ss.format = PA_SAMPLE_S16NE;
183 ss.format = PA_SAMPLE_ULAW;
187 ss.format = PA_SAMPLE_ALAW;
190 case SF_FORMAT_DOUBLE:
191 case SF_FORMAT_FLOAT:
193 ss.format = PA_SAMPLE_FLOAT32NE;
197 ss.rate = sfinfo.samplerate;
198 ss.channels = sfinfo.channels;
200 if (!pa_sample_spec_valid(&ss)) {
201 pa_log("Unsupported sample format in file %s", fname);
205 if ((pa_frame_size(&ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
206 pa_log("File too large: %s", fname);