Skip to content

Commit

Permalink
Add Decoder.init method, for common initialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
pekim committed Mar 7, 2024
1 parent e15defe commit b1b9b3c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
13 changes: 5 additions & 8 deletions decoder-stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ package opus
import "C"

import (
"fmt"
"io"
"sync"
"time"
"unsafe"
)

Expand Down Expand Up @@ -47,11 +45,7 @@ func NewStreamDecoder(stream io.ReadSeekCloser) (*Decoder, error) {
return nil, errorFromOpusFileError(opusFileErr)
}

d.opusFile = opusFile
d.link = C.op_current_link(opusFile)
d.channelCount = C.op_channel_count(opusFile, d.link)
pcmTotal := C.op_pcm_total(opusFile, d.link)
d.duration = time.Millisecond * time.Duration((float64(pcmTotal) / 48_000 * 1_000))
d.init(opusFile)
d.callbacks = callbacks

return d, nil
Expand Down Expand Up @@ -104,6 +98,9 @@ func goFileTell(stream unsafe.Pointer) C.opus_int64 {
//export goFileClose
func goFileClose(stream unsafe.Pointer) C.int {
d := getDecoderForId(stream)
fmt.Println(" ", d)
err := d.stream.Close()
if err != nil {
return -1
}
return 0
}
19 changes: 8 additions & 11 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,19 @@ func NewDecoder(data []byte) (*Decoder, error) {
return nil, errorFromOpusFileError(opusFileErr)
}

d := newDecoder(opusFile)
d := &Decoder{}
d.init(opusFile)
d.data = data

return d, nil
}

func newDecoder(opusFile *C.OggOpusFile) *Decoder {
link := C.op_current_link(opusFile)
channelCount := C.op_channel_count(opusFile, link)
d := &Decoder{
opusFile: opusFile,
link: link,
channelCount: channelCount,
}
func (d *Decoder) init(opusFile *C.OggOpusFile) {
d.opusFile = opusFile
d.opusFile = opusFile
d.link = C.op_current_link(opusFile)
d.channelCount = C.op_channel_count(opusFile, d.link)
d.duration = time.Millisecond * time.Duration((float64(d.Len()) / 48_000 * 1_000))

return d
}

func (d *Decoder) Close() {
Expand Down
1 change: 1 addition & 0 deletions example/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func main() {

fmt.Println("channel count :", decoder.ChannelCount())
fmt.Println("duration (seconds) :", decoder.Duration().Seconds())
fmt.Println("len :", decoder.Len())
fmt.Printf("tags, vendor : %#v\n", decoder.TagsVendor())
fmt.Println("tags, user comments :", decoder.TagsUserComments())
fmt.Printf("head : %#v\n", decoder.Head())
Expand Down
1 change: 1 addition & 0 deletions example/stream/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func main() {

fmt.Println("channel count :", decoder.ChannelCount())
fmt.Println("duration (seconds) :", decoder.Duration().Seconds())
fmt.Println("len :", decoder.Len())
fmt.Printf("tags, vendor : %#v\n", decoder.TagsVendor())
fmt.Println("tags, user comments :", decoder.TagsUserComments())
fmt.Printf("head : %#v\n", decoder.Head())
Expand Down

0 comments on commit b1b9b3c

Please sign in to comment.