Jump to content


Generating .wav files using php

  • Please log in to reply
1 reply to this topic

#1 ultrus

  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 16 October 2006 - 04:44 AM

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.tech...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...avedit.html.php (even comes with a wave form image maker!). More info on the actual .wav format can be seen at
http://ccrma.stanfor...cts/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,

Twitter | Blog | Worlds to explore. Worlds to create.

#2 ultrus

  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 18 October 2006 - 09:56 PM

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.
Twitter | Blog | Worlds to explore. Worlds to create.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users