mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 10:48:53 +01:00
audio: Add Context
This commit is contained in:
parent
c6fc5ab865
commit
58c5e5e655
@ -31,6 +31,12 @@ const (
|
||||
sampleRate = 44100
|
||||
)
|
||||
|
||||
var audioContext *audio.Context
|
||||
|
||||
func init() {
|
||||
audioContext = audio.NewContext(sampleRate)
|
||||
}
|
||||
|
||||
var frames = 0
|
||||
|
||||
const (
|
||||
@ -116,7 +122,7 @@ func addNote() error {
|
||||
square(l, vol, freq, 0.25)
|
||||
square(r, vol, freq, 0.25)
|
||||
b := toBytes(l, r)
|
||||
p, err := audio.NewPlayer(bytes.NewReader(b), sampleRate)
|
||||
p, err := audioContext.NewPlayer(bytes.NewReader(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -33,6 +33,12 @@ const (
|
||||
sampleRate = 44100
|
||||
)
|
||||
|
||||
var audioContext *audio.Context
|
||||
|
||||
func init() {
|
||||
audioContext = audio.NewContext(sampleRate)
|
||||
}
|
||||
|
||||
var pcm = make([]float64, 4*sampleRate)
|
||||
|
||||
const baseFreq = 220
|
||||
@ -81,7 +87,7 @@ func (s *stream) Close() error {
|
||||
func addNote(freq float64, vol float64) error {
|
||||
f := int(freq)
|
||||
if n, ok := noteCache[f]; ok {
|
||||
p, err := audio.NewPlayer(&stream{bytes.NewReader(n)}, sampleRate)
|
||||
p, err := audioContext.NewPlayer(&stream{bytes.NewReader(n)})
|
||||
if err == audio.ErrTooManyPlayers {
|
||||
return nil
|
||||
}
|
||||
@ -105,7 +111,7 @@ func addNote(freq float64, vol float64) error {
|
||||
}
|
||||
n := toBytes(l, r)
|
||||
noteCache[f] = n
|
||||
p, err := audio.NewPlayer(&stream{bytes.NewReader(n)}, sampleRate)
|
||||
p, err := audioContext.NewPlayer(&stream{bytes.NewReader(n)})
|
||||
if err == audio.ErrTooManyPlayers {
|
||||
return nil
|
||||
}
|
||||
|
@ -32,6 +32,12 @@ const (
|
||||
frequency = 440
|
||||
)
|
||||
|
||||
var audioContext *audio.Context
|
||||
|
||||
func init() {
|
||||
audioContext = audio.NewContext(sampleRate)
|
||||
}
|
||||
|
||||
type stream struct {
|
||||
position int64
|
||||
}
|
||||
@ -75,7 +81,7 @@ var player *audio.Player
|
||||
func update(screen *ebiten.Image) error {
|
||||
if player == nil {
|
||||
var err error
|
||||
player, err = audio.NewPlayer(&stream{}, sampleRate)
|
||||
player, err = audioContext.NewPlayer(&stream{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -19,6 +19,18 @@ import (
|
||||
"io"
|
||||
)
|
||||
|
||||
// TODO: Enable to specify the format like Mono8?
|
||||
|
||||
type Context struct {
|
||||
sampleRate int
|
||||
}
|
||||
|
||||
func NewContext(sampleRate int) *Context {
|
||||
return &Context{
|
||||
sampleRate: sampleRate,
|
||||
}
|
||||
}
|
||||
|
||||
type Player struct {
|
||||
player *player
|
||||
}
|
||||
@ -31,8 +43,8 @@ type Player struct {
|
||||
// without a header (e.g. RIFF header).
|
||||
//
|
||||
// TODO: Pass sample rate and num of channels.
|
||||
func NewPlayer(src io.ReadSeeker, sampleRate int) (*Player, error) {
|
||||
return newPlayer(src, sampleRate)
|
||||
func (c *Context) NewPlayer(src io.ReadSeeker) (*Player, error) {
|
||||
return newPlayer(src, c.sampleRate)
|
||||
}
|
||||
|
||||
var ErrTooManyPlayers = errors.New("audio: too many players exist")
|
||||
|
Loading…
Reference in New Issue
Block a user