|
| 1 | +#include "AudioDecoder.h" |
| 2 | + |
| 3 | +// dr_mp3 / dr_wav / stb_vorbis: public domain のヘッダオンリーデコーダ (third_party/)。 |
| 4 | +// メモリ入力のみ使用する。実装は本 TU に集約する (ODR 一意)。 |
| 5 | +#define DR_MP3_IMPLEMENTATION |
| 6 | +#define DR_MP3_NO_STDIO |
| 7 | +#include "third_party/dr_mp3.h" |
| 8 | + |
| 9 | +#define DR_WAV_IMPLEMENTATION |
| 10 | +#define DR_WAV_NO_STDIO |
| 11 | +#include "third_party/dr_wav.h" |
| 12 | + |
| 13 | +// stb_vorbis は .c 配布のためここで取り込む (STB_VORBIS_HEADER_ONLY は使わない)。 |
| 14 | +#define STB_VORBIS_NO_STDIO |
| 15 | +#define STB_VORBIS_NO_PUSHDATA_API |
| 16 | +#include "third_party/stb_vorbis.c" |
| 17 | + |
| 18 | +#include <cstring> |
| 19 | + |
| 20 | +namespace next2d { |
| 21 | + |
| 22 | +namespace { |
| 23 | + |
| 24 | +bool DecodeMp3(const std::vector<uint8_t>& input, PcmBuffer& out) |
| 25 | +{ |
| 26 | + drmp3_config config = {}; |
| 27 | + drmp3_uint64 total_frames = 0; |
| 28 | + float* frames = drmp3_open_memory_and_read_pcm_frames_f32( |
| 29 | + input.data(), input.size(), &config, &total_frames, nullptr); |
| 30 | + if (!frames || total_frames == 0 || config.channels == 0) { |
| 31 | + if (frames) { |
| 32 | + drmp3_free(frames, nullptr); |
| 33 | + } |
| 34 | + return false; |
| 35 | + } |
| 36 | + out.channels = config.channels; |
| 37 | + out.sample_rate = config.sampleRate; |
| 38 | + out.samples.assign(frames, frames + total_frames * config.channels); |
| 39 | + drmp3_free(frames, nullptr); |
| 40 | + return true; |
| 41 | +} |
| 42 | + |
| 43 | +bool DecodeWav(const std::vector<uint8_t>& input, PcmBuffer& out) |
| 44 | +{ |
| 45 | + unsigned int channels = 0, sample_rate = 0; |
| 46 | + drwav_uint64 total_frames = 0; |
| 47 | + float* frames = drwav_open_memory_and_read_pcm_frames_f32( |
| 48 | + input.data(), input.size(), &channels, &sample_rate, &total_frames, nullptr); |
| 49 | + if (!frames || total_frames == 0 || channels == 0) { |
| 50 | + if (frames) { |
| 51 | + drwav_free(frames, nullptr); |
| 52 | + } |
| 53 | + return false; |
| 54 | + } |
| 55 | + out.channels = channels; |
| 56 | + out.sample_rate = sample_rate; |
| 57 | + out.samples.assign(frames, frames + total_frames * channels); |
| 58 | + drwav_free(frames, nullptr); |
| 59 | + return true; |
| 60 | +} |
| 61 | + |
| 62 | +bool DecodeOgg(const std::vector<uint8_t>& input, PcmBuffer& out) |
| 63 | +{ |
| 64 | + int err = 0; |
| 65 | + stb_vorbis* vorbis = stb_vorbis_open_memory( |
| 66 | + input.data(), static_cast<int>(input.size()), &err, nullptr); |
| 67 | + if (!vorbis) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + const stb_vorbis_info info = stb_vorbis_get_info(vorbis); |
| 71 | + const unsigned int total_frames = stb_vorbis_stream_length_in_samples(vorbis); |
| 72 | + if (info.channels <= 0 || total_frames == 0) { |
| 73 | + stb_vorbis_close(vorbis); |
| 74 | + return false; |
| 75 | + } |
| 76 | + out.channels = static_cast<uint32_t>(info.channels); |
| 77 | + out.sample_rate = info.sample_rate; |
| 78 | + out.samples.resize(static_cast<size_t>(total_frames) * info.channels); |
| 79 | + const int read = stb_vorbis_get_samples_float_interleaved( |
| 80 | + vorbis, info.channels, out.samples.data(), |
| 81 | + static_cast<int>(out.samples.size())); |
| 82 | + stb_vorbis_close(vorbis); |
| 83 | + if (read <= 0) { |
| 84 | + out.samples.clear(); |
| 85 | + return false; |
| 86 | + } |
| 87 | + out.samples.resize(static_cast<size_t>(read) * info.channels); |
| 88 | + return true; |
| 89 | +} |
| 90 | + |
| 91 | +} // namespace |
| 92 | + |
| 93 | +bool DecodeAudio(const std::vector<uint8_t>& input, PcmBuffer& out) |
| 94 | +{ |
| 95 | + if (input.size() < 12) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + const uint8_t* d = input.data(); |
| 99 | + |
| 100 | + // マジックナンバーで形式を判定して該当デコーダを先に試し、 |
| 101 | + // 外れたら残りをカスケード (誤判定・ヘッダ無し MP3 の保険)。 |
| 102 | + const bool looks_wav = std::memcmp(d, "RIFF", 4) == 0 && std::memcmp(d + 8, "WAVE", 4) == 0; |
| 103 | + const bool looks_ogg = std::memcmp(d, "OggS", 4) == 0; |
| 104 | + const bool looks_mp3 = std::memcmp(d, "ID3", 3) == 0 |
| 105 | + || (d[0] == 0xFF && (d[1] & 0xE0) == 0xE0); |
| 106 | + |
| 107 | + if (looks_wav && DecodeWav(input, out)) { |
| 108 | + return true; |
| 109 | + } |
| 110 | + if (looks_ogg && DecodeOgg(input, out)) { |
| 111 | + return true; |
| 112 | + } |
| 113 | + if (looks_mp3 && DecodeMp3(input, out)) { |
| 114 | + return true; |
| 115 | + } |
| 116 | + |
| 117 | + // 判定が外れた場合のカスケード |
| 118 | + return DecodeWav(input, out) || DecodeOgg(input, out) || DecodeMp3(input, out); |
| 119 | +} |
| 120 | + |
| 121 | +} // namespace next2d |
0 commit comments