mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +01:00
audio: Move VorbisStream to a new package
This commit is contained in:
parent
71312ba26f
commit
bc49108c40
@ -21,6 +21,7 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
"github.com/hajimehoshi/ebiten/exp/audio"
|
"github.com/hajimehoshi/ebiten/exp/audio"
|
||||||
|
"github.com/hajimehoshi/ebiten/exp/audio/vorbis"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -62,7 +63,7 @@ func main() {
|
|||||||
audioLoadingDone = make(chan struct{})
|
audioLoadingDone = make(chan struct{})
|
||||||
// TODO: This doesn't work synchronously on browsers because of decoding. Fix this.
|
// TODO: This doesn't work synchronously on browsers because of decoding. Fix this.
|
||||||
go func() {
|
go func() {
|
||||||
s, err := audioContext.NewVorbisStream(f)
|
s, err := vorbis.Decode(audioContext, f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
return
|
return
|
||||||
|
@ -131,6 +131,13 @@ func (c *Context) Update() {
|
|||||||
c.frames++
|
c.frames++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SampleRate returns the sample rate.
|
||||||
|
func (c *Context) SampleRate() int {
|
||||||
|
c.Lock()
|
||||||
|
defer c.Unlock()
|
||||||
|
return c.sampleRate
|
||||||
|
}
|
||||||
|
|
||||||
type Player struct {
|
type Player struct {
|
||||||
context *Context
|
context *Context
|
||||||
src io.ReadSeeker
|
src io.ReadSeeker
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
// +build !js
|
// +build !js
|
||||||
|
|
||||||
package audio
|
package vorbis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@ -23,16 +23,15 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten/exp/audio"
|
||||||
"github.com/hajimehoshi/go-vorbis"
|
"github.com/hajimehoshi/go-vorbis"
|
||||||
)
|
)
|
||||||
|
|
||||||
type VorbisStream struct {
|
type Stream struct {
|
||||||
buf *bytes.Reader
|
buf *bytes.Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Rename to DecodeVorbis or Decode?
|
func Decode(context *audio.Context, src io.Reader) (*Stream, error) {
|
||||||
|
|
||||||
func (c *Context) NewVorbisStream(src io.Reader) (*VorbisStream, error) {
|
|
||||||
decoded, channels, sampleRate, err := vorbis.Decode(src)
|
decoded, channels, sampleRate, err := vorbis.Decode(src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -40,8 +39,8 @@ func (c *Context) NewVorbisStream(src io.Reader) (*VorbisStream, error) {
|
|||||||
if channels != 2 {
|
if channels != 2 {
|
||||||
return nil, errors.New("audio: number of channels must be 2")
|
return nil, errors.New("audio: number of channels must be 2")
|
||||||
}
|
}
|
||||||
if sampleRate != c.sampleRate {
|
if sampleRate != context.SampleRate() {
|
||||||
return nil, fmt.Errorf("audio: sample rate must be %d but %d", c.sampleRate, sampleRate)
|
return nil, fmt.Errorf("audio: sample rate must be %d but %d", context.SampleRate(), sampleRate)
|
||||||
}
|
}
|
||||||
// TODO: Read all data once so that Seek can be implemented easily.
|
// TODO: Read all data once so that Seek can be implemented easily.
|
||||||
// We should look for a wiser way.
|
// We should look for a wiser way.
|
||||||
@ -49,16 +48,16 @@ func (c *Context) NewVorbisStream(src io.Reader) (*VorbisStream, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
s := &VorbisStream{
|
s := &Stream{
|
||||||
buf: bytes.NewReader(b),
|
buf: bytes.NewReader(b),
|
||||||
}
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *VorbisStream) Read(p []byte) (int, error) {
|
func (s *Stream) Read(p []byte) (int, error) {
|
||||||
return s.buf.Read(p)
|
return s.buf.Read(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *VorbisStream) Seek(offset int64, whence int) (int64, error) {
|
func (s *Stream) Seek(offset int64, whence int) (int64, error) {
|
||||||
return s.buf.Seek(offset, whence)
|
return s.buf.Seek(offset, whence)
|
||||||
}
|
}
|
@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
// +build js
|
// +build js
|
||||||
|
|
||||||
package audio
|
package vorbis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@ -22,25 +22,26 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
|
||||||
"github.com/gopherjs/gopherjs/js"
|
"github.com/gopherjs/gopherjs/js"
|
||||||
|
"github.com/hajimehoshi/ebiten/exp/audio"
|
||||||
)
|
)
|
||||||
|
|
||||||
type VorbisStream struct {
|
type Stream struct {
|
||||||
buf *bytes.Reader
|
buf *bytes.Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: This just uses decodeAudioData can treat audio files other than Ogg/Vorbis.
|
// TODO: This just uses decodeAudioData can treat audio files other than Ogg/Vorbis.
|
||||||
// TODO: This doesn't work on iOS which doesn't have Ogg/Vorbis decoder.
|
// TODO: This doesn't work on iOS which doesn't have Ogg/Vorbis decoder.
|
||||||
|
|
||||||
func (c *Context) NewVorbisStream(src io.Reader) (*VorbisStream, error) {
|
func Decode(context *audio.Context, src io.Reader) (*Stream, error) {
|
||||||
b, err := ioutil.ReadAll(src)
|
b, err := ioutil.ReadAll(src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
s := &VorbisStream{}
|
s := &Stream{}
|
||||||
ch := make(chan struct{})
|
ch := make(chan struct{})
|
||||||
|
|
||||||
// TODO: 1 is a correct second argument?
|
// TODO: 1 is a correct second argument?
|
||||||
oc := js.Global.Get("OfflineAudioContext").New(2, 1, c.sampleRate)
|
oc := js.Global.Get("OfflineAudioContext").New(2, 1, context.SampleRate())
|
||||||
oc.Call("decodeAudioData", js.NewArrayBuffer(b), func(buf *js.Object) {
|
oc.Call("decodeAudioData", js.NewArrayBuffer(b), func(buf *js.Object) {
|
||||||
defer close(ch)
|
defer close(ch)
|
||||||
il := buf.Call("getChannelData", 0).Interface().([]float32)
|
il := buf.Call("getChannelData", 0).Interface().([]float32)
|
||||||
@ -60,10 +61,10 @@ func (c *Context) NewVorbisStream(src io.Reader) (*VorbisStream, error) {
|
|||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *VorbisStream) Read(p []byte) (int, error) {
|
func (s *Stream) Read(p []byte) (int, error) {
|
||||||
return s.buf.Read(p)
|
return s.buf.Read(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *VorbisStream) Seek(offset int64, whence int) (int64, error) {
|
func (s *Stream) Seek(offset int64, whence int) (int64, error) {
|
||||||
return s.buf.Seek(offset, whence)
|
return s.buf.Seek(offset, whence)
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user