mirror of
https://github.com/vsariola/sointu.git
synced 2025-05-25 18:00:37 -04:00
refactor: AudioSource is a func instead of single function interface
This avoids defining Processor altogether.
This commit is contained in:
parent
3eb4d86d52
commit
2aa0aaee0c
19
audio.go
19
audio.go
@ -29,11 +29,9 @@ type (
|
|||||||
Play(r AudioSource) CloserWaiter
|
Play(r AudioSource) CloserWaiter
|
||||||
}
|
}
|
||||||
|
|
||||||
// AudioSource is an interface for reading audio samples into an
|
// AudioSource is an function for reading audio samples into an AudioBuffer.
|
||||||
// AudioBuffer. Returns error if the buffer is not filled.
|
// Returns error if the buffer is not filled.
|
||||||
AudioSource interface {
|
AudioSource func(buf AudioBuffer) error
|
||||||
ReadAudio(buf AudioBuffer) error
|
|
||||||
}
|
|
||||||
|
|
||||||
BufferSource struct {
|
BufferSource struct {
|
||||||
buffer AudioBuffer
|
buffer AudioBuffer
|
||||||
@ -154,8 +152,15 @@ func (buffer AudioBuffer) Fill(synth Synth) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b AudioBuffer) Source() *BufferSource {
|
func (b AudioBuffer) Source() AudioSource {
|
||||||
return &BufferSource{buffer: b}
|
return func(buf AudioBuffer) error {
|
||||||
|
n := copy(buf, b)
|
||||||
|
b = b[n:]
|
||||||
|
if n < len(buf) {
|
||||||
|
return io.EOF
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadAudio reads audio samples from an AudioSource into an AudioBuffer.
|
// ReadAudio reads audio samples from an AudioSource into an AudioBuffer.
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"runtime/pprof"
|
"runtime/pprof"
|
||||||
|
|
||||||
"gioui.org/app"
|
"gioui.org/app"
|
||||||
|
"github.com/vsariola/sointu"
|
||||||
"github.com/vsariola/sointu/cmd"
|
"github.com/vsariola/sointu/cmd"
|
||||||
"github.com/vsariola/sointu/oto"
|
"github.com/vsariola/sointu/oto"
|
||||||
"github.com/vsariola/sointu/tracker"
|
"github.com/vsariola/sointu/tracker"
|
||||||
@ -61,8 +62,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
trackerUi := gioui.NewTracker(model)
|
trackerUi := gioui.NewTracker(model)
|
||||||
processor := tracker.NewProcessor(player, midiContext, trackerUi)
|
audioCloser := audioContext.Play(func(buf sointu.AudioBuffer) error {
|
||||||
audioCloser := audioContext.Play(processor)
|
player.Process(buf, midiContext, trackerUi)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
trackerUi.Main()
|
trackerUi.Main()
|
||||||
|
@ -77,7 +77,7 @@ func (o *OtoReader) Read(b []byte) (n int, err error) {
|
|||||||
} else if samples < len(o.tmpBuffer) {
|
} else if samples < len(o.tmpBuffer) {
|
||||||
o.tmpBuffer = o.tmpBuffer[:samples]
|
o.tmpBuffer = o.tmpBuffer[:samples]
|
||||||
}
|
}
|
||||||
err = o.audioSource.ReadAudio(o.tmpBuffer)
|
err = o.audioSource(o.tmpBuffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return o.closeWithError(err)
|
return o.closeWithError(err)
|
||||||
}
|
}
|
||||||
|
@ -1,20 +0,0 @@
|
|||||||
package tracker
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/vsariola/sointu"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Processor struct {
|
|
||||||
*Player
|
|
||||||
playerProcessContext PlayerProcessContext
|
|
||||||
uiProcessor EventProcessor
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewProcessor(player *Player, context PlayerProcessContext, uiProcessor EventProcessor) *Processor {
|
|
||||||
return &Processor{player, context, uiProcessor}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Processor) ReadAudio(buf sointu.AudioBuffer) error {
|
|
||||||
p.Player.Process(buf, p.playerProcessContext, p.uiProcessor)
|
|
||||||
return nil
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user