mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 02:38:53 +01:00
audio: Add NewPlayerFromBytes (#231)
This commit is contained in:
parent
a2402c32c0
commit
b02d89cdc3
@ -26,6 +26,7 @@
|
||||
package audio
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"runtime"
|
||||
@ -295,6 +296,27 @@ func NewPlayer(context *Context, src ReadSeekCloser) (*Player, error) {
|
||||
return p, nil
|
||||
}
|
||||
|
||||
type bytesReadSeekCloser struct {
|
||||
*bytes.Reader
|
||||
}
|
||||
|
||||
func (b *bytesReadSeekCloser) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewPlayerFromBytes creates a new player with the given bytes.
|
||||
//
|
||||
// As opposed to NewPlayer, you don't have to care if src is already used by another player or not.
|
||||
// src can be shared by multiple players.
|
||||
//
|
||||
// The format of src should be same as noted at NewPlayer.
|
||||
//
|
||||
// This function is concurrent-safe.
|
||||
func NewPlayerFromBytes(context *Context, src []byte) (*Player, error) {
|
||||
b := &bytesReadSeekCloser{bytes.NewReader(src)}
|
||||
return NewPlayer(context, b)
|
||||
}
|
||||
|
||||
// Close closes the stream. Ths source stream passed by NewPlayer will also be closed.
|
||||
//
|
||||
// After closing, the stream owned by the player will be usable to a new player again.
|
||||
|
@ -15,7 +15,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
@ -97,15 +96,6 @@ func toBytes(l, r []int16) []byte {
|
||||
return b
|
||||
}
|
||||
|
||||
type srcStream struct {
|
||||
*bytes.Reader
|
||||
}
|
||||
|
||||
func (s *srcStream) Close() error {
|
||||
s.Reader = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func addNote() error {
|
||||
size := sampleRate / ebiten.FPS
|
||||
notes := []float64{freqC, freqD, freqE, freqF, freqG, freqA * 2, freqB * 2}
|
||||
@ -135,8 +125,7 @@ func addNote() error {
|
||||
square(l, vol, freq, 0.25)
|
||||
square(r, vol, freq, 0.25)
|
||||
b := toBytes(l, r)
|
||||
s := &srcStream{bytes.NewReader(b)}
|
||||
p, err := audio.NewPlayer(audioContext, s)
|
||||
p, err := audio.NewPlayerFromBytes(audioContext, b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -15,7 +15,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image/color"
|
||||
"log"
|
||||
@ -80,22 +79,13 @@ func toBytes(l, r []int16) []byte {
|
||||
return b
|
||||
}
|
||||
|
||||
type stream struct {
|
||||
*bytes.Reader
|
||||
}
|
||||
|
||||
func (s *stream) Close() error {
|
||||
s.Reader = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func addNote(freq float64, vol float64) error {
|
||||
// TODO: Call Close method of *audio.Player.
|
||||
// However, this works without Close because Close is automatically called when GC
|
||||
// collects a *audio.Player object.
|
||||
f := int(freq)
|
||||
if n, ok := noteCache[f]; ok {
|
||||
p, err := audio.NewPlayer(audioContext, &stream{bytes.NewReader(n)})
|
||||
p, err := audio.NewPlayerFromBytes(audioContext, n)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -116,7 +106,7 @@ func addNote(freq float64, vol float64) error {
|
||||
}
|
||||
n := toBytes(l, r)
|
||||
noteCache[f] = n
|
||||
p, err := audio.NewPlayer(audioContext, &stream{bytes.NewReader(n)})
|
||||
p, err := audio.NewPlayerFromBytes(audioContext, n)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user