// NewClient creates a new SuperCollider client. addr and port are the listening // address and port, respectively, of scsynth func NewClient(addr string, port int) (*Client, error) { self := new(Client) self.nextSynthID = 1000 var err error self.addr, err = net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", addr, port)) if err != nil { return nil, err } self.oscServer = osc.NewServer(listenAddr, listenPort) // OSC relays self.statusChan = make(chan *osc.Message) self.doneChan = make(chan *osc.Message) self.gqueryTreeChan = make(chan *osc.Message) // listen for OSC messages self.addOscHandlers() self.oscErrChan = make(chan error) go func() { self.oscErrChan <- self.oscServer.ListenAndDispatch() }() // wait for the server to start running err = <-self.oscServer.Listening if err != nil { return nil, err } // add default group err = self.addDefaultGroup() if err != nil { return nil, err } return self, nil }
// Send a /quit message to scsynth func main() { addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:57130") if err != nil { log.Fatal(err) } oscServer := osc.NewServer(listenAddr, listenPort) errChan := make(chan error) doneChan := make(chan *osc.Message) err = oscServer.AddMsgHandler("/done", func(msg *osc.Message) { doneChan <- msg }) if err != nil { log.Println("could not send quit message") log.Fatal(err) } go func() { errChan <- oscServer.ListenAndDispatch() }() err = <-oscServer.Listening if err != nil { log.Fatal(err) } log.Println("sending quit request") quitReq := osc.NewMessage("/quit") err = oscServer.SendTo(addr, quitReq) if err != nil { log.Fatal(err) } select { case quitResp := <-doneChan: osc.PrintMessage(quitResp) case err := <-errChan: log.Fatal(err) } }
// Connect connects to an scsynth instance via UDP. func (self *Client) Connect(addr string) error { var err error self.conn, err = net.ResolveUDPAddr("udp", addr) if err != nil { return err } self.oscServer = osc.NewServer(self.addr) // OSC relays self.statusChan = make(chan *osc.Message) self.doneChan = make(chan *osc.Message) self.gqueryTreeChan = make(chan *osc.Message) self.oscErrChan = make(chan error) // listen for OSC messages self.addOscHandlers() go func() { self.oscErrChan <- self.oscServer.ListenAndDispatch() }() // wait for the OSC server to start err = <-self.oscServer.Listening if err != nil { return err } return nil }