mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
audio: Change API: accept []int16 instead of []float32
This commit is contained in:
parent
c2313c10c1
commit
e1336c2eba
@ -20,6 +20,7 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
"github.com/hajimehoshi/ebiten/exp/audio"
|
"github.com/hajimehoshi/ebiten/exp/audio"
|
||||||
"log"
|
"log"
|
||||||
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -48,7 +49,7 @@ const score = `CCGGAAGR FFEEDDCR GGFFEEDR GGFFEEDR CCGGAAGR FFEEDDCR`
|
|||||||
|
|
||||||
var scoreIndex = 0
|
var scoreIndex = 0
|
||||||
|
|
||||||
func square(out []float32, volume float64, freq float64, sequence float64) {
|
func square(out []int16, volume float64, freq float64, sequence float64) {
|
||||||
if freq == 0 {
|
if freq == 0 {
|
||||||
for i := 0; i < len(out); i++ {
|
for i := 0; i < len(out); i++ {
|
||||||
out[i] = 0
|
out[i] = 0
|
||||||
@ -60,7 +61,7 @@ func square(out []float32, volume float64, freq float64, sequence float64) {
|
|||||||
panic("invalid freq")
|
panic("invalid freq")
|
||||||
}
|
}
|
||||||
for i := 0; i < len(out); i++ {
|
for i := 0; i < len(out); i++ {
|
||||||
a := float32(volume)
|
a := int16(volume * math.MaxInt16)
|
||||||
if i%length < int(float64(length)*sequence) {
|
if i%length < int(float64(length)*sequence) {
|
||||||
a = -a
|
a = -a
|
||||||
}
|
}
|
||||||
@ -76,8 +77,8 @@ func addNote() {
|
|||||||
scoreIndex++
|
scoreIndex++
|
||||||
scoreIndex %= len(score)
|
scoreIndex %= len(score)
|
||||||
}()
|
}()
|
||||||
l := make([]float32, size*30)
|
l := make([]int16, size*30)
|
||||||
r := make([]float32, size*30)
|
r := make([]int16, size*30)
|
||||||
note := score[scoreIndex]
|
note := score[scoreIndex]
|
||||||
for note == ' ' {
|
for note == ' ' {
|
||||||
scoreIndex++
|
scoreIndex++
|
||||||
|
@ -32,7 +32,7 @@ var MaxChannel = audio.MaxChannel
|
|||||||
// If the channel is not empty, this function does nothing and returns false. This returns true otherwise.
|
// If the channel is not empty, this function does nothing and returns false. This returns true otherwise.
|
||||||
//
|
//
|
||||||
// This function is useful to play SE or a note of PCM synthesis immediately.
|
// This function is useful to play SE or a note of PCM synthesis immediately.
|
||||||
func Play(channel int, l []float32, r []float32) bool {
|
func Play(channel int, l []int16, r []int16) bool {
|
||||||
return audio.Play(channel, l, r)
|
return audio.Play(channel, l, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,7 +42,7 @@ func Play(channel int, l []float32, r []float32) bool {
|
|||||||
// channel must be a channel index. You can't give -1 to channel.
|
// channel must be a channel index. You can't give -1 to channel.
|
||||||
//
|
//
|
||||||
// This function is useful to play streaming data.
|
// This function is useful to play streaming data.
|
||||||
func Queue(channel int, l []float32, r []float32) {
|
func Queue(channel int, l []int16, r []int16) {
|
||||||
audio.Queue(channel, l, r)
|
audio.Queue(channel, l, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,8 +27,8 @@ var nextInsertionPosition = 0
|
|||||||
var currentPosition = 0
|
var currentPosition = 0
|
||||||
|
|
||||||
type channel struct {
|
type channel struct {
|
||||||
l []float32
|
l []int16
|
||||||
r []float32
|
r []int16
|
||||||
}
|
}
|
||||||
|
|
||||||
var MaxChannel = 32
|
var MaxChannel = 32
|
||||||
@ -39,8 +39,8 @@ var channelsLock sync.Mutex
|
|||||||
func init() {
|
func init() {
|
||||||
for i, _ := range channels {
|
for i, _ := range channels {
|
||||||
channels[i] = &channel{
|
channels[i] = &channel{
|
||||||
l: []float32{},
|
l: []int16{},
|
||||||
r: []float32{},
|
r: []int16{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -73,7 +73,7 @@ func channelAt(i int) *channel {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Play(channel int, l []float32, r []float32) bool {
|
func Play(channel int, l []int16, r []int16) bool {
|
||||||
channelsLock.Lock()
|
channelsLock.Lock()
|
||||||
defer channelsLock.Unlock()
|
defer channelsLock.Unlock()
|
||||||
|
|
||||||
@ -88,14 +88,14 @@ func Play(channel int, l []float32, r []float32) bool {
|
|||||||
if ch == nil {
|
if ch == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
ch.l = append(ch.l, make([]float32, nextInsertionPosition-len(ch.l))...)
|
ch.l = append(ch.l, make([]int16, nextInsertionPosition-len(ch.l))...)
|
||||||
ch.r = append(ch.r, make([]float32, nextInsertionPosition-len(ch.r))...)
|
ch.r = append(ch.r, make([]int16, nextInsertionPosition-len(ch.r))...)
|
||||||
ch.l = append(ch.l, l...)
|
ch.l = append(ch.l, l...)
|
||||||
ch.r = append(ch.r, r...)
|
ch.r = append(ch.r, r...)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func Queue(channel int, l []float32, r []float32) {
|
func Queue(channel int, l []int16, r []int16) {
|
||||||
channelsLock.Lock()
|
channelsLock.Lock()
|
||||||
defer channelsLock.Unlock()
|
defer channelsLock.Unlock()
|
||||||
|
|
||||||
@ -138,7 +138,7 @@ func isChannelsEmpty() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadChannelBuffers() (l, r []float32) {
|
func loadChannelBuffers() (l, r []int16) {
|
||||||
channelsLock.Lock()
|
channelsLock.Lock()
|
||||||
defer channelsLock.Unlock()
|
defer channelsLock.Unlock()
|
||||||
|
|
||||||
@ -146,8 +146,8 @@ func loadChannelBuffers() (l, r []float32) {
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
inputL := make([]float32, bufferSize)
|
inputL := make([]int16, bufferSize)
|
||||||
inputR := make([]float32, bufferSize)
|
inputR := make([]int16, bufferSize)
|
||||||
for _, ch := range channels {
|
for _, ch := range channels {
|
||||||
if len(ch.l) == 0 {
|
if len(ch.l) == 0 {
|
||||||
continue
|
continue
|
||||||
|
@ -38,6 +38,7 @@ func initialize() {
|
|||||||
r := e.Get("outputBuffer").Call("getChannelData", 1)
|
r := e.Get("outputBuffer").Call("getChannelData", 1)
|
||||||
inputL, inputR := loadChannelBuffers()
|
inputL, inputR := loadChannelBuffers()
|
||||||
nextInsertionPosition -= min(bufferSize, nextInsertionPosition)
|
nextInsertionPosition -= min(bufferSize, nextInsertionPosition)
|
||||||
|
const max = 1 << 16
|
||||||
for i := 0; i < bufferSize; i++ {
|
for i := 0; i < bufferSize; i++ {
|
||||||
// TODO: Use copyFromChannel?
|
// TODO: Use copyFromChannel?
|
||||||
if len(inputL) <= i {
|
if len(inputL) <= i {
|
||||||
@ -45,8 +46,8 @@ func initialize() {
|
|||||||
r.SetIndex(i, 0)
|
r.SetIndex(i, 0)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
l.SetIndex(i, inputL[i])
|
l.SetIndex(i, float64(inputL[i])/max)
|
||||||
r.SetIndex(i, inputR[i])
|
r.SetIndex(i, float64(inputR[i])/max)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
audioEnabled = true
|
audioEnabled = true
|
||||||
|
@ -22,20 +22,17 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/timshannon/go-openal/openal"
|
"github.com/timshannon/go-openal/openal"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func toBytes(l, r []float32) []byte {
|
func toBytes(l, r []int16) []byte {
|
||||||
if len(l) != len(r) {
|
if len(l) != len(r) {
|
||||||
panic("len(l) must equal to len(r)")
|
panic("len(l) must equal to len(r)")
|
||||||
}
|
}
|
||||||
b := &bytes.Buffer{}
|
b := &bytes.Buffer{}
|
||||||
for i, _ := range l {
|
for i, _ := range l {
|
||||||
l := int16(l[i] * math.MaxInt16)
|
if err := binary.Write(b, binary.LittleEndian, []int16{l[i], r[i]}); err != nil {
|
||||||
r := int16(r[i] * math.MaxInt16)
|
|
||||||
if err := binary.Write(b, binary.LittleEndian, []int16{l, r}); err != nil {
|
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user