Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement pipeline element register #61

Open
wants to merge 6 commits into
base: dev-next
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.16.0)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(esphome_audio)
53 changes: 21 additions & 32 deletions esphome/components/adf_pipeline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@

import os

from ._common import ( # noqa: F401
esp_adf_ns,
ADFPipelineElement,
ADFPipelineProcess,
ADFPipelineSink,
ADFPipelineSource,
ADFPipelineController,
register_element,
get_registered_element,
validate_element,
)

import esphome.codegen as cg
from esphome.components.esp32 import add_idf_component

from esphome.components import esp32
import esphome.config_validation as cv
from esphome.const import CONF_ID
Expand All @@ -19,16 +31,6 @@
CONF_ADF_PIPELINE = "pipeline"
CONF_ADF_KEEP_PIPELINE_ALIVE = "keep_pipeline_alive"

esp_adf_ns = cg.esphome_ns.namespace("esp_adf")
ADFPipelineController = esp_adf_ns.class_("ADFPipelineController")

ADFPipelineElement = esp_adf_ns.class_("ADFPipelineElement")
ADFPipelineSink = esp_adf_ns.class_("ADFPipelineSinkElement", ADFPipelineElement)
ADFPipelineSource = esp_adf_ns.class_("ADFPipelineSourceElement", ADFPipelineElement)
ADFPipelineProcess = esp_adf_ns.class_("ADFPipelineProcessElement", ADFPipelineElement)

BUILT_IN_AUDIO_ELEMENT_IDS = ["resampler"]

# Pipeline Controller

COMPONENT_TYPES = ["sink", "source", "filter"]
Expand All @@ -41,15 +43,13 @@
cv.Optional(CONF_ADF_PIPELINE): cv.ensure_list(
cv.Any(
cv.one_of(*SELF_DESCRIPTORS),
cv.one_of(*BUILT_IN_AUDIO_ELEMENT_IDS),
validate_element,
cv.use_id(ADFPipelineElement),
)
),
}
)

ADFResampler = esp_adf_ns.class_("ADFResampler", ADFPipelineProcess, ADFPipelineElement)


async def setup_pipeline_controller(cntrl, config: dict) -> None:
"""Set controller parameter and register elements to pipeline."""
Expand All @@ -60,34 +60,23 @@ async def setup_pipeline_controller(cntrl, config: dict) -> None:
for comp_id in config[CONF_ADF_PIPELINE]:
if comp_id in SELF_DESCRIPTORS:
cg.add(cntrl.append_own_elements())
elif comp_id in BUILT_IN_AUDIO_ELEMENT_IDS:
elif type_ := get_registered_element(comp_id):
element_id = ID(
cv.validate_id_name(config[CONF_ID].id + "_resampler"),
cv.validate_id_name(config[CONF_ID].id + "_" + comp_id),
is_declaration=True,
type=ADFResampler,
type=type_,
)
comp = cg.new_Pvariable(element_id)
cg.add(cntrl.add_element_to_pipeline(comp))
else:
comp = await cg.get_variable(comp_id)
comp = await get_registered_element(comp_id)
cg.add(cntrl.add_element_to_pipeline(comp))


# Pipeline Elements

ADF_PIPELINE_ELEMENT_SCHEMA = cv.Schema({})

element_classes = {
"resampler": esp_adf_ns.class_(
"ADFResampler", ADFPipelineProcess, ADFPipelineElement
)
}


@coroutine_with_priority(55.0)
async def to_code(config):
cg.add_define("USE_ESP_ADF_VAD")

cg.add_platformio_option("build_unflags", "-Wl,--end-group")

cg.add_platformio_option(
Expand All @@ -108,8 +97,8 @@ async def to_code(config):
"https://github.com/espressif/esp-adf/raw/v2.5/idf_patches/idf_v4.4_freertos.patch",
)

add_idf_component(
name="mdns",
esp32.add_idf_component(
name="esp-adf",
repo="https://github.com/espressif/esp-adf.git",
ref="v2.5",
path="components",
Expand Down
49 changes: 49 additions & 0 deletions esphome/components/adf_pipeline/_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from esphome.util import Registry
import esphome.config_validation as cv
import esphome.codegen as cg
from esphome.core import ID

esp_adf_ns = cg.esphome_ns.namespace("esp_adf")

ADFPipelineController = esp_adf_ns.class_("ADFPipelineController")

ADFPipelineElement = esp_adf_ns.class_("ADFPipelineElement")
ADFPipelineSink = esp_adf_ns.class_("ADFPipelineSinkElement", ADFPipelineElement)
ADFPipelineSource = esp_adf_ns.class_("ADFPipelineSourceElement", ADFPipelineElement)
ADFPipelineProcess = esp_adf_ns.class_("ADFPipelineProcessElement", ADFPipelineElement)


def register_element(name, element_type, schema=None):
if schema is None:
schema = []
return ELEMENT_REGISTRY.register(name, element_type, schema)


async def get_registered_element(conf):
return await cg.build_registry_entry(ELEMENT_REGISTRY, conf)


ELEMENT_REGISTRY = Registry()
validate_element = cv.validate_registry_entry("element", ELEMENT_REGISTRY)
validate_element_list = cv.validate_registry("element", ELEMENT_REGISTRY)


@register_element(
"resampler",
esp_adf_ns.class_("ADFResampler", ADFPipelineProcess),
{
cv.Optional("src_rate", default=16000): cv.int_,
cv.Optional("src_num_channels", default=2): cv.int_range(1, 2),
cv.Optional("dst_rate", default=16000): cv.int_,
cv.Optional("dst_num_channels", default=2): cv.int_range(1, 2),
},
)
def do_resampler(config, element_id):
cntrl = cg.new_Pvariable(element_id)
cg.add(cntrl.set_src_rate(config["src_rate"]))
cg.add(cntrl.set_src_num_channels(config["src_num_channels"]))

cg.add(cntrl.set_dst_rate(config["dst_rate"]))
cg.add(cntrl.set_dst_num_channels(config["dst_num_channels"]))

return cntrl
5 changes: 5 additions & 0 deletions esphome/components/adf_pipeline/adf_audio_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ class ADFPipelineProcessElement : public ADFPipelineElement {
class ADFResampler : public ADFPipelineProcessElement {
public:
const std::string get_name() override { return "Resampler"; }
void set_src_rate(int value) { this->src_rate_ = value; };
void set_dst_rate(int value) { this->dst_rate_ = value; };
void set_src_num_channels(int value) { this->src_num_channels_ = value; };
void set_dst_num_channels(int value) { this->dst_num_channels_ = value; };

protected:
bool init_adf_elements_() override;

void on_settings_request(AudioPipelineSettingsRequest &request) override;

int src_rate_{16000};
Expand Down