parent
efd5392f6c
commit
4fc5f931ac
@ -1,5 +1,15 @@ |
||||
idf_component_register( |
||||
SRCS "gay-ipod-fw.cpp" "dac.cpp" "gpio-expander.cpp" "battery.cpp" "storage.cpp" "i2c.cpp" |
||||
INCLUDE_DIRS "." |
||||
REQUIRES "esp_adc_cal" "fatfs" "audio_pipeline" "audio_stream" "result" |
||||
) |
||||
idf_component_register(SRCS |
||||
"gay-ipod-fw.cpp" |
||||
"dac.cpp" |
||||
"gpio-expander.cpp" |
||||
"battery.cpp" |
||||
"storage.cpp" |
||||
"i2c.cpp" |
||||
"playback.cpp" INCLUDE_DIRS "." REQUIRES |
||||
"esp_adc_cal" |
||||
"fatfs" |
||||
"audio_pipeline" |
||||
"audio_stream" |
||||
"result") |
||||
target_compile_options(${COMPONENT_LIB} PRIVATE - |
||||
DRESULT_DISABLE_EXCEPTIONS) |
||||
|
@ -0,0 +1,224 @@ |
||||
#include "playback.h" |
||||
|
||||
#include <cstdint> |
||||
|
||||
#include "audio_element.h" |
||||
#include "audio_event_iface.h" |
||||
#include "audio_pipeline.h" |
||||
#include "dac.h" |
||||
#include "driver/i2s.h" |
||||
#include "esp_err.h" |
||||
#include "mp3_decoder.h" |
||||
|
||||
static const char* kTag = "PLAYBACK"; |
||||
static const i2s_port_t kI2SPort = I2S_NUM_0; |
||||
|
||||
namespace gay_ipod { |
||||
|
||||
// Static functions for interrop with the ESP IDF API, which requires a
|
||||
// function pointer.
|
||||
namespace callback { |
||||
static DacAudioPlayback* instance = nullptr; |
||||
|
||||
// Fits the required function pointer signature, but just delegates to the
|
||||
// wrapper function. Does that make this the wrapper? Who knows.
|
||||
__attribute__((unused)) // (gcc incorrectly thinks this is unused)
|
||||
static esp_err_t |
||||
on_event(audio_event_iface_msg_t* event, void* data) { |
||||
if (instance == nullptr) { |
||||
ESP_LOGW(kTag, "uncaught ADF event, type %x", event->cmd); |
||||
return ESP_OK; |
||||
} |
||||
return instance->HandleEvent(event, data); |
||||
} |
||||
} // namespace callback
|
||||
|
||||
auto DacAudioPlayback::create(AudioDac* dac) |
||||
-> cpp::result<std::unique_ptr<DacAudioPlayback>, Error> { |
||||
assert(callback::instance == nullptr); |
||||
|
||||
// Ensure we're soft-muted before initialising, in order to reduce protential
|
||||
// clicks and pops.
|
||||
dac->WriteVolume(255); |
||||
|
||||
audio_pipeline_handle_t pipeline; |
||||
audio_element_handle_t fatfs_stream_reader; |
||||
audio_element_handle_t i2s_stream_writer; |
||||
audio_event_iface_handle_t event_interface; |
||||
|
||||
audio_pipeline_cfg_t pipeline_config = |
||||
audio_pipeline_cfg_t(DEFAULT_AUDIO_PIPELINE_CONFIG()); |
||||
pipeline = audio_pipeline_init(&pipeline_config); |
||||
if (pipeline == NULL) { |
||||
return cpp::fail(Error::PIPELINE_INIT); |
||||
} |
||||
|
||||
fatfs_stream_cfg_t fatfs_stream_config = |
||||
fatfs_stream_cfg_t(FATFS_STREAM_CFG_DEFAULT()); |
||||
fatfs_stream_config.type = AUDIO_STREAM_READER; |
||||
fatfs_stream_reader = fatfs_stream_init(&fatfs_stream_config); |
||||
if (fatfs_stream_reader == NULL) { |
||||
return cpp::fail(Error::PIPELINE_INIT); |
||||
} |
||||
|
||||
i2s_stream_cfg_t i2s_stream_config = i2s_stream_cfg_t{ |
||||
.type = AUDIO_STREAM_WRITER, |
||||
.i2s_config = |
||||
{ |
||||
// static_cast bc esp-adf uses enums incorrectly
|
||||
.mode = static_cast<i2s_mode_t>(I2S_MODE_MASTER | I2S_MODE_TX), |
||||
.sample_rate = 44100, |
||||
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, |
||||
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, |
||||
.communication_format = I2S_COMM_FORMAT_STAND_I2S, |
||||
.intr_alloc_flags = ESP_INTR_FLAG_LOWMED, |
||||
.dma_buf_count = 8, |
||||
.dma_buf_len = 64, |
||||
.use_apll = false, |
||||
.tx_desc_auto_clear = false, |
||||
.fixed_mclk = 0, |
||||
.mclk_multiple = I2S_MCLK_MULTIPLE_DEFAULT, |
||||
.bits_per_chan = I2S_BITS_PER_CHAN_DEFAULT, |
||||
}, |
||||
.i2s_port = kI2SPort, |
||||
.use_alc = false, |
||||
.volume = 0, // Does nothing; use AudioDac to change this.
|
||||
.out_rb_size = I2S_STREAM_RINGBUFFER_SIZE, |
||||
.task_stack = I2S_STREAM_TASK_STACK, |
||||
.task_core = I2S_STREAM_TASK_CORE, |
||||
.task_prio = I2S_STREAM_TASK_PRIO, |
||||
.stack_in_ext = false, |
||||
.multi_out_num = 0, |
||||
.uninstall_drv = true, |
||||
.need_expand = false, |
||||
.expand_src_bits = I2S_BITS_PER_SAMPLE_16BIT, |
||||
}; |
||||
i2s_stream_writer = i2s_stream_init(&i2s_stream_config); |
||||
if (i2s_stream_writer == NULL) { |
||||
return cpp::fail(Error::PIPELINE_INIT); |
||||
} |
||||
|
||||
// NOTE: i2s_stream_init does some additional setup that hardcodes MCK as
|
||||
// GPIO0. This happens to work fine for us, but be careful if changing.
|
||||
i2s_pin_config_t pin_config = {.mck_io_num = GPIO_NUM_0, |
||||
.bck_io_num = GPIO_NUM_26, |
||||
.ws_io_num = GPIO_NUM_27, |
||||
.data_out_num = GPIO_NUM_5, |
||||
.data_in_num = I2S_PIN_NO_CHANGE}; |
||||
if (esp_err_t err = i2s_set_pin(kI2SPort, &pin_config) != ESP_OK) { |
||||
ESP_LOGE(kTag, "failed to configure i2s pins %x", err); |
||||
return cpp::fail(Error::PIPELINE_INIT); |
||||
} |
||||
|
||||
// TODO: Create encoders dynamically when we need them.
|
||||
audio_element_handle_t mp3_decoder; |
||||
mp3_decoder_cfg_t mp3_config = |
||||
mp3_decoder_cfg_t(DEFAULT_MP3_DECODER_CONFIG()); |
||||
mp3_decoder = mp3_decoder_init(&mp3_config); |
||||
assert(mp3_decoder != NULL); |
||||
|
||||
audio_event_iface_cfg_t event_config = AUDIO_EVENT_IFACE_DEFAULT_CFG(); |
||||
event_config.on_cmd = &callback::on_event; |
||||
event_interface = audio_event_iface_init(&event_config); |
||||
audio_pipeline_set_listener(pipeline, event_interface); |
||||
|
||||
// TODO: most of this is likely post-init, since it involves a decoder.
|
||||
// All the elements of our pipeline have been initialised. Now switch them
|
||||
// together.
|
||||
audio_pipeline_register(pipeline, fatfs_stream_reader, "file"); |
||||
audio_pipeline_register(pipeline, mp3_decoder, "dec"); |
||||
audio_pipeline_register(pipeline, i2s_stream_writer, "i2s"); |
||||
|
||||
const char* link_tag[3] = {"file", "dec", "i2s"}; |
||||
audio_pipeline_link(pipeline, &link_tag[0], 3); |
||||
|
||||
return std::make_unique<DacAudioPlayback>(dac, pipeline, fatfs_stream_reader, |
||||
i2s_stream_writer, event_interface, |
||||
mp3_decoder); |
||||
} |
||||
|
||||
DacAudioPlayback::DacAudioPlayback(AudioDac* dac, |
||||
audio_pipeline_handle_t pipeline, |
||||
audio_element_handle_t fatfs_stream_reader, |
||||
audio_element_handle_t i2s_stream_writer, |
||||
audio_event_iface_handle_t event_interface, |
||||
audio_element_handle_t mp3_decoder) |
||||
: dac_(dac), |
||||
pipeline_(pipeline), |
||||
fatfs_stream_reader_(fatfs_stream_reader), |
||||
i2s_stream_writer_(i2s_stream_writer), |
||||
event_interface_(event_interface), |
||||
mp3_decoder_(mp3_decoder) { |
||||
callback::instance = this; |
||||
} |
||||
|
||||
DacAudioPlayback::~DacAudioPlayback() { |
||||
dac_->WriteVolume(255); |
||||
|
||||
audio_pipeline_remove_listener(pipeline_); |
||||
callback::instance = nullptr; |
||||
|
||||
audio_pipeline_stop(pipeline_); |
||||
audio_pipeline_wait_for_stop(pipeline_); |
||||
audio_pipeline_terminate(pipeline_); |
||||
|
||||
audio_pipeline_unregister(pipeline_, fatfs_stream_reader_); |
||||
audio_pipeline_unregister(pipeline_, mp3_decoder_); |
||||
audio_pipeline_unregister(pipeline_, i2s_stream_writer_); |
||||
|
||||
audio_event_iface_destroy(event_interface_); |
||||
|
||||
audio_pipeline_deinit(pipeline_); |
||||
audio_element_deinit(fatfs_stream_reader_); |
||||
audio_element_deinit(i2s_stream_writer_); |
||||
audio_element_deinit(mp3_decoder_); |
||||
} |
||||
|
||||
void DacAudioPlayback::Play(const std::string& filename) { |
||||
dac_->WriteVolume(255); |
||||
// TODO: handle reconfiguring the pipeline if needed.
|
||||
audio_element_set_uri(fatfs_stream_reader_, filename.c_str()); |
||||
audio_pipeline_run(pipeline_); |
||||
dac_->WriteVolume(volume_); |
||||
} |
||||
|
||||
void DacAudioPlayback::Resume() { |
||||
// TODO.
|
||||
} |
||||
void DacAudioPlayback::Pause() { |
||||
// TODO.
|
||||
} |
||||
|
||||
void DacAudioPlayback::WaitForSongEnd() { |
||||
while (1) { |
||||
audio_element_state_t state = audio_element_get_state(i2s_stream_writer_); |
||||
if (state == AEL_STATE_FINISHED) { |
||||
return; |
||||
} |
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(1000)); |
||||
} |
||||
} |
||||
|
||||
/* for gapless */ |
||||
void DacAudioPlayback::set_next_file(const std::string& filename) { |
||||
next_filename_ = filename; |
||||
} |
||||
|
||||
void DacAudioPlayback::set_volume(uint8_t volume) { |
||||
volume_ = volume; |
||||
// TODO: don't write immediately if we're muting to change track or similar.
|
||||
dac_->WriteVolume(volume); |
||||
} |
||||
|
||||
auto DacAudioPlayback::volume() -> uint8_t { |
||||
return volume_; |
||||
} |
||||
|
||||
auto DacAudioPlayback::HandleEvent(audio_event_iface_msg_t* event, void* data) |
||||
-> esp_err_t { |
||||
ESP_LOGI(kTag, "got event!"); |
||||
return ESP_OK; |
||||
} |
||||
|
||||
} // namespace gay_ipod
|
@ -0,0 +1,68 @@ |
||||
#pragma once |
||||
|
||||
#include <cstdint> |
||||
#include <memory> |
||||
#include <string> |
||||
|
||||
#include "audio_common.h" |
||||
#include "audio_element.h" |
||||
#include "audio_event_iface.h" |
||||
#include "audio_pipeline.h" |
||||
#include "dac.h" |
||||
#include "esp_err.h" |
||||
#include "fatfs_stream.h" |
||||
#include "i2s_stream.h" |
||||
#include "result.hpp" |
||||
#include "storage.h" |
||||
|
||||
namespace gay_ipod { |
||||
|
||||
class DacAudioPlayback { |
||||
public: |
||||
enum Error { PIPELINE_INIT }; |
||||
static auto create(AudioDac* dac) |
||||
-> cpp::result<std::unique_ptr<DacAudioPlayback>, Error>; |
||||
|
||||
DacAudioPlayback(AudioDac* dac, |
||||
audio_pipeline_handle_t pipeline, |
||||
audio_element_handle_t fatfs_stream_reader, |
||||
audio_element_handle_t i2s_stream_writer, |
||||
audio_event_iface_handle_t event_interface, |
||||
audio_element_handle_t mp3_decoder); |
||||
~DacAudioPlayback(); |
||||
|
||||
void Play(const std::string& filename); |
||||
void Resume(); |
||||
void Pause(); |
||||
|
||||
// For debug :)
|
||||
void WaitForSongEnd(); |
||||
|
||||
/* for gapless */ |
||||
void set_next_file(const std::string& filename); |
||||
|
||||
void set_volume(uint8_t volume); |
||||
auto volume() -> uint8_t; |
||||
|
||||
auto HandleEvent(audio_event_iface_msg_t* event, void* data) -> esp_err_t; |
||||
|
||||
// Not copyable or movable.
|
||||
DacAudioPlayback(const DacAudioPlayback&) = delete; |
||||
DacAudioPlayback& operator=(const DacAudioPlayback&) = delete; |
||||
|
||||
private: |
||||
AudioDac* dac_; |
||||
std::mutex playback_lock_; |
||||
|
||||
std::string next_filename_; |
||||
uint8_t volume_; |
||||
|
||||
audio_pipeline_handle_t pipeline_; |
||||
audio_element_handle_t fatfs_stream_reader_; |
||||
audio_element_handle_t i2s_stream_writer_; |
||||
audio_event_iface_handle_t event_interface_; |
||||
|
||||
audio_element_handle_t mp3_decoder_; |
||||
}; |
||||
|
||||
} // namespace gay_ipod
|
Loading…
Reference in new issue