View Source NxAudio.IO behaviour (nx_audio v0.1.0)

Defines a behaviour for audio backend operations

Summary

Types

A 2D tensor representing audio data. By default, the first dimension is the amount of channels and the second dimension is the amount of samples.

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.

Types

audio_tensor()

@type audio_tensor() :: Nx.Tensor.t()

A 2D tensor representing audio data. By default, the first dimension is the amount of channels and the second dimension is the amount of samples.

file_uri()

@type file_uri() :: binary() | Path.t()

Defines the input for the load function, which can be a binary audio or a valid file path

io_errors()

Defines the possible errors that can occur when loading an audio file

sample_rate()

@type sample_rate() :: non_neg_integer()

Audio sample rate in Hz

Callbacks

info(uri)

@callback info(uri :: file_uri()) ::
  {:ok, NxAudio.IO.AudioMetadata.t()} | {:error, io_errors()}

Returns the audio metadata for a given file.

load(uri, config)

@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.

save(uri, tensor, config)

@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.