Writing sound files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nsfx
Forum Newbie
Posts: 2
Joined: Tue Feb 20, 2007 1:53 pm

Writing sound files

Post by nsfx »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am a student scientist working on animated holograms controlled with stereo sound (ok, it's not that amazing) and want to use php to generate wave files.  I know how to read/write files in text format, but how do I write plain data in an encoded format?  I'm familiar with the WAV format - just need to figure out how to write the data in bytes, not stings.
I promise any code I develop with help from this forum will be publicly available.

General Idea:

Code: Select all

<?php

function soundDataL($frame){
  //function to output
  return $value;
}

function soundDataR($frame){
  //function to output
  return $value;
}

function writeSound($file, $header, $maxFrames){
  $snd=fopen($file, "x");
  fwrite($snd,$header);

  for($i=0; $i<$maxFrames; $i++){
    $right=soundDataR($i);
    $left=soundDataL($i);
    
    //write this data as data, not text
    
  }
  fclose($snd);
}

?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Strings are binary-safe in PHP. They are natively bytes.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

You'll probably need to use PCM WAV files, which could get large!

Have you considered midi as an alternative?

Wait, there is an ogg vorbis extension you can install... it seems to be an encoder/decoder wrapper that gives you access to the raw PCM WAV. Could save file space.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I've absolutely no idea about writing WAV files, but a while ago I wrote a script to analyse them and draw the waveform:

http://www.ooer.com/wavgrabber/

If you want to download it and mess around with it...

Save the source to a directory: http://www.ooer.com/wavgrabber/index.phps

There's a constant (ALLOW_UPLOADS) that you can set to 1 to put a little form in so you can upload wavs. That's switched off on the online version for reasons of saving space.

You'll need to create a couple of subdirectories:
/wavs (needs to be writable if you enable uploads.)
/images (needs to be writable so the script can cache waveform pngs.)

You'll need to download the following files too:

http://www.ooer.com/wavgrabber/wavs/ding.wav
http://www.ooer.com/wavgrabber/images/w ... ground.png

It only works with 8 bit uncompressed files. Oh, and the script requires GD.
nsfx
Forum Newbie
Posts: 2
Joined: Tue Feb 20, 2007 1:53 pm

Post by nsfx »

Thanks feyd for correcting me on the style. :oops:

These would be very small wav files - not music or anything. The length would be less than half a second so size is not an issue.

onion2k your code had at least some of what I was looking for:

Code: Select all

hexdec(bin2hex(strrev(substr($fileinfo,8,2))));
I'll try to work backwards and write the file instead of reading it.

THanks
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

You'll need to refer to http://ccrma.stanford.edu/courses/422/p ... aveFormat/ a lot. I did when I wrote that code.
Post Reply