Add MP3
This commit is contained in:
parent
3b51b42e56
commit
32835b29d9
5 changed files with 59 additions and 0 deletions
53
external/st_audiofile/src/st_audiofile.c
vendored
53
external/st_audiofile/src/st_audiofile.c
vendored
|
|
@ -14,7 +14,12 @@ struct st_audio_file {
|
||||||
union {
|
union {
|
||||||
drwav *wav;
|
drwav *wav;
|
||||||
drflac *flac;
|
drflac *flac;
|
||||||
|
drmp3 *mp3;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
union {
|
||||||
|
struct { uint64_t frames; } mp3;
|
||||||
|
} cache;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
|
|
@ -47,6 +52,19 @@ st_audio_file* st_open_file(const char* filename)
|
||||||
af->type = st_audio_file_flac;
|
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) {
|
if (af->type == st_audio_file_null) {
|
||||||
free(af);
|
free(af);
|
||||||
af = NULL;
|
af = NULL;
|
||||||
|
|
@ -82,6 +100,19 @@ st_audio_file* st_open_file_w(const wchar_t* filename)
|
||||||
af->type = st_audio_file_flac;
|
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) {
|
if (af->type == st_audio_file_null) {
|
||||||
free(af);
|
free(af);
|
||||||
af = NULL;
|
af = NULL;
|
||||||
|
|
@ -101,6 +132,10 @@ void st_close(st_audio_file* af)
|
||||||
case st_audio_file_flac:
|
case st_audio_file_flac:
|
||||||
drflac_close(af->flac);
|
drflac_close(af->flac);
|
||||||
break;
|
break;
|
||||||
|
case st_audio_file_mp3:
|
||||||
|
drmp3_uninit(af->mp3);
|
||||||
|
free(af->mp3);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
af->type = st_audio_file_null;
|
af->type = st_audio_file_null;
|
||||||
|
|
@ -122,6 +157,9 @@ uint32_t st_get_channels(st_audio_file* af)
|
||||||
case st_audio_file_flac:
|
case st_audio_file_flac:
|
||||||
channels = af->flac->channels;
|
channels = af->flac->channels;
|
||||||
break;
|
break;
|
||||||
|
case st_audio_file_mp3:
|
||||||
|
channels = af->mp3->channels;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return channels;
|
return channels;
|
||||||
|
|
@ -138,6 +176,9 @@ float st_get_sample_rate(st_audio_file* af)
|
||||||
case st_audio_file_flac:
|
case st_audio_file_flac:
|
||||||
sample_rate = af->flac->sampleRate;
|
sample_rate = af->flac->sampleRate;
|
||||||
break;
|
break;
|
||||||
|
case st_audio_file_mp3:
|
||||||
|
sample_rate = af->mp3->sampleRate;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return sample_rate;
|
return sample_rate;
|
||||||
|
|
@ -154,6 +195,9 @@ uint64_t st_get_frame_count(st_audio_file* af)
|
||||||
case st_audio_file_flac:
|
case st_audio_file_flac:
|
||||||
frames = af->flac->totalPCMFrameCount;
|
frames = af->flac->totalPCMFrameCount;
|
||||||
break;
|
break;
|
||||||
|
case st_audio_file_mp3:
|
||||||
|
frames = af->cache.mp3.frames;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return frames;
|
return frames;
|
||||||
|
|
@ -170,6 +214,9 @@ bool st_seek(st_audio_file* af, uint64_t frame)
|
||||||
case st_audio_file_flac:
|
case st_audio_file_flac:
|
||||||
success = drflac_seek_to_pcm_frame(af->flac, frame);
|
success = drflac_seek_to_pcm_frame(af->flac, frame);
|
||||||
break;
|
break;
|
||||||
|
case st_audio_file_mp3:
|
||||||
|
success = drmp3_seek_to_pcm_frame(af->mp3, frame);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return success;
|
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:
|
case st_audio_file_flac:
|
||||||
count = drflac_read_pcm_frames_s16(af->flac, count, buffer);
|
count = drflac_read_pcm_frames_s16(af->flac, count, buffer);
|
||||||
break;
|
break;
|
||||||
|
case st_audio_file_mp3:
|
||||||
|
count = drmp3_read_pcm_frames_s16(af->mp3, count, buffer);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
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:
|
case st_audio_file_flac:
|
||||||
count = drflac_read_pcm_frames_f32(af->flac, count, buffer);
|
count = drflac_read_pcm_frames_f32(af->flac, count, buffer);
|
||||||
break;
|
break;
|
||||||
|
case st_audio_file_mp3:
|
||||||
|
count = drmp3_read_pcm_frames_f32(af->mp3, count, buffer);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
|
|
|
||||||
1
external/st_audiofile/src/st_audiofile.h
vendored
1
external/st_audiofile/src/st_audiofile.h
vendored
|
|
@ -24,6 +24,7 @@ typedef enum st_audio_file_type {
|
||||||
st_audio_file_wav,
|
st_audio_file_wav,
|
||||||
st_audio_file_flac,
|
st_audio_file_flac,
|
||||||
st_audio_file_ogg,
|
st_audio_file_ogg,
|
||||||
|
st_audio_file_mp3,
|
||||||
st_audio_file_other,
|
st_audio_file_other,
|
||||||
} st_audio_file_type;
|
} st_audio_file_type;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,9 @@ const char* st_type_string(int type)
|
||||||
case st_audio_file_ogg:
|
case st_audio_file_ogg:
|
||||||
type_string = "OGG";
|
type_string = "OGG";
|
||||||
break;
|
break;
|
||||||
|
case st_audio_file_mp3:
|
||||||
|
type_string = "MP3";
|
||||||
|
break;
|
||||||
case st_audio_file_other:
|
case st_audio_file_other:
|
||||||
type_string = "other";
|
type_string = "other";
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -6,4 +6,5 @@
|
||||||
|
|
||||||
#define DR_WAV_IMPLEMENTATION
|
#define DR_WAV_IMPLEMENTATION
|
||||||
#define DR_FLAC_IMPLEMENTATION
|
#define DR_FLAC_IMPLEMENTATION
|
||||||
|
#define DR_MP3_IMPLEMENTATION
|
||||||
#include "st_audiofile_libs.h"
|
#include "st_audiofile_libs.h"
|
||||||
|
|
|
||||||
|
|
@ -7,3 +7,4 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "dr_wav.h"
|
#include "dr_wav.h"
|
||||||
#include "dr_flac.h"
|
#include "dr_flac.h"
|
||||||
|
#include "dr_mp3.h"
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue