Forum Moderators: coopster

Message Too Old, No Replies

Merging sound files

         

DrDoc

5:26 pm on Jan 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is it possible to merge sound files with PHP? I don't need to otherwise be able to edit the files, just merge a number of sounds.

jatar_k

6:59 pm on Jan 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hm, interesting, I never really thought about it before. Did you try it?

maybe specify the binary flag, it may work but I am really not sure. (I know, big help ;))

DrDoc

7:01 pm on Jan 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Haven't had a chance to try it yet. I've been finishing up recording the sounds.

They are all very short clips.

DrDoc

7:26 pm on Jan 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm, just wrote a quick Perl script that merged two WAV files. It will play the first sound, but not the second sound. That was just a raw merge of two binary files.

Anyone have an idea what I can do? Seems like a plain merge won't work.

jatar_k

8:20 pm on Jan 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I was wondering about that, you may have to grab some software to do it. I am not familiar enough with how sound files are terminated to make any assumptions.

jatar_k

8:31 pm on Jan 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



this search looks promising
[google.com...]

httpwebwitch

8:49 pm on Jan 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you just want to merge sounds permanently, do it with a sound editing program. But since you've posted this to the PHP scripting forum, I assume you want a way to merge sounds dynamically, perhaps to be played back together on a web page?

To merge two sounds but keep the sound files separated on the server, it's easiest to do it with a client-side player built with Flash. You can use PHP to find and/or choose the two sound files, pass the file names into Flash in the querystring, and have them both load and play (or loop) simultaneously in the Flash player.

There are obvious reasons why mixing sound files is useful, but I can't guess why you're trying to do it with PHP. Perhaps you could reply with a more detailed description of your project.

httpwebwitch

8:53 pm on Jan 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if you're trying to play back the files concurrently (like a jukebox) instead of simultaneously (mixed), then my advice is the same - Use PHP to assemble the sound file names and build a jukebox player with Flash. Flash will stream the audio to the browser, which is nice! - it has all the start/stop/pause functions built in, and FlashMX has a handy "onSoundComplete" event that you can use to trigger the next sound when the current one is finished.

DrDoc

9:04 pm on Jan 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Basically, it's an accessibility issue...

I have an image with random characters (think Yahoo signup page). But, for obvious reasons I can't put the characters in plain text anywhere (or else I would've used the alt attribute). So, I have made one sound file for each character. Say that the string is "foobar". I need to be able to play f.wav o.wav o.wav b.wav a.wav r.wav as one single sound file. It is not feasible to make one file for each combination (not only because it would be cumbersome, but because there are thousands and thousands of combinations). The Flash idea would be easy, but I can't pass a query string back to the page. The merging has to be done on the server.

Now, I know there are command line tools I can use. I already found one that works great for joining wav files. However, this method requires saving the merged file to some output file. True, I can delete the file once it's been used, but I'd rather do something that worked "on the fly".

jatar_k

9:07 pm on Jan 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



does it just read them when the page loads or is it initiated by a click?

DrDoc

9:08 pm on Jan 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can do either or, but I was considering a click trigger

jatar_k

9:16 pm on Jan 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



very interesting, I am talking with someone trying to think of something.

So, let me just make sure I am with you.

User comes to the site and can click on the link to read the letters that they need to type.

The script randomly gens the string 'foobar' and grabs the f-o-o-b-a-r, all wav's, and cats them together and creates a new file.

You tested slamming them together and only the first wav plays. Makes me think there is an end file char that is killing it.

So essentially it needs to grab the first letter, read contents, get rid of end char, get next letter, read contents, when at last letter leave end char and write the new file.

DrDoc

9:26 pm on Jan 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes... something like that...
Except that I don't want to write the output to a file (or else I'd use some command line tool). I want the PHP script to pass the sound on to the browser.

DrDoc

9:38 pm on Jan 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As far as EOF characters... I think it's more an issue of characters defining the beginning of the WAV file.

Oh, and, just in case someone's wondering -- yes, they have the same bitrate/frequency/etc.

DrDoc

9:56 pm on Jan 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



...or, if it would be possible to redirect the output from the command line tool... (i.e. instead of writing to a file, return the data string of the joined wav files, which can then be streamed to the browser)

But that requires knowing C, which I don't :(

jatar_k

10:02 pm on Jan 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could use passthru() [ca.php.net]

DrDoc

