from __future__ import print_function
import time
import sys
from board import SCL, SDA
import busio
import digitalio
import speech_recognition as sr
from num2words import num2words
from subprocess import call
import time
import board
import busio
import adafruit_mpr121


def recognize_speech_from_mic(recognizer, microphone):
    """Transcribe speech from recorded from `microphone`.                                                                     
    Returns a dictionary with three keys:                                                                                     
    "success": a boolean indicating whether or not the API request was                                                        
               successful                                                                                                     
    "error":   `None` if no error occured, otherwise a string containing                                                      
               an error message if the API could not be reached or                                                            
               speech was unrecognizable                                                                                      
    "transcription": `None` if speech could not be transcribed,                                                               
               otherwise a string containing the transcribed text                                                             
    """
    # check that recognizer and microphone arguments are appropriate type                                                     
    if not isinstance(recognizer, sr.Recognizer):
        raise TypeError("`recognizer` must be `Recognizer` instance")

    if not isinstance(microphone, sr.Microphone):
        raise TypeError("`microphone` must be `Microphone` instance")

    # adjust the recognizer sensitivity to ambient noise and record audio                                                     
    # from the microphone                                                                                                     
    with microphone as source:
        recognizer.adjust_for_ambient_noise(source)
        audio = recognizer.listen(source)

    # set up the response object                                                                                              
    response = {
        "success": True,
        "error": None,
        "transcription": None
    }

    # try recognizing the speech in the recording                                                                             
    # if a RequestError or UnknownValueError exception is caught,                                                             
    #     update the response object accordingly                                                                              
    try:
        response["transcription"] = recognizer.recognize_google(audio)
    except sr.RequestError:
        # API was unreachable or unresponsive                                                                                 
        response["success"] = False
        response["error"] = "API unavailable"
    except sr.UnknownValueError:
        # speech was unintelligible                                                                                           
        response["error"] = "Unable to recognize speech"

    return response


def run_example():
    i2c = busio.I2C(board.SCL, board.SDA)
    mpr121 = adafruit_mpr121.MPR121(i2c)
    recognizer = sr.Recognizer()
    microphone = sr.Microphone()
    flag = False
    save_txt = ""
    while True:
        for i in range(12):
            if mpr121[i].value:
                print(f"Key {i} touched!")
                if i == 0:
                    if flag is False:
                        txt = "No voice note yet, recording a new voice note..."
                        call(f"espeak '{txt}'", shell=True)
                        guess = recognize_speech_from_mic(recognizer, microphone)
                        txt = "New pi note added: " + guess["transcription"]
                        call(f"espeak '{txt}'", shell=True)
                        flag = True
                        save_txt = guess["transcription"]
                    elif flag is True:
                        call(f"espeak '{save_txt}'", shell=True)
                if i == 5:
                    txt = "This chapter writes about the launch of the first veersion of ipad."
                    call(f"espeak '{txt}'", shell=True)
                if i == 2:
                    txt = "The important point made in this chapter is that people starting to realize how addictive video games could be"
                    call(f"espeak '{txt}'", shell=True)
        time.sleep(0.25)

if __name__ == '__main__':
    txt = "Welcome to Pi note"
    call(f"espeak '{txt}'", shell=True)
    try:
        run_example()
    except (KeyboardInterrupt, SystemExit) as exErr:
        print("\nEnding Example 2")
        sys.exit(0)

2188a5dfaa2a6cb551bcdc580bf92f28d575b9f0
<mask token>


