Audiosys Module

sudio.audiosys.cacheman.is_file_locked(file_path)

Check if a file is currently locked or in use by another process.

Supports both Windows and Unix-like systems. On Windows, attempts to open the file exclusively. On Unix systems, uses file locking mechanisms.

Args:

  • file_path (str): Path to the file to check for lock status.

Returns:

:

  • bool: True if the file is locked, False otherwise.

sudio.audiosys.cacheman.write_to_cached_file(*head, head_dtype='u8', data=None, file_name=None, file_mode='wb+', buffered_random=None, pre_truncate=False, pre_seek=None, after_seek=None, pre_flush=False, after_flush=False, size_on_output=False, data_chunk=1000000)

Flexibly write data to a file with granular control over the writing process.

Provides advanced file writing capabilities, allowing precise manipulation of file operations before, during, and after writing data.

Return type:

Union[BufferedRandom, Tuple[BufferedRandom, int]]

Args:

  • *head: Variable header arguments to write at the beginning of the file.

  • head_dtype (str, optional): Numpy data type for header arguments. Defaults to ‘u8’ (64-bit unsigned integer).

  • data (bytes or BufferedRandom, optional): Data to write to the file. Can be bytes or a file-like object.

  • file_name (str, optional): Name of the file to write. Required if no buffered_random is provided.

  • file_mode (str, optional): Mode to open the file. Defaults to ‘wb+’ (write binary, read/write).

  • buffered_random (BufferedRandom, optional): Existing file object to write to. Overrides file_name if provided.

  • pre_truncate (bool, optional): Truncate the file before writing. Defaults to False.

  • pre_seek (tuple, optional): Seek to this position before writing. Tuple of (offset, whence).

  • after_seek (tuple, optional): Seek to this position after writing. Tuple of (offset, whence).

  • pre_flush (bool, optional): Flush the file before writing. Defaults to False.

  • after_flush (bool, optional): Flush the file after writing. Defaults to False.

  • size_on_output (bool, optional): Return total bytes written along with file object. Defaults to False.

  • data_chunk (int, optional): Size of chunks when reading BufferedRandom data. Defaults to 1,000,000 bytes.

Returns:

:

io.BufferedRandom or tuple: - File object if size_on_output is False - Tuple of (file object, total bytes written) if size_on_output is True

Raises:

ValueError: If neither buffered_random nor file_name is provided.

sudio.audiosys.cacheman.handle_cached_record(record, path_server, orphaned_cache, cache_info_size, buffer_type=None, decoder=None, sync_sample_format_id=1, sync_nchannels=2, sync_sample_rate=44100, safe_load=True, max_attempts=5)

Manage audio record caching with advanced synchronization and error recovery.

Handles complex file caching scenarios for audio records, including format conversion, file locking management, and safe loading strategies.

Return type:

AudioMetadata

Args:

  • record (AudioMetadata or dict): Audio record to be processed and cached.

  • path_server (TimedIndexedString): Service for generating unique file paths.

  • orphaned_cache (list): List tracking obsolete cache entries.

  • cache_info_size (str): Size of metadata in the cache.

  • buffer_type (str, optional): Type of buffer for filename parsing.

  • decoder (callable, optional): Custom function to decode audio if needed.

  • sync_sample_format_id (int, optional): Target audio sample format. Defaults to 1.

  • sync_nchannels (int, optional): Target number of audio channels. Defaults to 2.

  • sync_sample_rate (int, optional): Target audio sample rate. Defaults to 44100.

  • safe_load (bool, optional): Enable safe loading with error recovery. Defaults to True.

  • max_attempts (int, optional): Maximum retry attempts for file operations. Defaults to 5.

Returns:

:

  • AudioMetadata: Processed audio record with updated metadata and file reference.

Raises:

IOError: If file cannot be accessed after max attempts. DecodeError: If audio decoding fails and no decoder is provided.

sudio.audiosys.sync.synchronize_audio(rec, nchannels, sample_rate, sample_format_id, output_data='byte')

Synchronizes and transforms audio recording parameters.

Modifies audio recording by adjusting channels, resampling, and converting sample format.

Return type:

dict

Args:

  • rec: Audio recording metadata

  • nchannels: Desired number of audio channels

  • sample_rate: Target sample rate

  • sample_format_id: Desired audio sample format

  • output_data: Output data format (‘byte’ or ‘ndarray’)

Returns:

:

  • Modified audio recording metadata