Mar 14 2008

Converting WAV file to GSM file

Category: PHPBipin Balakrishnan @ 3:21 pm

soundvisualizer.png

Want to convert a sound file of type wav to gsm in your web application.You can use the code below.There are may desktop application softwares such as

switch,audacity which converts the sound format.Suppose we need in a web application in which we uploads the wav file and and it got converted into a gsm or any other format(depends on our need).

For me i need the wav file in gsm format as the output for some IVR application.

Before writing the code we need a opensource software called sox

to be installed in our system.Sox is a sound converter which converts sound from any format to any other sound format.

The rest is simple

copy and paste the code below

Uploading interface: HTML

Give some file name with extension Html or PHP

<form enctype=”multipart/form-data” action=”uploader.php” method=”POST”>
<input type=”hidden” name=”MAX_FILE_SIZE” value=”10000000″ />
Choose a file to upload: <input name=”uploadedfile” type=”file” /><br />
<input type=”submit” value=”Upload File” />
</form>

Conversion program :

<?php
$target_path = “uploads/”;
$file_name=basename( $_FILES['uploadedfile']['name']);
$new = substr($file_name,0,-3).”gsm”;
$cmd = “sox “.$file_name.” -r 8000 -c1 “.$new.” resample -ql”;
exec($cmd);
rename(“$new”, “uploads/$new”);
?>