Skip to content

Commit

Permalink
Fixes to buffer reader error
Browse files Browse the repository at this point in the history
  • Loading branch information
synesthesiam committed Nov 2, 2019
1 parent 4992b35 commit a93a29b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ docker: installer

tar-gz: installer
bash debianize.sh --nopackage --architecture $(DEBIAN_ARCH)
tar -C debian/voice2json_1.0_$(DEBIAN_ARCH) -czf dist/voice2json_$(DEBIAN_ARCH).tar.gz usr
tar -C debian/voice2json_1.0_$(DEBIAN_ARCH)/usr -czf dist/voice2json_$(DEBIAN_ARCH).tar.gz bin lib

# -----------------------------------------------------------------------------
# Multi-Arch Builds
Expand Down
4 changes: 3 additions & 1 deletion debian/voice2json_1.0_aarch64/usr/bin/voice2json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env bash
this_dir="$( cd "$( dirname "$0" )" && pwd )"

if [[ -z "${voice2json_dir}" ]]; then
export voice2json_dir='/usr/lib/voice2json'
export voice2json_dir="$(realpath "${this_dir}/../lib/voice2json")"
fi

CPU_ARCH="$(lscpu | awk '/^Architecture/{print $2}')"
Expand Down
4 changes: 3 additions & 1 deletion debian/voice2json_1.0_amd64/usr/bin/voice2json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env bash
this_dir="$( cd "$( dirname "$0" )" && pwd )"

if [[ -z "${voice2json_dir}" ]]; then
export voice2json_dir='/usr/lib/voice2json'
export voice2json_dir="$(realpath "${this_dir}/../lib/voice2json")"
fi

CPU_ARCH="$(lscpu | awk '/^Architecture/{print $2}')"
Expand Down
4 changes: 3 additions & 1 deletion debian/voice2json_1.0_armhf/usr/bin/voice2json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env bash
this_dir="$( cd "$( dirname "$0" )" && pwd )"

if [[ -z "${voice2json_dir}" ]]; then
export voice2json_dir='/usr/lib/voice2json'
export voice2json_dir="$(realpath "${this_dir}/../lib/voice2json")"
fi

CPU_ARCH="$(lscpu | awk '/^Architecture/{print $2}')"
Expand Down
16 changes: 15 additions & 1 deletion voice2json/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,21 @@ def record_command(
audio_source = get_audio_source(profile)
logger.debug(f"Recording raw 16-bit 16Khz mono audio")
elif args.audio_source == "-":
audio_source = sys.stdin.buffer
# Avoid crash when stdin is closed/read in daemon thread
class FakeStdin:
def __init__(self):
self.done = False

def read(self, n):
if self.done:
return None

return sys.stdin.buffer.read(n)

def close(self):
self.done = True

audio_source = FakeStdin()
logger.debug(f"Recording raw 16-bit 16Khz mono audio from stdin")
else:
audio_source: BinaryIO = open(args.audio_source, "rb")
Expand Down
10 changes: 9 additions & 1 deletion voice2json/command/webrtcvad.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ def read_audio():
try:
while True:
chunk = audio_file.read(chunk_size)
if chunk is None:
break

if len(chunk) == chunk_size:
if report_audio:
logger.debug("Receiving audio")
Expand All @@ -161,13 +164,18 @@ def read_audio():
except Exception as e:
logger.exception("read_audio")

threading.Thread(target=read_audio, daemon=True).start()
threading.Thread(target=read_audio).start()

# -------------------------------------------------------------------------

# Process a voice command immediately
process_audio()

try:
audio_file.close()
except Exception as e:
logger.exception("close audio")

# Merge before/during command audio data
before_buffer = bytes()
for chunk in before_phrase_chunks:
Expand Down

0 comments on commit a93a29b

Please sign in to comment.