37 lines
1.1 KiB
C
37 lines
1.1 KiB
C
|
#include <audiofile.h>
|
||
|
|
||
|
SNDFILE *audiofile_read(const char *path, SF_INFO *sfinfo) {
|
||
|
SNDFILE *sndfile = sf_open(path, SFM_READ, sfinfo);
|
||
|
if (sndfile == NULL) {
|
||
|
fprintf(stderr, "Failed to read audio file: %s\n", sf_strerror(NULL));
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
if (sfinfo->channels != 2) {
|
||
|
fprintf(stderr, "File must have 2 channels\n");
|
||
|
sf_close(sndfile);
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
int seconds = (double)sfinfo->frames / sfinfo->samplerate;
|
||
|
int minutes = seconds / 60;
|
||
|
printf("Duration: %d:%d\n", minutes, seconds % 60);
|
||
|
printf("Sample rate: %dHz\n\n", sfinfo->samplerate);
|
||
|
|
||
|
return sndfile;
|
||
|
}
|
||
|
|
||
|
SNDFILE *audiofile_open_for_write(const char *path, SF_INFO *og_info) {
|
||
|
SF_INFO sfinfo = {0};
|
||
|
sfinfo.samplerate = og_info->samplerate;
|
||
|
sfinfo.format = og_info->format;
|
||
|
sfinfo.channels = og_info->channels;
|
||
|
|
||
|
SNDFILE *sndfile = sf_open(path, SFM_WRITE, &sfinfo);
|
||
|
if (sndfile == NULL) {
|
||
|
fprintf(stderr, "Failed to open audio file: %s\n", sf_strerror(NULL));
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
return sndfile;
|
||
|
}
|