Skip to content

Commit

Permalink
finished adding beep and loading one model parameter for capture_acti…
Browse files Browse the repository at this point in the history
…vations
  • Loading branch information
AmateurAcademic committed Feb 4, 2023
1 parent 1e93daa commit a5fb831
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
41 changes: 33 additions & 8 deletions examples/capture_activations.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import pyaudio
import numpy as np
from openwakeword.model import Model
import openwakeword
import scipy.io.wavfile
import datetime
import argparse
Expand Down Expand Up @@ -67,6 +68,14 @@
default=False,
required=False
)

parser.add_argument(
"--model",
help="The model to use for openWakeWord, leave blank to use all available models",
type=str,
required=False
)

args=parser.parse_args()

# Get microphone stream
Expand All @@ -78,10 +87,26 @@
mic_stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)

# Load pre-trained openwakeword models
owwModel = Model(
enable_speex_noise_suppression=args.noise_suppression,
vad_threshold = args.vad_threshold
)
if args.model:
model_paths = openwakeword.get_pretrained_model_paths()
for path in model_paths:
if args.model in path:
model_path = path

if model_path:
owwModel = Model(
wakeword_model_paths=[model_path],
enable_speex_noise_suppression=args.noise_suppression,
vad_threshold = args.vad_threshold
)
else:
print(f'Could not find model \"{args.model}\"')
exit()
else:
owwModel = Model(
enable_speex_noise_suppression=args.noise_suppression,
vad_threshold=args.vad_threshold
)

# Set waiting period after activation before saving clip (to get some audio context after the activation)
save_delay = 1 # seconds
Expand All @@ -102,10 +127,10 @@
print("\n\nListening for wakewords...\n")
while True:
# Get audio
audio = np.frombuffer(mic_stream.read(CHUNK), dtype=np.int16)
mic_audio = np.frombuffer(mic_stream.read(CHUNK), dtype=np.int16)

# Feed to openWakeWord model
prediction = owwModel.predict(audio)
prediction = owwModel.predict(mic_audio)

# Check for model activations (score above threshold), and save clips
for mdl in prediction.keys():
Expand All @@ -120,10 +145,10 @@

print(f'Detected activation from \"{mdl}\" model at time {detect_time}!')

# Capture total of 5 seconds, with the audio associated with the
# Capture total of 5 seconds, with the mic_ associated with the
# activation around the ~4 second point
audio_context = np.array(list(owwModel.preprocessor.raw_data_buffer)[-16000*5:]).astype(np.int16)
fname = detect_time + f"_{mdl}.wav"
scipy.io.wavfile.write(os.path.join(os.path.abspath(args.output_dir), fname), 16000, audio_context)

playBeep('audio/activation.wav')
playBeep('audio/activation.wav', audio)
13 changes: 5 additions & 8 deletions examples/utils/beep.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import pyaudio
import wave

def playBeep(file_path):
def playBeep(file_path, audio):
CHUNK = 1024

wf = wave.open(file_path, 'rb')

p = pyaudio.PyAudio()

stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
stream = audio.open(format=audio.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)

data = wf.readframes(CHUNK)

while data != '':
while data != b'':
stream.write(data)
data = wf.readframes(CHUNK)

#stream.stop_stream()
#stream.close()

#p.terminate()
stream.stop_stream()
stream.close()

0 comments on commit a5fb831

Please sign in to comment.