refactor: rename sointu.Render as AudioBuffer.Fill

The Render name misleading as it did not do the same thing as normal
Synth.Render, because it disregarded time limits. Conceptually, as
the function modifies the state of the synth, it would be better to
be synth.Fill(audioBuffer), but we cannot define methods on
interfaces; therefore, it is audioBuffer.Fill(synth) now.
This commit is contained in:
5684185+vsariola@users.noreply.github.com 2023-10-19 11:11:42 +03:00
parent ff4155a08e
commit 5bbec75120
4 changed files with 22 additions and 23 deletions

View File

@ -3,6 +3,7 @@ package sointu
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"math"
)
@ -31,6 +32,19 @@ type (
}
)
// Fill fills the AudioBuffer using a Synth, disregarding all syncs and time
// limits. Note that this will change the state of the Synth.
func (buffer AudioBuffer) Fill(synth Synth) error {
s, _, err := synth.Render(buffer, math.MaxInt32)
if err != nil {
return fmt.Errorf("synth.Render failed: %v", err)
}
if s != len(buffer) {
return errors.New("in AudioBuffer.Fill, synth.Render should have filled the whole buffer but did not")
}
return nil
}
// Wav converts an AudioBuffer into a valid WAV-file, returned as a []byte
// array.
//

View File

@ -1,9 +1,7 @@
package sointu
import (
"errors"
"fmt"
"math"
)
type (
@ -40,19 +38,6 @@ type (
}
)
// Render fills an stereo audio buffer using a Synth, disregarding all syncs and
// time limits.
func Render(synth Synth, buffer AudioBuffer) error {
s, _, err := synth.Render(buffer, math.MaxInt32)
if err != nil {
return fmt.Errorf("sointu.Render failed: %v", err)
}
if s != len(buffer) {
return errors.New("in sointu.Render, synth.Render should have filled the whole buffer but did not")
}
return nil
}
// Play plays the Song by first compiling the patch with the given Synther,
// returning the stereo audio buffer as a result (and possible errors).
func Play(synther Synther, song Song) (AudioBuffer, error) {

View File

@ -60,12 +60,12 @@ func TestRenderSamples(t *testing.T) {
}
synth.Trigger(0, 64)
buffer := make(sointu.AudioBuffer, su_max_samples)
err = sointu.Render(synth, buffer[:len(buffer)/2])
err = buffer[:len(buffer)/2].Fill(synth)
if err != nil {
t.Fatalf("first render gave an error")
}
synth.Release(0)
err = sointu.Render(synth, buffer[len(buffer)/2:])
err = buffer[len(buffer)/2:].Fill(synth)
if err != nil {
t.Fatalf("first render gave an error")
}
@ -135,7 +135,7 @@ func TestStackUnderflow(t *testing.T) {
t.Fatalf("bridge compile error: %v", err)
}
buffer := make(sointu.AudioBuffer, 1)
err = sointu.Render(synth, buffer)
err = buffer.Fill(synth)
if err == nil {
t.Fatalf("rendering should have failed due to stack underflow")
}
@ -151,7 +151,7 @@ func TestStackBalancing(t *testing.T) {
t.Fatalf("bridge compile error: %v", err)
}
buffer := make(sointu.AudioBuffer, 1)
err = sointu.Render(synth, buffer)
err = buffer.Fill(synth)
if err == nil {
t.Fatalf("rendering should have failed due to unbalanced stack push/pop")
}
@ -184,7 +184,7 @@ func TestStackOverflow(t *testing.T) {
t.Fatalf("bridge compile error: %v", err)
}
buffer := make(sointu.AudioBuffer, 1)
err = sointu.Render(synth, buffer)
err = buffer.Fill(synth)
if err == nil {
t.Fatalf("rendering should have failed due to stack overflow, despite balanced push/pops")
}
@ -201,7 +201,7 @@ func TestDivideByZero(t *testing.T) {
t.Fatalf("bridge compile error: %v", err)
}
buffer := make(sointu.AudioBuffer, 1)
err = sointu.Render(synth, buffer)
err = buffer.Fill(synth)
if err == nil {
t.Fatalf("rendering should have failed due to divide by zero")
}

View File

@ -83,7 +83,7 @@ func TestStackUnderflow(t *testing.T) {
t.Fatalf("bridge compile error: %v", err)
}
buffer := make(sointu.AudioBuffer, 1)
err = sointu.Render(synth, buffer)
err = buffer.Fill(synth)
if err == nil {
t.Fatalf("rendering should have failed due to stack underflow")
}
@ -99,7 +99,7 @@ func TestStackBalancing(t *testing.T) {
t.Fatalf("bridge compile error: %v", err)
}
buffer := make(sointu.AudioBuffer, 1)
err = sointu.Render(synth, buffer)
err = buffer.Fill(synth)
if err == nil {
t.Fatalf("rendering should have failed due to unbalanced stack push/pop")
}