Jump to content


This topic is now archived and is closed to further replies.


Generating .wav files using php

Recommended Posts

PHP amazed me yet again with the ability to generate .wav files. One can take two or more .wav files and merge them together as seen at http://articles.techrepublic.com.com/5100-3513_11-5810215.html . It is also possible to apply effects to audio files like echo, fade, and crop like seen in the great php class at http://www.pviet.com/wavedit/test_wavedit.html.php (even comes with a wave form image maker!). More info on the actual .wav format can be seen at
http://ccrma.stanford.edu/CCRMA/Courses/422/projects/WaveFormat/ (still a bit greek to me)

I want to expand on this in my own fun projects. Using the second example from above, how can I generate my own audio files from scratch? It would be cool to generate raw audio data arrays using mathmatic equations, and create a file that will play in an audio player. Here is what I have added to the class so far with further questions  commented:

function makeFile($dataArray) {
//set name
$this->afiles[0]['name'] = "scratchFile.wav";
$this->wavs['ChunkID'] = "RIFF";
$this->wavs['ChunkSize'] = 83140; //how do I set this? I just pulled this number from an existing file. How do I  get this for one that does not exsist yet?
$this->wavs['Format'] = "WAVE";
$this->wavs['Subchunk1ID'] = "fmt";
$this->wavs['SubChunk1Size'] = 16;
$this->wavs['AudioFormat'] = 1;
$this->wavs['NumChannels'] = 1;
$this->wavs['SampleRate']  = 16000;
$this->wavs['ByteRate'] = 32000;
$this->wavs['BlockAlign'] = 2;
$this->wavs['BitsPerSample'] = 16;

$this->wavs['Subchunk2ID'] = "data";
$this->wavs['SubChunk2Size'] = 83104; //how do I set this?  This is an example from an exsisting file.
//$this->wavs['Data']= ""; //how? is a string, from data above? Can I skip this? assuming yes for now

$this->wavs['NumberSamples']= ($this->wavs['SubChunk2Size']*8)/($this->wavs['NumChannels']*$this->wavs['BitsPerSample']);

                $this->afiles[0]['structure'] = $this->wavs;
$this->afiles[0]['arrdata']= $dataArray;

This is a bit of an odd request compared to what I normally experiment with. Any feedback from the smart or inspired people out there would be greatly appriciated. :)

Thank you,


Share this post

Link to post
Share on other sites
Answered my own question. :)

A great class that generates .wav files from scratch can be found at:

It currently only generates sine waves, but it's a great start! Hope someone else enjoys this.

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.