Skip to content

Commit

Permalink
Concurrency for RP2040
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Jan 6, 2025
1 parent 65b5b20 commit 6a98cc1
Show file tree
Hide file tree
Showing 32 changed files with 774 additions and 345 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const char* ext="mp3";
AudioSourceSDFAT source(startFilePath, ext, PIN_AUDIO_KIT_SD_CARD_CS);
MP3DecoderHelix decoder;
//Setup of synchronized buffer
SynchronizedNBuffer<uint8_t> buffer(buffer_size,buffer_count, portMAX_DELAY, 10);
SynchronizedNBuffer buffer(buffer_size,buffer_count, portMAX_DELAY, 10);
QueueStream<uint8_t> out(buffer); // convert Buffer to Stream
AudioPlayer player(source, out, decoder);
BluetoothA2DPSource a2dp;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @file streams-url_mp3-out.ino
* @author Phil Schatzmann
* @brief decode MP3 stream from url and output it with the help of PWM
* @version 0.1
* @date 2021-96-25
*
* @copyright Copyright (c) 2021
*/

// install https://github.com/pschatzmann/arduino-libhelix.git

#include "AudioTools.h"
#include "AudioTools/AudioCodecs/CodecMP3Helix.h"


URLStream url("ssid","password");
PWMAudioOutput out; // final output of decoded stream
EncodedAudioStream dec(&out, new MP3DecoderHelix()); // Decoding stream
StreamCopy copier(dec, url); // copy url to decoder


void setup(){
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);

// setup out
auto config = out.defaultConfig(TX_MODE);
//config.resolution = 8; // must be between 8 and 11 -> drives pwm frequency (8 is default)
// alternative 1
//config.start_pin = 3;
// alternative 2
int pins[] = {22, 23};
// alternative 3
//Pins pins = {3};
//config.setPins(pins);
out.begin(config);

// setup I2S based on sampling rate provided by decoder
dec.begin();

// mp3 radio
url.begin("http://stream.srg-ssr.ch/m/rsj/mp3_128","audio/mp3");

}

void loop(){
copier.copy();
}
2 changes: 1 addition & 1 deletion examples/tests/basic/test-queue/test-queue.ino
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "AudioTools.h"
#include "AudioTools/Concurrency/QueueLockFree.h"
#include "AudioTools/Concurrency/QueueRTOS.h"
#include "AudioTools/Concurrency/RTOS/QueueRTOS.h"

// test different queue implementations:

Expand Down
2 changes: 1 addition & 1 deletion examples/tests/concurrency/BufferRTOS/BufferRTOS.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
*/
#include "AudioTools.h"
#include "AudioTools/AudioLibs/Concurrency.h"
#include "AudioTools/Concurrency/RTOS.h"

BufferRTOS<int16_t> buffer(512 * 10);

Expand Down
1 change: 0 additions & 1 deletion examples/tests/concurrency/NBuffer/NBuffer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*
*/
#include "AudioTools.h"
#include "AudioTools/AudioLibs/Concurrency.h"

NBuffer<int16_t> buffer(512, 4);

Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions examples/tests/concurrency/audio-test/audio-test.ino
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

#include "AudioTools.h"
#include "AudioTools/AudioLibs/AudioBoardStream.h"
#include "AudioTools/AudioLibs/Concurrency.h"
#include "AudioTools/Concurrency/RTOS.h"

AudioInfo info(44100, 2, 16);
// source and sink
SineWaveGenerator<int16_t> sineWave(32000);
GeneratedSoundStream<int16_t> sound(sineWave);
AudioBoardStream out(AudioKitEs8388V1);
// queue
// SynchronizedNBuffer<uint8_t> buffer(1024, 10);
// SynchronizedNBuffer buffer(1024, 10);
BufferRTOS<uint8_t> buffer(1024 * 10);
QueueStream<uint8_t> queue(buffer);
// copy
Expand Down
4 changes: 2 additions & 2 deletions examples/tests/concurrency/synchNBuffer/synchNBuffer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
*
*/
#include "AudioTools.h"
#include "AudioTools/AudioLibs/Concurrency.h"
#include "AudioTools/Concurrency/RTOS.h"

audio_tools::Mutex mutex;
MutexRTOS mutex;
NBuffer<int16_t> nbuffer(512, 10);
SynchronizedBuffer<int16_t> buffer(nbuffer, mutex);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
*
*/
#include "AudioTools.h"
#include "AudioTools/AudioLibs/Concurrency.h"
#include "AudioTools/Concurrency/RTOS.h"

audio_tools::Mutex mutex;
MutexRTOS mutex;
RingBuffer<int16_t> nbuffer(512 * 4);
SynchronizedBuffer<int16_t> buffer(nbuffer, mutex);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const char *startFilePath = "/";
const char *ext = "mp3";
AudioSourceSDFAT source(startFilePath, ext, PIN_AUDIO_KIT_SD_CARD_CS);
MP3DecoderHelix decoder;
SynchronizedNBuffer<uint8_t> buffer(buffer_size, buffer_count, portMAX_DELAY,
SynchronizedNBuffer buffer(buffer_size, buffer_count, portMAX_DELAY,
10);
QueueStream<uint8_t> out(buffer); // convert Buffer to Stream
AudioPlayer player(source, out, decoder);
Expand Down
31 changes: 31 additions & 0 deletions examples/tests/rp2040/buffer2040/buffer2040.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

#include "AudioTools.h"
#include "AudioTools/Concurrency/RP2040.h"

AudioInfo info(44100, 2, 16);
SineWaveGenerator<int16_t> sineWave(32000); // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<int16_t> sound(sineWave); // Stream generated from sine wave
BufferRP2040 buffer(1024, 10);
QueueStream queue(buffer);
CsvOutput<int16_t> csv(Serial);
StreamCopy copierFill(queue, sound); // copies sound into i2s
StreamCopy copierConsume(csv, queue); // copies sound into i2s


void setup(){
Serial.begin(115200);
while(!Serial);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);

queue.begin();
sineWave.begin(info, N_B4);
csv.begin(info);
}

