1

Can I stop listening for audio on keyboard press? I tried altering the record function (in init.py) like this:

    def record(self, source, duration=None, offset=None):
        """
        Records up to ``duration`` seconds of audio from ``source`` (an ``AudioSource`` instance) starting at ``offset`` (or at the beginning if not specified) into an ``AudioData`` instance, which it returns.

        If ``duration`` is not specified, then it will record until there is no more audio input.
        """
        assert isinstance(source, AudioSource), "Source must be an audio source"
        assert source.stream is not None, "Audio source must be entered before recording, see documentation for ``AudioSource``; are you using ``source`` outside of a ``with`` statement?"

        frames = io.BytesIO()
        seconds_per_buffer = (source.CHUNK + 0.0) / source.SAMPLE_RATE
        elapsed_time = 0
        offset_time = 0
        offset_reached = False
        while True:  # loop for the total number of chunks needed
            if offset and not offset_reached:
                offset_time += seconds_per_buffer
                if offset_time > offset:
                    offset_reached = True

            buffer = source.stream.read(source.CHUNK)
            if len(buffer) == 0: break

            if offset_reached or not offset:
                elapsed_time += seconds_per_buffer
                if keyboard.read_key() == "p":
                    print("\nYou pressed p")
                    break

                frames.write(buffer)

        frame_data = frames.getvalue()
        frames.close()
        return AudioData(frame_data, source.SAMPLE_RATE, source.SAMPLE_WIDTH)

And calling it from my main script like this:

def Main():
r = sr.Recognizer()
try:
    with sr.Microphone() as source:
        print("Listening....")
        audio = r.record(source)
        print("Recognizing....")
        r.adjust_for_ambient_noise(source)
    text = r.recognize_google(audio)
    print(text.lower())
    if "lock computer" in text.lower():
        ctypes.windll.user32.LockWorkStation()
    elif "joke" in text.lower():
        joke = pyjokes.get_joke()
        speak(joke)
except Exception as e:
    print(e)
    Main()

This listen to the audio and stops listening when I press p but does not recognize it

Nikolay Shmyrev
  • 24,897
  • 5
  • 43
  • 87
Ahmed
  • 41
  • 8

1 Answers1

1

I figured it out, I saved the input file and saw that there was no audio in it so google could not reognize it. The error was in this block:

                if keyboard.read_key() == "p":
                    print("\nYou pressed p")
                    break

I changed this to:

                if keyboard.read_key() == "p":
                    print("\nYou pressed p")
                    pressed = True
                    break

And copied the listen function and dublicated it, changed its name to listen1 And now when I press P it stops listening and recognizing is also working.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Ahmed
  • 41
  • 8