Skip to content

Commit

Permalink
Resample API input wav audio to ensure format matches listener config (
Browse files Browse the repository at this point in the history
…#180)

# Description
Adds explicit handling of API input audio to set sample_rate,
sample_width, and channels to match listener configuration
Refactor to prevent creating an audio stream object not used for
non-streaming STT

# Issues
NeonGeckoCom/neon-iris#28

# Other Notes
<!-- Note any breaking changes, WIP changes, requests for input, etc.
here -->

Co-authored-by: Daniel McKnight <[email protected]>
  • Loading branch information
NeonDaniel and NeonDaniel authored Nov 9, 2023
1 parent 3e59dbf commit 778c7e4
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions neon_speech/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,15 +456,21 @@ def _get_stt_from_file(self, wav_file: str,
:return: (AudioData of object, extracted context, transcriptions)
"""
from neon_utils.file_utils import get_audio_file_stream
lang = lang or 'en-us' # TODO: read default from config
segment = AudioSegment.from_file(wav_file)
lang = lang or self.config.get('lang')
desired_sample_rate = self.config['listener'].get('sample_rate', 16000)
desired_sample_width = self.config['listener'].get('sample_width', 2)
segment = (AudioSegment.from_file(wav_file).set_channels(1)
.set_frame_rate(desired_sample_rate)
.set_sample_width(desired_sample_width))
LOG.debug(f"Audio fr={segment.frame_rate},sw={segment.sample_width},"
f"fw={segment.frame_width},ch={segment.channels}")
audio_data = AudioData(segment.raw_data, segment.frame_rate,
segment.sample_width)
audio_stream = get_audio_file_stream(wav_file)
if not self.api_stt:
raise RuntimeError("api_stt not initialized."
" is `listener['enable_stt_api'] set to False?")
if hasattr(self.api_stt, 'stream_start'):
audio_stream = get_audio_file_stream(wav_file, desired_sample_rate)
if self.lock.acquire(True, 30):
LOG.info(f"Starting STT processing (lang={lang}): {wav_file}")
self.api_stt.stream_start(lang)
Expand Down

0 comments on commit 778c7e4

Please sign in to comment.