-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
303 changed files
with
28,399 additions
and
23,502 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Oops, something went wrong.