]> code.delx.au - pulseaudio/blob - polyp/sound-file.c
rename some stuff
[pulseaudio] / polyp / sound-file.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <assert.h>
28
29 #include <sndfile.h>
30
31 #include "sound-file.h"
32 #include "sample.h"
33 #include "log.h"
34
35 #define MAX_FILE_SIZE (1024*1024)
36
37 int pa_sound_file_load(const char *fname, struct pa_sample_spec *ss, struct pa_memchunk *chunk, struct pa_memblock_stat *s) {
38 SNDFILE*sf = NULL;
39 SF_INFO sfinfo;
40 int ret = -1;
41 size_t l;
42 assert(fname && ss && chunk);
43
44 memset(&sfinfo, 0, sizeof(sfinfo));
45
46 chunk->memblock = NULL;
47 chunk->index = chunk->length = 0;
48
49 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
50 pa_log(__FILE__": Failed to open file %s\n", fname);
51 goto finish;
52 }
53
54 ss->format = PA_SAMPLE_FLOAT32;
55 ss->rate = sfinfo.samplerate;
56 ss->channels = sfinfo.channels;
57
58 if (!pa_sample_spec_valid(ss)) {
59 pa_log(__FILE__": Unsupported sample format in file %s\n", fname);
60 goto finish;
61 }
62
63 if ((l = pa_frame_size(ss)*sfinfo.frames) > MAX_FILE_SIZE) {
64 pa_log(__FILE__": File to large\n");
65 goto finish;
66 }
67
68 chunk->memblock = pa_memblock_new(l, s);
69 assert(chunk->memblock);
70 chunk->index = 0;
71 chunk->length = l;
72
73 if (sf_readf_float(sf, chunk->memblock->data, sfinfo.frames) != sfinfo.frames) {
74 pa_log(__FILE__": Premature file end\n");
75 goto finish;
76 }
77
78 ret = 0;
79
80 finish:
81
82 if (sf)
83 sf_close(sf);
84
85 if (ret != 0 && chunk->memblock)
86 pa_memblock_unref(chunk->memblock);
87
88 return ret;
89
90 }