]>
code.delx.au - pulseaudio/blob - polyp/sound-file.c
c63aa628fc3f9b29cc05b7b38901ae9448d4445a
4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include "sound-file.h"
35 #define MAX_FILE_SIZE (1024*1024)
37 int pa_sound_file_load(const char *fname
, struct pa_sample_spec
*ss
, struct pa_memchunk
*chunk
, struct pa_memblock_stat
*s
) {
42 sf_count_t (*readf_function
)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
);
43 assert(fname
&& ss
&& chunk
);
45 chunk
->memblock
= NULL
;
46 chunk
->index
= chunk
->length
= 0;
48 memset(&sfinfo
, 0, sizeof(sfinfo
));
50 if (!(sf
= sf_open(fname
, SFM_READ
, &sfinfo
))) {
51 pa_log(__FILE__
": Failed to open file %s\n", fname
);
55 switch (sfinfo
.format
& 0xFF) {
56 case SF_FORMAT_PCM_16
:
57 case SF_FORMAT_PCM_U8
:
60 ss
->format
= PA_SAMPLE_S16NE
;
61 readf_function
= (sf_count_t (*)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_short
;
65 ss
->format
= PA_SAMPLE_FLOAT32NE
;
66 readf_function
= (sf_count_t (*)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_float
;
70 ss
->rate
= sfinfo
.samplerate
;
71 ss
->channels
= sfinfo
.channels
;
73 if (!pa_sample_spec_valid(ss
)) {
74 pa_log(__FILE__
": Unsupported sample format in file %s\n", fname
);
78 if ((l
= pa_frame_size(ss
)*sfinfo
.frames
) > MAX_FILE_SIZE
) {
79 pa_log(__FILE__
": File to large\n");
83 chunk
->memblock
= pa_memblock_new(l
, s
);
84 assert(chunk
->memblock
);
88 if (readf_function(sf
, chunk
->memblock
->data
, sfinfo
.frames
) != sfinfo
.frames
) {
89 pa_log(__FILE__
": Premature file end\n");
100 if (ret
!= 0 && chunk
->memblock
)
101 pa_memblock_unref(chunk
->memblock
);