def recognize_speech_from_mic(recognizer, microphone):
    """Transcribe speech from recorded from `microphone`.                                                                     
    Returns a dictionary with three keys:                                                                                     
    "success": a boolean indicating whether or not the API request was                                                        
               successful                                                                                                     
    "error":   `None` if no error occured, otherwise a string containing                                                      
               an error message if the API could not be reached or                                                            
               speech was unrecognizable                                                                                      
    "transcription": `None` if speech could not be transcribed,                                                               
               otherwise a string containing the transcribed text                                                             
    """
    if not isinstance(recognizer, sr.Recognizer):
        raise TypeError('`recognizer` must be `Recognizer` instance')
    if not isinstance(microphone, sr.Microphone):
        raise TypeError('`microphone` must be `Microphone` instance')
    with microphone as source:
        recognizer.adjust_for_ambient_noise(source)
        audio = recognizer.listen(source)
    response = {'success': True, 'error': None, 'transcription': None}
    try:
        response['transcription'] = recognizer.recognize_google(audio)
    except sr.RequestError:
        response['success'] = False
        response['error'] = 'API unavailable'
    except sr.UnknownValueError:
        response['error'] = 'Unable to recognize speech'
    return response


<mask token>

<mask token>


def recognize_speech_from_mic(recognizer, microphone):
    """Transcribe speech from recorded from `microphone`.                                                                     
    Returns a dictionary with three keys:                                                                                     
    "success": a boolean indicating whether or not the API request was                                                        
               successful                                                                                                     
    "error":   `None` if no error occured, otherwise a string containing                                                      
               an error message if the API could not be reached or                                                            
               speech was unrecognizable                                                                                      
    "transcription": `None` if speech could not be transcribed,                                                               
               otherwise a string containing the transcribed text                                                             
    """
    if not isinstance(recognizer, sr.Recognizer):
        raise TypeError('`recognizer` must be `Recognizer` instance')
    if not isinstance(microphone, sr.Microphone):
        raise TypeError('`microphone` must be `Microphone` instance')
    with microphone as source:
        recognizer.adjust_for_ambient_noise(source)
        audio = recognizer.listen(source)
    response = {'success': True, 'error': None, 'transcription': None}
    try:
        response['transcription'] = recognizer.recognize_google(audio)
    except sr.RequestError:
        response['success'] = False
        response['error'] = 'API unavailable'
    except sr.UnknownValueError:
        response['error'] = 'Unable to recognize speech'
    return response


def run_example():
    i2c = busio.I2C(board.SCL, board.SDA)
    mpr121 = adafruit_mpr121.MPR121(i2c)
    recognizer = sr.Recognizer()
    microphone = sr.Microphone()
    flag = False
    save_txt = ''
    while True:
        for i in range(12):
            if mpr121[i].value:
                print(f'Key {i} touched!')
                if i == 0:
                    if flag is False:
                        txt = (
                            'No voice note yet, recording a new voice note...')
                        call(f"espeak '{txt}'", shell=True)
                        guess = recognize_speech_from_mic(recognizer,
                            microphone)
                        txt = 'New pi note added: ' + guess['transcription']
                        call(f"espeak '{txt}'", shell=True)
                        flag = True
                        save_txt = guess['transcription']
                    elif flag is True:
                        call(f"espeak '{save_txt}'", shell=True)
                if i == 5:
                    txt = (
                        'This chapter writes about the launch of the first veersion of ipad.'
                        )
                    call(f"espeak '{txt}'", shell=True)
                if i == 2:
                    txt = (
                        'The important point made in this chapter is that people starting to realize how addictive video games could be'
                        )
                    call(f"espeak '{txt}'", shell=True)
        time.sleep(0.25)


<mask token>

<mask token>


def recognize_speech_from_mic(recognizer, microphone):
    """Transcribe speech from recorded from `microphone`.                                                                     
    Returns a dictionary with three keys:                                                                                     
    "success": a boolean indicating whether or not the API request was                                                        
               successful                                                                                                     
    "error":   `None` if no error occured, otherwise a string containing                                                      
               an error message if the API could not be reached or                                                            
               speech was unrecognizable                                                                                      
    "transcription": `None` if speech could not be transcribed,                                                               
               otherwise a string containing the transcribed text                                                             
    """
    if not isinstance(recognizer, sr.Recognizer):
        raise TypeError('`recognizer` must be `Recognizer` instance')
    if not isinstance(microphone, sr.Microphone):
        raise TypeError('`microphone` must be `Microphone` instance')
    with microphone as source:
        recognizer.adjust_for_ambient_noise(source)
        audio = recognizer.listen(source)
    response = {'success': True, 'error': None, 'transcription': None}
    try:
        response['transcription'] = recognizer.recognize_google(audio)
    except sr.RequestError:
        response['success'] = False
        response['error'] = 'API unavailable'
    except sr.UnknownValueError:
        response['error'] = 'Unable to recognize speech'
    return response


