mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
examples/realtimepcm: refactoring
This commit is contained in:
parent
ce6931f81c
commit
fa457f67cd
@ -42,10 +42,8 @@ type SineWave struct {
|
|||||||
minFrequency int
|
minFrequency int
|
||||||
maxFrequency int
|
maxFrequency int
|
||||||
|
|
||||||
// position is the position in the wave length in the range of [0, 1).
|
// pos is the position in the wave length in the range of [0, 1).
|
||||||
position float64
|
pos float64
|
||||||
|
|
||||||
remaining []byte
|
|
||||||
|
|
||||||
m sync.Mutex
|
m sync.Mutex
|
||||||
}
|
}
|
||||||
@ -77,20 +75,11 @@ func (s *SineWave) Read(buf []byte) (int, error) {
|
|||||||
s.m.Lock()
|
s.m.Lock()
|
||||||
defer s.m.Unlock()
|
defer s.m.Unlock()
|
||||||
|
|
||||||
if len(s.remaining) > 0 {
|
n := len(buf) / 4 * 4
|
||||||
n := copy(buf, s.remaining)
|
buf = buf[:n]
|
||||||
s.remaining = s.remaining[n:]
|
|
||||||
return n, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var origBuf []byte
|
|
||||||
if len(buf)%4 > 0 {
|
|
||||||
origBuf = buf
|
|
||||||
buf = make([]byte, len(origBuf)+4-len(origBuf)%4)
|
|
||||||
}
|
|
||||||
|
|
||||||
length := sampleRate / float64(s.frequency)
|
length := sampleRate / float64(s.frequency)
|
||||||
p := int64(length * s.position)
|
p := int64(length * s.pos)
|
||||||
for i := 0; i < len(buf)/4; i++ {
|
for i := 0; i < len(buf)/4; i++ {
|
||||||
const max = 32767
|
const max = 32767
|
||||||
b := int16(math.Sin(2*math.Pi*float64(p)/float64(length)) * max)
|
b := int16(math.Sin(2*math.Pi*float64(p)/float64(length)) * max)
|
||||||
@ -101,15 +90,10 @@ func (s *SineWave) Read(buf []byte) (int, error) {
|
|||||||
p++
|
p++
|
||||||
}
|
}
|
||||||
|
|
||||||
s.position = float64(p) / float64(length)
|
s.pos = float64(p) / float64(length)
|
||||||
s.position = s.position - math.Floor(s.position)
|
s.pos = s.pos - math.Floor(s.pos)
|
||||||
|
|
||||||
if origBuf != nil {
|
|
||||||
n := copy(origBuf, buf)
|
|
||||||
s.remaining = buf[n:]
|
|
||||||
return n, nil
|
return n, nil
|
||||||
}
|
|
||||||
return len(buf), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGame() *Game {
|
func NewGame() *Game {
|
||||||
|
Loading…
Reference in New Issue
Block a user