From c6c31a831cf84c49246d12fe53462ee6a8679c62 Mon Sep 17 00:00:00 2001 From: Daniel Wolf Date: Tue, 21 Jun 2016 22:20:18 +0200 Subject: [PATCH] Using WebRTC for voice activity detection (VAD) My simple power-based approach wasn't reliable enough. --- CMakeLists.txt | 28 ++++++++++- LICENSE.md | 16 +++++- src/audio/processing.cpp | 40 +++++++++++++++ src/audio/processing.h | 9 ++++ src/audio/voiceActivityDetection.cpp | 73 ++++++++-------------------- src/phoneExtraction.cpp | 38 ++------------- 6 files changed, 116 insertions(+), 88 deletions(-) create mode 100644 src/audio/processing.cpp create mode 100644 src/audio/processing.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e57218..daf1c1b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -93,6 +93,31 @@ set_target_properties(gtest_main PROPERTIES FOLDER lib) # ... GSL include_directories(SYSTEM "lib/gsl/include") +# ... WebRTC +include_directories(SYSTEM "lib/webrtc-8d2248ff") +set(webRTCFiles + lib/webrtc-8d2248ff/webrtc/common_audio/signal_processing/cross_correlation.c + lib/webrtc-8d2248ff/webrtc/common_audio/signal_processing/division_operations.c + lib/webrtc-8d2248ff/webrtc/common_audio/signal_processing/downsample_fast.c + lib/webrtc-8d2248ff/webrtc/common_audio/signal_processing/energy.c + lib/webrtc-8d2248ff/webrtc/common_audio/signal_processing/get_scaling_square.c + lib/webrtc-8d2248ff/webrtc/common_audio/signal_processing/min_max_operations.c + lib/webrtc-8d2248ff/webrtc/common_audio/signal_processing/resample_48khz.c + lib/webrtc-8d2248ff/webrtc/common_audio/signal_processing/resample_by_2_internal.c + lib/webrtc-8d2248ff/webrtc/common_audio/signal_processing/resample_fractional.c + lib/webrtc-8d2248ff/webrtc/common_audio/signal_processing/spl_init.c + lib/webrtc-8d2248ff/webrtc/common_audio/signal_processing/spl_inl.c + lib/webrtc-8d2248ff/webrtc/common_audio/signal_processing/vector_scaling_operations.c + lib/webrtc-8d2248ff/webrtc/common_audio/vad/vad_core.c + lib/webrtc-8d2248ff/webrtc/common_audio/vad/vad_filterbank.c + lib/webrtc-8d2248ff/webrtc/common_audio/vad/vad_gmm.c + lib/webrtc-8d2248ff/webrtc/common_audio/vad/vad_sp.c + lib/webrtc-8d2248ff/webrtc/common_audio/vad/webrtc_vad.c +) +add_library(webRTC ${webRTCFiles}) +target_compile_options(webRTC PRIVATE ${disableWarningsFlags}) +set_target_properties(webRTC PROPERTIES FOLDER lib) + # ... Flite include_directories("lib/flite-1.4/include" "lib/flite-1.4") set(fliteFiles @@ -180,6 +205,7 @@ set(SOURCE_FILES src/audio/voiceActivityDetection.cpp src/audio/voiceActivityDetection.h src/audio/WaveFileReader.cpp src/audio/WaveFileReader.h src/audio/waveFileWriting.cpp src/audio/waveFileWriting.h + src/audio/processing.cpp src/audio/processing.h src/stringTools.cpp src/stringTools.h src/NiceCmdLineOutput.cpp src/NiceCmdLineOutput.h src/TablePrinter.cpp src/TablePrinter.h @@ -198,7 +224,7 @@ set(SOURCE_FILES src/tupleHash.h ) add_executable(rhubarb ${SOURCE_FILES}) -target_link_libraries(rhubarb ${Boost_LIBRARIES} cppFormat sphinxbase pocketSphinx flite) +target_link_libraries(rhubarb ${Boost_LIBRARIES} cppFormat sphinxbase pocketSphinx flite webRTC) target_compile_options(rhubarb PUBLIC ${enableWarningsFlags}) # Define test project diff --git a/LICENSE.md b/LICENSE.md index 6bef3ff..f29f8b9 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -160,4 +160,18 @@ The [Sound Change Applier](http://www.zompist.com/sounds.htm) and its [rule set > The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +### WebRTC + +The [WebRTC](https://chromium.googlesource.com/external/webrtc) library is released under the **3-clause BSD License**. + +> Copyright (c) 2011, The WebRTC project authors. All rights reserved. + +> Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +> * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +> * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +> * Neither the name of Google nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +> THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/src/audio/processing.cpp b/src/audio/processing.cpp new file mode 100644 index 0000000..a4fbdff --- /dev/null +++ b/src/audio/processing.cpp @@ -0,0 +1,40 @@ +#include "processing.h" + +using std::function; +using std::vector; + +// Converts a float in the range -1..1 to a signed 16-bit int +inline int16_t floatSampleToInt16(float sample) { + sample = std::max(sample, -1.0f); + sample = std::min(sample, 1.0f); + return static_cast(((sample + 1) / 2) * (INT16_MAX - INT16_MIN) + INT16_MIN); +} + +void process16bitAudioStream(AudioStream& audioStream, function&)> processBuffer, size_t bufferCapacity, ProgressSink& progressSink) { + // Process entire sound stream + vector buffer; + buffer.reserve(bufferCapacity); + int sampleCount = 0; + do { + // Read to buffer + buffer.clear(); + while (buffer.size() < bufferCapacity && !audioStream.endOfStream()) { + // Read sample + float floatSample = audioStream.readSample(); + int16_t sample = floatSampleToInt16(floatSample); + buffer.push_back(sample); + } + + // Process buffer + processBuffer(buffer); + + sampleCount += buffer.size(); + progressSink.reportProgress(static_cast(sampleCount) / audioStream.getSampleCount()); + } while (buffer.size()); +} + +void process16bitAudioStream(AudioStream& audioStream, function&)> processBuffer, ProgressSink& progressSink) { + const size_t capacity = 1600; // 0.1 second capacity + process16bitAudioStream(audioStream, processBuffer, capacity, progressSink); +} + diff --git a/src/audio/processing.h b/src/audio/processing.h new file mode 100644 index 0000000..074d3c8 --- /dev/null +++ b/src/audio/processing.h @@ -0,0 +1,9 @@ +#pragma once + +#include +#include +#include "audio/AudioStream.h" +#include "ProgressBar.h" + +void process16bitAudioStream(AudioStream& audioStream, std::function&)> processBuffer, size_t bufferCapacity, ProgressSink& progressSink); +void process16bitAudioStream(AudioStream& audioStream, std::function&)> processBuffer, ProgressSink& progressSink); \ No newline at end of file diff --git a/src/audio/voiceActivityDetection.cpp b/src/audio/voiceActivityDetection.cpp index 8774c68..6e2482b 100644 --- a/src/audio/voiceActivityDetection.cpp +++ b/src/audio/voiceActivityDetection.cpp @@ -1,76 +1,45 @@ #include "voiceActivityDetection.h" #include