LaVOZs

The World’s Largest Online Community for Developers

'; What is the encoder used by PHP $POST command for audio blob? - LavOzs.Com

The first sequence of audio samples is recorded in JavaScript from the browser in mono, PCM format, 16-bit, and 96,000 Hz. I wrote this audio file as a blob to a server through a JavaScript FormData object using ajax.

These are the raw audio samples. When I retrieved the audio from the web server's directory listing, I received the second sequence of audio samples. It has been downsampled to 48000 Hz and the samples have been altered. What encoder is being used?

Server-side PHP code:

    $input = $_FILES['audio']['tmp_name']; //audio blob
    $output = $_POST['filename'];

    if(move_uploaded_file($input, $output))
        exit('Audio file Uploaded');

Client-side JavaScript code:

    function send_audio(fn, blob){

        var formData = new FormData();

        formData.append("filename", fn);
        formData.append('audio', blob);
        $.ajax({
            url:'save_audio.php',
            type:'post',
            data: formData,
            contentType:false,
            processData:false,
            cache:false,
            success: function(data){
                console.log("send_audio success!");
                console.log(data);
            }
        });
    }

It's not clear whats going on but consider the following:

(1) Try 16-bits not 32-bits per sample (might help).

There is no need to record these audio samples at 32 bits per sample. That means you're using 4 bytes to record one sample value that fits within 2 bytes (16 bits).

Your highest value is 30465 (bytes x77 x01) and the lowest value is -32513 (bytes x80 xFF).
Notice those values fit two bytes? By using 4 bytes you make x00 x00 x77 x01. A waste to store extra00.

Two bytes hold max value of 65535. Or even 32767 if you divide into two parts as +32767 and -32767.

(2) Make sure there is no confusion between encoding of bytes (as hex), and digits (as integers).

Bytes are written as hex inside a file. Your values seem to mess up when converting from integers into hex format. Also 16 bit vs 32 bits for the

Some examples from your numbers compared to 2 bytes (16-bit) hex:

  FIRST SEQ:        SECOND SEQ:
value   | hex       | value   | hex

100       00 64      612        02 64
553       02 29      41         00 29
203       00 CB      -309       FE CB
-409      FE 67      103        00 67
68        00 44      836        03 44
953       03 B9      185        00 B9
154       00 9A      -102       FF 9A
-127      FF 81      385        01 81

Notice the value of integer 100 when written as hex (byte values) it becomes byte x64, for some reason a 2 is added in front of that 64 now making it x0264 which converts to integer value of 612.

In the second sequence, where are these extra 02 and FE etc coming from? Why if hex of first seq has a 00, then for the second seq there's something extra now added? Even vice-versa (first = something, then second = something removed to become as 00)?

You need to double check your recording code. Does it add some increasing number to the recorded value?? Because, as you know, 2 is followed by 3 and that pattern is there in your second sequence. Also true that FE is followed by FF (and therefore previous to FE would be FD, FC, FB, FA, etc).

If you still can't explain it then:

  • Record at 16-bit with 44,100 Hz like the normal way others do it. Then later get complicated if the simple setup works okay. Why 32-bit and 96,000 Hz anyway? Is it a scientific test?

  • Use a Hex Editor to view your bytes from both sides (saved blob file via JS, against file downloaded via PHP) and compare any changes between the two files.

  • Show how/what your recording code is doing.

  • Share the bytes of "I retrieved the audio from the web server" (save as file) for checking.

Maybe someone else can help you. I don't use html forms so maybe there's an encoding option for the POST data type in there too?

Related
What is base 64 encoding used for?
Detecting request type in PHP (GET, POST, PUT or DELETE)
What is stdClass in PHP?
What are the differences in die() and exit() in PHP?
Reference — What does this symbol mean in PHP?
jQuery Ajax POST example with PHP
Reference - What does this error mean in PHP?
How to save 'multipart/form-data' (picture, pdf, and etc.) to mysql database mediumblob via jquery?