def run_example():
    i2c = busio.I2C(board.SCL, board.SDA)
    mpr121 = adafruit_mpr121.MPR121(i2c)
    recognizer = sr.Recognizer()
    microphone = sr.Microphone()
    flag = False
    save_txt = ''
    while True:
        for i in range(12):
            if mpr121[i].value:
                print(f'Key {i} touched!')
                if i == 0:
                    if flag is False:
                        txt = (
                            'No voice note yet, recording a new voice note...')
                        call(f"espeak '{txt}'", shell=True)
                        guess = recognize_speech_from_mic(recognizer,
                            microphone)
                        txt = 'New pi note added: ' + guess['transcription']
                        call(f"espeak '{txt}'", shell=True)
                        flag = True
                        save_txt = guess['transcription']
                    elif flag is True:
                        call(f"espeak '{save_txt}'", shell=True)
                if i == 5:
                    txt = (
                        'This chapter writes about the launch of the first veersion of ipad.'
                        )
                    call(f"espeak '{txt}'", shell=True)
                if i == 2:
                    txt = (
                        'The important point made in this chapter is that people starting to realize how addictive video games could be'
                        )
                    call(f"espeak '{txt}'", shell=True)
        time.sleep(0.25)


if __name__ == '__main__':
    txt = 'Welcome to Pi note'
    call(f"espeak '{txt}'", shell=True)
    try:
        run_example()
    except (KeyboardInterrupt, SystemExit) as exErr:
        print('\nEnding Example 2')
        sys.exit(0)

from __future__ import print_function
import time
import sys
from board import SCL, SDA
import busio
import digitalio
import speech_recognition as sr
from num2words import num2words
from subprocess import call
import time
import board
import busio
import adafruit_mpr121


def recognize_speech_from_mic(recognizer, microphone):
    """Transcribe speech from recorded from `microphone`.                                                                     
    Returns a dictionary with three keys:                                                                                     
    "success": a boolean indicating whether or not the API request was                                                        
               successful                                                                                                     
    "error":   `None` if no error occured, otherwise a string containing                                                      
               an error message if the API could not be reached or                                                            
               speech was unrecognizable                                                                                      
    "transcription": `None` if speech could not be transcribed,                                                               
               otherwise a string containing the transcribed text                                                             
    """
    if not isinstance(recognizer, sr.Recognizer):
        raise TypeError('`recognizer` must be `Recognizer` instance')
    if not isinstance(microphone, sr.Microphone):
        raise TypeError('`microphone` must be `Microphone` instance')
    with microphone as source:
        recognizer.adjust_for_ambient_noise(source)
        audio = recognizer.listen(source)
    response = {'success': True, 'error': None, 'transcription': None}
    try:
        response['transcription'] = recognizer.recognize_google(audio)
    except sr.RequestError:
        response['success'] = False
        response['error'] = 'API unavailable'
    except sr.UnknownValueError:
        response['error'] = 'Unable to recognize speech'
    return response


def run_example():
    i2c = busio.I2C(board.SCL, board.SDA)
    mpr121 = adafruit_mpr121.MPR121(i2c)
    recognizer = sr.Recognizer()
    microphone = sr.Microphone()
    flag = False
    save_txt = ''
    while True:
        for i in range(12):
            if mpr121[i].value:
                print(f'Key {i} touched!')
                if i == 0:
                    if flag is False:
                        txt = (
                            'No voice note yet, recording a new voice note...')
                        call(f"espeak '{txt}'", shell=True)
                        guess = recognize_speech_from_mic(recognizer,
                            microphone)
                        txt = 'New pi note added: ' + guess['transcription']
                        call(f"espeak '{txt}'", shell=True)
                        flag = True
                        save_txt = guess['transcription']
                    elif flag is True:
                        call(f"espeak '{save_txt}'", shell=True)
                if i == 5:
                    txt = (
                        'This chapter writes about the launch of the first veersion of ipad.'
                        )
                    call(f"espeak '{txt}'", shell=True)
                if i == 2:
                    txt = (
                        'The important point made in this chapter is that people starting to realize how addictive video games could be'
                        )
                    call(f"espeak '{txt}'", shell=True)
        time.sleep(0.25)


