Voice-Controlled Notes App

import speech_recognition as sr

import pyttsx3

import os


notes = {}


engine = pyttsx3.init()


def speak(text):

    engine.say(text)

    engine.runAndWait()


def listen_command():

    r = sr.Recognizer()

    with sr.Microphone() as source:

        speak("Listening...")

        audio = r.listen(source)

    try:

        command = r.recognize_google(audio)

        return command.lower()

    except:

        speak("Sorry, I didn't catch that.")

        return ""


def create_note():

    speak("What should I name the note?")

    title = listen_command()

    speak("What is the content?")

    content = listen_command()

    notes[title] = content

    speak(f"Note '{title}' created.")


def read_notes():

    if notes:

        for title, content in notes.items():

            speak(f"{title}: {content}")

    else:

        speak("No notes found.")


def delete_note():

    speak("Which note should I delete?")

    title = listen_command()

    if title in notes:

        del notes[title]

        speak(f"Note '{title}' deleted.")

    else:

        speak("Note not found.")


def main():

    speak("Voice Notes App Started.")

    while True:

        speak("Say a command: create, read, delete, or exit.")

        command = listen_command()


        if "create" in command:

            create_note()

        elif "read" in command:

            read_notes()

        elif "delete" in command:

            delete_note()

        elif "exit" in command:

            speak("Goodbye!")

            break

        else:

            speak("Unknown command.")


if __name__ == "__main__":

    main()


No comments: