WIP: seeking in lua example

custom
ailurux 1 year ago
parent a49d754da6
commit 665679b885
  1. 3
      lua/playing.lua
  2. 3
      src/audio/audio_decoder.cpp
  3. 6
      src/audio/audio_fsm.cpp
  4. 7
      src/audio/audio_source.cpp
  5. 2
      src/audio/fatfs_audio_input.cpp
  6. 3
      src/audio/include/audio_events.hpp
  7. 7
      src/audio/include/audio_source.hpp
  8. 5
      src/codecs/wav.cpp
  9. 16
      src/ui/ui_fsm.cpp

@ -147,7 +147,8 @@ return function(opts)
local play_pause_btn = controls:Button {} local play_pause_btn = controls:Button {}
play_pause_btn:onClicked(function() play_pause_btn:onClicked(function()
playback.playing:set(not playback.playing:get()) --playback.playing:set(not playback.playing:get())
playback.position:set(playback.position:get() + 5)
end) end)
play_pause_btn:focus() play_pause_btn:focus()
local play_pause_img = play_pause_btn:Image { src = img.pause } local play_pause_img = play_pause_btn:Image { src = img.pause }

@ -148,16 +148,15 @@ auto Decoder::BeginDecoding(std::shared_ptr<TaggedStream> stream) -> bool {
ESP_LOGI(kTag, "stream started ok"); ESP_LOGI(kTag, "stream started ok");
events::Audio().Dispatch(internal::InputFileOpened{}); events::Audio().Dispatch(internal::InputFileOpened{});
// TODO: How does this need to change?
auto tags = std::make_shared<Track>(Track{ auto tags = std::make_shared<Track>(Track{
.tags = stream->tags(), .tags = stream->tags(),
.db_info = {}, .db_info = {},
.bitrate_kbps = open_res->sample_rate_hz, .bitrate_kbps = open_res->sample_rate_hz,
.encoding = stream->type(), .encoding = stream->type(),
.filepath = stream->Filepath(),
}); });
timer_.reset(new Timer(tags, open_res.value(), stream->Offset())); timer_.reset(new Timer(tags, open_res.value(), stream->Offset()));
// TODO: How does *this?* need to change?
PlaybackUpdate ev{.seconds_elapsed = stream->Offset(), .track = tags}; PlaybackUpdate ev{.seconds_elapsed = stream->Offset(), .track = tags};
events::Audio().Dispatch(ev); events::Audio().Dispatch(ev);
events::Ui().Dispatch(ev); events::Ui().Dispatch(ev);

@ -246,14 +246,16 @@ void Uninitialised::react(const system_fsm::BootComplete& ev) {
void Standby::react(const PlayFile& ev) { void Standby::react(const PlayFile& ev) {
sCurrentTrack = 0; sCurrentTrack = 0;
sIsPlaybackAllowed = true; sIsPlaybackAllowed = true;
sFileSource->SetPath(ev.filename, 10); sFileSource->SetPath(ev.filename);
} }
void Playback::react(const PlayFile& ev) { void Playback::react(const PlayFile& ev) {
sFileSource->SetPath(ev.filename, 10); sFileSource->SetPath(ev.filename);
} }
void Standby::react(const SeekFile& ev) { void Standby::react(const SeekFile& ev) {
sCurrentTrack = 0;
sIsPlaybackAllowed = true;
sFileSource->SetPath(ev.filename, ev.offset); sFileSource->SetPath(ev.filename, ev.offset);
} }

@ -12,8 +12,9 @@ namespace audio {
TaggedStream::TaggedStream(std::shared_ptr<database::TrackTags> t, TaggedStream::TaggedStream(std::shared_ptr<database::TrackTags> t,
std::unique_ptr<codecs::IStream> w, std::unique_ptr<codecs::IStream> w,
std::string filepath,
uint32_t offset) uint32_t offset)
: codecs::IStream(w->type()), tags_(t), wrapped_(std::move(w)), offset_(offset) {} : codecs::IStream(w->type()), tags_(t), wrapped_(std::move(w)), filepath_(filepath), offset_(offset) {}
auto TaggedStream::tags() -> std::shared_ptr<database::TrackTags> { auto TaggedStream::tags() -> std::shared_ptr<database::TrackTags> {
return tags_; return tags_;
@ -43,6 +44,10 @@ auto TaggedStream::Offset() -> uint32_t {
return offset_; return offset_;
} }
auto TaggedStream::Filepath() -> std::string {
return filepath_;
}
auto TaggedStream::SetPreambleFinished() -> void { auto TaggedStream::SetPreambleFinished() -> void {
wrapped_->SetPreambleFinished(); wrapped_->SetPreambleFinished();
} }

@ -136,7 +136,7 @@ auto FatfsAudioInput::OpenFile(const std::string& path,uint32_t offset) -> bool
auto source = auto source =
std::make_unique<FatfsSource>(stream_type.value(), std::move(file)); std::make_unique<FatfsSource>(stream_type.value(), std::move(file));
new_stream_.reset(new TaggedStream(tags, std::move(source), offset)); new_stream_.reset(new TaggedStream(tags, std::move(source), path, offset));
return true; return true;
} }

@ -26,6 +26,7 @@ struct Track {
uint32_t duration; uint32_t duration;
uint32_t bitrate_kbps; uint32_t bitrate_kbps;
codecs::StreamType encoding; codecs::StreamType encoding;
std::string filepath;
}; };
struct PlaybackStarted : tinyfsm::Event {}; struct PlaybackStarted : tinyfsm::Event {};
@ -46,8 +47,8 @@ struct PlayFile : tinyfsm::Event {
}; };
struct SeekFile : tinyfsm::Event { struct SeekFile : tinyfsm::Event {
std::string filename;
uint32_t offset; uint32_t offset;
std::string filename;
}; };
struct StepUpVolume : tinyfsm::Event {}; struct StepUpVolume : tinyfsm::Event {};

