def get_audio_file_bytes()

in libs/libcommon/src/libcommon/viewer_utils/features.py [0:0]


def get_audio_file_bytes(value: Any) -> bytes:
    if "bytes" in value and isinstance(value["bytes"], bytes):
        audio_file_bytes = value["bytes"]
    elif "path" in value and isinstance(value["path"], str) and os.path.exists(value["path"]):
        with open(value["path"], "rb") as f:
            audio_file_bytes = f.read()
    elif (
        "array" in value
        and isinstance(value["array"], np.ndarray)
        and "sampling_rate" in value
        and isinstance(value["sampling_rate"], int)
    ):
        buffer = BytesIO()
        soundfile.write(buffer, value["array"], value["sampling_rate"], format="wav")
        audio_file_bytes = buffer.getvalue()
    else:
        raise ValueError(
            "An audio sample should have 'path' and 'bytes' (or 'array' and 'sampling_rate') but got"
            f" {', '.join(value)}."
        )
    return audio_file_bytes