#define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include #include #include #include "audio.h" #include "audio_internal.h" #define DR_MP3_IMPLEMENTATION #define DR_MP3_FLOAT_OUTPUT #if defined(__GNUC__) || defined(__clang__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-function" #pragma GCC diagnostic ignored "-Wpedantic" #pragma GCC diagnostic ignored "-Wsign-compare" #pragma GCC diagnostic ignored "-Wmissing-field-initializers" #pragma GCC diagnostic ignored "-Wconversion" #pragma GCC diagnostic ignored "-Wfloat-equal" #pragma GCC diagnostic ignored "-Wcast-qual" #pragma GCC diagnostic ignored "-Wcast-align" #pragma GCC diagnostic ignored "-Wimplicit-fallthrough" #pragma GCC diagnostic ignored "-Wshadow" #pragma GCC diagnostic ignored "-Wformat" #endif #include "dr_mp3.h" #if defined(__GNUC__) || defined(__clang__) #pragma GCC diagnostic pop #endif #define MP3_SCRATCH_FRAMES 4096U typedef struct { drmp3 decoder; float scratch[MP3_SCRATCH_FRAMES * 2U]; } Mp3Source; static bool mp3_read_frames(AudioSource *source, float *stereo, size_t requested, size_t *frames_read, char *message, size_t message_size) { Mp3Source *mp3 = source->impl; uint64_t remaining = source->total_frames - source->frames_read; size_t produced = 0U; *frames_read = 0U; if ((uint64_t)requested > remaining) { requested = (size_t)remaining; } while (produced < requested) { size_t want = requested - produced; drmp3_uint64 got; size_t frame; if (want > MP3_SCRATCH_FRAMES) { want = MP3_SCRATCH_FRAMES; } got = drmp3_read_pcm_frames_f32(&mp3->decoder, (drmp3_uint64)want, mp3->scratch); if (got == 0U) { return set_message(message, message_size, "MP3 stream ended after %" PRIu64 " of %" PRIu64 " frames", source->frames_read + produced, source->total_frames); } for (frame = 0U; frame < (size_t)got; ++frame) { if (source->channels == 2U) { stereo[(produced + frame) * 2U] = mp3->scratch[frame * 2U]; stereo[(produced + frame) * 2U + 1U] = mp3->scratch[frame * 2U + 1U]; } else { stereo[(produced + frame) * 2U] = mp3->scratch[frame]; stereo[(produced + frame) * 2U + 1U] = mp3->scratch[frame]; } } produced += (size_t)got; if ((size_t)got < want) { return set_message(message, message_size, "MP3 stream ended after %" PRIu64 " of %" PRIu64 " frames", source->frames_read + produced, source->total_frames); } } source->frames_read += produced; *frames_read = produced; return true; } static void mp3_close(AudioSource *source) { Mp3Source *mp3 = source->impl; if (mp3 != NULL) { drmp3_uninit(&mp3->decoder); free(mp3); source->impl = NULL; } } static bool mp3_configure(AudioSource *source, Mp3Source *mp3, const char *path, char *message, size_t message_size) { uint32_t sample_rate = mp3->decoder.sampleRate; uint16_t channels = (uint16_t)mp3->decoder.channels; uint64_t total = 0U; if (channels != 1U && channels != 2U) { return set_message(message, message_size, "MP3 must contain one or two channels (found %u)", (unsigned)channels); } if (sample_rate < 8000U || sample_rate > 768000U) { return set_message(message, message_size, "unsupported MP3 sample rate: %" PRIu32, sample_rate); } for (;;) { drmp3_uint64 got = drmp3_read_pcm_frames_f32(&mp3->decoder, MP3_SCRATCH_FRAMES, mp3->scratch); if (got == 0U) { break; } if (!checked_add_u64(total, (uint64_t)got, &total)) { return set_message(message, message_size, "MP3 '%s' is too long", path); } } if (total == 0U) { return set_message(message, message_size, "MP3 '%s' contains no audio frames", path); } if (mp3->decoder.totalPCMFrameCount != DRMP3_UINT64_MAX) { drmp3_uint64 declared = drmp3_get_pcm_frame_count(&mp3->decoder); if (declared != 0U && (uint64_t)declared != total) { fprintf(stderr, "warning: MP3 '%s' declares %" PRIu64 " frames but only %" PRIu64 " could be decoded; the file may be truncated\n", path, (uint64_t)declared, total); } } if (!drmp3_seek_to_pcm_frame(&mp3->decoder, 0)) { return set_message(message, message_size, "cannot rewind MP3 '%s'", path); } source->sample_rate = sample_rate; source->channels = channels; source->total_frames = total; source->frames_read = 0U; return true; } static const AudioSourceOps MP3_OPS = { mp3_read_frames, mp3_close }; bool mp3_source_open(AudioSource *source, const char *path, char *message, size_t message_size) { Mp3Source *mp3 = calloc(1U, sizeof(*mp3)); if (mp3 == NULL) { return set_message(message, message_size, "cannot allocate the MP3 decoder: %s", strerror(errno)); } if (!drmp3_init_file(&mp3->decoder, path, NULL)) { free(mp3); return set_message(message, message_size, "cannot decode MP3 '%s': invalid or unsupported stream", path); } if (!mp3_configure(source, mp3, path, message, message_size)) { drmp3_uninit(&mp3->decoder); free(mp3); return false; } source->format_name = "MP3"; source->ops = &MP3_OPS; source->impl = mp3; return true; }