utils Module

sudio.utils.channel.shuffle3d_channels(arr)

Shuffles the channels of a 3D array and returns a flattened result.

Parameters: - arr (numpy.ndarray): Input 3D array of shape (frames, channels, samples_per_frame)

Returns: - numpy.ndarray: Flattened array with interleaved channels.

sudio.utils.channel.shuffle2d_channels(arr)

Shuffles the channels of a 2D array and returns a flattened result.

Parameters: - arr (numpy.ndarray): Input 2D array of shape (m, n), where m and n are dimensions.

Returns: - numpy.ndarray: Flattened array with shuffled channels.

sudio.utils.channel.get_mute_mode_data(nchannel, nperseg)
sudio.utils.channel.map_channels(in_data, in_channels, out_channels)

Map input audio channels to desired output channels.

Args: in_data (np.ndarray): Input audio data. in_channels (int): Number of input channels. out_channels (int): Number of desired output channels. data_chunk (int): Size of data chunk for processing.

Returns: np.ndarray: Processed audio data with desired number of channels.

sudio.utils.io.get_encoded_filename_bytes(filepath)

Encode the given file path string into bytes using the system’s filesystem encoding.

Parameters: - filepath (str): The input file path.

Returns: - bytes: The encoded file path as bytes.

Raises: - FileNotFoundError: If the specified file does not exist.

Return type:

bytes

sudio.utils.strtool.convert_string_to_python_type(st)

Converts the input string ‘st’ to a more appropriate Python data type based on its content.

Parameters:

st (str) – The input string to be converted.

Returns:

The converted value. If ‘st’ represents a numeric value, it is converted to an integer or float. If ‘st’ contains curly braces, it is converted to a dictionary. If ‘st’ contains square brackets, it is converted to a list. Otherwise, the original string is returned.

Return type:

Union[int, float, dict, list, str]

Example

>>> convert_string_to_python_type("42")
42
>>> convert_string_to_python_type("3.14")
3.14
>>> convert_string_to_python_type("{ 'key': 'value' }")
{'key': 'value'}
>>> convert_string_to_python_type("[1, 2, 3]")
[1, 2, 3]
>>> convert_string_to_python_type("Hello World")
'Hello World'
sudio.utils.strtool.parse_dictionary_string(st, dict_eq=':', item_sep=',')

Converts a string ‘st’ representing a dictionary-like structure to a Python dictionary.

Parameters:
  • st (str) – The input string representing the dictionary.

  • dict_eq (str, optional) – The string used as the key-value separator. Defaults to ‘:’.

  • item_sep (str, optional) – The string used as the item separator. Defaults to ‘,’.

Returns:

The converted dictionary.

Return type:

dict

Example

>>> parse_dictionary_string("{key1:value1, key2:value2}")
{'key1': 'value1', 'key2': 'value2'}
>>> parse_dictionary_string("name:John, age:30, city:New York", dict_eq=':', item_sep=', ')
{'name': 'John', 'age': 30, 'city': 'New York'}
>>> parse_dictionary_string("a=1; b=2; c=3", dict_eq='=', item_sep='; ')
{'a': 1, 'b': 2, 'c': 3}
sudio.utils.strtool.parse_list_string(st)

Converts a string ‘st’ representing a list-like structure to a Python list.

Parameters:

st (str) – The input string representing the list.

Returns:

The converted list.

Return type:

list

Example

>>> parse_list_string("[1, 2, 3, 4, 5]")
[1, 2, 3, 4, 5]
>>> parse_list_string("apple, orange, banana, mango")
['apple', 'orange', 'banana', 'mango']
>>> parse_list_string("3.14, 2.71, 1.618", convert_string_to_python_type=float)
[3.14, 2.71, 1.618]
sudio.utils.strtool.generate_timestamp_name(seed=None)

Generates a timestamp-based name.

Parameters:

seed (str, optional) – Seed value for additional uniqueness. Defaults to None.

Returns:

The generated timestamp-based name.

Return type:

str

Example

>>> generate_timestamp_name()
'YYYYMMDD_SSMMM'
>>> generate_timestamp_name("custom_seed")
'custom_seedDD_SSMMM'

Note

  • ‘YYYY’ represents the year.

  • ‘MM’ represents the month.

  • ‘DD’ represents the day.

  • ‘SS’ represents the second.

  • ‘MMM’ represents the millisecond.

  • The seed, if provided, is appended to the beginning of the name.

class sudio.utils.timed_indexed_string.TimedIndexedString(string, start_from=None, first_index=0, start_before=None, max_index=None, seed='')

Bases: object

A class for generating indexed strings with optional time-based suffixes.

Parameters:
  • base_string (str) – The initial string.

  • start_from (int, optional) – Starting index. Defaults to None.

  • first_index (int, optional) – Initial index. Defaults to 0.

  • start_before (str, optional) – Character before which the index should start. Defaults to None.

  • max_index (int, optional) – Maximum index. Defaults to None.

  • seed (str, optional) – Seed for additional uniqueness. Defaults to ‘’.

reset()

Resets the index counter to its initial value.

get()

Gets the current indexed string.

Returns:

The current indexed string.

Return type:

str

sudio.utils.typeconversion.convert_array_type(arr, target_format, source_format=sudio.io.SampleFormat.UNKNOWN)

Convert the data type of a NumPy array based on the given SampleFormat.

Args: arr (np.ndarray): Input NumPy array target_format (SampleFormat): Desired output format

Returns: np.ndarray: Converted NumPy array

sudio.utils.window.win_parser_mono(window, win_num)
sudio.utils.window.win_parser(window, win_num, nchannel)
sudio.utils.window.single_channel_windowing(data, windowing_buffer, window, nhop)
sudio.utils.window.multi_channel_windowing(data, windowing_buffer, window, nhop, nchannels)
sudio.utils.window.single_channel_overlap(data, overlap_buffer, nhop)
sudio.utils.window.multi_channel_overlap(data, overlap_buffer, nhop, nchannels)