mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +01:00
audio: Add NewPlayerFromBytes (#231)
This commit is contained in:
parent
a2402c32c0
commit
b02d89cdc3
@ -26,6 +26,7 @@
|
|||||||
package audio
|
package audio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -295,6 +296,27 @@ func NewPlayer(context *Context, src ReadSeekCloser) (*Player, error) {
|
|||||||
return p, nil
|
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.
|
// 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.
|
// After closing, the stream owned by the player will be usable to a new player again.
|
||||||
|
@ -15,7 +15,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
@ -97,15 +96,6 @@ func toBytes(l, r []int16) []byte {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
type srcStream struct {
|
|
||||||
*bytes.Reader
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *srcStream) Close() error {
|
|
||||||
s.Reader = nil
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func addNote() error {
|
func addNote() error {
|
||||||
size := sampleRate / ebiten.FPS
|
size := sampleRate / ebiten.FPS
|
||||||
notes := []float64{freqC, freqD, freqE, freqF, freqG, freqA * 2, freqB * 2}
|
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(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)
|
||||||
s := &srcStream{bytes.NewReader(b)}
|
p, err := audio.NewPlayerFromBytes(audioContext, b)
|
||||||
p, err := audio.NewPlayer(audioContext, s)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"image/color"
|
"image/color"
|
||||||
"log"
|
"log"
|
||||||
@ -80,22 +79,13 @@ func toBytes(l, r []int16) []byte {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
type stream struct {
|
|
||||||
*bytes.Reader
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *stream) Close() error {
|
|
||||||
s.Reader = nil
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func addNote(freq float64, vol float64) error {
|
func addNote(freq float64, vol float64) error {
|
||||||
// TODO: Call Close method of *audio.Player.
|
// TODO: Call Close method of *audio.Player.
|
||||||
// However, this works without Close because Close is automatically called when GC
|
// However, this works without Close because Close is automatically called when GC
|
||||||
// collects a *audio.Player object.
|
// collects a *audio.Player object.
|
||||||
f := int(freq)
|
f := int(freq)
|
||||||
if n, ok := noteCache[f]; ok {
|
if n, ok := noteCache[f]; ok {
|
||||||
p, err := audio.NewPlayer(audioContext, &stream{bytes.NewReader(n)})
|
p, err := audio.NewPlayerFromBytes(audioContext, n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -116,7 +106,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(audioContext, &stream{bytes.NewReader(n)})
|
p, err := audio.NewPlayerFromBytes(audioContext, n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user