nRF Connect SDK API 0.1.0
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#include <zephyr/sys/ring_buffer.h>
23#include <dsp/filtering_functions.h>
24
36#define SAMPLE_RATE_CONVERTER_STATE_BUFFER_SIZE \
37 (CONFIG_SAMPLE_RATE_CONVERTER_BLOCK_SIZE_MAX + \
38 CONFIG_SAMPLE_RATE_CONVERTER_MAX_FILTER_SIZE - 1)
39
44};
45
50#define SAMPLE_RATE_CONVERTER_INPUT_BUFFER_NUMBER_OVERFLOW_SAMPLES 2
51
57#define SAMPLE_RATE_CONVERTER_OUTPUT_BUFFER_NUMBER_OVERFLOW_SAMPLES 6
58
59#ifdef CONFIG_SAMPLE_RATE_CONVERTER_BIT_DEPTH_16
60#define SAMPLE_RATE_CONVERTER_INPUT_BUF_SIZE \
61 (SAMPLE_RATE_CONVERTER_INPUT_BUFFER_NUMBER_OVERFLOW_SAMPLES * sizeof(uint16_t))
62#define SAMPLE_RATE_CONVERTER_RINGBUF_SIZE \
63 ((CONFIG_SAMPLE_RATE_CONVERTER_BLOCK_SIZE_MAX + \
64 SAMPLE_RATE_CONVERTER_OUTPUT_BUFFER_NUMBER_OVERFLOW_SAMPLES) * \
65 sizeof(uint16_t))
66#elif CONFIG_SAMPLE_RATE_CONVERTER_BIT_DEPTH_32
67#define SAMPLE_RATE_CONVERTER_INPUT_BUF_SIZE \
68 (SAMPLE_RATE_CONVERTER_INPUT_BUFFER_NUMBER_OVERFLOW_SAMPLES * sizeof(uint32_t))
69#define SAMPLE_RATE_CONVERTER_RINGBUF_SIZE \
70 ((CONFIG_SAMPLE_RATE_CONVERTER_BLOCK_SIZE_MAX + \
71 SAMPLE_RATE_CONVERTER_OUTPUT_BUFFER_NUMBER_OVERFLOW_SAMPLES) * \
72 sizeof(uint32_t))
73#else
74#define SAMPLE_RATE_CONVERTER_INPUT_BUF_SIZE 0
75#define SAMPLE_RATE_CONVERTER_RINGBUF_SIZE 0
76#endif
77
79struct buf_ctx {
82};
83
86 /* Input and output sample rate to be used for the conversion. */
89
90 /* The ratio for the current conversion. When the conversion is upsampling the ratio is
91 * positive and negative when downsampling.
92 */
94
95 /* Filter type to be used for the conversion. */
97
98 /* Buffer used to store input samples between process calls. */
100
101 /* Ring buffer used to store output samples between process calls. */
102 struct ring_buf output_ringbuf;
104
105 /* Contexts for the CMSIS DSP filter functions. */
106 union {
107#ifdef CONFIG_SAMPLE_RATE_CONVERTER_BIT_DEPTH_16
108 arm_fir_interpolate_instance_q15 fir_interpolate_q15;
109 arm_fir_decimate_instance_q15 fir_decimate_q15;
110#elif CONFIG_SAMPLE_RATE_CONVERTER_BIT_DEPTH_32
111 arm_fir_interpolate_instance_q31 fir_interpolate_q31;
112 arm_fir_decimate_instance_q31 fir_decimate_q31;
113#endif
114 };
115
116 /* State buffers used by the CMSIS DSP filters to keep history of the stream between process
117 * calls.
118 */
119#ifdef CONFIG_SAMPLE_RATE_CONVERTER_BIT_DEPTH_16
120 q15_t state_buf_15[SAMPLE_RATE_CONVERTER_STATE_BUFFER_SIZE];
121#elif CONFIG_SAMPLE_RATE_CONVERTER_BIT_DEPTH_32
122 q31_t state_buf_31[SAMPLE_RATE_CONVERTER_STATE_BUFFER_SIZE];
123#endif
124};
125
140
168 enum sample_rate_converter_filter filter, void const *const input,
169 size_t input_size, uint32_t input_sample_rate, void *const output,
170 size_t output_size, size_t *output_written,
171 uint32_t output_sample_rate);
172
177#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:36
sample_rate_converter_filter
Definition: sample_rate_converter.h:41
@ SAMPLE_RATE_FILTER_SIMPLE
Definition: sample_rate_converter.h:43
@ SAMPLE_RATE_FILTER_TEST
Definition: sample_rate_converter.h:42
#define SAMPLE_RATE_CONVERTER_RINGBUF_SIZE
Definition: sample_rate_converter.h:75
#define SAMPLE_RATE_CONVERTER_INPUT_BUF_SIZE
Definition: sample_rate_converter.h:74
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:80
size_t bytes_in_buf
Definition: sample_rate_converter.h:81
Definition: sample_rate_converter.h:79
uint32_t sample_rate_output
Definition: sample_rate_converter.h:88
struct ring_buf output_ringbuf
Definition: sample_rate_converter.h:102
uint32_t sample_rate_input
Definition: sample_rate_converter.h:87
int conversion_ratio
Definition: sample_rate_converter.h:93
enum sample_rate_converter_filter filter_type
Definition: sample_rate_converter.h:96
struct buf_ctx input_buf
Definition: sample_rate_converter.h:99
uint8_t output_ringbuf_data[0]
Definition: sample_rate_converter.h:103
Definition: sample_rate_converter.h:85