Skip to content

examples.mic_live

Live Speech Transcription - Real-time with Voice Activity Detection Transcribes speech as you talk, detecting when you pause to process audio.

View the complete module source at #L1-L245.

API details

Each callable below includes its exact signature, type annotations, inputs, defaults, return contract, documented exceptions, implementation source, and parsed docstring sections when the source provides them.

examples.mic_live

Live Speech Transcription - Real-time with Voice Activity Detection

Transcribes speech as you talk, detecting when you pause to process audio. Much more natural than fixed-chunk transcription.

Usage

python examples/mic_live.py python examples/mic_live.py --model parakeet # Faster for English

Requirements

pip install sounddevice soundfile numpy

examples.mic_live.MODEL_ALIASES module-attribute

MODEL_ALIASES = {'whisper-small': 'mlx-community/whisper-small-mlx', 'whisper-medium': 'mlx-community/whisper-medium-mlx', 'whisper-large-v3': 'mlx-community/whisper-large-v3-mlx', 'whisper-turbo': 'mlx-community/whisper-large-v3-turbo', 'parakeet': 'mlx-community/parakeet-tdt-0.6b-v2'}

examples.mic_live.SAMPLE_RATE module-attribute

SAMPLE_RATE = 16000

examples.mic_live.LiveTranscriber

LiveTranscriber(model_name: str, language: str = None)

Live transcription with voice activity detection.

Source code in examples/mic_live.py
def __init__(self, model_name: str, language: str = None):
    self.model_name = model_name
    self.language = language

    # VAD settings
    self.silence_threshold = 0.015  # Audio level threshold
    self.speech_pad_ms = 300        # Padding around speech
    self.min_speech_ms = 500        # Minimum speech duration
    self.silence_duration_ms = 700  # Silence to trigger transcription

    # State
    self.audio_buffer = deque(maxlen=SAMPLE_RATE * 30)  # 30s max
    self.is_speaking = False
    self.speech_start = 0
    self.last_speech_time = 0
    self.pending_audio = []

    # Threading
    self.audio_queue = queue.Queue()
    self.result_queue = queue.Queue()
    self.running = False

    self.engine = None
    self.full_transcript = []

examples.mic_live.LiveTranscriber.model_name instance-attribute

model_name = model_name

examples.mic_live.LiveTranscriber.language instance-attribute

language = language

examples.mic_live.LiveTranscriber.silence_threshold instance-attribute

silence_threshold = 0.015

examples.mic_live.LiveTranscriber.speech_pad_ms instance-attribute

speech_pad_ms = 300

examples.mic_live.LiveTranscriber.min_speech_ms instance-attribute

min_speech_ms = 500

examples.mic_live.LiveTranscriber.silence_duration_ms instance-attribute

silence_duration_ms = 700

examples.mic_live.LiveTranscriber.audio_buffer instance-attribute

audio_buffer = deque(maxlen=SAMPLE_RATE * 30)

examples.mic_live.LiveTranscriber.is_speaking instance-attribute

is_speaking = False

examples.mic_live.LiveTranscriber.speech_start instance-attribute

speech_start = 0

examples.mic_live.LiveTranscriber.last_speech_time instance-attribute

last_speech_time = 0

examples.mic_live.LiveTranscriber.pending_audio instance-attribute

pending_audio = []

examples.mic_live.LiveTranscriber.audio_queue instance-attribute

audio_queue = queue.Queue()

examples.mic_live.LiveTranscriber.result_queue instance-attribute

result_queue = queue.Queue()

examples.mic_live.LiveTranscriber.running instance-attribute

running = False

examples.mic_live.LiveTranscriber.engine instance-attribute

engine = None

examples.mic_live.LiveTranscriber.full_transcript instance-attribute

full_transcript = []

examples.mic_live.LiveTranscriber.load_model

load_model()

Load STT model.

Source code in examples/mic_live.py
def load_model(self):
    """Load STT model."""
    from vllm_mlx.audio.stt import STTEngine
    print(f"Loading: {self.model_name}")
    self.engine = STTEngine(self.model_name)
    self.engine.load()
    print("Ready!")

