Skip to content

Commit f0b9817

Browse files
committed
SilenceRemovalConverter
2 parents 9d62a7c + c38ef97 commit f0b9817

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ add_subdirectory( "${CMAKE_CURRENT_SOURCE_DIR}/time-to-speech")
2929
add_subdirectory( "${CMAKE_CURRENT_SOURCE_DIR}/unit-to-text")
3030
add_subdirectory( "${CMAKE_CURRENT_SOURCE_DIR}/number-to-speech")
3131
add_subdirectory( "${CMAKE_CURRENT_SOURCE_DIR}/unit-to-speech")
32+
add_subdirectory( "${CMAKE_CURRENT_SOURCE_DIR}/audio-i2s-nosilence")
3233
# not supported (yet?)
3334
#add_subdirectory( "${CMAKE_CURRENT_SOURCE_DIR}/time-to-speech-sd")
3435
#add_subdirectory( "${CMAKE_CURRENT_SOURCE_DIR}/talking-clock")
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
# set the project name
4+
project(audio-i2s-nosilence)
5+
set (CMAKE_CXX_STANDARD 11)
6+
set (DCMAKE_CXX_FLAGS "-Werror")
7+
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
8+
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
9+
set (CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
10+
endif()
11+
12+
include(FetchContent)
13+
14+
# build sketch as executable
15+
add_executable (audio-i2s-nosilence audio-i2s-nosilence.ino ../main.cpp)
16+
# defines needed for desktop build
17+
target_compile_definitions(audio-i2s-nosilence PUBLIC -DARDUINO -DEXIT_ON_STOP -DIS_DESKTOP -DUSE_HELIX)
18+
# define ino as cpp
19+
set_source_files_properties(audio-i2s-nosilence.ino PROPERTIES LANGUAGE CXX)
20+
# specify libraries
21+
target_link_libraries(audio-i2s-nosilence arduino_emulator arduino_helix arduino-audio-tools simple-tts portaudio)
22+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
/**
3+
* @brief Some special rules for desktop builds.
4+
* The output should go to PortAudioStream
5+
*/
6+
7+
#ifdef IS_DESKTOP
8+
#include "AudioLibs/PortAudioStream.h"
9+
#define I2SStream PortAudioStream
10+
#define AudioKitStream PortAudioStream
11+
#endif

examples/audio-i2s-nosilence/audio-i2s-nosilence.ino

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111

1212
#include "SimpleTTS.h"
1313
#include "AudioCodecs/CodecMP3Helix.h"
14+
#include "Desktop.h"
1415

15-
I2SStream i2s; // final output
16-
VolumeStream volume(i2s); // volume support
17-
SilenceRemovalConverter<int16_t> silence(8,2); // suppress silence
18-
ConvertedStream<int16_t, SilenceRemovalConverter<int16_t>> out(volume,silence);
16+
I2SStream i2s;
17+
VolumeStream volume(i2s);
18+
SilenceRemovalConverter<int16_t> rem(8, 2);
19+
ConvertedStream<int16_t,SilenceRemovalConverter<int16_t>> out(volume, rem);
1920

2021
MP3DecoderHelix mp3;
2122
AudioDictionary dictionary(ExampleAudioDictionaryValues);

src/SilenceRemovalStream.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ class SilenceRemovalStream : public audio_tools::AudioStreamX {
2626
LOGI("begin(n=%d, aplidudeLimit=%d", n, aplidudeLimit);
2727
this->n = n;
2828
this->amplidude_limit = aplidudeLimit;
29-
this->priorLastAudioPos = 0;
29+
this->priorLastAudioPos = n+1; // ignore first values
3030
this->active = n > 0;
31+
return true;
3132
}
3233

3334
virtual size_t write(const uint8_t *data, size_t size) override {
@@ -53,18 +54,18 @@ class SilenceRemovalStream : public audio_tools::AudioStreamX {
5354
// write audio data w/o silence
5455
size_t write_size = write_count * sizeof(T);
5556
p_out->write(buffer, write_size);
56-
LOGD("filtered from %d -> %d", size, write_size);
57+
LOGI("filtered silence from %d -> %d", (int)size, (int)write_size);
5758

58-
// calculate negative offset for next run
59-
priorLastAudioPos = size - findLastAudioPos(audio, sample_count - 1);
59+
// number of empty samples of prior buffer
60+
priorLastAudioPos = findLastAudioPos(audio, sample_count - 1);
6061

6162
// return processed data size
6263
return size;
6364
}
6465

6566
virtual int availableForWrite() override { return p_out->availableForWrite(); }
6667

67-
void end() {
68+
void end() override {
6869
priorLastAudioPos = 0;
6970
active = false;
7071
}
@@ -87,8 +88,8 @@ class SilenceRemovalStream : public audio_tools::AudioStreamX {
8788
int findLastAudioPos(T *audio, int pos) {
8889
for (int j = 0; j < n; j++) {
8990
// we are before the start of the current buffer
90-
if (pos - j < 0) {
91-
return pos - j + priorLastAudioPos;
91+
if (pos - j <= 0) {
92+
return priorLastAudioPos;
9293
}
9394
// we are in the current buffer
9495
if (abs(audio[pos - j]) > amplidude_limit) {

0 commit comments

Comments
 (0)