ok got mp3 working

This commit is contained in:
Brooke Vibber 2023-03-07 15:17:30 -08:00
parent 6f9d18670d
commit cd2dfcd91e

View file

@ -527,14 +527,10 @@ class MP3FrameHeader {
if ( $this->sync ) {
if ( $this->bitrate == 0 ) {
$this->valid = false;
var_dump( $this );
echo "br: $br\n";
throw new Exception( "Invalid bitrate" );
}
if ( $this->sampleRate == 1 ) {
$this->valid = false;
var_dump( $this );
echo "sr: $sr\n";
throw new Exception( "Invalid sample rate" );
}
$this->valid = true;
@ -618,7 +614,6 @@ class MP3Reader {
$hex = hexdump( $bytes );
$safe = safestr ( $bytes );
var_dump($segments);
throw new Exception("Invalid packet at $start? $hex $safe");
// back up and try again
@ -639,13 +634,11 @@ function extractMP3( $filename ) {
while ( true ) {
$start = $reader->pos();
$segment = $reader->readSegment();
var_dump($segment);
if ( !$segment ) {
break;
}
$segments[] = $segment;
}
var_dump($segments);
return $segments;
} finally {
fclose( $file );
@ -661,19 +654,26 @@ function consolidate( $target, $segments ) {
return $segments;
}
$n = count( $segments ) - 1;
$n = count( $segments );
if ( isset( $segments['init'] ) ) {
$n--;
}
$start = $segments[0]['start'];
$size = $segments[0]['size'];
$timestamp = $segments[0]['timestamp'];
$duration = $segments[0]['duration'];
$nextTarget = $timestamp + $target;
$nextDuration = $target;
$i = 1;
while ( $i < $n ) {
while ( $i < $n - 1 ) {
// Append segments until we get close
while ( $i < $n && $duration < $target ) {
while ( $i < $n - 1 && $duration < $nextDuration ) {
$total = $duration + $segments[$i]['duration'];
if ( $total > $target ) {
$after = $total - $target;
$before = $target - $duration;
if ( $total >= $nextDuration ) {
$after = $total - $nextDuration;
$before = $nextDuration - $duration;
echo "$before $after\n";
echo "($total $nextDuration)\n";
if ( $before < $after ) {
// Break segment early
break;
@ -691,6 +691,9 @@ function consolidate( $target, $segments ) {
'timestamp' => $timestamp,
'duration' => $duration,
];
$nextTarget += $target;
$nextDuration = $nextTarget - $timestamp - $duration;
echo "[$nextTarget, $nextDuration]\n";
if ( $i < $n ) {
$segment = $segments[$i];
@ -701,6 +704,12 @@ function consolidate( $target, $segments ) {
$i++;
}
}
$out[] = [
'start' => $start,
'size' => $size,
'timestamp' => $timestamp,
'duration' => $duration,
];
return $out;
}