mirror of
https://github.com/vsariola/sointu.git
synced 2025-05-28 03:10:24 -04:00
90 lines
2.1 KiB
Go
90 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"runtime/pprof"
|
|
|
|
"gioui.org/app"
|
|
"github.com/vsariola/sointu"
|
|
"github.com/vsariola/sointu/cmd"
|
|
"github.com/vsariola/sointu/oto"
|
|
"github.com/vsariola/sointu/tracker"
|
|
"github.com/vsariola/sointu/tracker/gioui"
|
|
"github.com/vsariola/sointu/tracker/gomidi"
|
|
)
|
|
|
|
type PlayerAudioSource struct {
|
|
*tracker.Player
|
|
playerProcessContext tracker.PlayerProcessContext
|
|
}
|
|
|
|
func (p *PlayerAudioSource) ReadAudio(buf sointu.AudioBuffer) error {
|
|
p.Player.Process(buf, p.playerProcessContext)
|
|
return nil
|
|
}
|
|
|
|
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
|
|
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
var f *os.File
|
|
if *cpuprofile != "" {
|
|
var err error
|
|
f, err = os.Create(*cpuprofile)
|
|
if err != nil {
|
|
log.Fatal("could not create CPU profile: ", err)
|
|
}
|
|
if err := pprof.StartCPUProfile(f); err != nil {
|
|
log.Fatal("could not start CPU profile: ", err)
|
|
}
|
|
}
|
|
audioContext, err := oto.NewContext()
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
recoveryFile := ""
|
|
if configDir, err := os.UserConfigDir(); err == nil {
|
|
recoveryFile = filepath.Join(configDir, "Sointu", "sointu-track-recovery")
|
|
}
|
|
model, player := tracker.NewModelPlayer(cmd.MainSynther, recoveryFile)
|
|
model.MIDI = gomidi.NewContext()
|
|
defer model.MIDI.Close()
|
|
if a := flag.Args(); len(a) > 0 {
|
|
f, err := os.Open(a[0])
|
|
if err == nil {
|
|
model.ReadSong(f)
|
|
}
|
|
f.Close()
|
|
}
|
|
tracker := gioui.NewTracker(model)
|
|
audioCloser := audioContext.Play(&PlayerAudioSource{player, model.MIDI})
|
|
go func() {
|
|
tracker.Main()
|
|
audioCloser.Close()
|
|
if *cpuprofile != "" {
|
|
pprof.StopCPUProfile()
|
|
f.Close()
|
|
}
|
|
if *memprofile != "" {
|
|
f, err := os.Create(*memprofile)
|
|
if err != nil {
|
|
log.Fatal("could not create memory profile: ", err)
|
|
}
|
|
defer f.Close() // error handling omitted for example
|
|
runtime.GC() // get up-to-date statistics
|
|
if err := pprof.WriteHeapProfile(f); err != nil {
|
|
log.Fatal("could not write memory profile: ", err)
|
|
}
|
|
}
|
|
os.Exit(0)
|
|
}()
|
|
app.Main()
|
|
}
|