diff options
author | Lennart Poettering <lennart@poettering.net> | 2004-08-04 16:42:37 +0000 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2004-08-04 16:42:37 +0000 |
commit | 839f99ffbf5b01e585845a061830a6fa5f0743dc (patch) | |
tree | fb5bc8e07b337b7454dc897d2cc56e840b56e592 /polyp/sound-file.c | |
parent | 46091a9237f17f4295dca7140d8d70b4fce8b357 (diff) |
forgot some files
git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@104 fefdeb5f-60dc-0310-8127-8f9354f1896f
Diffstat (limited to 'polyp/sound-file.c')
-rw-r--r-- | polyp/sound-file.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/polyp/sound-file.c b/polyp/sound-file.c new file mode 100644 index 00000000..b0df7185 --- /dev/null +++ b/polyp/sound-file.c @@ -0,0 +1,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) { + 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); + 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; + +} |