New Sampling/Audio library for Arduino : AutoAnalogAudio
Easy access to the internal DAC(or PWM), ADC, Timers & DMA for sound generation
I've been playing around with the Arduino Due for a while now, and finally got to looking over the datasheet and playing around with some of the internals because of ongoing requests regarding audio functionality and advanced timer usage etc.
As a result of my dabbling, I've created a sampling library to simplify access to the onboard Analog-to-Digital (ADC), Digital-to-Analog (DAC), Timer and DMA peripherals.
It is a simplified API that allows users to create a wide range of audio or related applications in short order.
Auto Analog Audio (Automatic DAC, ADC & Timer) library
Goals:
Extremely low-latency digital audio recording, playback, communication and relaying using a simple API
Features:
- New: Now supports AVR devices (Uno,Nano,Mega,etc)
- Designed with low-latency radio/wireless communication in mind
- Very simple user interface/API to Arduino DUE DAC, ADC, Timer and DMA
- PCM/WAV Audio/Analog Data playback using Arduino Due DAC
- PCM/WAV Audio/Analog Data recording using Arduino Due ADC
- Onboard timers drive the DAC & ADC automatically
- Automatic sample rate/timer adjustment based on rate of user-driven data requests/input
- Uses DMA (Direct Memory Access) to buffer DAC & ADC data
- ADC & DAC: 8, 10 or 12-bit sampling
- Single channel or stereo output
- Multi-channel ADC sampling
- AVR devices with no DAC or DMA use pseudo DAC(PWM) & DMA(Timer + Memory Buffer)
Testing:
The results so far have been very good. Using the nrf24l01+ radio modules, I was able to stream a standard format *.wav file of high quality (48khz, 16-bit, Stereo) from a Raspberry Pi to the Arduino Due using a slightly modified version of the included Wireless Speaker example.
To put that in perspective:
Data Rate = Sample Rate * Channels * BytesPerSample
Streaming over Radio (RF24):
Data Rate = 48,000 * 2 * 2 = 192.0KB/s
Streaming from SD Card:
Data Rate = 44,100 * 2 * 1 = 88.2KB/s (maximum rate with noticeable slowdown)
Using an SD card is a bit less exciting, as the audio starts to slow down noticeably with audio of 44.1khz, 8-bit, Stereo. Unfortunately, the SD speed is currently a bit limited, since the HSMCI is not available on stock Arduino Due,
and I haven't found a faster working library like sdFat for Due.
SdFat Lib:
The sdFat library works with the due, I just had to initialize it at SPI_DIV6_SPEED (10.25Mhz SPI), and it worked using a value of 5 (17mhz SPI) as well. Initial tests show the max read speeds with the stock SD library at about 113KB/s, and 182KB/s with sdFat lib. Adjusting the SdFatConfig.h file to set ARDUINO_FILE_USES_STREAM = 0 results in speeds of 204KB/s with my current card & module.
These numbers indicate that the Due can handle quite a bit of punishment, and is easily up to the task at hand. The main limitation seems to be the throughput of whatever device is providing the audio or recording it etc.
Initial testing on AVR devices seems to indicate functionality similar to my RF24Audio and TMRpcm libraries.
If using a faster device, or generating audio from tables stored in memory, it seems that the Due with the AAAudio library will most likely handle it.
Notes:
If using the RF24 library in combination with an SD card, I highly recommend using a separate SPI BUS for the radio and SD card. I experienced a number of problems with high speed transfers etc when attempting to extend the length of wires used or connect an SD card module as well.
See the
SPI_UART library section of the RF24 docs to enable a secondary SPI BUS for the radio.
Due Pins - TX1: MOSI, RX1: MISO, SDA1: SCK, CS&CE: User selected
Installation: AutoAnalogAudio is available via the Arduino Library Manager
I've also included a number of examples that demonstrate usage:
Click: File -> Examples -> AutoAnalogAudio in the Arduino IDE
SDAudio Examples: Demonstrate how to play back and record WAV/PCM format audio from SD card using the AAAudio library
Wireless Examples: Demonstrate receiving and sending streams of audio data via nrf24l01+ radio modules and are easily modified to work with other radios or devices. Some matching examples and audio samples are included for Raspberry Pi.
Audio Generation Examples: Demonstrate playback of simple audio tones or synthesized audio.
ADC/Audio Capture Examples: Demonstrate how to capture data/audio streams from the ADC on one or more pins/channels.
Documentation: http://tmrh20.github.io/AutoAnalogAudio
Source Code: https://github.com/TMRh20/AutoAnalogAudio