WIP booyah mp3

This commit is contained in:
Brooke Vibber 2023-03-07 14:41:16 -08:00
parent cbae496cf8
commit 6f9d18670d

View file

@ -568,7 +568,7 @@ class MP3Reader {
return ftell( $this->file ); return ftell( $this->file );
} }
public function readFrame() { public function readSegment() {
while ( true ) { while ( true ) {
$start = $this->pos(); $start = $this->pos();
$lookahead = 10; $lookahead = 10;
@ -582,16 +582,15 @@ class MP3Reader {
$data = unpack( "Nval", $bytes ); $data = unpack( "Nval", $bytes );
$header = new MP3FrameHeader( $data['val'] ); $header = new MP3FrameHeader( $data['val'] );
if ( $header->sync ) { if ( $header->sync ) {
$segments[] = [ // Note we don't need the data at this time.
fseek( $this->file, $start + $header->size, SEEK_SET );
$this->timestamp += $header->duration;
return [
'start' => $start, 'start' => $start,
'size' => $header->size, 'size' => $header->size,
'timestamp' => $this->timestamp, 'timestamp' => $this->timestamp,
'duration' => $header->duration, 'duration' => $header->duration,
]; ];
$this->timestamp += $header->duration;
// Note we don't need the data at this time.
fseek( $this->file, $start + $header->size, SEEK_SET );
continue;
} }
// ID3v2.3 // ID3v2.3
@ -608,14 +607,13 @@ class MP3Reader {
( $id3['size2'] << 14) | ( $id3['size2'] << 14) |
( $id3['size1'] << 21) ); ( $id3['size1'] << 21) );
// For byte range purposes; count as zero duration // For byte range purposes; count as zero duration
$segments[] = [ fseek( $this->file, $start + $size, SEEK_SET );
return [
'start' => $start, 'start' => $start,
'size' => $size, 'size' => $size,
'timestamp' => $this->timestamp, 'timestamp' => $this->timestamp,
'duration' => 0.0, 'duration' => 0.0,
]; ];
fseek( $this->file, $start + $size, SEEK_SET );
continue;
} }
$hex = hexdump( $bytes ); $hex = hexdump( $bytes );
@ -640,12 +638,14 @@ function extractMP3( $filename ) {
$timestamp = 0.0; $timestamp = 0.0;
while ( true ) { while ( true ) {
$start = $reader->pos(); $start = $reader->pos();
$segment = $reader->readFrame(); $segment = $reader->readSegment();
var_dump($segment);
if ( !$segment ) { if ( !$segment ) {
return $segments; break;
} }
$segments[] = $segment; $segments[] = $segment;
} }
var_dump($segments);
return $segments; return $segments;
} finally { } finally {
fclose( $file ); fclose( $file );