summaryrefslogtreecommitdiff
path: root/src/audio
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio')
-rw-r--r--src/audio/audio_fsm.cpp25
-rw-r--r--src/audio/bt_audio_output.cpp6
-rw-r--r--src/audio/include/audio_events.hpp2
-rw-r--r--src/audio/include/audio_fsm.hpp2
-rw-r--r--src/audio/include/bt_audio_output.hpp4
5 files changed, 32 insertions, 7 deletions
diff --git a/src/audio/audio_fsm.cpp b/src/audio/audio_fsm.cpp
index af43c9b9..06b98420 100644
--- a/src/audio/audio_fsm.cpp
+++ b/src/audio/audio_fsm.cpp
@@ -26,6 +26,7 @@
#include "future_fetcher.hpp"
#include "i2s_audio_output.hpp"
#include "i2s_dac.hpp"
+#include "nvs.hpp"
#include "service_locator.hpp"
#include "system_events.hpp"
#include "track.hpp"
@@ -42,6 +43,7 @@ std::shared_ptr<FatfsAudioInput> AudioState::sFileSource;
std::unique_ptr<Decoder> AudioState::sDecoder;
std::shared_ptr<SampleConverter> AudioState::sSampleConverter;
std::shared_ptr<I2SAudioOutput> AudioState::sI2SOutput;
+std::shared_ptr<BluetoothAudioOutput> AudioState::sBtOutput;
std::shared_ptr<IAudioOutput> AudioState::sOutput;
std::optional<database::TrackId> AudioState::sCurrentTrack;
@@ -75,6 +77,20 @@ void AudioState::react(const ChangeMaxVolume& ev) {
sServices->nvs().AmpMaxVolume(ev.new_max);
}
+void AudioState::react(const OutputModeChanged& ev) {
+ // TODO: handle SetInUse
+ ESP_LOGI(kTag, "output mode changed");
+ auto new_mode = sServices->nvs().OutputMode();
+ switch (new_mode.get()) {
+ case drivers::NvsStorage::Output::kBluetooth:
+ sOutput = sBtOutput;
+ break;
+ case drivers::NvsStorage::Output::kHeadphones:
+ sOutput = sI2SOutput;
+ break;
+ }
+}
+
namespace states {
void Uninitialised::react(const system_fsm::BootComplete& ev) {
@@ -90,13 +106,18 @@ void Uninitialised::react(const system_fsm::BootComplete& ev) {
sFileSource.reset(new FatfsAudioInput(sServices->tag_parser()));
sI2SOutput.reset(new I2SAudioOutput(sServices->gpios(),
std::unique_ptr<drivers::I2SDac>{*dac}));
+ sBtOutput.reset(new BluetoothAudioOutput(sServices->bluetooth()));
auto& nvs = sServices->nvs();
sI2SOutput->SetMaxVolume(nvs.AmpMaxVolume().get());
sI2SOutput->SetVolumeDb(nvs.AmpCurrentVolume().get());
- sOutput = sI2SOutput;
- // sOutput.reset(new BluetoothAudioOutput(bluetooth));
+ if (sServices->nvs().OutputMode().get() ==
+ drivers::NvsStorage::Output::kHeadphones) {
+ sOutput = sI2SOutput;
+ } else {
+ sOutput = sBtOutput;
+ }
sSampleConverter.reset(new SampleConverter());
sSampleConverter->SetOutput(sOutput);
diff --git a/src/audio/bt_audio_output.cpp b/src/audio/bt_audio_output.cpp
index 2e54f69a..374906fd 100644
--- a/src/audio/bt_audio_output.cpp
+++ b/src/audio/bt_audio_output.cpp
@@ -29,16 +29,16 @@ namespace audio {
static constexpr size_t kDrainBufferSize = 48 * 1024;
-BluetoothAudioOutput::BluetoothAudioOutput(drivers::Bluetooth* bt)
+BluetoothAudioOutput::BluetoothAudioOutput(drivers::Bluetooth& bt)
: IAudioOutput(kDrainBufferSize, MALLOC_CAP_SPIRAM), bluetooth_(bt) {}
BluetoothAudioOutput::~BluetoothAudioOutput() {}
auto BluetoothAudioOutput::SetInUse(bool in_use) -> void {
if (in_use) {
- bluetooth_->SetSource(stream());
+ bluetooth_.SetSource(stream());
} else {
- bluetooth_->SetSource(nullptr);
+ bluetooth_.SetSource(nullptr);
}
}
diff --git a/src/audio/include/audio_events.hpp b/src/audio/include/audio_events.hpp
index db6a3297..5af419ab 100644
--- a/src/audio/include/audio_events.hpp
+++ b/src/audio/include/audio_events.hpp
@@ -41,6 +41,8 @@ struct ChangeMaxVolume : tinyfsm::Event {
struct TogglePlayPause : tinyfsm::Event {};
+struct OutputModeChanged : tinyfsm::Event {};
+
namespace internal {
struct InputFileOpened : tinyfsm::Event {};
diff --git a/src/audio/include/audio_fsm.hpp b/src/audio/include/audio_fsm.hpp
index 46d3d338..1c0b8aaa 100644
--- a/src/audio/include/audio_fsm.hpp
+++ b/src/audio/include/audio_fsm.hpp
@@ -45,6 +45,7 @@ class AudioState : public tinyfsm::Fsm<AudioState> {
void react(const system_fsm::KeyDownChanged&);
void react(const system_fsm::HasPhonesChanged&);
void react(const ChangeMaxVolume&);
+ void react(const OutputModeChanged&);
virtual void react(const system_fsm::BootComplete&) {}
@@ -65,6 +66,7 @@ class AudioState : public tinyfsm::Fsm<AudioState> {
static std::unique_ptr<Decoder> sDecoder;
static std::shared_ptr<SampleConverter> sSampleConverter;
static std::shared_ptr<I2SAudioOutput> sI2SOutput;
+ static std::shared_ptr<BluetoothAudioOutput> sBtOutput;
static std::shared_ptr<IAudioOutput> sOutput;
static std::optional<database::TrackId> sCurrentTrack;
diff --git a/src/audio/include/bt_audio_output.hpp b/src/audio/include/bt_audio_output.hpp
index a8e44f31..734a7ed1 100644
--- a/src/audio/include/bt_audio_output.hpp
+++ b/src/audio/include/bt_audio_output.hpp
@@ -21,7 +21,7 @@ namespace audio {
class BluetoothAudioOutput : public IAudioOutput {
public:
- BluetoothAudioOutput(drivers::Bluetooth* bt);
+ BluetoothAudioOutput(drivers::Bluetooth& bt);
~BluetoothAudioOutput();
auto SetInUse(bool) -> void override;
@@ -39,7 +39,7 @@ class BluetoothAudioOutput : public IAudioOutput {
BluetoothAudioOutput& operator=(const BluetoothAudioOutput&) = delete;
private:
- drivers::Bluetooth* bluetooth_;
+ drivers::Bluetooth& bluetooth_;
};
} // namespace audio