mirror of
https://github.com/pschatzmann/arduino-audio-tools.git
synced 2024-09-21 02:17:31 +00:00
Page:
Converting the Data Format
Pages
Audio Boards
Audio Effects
Audio Input and Output
Audio Metadata
Audio Sources and Sinks
Communication
Converting the Data Format
Converting the Number of Input and Output Channels
DSP Libraries
Design Principles
Determining the Volume
Encoding and Decoding of Audio
Equilizer
Examples
External ADC
External DAC
External TDM DACs
FFT
Faust
Filters
Home
Interactive Audio
Introduction
It's not working
Jupyterlab
Low Latency Streaming of Audio
MemoryStream: Converting a File to Flash Memory
Multicore Processing
Optional Libraries
Performance
Pipelines
Pitch Shifting
Project Status
Propagation of Audio Format Changes
Pure Data
Resampling
Running an Audio Sketch on the Desktop
Splitting and Merging Audio
Supported Architectures
Supported Scenarios
TensorFlow Lite MicroSpeech
Tensorflow Lite Audio Output
The Audio Player Class
Using the Framework w o Arduino
Using the Project as library with cmake
Vector, Allocators & PSRAM
Volume Control
Working with PlatformIO
Writing your Custom Input&Output Class
21
Converting the Data Format
Phil Schatzmann edited this page 2024-05-04 09:43:15 +02:00
Table of Contents
I am currently providing quite a few audio processing classes which can be used to change the audio format.
You can use the more generic FormatConverterStream to change of the
- bits_per_sample
- number of channels
- sample_rate
This class is supporting both: the conversion on the input and on the output side.
Converting on the Output Side
This is the recommended way and the propagation of changes of the AudioInfo should work automatically in all the cases.
#include "AudioTools.h"
SineWaveGenerator<int32_t> sine_wave; // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<int32_t> in_stream(sine_wave); // Stream generated from sine wave
CsvStream<int16_t> out(Serial); // Output to Serial
AudioInfo from(44100, 2, 32);
AudioInfo to(44100, 2, 16);
FormatConverterStream conv(out);
StreamCopy copier(conv, in_stream); // copies sound to out
void setup(){
Serial.begin(115200);
AudioLogger::instance().begin(Serial, AudioLogger::Info);
sine_wave.begin(from, N_B4);
in_stream.begin(from);
conv.begin(from, to);
out.begin(to);
}
void loop(){
copier.copy();
}
Converting on the Input Side
Conversion on the input side is less efficient and needs more memory, but sometimes it is just more convenient to use.
#include "AudioTools.h"
SineWaveGenerator<int32_t> sine_wave; // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<int32_t> in_stream(sine_wave); // Stream generated from sine wave
CsvStream<int16_t> out(Serial); // Output to Serial
AudioInfo from(44100, 2, 32);
AudioInfo to(44100, 2, 16);
FormatConverterStream conv(in_stream); // Define input
StreamCopy copier(out, conv); // copies converted sound to out
void setup(){
Serial.begin(115200);
AudioLogger::instance().begin(Serial, AudioLogger::Info);
sine_wave.begin(from, N_B4);
in_stream.begin(from);
conv.begin(from, to);
out.begin(to);
}
void loop(){
copier.copy();
}
Examples:
Final Comments
This class is very flexible and convenient to use. However if you need to save some milliseconds in your processing, it is a bit more efficient to use the dedicated class if you need to change only a single parameter: e.g. the sample_rate: