Skip to content

Commit 108b758

Browse files
committed
add a simple Server type to start/stop scsynth
1 parent f8222fd commit 108b758

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

server.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package sc
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
"strconv"
7+
)
8+
9+
// Server represents a running instance of scsynth.
10+
type Server struct {
11+
*exec.Cmd
12+
13+
Network string
14+
Port int
15+
}
16+
17+
// args gets the command line args to scsynth
18+
func (s *Server) args() ([]string, error) {
19+
args := []string{}
20+
21+
var (
22+
portArg = strconv.FormatInt(int64(s.Port), 10)
23+
)
24+
switch s.Network {
25+
default:
26+
return nil, fmt.Errorf("unrecognized network type: %s", s.Network)
27+
case "udp":
28+
args = append(args, "-u", portArg)
29+
case "tcp":
30+
args = append(args, "-t", portArg)
31+
}
32+
33+
return args, nil
34+
}
35+
36+
// Start starts a new instance of scsynth.
37+
func (s *Server) Start() error {
38+
args, err := s.args()
39+
if err != nil {
40+
return err
41+
}
42+
s.Cmd = exec.Command(ServerPath, args...)
43+
return s.Cmd.Start()
44+
}
45+
46+
// Stop stops a running server.
47+
func (s *Server) Stop() error {
48+
return s.Process.Kill()
49+
}

server_darwin.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// +build darwin
2+
package sc
3+
4+
const ServerPath = "/Applications/SuperCollider.app/Contents/Resources/scsynth"

0 commit comments

Comments
 (0)