Page 1 of 1

php function to convert recording to .wav file

Posted: Sat Dec 08, 2007 7:25 am
by jbrohan
This php function will convert a recording into a ulaw compressed wave file that is playable on any application. Be sure to have magic-quotes off while the function is running.

Improvements and comments to jbrohan (at) tradersmicro.com

(c) Traders Micro. you may freely use this code for any purpose. No guarantees given or implied.




function Store_to_wave ( $infilename, $wavefilename ){
$a = file_get_contents ( $infilename );
$b = "RIFF";
$n = strlen ( $a ) + 44;
$b .= pack ( 'V', $n );
$b .= "WAVEfmt "; // 8
$b .= pack ( 'V', 16 ); // length of format block 4
$b .= pack ( 'v', 7 ); // ulaw encoding 2
$b .= pack ( 'v', 1 ); // channel numbers 2
$b .= pack ( 'V', 8000 ); // sample rate Hz 4
$b .= pack ( 'V', 8000 ); // bytes / sec 4
$b .= pack ( 'v', 1 ); // bytes / sample 2
$b .= pack ( 'v', 8 ); // bits / sample 2
$b .= "data"; // 4
$b .= pack ( 'V', strlen ( $a )); // 4
$b .= $a;
$f = fopen ( $wavefilename, "wb" );
fwrite ( $f, $b, strlen ( $b ));
fclose ( $f );
}