A Python package for real-time audio streaming in memory for flexible use cases
Project description
StreamAI
StreamAI is a Python package designed for real-time audio streaming directly from the microphone. The audio is stored in memory, and the package provides a flexible way to handle or send the audio data to external applications or services in real time.
Features
- Stream audio from the microphone directly into memory (without saving to disk).
- Handle audio data with a callback function to process it as needed (e.g., send to an API, analyze, etc.).
- Ideal for real-time applications such as voice-controlled systems or audio analysis.
Installation
You can install StreamAI using pip:
pip install StreamAudio
Example
import pyaudio
import io
import threading
import time
import speech_recognition as sr
# Audio settings for streaming
FORMAT = pyaudio.paInt16 # Audio format: 16-bit resolution
CHANNELS = 1 # Mono channel
RATE = 16000 # Sample rate: 16kHz (common for speech)
CHUNK = 1024 # Buffer size per audio read
RECORD_SECONDS = 5 # Record only 5 seconds of audio
# Function to stream audio for a limited time and store it in memory
def stream_audio_to_memory(callback=None):
"""
Streams audio from the microphone for 5 seconds, stores it in a BytesIO buffer in memory,
and optionally passes the audio buffer to a callback function for processing.
:param callback: Optional function to handle the in-memory audio data.
"""
# Initialize PyAudio for audio streaming
audio = pyaudio.PyAudio()
stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)
# BytesIO stream to hold audio data temporarily in memory
audio_buffer = io.BytesIO()
print("Recording audio for 5 seconds...")
# Record for 5 seconds
start_time = time.time()
while time.time() - start_time < RECORD_SECONDS:
# Read audio data from the stream
data = stream.read(CHUNK)
audio_buffer.write(data)
print("Recording complete.")
# Clean up the stream and PyAudio instance
stream.stop_stream()
stream.close()
audio.terminate()
# Reset buffer pointer to the beginning
audio_buffer.seek(0)
# Show all recorded bytes
print(f"Recorded bytes: {audio_buffer.getvalue()}")
# If a callback function is provided, send the audio buffer for processing
if callback:
callback(audio_buffer)
else:
return audio_buffer
# Function to transcribe audio and print the result
def transcribe_audio(audio_buffer):
"""
Transcribes audio from a BytesIO buffer using SpeechRecognition.
:param audio_buffer: In-memory audio buffer to be transcribed.
"""
# Initialize the recognizer
recognizer = sr.Recognizer()
# Convert the BytesIO buffer into AudioData that speech_recognition can process
audio_buffer.seek(0) # Ensure we start reading from the beginning of the buffer
audio_data = sr.AudioData(audio_buffer.read(), RATE, 2)
try:
# Perform the transcription using Google's speech recognition service
text = recognizer.recognize_google(audio_data)
print("Transcription:", text)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print(f"Could not request results from Google Speech Recognition service; {e}")
# Function to start recording and processing audio
def start_audio_streaming():
"""
Starts streaming audio from the microphone for 5 seconds,
sends the recorded audio to be transcribed, and prints the bytes and transcription.
"""
# Start streaming audio and transcribe after recording
audio_buffer = stream_audio_to_memory(transcribe_audio)
# Start the recording and transcription
start_audio_streaming()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file streamaudio-0.1.4.tar.gz.
File metadata
- Download URL: streamaudio-0.1.4.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a51b04115da1dc3d350251219b962f454d4af68adc834074156b34c767010c2
|
|
| MD5 |
971cf9f18b6d055bca18943f3b753f22
|
|
| BLAKE2b-256 |
e7424fcce61fa31b97285614e7ae2633a0dab59700cc3fd5d67c735311a13074
|
File details
Details for the file StreamAudio-0.1.4-py3-none-any.whl.
File metadata
- Download URL: StreamAudio-0.1.4-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a036dbdadbad001fce3ac557811f48c3db5e2059353e1954ada4b8b77cd7156
|
|
| MD5 |
8a781ea7bc423e33c80fac5ff28cad47
|
|
| BLAKE2b-256 |
630abd48f222ab35ab17736114730436d9740cd7cefdcb00de59cbb7c3c692ab
|