From 32835b29d9a5e911d5162184677ff21e960f639d Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Fri, 9 Oct 2020 15:41:08 +0200 Subject: [PATCH] Add MP3 --- external/st_audiofile/src/st_audiofile.c | 53 +++++++++++++++++++ external/st_audiofile/src/st_audiofile.h | 1 + .../st_audiofile/src/st_audiofile_common.c | 3 ++ external/st_audiofile/src/st_audiofile_libs.c | 1 + external/st_audiofile/src/st_audiofile_libs.h | 1 + 5 files changed, 59 insertions(+) diff --git a/external/st_audiofile/src/st_audiofile.c b/external/st_audiofile/src/st_audiofile.c index efaccb85..671e8618 100644 --- a/external/st_audiofile/src/st_audiofile.c +++ b/external/st_audiofile/src/st_audiofile.c @@ -14,7 +14,12 @@ struct st_audio_file { union { drwav *wav; drflac *flac; + drmp3 *mp3; }; + + union { + struct { uint64_t frames; } mp3; + } cache; }; enum { @@ -47,6 +52,19 @@ st_audio_file* st_open_file(const char* filename) af->type = st_audio_file_flac; } + if (af->type == st_audio_file_null) { + af->mp3 = (drmp3*)malloc(sizeof(drmp3)); + if (!af->mp3) { + free(af); + return NULL; + } + if (!drmp3_init_file(af->mp3, filename, NULL) || + (af->cache.mp3.frames = drmp3_get_pcm_frame_count(af->mp3)) == 0) + free(af->mp3); + else + af->type = st_audio_file_mp3; + } + if (af->type == st_audio_file_null) { free(af); af = NULL; @@ -82,6 +100,19 @@ st_audio_file* st_open_file_w(const wchar_t* filename) af->type = st_audio_file_flac; } + if (af->type == st_audio_file_null) { + af->mp3 = (drmp3*)malloc(sizeof(drmp3)); + if (!af->mp3) { + free(af); + return NULL; + } + if (!drmp3_init_file_w(af->mp3, filename, NULL) || + (af->cache.mp3.frames = drmp3_get_pcm_frame_count(af->mp3)) == 0) + free(af->mp3); + else + af->type = st_audio_file_mp3; + } + if (af->type == st_audio_file_null) { free(af); af = NULL; @@ -101,6 +132,10 @@ void st_close(st_audio_file* af) case st_audio_file_flac: drflac_close(af->flac); break; + case st_audio_file_mp3: + drmp3_uninit(af->mp3); + free(af->mp3); + break; } af->type = st_audio_file_null; @@ -122,6 +157,9 @@ uint32_t st_get_channels(st_audio_file* af) case st_audio_file_flac: channels = af->flac->channels; break; + case st_audio_file_mp3: + channels = af->mp3->channels; + break; } return channels; @@ -138,6 +176,9 @@ float st_get_sample_rate(st_audio_file* af) case st_audio_file_flac: sample_rate = af->flac->sampleRate; break; + case st_audio_file_mp3: + sample_rate = af->mp3->sampleRate; + break; } return sample_rate; @@ -154,6 +195,9 @@ uint64_t st_get_frame_count(st_audio_file* af) case st_audio_file_flac: frames = af->flac->totalPCMFrameCount; break; + case st_audio_file_mp3: + frames = af->cache.mp3.frames; + break; } return frames; @@ -170,6 +214,9 @@ bool st_seek(st_audio_file* af, uint64_t frame) case st_audio_file_flac: success = drflac_seek_to_pcm_frame(af->flac, frame); break; + case st_audio_file_mp3: + success = drmp3_seek_to_pcm_frame(af->mp3, frame); + break; } return success; @@ -184,6 +231,9 @@ uint64_t st_read_s16(st_audio_file* af, int16_t* buffer, uint64_t count) case st_audio_file_flac: count = drflac_read_pcm_frames_s16(af->flac, count, buffer); break; + case st_audio_file_mp3: + count = drmp3_read_pcm_frames_s16(af->mp3, count, buffer); + break; } return count; @@ -198,6 +248,9 @@ uint64_t st_read_f32(st_audio_file* af, float* buffer, uint64_t count) case st_audio_file_flac: count = drflac_read_pcm_frames_f32(af->flac, count, buffer); break; + case st_audio_file_mp3: + count = drmp3_read_pcm_frames_f32(af->mp3, count, buffer); + break; } return count; diff --git a/external/st_audiofile/src/st_audiofile.h b/external/st_audiofile/src/st_audiofile.h index 704addb8..fd62c77f 100644 --- a/external/st_audiofile/src/st_audiofile.h +++ b/external/st_audiofile/src/st_audiofile.h @@ -24,6 +24,7 @@ typedef enum st_audio_file_type { st_audio_file_wav, st_audio_file_flac, st_audio_file_ogg, + st_audio_file_mp3, st_audio_file_other, } st_audio_file_type; diff --git a/external/st_audiofile/src/st_audiofile_common.c b/external/st_audiofile/src/st_audiofile_common.c index 2b6e5d34..d8e2ac6e 100644 --- a/external/st_audiofile/src/st_audiofile_common.c +++ b/external/st_audiofile/src/st_audiofile_common.c @@ -26,6 +26,9 @@ const char* st_type_string(int type) case st_audio_file_ogg: type_string = "OGG"; break; + case st_audio_file_mp3: + type_string = "MP3"; + break; case st_audio_file_other: type_string = "other"; break; diff --git a/external/st_audiofile/src/st_audiofile_libs.c b/external/st_audiofile/src/st_audiofile_libs.c index 77ae28c2..4c39e703 100644 --- a/external/st_audiofile/src/st_audiofile_libs.c +++ b/external/st_audiofile/src/st_audiofile_libs.c @@ -6,4 +6,5 @@ #define DR_WAV_IMPLEMENTATION #define DR_FLAC_IMPLEMENTATION +#define DR_MP3_IMPLEMENTATION #include "st_audiofile_libs.h" diff --git a/external/st_audiofile/src/st_audiofile_libs.h b/external/st_audiofile/src/st_audiofile_libs.h index 8620cdce..16f329b0 100644 --- a/external/st_audiofile/src/st_audiofile_libs.h +++ b/external/st_audiofile/src/st_audiofile_libs.h @@ -7,3 +7,4 @@ #pragma once #include "dr_wav.h" #include "dr_flac.h" +#include "dr_mp3.h"