#define MINIMP3_FLOAT_OUTPUT #define MINIMP3_IMPLEMENTATION #include #include #include #include "load.h" #define ABORT(...) do { \ fprintf(stderr, __VA_ARGS__); \ free(mp3.buffer); \ return NULL; \ } while(0) struct mossrose_sound_t * load_mp3(const char *filename) { mp3dec_t mp3d; mp3dec_file_info_t mp3; if (mp3dec_load(&mp3d, filename, &mp3, NULL, NULL)) { fprintf(stderr, "failed to decode mp3 file '%s'\n", filename); return NULL; } printf("samples: %lu\n" "channels: %d\n" "hz: %d\n" "layer: %d\n" "bitrate: %d\n", mp3.samples, mp3.channels, mp3.hz, mp3.layer, mp3.avg_bitrate_kbps); if (mp3.channels != 1 && mp3.channels != 2) { ABORT("files with %d audio tracks are not supported!\n", mp3.channels); } struct mossrose_sound_t *sound = malloc(sizeof(struct mossrose_sound_t)); if (sound == NULL) ABORT("failed to allocate sound structure\n"); if (mp3.channels == 1) { sound->mono = true; sound->left = malloc(mp3.samples * sizeof(float)); if (sound->left == NULL) { free(sound); ABORT("failed to allocate mono audio data!\n"); } memcpy(sound->left, mp3.buffer, mp3.samples * sizeof(float)); sound->right = NULL; sound->len = mp3.samples; } else { /* stereo */ sound->mono = false; size_t sz = (mp3.samples/2) * sizeof(float); sound->left = malloc(sz); if (sound->left == NULL) { free(sound); ABORT("failed to allocate left audio channel!\n"); } sound->right = malloc(sz); if (sound->right == NULL) { free(sound->left); free(sound); ABORT("failed to allocate right audio channel!\n"); } for (int i=0; ileft[i/2] = mp3.buffer[i]; else sound->right[i/2] = mp3.buffer[i]; } sound->len = mp3.samples/2; } free(mp3.buffer); return sound; }