@ -17,7 +17,9 @@ class TaggedStream : public codecs::IStream {
public: public:
TaggedStream(std::shared_ptr<database::TrackTags>, TaggedStream(std::shared_ptr<database::TrackTags>,
std::unique_ptr<codecs::IStream> wrapped, std::unique_ptr<codecs::IStream> wrapped,
uint32_t offset = 0); std::string path,
uint32_t offset = 0
);
auto tags() -> std::shared_ptr<database::TrackTags>; auto tags() -> std::shared_ptr<database::TrackTags>;
@ -33,11 +35,14 @@ class TaggedStream : public codecs::IStream {
auto Offset() -> uint32_t; auto Offset() -> uint32_t;
auto Filepath() -> std::string;
auto SetPreambleFinished() -> void override; auto SetPreambleFinished() -> void override;
private: private:
std::shared_ptr<database::TrackTags> tags_; std::shared_ptr<database::TrackTags> tags_;
std::unique_ptr<codecs::IStream> wrapped_; std::unique_ptr<codecs::IStream> wrapped_;
std::string filepath_;
int32_t offset_; int32_t offset_;
}; };

@ -218,7 +218,6 @@ auto WavDecoder::DecodeTo(cpp::span<sample::Sample> output)
buffer_.ConsumeBytes([&](cpp::span<std::byte> buf) -> size_t { buffer_.ConsumeBytes([&](cpp::span<std::byte> buf) -> size_t {
size_t bytes_read = buf.size_bytes(); size_t bytes_read = buf.size_bytes();
ESP_LOGI(kTag, "Bytes read: %d", bytes_read);
size_t frames_read = size_t frames_read =
bytes_read / bytes_per_sample_ / output_format_.num_channels; bytes_read / bytes_per_sample_ / output_format_.num_channels;
@ -244,10 +243,6 @@ auto WavDecoder::DecodeTo(cpp::span<sample::Sample> output)
return samples_written * bytes_per_sample_; return samples_written * bytes_per_sample_;
}); });
ESP_LOGI(kTag, "Samples written %d", samples_written);
if (is_eof) {
ESP_LOGI(kTag, "EOF");
}
return OutputInfo{.samples_written = samples_written, return OutputInfo{.samples_written = samples_written,
.is_stream_finished = samples_written == 0 && is_eof}; .is_stream_finished = samples_written == 0 && is_eof};

@ -123,7 +123,21 @@ lua::Property UiState::sPlaybackPlaying{
}}; }};
lua::Property UiState::sPlaybackTrack{}; lua::Property UiState::sPlaybackTrack{};
lua::Property UiState::sPlaybackPosition{0}; lua::Property UiState::sPlaybackPosition{0, [](const lua::LuaValue& val) {
int current_val = std::get<int>(sPlaybackPosition.Get());
if (!std::holds_alternative<int>(val)) {
return false;
}
int new_val = std::get<int>(val);
if (current_val != new_val) {
auto track = sPlaybackTrack.Get();
if (!std::holds_alternative<audio::Track>(track)) {
return false;
}
events::Audio().Dispatch(audio::SeekFile{.offset = (uint32_t)new_val, .filename = std::get<audio::Track>(track).filepath});
}
return true;
}};
lua::Property UiState::sQueuePosition{0}; lua::Property UiState::sQueuePosition{0};
lua::Property UiState::sQueueSize{0}; lua::Property UiState::sQueueSize{0};

Loading…
Cancel
Save