I'm trying to build a way to encode AS3 audio recording data to WAV format in PHP. I was using a WAV encoder library in AS3 previously, but a 2 minute recording would take 4-6 minutes on mobile, depending on device. So I need to encode to WAV (or some other format that would make it usable) in PHP to take the load off the app. I've transated this encoder from Imbert Thibault to PHP. See the translation: function encode($samples, $channels = 2, $bits = 16, $rate = 44100){ $data = create($samples); $length = strlen($data); $bytes = createHeaders($length, $channels, $bits, $rate); $bytes .= $data; return $bytes; } function create($samples) { $newBytes = ''; $bytes = unpack("f*", $samples); foreach ($bytes as $value) { $newBytes .= pack("V*",$value * 0x7FFF); } return $newBytes; } function createHeaders($length, $channels = 2, $bits = 16, $rate = 44100) { $RIFF = "RIFF"; $WAVE = "WAVE"; $FMT = "fmt "; $DATA = "data"; $header = ''; $header .= $RIFF; $header .= pack("I*", $length + 44); $header .= $WAVE; $header .= $FMT; $header .= pack("I*", 16); $header .= pack("v*", 1); $header .= pack("v*", $channels); $header .= pack("I*", $rate); $header .= pack("I*", ( $rate * $channels * ($bits >> 3))); $header .= pack("v*", ($channels * ($bits >> 3))); $header .= pack("v*", $bits); $header .= $DATA; $header .= pack("I*", $length); return $header; } That will create a readable file at the correct length (so the headers are obviously correct), but there is only silence when it is played. If I change the pack format in "create()" to "f*" (float, instead of unsigned 32-bit little endian int), I get static and I can faintly hear the sound in the background. I cannot adjust that multiplier to clear up the static, however. (EDIT: Before it is mentioned, I know the asterisk is not needed for most if not all of the pack formats. It was something I tried to see if it would fix things and I never deleted it) The file itself is created as so: private function start():void { if ( !mic ) { this.mic = Microphone.getMicrophone(); this.mic.rate = 44; this.mic.setSilenceLevel( 0, 10000 ); this.recording = new ByteArray(); } this.recording.length = 0; //reset byte array in case this is not first recording this.mic.addEventListener( SampleDataEvent.SAMPLE_DATA, this.saveSample ); } private function saveSample( e:SampleDataEvent 😞void { while( e.data.bytesAvailable ) { var byte:Number = e.data.readFloat(); this.recording.writeFloat( byte ); } } public function stopRecording():void { var file:File = File.applicationStorageDirectory.resolvePath( "audio-recording.wav" ); this.fs.open( file, FileMode.WRITE ); this.fs.writeBytes( this.recording ); this.fs.close(); } I am trying to encode the file saved in stopRecording(). At this point, I don't know what to do. I tried translating another encoder as well, but that seemed to run through memory like it was nothing. Does anyone have any suggestion as to what the problem is? Both encoders work just fine in AS3. Is it a problem with writing the file? Problem with writing the audio data to the byte array? Any suggestions at all at this point will be useful
... View more