Skip to content

Commit a78f026

Browse files
committed
GeneratorFromStream
1 parent ad3d9f4 commit a78f026

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ void loop() {
3434
if (millis()>timeout && !sound.isActive()){
3535
sound.begin();
3636
timeout = millis() + 5000;
37+
3738
}
3839
}

src/AudioEffects/AudioEffects.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class AudioEffects : public SoundGenerator<effect_t> {
4848
}
4949

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

src/AudioEffects/SoundGenerator.h

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,10 @@ class GeneratorFromArray : public SoundGenerator<T> {
385385
public:
386386

387387
template <size_t arrayLen>
388-
GeneratorFromArray(T(&array)[arrayLen], int repeat=0) {
388+
GeneratorFromArray(T(&array)[arrayLen], int repeat=0, bool setInactiveAtEnd=true) {
389389
LOGD(LOG_METHOD);
390-
this->maxRepeat = repeat;
390+
this->max_repeat = repeat;
391+
this->inactive_at_end = setInactiveAtEnd;
391392
setArray(array, arrayLen);
392393
}
393394

@@ -398,49 +399,60 @@ class GeneratorFromArray : public SoundGenerator<T> {
398399
}
399400

400401
void setArray(T*array, size_t size){
401-
this->tableLength = size;
402+
this->table_length = size;
402403
this->table = array;
403-
LOGI("tableLength: %d", (int)size);
404+
LOGI("table_length: %d", (int)size);
404405
}
405406

406407
/// Starts the generation of samples
407408
void begin() override {
408409
LOGI(LOG_METHOD);
409410
SoundGenerator<T>::begin();
410-
soundIndex = 0;
411-
repeatCounter = 0;
411+
sound_index = 0;
412+
repeat_counter = 0;
413+
is_running = true;
412414
}
413415

414416
/// Provides a single sample
415417
T readSample() override {
416418
// at end deactivate output
417-
if (soundIndex >= tableLength) {
418-
// LOGD("reset index - soundIndex: %d, tableLength: %d",soundIndex,tableLength);
419-
soundIndex = 0;
419+
if (sound_index >= table_length) {
420+
// LOGD("reset index - sound_index: %d, table_length: %d",sound_index,table_length);
421+
sound_index = 0;
420422
// deactivate when count has been used up
421-
if (maxRepeat>=1 && ++repeatCounter>=maxRepeat){
422-
this->active = false;
423-
LOGD("active: false");
423+
if (max_repeat>=1 && ++repeat_counter>=max_repeat){
424+
LOGD("atEnd");
425+
this->is_running = false;
426+
if (inactive_at_end){
427+
this->active = false;
428+
}
424429
}
425430
}
426431

427-
//LOGD("index: %d - active: %d", soundIndex, this->active);
432+
//LOGD("index: %d - active: %d", sound_index, this->active);
428433
T result = 0;
429-
if (this->active) {
430-
result = table[soundIndex];
431-
soundIndex++;
434+
if (this->is_running) {
435+
result = table[sound_index];
436+
sound_index++;
432437
}
433438

434439
return result;
435440
}
436441

442+
// Similar like
443+
bool isRunning() {
444+
return is_running;
445+
}
446+
437447

438448
protected:
439-
int soundIndex = 0;
440-
int maxRepeat = 0;
441-
int repeatCounter = 0;
449+
int sound_index = 0;
450+
int max_repeat = 0;
451+
int repeat_counter = 0;
452+
bool inactive_at_end;
453+
bool is_running = false;
442454
T *table;
443-
size_t tableLength = 0;
455+
size_t table_length = 0;
444456

445457
};
446458

src/AudioLogger.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
// added so that we can include the AudioLogger from Arduino w/o calling include "AudioTools.h" first.
3+
#include "AudioTools/AudioLogger.h"

0 commit comments

Comments
 (0)