public static function packSample($sample, $bitDepth)
    {
        switch ($bitDepth) {
            case 8:
                // unsigned char
                return chr($sample);

            case 16:
                // signed short, little endian
                if ($sample < 0) {
                    $sample += 0x10000;
                }
                return pack('v', $sample);

            case 24:
                // 3 byte packed signed integer, little endian
                if ($sample < 0) {
                    $sample += 0x1000000;
                }
                return pack('C3', $sample & 0xff, ($sample >>  8) & 0xff, ($sample >> 16) & 0xff);

            case 32:
                // 32-bit float
                return pack('f', $sample);

            default:
                return null;
        }
    }
public static function packSample($sample, $bitDepth)
    {
        switch ($bitDepth) {
            case 8:
                // unsigned char
                return chr($sample);

            case 16:
                // signed short, little endian
                if ($sample < 0) {
                    $sample += 0x10000;
                }
                return pack('v', $sample);

            case 24:
                // 3 byte packed signed integer, little endian
                if ($sample < 0) {
                    $sample += 0x1000000;
                }
                return pack('C3', $sample & 0xff, ($sample >>  8) & 0xff, ($sample >> 16) & 0xff);

            case 32:
                // 32-bit float
                return pack('f', $sample);

            default:
                return null;
        }
    }
public static function packSample ( $ sample , $ bitDepth ) { switch ( $ bitDepth ) { case 8 : // unsigned char return chr ( $ sample ) ; case 16 : // signed short, little endian if ( $ sample < 0 ) { $ sample += 0x10000 ; } return pack ( 'v' , $ sample ) ; case 24 : // 3 byte packed signed integer, little endian if ( $ sample < 0 ) { $ sample += 0x1000000 ; } return pack ( 'C3' , $ sample & 0xff , ( $ sample >> 8 ) & 0xff , ( $ sample >> 16 ) & 0xff ) ; case 32 : // 32-bit float return pack ( 'f' , $ sample ) ; default : return null ; } }
Packs a single numeric sample to binary.

@param int|float $sample  (Required) The sample to encode. Has to be within valid range for $bitDepth. Float values only for 32 bits.
@param int $bitDepth  (Required) The bits per sample to encode with.
@return string|null  The encoded binary sample. Returns null for unsupported bit depths.
Packs a single numeric sample to binary .