examples.mic_live.LiveTranscriber.get_audio_level

get_audio_level(audio)

Get RMS audio level.

Source code in examples/mic_live.py
def get_audio_level(self, audio):
    """Get RMS audio level."""
    return np.sqrt(np.mean(audio ** 2))

examples.mic_live.LiveTranscriber.audio_callback

audio_callback(indata, frames, time_info, status)

Audio input callback.

Source code in examples/mic_live.py
def audio_callback(self, indata, frames, time_info, status):
    """Audio input callback."""
    if self.running:
        self.audio_queue.put((time.time(), indata.copy().flatten()))

examples.mic_live.LiveTranscriber.transcribe_audio

transcribe_audio(audio)

Transcribe audio array.

Source code in examples/mic_live.py
def transcribe_audio(self, audio):
    """Transcribe audio array."""
    import soundfile as sf

    with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
        temp_path = f.name

    try:
        sf.write(temp_path, audio, SAMPLE_RATE)
        result = self.engine.transcribe(temp_path, language=self.language)
        return result.text.strip()
    finally:
        os.unlink(temp_path)

examples.mic_live.LiveTranscriber.process_audio_stream

process_audio_stream()

Process audio with VAD.

Source code in examples/mic_live.py
def process_audio_stream(self):
    """Process audio with VAD."""
    speech_buffer = []

    while self.running:
        try:
            timestamp, audio = self.audio_queue.get(timeout=0.05)
            level = self.get_audio_level(audio)

            is_speech = level > self.silence_threshold

            if is_speech:
                if not self.is_speaking:
                    # Speech started
                    self.is_speaking = True
                    self.speech_start = timestamp
                    print("\r🎀 Listening...", end="", flush=True)

                self.last_speech_time = timestamp
                speech_buffer.extend(audio)

            elif self.is_speaking:
                # Still collecting (might be brief pause)
                speech_buffer.extend(audio)

                silence_ms = (timestamp - self.last_speech_time) * 1000
                speech_ms = (timestamp - self.speech_start) * 1000

                # Check if silence long enough to trigger transcription
                if silence_ms > self.silence_duration_ms and speech_ms > self.min_speech_ms:
                    # Transcribe collected audio
                    audio_array = np.array(speech_buffer, dtype=np.float32)

                    print("\r⏳ Processing...", end="", flush=True)

                    text = self.transcribe_audio(audio_array)

                    if text and len(text) > 1:
                        self.full_transcript.append(text)
                        # Clear line and print result
                        print(f"\r\033[KπŸ’¬ {text}")
                    else:
                        print("\r\033[K", end="")

                    # Reset
                    speech_buffer = []
                    self.is_speaking = False

        except queue.Empty:
            continue
        except Exception as e:
            print(f"\nError: {e}")

    # Process remaining audio
    if speech_buffer and len(speech_buffer) > SAMPLE_RATE * 0.5:
        audio_array = np.array(speech_buffer, dtype=np.float32)
        text = self.transcribe_audio(audio_array)
        if text:
            self.full_transcript.append(text)
            print(f"\r\033[KπŸ’¬ {text}")

examples.mic_live.LiveTranscriber.run

run()

Start live transcription.

Source code in examples/mic_live.py
def run(self):
    """Start live transcription."""
    print()
    print("=" * 60)
    print(" πŸŽ™οΈ  LIVE TRANSCRIPTION")
    print("=" * 60)
    print()
    print(" Speak naturally - transcribes when you pause")
    print(" Press Ctrl+C to stop")
    print()
    print("-" * 60)
    print()

    self.running = True

    # Start processing thread
    process_thread = threading.Thread(target=self.process_audio_stream, daemon=True)
    process_thread.start()

    # Start audio stream
    stream = sd.InputStream(
        samplerate=SAMPLE_RATE,
        channels=1,
        dtype=np.float32,
        callback=self.audio_callback,
        blocksize=int(SAMPLE_RATE * 0.1)
    )

    try:
        with stream:
            while True:
                time.sleep(0.1)
    except KeyboardInterrupt:
        pass
    finally:
        print("\n")
        self.running = False
        process_thread.join(timeout=3)

    return self.full_transcript