void loop(){
copierFill.copy();
}

void loop1(){
copierConsume.copy();
}
61 changes: 61 additions & 0 deletions examples/tests/rp2040/mutex/mutex.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "AudioTools.h"
#include "AudioTools/Concurrency/RP2040.h"

SpinLock mutex;
NBuffer<int16_t> nbuffer(512, 10);
SynchronizedBuffer<int16_t> buffer(nbuffer, mutex);

void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("starting...");
}

void loop() {
int16_t data[512];
for (int j = 0; j < 512; j++) {
data[j] = j;
}
size_t result = buffer.writeArray(data, 512);
if(result != 512){
// queue is full: give the reading queue some chance to empty
delay(5);
}
}

void loop1() {
static uint64_t start = millis();
static size_t total_bytes = 0;
static size_t errors = 0;
static int16_t data[512];

// read data
size_t result = buffer.readArray(data, 512);
if(result == 0){
// reading queue is empty: give some time to fill
delay(5);
return;
}

// check data
for (int j = 0; j < 512; j++) {
if (data[j] != j) errors++;
}
// calculate bytes per second
total_bytes += sizeof(int16_t) * 512;
if (total_bytes >= 1024000) {
uint64_t duration = millis() - start;
float mbps = static_cast<float>(total_bytes) / duration / 1000.0;

// print result
Serial.print("Mbytes per second: ");
Serial.print(mbps);
Serial.print(" with ");
Serial.print(errors);
Serial.println(" errors");

start = millis();
errors = 0;
total_bytes = 0;
}
}
60 changes: 60 additions & 0 deletions examples/tests/rp2040/mutex2040/mutex2040.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "AudioTools.h"
#include "AudioTools/Concurrency/RP2040.h"

MutexRP2040 mutex;
NBuffer<int16_t> nbuffer(512, 10);
SynchronizedBuffer<int16_t> buffer(nbuffer, mutex);

void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("starting...");
}

void loop() {
int16_t data[512];
for (int j = 0; j < 512; j++) {
data[j] = j;
}
size_t result = buffer.writeArray(data, 512);
if(result != 512){
Serial.print("write failed: ");
Serial.println(result);
}
}

void loop1() {
static uint64_t start = millis();
static size_t total_bytes = 0;
static size_t errors = 0;
static int16_t data[512];

// read data
size_t result = buffer.readArray(data, 512);
if(result != 512){
Serial.print("read failed: ");
Serial.println(result);
}

// check data
for (int j = 0; j < 512; j++) {
if (data[j] != j) errors++;
}
// calculate bytes per second
total_bytes += sizeof(int16_t) * 512;
if (total_bytes >= 1024000) {
uint64_t duration = millis() - start;
float mbps = static_cast<float>(total_bytes) / duration / 1000.0;

// print result
Serial.print("Mbytes per second: ");
Serial.print(mbps);
Serial.print(" with ");
Serial.print(errors);
Serial.println(" errors");

start = millis();
errors = 0;
total_bytes = 0;
}
}
2 changes: 1 addition & 1 deletion src/AudioTools/AudioLibs/A2DPStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "BluetoothA2DPSink.h"
#include "BluetoothA2DPSource.h"
#include "AudioTools/CoreAudio/AudioStreams.h"
#include "AudioTools/Concurrency/BufferRTOS.h"
#include "AudioTools/Concurrency/RTOS/BufferRTOS.h"
#include "AudioTools/CoreAudio/AudioBasic/StrView.h"


Expand Down
2 changes: 1 addition & 1 deletion src/AudioTools/AudioLibs/VBANStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "AudioTools/AudioLibs/vban/vban.h"
#include "AudioTools/CoreAudio/AudioStreams.h"
#include "AudioTools/Concurrency/BufferRTOS.h"
#include "AudioTools/Concurrency/RTOS/BufferRTOS.h"

namespace audio_tools {

Expand Down
3 changes: 1 addition & 2 deletions src/AudioTools/Concurrency/Mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ class MutexBase {
virtual void unlock() {}
};


class SpinLock : public MutexBase {
std::atomic<bool> lock_ = {0};
volatile std::atomic<bool> lock_ = {0};

void lock() {
for (;;) {
Expand Down
6 changes: 6 additions & 0 deletions src/AudioTools/Concurrency/RP2040.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once
#include "AudioTools/Concurrency/Mutex.h"
#include "AudioTools/Concurrency/SynchronizedBuffer.h"
#include "AudioTools/Concurrency/SynchronizedQueue.h"
#include "AudioTools/Concurrency/RP2040/BufferRP2040.h"
#include "AudioTools/Concurrency/RP2040/MutexRP2040.h"
Loading

0 comments on commit 6a98cc1

Please sign in to comment.