Skip to content

Commit ec6e5fe

Browse files
committed
DynamicMemoryStream post processing
1 parent 676c95c commit ec6e5fe

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

examples/examples-audiokit/streams-audiokit-ram-audiokit/streams-audiokit-ram-audiokit.ino

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ void record_start(bool pinStatus, int pin, void* ref){
2929

3030
void record_end(bool pinStatus, int pin, void* ref){
3131
Serial.println("Playing...");
32+
33+
// Remove popping noise, from button: we delete 6 segments at the beginning and end
34+
// and on the resulting audio we slowly raise the volume on the first segment
35+
// end decrease it on the last segment
36+
recording.postProcessSmoothTransition<int16_t>(channels, 6, 0.01);
37+
3238
copier.begin(kit, recording); // start playback
3339
}
3440

src/AudioTools/AudioStreams.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ class DynamicMemoryStream : public AudioStreamX {
338338
};
339339

340340
DynamicMemoryStream() = default;
341+
341342
DynamicMemoryStream(bool isLoop, int defaultBufferSize=DEFAULT_BUFFER_SIZE ) {
342343
this->default_buffer_size = defaultBufferSize;
343344
is_loop = isLoop;
@@ -460,6 +461,37 @@ class DynamicMemoryStream : public AudioStreamX {
460461
return audio_list;
461462
}
462463

464+
/// @brief Post processing after the recording. We add a smooth transition at the beginning and at the end
465+
/// @tparam T
466+
/// @param factor
467+
template<typename T>
468+
void postProcessSmoothTransition(int channels, int remove, float factor = 0.01){
469+
if (remove>0){
470+
for (int j=0;j<remove;j++){
471+
DataNode* node = nullptr;
472+
audio_list.pop_front(node);
473+
if (node!=nullptr) delete node;
474+
node = nullptr;
475+
audio_list.pop_back(node);
476+
if (node!=nullptr) delete node;
477+
}
478+
}
479+
480+
// Remove popping noise
481+
SmoothTransition<T> clean_start(channels, true, false, factor);
482+
auto first = *list().begin();
483+
if (first!=nullptr){
484+
clean_start.convert(first->data,first->len);
485+
}
486+
487+
SmoothTransition<T> clean_end(channels, false, true, factor);
488+
auto last = * (--(list().end()));
489+
if (last!=nullptr){
490+
clean_end.convert(last->data,last->len);
491+
}
492+
}
493+
494+
463495
protected:
464496
List<DataNode*> audio_list;
465497
List<DataNode*>::Iterator it = audio_list.end();

src/AudioTools/Converter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ class SmoothTransition : public BaseConverter<T> {
941941
if (factor>=0.8){
942942
break;
943943
} else {
944-
values[j]*=factor;
944+
values[j]=factor*values[j];
945945
}
946946
factor += inc;
947947
}
@@ -953,7 +953,7 @@ class SmoothTransition : public BaseConverter<T> {
953953
if (factor>=0.8){
954954
break;
955955
} else {
956-
values[j]*=factor;
956+
values[j]=factor*values[j];
957957
}
958958
}
959959
}

0 commit comments

Comments
 (0)