This commit is contained in:
Jean Pierre Cimalando 2020-10-09 15:41:08 +02:00
parent 3b51b42e56
commit 32835b29d9
5 changed files with 59 additions and 0 deletions

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -6,4 +6,5 @@
#define DR_WAV_IMPLEMENTATION
#define DR_FLAC_IMPLEMENTATION
#define DR_MP3_IMPLEMENTATION
#include "st_audiofile_libs.h"

View file

@ -7,3 +7,4 @@
#pragma once
#include "dr_wav.h"
#include "dr_flac.h"
#include "dr_mp3.h"