Skip to content

Global Functions

All global functions live in the PhpMlKit\SoundFile namespace and are importable with use function.

Importing

php
use function PhpMlKit\SoundFile\sf_read;
use function PhpMlKit\SoundFile\sf_write;
use function PhpMlKit\SoundFile\{sf_read, sf_write, sf_resample};

sf_read()

Read an audio file into a single NDArray. Opens the file, reads data in chunks via a single pre-allocated buffer, and closes automatically.

php
function sf_read(
    string $file,
    ?int $start = null,
    ?int $stop = null,
    DType $dtype = DType::Float32,
    bool $always2d = false,
    int $blocksize = 4096,
): array // [NDArray, SfInfo]

Parameters:

ParameterTypeDescription
$filestringPath to the audio file
$start?intFirst frame index to read (0-based). null = beginning.
$stop?intOne past the last frame index. null = end of file. Clipped to file length.
$dtypeDTypeDesired dtype of the returned array. One of Float32 (default), Float64, Int16, Int32.
$always2dboolIf true, mono files return [N, 1] instead of [N]. Default false.
$blocksizeintFrames per internal read chunk. Affects memory usage during read, not the result.

Returns: [NDArray, SfInfo] — the signal data (default Float32, normalized to [-1.0, 1.0] for integer files) and the file's full metadata.

Throws: SoundFileException if the file cannot be opened, an unsupported dtype is given, or a read error occurs.

Examples:

php
// Read entire file (default Float32)
[$audio, $info] = sf_read('song.wav');

// Read as raw 16-bit integers
[$audio, $info] = sf_read('song.wav', dtype: DType::Int16);

// Read frames 100–299 (200 frames starting at frame 100)
[$slice, $info] = sf_read('song.wav', start: 100, stop: 300);

// Read mono file as 2D
[$audio, $info] = sf_read('song.wav', always2d: true);
// Shape: [frames, 1]

sf_write()

Write an NDArray to an audio file. Format is inferred from the file extension when not specified. When the encoding subtype is omitted, it is resolved by consulting both the AudioFormat's compatible subtypes and the data's DType. The DType is auto-converted to match the chosen subtype.

php
function sf_write(
    string $file,
    NDArray $data,
    int $sampleRate,
    ?AudioFormat $format = null,
    ?SampleFormat $subtype = null,
): void

Parameters:

ParameterTypeDescription
$filestringOutput file path
$dataNDArraySignal data, [N] or [N, channels]. 1D is auto-expanded to [N, 1].
$sampleRateintSample rate in Hz
$format?AudioFormatContainer format. null = inferred from extension.
$subtype?SampleFormatEncoding subtype. null = resolved from the format's compatible subtypes and the data's DType.

Throws: SoundFileException if the format/subtype combination is invalid or a write error occurs.

Examples:

php
// Subtype resolved from format and data dtype — Float32 in WAV produces Float encoding
sf_write('output.wav', $audio, sampleRate: 44100);

// Explicit format and subtype
use PhpMlKit\SoundFile\Enums\AudioFormat;
use PhpMlKit\SoundFile\Enums\SampleFormat;

sf_write('output.flac', $audio, 44100,
    format: AudioFormat::Flac,
    subtype: SampleFormat::Pcm24,
);

// 1D input works
$mono = NDArray::array([0.1, 0.2, 0.3], DType::Float32);
sf_write('out.wav', $mono, 8000); // Auto-expands to [3, 1]

sf_info()

Read signal properties from an audio file without loading its audio data. Returns frames, channels, sample rate, format, and seekability. For embedded string tags, use sf_metadata() instead.

php
function sf_info(string $file): SfInfo

Returns: SfInfo — all technical fields populated from the file header.

Throws: SoundFileException if the file cannot be opened.

Example:

php
$info = sf_info('song.wav');
echo "{$info->frames} frames, {$info->channels} channels, {$info->duration()}s";

sf_metadata()

Read embedded string tags from an audio file without loading its audio data. Each tag is null if not present in the file.

php
function sf_metadata(string $file): SfMetadata

Returns: SfMetadata — all fields populated from the file's tags. Null for any tag not present.

Throws: SoundFileException if the file cannot be opened.

Example:

php
$meta = sf_metadata('song.wav');
echo $meta->artist;
echo $meta->album;

sf_check_format()

Validate that an AudioFormat and SampleFormat combination is supported by libsndfile.

php
function sf_check_format(AudioFormat $format, SampleFormat $subtype): bool

Parameters:

ParameterTypeDescription
$formatAudioFormatContainer format
$subtypeSampleFormatEncoding subtype

Returns: booltrue if the combination is valid.

Examples:

php
sf_check_format(AudioFormat::Wav, SampleFormat::Pcm16);   // true
sf_check_format(AudioFormat::Ogg, SampleFormat::Pcm16);   // false
sf_check_format(AudioFormat::Flac, SampleFormat::Float);  // false

sf_resample()

Convert an NDArray from one sample rate to another using libsamplerate.

php
function sf_resample(
    NDArray $input,
    int $inputRate,
    int $outputRate,
    ResampleQuality $quality = ResampleQuality::Best,
    ?int $chunkSize = 2048,
): NDArray

Parameters:

ParameterTypeDescription
$inputNDArraySignal of shape [frames, channels]
$inputRateintSource sample rate in Hz
$outputRateintTarget sample rate in Hz
$qualityResampleQualityConverter quality level. Default Best.
$chunkSize?intFrames per processing chunk. null = one-shot simple mode. Default 2048.

Returns: NDArray of shape [newFrames, channels] with DType::Float32.

Throws: SoundFileException if the resampler fails or produces zero output.

Examples:

php
// Chunked progressive (default) — safe for large files
$r = sf_resample($audio, 44100, 22050);

// One-shot simple — best for small signals
$r = sf_resample($audio, 44100, 8000, chunkSize: null);

// Explicit quality and chunk size
$r = sf_resample($audio, 44100, 48000,
    quality: ResampleQuality::Fastest,
    chunkSize: 1024,
);

Released under the MIT License.