mirror of
https://github.com/freewilll/apple2-go.git
synced 2024-11-17 18:07:38 +00:00
84 lines
1.4 KiB
Go
84 lines
1.4 KiB
Go
package audio
|
|
|
|
import (
|
|
"errors"
|
|
|
|
ebiten_audio "github.com/hajimehoshi/ebiten/audio"
|
|
|
|
"apple2/system"
|
|
)
|
|
|
|
var (
|
|
audioContext *ebiten_audio.Context
|
|
player *ebiten_audio.Player
|
|
firstAudio bool
|
|
Mute bool
|
|
ClickWhenDriveHeadMoves bool
|
|
)
|
|
|
|
type stream struct{}
|
|
|
|
func (s *stream) Read(data []byte) (int, error) {
|
|
dataLen := len(data)
|
|
|
|
if firstAudio {
|
|
// The first time, drain the audio queue
|
|
firstAudio = false
|
|
|
|
for i := 0; i < len(system.AudioChannel); i++ {
|
|
<-system.AudioChannel
|
|
}
|
|
return dataLen, nil
|
|
}
|
|
|
|
if dataLen%4 != 0 {
|
|
return 0, errors.New("dataLen % 4 must be 0")
|
|
}
|
|
|
|
if Mute {
|
|
return dataLen, nil
|
|
}
|
|
|
|
samples := dataLen / 4
|
|
|
|
for i := 0; i < dataLen; i++ {
|
|
data[i] = 0
|
|
}
|
|
|
|
for i := 0; i < samples; i++ {
|
|
b := <-system.AudioChannel
|
|
|
|
data[4*i] = byte(b)
|
|
data[4*i+1] = byte(b >> 8)
|
|
data[4*i+2] = byte(b)
|
|
data[4*i+3] = byte(b >> 8)
|
|
}
|
|
|
|
return dataLen, nil
|
|
}
|
|
|
|
func (s *stream) Close() error {
|
|
return nil
|
|
}
|
|
|
|
func InitEbiten() {
|
|
firstAudio = true
|
|
Mute = false
|
|
ClickWhenDriveHeadMoves = false
|
|
|
|
var err error
|
|
audioContext, err = ebiten_audio.NewContext(system.AudioSampleRate)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// Pass the (infinite) stream to audio.NewPlayer.
|
|
// After calling Play, the stream never ends as long as the player object lives.
|
|
// var err error
|
|
player, err = ebiten_audio.NewPlayer(audioContext, &stream{})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
player.Play()
|
|
}
|