user993683
user993683

Reputation:

Create a .wav file of a certain frequency with PHP

I'm a rookie with this stuff so go easy...

All I want to do is construct a simple tone of a certain frequency using PHP. It should output a .wav file which can be inserted straight into a HTML audio tag.

I really have no idea where to begin as I don't even understand how audio data is stored. Can anyone suggest a resource or help me out directly?

It would be muchly appreciated :)

P.S there is one similar question, though I don't think it's what I'm looking for.

Upvotes: 5

Views: 2669

Answers (1)

chrislondon
chrislondon

Reputation: 12031

Try this. (It uses SoX so it's not a pure PHP solution but it works well.

Your HTML

<embed height="50" width="100" src="/test.php?tone=1000">

Your PHP

<?php
    $tempFile = tempnam(sys_get_temp_dir(), 'AUD') . '.wav';
    $pitch    = (int)$_GET['tone'];

    exec("sox -n $tempFile synth 5 sin $pitch");

    header('Cache-Control: max-age=604800');
    header('Content-type: audio/x-wav');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.filesize($tempFile));
    readfile($tempFile);

Upvotes: 1

Related Questions