Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion components/box-emu/idf_component.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ dependencies:
espp/drv2605: '>=1.3.4'
# >=1.3.8: the I2S feed task waits for a whole block instead of padding a
# partial one with silence (espp #845) -- needed for stutter-free Genesis audio
espp/esp-box: '>=1.3.8'
# >=1.4.1: enable_audio() / clear_audio() (espp::Audio, espp #889), used by
# BoxEmu::audio_sample_rate()
espp/esp-box: '>=1.4.1'
espp/event_manager: '>=1.3.4'
espp/max1704x: '>=1.3.4'
espp/mcp23x17: '>=1.3.4'
Expand Down
4 changes: 3 additions & 1 deletion components/box-emu/include/box-emu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ class BoxEmu : public espp::BaseComponent {
void mute(bool v) { Bsp::get().mute(v); }
void volume(float volume) { Bsp::get().volume(volume); }
float volume() const { return Bsp::get().volume(); }
void audio_sample_rate(int rate) { Bsp::get().audio_sample_rate(rate); }
/// Switch the audio output to \p rate (no-op at the current rate). The
/// speaker amplifier is held off across the switch, see the definition.
void audio_sample_rate(int rate);
uint32_t audio_sample_rate() const { return Bsp::get().audio_sample_rate(); }
void play_audio(const uint8_t *data, size_t size) { Bsp::get().play_audio(data, size); }
void play_audio(const std::vector<uint8_t> &data) { Bsp::get().play_audio(data); }
Expand Down
19 changes: 19 additions & 0 deletions components/box-emu/src/box-emu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <cstdio>
#include <cstring>
#include <thread>
#include <esp_cpu.h>
#include <soc/spi_struct.h>

Expand Down Expand Up @@ -577,6 +578,24 @@ std::shared_ptr<espp::Drv2605> BoxEmu::haptics() const {
return haptic_motor_;
}

void BoxEmu::audio_sample_rate(int rate) {
auto &bsp = Bsp::get();
if (rate <= 0 || bsp.audio_sample_rate() == static_cast<uint32_t>(rate)) {
return;
}
// Changing the rate stops the I2S clocks and reprograms the ES8311's clock
// dividers under a live DAC, which the speaker played as a pop each time a
// game started or exited (every core sets its own rate, then 48 kHz again).
// Hold the speaker amplifier off across the switch and until the DAC has
// settled on silence at the new rate.
static constexpr auto settle_time = std::chrono::milliseconds(50);
bsp.enable_audio(false);
bsp.clear_audio();
bsp.audio_sample_rate(rate);
std::this_thread::sleep_for(settle_time);
bsp.enable_audio(true);
Comment on lines +592 to +596

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch on the requirement: enable_audio() / clear_audio() came with the espp::Audio move (espp #889, first in v1.4.1), so f63fc69 raises it to espp/esp-box >=1.4.1 instead of switching back to the deprecated enable_sound().

}

void BoxEmu::play_haptic_effect() {
if (haptic_motor_ == nullptr) {
logger_.error("Haptic motor not initialized!");
Expand Down
2 changes: 1 addition & 1 deletion components/genesis/gwenesis
Loading