refactor: AudioSource is a func instead of single function interface

This avoids defining Processor altogether.
This commit is contained in:
5684185+vsariola@users.noreply.github.com
2024-11-02 19:50:20 +02:00
parent 3eb4d86d52
commit 2aa0aaee0c
4 changed files with 18 additions and 30 deletions

View File

@ -29,11 +29,9 @@ type (
Play(r AudioSource) CloserWaiter
}
// AudioSource is an interface for reading audio samples into an
// AudioBuffer. Returns error if the buffer is not filled.
AudioSource interface {
ReadAudio(buf AudioBuffer) error
}
// AudioSource is an function for reading audio samples into an AudioBuffer.
// Returns error if the buffer is not filled.
AudioSource func(buf AudioBuffer) error
BufferSource struct {
buffer AudioBuffer
@ -154,8 +152,15 @@ func (buffer AudioBuffer) Fill(synth Synth) error {
return nil
}
func (b AudioBuffer) Source() *BufferSource {
return &BufferSource{buffer: b}
func (b AudioBuffer) Source() AudioSource {
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.