]> code.delx.au - pulseaudio/blob - polyp/sound-file.c
remove global memblock statistic variables in favor of memblock_stat objects
[pulseaudio] / polyp / sound-file.c
1 #include <string.h>
2 #include <assert.h>
3
4 #include <sndfile.h>
5
6 #include "sound-file.h"
7 #include "sample.h"
8
9 #define MAX_FILE_SIZE (1024*1024)
10
11 int pa_sound_file_load(const char *fname, struct pa_sample_spec *ss, struct pa_memchunk *chunk, struct pa_memblock_stat *s) {
12 SNDFILE*sf = NULL;
13 SF_INFO sfinfo;
14 int ret = -1;
15 size_t l;
16 assert(fname && ss && chunk);
17
18 memset(&sfinfo, 0, sizeof(sfinfo));
19
20 chunk->memblock = NULL;
21 chunk->index = chunk->length = 0;
22
23 if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
24 fprintf(stderr, __FILE__": Failed to open file %s\n", fname);
25 goto finish;
26 }
27
28 ss->format = PA_SAMPLE_FLOAT32;
29 ss->rate = sfinfo.samplerate;
30 ss->channels = sfinfo.channels;
31
32 if (!pa_sample_spec_valid(ss)) {
33 fprintf(stderr, __FILE__": Unsupported sample format in file %s\n", fname);
34 goto finish;
35 }
36
37 if ((l = pa_frame_size(ss)*sfinfo.frames) > MAX_FILE_SIZE) {
38 fprintf(stderr, __FILE__": File to large\n");
39 goto finish;
40 }
41
42 chunk->memblock = pa_memblock_new(l, s);
43 assert(chunk->memblock);
44 chunk->index = 0;
45 chunk->length = l;
46
47 if (sf_readf_float(sf, chunk->memblock->data, sfinfo.frames) != sfinfo.frames) {
48 fprintf(stderr, __FILE__": Premature file end\n");
49 goto finish;
50 }
51
52 ret = 0;
53
54 finish:
55
56 if (sf)
57 sf_close(sf);
58
59 if (ret != 0 && chunk->memblock)
60 pa_memblock_unref(chunk->memblock);
61
62 return ret;
63
64 }