]>
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>
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 #ifdef HAVE_POSIX_FADVISE
71 if (posix_fadvise(fd
, 0, 0, POSIX_FADV_SEQUENTIAL
) < 0) {
72 pa_log_warn("POSIX_FADV_SEQUENTIAL failed: %s", pa_cstrerror(errno
));
75 pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
78 if (!(sf
= sf_open_fd(fd
, SFM_READ
, &sfinfo
, 1))) {
79 pa_log("Failed to open file %s", fname
);
84 switch (sfinfo
.format
& SF_FORMAT_SUBMASK
) {
85 case SF_FORMAT_PCM_16
:
86 case SF_FORMAT_PCM_U8
:
87 case SF_FORMAT_PCM_S8
:
88 ss
->format
= PA_SAMPLE_S16NE
;
89 readf_function
= (sf_count_t (*)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_short
;
93 ss
->format
= PA_SAMPLE_ULAW
;
97 ss
->format
= PA_SAMPLE_ALAW
;
100 case SF_FORMAT_FLOAT
:
101 case SF_FORMAT_DOUBLE
:
103 ss
->format
= PA_SAMPLE_FLOAT32NE
;
104 readf_function
= (sf_count_t (*)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_float
;
108 ss
->rate
= sfinfo
.samplerate
;
109 ss
->channels
= sfinfo
.channels
;
111 if (!pa_sample_spec_valid(ss
)) {
112 pa_log("Unsupported sample format in file %s", fname
);
117 pa_channel_map_init_auto(map
, ss
->channels
, PA_CHANNEL_MAP_DEFAULT
);
119 if ((l
= pa_frame_size(ss
) * sfinfo
.frames
) > PA_SCACHE_ENTRY_SIZE_MAX
) {
120 pa_log("File too large");
124 chunk
->memblock
= pa_memblock_new(pool
, l
);
128 ptr
= pa_memblock_acquire(chunk
->memblock
);
130 if ((readf_function
&& readf_function(sf
, ptr
, sfinfo
.frames
) != sfinfo
.frames
) ||
131 (!readf_function
&& sf_read_raw(sf
, ptr
, l
) != (sf_count_t
) l
)) {
132 pa_log("Premature file end");
144 pa_memblock_release(chunk
->memblock
);
146 if (ret
!= 0 && chunk
->memblock
)
147 pa_memblock_unref(chunk
->memblock
);
152 int pa_sound_file_too_big_to_cache(const char *fname
) {
160 if (!(sf
= sf_open(fname
, SFM_READ
, &sfinfo
))) {
161 pa_log("Failed to open file %s", fname
);
167 switch (sfinfo
.format
& SF_FORMAT_SUBMASK
) {
168 case SF_FORMAT_PCM_16
:
169 case SF_FORMAT_PCM_U8
:
170 case SF_FORMAT_PCM_S8
:
171 ss
.format
= PA_SAMPLE_S16NE
;
175 ss
.format
= PA_SAMPLE_ULAW
;
179 ss
.format
= PA_SAMPLE_ALAW
;
182 case SF_FORMAT_DOUBLE
:
183 case SF_FORMAT_FLOAT
:
185 ss
.format
= PA_SAMPLE_FLOAT32NE
;
189 ss
.rate
= sfinfo
.samplerate
;
190 ss
.channels
= sfinfo
.channels
;
192 if (!pa_sample_spec_valid(&ss
)) {
193 pa_log("Unsupported sample format in file %s", fname
);
197 if ((pa_frame_size(&ss
) * sfinfo
.frames
) > PA_SCACHE_ENTRY_SIZE_MAX
) {
198 pa_log("File too large: %s", fname
);