summaryrefslogtreecommitdiffstats
path: root/polyp/sound-file.c
blob: bf635fa087c19adc916b5aa41b7e2e7bc10e53e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <string.h>
#include <assert.h>

#include <sndfile.h>

#include "sound-file.h"
#include "sample.h"

#define MAX_FILE_SIZE (1024*1024)

int pa_sound_file_load(const char *fname, struct pa_sample_spec *ss, struct pa_memchunk *chunk, struct pa_memblock_stat *s) {
    SNDFILE*sf = NULL;
    SF_INFO sfinfo;
    int ret = -1;
    size_t l;
    assert(fname && ss && chunk);

    memset(&sfinfo, 0, sizeof(sfinfo));

    chunk->memblock = NULL;
    chunk->index = chunk->length = 0;
    
    if (!(sf = sf_open(fname, SFM_READ, &sfinfo))) {
        fprintf(stderr, __FILE__": Failed to open file %s\n", fname);
        goto finish;
    }

    ss->format = PA_SAMPLE_FLOAT32;
    ss->rate = sfinfo.samplerate;
    ss->channels = sfinfo.channels;

    if (!pa_sample_spec_valid(ss)) {
        fprintf(stderr, __FILE__": Unsupported sample format in file %s\n", fname);
        goto finish;
    }
    
    if ((l = pa_frame_size(ss)*sfinfo.frames) > MAX_FILE_SIZE) {
        fprintf(stderr, __FILE__": File to large\n");
        goto finish;
    }

    chunk->memblock = pa_memblock_new(l, s);
    assert(chunk->memblock);
    chunk->index = 0;
    chunk->length = l;

    if (sf_readf_float(sf, chunk->memblock->data, sfinfo.frames) != sfinfo.frames) {
        fprintf(stderr, __FILE__": Premature file end\n");
        goto finish;
    }

    ret = 0;

finish:

    if (sf)
        sf_close(sf);

    if (ret != 0 && chunk->memblock)
        pa_memblock_unref(chunk->memblock);
    
    return ret;
    
}