Skip to content

Commit

Permalink
Update oto to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
WinPooh32 committed Dec 1, 2021
1 parent 73853c7 commit 127a152
Show file tree
Hide file tree
Showing 303 changed files with 28,399 additions and 23,502 deletions.
8 changes: 5 additions & 3 deletions audio/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package audio
import (
"log"

"github.com/hajimehoshi/oto"
"github.com/hajimehoshi/oto/v2"
)

var otoContext *oto.Context
Expand All @@ -12,14 +12,16 @@ const (
sampleRate = 44100
channelNum = 2
bitDepth = 2
bufferSize = 8 << 10
)

func init() {
var err error
var ready chan struct{}

otoContext, err = oto.NewContext(sampleRate, channelNum, bitDepth, bufferSize)
otoContext, ready, err = oto.NewContext(sampleRate, channelNum, bitDepth)
if err != nil {
log.Fatalf("failed to start Oto context: %s\n", err)
}

<-ready
}
102 changes: 27 additions & 75 deletions audio/player.go
Original file line number Diff line number Diff line change
@@ -1,123 +1,75 @@
package audio

import (
"bytes"
"io"
"sync"

"github.com/hajimehoshi/oto"
"github.com/hajimehoshi/oto/v2"
)

type Player struct {
ch chan int
data *bytes.Reader
player *oto.Player

once sync.Once
done chan struct{}

playing bool
loop bool
player oto.Player
sb *soundbuf

start chan struct{}
pause chan struct{}
pause bool
loop bool
}

func (p *Player) Play(loop bool) {
if p.playing {
return
}
p.playing = true
p.loop = loop
p.start <- struct{}{}
p.sb.Loop(loop)

if !p.player.IsPlaying() {
if p.sb.finished {
p.Seek(0)
} else {
p.player.Play()
}
} else {
p.Seek(0)
}
}

func (p *Player) Pause() {
if !p.playing {
if !p.player.IsPlaying() {
return
}
p.playing = false
p.pause <- struct{}{}
p.player.Pause()
}

func (p *Player) Seek(ms int) {
p.ch <- ms
p.player.Reset()
p.sb.Seek(ms)
p.player.Play()
}

func (p *Player) Playing() bool {
return p.playing
return p.player.IsPlaying()
}

func (p *Player) Close() error {
var err error

p.once.Do(func() {
close(p.done)
close(p.start)
close(p.pause)
err = p.player.Close()
})

return err
}

func NewPlayer(data []byte) *Player {
sb := newSoundbuf(data)

var p = &Player{
ch: make(chan int, 1),
data: bytes.NewReader(data),
player: otoContext.NewPlayer(),
sb: sb,
player: otoContext.NewPlayer(sb),

once: sync.Once{},
done: make(chan struct{}),

playing: false,
loop: false,

start: make(chan struct{}, 1),
pause: make(chan struct{}, 1),
pause: false,
loop: false,
}

// p.pause <- struct{}{}

go func() {
var playing bool

for {

select {
case <-p.pause:
// <-p.start
playing = false
case <-p.start:
playing = true
default:
}

select {
case <-p.done:
break
case ms := <-p.ch:
// TODO convert ms to buffer's offset.
p.data.Seek(int64(ms), io.SeekStart)
default:
}

var b [bufferSize]byte

// Always write zeros, otherwise it locks on creating new player.
// issue: https://github.com/hajimehoshi/oto/issues/117
if playing {
n, err := p.data.Read(b[:])
if err == io.EOF && p.loop {
p.data.Seek(0, io.SeekStart)
p.data.Read(b[n:])
}
}

p.player.Write(b[:])
}
}()

return p
}
77 changes: 77 additions & 0 deletions audio/soundbuf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package audio

import (
"fmt"
"io"
)

type soundbuf struct {
buf []byte
ptr int
loop bool
finished bool
}

func newSoundbuf(data []byte) *soundbuf {
return &soundbuf{
buf: data,
ptr: 0,
}
}

func (rb *soundbuf) Done() bool {
return rb.finished
}

func (rb *soundbuf) Loop(enabled bool) {
rb.loop = enabled
}

func (rb *soundbuf) Seek(pos int) int {
if pos < len(rb.buf) {
rb.ptr = pos
} else {
rb.ptr = len(rb.buf) - 1
}
rb.finished = false
return rb.ptr
}

func (rb *soundbuf) Read(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, fmt.Errorf("empty p")
}

ptr := rb.ptr

if rb.loop {
beg := ptr
end := (ptr + len(p)) % len(rb.buf)

if end > beg {
copy(p, rb.buf[beg:end])
} else {
part := rb.buf[beg:]
copy(p, part)

if len(p) > len(part) {
copy(p[len(part):], rb.buf[:end])
}
}

rb.ptr = end

return len(p), nil
} else {
n = copy(p, rb.buf[ptr:])

if n == 0 {
rb.finished = true
return 0, io.EOF
}

rb.ptr = ptr + n

return n, nil
}
}
18 changes: 14 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
module github.com/WinPooh32/suslik

go 1.16
go 1.17

require (
github.com/EngoEngine/math v1.0.4
github.com/ajhager/webgl v0.0.0-20160525004648-5c5cb2d83893
github.com/go-gl/gl v0.0.0-20210315015930-ae072cafe09d // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20210311203641-62640a716d48
github.com/gopherjs/gopherjs v0.0.0-20210202160940-bed99a852dfe
github.com/gopherjs/gopherjs v0.0.0-20211111143520-d0d5ecc1a356
github.com/hajimehoshi/go-mp3 v0.3.1
github.com/hajimehoshi/oto v0.6.1
github.com/hajimehoshi/oto/v2 v2.0.2
github.com/jfreymuth/oggvorbis v1.0.3
github.com/stretchr/testify v1.7.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-gl/gl v0.0.0-20210315015930-ae072cafe09d // indirect
github.com/jfreymuth/vorbis v1.0.2 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Loading

0 comments on commit 127a152

Please sign in to comment.