|
| 1 | +/* |
| 2 | + * Copyright(c) 2024 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | +#include "audio_module_template.h" |
| 7 | + |
| 8 | +#include <stdlib.h> |
| 9 | +#include <stdio.h> |
| 10 | +#include <ctype.h> |
| 11 | +#include <zephyr/kernel.h> |
| 12 | +#include <errno.h> |
| 13 | +#include "audio_defines.h" |
| 14 | +#include "audio_module.h" |
| 15 | + |
| 16 | +#include <zephyr/logging/log.h> |
| 17 | +LOG_MODULE_REGISTER(audio_module_template, CONFIG_AUDIO_MODULE_TEMPLATE_LOG_LEVEL); |
| 18 | + |
| 19 | +static int audio_module_template_open(struct audio_module_handle_private *handle, |
| 20 | + struct audio_module_configuration const *const configuration) |
| 21 | + |
| 22 | +{ |
| 23 | + struct audio_module_handle *hdl = (struct audio_module_handle *)handle; |
| 24 | + struct audio_module_template_context *ctx = |
| 25 | + (struct audio_module_template_context *)hdl->context; |
| 26 | + struct audio_module_template_configuration *config = |
| 27 | + (struct audio_module_template_configuration *)configuration; |
| 28 | + |
| 29 | + /* Perform any other functions required to open the module. */ |
| 30 | + |
| 31 | + /* For example: Clear the module's context. |
| 32 | + * Save the initial configuration to the module context. |
| 33 | + */ |
| 34 | + memset(ctx, 0, sizeof(struct audio_module_template_context)); |
| 35 | + memcpy(&ctx->config, config, sizeof(struct audio_module_template_configuration)); |
| 36 | + |
| 37 | + LOG_DBG("Open %s module", hdl->name); |
| 38 | + |
| 39 | + return 0; |
| 40 | +} |
| 41 | + |
| 42 | +static int audio_module_template_close(struct audio_module_handle_private *handle) |
| 43 | +{ |
| 44 | + struct audio_module_handle *hdl = (struct audio_module_handle *)handle; |
| 45 | + struct audio_module_template_context *ctx = |
| 46 | + (struct audio_module_template_context *)hdl->context; |
| 47 | + |
| 48 | + /* Perform any other functions required to close the module. */ |
| 49 | + |
| 50 | + /* For example: Clear the context data */ |
| 51 | + memset(ctx, 0, sizeof(struct audio_module_template_context)); |
| 52 | + |
| 53 | + LOG_DBG("Close %s module", hdl->name); |
| 54 | + |
| 55 | + return 0; |
| 56 | +} |
| 57 | + |
| 58 | +static int audio_module_template_configuration_set( |
| 59 | + struct audio_module_handle_private *handle, |
| 60 | + struct audio_module_configuration const *const configuration) |
| 61 | +{ |
| 62 | + struct audio_module_template_configuration *config = |
| 63 | + (struct audio_module_template_configuration *)configuration; |
| 64 | + struct audio_module_handle *hdl = (struct audio_module_handle *)handle; |
| 65 | + struct audio_module_template_context *ctx = |
| 66 | + (struct audio_module_template_context *)hdl->context; |
| 67 | + |
| 68 | + /* Perform any other functions to configure the module. */ |
| 69 | + |
| 70 | + /* For example: Copy the configuration into the context. */ |
| 71 | + memcpy(&ctx->config, config, sizeof(struct audio_module_template_configuration)); |
| 72 | + |
| 73 | + LOG_DBG("Set the configuration for %s module: sample rate = %d bit depth = %d string = " |
| 74 | + "%s", |
| 75 | + hdl->name, ctx->config.sample_rate_hz, ctx->config.bit_depth, |
| 76 | + ctx->config.module_description); |
| 77 | + |
| 78 | + return 0; |
| 79 | +} |
| 80 | + |
| 81 | +static int |
| 82 | +audio_module_template_configuration_get(struct audio_module_handle_private const *const handle, |
| 83 | + struct audio_module_configuration *configuration) |
| 84 | +{ |
| 85 | + struct audio_module_template_configuration *config = |
| 86 | + (struct audio_module_template_configuration *)configuration; |
| 87 | + struct audio_module_handle *hdl = (struct audio_module_handle *)handle; |
| 88 | + struct audio_module_template_context *ctx = |
| 89 | + (struct audio_module_template_context *)hdl->context; |
| 90 | + |
| 91 | + /* Perform any other functions to extract the configuration of the module. */ |
| 92 | + |
| 93 | + /* For example: Copy the configuration from the context into the output configuration. */ |
| 94 | + memcpy(config, &ctx->config, sizeof(struct audio_module_template_configuration)); |
| 95 | + |
| 96 | + LOG_DBG("Get the configuration for %s module: sample rate = %d bit depth = %d string = " |
| 97 | + "%s", |
| 98 | + hdl->name, config->sample_rate_hz, config->bit_depth, config->module_description); |
| 99 | + |
| 100 | + return 0; |
| 101 | +} |
| 102 | + |
| 103 | +static int audio_module_template_start(struct audio_module_handle_private *handle) |
| 104 | +{ |
| 105 | + struct audio_module_handle *hdl = (struct audio_module_handle *)handle; |
| 106 | + |
| 107 | + /* Perform any other functions to start the module. */ |
| 108 | + |
| 109 | + LOG_DBG("Start the %s module", hdl->name); |
| 110 | + |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
| 114 | +static int audio_module_template_stop(struct audio_module_handle_private *handle) |
| 115 | +{ |
| 116 | + struct audio_module_handle *hdl = (struct audio_module_handle *)handle; |
| 117 | + |
| 118 | + /* Perform any other functions to stop the module. */ |
| 119 | + |
| 120 | + LOG_DBG("Stop the %s module", hdl->name); |
| 121 | + |
| 122 | + return 0; |
| 123 | +} |
| 124 | + |
| 125 | +static int audio_module_template_data_process(struct audio_module_handle_private *handle, |
| 126 | + struct audio_data const *const audio_data_in, |
| 127 | + struct audio_data *audio_data_out) |
| 128 | +{ |
| 129 | + struct audio_module_handle *hdl = (struct audio_module_handle *)handle; |
| 130 | + |
| 131 | + /* Perform any other functions to process the data within the module. */ |
| 132 | + |
| 133 | + /* For example: Copy the input to the output. */ |
| 134 | + { |
| 135 | + size_t size = audio_data_in->data_size < audio_data_out->data_size |
| 136 | + ? audio_data_in->data_size |
| 137 | + : audio_data_out->data_size; |
| 138 | + |
| 139 | + memcpy(&audio_data_out->meta, &audio_data_in->meta, sizeof(struct audio_metadata)); |
| 140 | + memcpy(audio_data_out->data, audio_data_in->data, size); |
| 141 | + audio_data_out->data_size = size; |
| 142 | + } |
| 143 | + |
| 144 | + LOG_DBG("Process the input audio data into the output audio data item for %s module", |
| 145 | + hdl->name); |
| 146 | + |
| 147 | + return 0; |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * @brief Table of the dummy module functions. |
| 152 | + */ |
| 153 | +const struct audio_module_functions audio_module_template_functions = { |
| 154 | + /** |
| 155 | + * @brief Function to an open the dummy module. |
| 156 | + */ |
| 157 | + .open = audio_module_template_open, |
| 158 | + |
| 159 | + /** |
| 160 | + * @brief Function to close the dummy module. |
| 161 | + */ |
| 162 | + .close = audio_module_template_close, |
| 163 | + |
| 164 | + /** |
| 165 | + * @brief Function to set the configuration of the dummy module. |
| 166 | + */ |
| 167 | + .configuration_set = audio_module_template_configuration_set, |
| 168 | + |
| 169 | + /** |
| 170 | + * @brief Function to get the configuration of the dummy module. |
| 171 | + */ |
| 172 | + .configuration_get = audio_module_template_configuration_get, |
| 173 | + |
| 174 | + /** |
| 175 | + * @brief Start a module processing data. |
| 176 | + */ |
| 177 | + .start = audio_module_template_start, |
| 178 | + |
| 179 | + /** |
| 180 | + * @brief Pause a module processing data. |
| 181 | + */ |
| 182 | + .stop = audio_module_template_stop, |
| 183 | + |
| 184 | + /** |
| 185 | + * @brief The core data processing function in the dummy module. |
| 186 | + */ |
| 187 | + .data_process = audio_module_template_data_process, |
| 188 | +}; |
| 189 | + |
| 190 | +/** |
| 191 | + * @brief The set-up description for the LC3 decoder. |
| 192 | + */ |
| 193 | +struct audio_module_description audio_module_template_dept = { |
| 194 | + .name = "Audio Module Temp", |
| 195 | + .type = AUDIO_MODULE_TYPE_IN_OUT, |
| 196 | + .functions = &audio_module_template_functions}; |
| 197 | + |
| 198 | +/** |
| 199 | + * @brief A private pointer to the template set-up parameters. |
| 200 | + */ |
| 201 | +struct audio_module_description *audio_module_template_description = &audio_module_template_dept; |
0 commit comments