examples.mic_live.main

main()
Source code in examples/mic_live.py
def main():
    parser = argparse.ArgumentParser(description="Live Speech Transcription")
    parser.add_argument("--model", "-m", default="whisper-small",
                        help="Model (whisper-small, whisper-medium, parakeet)")
    parser.add_argument("--language", "-l", help="Language code (en, es, etc.)")
    parser.add_argument("--sensitivity", "-s", type=float, default=0.015,
                        help="Mic sensitivity 0.01-0.05 (default: 0.015)")
    args = parser.parse_args()

    print()
    print("╔════════════════════════════════════════════════════════╗")
    print("β•‘     πŸŽ™οΈ  Live Speech Transcription - vllm-mlx          β•‘")
    print("β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•")
    print()

    model_name = MODEL_ALIASES.get(args.model, args.model)

    transcriber = LiveTranscriber(
        model_name=model_name,
        language=args.language
    )
    transcriber.silence_threshold = args.sensitivity

    transcriber.load_model()

    transcripts = transcriber.run()

    # Final summary
    print("-" * 60)
    print()
    print("πŸ“ FULL TRANSCRIPT:")
    print()
    if transcripts:
        print(" " + " ".join(transcripts))
    else:
        print(" (No speech detected)")
    print()
    print("=" * 60)

Complete contract reference

Expand any definition for its exact inputs, annotations, defaults, return contract, directly raised exceptions, source-grounded behavior, and immutable line link. This section includes private and nested definitions that ordinary API generators omit.

examples.mic_live.LiveTranscriber Β· class
examples.mic_live.LiveTranscriber(model_name: str, language: str = None)

Live transcription with voice activity detection.

Parameters

Name Type Required Default Description
model_name str yes none Required positional or keyword input.
language str no None Optional positional or keyword input; defaults to None.

Returns

  • Constructs: examples.mic_live.LiveTranscriber

Exceptions and behavior

Class LiveTranscriber declares 7 direct member(s). No direct raise statement appears in this definition.

View source #L42-L201.

examples.mic_live.LiveTranscriber.__init__ Β· method
examples.mic_live.LiveTranscriber.__init__(model_name: str, language: str = None) -> not annotated

Method LiveTranscriber.__init__ updates self.model_name, self.language, self.silence_threshold, self.speech_pad_ms; calls deque, queue.Queue.

Parameters

Name Type Required Default Description
model_name str yes none Required positional or keyword input.
language str no None Optional positional or keyword input; defaults to None.

Returns

  • Type: not annotated

Exceptions and behavior

Method LiveTranscriber.__init__ updates self.model_name, self.language, self.silence_threshold, self.speech_pad_ms; calls deque, queue.Queue. No direct raise statement appears in this definition.

View source #L45-L68.

examples.mic_live.LiveTranscriber.load_model Β· method
examples.mic_live.LiveTranscriber.load_model() -> not annotated

Load STT model.

Parameters

This callable has no explicit inputs.

Returns

  • Type: not annotated

Exceptions and behavior

Method LiveTranscriber.load_model updates self.engine; calls print, STTEngine, self.engine.load. No direct raise statement appears in this definition.

View source #L70-L76.

examples.mic_live.LiveTranscriber.get_audio_level Β· method
examples.mic_live.LiveTranscriber.get_audio_level(audio) -> not annotated

Get RMS audio level.

Parameters

Name Type Required Default Description
audio not annotated yes none Required positional or keyword input.

Returns

  • Type: not annotated
  • Direct return expressions: np.sqrt(np.mean(audio ** 2))

Exceptions and behavior

Method LiveTranscriber.get_audio_level calls np.sqrt, np.mean; returns np.sqrt(np.mean(audio ** 2)). No direct raise statement appears in this definition.

View source #L78-L80.

examples.mic_live.LiveTranscriber.audio_callback Β· method
examples.mic_live.LiveTranscriber.audio_callback(indata, frames, time_info, status) -> not annotated

Audio input callback.

Parameters

