Page 1 of 1

Writing sound files

Posted: Wed Feb 21, 2007 3:18 pm
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]

Posted: Wed Feb 21, 2007 3:59 pm
by feyd
Strings are binary-safe in PHP. They are natively bytes.

Posted: Wed Feb 21, 2007 10:22 pm
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.

Posted: Thu Feb 22, 2007 4:23 am
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.

Posted: Thu Feb 22, 2007 9:28 am
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

Posted: Thu Feb 22, 2007 9:38 am
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.