NxAudio.IO behaviour (nx_audio v0.3.1)
View SourceDefines a behaviour for audio backend operations
Summary
Types
A 2D tensor {channels, samples} representing audio data.
Defines the input for the load function, which can be a binary audio or a valid file path
Defines the possible errors that can occur when loading an audio file
Audio sample rate in Hz
Callbacks
Returns the audio metadata for a given file.
Loads an audio file from a given URI and returns a tuple with the audio tensor and the sample rate.
Saves an audio tensor to a given URI.
Streams an audio file from a given URI and returns an enumerable with the audio tensor and the sample rate.
Functions
Retrieves metadata information about an audio file at the specified URI.
Loads an audio file from the given URI using the configured backend.
Saves an audio tensor to a file at the specified URI using the configured backend.
Streams an audio file from the given URI using the configured backend.
Types
@type audio_tensor() :: Nx.Tensor.t()
A 2D tensor {channels, samples} representing audio data.
Defines the input for the load function, which can be a binary audio or a valid file path
@type io_errors() :: NxAudio.IO.Errors.FailedToExecuteBackend.t() | NxAudio.IO.Errors.InvalidMetadata.t()
Defines the possible errors that can occur when loading an audio file
@type sample_rate() :: non_neg_integer()
Audio sample rate in Hz
Callbacks
@callback info(uri :: file_uri()) :: {:ok, NxAudio.IO.AudioMetadata.t()} | {:error, io_errors()}
Returns the audio metadata for a given file.
@callback load(uri :: file_uri(), config :: NxAudio.IO.BackendReadConfig.t()) :: {:ok, {audio_tensor(), sample_rate()}} | {:error, io_errors()}
Loads an audio file from a given URI and returns a tuple with the audio tensor and the sample rate.
@callback save( uri :: file_uri(), tensor :: audio_tensor(), config :: NxAudio.IO.BackendSaveConfig.t() ) :: :ok | {:error, io_errors()}
Saves an audio tensor to a given URI.
@callback stream!(uri :: file_uri(), config :: NxAudio.IO.BackendReadConfig.t()) :: Enumerable.t()
Streams an audio file from a given URI and returns an enumerable with the audio tensor and the sample rate.
Functions
@spec info(file_uri(), NxAudio.IO.BackendReadConfig.t()) :: {:ok, NxAudio.IO.AudioMetadata.t()} | {:error, io_errors()}
Retrieves metadata information about an audio file at the specified URI.
Parameters
uri
- The file path of the audio file to analyzeconfig
- The configuration for reading the audio file (seeNxAudio.IO.BackendReadConfig
)
Returns
{:ok, metadata}
- On success, returns the audio metadata (seeNxAudio.IO.AudioMetadata
){:error, error}
- On failure, returns an error struct
@spec load(file_uri(), NxAudio.IO.BackendReadConfig.t()) :: {:ok, {audio_tensor(), sample_rate()}} | {:error, io_errors()}
Loads an audio file from the given URI using the configured backend.
Parameters
uri
- The file path or binary audio data to loadconfig
- The configuration for reading the audio file (seeNxAudio.IO.BackendReadConfig
)
Returns
{:ok, {tensor, sample_rate}}
- On success, returns a tuple with the audio tensor and sample rate{:error, error}
- On failure, returns an error struct
@spec save(file_uri(), audio_tensor(), NxAudio.IO.BackendSaveConfig.t()) :: :ok | {:error, io_errors()}
Saves an audio tensor to a file at the specified URI using the configured backend.
Parameters
uri
- The target file path where the audio will be savedtensor
- The audio tensor to save (2D tensor with channels x samples)config
- The configuration for saving the audio file (seeNxAudio.IO.BackendSaveConfig
)
Returns
:ok
- On successful save{:error, error}
- On failure, returns an error struct
@spec stream!(file_uri(), NxAudio.IO.BackendReadConfig.t()) :: Enumerable.t()
Streams an audio file from the given URI using the configured backend.
This function returns an enumerable that can be used to process the audio data in chunks, which is useful for handling large audio files without loading them entirely into memory.
Parameters
uri
- The file path or binary audio data to streamconfig
- The configuration for reading the audio file (seeNxAudio.IO.BackendReadConfig
)
Returns
- An
Enumerable.t()
that yields audio chunks
Raises
- Raises an error if the streaming operation fails