arduino-audio-tools/README.md

148 lines
7.4 KiB
Markdown
Raw Normal View History

2021-04-28 06:06:15 +00:00
# Arduino Audio Tools
2021-05-05 17:17:13 +00:00
Some basic __header-only C++ classes__ that can be used for __Audio Processing__ provided as __Arduino Library__:
2021-04-28 06:06:15 +00:00
2021-05-05 17:17:13 +00:00
- a simple I2S class (to read and write to the internal I2S)
- a simple ADC class (to read analog data with the help of I2S)
- Additional Stream implementations: MemoryStream, UrlStream, I2SStream, A2DPStream, PrintStream
2021-04-28 06:06:15 +00:00
- Converters
2021-05-05 17:17:13 +00:00
- Musical Notes (with frequencies of notes)
2021-04-28 06:06:15 +00:00
- SineWaveGenerator (to generate some sine tone)
2021-04-29 20:50:28 +00:00
- NBuffer (Multi buffer for writing and reading of (audio) data)
- TimerAlarmRepeating (e.g. for sampling audio data using exact times) [ESP32 only]
2021-04-29 09:52:46 +00:00
- A Wav Encoder and Decoder
2021-05-05 17:17:13 +00:00
- AudioOutputWithCallback class to provide callback integration e.g. with ESP8266Audio
2021-04-28 06:06:15 +00:00
2021-04-29 20:50:28 +00:00
This functionality provides the glue which makes different audio processing components and libraries work together.
2021-05-05 18:03:26 +00:00
We also provide plenty of examples that demonstrate how to implement the different scenarios. The __design philosophy__ is based on the Arduino conventions: we use the ```begin()``` and ```end()``` methods to start and stop the processing and we propagate the __use of Streams__. We all know the Arduino Streams: We use them to write out print messages and sometimes we use them to read the output from Serial devices. The same thing applies to my “Audio Streams”: You can read audio data from “Audio Sources” and you write them to “Audio Sinks”.
2021-05-01 11:54:12 +00:00
2021-05-05 17:17:13 +00:00
As “Audio Sources” we will have e.g.:
2021-05-01 11:54:12 +00:00
2021-05-05 18:03:26 +00:00
- Analog Microphones [AnalogAudio](https://pschatzmann.github.io/arduino-audio-tools/html/classaudio__tools_1_1_analog_audio.html)
- Digital Microphonse [I2SStream](https://pschatzmann.github.io/arduino-audio-tools/html/classaudio__tools_1_1_i2_s_stream.html)
- Files on the Internet [UrlStream](https://pschatzmann.github.io/arduino-audio-tools/html/classaudio__tools_1_1_url_stream.html)
- Generated Sound [GeneratedSoundStream](https://pschatzmann.github.io/arduino-audio-tools/html/classaudio__tools_1_1_generated_sound_stream.html)
- Mobile Phone A2DP Bluetooth [A2DPStream](https://pschatzmann.github.io/arduino-audio-tools/html/classaudio__tools_1_1_a2_d_p_stream.html)
- Binary Data in Flash Memory [MemoryStream](https://pschatzmann.github.io/arduino-audio-tools/html/classaudio__tools_1_1_memory_stream.html)
2021-05-05 17:17:13 +00:00
- SD Files
2021-04-28 06:06:15 +00:00
2021-05-05 17:17:13 +00:00
As “Audio Sinks” we will have e.g:
2021-04-28 06:06:15 +00:00
2021-05-05 18:03:26 +00:00
- external DAC [I2SStream](https://pschatzmann.github.io/arduino-audio-tools/html/classaudio__tools_1_1_i2_s_stream.html)
- an Amplifier [AnalogAudio](https://pschatzmann.github.io/arduino-audio-tools/html/classaudio__tools_1_1_analog_audio.html)
- Bluetooth Speakers [A2DPStream](https://pschatzmann.github.io/arduino-audio-tools/html/classaudio__tools_1_1_a2_d_p_stream.html)
- Serial to display the data as CSV [CsvStream](https://pschatzmann.github.io/arduino-audio-tools/html/classaudio__tools_1_1_csv_stream.html)
2021-05-05 17:17:13 +00:00
- SD Files
Here is an simple example which streams a file from the Flash Memory and writes it to I2S:
```
#include "AudioTools.h"
#include "StarWars30.h"
using namespace audio_tools;
uint8_t channels = 2;
uint16_t sample_rate = 22050;
MemoryStream music(StarWars30_raw, StarWars30_raw_len);
I2SStream i2s; // Output to I2S
StreamCopyT<int16_t> copier(i2s, music); // copies sound into i2s
void setup(){
Serial.begin(115200);
I2SConfig config = i2s.defaultConfig(TX_MODE);
config.sample_rate = sample_rate;
config.channels = channels;
config.bits_per_sample = 16;
i2s.begin(config);
}
void loop(){
if (!copier.copy2()){
i2s.end();
stop();
}
}
```
A complete list of the supported Audio Stream classes and scenarios can be found in the [Scenarios Document](Scenarios.md)
## Examples
2021-04-30 02:48:08 +00:00
2021-05-05 17:17:13 +00:00
The examples follow the following naming convention: "scenario type"-"source"-"destination". For the scenario types we might have __base__ (using basic api functionality), __stream__ for examples using Streams and __test__ for the test cases.
2021-04-28 06:06:15 +00:00
2021-05-05 17:17:13 +00:00
For the __source__ we currently have __adc__ for analog input devices like analog microphones, __i2s__ for digital input devices (e.g. digital microphones), __file__ for SD files and __a2dp__ for input from Bluetooth A2DP (e.g. from a Mobile Phone).
2021-04-28 09:01:34 +00:00
2021-05-05 17:17:13 +00:00
For the __destination__ we use __dac__ for analog output (e.g. to an amplifier), __i2s__ for digital output devices (e.g. an external DAC), __file__ for SD files and __a2dp__ for output to Bluetooth A2DP (e.g. a Bluetooth Speaker).
Here is the list of examples:
#### Stream API
- [streams-adc-serial](/examples/streams-adc-serial) Displaying input from analog microphone on the Serial Plotter
2021-05-05 17:24:49 +00:00
- [streams-generator-serial](/examples/streams-generator-serial) Displaying generated sound on the Serial Plotter
- [streams-generator-i2s_external_dac](/examples/streams-generator-i2s_external_dac) Playing generated sound on external DAC via I2S
2021-05-05 17:17:13 +00:00
- [streams-memory_raw-i2s_external_dac](examples/streams-memory_raw-i2s_external_dac) - Play music form Flash Memory via I2S to External DAC
2021-05-05 17:24:49 +00:00
- [streams-url_raw-serial](/examples/streams-url_raw-serial) Displaying a music file from the internet on the Serial Plotter
2021-05-05 17:51:13 +00:00
- [streams-url_raw-I2S_external_dac.ino](/examples/streams-url_raw-I2S_external_dac.ino) Streaming a File from the Internet to on external DAC via I2S
2021-05-05 17:17:13 +00:00
... these are just a few examples, but you can combine any Input Stream with any Output Stream as you like...
#### Basic API
- [base-adc-serial](examples/base-adc-serial) - Sample analog sound and write it to Serial
- [base-adc-a2dp](examples/base-adc-a2dp) - Sample analog sound and write it to a A2DP Bluetooth source
- [base-file_raw-serial](examples/base-file_raw-serial) - Read Raw File from SD card to and write it to Serial
- [base-file_raw-a2dp](examples/base-file_raw-a2dp) - Read Raw File from SD card write it A2DP Bluetooth
- [base-file_mp3-a2dp](examples/base-file_mp3-a2dp) - Stream MP3 File from SD card to A2DP Bluetooth using the ESP8266Audio library
- [base-i2s-serial](examples/base-i2s-serial) - Sample digital sound and write it to Serial
- [base-i2s-a2dp](examples/base-i2s-a2dp) - Sample analog sound and write it to a A2DP Bluetooth source
## Optional Libraries
Dependent on the example you might need to install some of the following libraries:
- [ESP32-A2DP Library](https://github.com/pschatzmann/ESP32-A2DP) to support A2DP Bluetooth Audio
- [ESP8266Audio]( ) to play different audio Formats
- [SD Library](https://www.arduino.cc/en/reference/SD) to read and write files.
- [arduino-fdk-aac](https://github.com/pschatzmann/arduino-fdk-aac) to encode or decode AAC
2021-04-30 02:48:08 +00:00
2021-04-28 09:01:34 +00:00
2021-04-28 16:15:42 +00:00
## Installation
You can download the library as zip and call include Library -> zip library. Or you can git clone this project into the Arduino libraries folder e.g. with
```
cd ~/Documents/Arduino/libraries
2021-04-29 20:50:28 +00:00
git clone pschatzmann/arduino-audio-tools.git
2021-04-28 16:15:42 +00:00
```
2021-04-28 09:01:34 +00:00
## Documentation
2021-04-28 06:11:01 +00:00
2021-05-05 17:17:13 +00:00
Here is the generated [Class documentation](https://pschatzmann.github.io/arduino-audio-tools/html/annotated.html).
You also might find further information in [one of my blogs](https://www.pschatzmann.ch/home/category/machine-sound/)
2021-04-28 06:11:01 +00:00
2021-04-28 06:06:15 +00:00
## Project Status
2021-04-28 06:11:01 +00:00
2021-04-29 09:52:46 +00:00
This is currently work in progress:
2021-04-29 09:55:35 +00:00
| Functionality | Status |
|------------------------|---------|
| Analog input - ADC | tested |
| I2S | tested |
2021-04-29 19:18:30 +00:00
| Files (RAW, MP3...) | tested |
2021-05-05 17:17:13 +00:00
| Streams | tested |
2021-04-29 09:55:35 +00:00
| WAV encoding/deconding | open |
| AAC encoding/deconding | open |
2021-05-01 08:42:05 +00:00
| int24_t | tested |
2021-04-28 06:06:15 +00:00