mirror of
https://github.com/maglore9900/max_headroom.git
synced 2025-06-07 03:55:30 +00:00
add vosk audio model
added try/except on spotify some basic file clean up
This commit is contained in:
parent
10e76f00b8
commit
28872342ee
1
.gitignore
vendored
1
.gitignore
vendored
@ -25,6 +25,7 @@ share/python-wheels/
|
|||||||
.installed.cfg
|
.installed.cfg
|
||||||
*.egg
|
*.egg
|
||||||
MANIFEST
|
MANIFEST
|
||||||
|
models
|
||||||
|
|
||||||
# PyInstaller
|
# PyInstaller
|
||||||
# Usually these files are written by a python script from a template
|
# Usually these files are written by a python script from a template
|
||||||
|
4
main.py
4
main.py
@ -10,8 +10,10 @@ graph = agent.Agent()
|
|||||||
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
text = sp.listen()
|
text = sp.listen2()
|
||||||
if text and "max" in text.lower():
|
if text and "max" in text.lower():
|
||||||
|
if "exit" in text.lower():
|
||||||
|
break
|
||||||
response = loop.run_until_complete(graph.invoke_agent(text))
|
response = loop.run_until_complete(graph.invoke_agent(text))
|
||||||
if response:
|
if response:
|
||||||
sp.glitch_stream_output2(response)
|
sp.glitch_stream_output2(response)
|
||||||
|
@ -53,7 +53,7 @@ class Agent:
|
|||||||
|
|
||||||
@tool("spotify")
|
@tool("spotify")
|
||||||
async def spotify(self, command: str):
|
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"""
|
Only use this tool if the user says Spotify in their query"""
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
@ -138,8 +138,8 @@ class Agent:
|
|||||||
self.sp.previous_track()
|
self.sp.previous_track()
|
||||||
elif command == "favorite":
|
elif command == "favorite":
|
||||||
self.sp.favorite_current_song()
|
self.sp.favorite_current_song()
|
||||||
elif command == "search":
|
# elif command == "search":
|
||||||
self.sp.search_song_and_play(search)
|
# self.sp.search_song_and_play(search)
|
||||||
else:
|
else:
|
||||||
print("Invalid command")
|
print("Invalid command")
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ from difflib import get_close_matches
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
class AppLauncher:
|
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:
|
if search_paths is None:
|
||||||
search_paths = [
|
search_paths = [
|
||||||
r'C:\Program Files',
|
r'C:\Program Files',
|
||||||
|
@ -10,6 +10,14 @@ from pydub import AudioSegment
|
|||||||
import random
|
import random
|
||||||
import urllib.parse
|
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:
|
class Speak:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.url = "http://127.0.0.1:7851/api/tts-generate"
|
self.url = "http://127.0.0.1:7851/api/tts-generate"
|
||||||
@ -17,6 +25,11 @@ class Speak:
|
|||||||
self.microphone = sr.Microphone()
|
self.microphone = sr.Microphone()
|
||||||
self.engine = pyttsx3.init()
|
self.engine = pyttsx3.init()
|
||||||
self.engine.setProperty('rate', 150)
|
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):
|
def max_headroom(self, text):
|
||||||
data = {
|
data = {
|
||||||
@ -61,7 +74,7 @@ class Speak:
|
|||||||
print("Listening...")
|
print("Listening...")
|
||||||
try:
|
try:
|
||||||
# Listen with a 5-second timeout
|
# Listen with a 5-second timeout
|
||||||
audio = self.recognizer.listen(source, timeout=5)
|
audio = self.recognizer.listen(source, timeout=10)
|
||||||
try:
|
try:
|
||||||
text = self.recognizer.recognize_google(audio)
|
text = self.recognizer.recognize_google(audio)
|
||||||
print("You said: ", text)
|
print("You said: ", text)
|
||||||
@ -76,7 +89,59 @@ class Speak:
|
|||||||
print("Timeout. No speech detected.")
|
print("Timeout. No speech detected.")
|
||||||
return None
|
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):
|
def stream_output(self, text):
|
||||||
|
@ -21,71 +21,93 @@ class Spotify:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
def play(self):
|
def play(self):
|
||||||
device_id = self.get_active_device()
|
try:
|
||||||
self.sp.start_playback(device_id=device_id)
|
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):
|
def pause(self):
|
||||||
device_id = self.get_active_device()
|
try:
|
||||||
self.sp.pause_playback(device_id=device_id)
|
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):
|
def next_track(self):
|
||||||
device_id = self.get_active_device()
|
try:
|
||||||
self.sp.next_track(device_id=device_id)
|
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):
|
def previous_track(self):
|
||||||
device_id = self.get_active_device()
|
try:
|
||||||
self.sp.previous_track(device_id=device_id)
|
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):
|
def favorite_current_song(self):
|
||||||
current_track = self.sp.current_playback()
|
try:
|
||||||
if current_track and current_track['item']:
|
current_track = self.sp.current_playback()
|
||||||
track_id = current_track['item']['id']
|
if current_track and current_track['item']:
|
||||||
self.sp.current_user_saved_tracks_add([track_id])
|
track_id = current_track['item']['id']
|
||||||
print(f"Added '{current_track['item']['name']}' to favorites")
|
self.sp.current_user_saved_tracks_add([track_id])
|
||||||
else:
|
print(f"Added '{current_track['item']['name']}' to favorites")
|
||||||
print("No song is currently playing")
|
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):
|
def search_song_and_play(self, song_name):
|
||||||
results = self.sp.search(q='track:' + song_name, type='track')
|
try:
|
||||||
if results['tracks']['items']:
|
results = self.sp.search(q='track:' + song_name, type='track')
|
||||||
track_uri = results['tracks']['items'][0]['uri']
|
if results['tracks']['items']:
|
||||||
device_id = self.get_active_device()
|
track_uri = results['tracks']['items'][0]['uri']
|
||||||
if device_id:
|
device_id = self.get_active_device()
|
||||||
self.sp.start_playback(device_id=device_id, uris=[track_uri])
|
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:
|
else:
|
||||||
print("No active device found. Please start Spotify on a device and try again.")
|
print(f"No results found for song: {song_name}")
|
||||||
else:
|
except Exception as e:
|
||||||
print(f"No results found for song: {song_name}")
|
print(f"Failed to search and play song '{song_name}': {e}")
|
||||||
|
|
||||||
def search_artist_and_play(self, artist_name):
|
def search_artist_and_play(self, artist_name):
|
||||||
results = self.sp.search(q='artist:' + artist_name, type='artist')
|
try:
|
||||||
if results['artists']['items']:
|
results = self.sp.search(q='artist:' + artist_name, type='artist')
|
||||||
artist_uri = results['artists']['items'][0]['uri']
|
if results['artists']['items']:
|
||||||
device_id = self.get_active_device()
|
artist_uri = results['artists']['items'][0]['uri']
|
||||||
if device_id:
|
device_id = self.get_active_device()
|
||||||
self.sp.start_playback(device_id=device_id, context_uri=artist_uri)
|
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:
|
else:
|
||||||
print("No active device found. Please start Spotify on a device and try again.")
|
print(f"No results found for artist: {artist_name}")
|
||||||
else:
|
except Exception as e:
|
||||||
print(f"No results found for artist: {artist_name}")
|
print(f"Failed to search and play artist '{artist_name}': {e}")
|
||||||
|
|
||||||
def search_album_and_play(self, album_name):
|
def search_album_and_play(self, album_name):
|
||||||
results = self.sp.search(q='album:' + album_name, type='album')
|
try:
|
||||||
if results['albums']['items']:
|
results = self.sp.search(q='album:' + album_name, type='album')
|
||||||
album_uri = results['albums']['items'][0]['uri']
|
if results['albums']['items']:
|
||||||
device_id = self.get_active_device()
|
album_uri = results['albums']['items'][0]['uri']
|
||||||
if device_id:
|
device_id = self.get_active_device()
|
||||||
self.sp.start_playback(device_id=device_id, context_uri=album_uri)
|
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:
|
else:
|
||||||
print("No active device found. Please start Spotify on a device and try again.")
|
print(f"No results found for album: {album_name}")
|
||||||
else:
|
except Exception as e:
|
||||||
print(f"No results found for album: {album_name}")
|
print(f"Failed to search and play album '{album_name}': {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")
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user