Arduino Audio Tools on Daisy ? #992
-
Hello, I really like your work and was wondering if your libraries could be used on the daisy seed platform? I've tried different approaches but without success. Thanks a lot! |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 21 replies
-
I don't see any reason why not. The main part is platform independent C++ and can be used with cmake or as part of Arduino. However you would need to implement the platform specific input or output stream classes that you want to use yourself, since DaisyDuino does not provide any such audio classes. This should not be too difficult... Where exactly are you struggling ? |
Beta Was this translation helpful? Give feedback.
-
The easiest way forward would be to use DaisyDuino and install my library. As a first step you could try to use my library as a data source (e.g. with the sine generator). |
Beta Was this translation helpful? Give feedback.
-
So, next you need to implement a Stream class in my framework. Something like the following. You can extend the logic a bit to be more flexible with the number of channels and the bits_per_sample. #pragma once;
#include "AudioToos/AudioStreams.h"
#include "DaisyDuino.h"
namespace audio_tools {
/**
* @brief Custom Stream for Daisyduino
* https://github.com/electro-smith/DaisyDuino
*/
class DaisyStream : public AudioStream {
public:
DaisyStream() { self = this; };
bool begin() {
TRACEI();
petal = DAISY.init(DAISY_SEED, daisy_rate);
petal.ClearLeds();
// start callback
DAISY.begin(AudioCallback);
return true;
}
/// Stops the processing and releases the memory
void end() {
TRACEI();
DAISY.end();
}
void setAudioInfo(AudioInfo info) override {
TRACED();
AudioStream::setAudioInfo(info);
switch (info.sample_rate) {
case 8000:
daisy_rate = AUDIO_SR_8K;
break;
case 16000:
daisy_rate = AUDIO_SR_16K;
break;
case 32000:
daisy_rate = AUDIO_SR_32K;
break;
case 48000:
daisy_rate = AUDIO_SR_48K;
break;
case 96000:
daisy_rate = AUDIO_SR_96K;
break;
default:
LOGE("Unsupported sample rate: %d", info.sample_rate);
break;
}
}
/// Write audio data to buffer
size_t write(const uint8_t *buffer, size_t size) override {
// check channels
if (info.channels != petal.num_channels) {
LOGE("Invalid channels %d - expected %d", info.channels, petal.num_channels);
return 0;
}
// write to buffer
return write_buffer.writeArray(buffer, size);
}
/// Limit amout of data to be written
int availableForWrite() { return 1024; }
protected:
// daisy hardware
static DaisyHardware petal;
// provide access from static methods
static DaisyStream *self;
// output buffer
NBuffer<uint8_t> *write_buffer(1024, 5);
// actually selected sample rate
DaisyDuinoSampleRate daisy_rate = AUDIO_SR_96K;
static void AudioCallback(float *in, float *out, size_t size) {
// process out
switch (self->bits_per_sample) {
case 16: {
for (int j = 0; j < size; j++) {
// get array of int16_t data from buffer
int bytes = size * sizeof(int16_t);
int16_t buffer[size];
self->writeBuffer->readArray((uint8_t *)buffer, bytes);
// output floats
for (int j = 0; j < size; j++) {
out[j] = static_cast<float>(buffer[j]) / 32768.0;
}
}
} break;
default:
LOGE("Unsupported bits_per_sample: %d", self->bits_per_sample);
}
}
}
};
DaisyStream *DaisyStream::self = nullptr;
} // namespace audio_tools
|
Beta Was this translation helpful? Give feedback.
-
I added the following to the documentation: https://github.com/pschatzmann/arduino-audio-tools/wiki/Writing-your-Custom-Input&Output-Class |
Beta Was this translation helpful? Give feedback.
-
Hello, it's me again, after a few months... I tried to implement your solution, but all I get are errors. In particular, I get this:
This raises two questions for me: firstly, indeed, this file is missing from this location. But more importantly, why is it important for us here? Isn't it the purpose of DaisyDuino to handle all this? Isn't the implementation of the custom class meant for this? Thank you for your help. (Here is my whole code if this can help : https://codeshare.io/obQ3bL |
Beta Was this translation helpful? Give feedback.
-
It should be NBuffer<uint8_t> write_buffer(1024, 3); which is a valid initialization in some cases but it should be better NBuffer<uint8_t> write_buffer{1024, 3}; Not sure why you run out of flash: I have my framework working on a STM32F411 Blackpill which has only 512KB flash. |
Beta Was this translation helpful? Give feedback.
I don't see any reason why not. The main part is platform independent C++ and can be used with cmake or as part of Arduino.
However you would need to implement the platform specific input or output stream classes that you want to use yourself, since DaisyDuino does not provide any such audio classes. This should not be too difficult...
Where exactly are you struggling ?