예제 #1
0
파일: buffer.go 프로젝트: go-on/sc
// Gen generates a buffer using a routine.
// A runtime panic will occur if routine is not one of the
// BufferRoutine constants.
func (self *Buffer) Gen(routine string, flags int, args ...float32) error {
	if err := checkBufferRoutine(routine); err != nil {
		return err
	}
	if err := checkBufferGenFlags(flags); err != nil {
		return err
	}

	pat := bufferGenAddress
	gen, err := osc.NewMessage(pat)
	if err != nil {
		return err
	}
	if err := gen.WriteInt32(self.Num); err != nil {
		return err
	}
	if err := gen.WriteString(routine); err != nil {
		return err
	}
	if err := gen.WriteInt32(int32(flags)); err != nil {
		return err
	}
	for _, arg := range args {
		if err := gen.WriteFloat32(arg); err != nil {
			return err
		}
	}
	if err := self.client.oscConn.Send(gen); err != nil {
		return err
	}

	var done *osc.Message
	select {
	case done = <-self.client.doneChan:
	case err = <-self.client.oscErrChan:
		return err
	}

	if done.CountArguments() != 2 {
		return errors.New("expected two arguments to /done message")
	}
	_, err = done.ReadString()
	if err != nil {
		return err
	}
	bufnum, err := done.ReadInt32()
	if err != nil {
		return err
	}

	// TODO:
	// Don't error if we get a done message for a different buffer.
	// We should probably requeue this particular done message on doneChan.
	if bufnum != self.Num {
		m := "expected done message for buffer %d, but got one for buffer %d"
		return fmt.Errorf(m, self.Num, bufnum)
	}

	return nil
}
예제 #2
0
파일: group.go 프로젝트: go-on/sc
// parseGroup parses information about a group from a message
// received at /g_queryTree
// it *does not* recursively query for child groups
func parseGroup(msg *osc.Message) (*Group, error) {
	// return an error if msg.Address is not right
	if msg.Address() != gQueryTreeReply {
		return nil, fmt.Errorf("msg.Address should be %s, got %s", gQueryTreeReply, msg.Address())
	}
	// g_queryTree replies should have at least 3 arguments
	g, numArgs := new(Group), msg.CountArguments()
	if numArgs < 3 {
		return nil, fmt.Errorf("expected 3 arguments for message, got %d", numArgs)
	}
	// get the id of the group this reply is for
	nodeID, err := msg.ReadInt32()
	if err != nil {
		return nil, err
	}
	g.Node.id = nodeID

	// initialize the children array
	numChildren, err := msg.ReadInt32()
	if err != nil {
		return nil, err
	}
	if numChildren < 0 {
		return nil, fmt.Errorf("expected numChildren >= 0, got %d", numChildren)
	}
	g.children = make([]*Node, numChildren)
	// get the childrens' ids
	var numControls, numSubChildren int32
	for i := 3; i < numArgs; {
		nodeID, err = msg.ReadInt32()
		if err != nil {
			return nil, err
		}
		g.children[i-3] = &Node{nodeID}
		// get the number of children of this node
		// if -1 this is a synth, if >= 0 this is a group
		numSubChildren, err = msg.ReadInt32()
		if err != nil {
			return nil, err
		}
		if numSubChildren == -1 {
			// synth
			i += 3
			numControls, err = msg.ReadInt32()
			if err != nil {
				return nil, err
			}
			i += 1 + int(numControls*2)
		} else if numSubChildren >= 0 {
			// group
			i += 2
		}
	}
	return g, nil
}
예제 #3
0
파일: client.go 프로젝트: go-on/sc
// AllocBuffer allocates a buffer on the server
func (self *Client) AllocBuffer(frames, channels int) (*Buffer, error) {
	buf := newBuffer(self)
	pat := bufferAllocAddress
	alloc, err := osc.NewMessage(pat)
	if err != nil {
		return nil, err
	}
	if err := alloc.WriteInt32(buf.Num); err != nil {
		return nil, err
	}
	if err := alloc.WriteInt32(int32(frames)); err != nil {
		return nil, err
	}
	if err := alloc.WriteInt32(int32(channels)); err != nil {
		return nil, err
	}
	if err := self.oscConn.Send(alloc); err != nil {
		return nil, err
	}

	var done *osc.Message
	select {
	case done = <-self.doneChan:
		break
	case err = <-self.oscErrChan:
		return nil, err
	}

	// error if this message was not an ack of the synthdef
	if done.CountArguments() != 2 {
		return nil, fmt.Errorf("expected two arguments to /done message")
	}
	addr, err := done.ReadString()
	if err != nil {
		return nil, err
	}
	if addr != pat {
		return nil, fmt.Errorf("expected first argument to be %s but got %s", pat, addr)
	}
	bufnum, err := done.ReadInt32()
	if err != nil {
		return nil, err
	}
	// TODO:
	// Don't error if we get a done message for a different buffer.
	// We should probably requeue this particular done message on doneChan.
	if bufnum != buf.Num {
		m := "expected done message for buffer %d, but got one for buffer %d"
		return nil, fmt.Errorf(m, buf.Num, bufnum)
	}
	return buf, nil
}
예제 #4
0
파일: client.go 프로젝트: go-on/sc
// ReadBuffer tells the server to read an audio file and
// load it into a buffer
func (self *Client) ReadBuffer(path string, num int32) (*Buffer, error) {
	allocRead, err := osc.NewMessage(bufferReadAddress)
	if err != nil {
		return nil, err
	}

	buf := newReadBuffer(path, num, self)
	if err := allocRead.WriteInt32(buf.Num); err != nil {
		return nil, err
	}
	if err := allocRead.WriteString(path); err != nil {
		return nil, err
	}
	if err := self.oscConn.Send(allocRead); err != nil {
		return nil, err
	}

	var done *osc.Message
	select {
	case done = <-self.doneChan:
	case err = <-self.oscErrChan:
		return nil, err
	}

	// error if this message was not an ack of the buffer read
	if done.CountArguments() != 2 {
		return nil, fmt.Errorf("expected two arguments to /done message")
	}
	addr, err := done.ReadString()
	if err != nil {
		return nil, err
	}
	if addr != bufferReadAddress {
		return nil, fmt.Errorf("expected first argument to be %s but got %s", bufferReadAddress, addr)
	}
	bufnum, err := done.ReadInt32()
	if err != nil {
		return nil, err
	}
	// TODO:
	// Don't error if we get a done message for a different buffer.
	// We should probably requeue this particular done message on doneChan.
	if bufnum != buf.Num {
		m := "expected done message for buffer %d, but got one for buffer %d"
		return nil, fmt.Errorf(m, buf.Num, bufnum)
	}
	return buf, nil
}