Skip to content

Commit

Permalink
Sound (#8)
Browse files Browse the repository at this point in the history
* volumem ctl

* sound mizxer and volume
  • Loading branch information
laullon authored May 28, 2020
1 parent ae38edf commit 57b81ab
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 173 deletions.
Binary file added data/icons/volume-control-full.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 39 additions & 29 deletions emulator/ay8912/ay8912.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ type AY8912 interface {
}

type channel struct {
pitch uint16
volume uint16
pitch uint16
envelope bool
tone bool
noise bool
count uint16
output bool
out uint16
buff []*emulator.SoundData
mux sync.Mutex
ticks int
}

type envelope struct {
Expand All @@ -43,15 +47,10 @@ type ay8912 struct {
regs []byte
selectedReg byte

channels []*channel
envelope *envelope
noise *noise

volume uint16
maxvol float64
out []*emulator.SoundData
ticks int
mux sync.Mutex
channels []*channel
soundChannels []emulator.SoundChannel
envelope *envelope
noise *noise
}

var regMasks = []byte{0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0x1f, 0xff, 0x1f, 0x1f, 0x1f, 0xff, 0xff, 0x0f, 0xff}
Expand All @@ -64,7 +63,9 @@ func New() AY8912 {
}

for i := 0; i < 3; i++ {
ay.channels = append(ay.channels, &channel{})
ch := &channel{}
ay.channels = append(ay.channels, ch)
ay.soundChannels = append(ay.soundChannels, ch)
}

for env := 0; env < 16; env++ {
Expand Down Expand Up @@ -168,36 +169,45 @@ func (ay *ay8912) Tick() {
for _, channel := range ay.channels {
if (channel.output && channel.tone) || (ay.noise.output && channel.noise) {
if channel.envelope {
ay.volume += ay.envelope.volume[ay.envelope.shape][ay.envelope.pos]
channel.out += ay.envelope.volume[ay.envelope.shape][ay.envelope.pos]
} else {
ay.volume += channel.volume
channel.out += channel.volume
}
}
channel.ticks++
}

ay.ticks++
}

func (ay *ay8912) SoundTick() {
ay.mux.Lock()
defer ay.mux.Unlock()
v := float64(ay.volume) / float64(16*3*ay.ticks)
ay.out = append(ay.out, &emulator.SoundData{L: v, R: v})
ay.ticks = 0
ay.volume = 0
for _, channel := range ay.channels {
channel.soundTick()
}
}

func (ay *ay8912) GetChannels() []emulator.SoundChannel {
return ay.soundChannels
}

func (ch *channel) soundTick() {
ch.mux.Lock()
defer ch.mux.Unlock()
v := float64(ch.out) / float64(16*ch.ticks)
ch.buff = append(ch.buff, &emulator.SoundData{L: v, R: v})
ch.ticks = 0
ch.out = 0
}

func (ay *ay8912) GetBuffer(max int) (res []*emulator.SoundData, l int) {
ay.mux.Lock()
defer ay.mux.Unlock()
func (ch *channel) GetBuffer(max int) (res []*emulator.SoundData, l int) {
ch.mux.Lock()
defer ch.mux.Unlock()

if len(ay.out) > max {
res = ay.out[:max]
ay.out = ay.out[max:]
if len(ch.buff) > max {
res = ch.buff[:max]
ch.buff = ch.buff[max:]
l = max
} else {
res = ay.out
ay.out = nil
res = ch.buff
ch.buff = nil
l = len(res)
}
return
Expand Down
72 changes: 36 additions & 36 deletions emulator/sound.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/faiface/beep"
"github.com/faiface/beep/effects"
"github.com/faiface/beep/speaker"
)

Expand All @@ -15,6 +16,10 @@ type SoundSystem interface {

type SoundSource interface {
SoundTick()
GetChannels() []SoundChannel
}

type SoundChannel interface {
GetBuffer(int) ([]*SoundData, int)
}

Expand All @@ -24,20 +29,37 @@ type SoundData struct {

type soundSystem struct {
sources []SoundSource
volume float64
stream *beep.Mixer
volume *effects.Volume
}

type channel struct {
source SoundChannel
}

func NewSoundSystem(sampleRate int) SoundSystem {
ss := &soundSystem{volume: 100}
ss := &soundSystem{}

sr := beep.SampleRate(sampleRate)
speaker.Init(sr, sr.N(time.Second/100))
speaker.Play(ss)
speaker.Init(sr, sr.N(time.Second/30))
ss.stream = &beep.Mixer{}

ss.volume = &effects.Volume{
Streamer: ss.stream,
Base: 2,
Volume: -7,
}

speaker.Play(ss.volume)

return ss
}

func (ss *soundSystem) AddSource(source SoundSource) {
channels := source.GetChannels()
for _, ch := range channels {
ss.stream.Add(&channel{ch})
}
ss.sources = append(ss.sources, source)
}

Expand All @@ -48,42 +70,20 @@ func (ss *soundSystem) Tick() {
}

func (ss *soundSystem) SetVolume(volume float64) {
ss.volume = 100 - volume
ss.volume.Volume = -3 - ((1 - volume) * 4)
ss.volume.Silent = volume == 0
println(volume, ss.volume.Volume)
}

func (ss *soundSystem) Stream(samples [][2]float64) (int, bool) {
n := 0
for _, source := range ss.sources {
buff, len := source.GetBuffer(len(samples))
n = max(n, len)
for i, data := range buff {
samples[i][0] += data.L
samples[i][1] += data.R
}
}

if n == 0 {
n = len(samples)
}

for i := 0; i < n; i++ {
samples[i][0] /= float64(len(ss.sources))
samples[i][1] /= float64(len(ss.sources))

samples[i][0] /= ss.volume
samples[i][1] /= ss.volume
func (ch *channel) Stream(samples [][2]float64) (int, bool) {
buff, _ := ch.source.GetBuffer(len(samples))
for i, data := range buff {
samples[i][0] = data.L
samples[i][1] = data.R
}

return n, true
return len(samples), true
}

func (ss *soundSystem) Err() error {
func (ss *channel) Err() error {
return nil
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
11 changes: 4 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ go 1.13
require (
fyne.io/fyne v1.2.4
github.com/faiface/beep v1.0.2
github.com/go-gl/glfw v0.0.0-20191125211704-12ad95a8df72
github.com/hajimehoshi/oto v0.5.3 // indirect
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709
github.com/therecipe/qt v0.0.0-20200126204426-5074eb6d8c41 // indirect
golang.org/x/image v0.0.0-20190802002840-cff245a6509b
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect
honnef.co/go/tools v0.0.1-2019.2.3 // indirect
github.com/go-gl/glfw v0.0.0-20200420212212-258d9bec320e
github.com/stretchr/testify v1.5.1
golang.org/x/image v0.0.0-20200430140353-33d19683fad8
golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2 // indirect
)

replace fyne.io/fyne => github.com/laullon/fyne v1.2.4-0.20200325171900-5acf734371e2
Loading

0 comments on commit 57b81ab

Please sign in to comment.