> For the complete documentation index, see [llms.txt](https://prof-rossetti.gitbook.io/intro-to-python/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://prof-rossetti.gitbook.io/intro-to-python/notes/python/packages/speech_recognition.md).

# The SpeechRecognition Package

The `SpeechRecognition` package provides a high-level interface to record and process audio inputs in Python.

Reference:

* <https://github.com/Uberi/speech_recognition>
* <https://github.com/Uberi/speech_recognition/blob/master/reference/library-reference.rst>
* <https://github.com/s2t2/learning-new-sounds>
* <https://github.com/prof-rossetti/voice-interface-demo-py>

## Prerequisites

This package depends on another Python package called ["pyaudio"](http://people.csail.mit.edu/hubert/pyaudio/#downloads), which itself depends on a lower-level library caled "portaudio" (not a Python package). To install "portaudio":

* On a Mac, use homebrew (`brew install portaudio`).&#x20;
* On Windows, use pipwin within an active virtual environment (see installation steps below).

## Installation

Do these installation steps after activating a virtual environment.

Windows:

```bash
pip install pipwin
pipwin install pyaudio # will install along with lower level binaries
pip install SpeechRecognition # depends on the "pyaudio" Python package
```

Mac:

```bash
pip install pyaudio 
pip install SpeechRecognition
```

## Usage

### Recording Audio

Record audio using your computer's built-in microphone, and save that to a file:

```python
import speech_recognition as sr

client = sr.Recognizer()

with sr.Microphone() as mic:
    print("Say something!")
    audio = client.listen(mic)

with open("my-recording.flac", "wb") as f:
    f.write(audio.get_flac_data())
```

### Recognizing Speech

Record audio using your computer's built-in microphone, and recognize the spoken words:

```python
import speech_recognition as sr

client = sr.Recognizer()

with sr.Microphone() as mic:
    print("Say something!")
    audio = client.listen(mic)

# returns the transcript with the highest confidence:
transcript = client.recognize_google(audio)
#> 'how old is the Brooklyn Bridge'

# returns all transcripts:
response = client.recognize_google(audio, show_all=True)
#> {
#>   'alternative': [
#>     {
#>       'transcript': 'how old is the Brooklyn Bridge',
#>       'confidence': 0.987629
#>     }
#>   ],
#>   'final': True
#> }
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://prof-rossetti.gitbook.io/intro-to-python/notes/python/packages/speech_recognition.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
