Skip to content

Microphone

pipeline pipeline

The Microphone pipeline reads input audio from a microphone device. This pipeline is designed to run on local machines given that it requires access to read from an input device.

Example

The following shows a simple example using this pipeline.

from txtai.pipeline import Microphone

# Create and run pipeline
microphone = Microphone()
microphone()

This pipeline may require additional system dependencies. See this section for more.

Configuration-driven example

Pipelines are run with Python or configuration. Pipelines can be instantiated in configuration using the lower case name of the pipeline. Configuration-driven pipelines are run with workflows or the API.

config.yml

# Create pipeline using lower case class name
audiostream:

# Run pipeline with workflow
workflow:
  audiostream:
    tasks:
      - action: audiostream

Run with Workflows

from txtai import Application

# Create and run pipeline with workflow
app = Application("config.yml")
list(app.workflow("audiostream", ["numpy data"]))

Run with API

CONFIG=config.yml uvicorn "txtai.api:app" &

curl \
  -X POST "http://localhost:8000/workflow" \
  -H "Content-Type: application/json" \
  -d '{"name":"audiostream", "elements":["numpy data"]}'

Methods

Python documentation for the pipeline.

__init__(rate=16000, vadmode=1, vadframe=30, vadthreshold=0.6)

Creates a new Microphone pipeline.

Parameters:

Name Type Description Default
rate

sample rate to record audio in, defaults to 16 kHz

16000
vadmode

aggressiveness of the voice activity detector, defaults to 1

1
vadframe

voice activity detector frame size in ms, defaults to 30

30
vadthreshold

percentage of frames (0.0 - 1.0) that must be voice to be considered speech, defaults to 0.6

0.6
Source code in txtai/pipeline/audio/microphone.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def __init__(self, rate=16000, vadmode=1, vadframe=30, vadthreshold=0.6):
    """
    Creates a new Microphone pipeline.

    Args:
        rate: sample rate to record audio in, defaults to 16 kHz
        vadmode: aggressiveness of the voice activity detector, defaults to 1
        vadframe: voice activity detector frame size in ms, defaults to 30
        vadthreshold: percentage of frames (0.0 - 1.0) that must be voice to be considered speech, defaults to 0.6
    """

    if not PYAUDIO:
        raise ImportError('Microphone pipeline is not available - install "pipeline" extra to enable')

    # Voice activity detector
    self.vad = webrtcvad.Vad(vadmode)
    self.vadframe = vadframe
    self.vadthreshold = vadthreshold

    # Sample rate
    self.rate = rate

    # Speech recognition config
    self.recognizer = sr.Recognizer()

__call__(device=None)

Source code in txtai/pipeline/audio/microphone.py
50
51
52
53
54
55
56
57
58
59
60
61
62
def __call__(self, device=None):
    # Read from microphone
    with sr.Microphone(sample_rate=self.rate) as source:
        # Calibrate microphone
        self.recognizer.adjust_for_ambient_noise(source)

        # Wait for speech
        audio = None
        while audio is None:
            audio = self.listen(source)

        # Return single element if single element passed in
        return (audio, self.rate) if device is None or not isinstance(device, list) else [(audio, self.rate)]