🎵
Read & Write Audio
WAV, FLAC, Ogg Vorbis, MP3, AIFF, and 20+ formats. Data flows in and out as NDArrays — no intermediate buffers.
use function PhpMlKit\SoundFile\{sf_read, sf_write, sf_resample};
// Read an audio file — returns [NDArray, SfInfo]
[$audio, $info] = sf_read('input.wav');
// $audio shape: [441000] (mono, Float32)
// Resample from 44.1kHz to 16kHz
$audio16k = sf_resample($audio, $sr, 16000);
// Write it back
sf_write('output.wav', $audio16k, 22050);use PhpMlKit\SoundFile\SoundFile;
use PhpMlKit\SoundFile\Enums\FileMode;
// Open for reading
$sf = new SoundFile('input.wav', FileMode::Read);
// Seek and read
$sf->seek(44100);
$chunk = $sf->read(512);
// Iterate in blocks — each block is an NDArray
foreach ($sf->blocks(1024) as $block) {
process($block);
}
$sf->close();
// Open for writing
$out = new SoundFile('output.wav', FileMode::Write,
sampleRate: 44100, channels: 2,
);
$out->setTitle('My Track');
$out->write($stereoData);
$out->close();