Skip to content

Commit

Permalink
Update decoder and example
Browse files Browse the repository at this point in the history
Add BytesPerFrame (credit to sukus21)
#1
Modified decoder
#2
Added EOF Callback
Added NewDecoderCallback
Added SetCallback
  • Loading branch information
ImCarsen committed May 4, 2024
1 parent 2cc9c4b commit 5f98fc8
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 35 deletions.
123 changes: 88 additions & 35 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Decoder struct {
frame *frame.Frame
pos int64
bytesPerFrame int64
cb func()
}

func (d *Decoder) readFrame() error {
Expand All @@ -58,6 +59,9 @@ func (d *Decoder) readFrame() error {
func (d *Decoder) Read(buf []byte) (int, error) {
for len(d.buf) == 0 {
if err := d.readFrame(); err != nil {
if err == io.EOF {
d.Callback() // EOF Callback
}
return 0, err
}
}
Expand All @@ -67,6 +71,17 @@ func (d *Decoder) Read(buf []byte) (int, error) {
return n, nil
}

// Called when the decoder reachs EOF
func (d *Decoder) Callback() {
if d.cb != nil {
d.cb()
}
}

func (d *Decoder) SetCallback(callback func()) {
d.cb = callback
}

// Seek is io.Seeker's Seek.
//
// Seek returns an error when the underlying source is not io.Seeker.
Expand All @@ -75,54 +90,50 @@ func (d *Decoder) Read(buf []byte) (int, error) {
// channels, 2 bytes each). Be careful to seek to an offset that is divisible by
// 4 if you want to read at full sample boundaries.
func (d *Decoder) Seek(offset int64, whence int) (int64, error) {
var newPosition int64
if offset == 0 && whence == io.SeekCurrent {
// Handle the special case of asking for the current position specially.
return d.pos, nil
}

npos := int64(0)
switch whence {
case io.SeekStart:
newPosition = offset
npos = offset
case io.SeekCurrent:
newPosition = d.pos + offset
npos = d.pos + offset
case io.SeekEnd:
newPosition = d.Length() + offset
npos = d.Length() + offset
default:
return 0, errors.New("mp3: invalid whence")
}

// Ensure the new position is within bounds
if newPosition < 0 {
newPosition = 0
} else if newPosition > d.Length() {
newPosition = d.Length()
}

// Calculate the frame index and byte offset
frameIndex := newPosition / int64(d.bytesPerFrame)
byteOffset := newPosition % int64(d.bytesPerFrame)

// Seek to the start of the frame
if _, err := d.source.Seek(d.frameStarts[frameIndex], io.SeekStart); err != nil {
return 0, err
}

// Discard any buffered data
d.pos = npos
d.buf = nil
d.frame = nil

// Read frames until reaching the desired position
for i := int64(0); i < frameIndex; i++ {
f := d.pos / d.bytesPerFrame
// If the frame is not first, read the previous ahead of reading that
// because the previous frame can affect the targeted frame.
if f > 0 {
f--
if _, err := d.source.Seek(d.frameStarts[f], 0); err != nil {
return 0, err
}
if err := d.readFrame(); err != nil {
return 0, err
}
if err := d.readFrame(); err != nil {
return 0, err
}
d.buf = d.buf[min(d.bytesPerFrame+(d.pos%d.bytesPerFrame), int64(len(d.buf))):]
} else {
if _, err := d.source.Seek(d.frameStarts[f], 0); err != nil {
return 0, err
}
if err := d.readFrame(); err != nil {
return 0, err
}
d.buf = d.buf[d.pos:]
}

// Skip bytes within the frame
if _, err := io.CopyN(io.Discard, d.source.reader, byteOffset); err != nil {
return 0, err
}

// Update the position
d.pos = newPosition

return d.pos, nil
return npos, nil
}

// SampleRate returns the sample rate like 44100.
Expand Down Expand Up @@ -200,6 +211,12 @@ func (d *Decoder) Length() int64 {
return d.length
}

// Exposes how many bytes are in a single frame.
// Credit to sukus21/go-mp3.
func (d *Decoder) BytesPerFrame() int64 {
return d.bytesPerFrame
}

// NewDecoder decodes the given io.Reader and returns a decoded stream.
//
// The stream is always formatted as 16bit (little endian) 2 channels
Expand Down Expand Up @@ -233,3 +250,39 @@ func NewDecoder(r io.Reader) (*Decoder, error) {

return d, nil
}

// NewDecoderCallback decodes the given io.Reader and returns a decoded stream.
// It also registers the callback for the decoder
//
// The stream is always formatted as 16bit (little endian) 2 channels
// even if the source is single channel MP3.
// Thus, a sample always consists of 4 bytes.
func NewDecoderCallback(r io.Reader, callback func()) (*Decoder, error) {
s := &source{
reader: r,
}
d := &Decoder{
source: s,
length: invalidLength,
cb: callback,
}

if err := s.skipTags(); err != nil {
return nil, err
}
// TODO: Is readFrame here really needed?
if err := d.readFrame(); err != nil {
return nil, err
}
freq, err := d.frame.SamplingFrequency()
if err != nil {
return nil, err
}
d.sampleRate = freq

if err := d.ensureFrameStartsAndLength(); err != nil {
return nil, err
}

return d, nil
}
23 changes: 23 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package main

import (
"fmt"
"io"
"log"
"os"
"time"
Expand Down Expand Up @@ -50,6 +51,9 @@ func run() error {
}
}

// seconds := 40
// SeekToSeconds(seconds)

return nil
}

Expand All @@ -74,3 +78,22 @@ func CreateContext(sampleRate int) *oto.Context {
<-readyChan
return otoCtx
}

func SeekToSeconds(p *oto.Player, d *mp3.Decoder, seconds int) (int64, error) {
// Get the total length of the audio in bytes
totalLength := d.Length()
// Calculate the duration of the audio in seconds
duration := float64(totalLength) / float64(d.SampleRate()) / float64(2*2) // 2 bytes per sample
// Calculate the bitrate in bits per second
bitrate := int(float64(totalLength) * 8 / duration)
// Calculate the byte offset for the desired position in seconds
byteOffset := int64(float64(seconds) * float64(bitrate) / 8)

// Seek to the calculated byte offset
newPosition, err := p.Seek(byteOffset, io.SeekStart)
if err != nil {
return 0, err
}

return newPosition, nil
}

0 comments on commit 5f98fc8

Please sign in to comment.