Overview
TheAudioProcessor class handles audio processing and OpenAI API interactions with rate limiting and connection tracing.
Location: backend/src/audio_processor.py:10-72
Class Definition
Constructor
Parameters
OpenAI API key for authentication. The key is automatically stripped of whitespace.
Maximum number of concurrent OpenAI API calls. Additional requests are queued using an asyncio Semaphore.
Example
Methods
call_openai_api
audio_processor.py:19-47
Parameters
Raw audio data in MP4 format to be transcribed
8-character unique identifier for request tracing in logs
Returns
Returns an OpenAI transcription response object with:language: Detected language code (ISO 639-1)- Additional fields from
verbose_jsonformat
Implementation Details
- Acquires semaphore slot (logs acquisition)
- Wraps audio data in
BytesIObuffer with filename"audio.mp4" - Calls OpenAI API using
asyncio.to_threadfor non-blocking execution - Uses
whisper-1model withverbose_jsonresponse format - Logs API call duration
- Releases semaphore in finally block
Example
Error Handling
Raises exceptions from OpenAI API calls. Logs all errors with connection ID for tracing.process_audio
audio_processor.py:49-72
Parameters
Raw audio data in MP4 format to be processed
ServerMetrics instance for tracking processing times and errors
8-character unique identifier for request tracing
Returns
Returns a dictionary with processing results: Success response:ISO 639-1 language code detected by Whisper
Confidence score (currently fixed at 0.9)
Total processing time in seconds
Connection identifier for tracing
Error message describing what went wrong
Connection identifier for tracing
Implementation Details
- Records start time
- Calls
call_openai_api()to get transcription - Calculates total processing time
- Appends processing time to metrics deque
- Returns structured response with language and timing info
- On error: increments metrics error counter and returns error dict
- Calls
gc.collect()in finally block for memory cleanup
Example
Dependencies
Rate Limiting
The class implements rate limiting usingasyncio.Semaphore:
- Default: Maximum 3 concurrent API calls
- Additional requests queue until a slot becomes available
- Prevents overwhelming the OpenAI API
- Logs semaphore acquisition and release
Connection Tracing
All methods accept aconnection_id parameter used for:
- Logging request flow through the system
- Correlating logs from different components
- Debugging specific requests
- Monitoring individual connection performance
[connection_id] Message
