-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.go
114 lines (97 loc) · 3.04 KB
/
engine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package hypatia
//go:generate go-bindata -pkg hypatia -o assets.go main.csd
import (
"github.com/spf13/viper"
"io"
"os"
"os/exec"
"strings"
)
const (
configFileName = "hypatia-config.yaml"
csoundFileName = "main.csd"
oscAddress = "/score" // <-- unused now
)
// the running csound instance and an OSC client into it
type Engine struct {
stdinPipe io.WriteCloser
Command *exec.Cmd
}
// boots up csound in the current working directory,
// with configuration provided by a config file (should it exist)
// defaults are used if the config doesn't exist
// returns the engine
func New() (*Engine, error) {
// read config file (if it exists)
// otherwise use defaults (which are set below)
v := viper.New()
v.AddConfigPath(".")
v.SetConfigFile(configFileName)
v.SetConfigType("yaml")
v.ReadInConfig() // if it errors, it doesn't matter (we have defaults)
v.SetDefault("input", "adc")
v.SetDefault("output", "dac")
v.SetDefault("sample-rate", "44100")
v.SetDefault("ksmps", "128")
v.SetDefault("number-of-parts", "16")
v.SetDefault("number-of-fx-sends", "2")
v.SetDefault("osc-listen-port", "0") // <-- 0 means no OSC listening
// v.SetDefault("logfile", "null")
v.SetDefault("other-flags", "") // <-- option to add further csound flags
// convert values read from the config file into csound flags
flags := []string{
"--input=" + v.GetString("input"),
"--output=" + v.GetString("output"),
"--sample-rate=" + v.GetString("sample-rate"),
"--ksmps=" + v.GetString("ksmps"),
"--omacro:NUMBER_OF_PARTS=" + v.GetString("number-of-parts"),
"--omacro:NUMBER_OF_FX_SENDS=" + v.GetString("number-of-fx-sends"),
"--omacro:OSC_LISTEN_PORT=" + v.GetString("osc-listen-port"),
// "--logfile=" + v.GetString("logfile"),
}
if v.GetString("other-flags") != "" {
flags = append(flags, v.GetString("other-flags"))
}
// create OSC client
// c := osc.NewClient("localhost", v.GetInt("osc-listen-port"))
// get the csound csd file asset
// create a temp file with the csound csd file asset data
// the temp file is removed in func Close() beneath
err := RestoreAsset("./", csoundFileName)
if err != nil {
return nil, err
}
// create arguments for csound
// args := append([]string{csoundFileName}, flags...)
args := append([]string{csoundFileName, "-Lstdin"}, flags...)
// create the csound command
// which utilizes the recently created csound csd temp file and flags
p := exec.Command("csound", args...)
in, err := p.StdinPipe()
if err != nil {
return nil, err
}
// return the "engine"
return &Engine{
stdinPipe: in,
Command: p,
}, nil
}
func (e *Engine) Start() error {
// start the command
return e.Command.Start()
}
// closes the underlying csound process
func (e *Engine) Close() error {
// remove the temp csound file
os.Remove(csoundFileName)
// close stdin
e.stdinPipe.Close()
// end the csound process
return e.Command.Process.Kill()
}
// sends csound score data (strings) into the running csound instance
func (e *Engine) Perform(scoreLines ...string) {
io.WriteString(e.stdinPipe,
strings.Join(scoreLines, ""))
}