]>
code.delx.au - pulseaudio/blob - src/pulsecore/sound-file.c
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 pa_channel_map_init_auto(map
, ss
->channels
, PA_CHANNEL_MAP_DEFAULT
);
124 if ((l
= pa_frame_size(ss
) * sfinfo
.frames
) > PA_SCACHE_ENTRY_SIZE_MAX
) {
125 pa_log("File too large");
129 chunk
->memblock
= pa_memblock_new(pool
, l
);
133 ptr
= pa_memblock_acquire(chunk
->memblock
);
135 if ((readf_function
&& readf_function(sf
, ptr
, sfinfo
.frames
) != sfinfo
.frames
) ||
136 (!readf_function
&& sf_read_raw(sf
, ptr
, l
) != (sf_count_t
) l
)) {
137 pa_log("Premature file end");
149 pa_memblock_release(chunk
->memblock
);
151 if (ret
!= 0 && chunk
->memblock
)
152 pa_memblock_unref(chunk
->memblock
);
157 int pa_sound_file_too_big_to_cache(const char *fname
) {
165 if (!(sf
= sf_open(fname
, SFM_READ
, &sfinfo
))) {
166 pa_log("Failed to open file %s", fname
);
172 switch (sfinfo
.format
& SF_FORMAT_SUBMASK
) {
173 case SF_FORMAT_PCM_16
:
174 case SF_FORMAT_PCM_U8
:
175 case SF_FORMAT_PCM_S8
:
176 ss
.format
= PA_SAMPLE_S16NE
;
180 ss
.format
= PA_SAMPLE_ULAW
;
184 ss
.format
= PA_SAMPLE_ALAW
;
187 case SF_FORMAT_DOUBLE
:
188 case SF_FORMAT_FLOAT
:
190 ss
.format
= PA_SAMPLE_FLOAT32NE
;
194 ss
.rate
= sfinfo
.samplerate
;
195 ss
.channels
= sfinfo
.channels
;
197 if (!pa_sample_spec_valid(&ss
)) {
198 pa_log("Unsupported sample format in file %s", fname
);
202 if ((pa_frame_size(&ss
) * sfinfo
.frames
) > PA_SCACHE_ENTRY_SIZE_MAX
) {
203 pa_log("File too large: %s", fname
);