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>
40 #include "sound-file.h"
41 #include "core-scache.h"
43 int pa_sound_file_load(
54 sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames) = NULL;
62 pa_memchunk_reset(chunk);
63 memset(&sfinfo, 0, sizeof(sfinfo));
65 if ((fd = open(fname, O_RDONLY|O_NOCTTY)) < 0) {
66 pa_log("Failed to open file %s: %s", fname, pa_cstrerror(errno));
70 if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL) < 0) {
71 pa_log_warn("POSIX_FADV_SEQUENTIAL failed: %s", pa_cstrerror(errno));
74 pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
76 if (!(sf = sf_open_fd(fd, SFM_READ, &sfinfo, 1))) {
77 pa_log("Failed to open file %s", fname);
82 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
83 case SF_FORMAT_PCM_16:
84 case SF_FORMAT_PCM_U8:
85 case SF_FORMAT_PCM_S8:
86 ss->format = PA_SAMPLE_S16NE;
87 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
91 ss->format = PA_SAMPLE_ULAW;
95 ss->format = PA_SAMPLE_ALAW;
99 case SF_FORMAT_DOUBLE:
101 ss->format = PA_SAMPLE_FLOAT32NE;
102 readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
106 ss->rate = sfinfo.samplerate;
107 ss->channels = sfinfo.channels;
109 if (!pa_sample_spec_valid(ss)) {
110 pa_log("Unsupported sample format in file %s", fname);
115 pa_channel_map_init_auto(map, ss->channels, PA_CHANNEL_MAP_DEFAULT);
117 if ((l = pa_frame_size(ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
118 pa_log("File too large");
122 chunk->memblock = pa_memblock_new(pool, l);
126 ptr = pa_memblock_acquire(chunk->memblock);
128 if ((readf_function && readf_function(sf, ptr, sfinfo.frames) != sfinfo.frames) ||
129 (!readf_function && sf_read_raw(sf, ptr, l) != l)) {
130 pa_log("Premature file end");
142 pa_memblock_release(chunk->memblock);
144 if (ret != 0 && chunk->memblock)
145 pa_memblock_unref(chunk->memblock);
150 int pa_sound_file_too_big_to_cache(const char *fname) {
158 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
159 pa_log("Failed to open file %s", fname);
165 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
166 case SF_FORMAT_PCM_16:
167 case SF_FORMAT_PCM_U8:
168 case SF_FORMAT_PCM_S8:
169 ss.format = PA_SAMPLE_S16NE;
173 ss.format = PA_SAMPLE_ULAW;
177 ss.format = PA_SAMPLE_ALAW;
180 case SF_FORMAT_DOUBLE:
181 case SF_FORMAT_FLOAT:
183 ss.format = PA_SAMPLE_FLOAT32NE;
187 ss.rate = sfinfo.samplerate;
188 ss.channels = sfinfo.channels;
190 if (!pa_sample_spec_valid(&ss)) {
191 pa_log("Unsupported sample format in file %s", fname);
195 if ((pa_frame_size(&ss) * sfinfo.frames) > PA_SCACHE_ENTRY_SIZE_MAX) {
196 pa_log("File too large: %s", fname);