mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-27 11:12:44 +01:00
audio: Remove channel argument from audio.Queue
This commit is contained in:
parent
34691dabbf
commit
70fe6d8169
@ -112,7 +112,7 @@ func addNote() {
|
|||||||
vol := 1.0 / 16.0
|
vol := 1.0 / 16.0
|
||||||
square(l, vol, freq, 0.25)
|
square(l, vol, freq, 0.25)
|
||||||
square(r, vol, freq, 0.25)
|
square(r, vol, freq, 0.25)
|
||||||
audio.Queue(-1, toBytes(l, r))
|
audio.Queue(toBytes(l, r))
|
||||||
}
|
}
|
||||||
|
|
||||||
func update(screen *ebiten.Image) error {
|
func update(screen *ebiten.Image) error {
|
||||||
|
@ -71,7 +71,7 @@ func toBytes(l, r []int16) []byte {
|
|||||||
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(-1, n)
|
audio.Queue(n)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
length := len(pcm) * baseFreq / f
|
length := len(pcm) * baseFreq / f
|
||||||
@ -88,7 +88,7 @@ func addNote(freq float64, vol float64) {
|
|||||||
}
|
}
|
||||||
n := toBytes(l, r)
|
n := toBytes(l, r)
|
||||||
noteCache[f] = n
|
noteCache[f] = n
|
||||||
audio.Queue(-1, n)
|
audio.Queue(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
var keys = []ebiten.Key{
|
var keys = []ebiten.Key{
|
||||||
|
@ -28,16 +28,10 @@ const MaxChannel = audio.MaxChannel
|
|||||||
// 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.
|
||||||
//
|
//
|
||||||
// channel must be -1 or a channel index.
|
|
||||||
// If channel is -1, an empty channel is automatically selected.
|
|
||||||
//
|
|
||||||
// If the channel is not empty, this function does nothing and returns false.
|
|
||||||
// This returns true otherwise.
|
|
||||||
//
|
|
||||||
// data's format must be linear PCM (44100Hz, 16bits, 2 channel stereo, little endian)
|
// data's format must be linear PCM (44100Hz, 16bits, 2 channel stereo, little endian)
|
||||||
// without a header (e.g. RIFF header).
|
// without a header (e.g. RIFF header).
|
||||||
func Queue(channel int, data []byte) bool {
|
func Queue(data []byte) bool {
|
||||||
return audio.Queue(channel, data)
|
return audio.Queue(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsPlaying returns a boolean value which indicates if the channel buffer has data to play.
|
// IsPlaying returns a boolean value which indicates if the channel buffer has data to play.
|
||||||
|
@ -70,14 +70,14 @@ func Tick() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Accept sample rate
|
// TODO: Accept sample rate
|
||||||
func Queue(channel int, data []byte) bool {
|
func Queue(data []byte) bool {
|
||||||
result := true
|
result := true
|
||||||
withChannels(func() {
|
withChannels(func() {
|
||||||
if !audioEnabled {
|
if !audioEnabled {
|
||||||
result = false
|
result = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ch := channelAt(channel)
|
ch := channelAt(-1)
|
||||||
if ch == nil {
|
if ch == nil {
|
||||||
result = false
|
result = false
|
||||||
return
|
return
|
||||||
|
@ -28,8 +28,9 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type audioProcessor struct {
|
type audioProcessor struct {
|
||||||
channel int
|
channel int
|
||||||
position float64
|
sampleRate int
|
||||||
|
position float64
|
||||||
}
|
}
|
||||||
|
|
||||||
var audioProcessors [MaxChannel]*audioProcessor
|
var audioProcessors [MaxChannel]*audioProcessor
|
||||||
@ -44,13 +45,13 @@ func toLR(data []byte) ([]int16, []int16) {
|
|||||||
return l, r
|
return l, r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *audioProcessor) playChunk(buf []byte, sampleRate int) {
|
func (a *audioProcessor) playChunk(buf []byte) {
|
||||||
if len(buf) == 0 {
|
if len(buf) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const channelNum = 2
|
const channelNum = 2
|
||||||
const bytesPerSample = channelNum * 16 / 8
|
const bytesPerSample = channelNum * 16 / 8
|
||||||
b := context.Call("createBuffer", channelNum, len(buf)/bytesPerSample, sampleRate)
|
b := context.Call("createBuffer", channelNum, len(buf)/bytesPerSample, a.sampleRate)
|
||||||
l := b.Call("getChannelData", 0)
|
l := b.Call("getChannelData", 0)
|
||||||
r := b.Call("getChannelData", 1)
|
r := b.Call("getChannelData", 1)
|
||||||
il, ir := toLR(buf)
|
il, ir := toLR(buf)
|
||||||
@ -67,7 +68,7 @@ func (a *audioProcessor) playChunk(buf []byte, sampleRate int) {
|
|||||||
a.position = c
|
a.position = c
|
||||||
}
|
}
|
||||||
s.Call("start", a.position)
|
s.Call("start", a.position)
|
||||||
a.position += float64(len(il)) / float64(sampleRate)
|
a.position += float64(len(il)) / float64(a.sampleRate)
|
||||||
}
|
}
|
||||||
|
|
||||||
func isPlaying(channel int) bool {
|
func isPlaying(channel int) bool {
|
||||||
@ -77,9 +78,8 @@ func isPlaying(channel int) bool {
|
|||||||
|
|
||||||
func tick() {
|
func tick() {
|
||||||
const bufferSize = 1024
|
const bufferSize = 1024
|
||||||
const sampleRate = 44100 // TODO: This should be changeable
|
|
||||||
for _, a := range audioProcessors {
|
for _, a := range audioProcessors {
|
||||||
a.playChunk(loadChannelBuffer(a.channel, bufferSize*4), sampleRate)
|
a.playChunk(loadChannelBuffer(a.channel, bufferSize*4))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,8 +100,9 @@ func initialize() {
|
|||||||
audioEnabled = true
|
audioEnabled = true
|
||||||
for i := 0; i < len(audioProcessors); i++ {
|
for i := 0; i < len(audioProcessors); i++ {
|
||||||
audioProcessors[i] = &audioProcessor{
|
audioProcessors[i] = &audioProcessor{
|
||||||
channel: i,
|
channel: i,
|
||||||
position: 0,
|
sampleRate: 44100, // TODO: Change this for each chunks
|
||||||
|
position: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user