nRF Connect SDK API 3.3.99
Loading...
Searching...
No Matches
sample_rate_converter.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5 */
6
11#ifndef _SAMPLE_RATE_CONVERTER_H_
12#define _SAMPLE_RATE_CONVERTER_H_
13
22#ifdef CONFIG_SAMPLE_RATE_CONVERTER
23#include <zephyr/sys/ring_buffer.h>
24#include <dsp/filtering_functions.h>
25#endif /* CONFIG_SAMPLE_RATE_CONVERTER */
26
38#define SAMPLE_RATE_CONVERTER_STATE_BUFFER_SIZE \
39 (CONFIG_SAMPLE_RATE_CONVERTER_BLOCK_SIZE_MAX + \
40 CONFIG_SAMPLE_RATE_CONVERTER_MAX_FILTER_SIZE - 1)
41
47
52#define SAMPLE_RATE_CONVERTER_INPUT_BUFFER_NUMBER_OVERFLOW_SAMPLES 2
53
59#define SAMPLE_RATE_CONVERTER_OUTPUT_BUFFER_NUMBER_OVERFLOW_SAMPLES 6
60
61#ifdef CONFIG_SAMPLE_RATE_CONVERTER_BIT_DEPTH_16
62#define SAMPLE_RATE_CONVERTER_INPUT_BUF_SIZE \
63 (SAMPLE_RATE_CONVERTER_INPUT_BUFFER_NUMBER_OVERFLOW_SAMPLES * sizeof(uint16_t))
64#define SAMPLE_RATE_CONVERTER_RINGBUF_SIZE \
65 ((CONFIG_SAMPLE_RATE_CONVERTER_BLOCK_SIZE_MAX + \
66 SAMPLE_RATE_CONVERTER_OUTPUT_BUFFER_NUMBER_OVERFLOW_SAMPLES) * \
67 sizeof(uint16_t))
68#elif CONFIG_SAMPLE_RATE_CONVERTER_BIT_DEPTH_32
69#define SAMPLE_RATE_CONVERTER_INPUT_BUF_SIZE \
70 (SAMPLE_RATE_CONVERTER_INPUT_BUFFER_NUMBER_OVERFLOW_SAMPLES * sizeof(uint32_t))
71#define SAMPLE_RATE_CONVERTER_RINGBUF_SIZE \
72 ((CONFIG_SAMPLE_RATE_CONVERTER_BLOCK_SIZE_MAX + \
73 SAMPLE_RATE_CONVERTER_OUTPUT_BUFFER_NUMBER_OVERFLOW_SAMPLES) * \
74 sizeof(uint32_t))
75#else
76#define SAMPLE_RATE_CONVERTER_INPUT_BUF_SIZE 0
77#define SAMPLE_RATE_CONVERTER_RINGBUF_SIZE 0
78#endif
79
85
88 /* Input and output sample rate to be used for the conversion. */
91
92 /* The ratio for the current conversion. When the conversion is upsampling the ratio is
93 * positive and negative when downsampling.
94 */
96
97 /* Filter type to be used for the conversion. */
99
100 /* Buffer used to store input samples between process calls. */
102
103 /* Ring buffer used to store output samples between process calls. */
104 struct ring_buf output_ringbuf;
106
107 /* Contexts for the CMSIS DSP filter functions. */
108 union {
109#ifdef CONFIG_SAMPLE_RATE_CONVERTER_BIT_DEPTH_16
110 arm_fir_interpolate_instance_q15 fir_interpolate_q15;
111 arm_fir_decimate_instance_q15 fir_decimate_q15;
112#elif CONFIG_SAMPLE_RATE_CONVERTER_BIT_DEPTH_32
113 arm_fir_interpolate_instance_q31 fir_interpolate_q31;
114 arm_fir_decimate_instance_q31 fir_decimate_q31;
115#endif
116 };
117
118 /* State buffers used by the CMSIS DSP filters to keep history of the stream between process
119 * calls.
120 */
121#ifdef CONFIG_SAMPLE_RATE_CONVERTER_BIT_DEPTH_16
122 q15_t state_buf_15[SAMPLE_RATE_CONVERTER_STATE_BUFFER_SIZE];
123#elif CONFIG_SAMPLE_RATE_CONVERTER_BIT_DEPTH_32
124 q31_t state_buf_31[SAMPLE_RATE_CONVERTER_STATE_BUFFER_SIZE];
125#endif
126};
127
142
170 enum sample_rate_converter_filter filter, void const *const input,
171 size_t input_size, uint32_t input_sample_rate, void *const output,
172 size_t output_size, size_t *output_written,
173 uint32_t output_sample_rate);
174
179#endif /* _SAMPLE_RATE_CONVERTER_H_ */
int sample_rate_converter_open(struct sample_rate_converter_ctx *ctx)
Open the sample rate converter for a new context.
#define SAMPLE_RATE_CONVERTER_STATE_BUFFER_SIZE
Definition sample_rate_converter.h:38
sample_rate_converter_filter
Definition sample_rate_converter.h:43
@ SAMPLE_RATE_FILTER_SIMPLE
Definition sample_rate_converter.h:45
@ SAMPLE_RATE_FILTER_TEST
Definition sample_rate_converter.h:44
#define SAMPLE_RATE_CONVERTER_RINGBUF_SIZE
Definition sample_rate_converter.h:77
#define SAMPLE_RATE_CONVERTER_INPUT_BUF_SIZE
Definition sample_rate_converter.h:76
int sample_rate_converter_process(struct sample_rate_converter_ctx *ctx, enum sample_rate_converter_filter filter, void const *const input, size_t input_size, uint32_t input_sample_rate, void *const output, size_t output_size, size_t *output_written, uint32_t output_sample_rate)
Process input samples and produce output samples with new sample rate.
uint8_t buf[0]
Definition sample_rate_converter.h:82
size_t bytes_in_buf
Definition sample_rate_converter.h:83
Definition sample_rate_converter.h:81
uint32_t sample_rate_output
Definition sample_rate_converter.h:90
struct ring_buf output_ringbuf
Definition sample_rate_converter.h:104
uint32_t sample_rate_input
Definition sample_rate_converter.h:89
int conversion_ratio
Definition sample_rate_converter.h:95
enum sample_rate_converter_filter filter_type
Definition sample_rate_converter.h:98
struct buf_ctx input_buf
Definition sample_rate_converter.h:101
uint8_t output_ringbuf_data[0]
Definition sample_rate_converter.h:105
Definition sample_rate_converter.h:87