10:10 pm on Jan 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, let's rewind for a sec :)

Currently I have a script that merges a number of files, and outputs the data to a new file. But, this is just for testing purposes. Once I have something that works, I'll replace the saving stage with passthru(). However, I have yet to find a way of successfully merging/joining multiple wav files together. So far, only the first file is played.

I also have this command line tool that I can use. It is written in C, and it takes arguments for joining a number of wav files. However, it writes the output to a new file. True, I could then open this new file with PHP, grab the data, delete the file, and then send the data to the browser. But that seems like being a roundabout way of doing things.

I would rather do one of the following:

1) find a way of merging the files in PHP, and just pass the data on to the browser
2) find a way of redirecting the output from the other program. Instead of having it write data to a file (which I would then have to open in PHP) I would like it to simply

return
the output, which can then be passed on straight to the browser

If I knew C I could either convert the C script to PHP, or rewrite the C script to write the data to STDOUT instead.

DrDoc

11:17 pm on Jan 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm, maybe this is something :)

[search.cpan.org...]

DrDoc

12:06 am on Jan 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, after a lot of searching, testing, and otherwise getting frustrated, I finally found a Perl script that lets me do exactly what I want.

I'll post the URL here in case someone else runs into the same problem :)
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=71&lngWId=6

Thanks so much for your help! :)

NickCoons

12:55 am on Jan 3, 2004 (gmt 0)

10+ Year Member



I know you found the script to accomplish the task, but I'd like to make a couple of comments that may be useful.

WAV files have headers that determine what the rest of the file contains, so simply concatenating the files won't work -- As you've mentioned.

Combining two WAV files would require reading both files, and then building a new header based on the information in each. This would require a bit of processing. And even though you'd prefer not to use your command-line tool to create the file, then delete it, your command-line tool is written in compiled C instead of interpreted PHP. This means that, even though it's a roundabout way, it's likely to be faster and more efficient as far as server processing time is concerned and is probably your best bet.

Also.. not sure how your command-line programs outputs, but if the output could be redirected to a the standard output, it could be read in as a variable in PHP, bypassing the need to create and delete a file.

DrDoc

3:48 pm on Jan 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Unfortunately, the command line tool only has two options -- add all files to the first one, or create a new file.
If it could've been redirected to STDOUT I would've chosen that.

httpwebwitch

6:18 pm on Jan 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I understand what you're trying to do, and this is an interesting intellectual exercise, but seriously I think you're solving the problem in an inappropriate way.

For one thing - and this should be obvious - not everyone has speakers on their computer. Many people access the web from the public Library or cafe, and most of the time these are silent machines. And moreso ... you've alienated every deaf or hearing-impaired person who tries to use your site.

If you want someone to confirm a random string in order to confuse harvesting robots, do what the experts do. Create a graphic that has your word in it, but obfuscated in some way that makes it readable to a human brain, but not to a harvesting OCR program. A great example of this is when you sign up for a Yahoo ID or a Yahoo forum.

bofe

9:04 pm on Jan 8, 2004 (gmt 0)

10+ Year Member



I understand what you're trying to do, and this is an interesting intellectual exercise, but seriously I think you're solving the problem in an inappropriate way.

For one thing - and this should be obvious - not everyone has speakers on their computer. Many people access the web from the public Library or cafe, and most of the time these are silent machines. And moreso ... you've alienated every deaf or hearing-impaired person who tries to use your site.

If you want someone to confirm a random string in order to confuse harvesting robots, do what the experts do. Create a graphic that has your word in it, but obfuscated in some way that makes it readable to a human brain, but not to a harvesting OCR program. A great example of this is when you sign up for a Yahoo ID or a Yahoo forum.

I think he's doing it as an alternative to the "random string in an image" approach since screen readers cannot read those images. (because alt text would be harvested by the bots)

It's a unique approach. I like it.

DrDoc

6:25 am on Jan 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just to clearify...
Yes, I have an image with obfuscated text (for those with good vision). Those who, for whatever reason, cannot read the image can click on a link pointing to a script that builds a sound file spelling the word out...

This way, the page should be accessible for anyone, no matter if they are blind or deaf, or dyslexic, or whatever it may be.

bofe

2:38 pm on Jan 9, 2004 (gmt 0)

10+ Year Member



Blind and deaf? ;-)

httpwebwitch

2:55 pm on Jan 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



well hey, then that's a great idea! (genuine applause for accessibility)