From e12ac1d9632856237388614bf393c7338500e00a Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 7 Jun 2023 15:39:28 +1000 Subject: Do some prep cleanup for multiple filetypes --- src/audio/fatfs_audio_input.cpp | 69 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 8 deletions(-) (limited to 'src/audio/fatfs_audio_input.cpp') diff --git a/src/audio/fatfs_audio_input.cpp b/src/audio/fatfs_audio_input.cpp index 9affcf1a..a89858ca 100644 --- a/src/audio/fatfs_audio_input.cpp +++ b/src/audio/fatfs_audio_input.cpp @@ -5,6 +5,7 @@ */ #include "fatfs_audio_input.hpp" +#include #include #include @@ -23,10 +24,12 @@ #include "audio_element.hpp" #include "chunk.hpp" +#include "song.hpp" #include "stream_buffer.hpp" #include "stream_event.hpp" #include "stream_info.hpp" #include "stream_message.hpp" +#include "tag_parser.hpp" #include "types.hpp" static const char* kTag = "SRC"; @@ -34,7 +37,11 @@ static const char* kTag = "SRC"; namespace audio { FatfsAudioInput::FatfsAudioInput() - : IAudioElement(), current_file_(), is_file_open_(false) {} + : IAudioElement(), + current_file_(), + is_file_open_(false), + current_container_(), + current_format_() {} FatfsAudioInput::~FatfsAudioInput() {} @@ -44,6 +51,36 @@ auto FatfsAudioInput::OpenFile(const std::string& path) -> bool { is_file_open_ = false; } ESP_LOGI(kTag, "opening file %s", path.c_str()); + + database::TagParserImpl tag_parser; + database::SongTags tags; + if (!tag_parser.ReadAndParseTags(path, &tags)) { + ESP_LOGE(kTag, "failed to read tags"); + return false; + } + + auto stream_type = ContainerToStreamType(tags.encoding); + if (!stream_type.has_value()) { + return false; + } + + current_container_ = tags.encoding; + + if (*stream_type == codecs::StreamType::kPcm && tags.channels && + tags.bits_per_sample && tags.channels) { + // WAV files are a special case bc they contain raw PCM streams. These don't + // need decoding, but we *do* need to parse the PCM format from the header. + // TODO(jacqueline): Maybe we should have a decoder for this just to deal + // with endianness differences? + current_format_ = StreamInfo::Pcm{ + .channels = static_cast(*tags.channels), + .bits_per_sample = static_cast(*tags.bits_per_sample), + .sample_rate = static_cast(*tags.sample_rate), + }; + } else { + current_format_ = StreamInfo::Encoded{*stream_type}; + } + FRESULT res = f_open(¤t_file_, path.c_str(), FA_READ); if (res != FR_OK) { ESP_LOGE(kTag, "failed to open file! res: %i", res); @@ -61,13 +98,10 @@ auto FatfsAudioInput::NeedsToProcess() const -> bool { auto FatfsAudioInput::Process(const std::vector& inputs, OutputStream* output) -> void { if (!is_file_open_) { - // TODO(jacqueline): should we clear the stream format? - // output->prepare({}); return; } - StreamInfo::Format format = StreamInfo::Encoded{codecs::STREAM_MP3}; - if (!output->prepare(format)) { + if (!output->prepare(*current_format_)) { return; } @@ -91,12 +125,31 @@ auto FatfsAudioInput::Process(const std::vector& inputs, f_close(¤t_file_); is_file_open_ = false; - // TODO(jacqueline): MP3 only - std::fill_n(output->data().begin(), 8, std::byte(0)); - output->add(8); + // HACK: libmad requires an 8 byte padding at the end of each file. + if (current_container_ == database::Encoding::kMp3) { + std::fill_n(output->data().begin(), 8, std::byte(0)); + output->add(8); + } events::Dispatch({}); } } +auto FatfsAudioInput::ContainerToStreamType(database::Encoding enc) + -> std::optional { + switch (enc) { + case database::Encoding::kMp3: + return codecs::StreamType::kMp3; + case database::Encoding::kWav: + return codecs::StreamType::kPcm; + case database::Encoding::kFlac: + return codecs::StreamType::kFlac; + case database::Encoding::kOgg: + return codecs::StreamType::kOgg; + case database::Encoding::kUnsupported: + default: + return {}; + } +} + } // namespace audio -- cgit v1.2.3