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