This repository has been archived on 2017-07-22. You can view files and clone it, but cannot push or open issues/pull-requests.
jlsampler/soundfile.c

49 lines
1.2 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <sndfile.h>
#include "jl_mem.h"
#include "const.h"
#include "soundfile.h"
int soundfile_load_stereo(const char *path, float **Lp, float **Rp) {
SF_INFO fileInfo;
fileInfo.format = 0;
fileInfo.channels = 2;
fileInfo.samplerate = 48000;
// Open the file.
SNDFILE *sndFile = sf_open(path, SFM_READ, &fileInfo);
if (sndFile == NULL) {
printf("Failed to open file: %s\n", path);
printf(" Error: %s\n", sf_strerror(sndFile));
exit(1);
}
// Check that the parameters are correct.
if (fileInfo.channels != 2 || fileInfo.samplerate != 48000) {
printf("File must be 2 channels and 48 kHz.\n");
exit(1);
}
int len = fileInfo.frames;
float *L = jl_malloc_exit(len * sizeof(float));
float *R = jl_malloc_exit(len * sizeof(float));
float buf[2];
for(int i = 0; i < len; ++i) {
if(sf_readf_float(sndFile, buf, 1) != 1) {
printf("Failed to read frame from file: %s\n", path);
exit(1);
}
L[i] = buf[0];
R[i] = buf[1];
}
sf_close(sndFile);
*Lp = L;
*Rp = R;
return len;
}