mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
audio: Reduce magic numbers
This commit is contained in:
parent
8c065a28e1
commit
305016f636
@ -21,7 +21,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: In JavaScript, mixing should be done by WebAudio for performance.
|
|
||||||
type mixedPlayersStream struct {
|
type mixedPlayersStream struct {
|
||||||
context *Context
|
context *Context
|
||||||
writtenBytes int
|
writtenBytes int
|
||||||
@ -34,12 +33,18 @@ func min(a, b int) int {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
channelNum = 2
|
||||||
|
bytesPerSample = 2
|
||||||
|
bitsPerSample = bytesPerSample * 8
|
||||||
|
)
|
||||||
|
|
||||||
func (s *mixedPlayersStream) Read(b []byte) (int, error) {
|
func (s *mixedPlayersStream) Read(b []byte) (int, error) {
|
||||||
s.context.Lock()
|
s.context.Lock()
|
||||||
defer s.context.Unlock()
|
defer s.context.Unlock()
|
||||||
|
|
||||||
// TODO: 60 (FPS) is a magic number
|
// TODO: 60 (FPS) is a magic number
|
||||||
bytesPerFrame := s.context.sampleRate * 4 / 60
|
bytesPerFrame := s.context.sampleRate * bytesPerSample * channelNum / 60
|
||||||
x := s.context.frames*bytesPerFrame + len(b)
|
x := s.context.frames*bytesPerFrame + len(b)
|
||||||
if x <= s.writtenBytes {
|
if x <= s.writtenBytes {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
|
@ -75,9 +75,7 @@ func max64(a, b int64) int64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *player) proceed() error {
|
func (p *player) proceed() error {
|
||||||
const channelNum = 2
|
bufferSize := p.sampleRate * bytesPerSample * channelNum / 60
|
||||||
const bytesPerSample = channelNum * 16 / 8
|
|
||||||
bufferSize := p.sampleRate * bytesPerSample / 60
|
|
||||||
c := int64(p.context.Get("currentTime").Float() * float64(p.sampleRate))
|
c := int64(p.context.Get("currentTime").Float() * float64(p.sampleRate))
|
||||||
if p.positionInSamples < c {
|
if p.positionInSamples < c {
|
||||||
p.positionInSamples = c
|
p.positionInSamples = c
|
||||||
@ -85,7 +83,7 @@ func (p *player) proceed() error {
|
|||||||
b := make([]byte, bufferSize)
|
b := make([]byte, bufferSize)
|
||||||
n, err := p.src.Read(b)
|
n, err := p.src.Read(b)
|
||||||
if 0 < n {
|
if 0 < n {
|
||||||
buf := p.context.Call("createBuffer", channelNum, n/bytesPerSample, p.sampleRate)
|
buf := p.context.Call("createBuffer", channelNum, n/bytesPerSample/channelNum, p.sampleRate)
|
||||||
l := buf.Call("getChannelData", 0)
|
l := buf.Call("getChannelData", 0)
|
||||||
r := buf.Call("getChannelData", 1)
|
r := buf.Call("getChannelData", 1)
|
||||||
il, ir := toLR(b[:n])
|
il, ir := toLR(b[:n])
|
||||||
|
@ -78,12 +78,10 @@ type player struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func startPlaying(src io.Reader, sampleRate int) (*player, error) {
|
func startPlaying(src io.Reader, sampleRate int) (*player, error) {
|
||||||
const numChannels = 2
|
const numBlockAlign = channelNum * bitsPerSample / 8
|
||||||
const bitsPerSample = 16
|
|
||||||
const numBlockAlign = numChannels * bitsPerSample / 8
|
|
||||||
f := C.WAVEFORMATEX{
|
f := C.WAVEFORMATEX{
|
||||||
wFormatTag: C.WAVE_FORMAT_PCM,
|
wFormatTag: C.WAVE_FORMAT_PCM,
|
||||||
nChannels: numChannels,
|
nChannels: channelNum,
|
||||||
nSamplesPerSec: C.DWORD(sampleRate),
|
nSamplesPerSec: C.DWORD(sampleRate),
|
||||||
nAvgBytesPerSec: C.DWORD(sampleRate) * numBlockAlign,
|
nAvgBytesPerSec: C.DWORD(sampleRate) * numBlockAlign,
|
||||||
wBitsPerSample: bitsPerSample,
|
wBitsPerSample: bitsPerSample,
|
||||||
|
Loading…
Reference in New Issue
Block a user