michael@0: // Copyright (c) 2013 The Chromium Authors. All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: #include "endpointer.h" michael@0: michael@0: #include "AudioSegment.h" michael@0: michael@0: namespace { michael@0: const int kFrameRate = 200; // 1 frame = 5ms of audio. michael@0: } michael@0: michael@0: namespace mozilla { michael@0: michael@0: Endpointer::Endpointer(int sample_rate) michael@0: : speech_input_possibly_complete_silence_length_us_(-1), michael@0: speech_input_complete_silence_length_us_(-1), michael@0: audio_frame_time_us_(0), michael@0: sample_rate_(sample_rate), michael@0: frame_size_(0) { michael@0: Reset(); michael@0: michael@0: frame_size_ = static_cast(sample_rate / static_cast(kFrameRate)); michael@0: michael@0: speech_input_minimum_length_us_ = michael@0: static_cast(1.7 * 1000000); michael@0: speech_input_complete_silence_length_us_ = michael@0: static_cast(0.5 * 1000000); michael@0: long_speech_input_complete_silence_length_us_ = -1; michael@0: long_speech_length_us_ = -1; michael@0: speech_input_possibly_complete_silence_length_us_ = michael@0: 1 * 1000000; michael@0: michael@0: // Set the default configuration for Push To Talk mode. michael@0: EnergyEndpointerParams ep_config; michael@0: ep_config.set_frame_period(1.0f / static_cast(kFrameRate)); michael@0: ep_config.set_frame_duration(1.0f / static_cast(kFrameRate)); michael@0: ep_config.set_endpoint_margin(0.2f); michael@0: ep_config.set_onset_window(0.15f); michael@0: ep_config.set_speech_on_window(0.4f); michael@0: ep_config.set_offset_window(0.15f); michael@0: ep_config.set_onset_detect_dur(0.09f); michael@0: ep_config.set_onset_confirm_dur(0.075f); michael@0: ep_config.set_on_maintain_dur(0.10f); michael@0: ep_config.set_offset_confirm_dur(0.12f); michael@0: ep_config.set_decision_threshold(1000.0f); michael@0: ep_config.set_min_decision_threshold(50.0f); michael@0: ep_config.set_fast_update_dur(0.2f); michael@0: ep_config.set_sample_rate(static_cast(sample_rate)); michael@0: ep_config.set_min_fundamental_frequency(57.143f); michael@0: ep_config.set_max_fundamental_frequency(400.0f); michael@0: ep_config.set_contamination_rejection_period(0.25f); michael@0: energy_endpointer_.Init(ep_config); michael@0: } michael@0: michael@0: void Endpointer::Reset() { michael@0: old_ep_status_ = EP_PRE_SPEECH; michael@0: waiting_for_speech_possibly_complete_timeout_ = false; michael@0: waiting_for_speech_complete_timeout_ = false; michael@0: speech_previously_detected_ = false; michael@0: speech_input_complete_ = false; michael@0: audio_frame_time_us_ = 0; // Reset time for packets sent to endpointer. michael@0: speech_end_time_us_ = -1; michael@0: speech_start_time_us_ = -1; michael@0: } michael@0: michael@0: void Endpointer::StartSession() { michael@0: Reset(); michael@0: energy_endpointer_.StartSession(); michael@0: } michael@0: michael@0: void Endpointer::EndSession() { michael@0: energy_endpointer_.EndSession(); michael@0: } michael@0: michael@0: void Endpointer::SetEnvironmentEstimationMode() { michael@0: Reset(); michael@0: energy_endpointer_.SetEnvironmentEstimationMode(); michael@0: } michael@0: michael@0: void Endpointer::SetUserInputMode() { michael@0: energy_endpointer_.SetUserInputMode(); michael@0: } michael@0: michael@0: EpStatus Endpointer::Status(int64_t *time) { michael@0: return energy_endpointer_.Status(time); michael@0: } michael@0: michael@0: EpStatus Endpointer::ProcessAudio(const AudioChunk& raw_audio, float* rms_out) { michael@0: MOZ_ASSERT(raw_audio.mBufferFormat == AUDIO_FORMAT_S16, "Audio is not in 16 bit format"); michael@0: const int16_t* audio_data = static_cast(raw_audio.mChannelData[0]); michael@0: const int num_samples = raw_audio.mDuration; michael@0: EpStatus ep_status = EP_PRE_SPEECH; michael@0: michael@0: // Process the input data in blocks of frame_size_, dropping any incomplete michael@0: // frames at the end (which is ok since typically the caller will be recording michael@0: // audio in multiples of our frame size). michael@0: int sample_index = 0; michael@0: while (sample_index + frame_size_ <= num_samples) { michael@0: // Have the endpointer process the frame. michael@0: energy_endpointer_.ProcessAudioFrame(audio_frame_time_us_, michael@0: audio_data + sample_index, michael@0: frame_size_, michael@0: rms_out); michael@0: sample_index += frame_size_; michael@0: audio_frame_time_us_ += (frame_size_ * 1000000) / michael@0: sample_rate_; michael@0: michael@0: // Get the status of the endpointer. michael@0: int64_t ep_time; michael@0: ep_status = energy_endpointer_.Status(&ep_time); michael@0: if (old_ep_status_ != ep_status) michael@0: fprintf(stderr, "Status changed old= %d, new= %d\n", old_ep_status_, ep_status); michael@0: michael@0: // Handle state changes. michael@0: if ((EP_SPEECH_PRESENT == ep_status) && michael@0: (EP_POSSIBLE_ONSET == old_ep_status_)) { michael@0: speech_end_time_us_ = -1; michael@0: waiting_for_speech_possibly_complete_timeout_ = false; michael@0: waiting_for_speech_complete_timeout_ = false; michael@0: // Trigger SpeechInputDidStart event on first detection. michael@0: if (false == speech_previously_detected_) { michael@0: speech_previously_detected_ = true; michael@0: speech_start_time_us_ = ep_time; michael@0: } michael@0: } michael@0: if ((EP_PRE_SPEECH == ep_status) && michael@0: (EP_POSSIBLE_OFFSET == old_ep_status_)) { michael@0: speech_end_time_us_ = ep_time; michael@0: waiting_for_speech_possibly_complete_timeout_ = true; michael@0: waiting_for_speech_complete_timeout_ = true; michael@0: } michael@0: if (ep_time > speech_input_minimum_length_us_) { michael@0: // Speech possibly complete timeout. michael@0: if ((waiting_for_speech_possibly_complete_timeout_) && michael@0: (ep_time - speech_end_time_us_ > michael@0: speech_input_possibly_complete_silence_length_us_)) { michael@0: waiting_for_speech_possibly_complete_timeout_ = false; michael@0: } michael@0: if (waiting_for_speech_complete_timeout_) { michael@0: // The length of the silence timeout period can be held constant, or it michael@0: // can be changed after a fixed amount of time from the beginning of michael@0: // speech. michael@0: bool has_stepped_silence = michael@0: (long_speech_length_us_ > 0) && michael@0: (long_speech_input_complete_silence_length_us_ > 0); michael@0: int64_t requested_silence_length; michael@0: if (has_stepped_silence && michael@0: (ep_time - speech_start_time_us_) > long_speech_length_us_) { michael@0: requested_silence_length = michael@0: long_speech_input_complete_silence_length_us_; michael@0: } else { michael@0: requested_silence_length = michael@0: speech_input_complete_silence_length_us_; michael@0: } michael@0: michael@0: // Speech complete timeout. michael@0: if ((ep_time - speech_end_time_us_) > requested_silence_length) { michael@0: waiting_for_speech_complete_timeout_ = false; michael@0: speech_input_complete_ = true; michael@0: } michael@0: } michael@0: } michael@0: old_ep_status_ = ep_status; michael@0: } michael@0: return ep_status; michael@0: } michael@0: michael@0: } // namespace mozilla