Name Type Required Default Description
indata not annotated yes none Required positional or keyword input.
frames not annotated yes none Required positional or keyword input.
time_info not annotated yes none Required positional or keyword input.
status not annotated yes none Required positional or keyword input.

Returns

  • Type: not annotated

Exceptions and behavior

Method LiveTranscriber.audio_callback calls self.audio_queue.put, time.time, indata.copy().flatten, indata.copy. No direct raise statement appears in this definition.

View source #L82-L85.

examples.mic_live.LiveTranscriber.transcribe_audio Β· method
examples.mic_live.LiveTranscriber.transcribe_audio(audio) -> not annotated

Transcribe audio array.

Parameters

Name Type Required Default Description
audio not annotated yes none Required positional or keyword input.

Returns

  • Type: not annotated
  • Direct return expressions: result.text.strip()

Exceptions and behavior

Method LiveTranscriber.transcribe_audio calls tempfile.NamedTemporaryFile, sf.write, self.engine.transcribe, result.text.strip; returns result.text.strip(). No direct raise statement appears in this definition.

View source #L87-L99.

examples.mic_live.LiveTranscriber.process_audio_stream Β· method
examples.mic_live.LiveTranscriber.process_audio_stream() -> not annotated

Process audio with VAD.

Parameters

This callable has no explicit inputs.

Returns

  • Type: not annotated

Exceptions and behavior

Method LiveTranscriber.process_audio_stream updates self.is_speaking, self.speech_start, self.last_speech_time; calls self.audio_queue.get, self.get_audio_level, print, speech_buffer.extend. No direct raise statement appears in this definition.

View source #L101-L160.

examples.mic_live.LiveTranscriber.run Β· method
examples.mic_live.LiveTranscriber.run() -> not annotated

Start live transcription.

Parameters

This callable has no explicit inputs.

Returns

  • Type: not annotated
  • Direct return expressions: self.full_transcript

Exceptions and behavior

Method LiveTranscriber.run updates self.running; calls print, threading.Thread, process_thread.start, sd.InputStream; returns self.full_transcript. No direct raise statement appears in this definition.

View source #L162-L201.

examples.mic_live.main Β· function
examples.mic_live.main() -> not annotated

Function main calls argparse.ArgumentParser, parser.add_argument, parser.parse_args, print.

Parameters

This callable has no explicit inputs.

Returns

  • Type: not annotated

Exceptions and behavior

Function main calls argparse.ArgumentParser, parser.add_argument, parser.parse_args, print. No direct raise statement appears in this definition.

View source #L204-L241.

Complete symbol map

This map also includes private definitions and nested helpers. The signature column exposes every explicit input even when an internal helper has no dedicated parameter prose.

Symbol Kind Signature and inputs What it does Source
LiveTranscriber class LiveTranscriber(model_name: str, language: str = None) Live transcription with voice activity detection. #L42-L201
LiveTranscriber.__init__ method LiveTranscriber.__init__(model_name: str, language: str = None) -> not annotated Method LiveTranscriber.__init__ updates self.model_name, self.language, self.silence_threshold, self.speech_pad_ms; calls deque, queue.Queue. #L45-L68
LiveTranscriber.load_model method LiveTranscriber.load_model() -> not annotated Load STT model. #L70-L76
LiveTranscriber.get_audio_level method LiveTranscriber.get_audio_level(audio) -> not annotated Get RMS audio level. #L78-L80
LiveTranscriber.audio_callback method LiveTranscriber.audio_callback(indata, frames, time_info, status) -> not annotated Audio input callback. #L82-L85
LiveTranscriber.transcribe_audio method LiveTranscriber.transcribe_audio(audio) -> not annotated Transcribe audio array. #L87-L99
LiveTranscriber.process_audio_stream method LiveTranscriber.process_audio_stream() -> not annotated Process audio with VAD. #L101-L160
LiveTranscriber.run method LiveTranscriber.run() -> not annotated Start live transcription. #L162-L201
main function main() -> not annotated Function main calls argparse.ArgumentParser, parser.add_argument, parser.parse_args, print. #L204-L241