mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 10:48:53 +01:00
audio: Introduce audio.Player
This commit is contained in:
parent
d52118639d
commit
10650d1c6e
@ -122,7 +122,8 @@ func addNote() {
|
|||||||
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)
|
||||||
audio.Queue(&stream{bytes.NewReader(b)}, sampleRate)
|
p := audio.NewPlayer(&stream{bytes.NewReader(b)}, sampleRate)
|
||||||
|
p.Play()
|
||||||
}
|
}
|
||||||
|
|
||||||
func update(screen *ebiten.Image) error {
|
func update(screen *ebiten.Image) error {
|
||||||
|
@ -81,7 +81,8 @@ func (s *stream) Close() error {
|
|||||||
func addNote(freq float64, vol float64) {
|
func addNote(freq float64, vol float64) {
|
||||||
f := int(freq)
|
f := int(freq)
|
||||||
if n, ok := noteCache[f]; ok {
|
if n, ok := noteCache[f]; ok {
|
||||||
audio.Queue(&stream{bytes.NewReader(n)}, sampleRate)
|
p := audio.NewPlayer(&stream{bytes.NewReader(n)}, sampleRate)
|
||||||
|
p.Play()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
length := len(pcm) * baseFreq / f
|
length := len(pcm) * baseFreq / f
|
||||||
@ -98,7 +99,8 @@ func addNote(freq float64, vol float64) {
|
|||||||
}
|
}
|
||||||
n := toBytes(l, r)
|
n := toBytes(l, r)
|
||||||
noteCache[f] = n
|
noteCache[f] = n
|
||||||
audio.Queue(&stream{bytes.NewReader(n)}, sampleRate)
|
p := audio.NewPlayer(&stream{bytes.NewReader(n)}, sampleRate)
|
||||||
|
p.Play()
|
||||||
}
|
}
|
||||||
|
|
||||||
var keys = []ebiten.Key{
|
var keys = []ebiten.Key{
|
||||||
|
@ -25,7 +25,12 @@ type ReadSeekCloser interface {
|
|||||||
io.Closer
|
io.Closer
|
||||||
}
|
}
|
||||||
|
|
||||||
// Queue queues the given data to the given channel.
|
type Player struct {
|
||||||
|
src ReadSeekCloser
|
||||||
|
sampleRate int
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewPlayer creates a new player with the given data to the given channel.
|
||||||
// The given data is queued to the end of the buffer.
|
// The given data is queued to the end of the buffer.
|
||||||
// This may not be played immediately when data already exists in the buffer.
|
// This may not be played immediately when data already exists in the buffer.
|
||||||
//
|
//
|
||||||
@ -33,6 +38,13 @@ type ReadSeekCloser interface {
|
|||||||
// 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 Queue(src ReadSeekCloser, sampleRate int) error {
|
func NewPlayer(src ReadSeekCloser, sampleRate int) *Player {
|
||||||
return audio.Queue(src, sampleRate)
|
return &Player{
|
||||||
|
src: src,
|
||||||
|
sampleRate: sampleRate,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Player) Play() error {
|
||||||
|
return audio.Play(p.src, p.sampleRate)
|
||||||
}
|
}
|
||||||
|
@ -25,15 +25,10 @@ type ReadSeekCloser interface {
|
|||||||
io.Closer
|
io.Closer
|
||||||
}
|
}
|
||||||
|
|
||||||
type chunk struct {
|
|
||||||
buffer ReadSeekCloser
|
|
||||||
sampleRate int
|
|
||||||
}
|
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
initialize()
|
initialize()
|
||||||
}
|
}
|
||||||
|
|
||||||
func Queue(src ReadSeekCloser, sampleRate int) error {
|
func Play(src ReadSeekCloser, sampleRate int) error {
|
||||||
return playChunk(src, sampleRate)
|
return play(src, sampleRate)
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ func (a *audioProcessor) play() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func playChunk(src ReadSeekCloser, sampleRate int) error {
|
func play(src ReadSeekCloser, sampleRate int) error {
|
||||||
a := &audioProcessor{
|
a := &audioProcessor{
|
||||||
src: src,
|
src: src,
|
||||||
sampleRate: sampleRate,
|
sampleRate: sampleRate,
|
||||||
|
@ -24,7 +24,7 @@ import (
|
|||||||
|
|
||||||
var players = map[*audio.Player]struct{}{}
|
var players = map[*audio.Player]struct{}{}
|
||||||
|
|
||||||
func playChunk(src ReadSeekCloser, sampleRate int) error {
|
func play(src ReadSeekCloser, sampleRate int) error {
|
||||||
// TODO: audio.NewPlayer interprets WAV header, which we don't want.
|
// TODO: audio.NewPlayer interprets WAV header, which we don't want.
|
||||||
// Use OpenAL or native API instead.
|
// Use OpenAL or native API instead.
|
||||||
p, err := audio.NewPlayer(src, audio.Stereo16, int64(sampleRate))
|
p, err := audio.NewPlayer(src, audio.Stereo16, int64(sampleRate))
|
||||||
|
Loading…
Reference in New Issue
Block a user