mirror of
https://github.com/vsariola/sointu.git
synced 2025-05-28 03:10:24 -04:00
The old "native" compiler bridged version is now started with cmd/sointu-nativetrack, while the new pure-Go bytecode implemented bytecode interpreter is started with cmd/sointu-track Thus, you do not need any of the CMake / cgo stuff to run cmd/sointu-track
89 lines
2.4 KiB
Go
89 lines
2.4 KiB
Go
package sointu
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
)
|
|
|
|
type Synth interface {
|
|
Render(buffer []float32, maxtime int) (int, int, error)
|
|
Update(patch Patch) error
|
|
Trigger(voice int, note byte)
|
|
Release(voice int)
|
|
}
|
|
|
|
type SynthService interface {
|
|
Compile(patch Patch) (Synth, error)
|
|
}
|
|
|
|
func Render(synth Synth, buffer []float32) error {
|
|
s, _, err := synth.Render(buffer, math.MaxInt32)
|
|
if err != nil {
|
|
return fmt.Errorf("sointu.Render failed: %v", err)
|
|
}
|
|
if s != len(buffer)/2 {
|
|
return errors.New("in sointu.Render, synth.Render should have filled the whole buffer but did not")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Play(synth Synth, song Song) ([]float32, error) {
|
|
err := song.Validate()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
curVoices := make([]int, len(song.Score.Tracks))
|
|
for i := range curVoices {
|
|
curVoices[i] = song.Score.FirstVoiceForTrack(i)
|
|
}
|
|
initialCapacity := song.Score.LengthInRows() * song.SamplesPerRow() * 2
|
|
buffer := make([]float32, 0, initialCapacity)
|
|
rowbuffer := make([]float32, song.SamplesPerRow()*2)
|
|
for row := 0; row < song.Score.LengthInRows(); row++ {
|
|
patternRow := row % song.Score.RowsPerPattern
|
|
pattern := row / song.Score.RowsPerPattern
|
|
for t := range song.Score.Tracks {
|
|
order := song.Score.Tracks[t].Order
|
|
if pattern < 0 || pattern >= len(order) {
|
|
continue
|
|
}
|
|
patternIndex := song.Score.Tracks[t].Order[pattern]
|
|
patterns := song.Score.Tracks[t].Patterns
|
|
if patternIndex < 0 || int(patternIndex) >= len(patterns) {
|
|
continue
|
|
}
|
|
pattern := patterns[patternIndex]
|
|
if patternRow < 0 || patternRow >= len(pattern) {
|
|
continue
|
|
}
|
|
note := pattern[patternRow]
|
|
if note > 0 && note <= 1 { // anything but hold causes an action.
|
|
continue
|
|
}
|
|
synth.Release(curVoices[t])
|
|
if note > 1 {
|
|
curVoices[t]++
|
|
first := song.Score.FirstVoiceForTrack(t)
|
|
if curVoices[t] >= first+song.Score.Tracks[t].NumVoices {
|
|
curVoices[t] = first
|
|
}
|
|
synth.Trigger(curVoices[t], note)
|
|
}
|
|
}
|
|
tries := 0
|
|
for rowtime := 0; rowtime < song.SamplesPerRow(); {
|
|
samples, time, err := synth.Render(rowbuffer, song.SamplesPerRow()-rowtime)
|
|
if err != nil {
|
|
return buffer, fmt.Errorf("render failed: %v", err)
|
|
}
|
|
rowtime += time
|
|
buffer = append(buffer, rowbuffer[:samples*2]...)
|
|
if tries > 100 {
|
|
return nil, fmt.Errorf("Song speed modulation likely so slow that row never advances; error at pattern %v, row %v", pattern, patternRow)
|
|
}
|
|
}
|
|
}
|
|
return buffer, nil
|
|
}
|