Question

I wrote a PHP-CLI script that mixes two audio (.WAV PCM) files (with some math involved) so PHP needs to crunch through thousands (if not even millions) of samples with unpack(), do math on them and save them with pack().

Now, I dont need actual info on how to do the mixing or anything, as the title says, I'm looking for possibilites to speed this process up since the script needs 30 seconds of processing time to produce 10 seconds of audio output.

Things that I tried:

  • Cache the audiofiles to memory and crunch through with substr() instead of fseek()/fread(). Performance gain: 3 seconds.
  • Write the output file in 5000-samples chunks. Performance gain: 10 seconds.

After those optimizations I ended up at approximately 17 seconds processing time for 10 seconds audio output. What bugs me, is that other tools can do simple audio operations like mixing two files in realtime or even much faster.

Another idea I had was paralellization, but I refrained from that due to the extra problems that would occur (like calculating correct seek positions for the forks/threads and other related things).

So am I missing stuff out or is this actually good performance for a PHP-CLI script?

Was it helpful?

Solution

Thanks for everyone's input on this one.

I rewrote the thing in C++ and can now perform the above actions in less than a second.

I'd never have thought that the speed difference is that huge (compiled application is ~40X faster).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top