|
| 1 | +/* |
| 2 | + * Copyright 2025 NXP |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/kernel.h> |
| 8 | +#include <zephyr/logging/log.h> |
| 9 | + |
| 10 | +#include <mp.h> |
| 11 | + |
| 12 | +LOG_MODULE_REGISTER(main); |
| 13 | + |
| 14 | +#define LOG_LEVEL LOG_LEVEL_DBG |
| 15 | + |
| 16 | +/* |
| 17 | + * WORKAROUND: Direct memory slab management in application code |
| 18 | + * |
| 19 | + * TODO: Normally, applications should not set this because they do not |
| 20 | + * need to know about the memory slab audio buffers implementation. |
| 21 | + * |
| 22 | + * The __nocache attribute ensures this memory is not cached, which is |
| 23 | + * required for DMA operations used by audio hardware. |
| 24 | + */ |
| 25 | +__nocache struct k_mem_slab mem_slab; |
| 26 | + |
| 27 | +int main() |
| 28 | +{ |
| 29 | + int gain = 90; /* Set gain to 90% (0.9x amplification) */ |
| 30 | + int ret = 0; |
| 31 | + |
| 32 | + /* Create elements */ |
| 33 | + MpElement *source = mp_element_factory_create("zaud_dmic_src", "dmic"); |
| 34 | + if (source == NULL) { |
| 35 | + LOG_ERR("Failed to create dmic element"); |
| 36 | + return 0; |
| 37 | + } |
| 38 | + |
| 39 | + MpElement *transform = mp_element_factory_create("zaud_gain", "gain"); |
| 40 | + if (transform == NULL) { |
| 41 | + LOG_ERR("Failed to create gain element"); |
| 42 | + return 0; |
| 43 | + } |
| 44 | + |
| 45 | + MpElement *sink = mp_element_factory_create("zaud_i2s_codec_sink", "speaker"); |
| 46 | + if (sink == NULL) { |
| 47 | + LOG_ERR("Failed to create speaker element"); |
| 48 | + return 0; |
| 49 | + } |
| 50 | + |
| 51 | + ret = mp_object_set_properties(MP_OBJECT(source), PROP_ZAUD_SRC_SLAB_PTR, &mem_slab, |
| 52 | + PROP_LIST_END); |
| 53 | + if (ret < 0) { |
| 54 | + LOG_ERR("Failed to set properties for dmic element"); |
| 55 | + return 0; |
| 56 | + } |
| 57 | + |
| 58 | + ret = mp_object_set_properties(MP_OBJECT(transform), PROP_GAIN, &gain, PROP_LIST_END); |
| 59 | + if (ret < 0) { |
| 60 | + LOG_ERR("Failed to set properties for transform element"); |
| 61 | + return 0; |
| 62 | + } |
| 63 | + |
| 64 | + ret = mp_object_set_properties(MP_OBJECT(sink), PROP_ZAUD_SINK_SLAB_PTR, &mem_slab, |
| 65 | + PROP_LIST_END); |
| 66 | + if (ret < 0) { |
| 67 | + LOG_ERR("Failed to set properties for speaker element"); |
| 68 | + return 0; |
| 69 | + } |
| 70 | + |
| 71 | + /* Create a new pipeline */ |
| 72 | + MpElement *pipeline = mp_pipeline_new("dmic_gain_speaker_pipeline"); |
| 73 | + if (pipeline == NULL) { |
| 74 | + LOG_ERR("Failed to create pipeline"); |
| 75 | + return 0; |
| 76 | + } |
| 77 | + |
| 78 | + /* Add elements to the pipeline - order does not matter */ |
| 79 | + if (mp_bin_add(MP_BIN(pipeline), source, transform, sink, NULL) == false) { |
| 80 | + LOG_ERR("Failed to add elements"); |
| 81 | + return 0; |
| 82 | + } |
| 83 | + |
| 84 | + /* Link elements together - order does matter */ |
| 85 | + if (mp_element_link(source, transform, sink, NULL) == false) { |
| 86 | + LOG_ERR("Failed to link elements"); |
| 87 | + return 0; |
| 88 | + } |
| 89 | + |
| 90 | + /* Start playing */ |
| 91 | + if (mp_element_set_state(pipeline, MP_STATE_PLAYING) != MP_STATE_CHANGE_SUCCESS) { |
| 92 | + LOG_ERR("Failed to start pipeline"); |
| 93 | + return 0; |
| 94 | + } |
| 95 | + |
| 96 | + /* Handle message from the pipeline */ |
| 97 | + MpBus *bus = mp_element_get_bus(pipeline); |
| 98 | + /* Wait until an Error or an EOS - blocking */ |
| 99 | + MpMessage *msg = mp_bus_pop_msg(bus, MP_MESSAGE_ERROR | MP_MESSAGE_EOS); |
| 100 | + if (msg != NULL) { |
| 101 | + switch (MP_MESSAGE_TYPE(msg)) { |
| 102 | + case MP_MESSAGE_ERROR: |
| 103 | + LOG_INF("Received ERROR from %s\n", msg->src->name); |
| 104 | + break; |
| 105 | + case MP_MESSAGE_EOS: |
| 106 | + LOG_INF("Received EOS from %s\n", msg->src->name); |
| 107 | + break; |
| 108 | + default: |
| 109 | + LOG_ERR("Unexpected message received from %s\n", msg->src->name); |
| 110 | + break; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /* TODO: Stop the pipeline */ |
| 115 | + |
| 116 | + return 0; |
| 117 | +} |
0 commit comments