add vosk audio model

added try/except on spotify
some basic file clean up
This commit is contained in:
maglore9900 2024-08-28 19:56:22 -04:00
parent 10e76f00b8
commit 28872342ee
7 changed files with 149 additions and 59 deletions

1
.gitignore vendored
View File

@ -25,6 +25,7 @@ share/python-wheels/
.installed.cfg
*.egg
MANIFEST
models
# PyInstaller
# Usually these files are written by a python script from a template

View File

@ -10,8 +10,10 @@ graph = agent.Agent()
while True:
text = sp.listen()
text = sp.listen2()
if text and "max" in text.lower():
if "exit" in text.lower():
break
response = loop.run_until_complete(graph.invoke_agent(text))
if response:
sp.glitch_stream_output2(response)

View File

@ -53,7 +53,7 @@ class Agent:
@tool("spotify")
async def spotify(self, command: str):
"""Use this tool to control spotify, commands include: play, pause, next, previous, favorite, search
"""Use this tool to control spotify, commands include: play, pause, stop, next, previous, favorite, search
Only use this tool if the user says Spotify in their query"""
return ""
@ -138,8 +138,8 @@ class Agent:
self.sp.previous_track()
elif command == "favorite":
self.sp.favorite_current_song()
elif command == "search":
self.sp.search_song_and_play(search)
# elif command == "search":
# self.sp.search_song_and_play(search)
else:
print("Invalid command")

View File

@ -5,7 +5,7 @@ from difflib import get_close_matches
from datetime import datetime, timedelta
class AppLauncher:
def __init__(self, search_paths=None, index_file='app_index.json'):
def __init__(self, search_paths=None, index_file='tmp/app_index.json'):
if search_paths is None:
search_paths = [
r'C:\Program Files',

View File

@ -10,6 +10,14 @@ from pydub import AudioSegment
import random
import urllib.parse
import os
import json
import pyaudio
from vosk import Model, KaldiRecognizer
import noisereduce as nr
from numpy import frombuffer, int16
import numpy as np
class Speak:
def __init__(self):
self.url = "http://127.0.0.1:7851/api/tts-generate"
@ -17,6 +25,11 @@ class Speak:
self.microphone = sr.Microphone()
self.engine = pyttsx3.init()
self.engine.setProperty('rate', 150)
self.model_path = os.path.join(os.path.dirname(__file__), "../models/vosk-model-en-us-0.42-gigaspeech")
self.model = Model(self.model_path)
self.recognizer = KaldiRecognizer(self.model, 16000)
def max_headroom(self, text):
data = {
@ -61,7 +74,7 @@ class Speak:
print("Listening...")
try:
# Listen with a 5-second timeout
audio = self.recognizer.listen(source, timeout=5)
audio = self.recognizer.listen(source, timeout=10)
try:
text = self.recognizer.recognize_google(audio)
print("You said: ", text)
@ -76,7 +89,59 @@ class Speak:
print("Timeout. No speech detected.")
return None
# def listen2(self):
# p = pyaudio.PyAudio()
# stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=8000)
# stream.start_stream()
# print("Listening...")
# try:
# while True:
# data = stream.read(8000, exception_on_overflow=False)
# filtered_data = nr.reduce_noise(y=frombuffer(data, dtype=int16), sr=16000).astype(int16).tobytes()
# if self.recognizer.AcceptWaveform(filtered_data):
# result = json.loads(self.recognizer.Result())
# if result["text"]:
# print(f"Recognized: {result['text']}")
# return result['text']
# else:
# pass
# except KeyboardInterrupt:
# print("Stopping...")
# finally:
# stream.stop_stream()
# stream.close()
# p.terminate()
def listen2(self, noise_threshold=500):
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=8000)
stream.start_stream()
print("Listening...")
try:
while True:
data = stream.read(8000, exception_on_overflow=False)
filtered_data = nr.reduce_noise(y=frombuffer(data, dtype=int16), sr=16000).astype(int16).tobytes()
# Calculate RMS to detect ambient noise levels
rms_value = np.sqrt(np.mean(np.square(np.frombuffer(filtered_data, dtype=int16))))
if rms_value < noise_threshold:
if self.recognizer.AcceptWaveform(filtered_data):
result = json.loads(self.recognizer.Result())
if result["text"]:
print(f"Recognized: {result['text']}")
return result['text']
else:
print(f"Ambient noise detected: RMS {rms_value} exceeds threshold {noise_threshold}")
except KeyboardInterrupt:
print("Stopping...")
finally:
stream.stop_stream()
stream.close()
p.terminate()
def stream_output(self, text):

View File

@ -21,71 +21,93 @@ class Spotify:
return None
def play(self):
device_id = self.get_active_device()
self.sp.start_playback(device_id=device_id)
try:
device_id = self.get_active_device()
self.sp.start_playback(device_id=device_id)
except Exception as e:
print(f"Failed to play: {e}")
def pause(self):
device_id = self.get_active_device()
self.sp.pause_playback(device_id=device_id)
try:
device_id = self.get_active_device()
self.sp.pause_playback(device_id=device_id)
except Exception as e:
print(f"Failed to pause playback: {e}")
def stop(self):
try:
device_id = self.get_active_device()
self.sp.pause_playback(device_id=device_id)
except Exception as e:
print(f"Failed to stop playback: {e}")
def next_track(self):
device_id = self.get_active_device()
self.sp.next_track(device_id=device_id)
try:
device_id = self.get_active_device()
self.sp.next_track(device_id=device_id)
except Exception as e:
print(f"Failed to skip to the next track: {e}")
def previous_track(self):
device_id = self.get_active_device()
self.sp.previous_track(device_id=device_id)
try:
device_id = self.get_active_device()
self.sp.previous_track(device_id=device_id)
except Exception as e:
print(f"Failed to go to the previous track: {e}")
def favorite_current_song(self):
current_track = self.sp.current_playback()
if current_track and current_track['item']:
track_id = current_track['item']['id']
self.sp.current_user_saved_tracks_add([track_id])
print(f"Added '{current_track['item']['name']}' to favorites")
else:
print("No song is currently playing")
try:
current_track = self.sp.current_playback()
if current_track and current_track['item']:
track_id = current_track['item']['id']
self.sp.current_user_saved_tracks_add([track_id])
print(f"Added '{current_track['item']['name']}' to favorites")
else:
print("No song is currently playing")
except Exception as e:
print(f"Failed to add current song to favorites: {e}")
def search_song_and_play(self, song_name):
results = self.sp.search(q='track:' + song_name, type='track')
if results['tracks']['items']:
track_uri = results['tracks']['items'][0]['uri']
device_id = self.get_active_device()
if device_id:
self.sp.start_playback(device_id=device_id, uris=[track_uri])
try:
results = self.sp.search(q='track:' + song_name, type='track')
if results['tracks']['items']:
track_uri = results['tracks']['items'][0]['uri']
device_id = self.get_active_device()
if device_id:
self.sp.start_playback(device_id=device_id, uris=[track_uri])
else:
print("No active device found. Please start Spotify on a device and try again.")
else:
print("No active device found. Please start Spotify on a device and try again.")
else:
print(f"No results found for song: {song_name}")
print(f"No results found for song: {song_name}")
except Exception as e:
print(f"Failed to search and play song '{song_name}': {e}")
def search_artist_and_play(self, artist_name):
results = self.sp.search(q='artist:' + artist_name, type='artist')
if results['artists']['items']:
artist_uri = results['artists']['items'][0]['uri']
device_id = self.get_active_device()
if device_id:
self.sp.start_playback(device_id=device_id, context_uri=artist_uri)
try:
results = self.sp.search(q='artist:' + artist_name, type='artist')
if results['artists']['items']:
artist_uri = results['artists']['items'][0]['uri']
device_id = self.get_active_device()
if device_id:
self.sp.start_playback(device_id=device_id, context_uri=artist_uri)
else:
print("No active device found. Please start Spotify on a device and try again.")
else:
print("No active device found. Please start Spotify on a device and try again.")
else:
print(f"No results found for artist: {artist_name}")
print(f"No results found for artist: {artist_name}")
except Exception as e:
print(f"Failed to search and play artist '{artist_name}': {e}")
def search_album_and_play(self, album_name):
results = self.sp.search(q='album:' + album_name, type='album')
if results['albums']['items']:
album_uri = results['albums']['items'][0]['uri']
device_id = self.get_active_device()
if device_id:
self.sp.start_playback(device_id=device_id, context_uri=album_uri)
try:
results = self.sp.search(q='album:' + album_name, type='album')
if results['albums']['items']:
album_uri = results['albums']['items'][0]['uri']
device_id = self.get_active_device()
if device_id:
self.sp.start_playback(device_id=device_id, context_uri=album_uri)
else:
print("No active device found. Please start Spotify on a device and try again.")
else:
print("No active device found. Please start Spotify on a device and try again.")
else:
print(f"No results found for album: {album_name}")
def favorite_current_song(self):
current_track = self.sp.current_playback()
if current_track and current_track['item']:
track_id = current_track['item']['id']
self.sp.current_user_saved_tracks_add([track_id])
print(f"Added '{current_track['item']['name']}' to favorites")
else:
print("No song is currently playing")
print(f"No results found for album: {album_name}")
except Exception as e:
print(f"Failed to search and play album '{album_name}': {e}")