Overview
SliceManager handles automatic slicing of continuous audio streams into manageable chunks for transcription. It manages memory efficiently by limiting the number of slices kept in memory and automatically releasing old slices. Key Features:- Automatic audio slicing based on duration
- Circular buffer strategy for memory management
- Configurable slice duration and memory limits
- Audio data retrieval for transcription
- Memory usage tracking and statistics
Constructor
Parameters
Duration of each audio slice in seconds
Maximum number of audio slices to keep in memory. Older slices are automatically released when this limit is exceeded.
Audio sample rate in Hz (whisper.cpp requires 16000 Hz)
Methods
addAudioData()
Adds audio data to the current slice. Automatically creates new slices when capacity is reached.Raw audio data to add (16-bit PCM format, 2 bytes per sample)
The current audio slice being built
- Creates a new slice if none exists
- Appends data to the current slice
- Finalizes and creates a new slice when capacity (80% full) is reached
- Cleans up old slices when memory limit is exceeded
getSliceForTranscription()
Returns the next unprocessed slice ready for transcription.The next slice to transcribe, or null if no unprocessed slices are available
getAudioDataForTranscription()
Retrrieves audio data for a specific slice index.Index of the slice to retrieve audio data from
Audio data for the specified slice, or null if slice not found or has no data
getSliceByIndex()
Retrieves a specific slice by its index.Index of the slice to retrieve
The requested slice, or null if not found
markSliceAsProcessed()
Marks a slice as processed after transcription.Index of the slice to mark as processed
moveToNextTranscribeSlice()
Moves the internal transcription pointer to the next slice.forceNextSlice()
Forces finalization of the current slice and moves to the next one, regardless of capacity.The finalized slice, if it contains data
getMemoryUsage()
Returns current memory usage statistics.Number of active slices in memory (not released)
Total number of audio samples across all active slices
Estimated memory usage in megabytes (rounded to 2 decimal places)
getCurrentSliceInfo()
Returns information about the current slice state.Index of the slice currently being built
Index of the next slice to be transcribed
Total number of slices in memory
Current memory usage statistics
reset()
Resets the slice manager, releasing all slices and resetting indices.- Releases all slices (clears audio data)
- Resets slice indices to 0
- Clears the internal slice array
Types
AudioSlice
Unique index of the slice
Raw audio data (16-bit PCM)
Number of bytes in the audio data
Timestamp when slice was created (milliseconds)
Timestamp of last data added to slice (milliseconds)
Whether the slice has been transcribed
Whether the slice has been released from memory
MemoryUsage
Number of active slices
Total audio samples
Estimated memory in MB
Example Usage
Memory Management Strategy
SliceManager uses a circular buffer approach:- Slice Creation: New slices are created when audio data is added
- Auto-Finalization: Slices are finalized when they reach 80% capacity
- Memory Limit: When
maxSlicesInMemoryis exceeded, oldest slices are released - Data Cleanup: Released slices have their audio data cleared to free memory
- Slice Retention: Only the most recent
maxSlicesInMemoryslices are kept
- Predictable memory usage
- No memory leaks from unbounded audio buffers
- Access to recent audio history for context
- Automatic garbage collection of old data