feat!: implement vsti, along with various refactorings and api changes for it

The RPC and sync library mechanisms were removed for now; they never really worked and contained several obvious bugs. Need to consider if syncs are useful at all during the compose time, or just used during intro.
This commit is contained in:
5684185+vsariola@users.noreply.github.com
2023-05-09 11:24:49 +03:00
parent 70080c2b9d
commit cd700ed954
34 changed files with 1210 additions and 750 deletions

View File

@ -0,0 +1,7 @@
//go:build native
package cmd
import "github.com/vsariola/sointu/vm/compiler/bridge"
var DefaultService = bridge.BridgeService{}

View File

@ -0,0 +1,7 @@
//go:build !native
package cmd
import "github.com/vsariola/sointu/vm"
var DefaultService = vm.SynthService{}

View File

@ -1,23 +0,0 @@
package main
import (
"fmt"
"os"
"github.com/vsariola/sointu/oto"
"github.com/vsariola/sointu/tracker/gioui"
"github.com/vsariola/sointu/vm/compiler/bridge"
)
func main() {
audioContext, err := oto.NewContext()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer audioContext.Close()
synthService := bridge.BridgeService{}
// TODO: native track does not support syncing at the moment (which is why
// we pass nil), as the native bridge does not support sync data
gioui.Main(audioContext, synthService, nil)
}

View File

@ -88,7 +88,7 @@ func main() {
return fmt.Errorf("the song could not be parsed as .json (%v) or .yml (%v)", errJSON, errYaml)
}
}
buffer, _, err := sointu.Play(bridge.BridgeService{}, song, !*unreleased) // render the song to calculate its length
buffer, err := sointu.Play(bridge.BridgeService{}, song, !*unreleased) // render the song to calculate its length
if err != nil {
return fmt.Errorf("sointu.Play failed: %v", err)
}

View File

@ -5,14 +5,25 @@ import (
"fmt"
"os"
"gioui.org/app"
"github.com/vsariola/sointu/cmd"
"github.com/vsariola/sointu/oto"
"github.com/vsariola/sointu/rpc"
"github.com/vsariola/sointu/tracker"
"github.com/vsariola/sointu/tracker/gioui"
"github.com/vsariola/sointu/vm"
)
type NullContext struct {
}
func (NullContext) NextEvent() (event tracker.PlayerProcessEvent, ok bool) {
return tracker.PlayerProcessEvent{}, false
}
func (NullContext) BPM() (bpm float64, ok bool) {
return 0, false
}
func main() {
syncAddress := flag.String("address", "", "remote RPC server where to send sync data")
flag.Parse()
audioContext, err := oto.NewContext()
if err != nil {
@ -20,14 +31,24 @@ func main() {
os.Exit(1)
}
defer audioContext.Close()
var syncChannel chan<- []float32
if *syncAddress != "" {
syncChannel, err = rpc.Sender(*syncAddress)
if err != nil {
fmt.Println(err)
os.Exit(1)
modelMessages := make(chan interface{}, 1024)
playerMessages := make(chan tracker.PlayerMessage, 1024)
model := tracker.NewModel(modelMessages, playerMessages)
player := tracker.NewPlayer(cmd.DefaultService, playerMessages, modelMessages)
tracker := gioui.NewTracker(model, cmd.DefaultService)
output := audioContext.Output()
defer output.Close()
go func() {
buf := make([]float32, 2048)
ctx := NullContext{}
for {
player.Process(buf, ctx)
output.WriteAudio(buf)
}
}
synthService := vm.SynthService{}
gioui.Main(audioContext, synthService, syncChannel)
}()
go func() {
tracker.Main()
os.Exit(0)
}()
app.Main()
}

104
cmd/sointu-vsti/main.go Normal file
View File

@ -0,0 +1,104 @@
//go:build plugin
package main
import (
"github.com/vsariola/sointu/cmd"
"github.com/vsariola/sointu/tracker"
"github.com/vsariola/sointu/tracker/gioui"
"pipelined.dev/audio/vst2"
)
type VSTIProcessContext struct {
events []vst2.MIDIEvent
host vst2.Host
}
func (c *VSTIProcessContext) NextEvent() (event tracker.PlayerProcessEvent, ok bool) {
var ev vst2.MIDIEvent
for len(c.events) > 0 {
ev, c.events = c.events[0], c.events[1:]
switch {
case ev.Data[0] >= 0x80 && ev.Data[0] < 0x90:
channel := ev.Data[0] - 0x80
note := ev.Data[1]
return tracker.PlayerProcessEvent{Frame: int(ev.DeltaFrames), On: false, Channel: int(channel), Note: note}, true
case ev.Data[0] >= 0x90 && ev.Data[0] < 0xA0:
channel := ev.Data[0] - 0x90
note := ev.Data[1]
return tracker.PlayerProcessEvent{Frame: int(ev.DeltaFrames), On: true, Channel: int(channel), Note: note}, true
default:
// ignore all other MIDI messages
}
}
return tracker.PlayerProcessEvent{}, false
}
func (c *VSTIProcessContext) BPM() (bpm float64, ok bool) {
timeInfo := c.host.GetTimeInfo()
return timeInfo.Tempo, true
}
func init() {
var (
uniqueID = [4]byte{'S', 'n', 't', 'u'}
version = int32(100)
)
vst2.PluginAllocator = func(h vst2.Host) (vst2.Plugin, vst2.Dispatcher) {
modelMessages := make(chan interface{}, 1024)
playerMessages := make(chan tracker.PlayerMessage, 1024)
model := tracker.NewModel(modelMessages, playerMessages)
player := tracker.NewPlayer(cmd.DefaultService, playerMessages, modelMessages)
tracker := gioui.NewTracker(model, cmd.DefaultService)
tracker.SetInstrEnlarged(true) // start the vsti with the instrument editor enlarged
go tracker.Main()
context := VSTIProcessContext{make([]vst2.MIDIEvent, 100), h}
buf := make([]float32, 2048)
return vst2.Plugin{
UniqueID: uniqueID,
Version: version,
InputChannels: 0,
OutputChannels: 2,
Name: "Sointu",
Vendor: "vsariola/sointu",
Category: vst2.PluginCategorySynth,
Flags: vst2.PluginIsSynth,
ProcessFloatFunc: func(in, out vst2.FloatBuffer) {
left := out.Channel(0)
right := out.Channel(1)
if len(buf) < out.Frames*2 {
buf = append(buf, make([]float32, out.Frames*2-len(buf))...)
}
buf = buf[:out.Frames*2]
player.Process(buf, &context)
for i := 0; i < out.Frames; i++ {
left[i], right[i] = buf[i*2], buf[i*2+1]
}
context.events = context.events[:0]
},
}, vst2.Dispatcher{
CanDoFunc: func(pcds vst2.PluginCanDoString) vst2.CanDoResponse {
switch pcds {
case vst2.PluginCanReceiveEvents, vst2.PluginCanReceiveMIDIEvent, vst2.PluginCanReceiveTimeInfo:
return vst2.YesCanDo
}
return vst2.NoCanDo
},
ProcessEventsFunc: func(ev *vst2.EventsPtr) {
for i := 0; i < ev.NumEvents(); i++ {
a := ev.Event(i)
switch v := a.(type) {
case *vst2.MIDIEvent:
context.events = append(context.events, *v)
}
}
},
CloseFunc: func() {
tracker.Quit(true)
},
}
}
}
func main() {}