Skip to main content

Record audio directly within Jupyter/IPython/Colab using the browser microphone and ffmpeg.

Project description

Lab Mic

PyPI version License: MIT

Record audio directly within Jupyter Notebooks, JupyterLab, Google Colab, or other IPython environments using your browser's microphone. This package provides an ipywidgets-based interface, captures audio using standard browser APIs, and leverages ffmpeg for reliable conversion to WAV format (16-bit PCM mono).

Features

  • Simple ipywidgets interface for recording start/stop within notebooks.
  • Captures audio directly in the browser using getUserMedia and MediaRecorder.
  • Uses ffmpeg for robust conversion from web formats (like webm/opus) to WAV (16-bit PCM, mono).
  • Works across different Jupyter environments including Colab.
  • Allows specifying the target sampling rate for the output.
  • Returns the final audio data conveniently as a NumPy array.
  • Includes basic UI feedback for status and errors.

Installation

1. Install ffmpeg:

This package requires the ffmpeg command-line tool to be installed on the system where the Python kernel is running (this includes the Colab runtime if using Colab). ffmpeg must be accessible in the system's PATH.

  • Ubuntu/Debian: sudo apt update && sudo apt install ffmpeg
  • macOS (using Homebrew): brew install ffmpeg
  • Windows: Download from the official ffmpeg website, extract, and add the bin directory to your system's PATH.
  • Google Colab: ffmpeg is usually pre-installed on Colab runtimes.

2. Install the Python package:

Install lab_mic using pip. This will also install required Python dependencies (numpy, scipy, ffmpeg-python, ipython, ipywidgets).

pip install lab_mic

Usage

from lab_mic import LabMic

# Create an instance of LabMic
mic = LabMic()

# Start recording
mic.start_recording()

# Stop recording after a specified duration (e.g., 5 seconds)
mic.stop_recording(duration=5)

# Get the recorded audio data as a NumPy array
audio_data = mic.get_audio_data()

# Save the recorded audio to a file (optional)
mic.save_to_file("output.wav")

Example in Jupyter Notebook

from lab_mic import LabMic

# Initialize the microphone recorder
mic = LabMic(sampling_rate=16000)

print("Displaying recording interface...")
# --- Display the Interface ---
# This will show the button and status/preview area in the cell output
mic.display()

# --- Get the Processed Audio ---
audio_data = mic.get_result()

How it Works

The NotebookMic (or similarly named class) uses ipywidgets to create interactive elements (Button, Output area, hidden Text widget) in the notebook frontend. When initiated, it injects HTML and JavaScript code into the ipywidgets.Output area.
The JavaScript utilizes the browser's navigator.mediaDevices.getUserMedia to request microphone access and MediaRecorder to capture audio (typically in formats like webm/opus). Upon stopping, the recorded audio data is encoded into a Base64 Data URL by the JavaScript.
This Data URL is sent back to the Python kernel by setting the .value of the hidden ipywidgets.Text widget.
A Python observer callback (_handle_received_data within the class) detects the change in the Text widget's value.
The Python code decodes the Base64 data.
It uses the ffmpeg-python library to execute the external ffmpeg command, piping the received audio binary data in and piping the converted WAV audio data out. The resulting WAV data (as bytes) is parsed, potentially resampled or type-converted, and loaded into a NumPy array using scipy.io.wavfile and numpy.
The final NumPy array (int16 PCM) is stored and made accessible via the .get_result() method.

License

This project is licensed under the MIT License.

Author

voidful voidful.stack@gmail.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lab_mic-0.2.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lab_mic-0.2.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file lab_mic-0.2.0.tar.gz.

File metadata

  • Download URL: lab_mic-0.2.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for lab_mic-0.2.0.tar.gz
Algorithm Hash digest
SHA256 bfe8ea70654eb47c60ec1294f3b8ff72d6eb0a6155bd2ce2e47ce9e9f03d1d45
MD5 4bf45fd68e8be3f4c42806d7252b1b7b
BLAKE2b-256 bd468acede09238a1bd16fa5c7f18dd57c6d12ac2b1da547719f115fd1f1df51

See more details on using hashes here.

File details

Details for the file lab_mic-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: lab_mic-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for lab_mic-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ffa75fdcb1e19880f8363d082029ef44023f5930bb9d9ed7fb63eca83f751a5c
MD5 80310673a8e2aae207ae654497ab3497
BLAKE2b-256 4935c65dd5c9ae7df60a29d4f9b6d211e5eb310ab2ff43b0892dd09448b22dd4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page