1. Go to this page and download the library: Download phpmlkit/soundfile library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
phpmlkit / soundfile example snippets
use function PhpMlKit\SoundFile\{sf_read, sf_write, sf_info};
// Read a file — returns [NDArray, SfInfo]
[$audio, $info] = sf_read('input.wav');
// $audio shape: [441000] (mono, Float32)
// Write it back (format and subtype auto-detected from extension)
sf_write('output.wav', $audio, $info->sampleRate);
// Probe signal properties without loading data
$info = sf_info('input.wav');
echo "{$info->frames} frames, {$info->channels} channels, {$info->duration()}s";
use function PhpMlKit\SoundFile\sf_resample;
use PhpMlKit\SoundFile\Enums\ResampleQuality;
// Chunked progressive — safe for large files (default)
$resampled = sf_resample($audio, inputRate: 44100, outputRate: 22050);
// One-shot simple — best for small signals
$resampled = sf_resample($audio, 44100, 16000, chunkSize: null);
// Best quality, explicit chunk size
$resampled = sf_resample(
$audio, 44100, 8000,
quality: ResampleQuality::Best,
chunkSize: 4096,
);
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);
echo $sf->tell(); // 44612
// Iterate in blocks
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->setArtist('My Artist');
$out->write($stereoData);
$out->close();
function sf_read(
string $file,
?int $start = null, // First frame (0-based; null = beginning)
?int $stop = null, // One past last frame (null = EOF)
bool $always2d = false, // If true, mono returns [frames, 1] instead of [frames]
int $blocksize = 4096, // Chunk size for internal read loop
): array // [NDArray, SfInfo]