GeneratorFromStream

This commit is contained in:
Phil Schatzmann 2022-03-08 16:08:02 +01:00
parent ad3d9f44c4
commit a78f026e13
4 changed files with 37 additions and 21 deletions

View File

@ -34,5 +34,6 @@ void loop() {
if (millis()>timeout && !sound.isActive()){
sound.begin();
timeout = millis() + 5000;
}
}

View File

@ -48,7 +48,7 @@ class AudioEffects : public SoundGenerator<effect_t> {
}
/// Constructor which is assigning a Stream as input. The stream must consist of int16_t values
/// with the indicated number of channels
/// with the indicated number of channels. Type type parameter is e.g. <GeneratorFromStream<effect_t>
AudioEffects(Stream &input, int channels=2, float volume=1.0) {
setInput(* (new GeneratorT(input, channels, volume)));
owns_generator = true;

View File

@ -385,9 +385,10 @@ class GeneratorFromArray : public SoundGenerator<T> {
public:
template <size_t arrayLen>
GeneratorFromArray(T(&array)[arrayLen], int repeat=0) {
GeneratorFromArray(T(&array)[arrayLen], int repeat=0, bool setInactiveAtEnd=true) {
LOGD(LOG_METHOD);
this->maxRepeat = repeat;
this->max_repeat = repeat;
this->inactive_at_end = setInactiveAtEnd;
setArray(array, arrayLen);
}
@ -398,49 +399,60 @@ class GeneratorFromArray : public SoundGenerator<T> {
}
void setArray(T*array, size_t size){
this->tableLength = size;
this->table_length = size;
this->table = array;
LOGI("tableLength: %d", (int)size);
LOGI("table_length: %d", (int)size);
}
/// Starts the generation of samples
void begin() override {
LOGI(LOG_METHOD);
SoundGenerator<T>::begin();
soundIndex = 0;
repeatCounter = 0;
sound_index = 0;
repeat_counter = 0;
is_running = true;
}
/// Provides a single sample
T readSample() override {
// at end deactivate output
if (soundIndex >= tableLength) {
// LOGD("reset index - soundIndex: %d, tableLength: %d",soundIndex,tableLength);
soundIndex = 0;
if (sound_index >= table_length) {
// LOGD("reset index - sound_index: %d, table_length: %d",sound_index,table_length);
sound_index = 0;
// deactivate when count has been used up
if (maxRepeat>=1 && ++repeatCounter>=maxRepeat){
this->active = false;
LOGD("active: false");
if (max_repeat>=1 && ++repeat_counter>=max_repeat){
LOGD("atEnd");
this->is_running = false;
if (inactive_at_end){
this->active = false;
}
}
}
//LOGD("index: %d - active: %d", soundIndex, this->active);
//LOGD("index: %d - active: %d", sound_index, this->active);
T result = 0;
if (this->active) {
result = table[soundIndex];
soundIndex++;
if (this->is_running) {
result = table[sound_index];
sound_index++;
}
return result;
}
// Similar like
bool isRunning() {
return is_running;
}
protected:
int soundIndex = 0;
int maxRepeat = 0;
int repeatCounter = 0;
int sound_index = 0;
int max_repeat = 0;
int repeat_counter = 0;
bool inactive_at_end;
bool is_running = false;
T *table;
size_t tableLength = 0;
size_t table_length = 0;
};

3
src/AudioLogger.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
// added so that we can include the AudioLogger from Arduino w/o calling include "AudioTools.h" first.
#include "AudioTools/AudioLogger.h"