diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-06-07 15:39:28 +1000 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-06-07 15:39:28 +1000 |
| commit | e12ac1d9632856237388614bf393c7338500e00a (patch) | |
| tree | 9f353c516427225906eafa9704f312dd12174a46 /src/audio/fatfs_audio_input.cpp | |
| parent | 7e96482087632278c3d9e4a5db6bad25374ada8f (diff) | |
| download | tangara-fw-e12ac1d9632856237388614bf393c7338500e00a.tar.gz | |
Do some prep cleanup for multiple filetypes
Diffstat (limited to 'src/audio/fatfs_audio_input.cpp')
| -rw-r--r-- | src/audio/fatfs_audio_input.cpp | 69 |
1 files changed, 61 insertions, 8 deletions
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 <stdint.h> #include <algorithm> #include <cstdint> @@ -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<uint8_t>(*tags.channels), + .bits_per_sample = static_cast<uint8_t>(*tags.bits_per_sample), + .sample_rate = static_cast<uint32_t>(*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<InputStream>& 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<InputStream>& 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<InputFileFinished, AudioState>({}); } } +auto FatfsAudioInput::ContainerToStreamType(database::Encoding enc) + -> std::optional<codecs::StreamType> { + 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 |