if __name__ == '__main__':
    txt = 'Welcome to Pi note'
    call(f"espeak '{txt}'", shell=True)
    try:
        run_example()
    except (KeyboardInterrupt, SystemExit) as exErr:
        print('\nEnding Example 2')
        sys.exit(0)

from __future__ import print_function
import time
import sys
from board import SCL, SDA
import busio
import digitalio
import speech_recognition as sr
from num2words import num2words
from subprocess import call
import time
import board
import busio
import adafruit_mpr121


def recognize_speech_from_mic(recognizer, microphone):
    """Transcribe speech from recorded from `microphone`.                                                                     
    Returns a dictionary with three keys:                                                                                     
    "success": a boolean indicating whether or not the API request was                                                        
               successful                                                                                                     
    "error":   `None` if no error occured, otherwise a string containing                                                      
               an error message if the API could not be reached or                                                            
               speech was unrecognizable                                                                                      
    "transcription": `None` if speech could not be transcribed,                                                               
               otherwise a string containing the transcribed text                                                             
    """
    # check that recognizer and microphone arguments are appropriate type                                                     
    if not isinstance(recognizer, sr.Recognizer):
        raise TypeError("`recognizer` must be `Recognizer` instance")

    if not isinstance(microphone, sr.Microphone):
        raise TypeError("`microphone` must be `Microphone` instance")

    # adjust the recognizer sensitivity to ambient noise and record audio                                                     
    # from the microphone                                                                                                     
    with microphone as source:
        recognizer.adjust_for_ambient_noise(source)
        audio = recognizer.listen(source)

    # set up the response object                                                                                              
    response = {
        "success": True,
        "error": None,
        "transcription": None
    }

    # try recognizing the speech in the recording                                                                             
    # if a RequestError or UnknownValueError exception is caught,                                                             
    #     update the response object accordingly                                                                              
    try:
        response["transcription"] = recognizer.recognize_google(audio)
    except sr.RequestError:
        # API was unreachable or unresponsive                                                                                 
        response["success"] = False
        response["error"] = "API unavailable"
    except sr.UnknownValueError:
        # speech was unintelligible                                                                                           
        response["error"] = "Unable to recognize speech"

    return response


def run_example():
    i2c = busio.I2C(board.SCL, board.SDA)
    mpr121 = adafruit_mpr121.MPR121(i2c)
    recognizer = sr.Recognizer()
    microphone = sr.Microphone()
    flag = False
    save_txt = ""
    while True:
        for i in range(12):
            if mpr121[i].value:
                print(f"Key {i} touched!")
                if i == 0:
                    if flag is False:
                        txt = "No voice note yet, recording a new voice note..."
                        call(f"espeak '{txt}'", shell=True)
                        guess = recognize_speech_from_mic(recognizer, microphone)
                        txt = "New pi note added: " + guess["transcription"]
                        call(f"espeak '{txt}'", shell=True)
                        flag = True
                        save_txt = guess["transcription"]
                    elif flag is True:
                        call(f"espeak '{save_txt}'", shell=True)
                if i == 5:
                    txt = "This chapter writes about the launch of the first veersion of ipad."
                    call(f"espeak '{txt}'", shell=True)
                if i == 2:
                    txt = "The important point made in this chapter is that people starting to realize how addictive video games could be"
                    call(f"espeak '{txt}'", shell=True)
        time.sleep(0.25)

if __name__ == '__main__':
    txt = "Welcome to Pi note"
    call(f"espeak '{txt}'", shell=True)
    try:
        run_example()
    except (KeyboardInterrupt, SystemExit) as exErr:
        print("\nEnding Example 2")
        sys.exit(0)
