Ejemplo n.º 1
0
func main() {
	const synthName = "DelayCExample"

	client, err := sc.NewClient("udp", "127.0.0.1:57112", "127.0.0.1:57110", 5*time.Second)
	if err != nil {
		log.Fatal(err)
	}
	defaultGroup, err := client.AddDefaultGroup()
	if err != nil {
		log.Fatal(err)
	}
	def := sc.NewSynthdef(synthName, func(p sc.Params) sc.Ugen {
		bus, dust := sc.C(0), sc.Dust{Density: sc.C(1)}.Rate(sc.AR).Mul(sc.C(0.5))
		noise := sc.WhiteNoise{}.Rate(sc.AR)
		decay := sc.Decay{In: dust, Decay: sc.C(0.3)}.Rate(sc.AR).Mul(noise)
		sig := sc.Delay{
			Interpolation: sc.InterpolationCubic,
			In:            decay,
			MaxDelayTime:  sc.C(0.2),
			DelayTime:     sc.C(0.2),
		}.Rate(sc.AR).Add(decay)
		return sc.Out{bus, sig}.Rate(sc.AR)
	})
	if err := client.SendDef(def); err != nil {
		log.Fatal(err)
	}

	synthID := client.NextSynthID()
	if _, err = defaultGroup.Synth(synthName, synthID, sc.AddToTail, nil); err != nil {
		log.Fatal(err)
	}
	log.Printf("created synth %d\n", synthID)
}
Ejemplo n.º 2
0
func main() {
	const synthName = "sc.LFPulseExample"

	client, err := sc.NewClient("udp", "127.0.0.1:57112", "127.0.0.1:57110", 5*time.Second)
	if err != nil {
		log.Fatal(err)
	}
	defaultGroup, err := client.AddDefaultGroup()
	if err != nil {
		log.Fatal(err)
	}
	def := sc.NewSynthdef(synthName, func(p sc.Params) sc.Ugen {
		lfoFreq, lfoPhase, lfoWidth := sc.C(3), sc.C(0), sc.C(0.3)
		bus, gain := sc.C(0), sc.C(0.1)
		freq := sc.LFPulse{lfoFreq, lfoPhase, lfoWidth}.Rate(sc.KR).MulAdd(sc.C(200), sc.C(200))
		iphase, width := sc.C(0), sc.C(0.2)
		sig := sc.LFPulse{freq, iphase, width}.Rate(sc.AR).Mul(gain)
		return sc.Out{bus, sig}.Rate(sc.AR)
	})
	err = client.SendDef(def)
	if err != nil {
		log.Fatal(err)
	}
	synthID := client.NextSynthID()
	_, err = defaultGroup.Synth(synthName, synthID, sc.AddToTail, nil)
	log.Printf("created synth %d\n", synthID)
}
Ejemplo n.º 3
0
func main() {
	const synthName = "FreeVerbExample"

	client, err := sc.NewClient("udp", "127.0.0.1:57110", "127.0.0.1:57120", 5*time.Second)
	if err != nil {
		log.Fatal(err)
	}
	defaultGroup, err := client.AddDefaultGroup()
	if err != nil {
		log.Fatal(err)
	}
	def := sc.NewSynthdef(synthName, func(p sc.Params) sc.Ugen {
		mix := p.Add("mix", 0.25)
		room := p.Add("room", 0.15)
		damp := p.Add("damp", 0.5)
		bus := sc.C(0)
		impulse := sc.Impulse{Freq: sc.C(1)}.Rate(sc.AR)
		lfcub := sc.LFCub{Freq: sc.C(1200), Iphase: sc.C(0)}.Rate(sc.AR).Mul(sc.C(0.1))
		decay := sc.Decay{In: impulse, Decay: sc.C(0.25)}.Rate(sc.AR).Mul(lfcub)
		sig := sc.FreeVerb{In: decay, Mix: mix, Room: room, Damp: damp}.Rate(sc.AR)
		return sc.Out{bus, sig}.Rate(sc.AR)
	})
	if err := client.SendDef(def); err != nil {
		log.Fatal(err)
	}

	synthID := client.NextSynthID()
	if _, err := defaultGroup.Synth(synthName, synthID, sc.AddToTail, nil); err != nil {
		log.Fatal(err)
	}
	log.Printf("created synth %d\n", synthID)
}
Ejemplo n.º 4
0
func main() {
	const synthName = "sc.LFCubExample"

	client, err := sc.NewClient("udp", "127.0.0.1:57110", "127.0.0.1:57120", 5*time.Second)
	if err != nil {
		log.Fatal(err)
	}
	defaultGroup, err := client.AddDefaultGroup()
	if err != nil {
		log.Fatal(err)
	}
	def := sc.NewSynthdef(synthName, func(p sc.Params) sc.Ugen {
		bus, gain := sc.C(0), sc.C(0.1)
		lfo1 := sc.LFCub{Freq: sc.C(0.2)}.Rate(sc.KR).MulAdd(sc.C(8), sc.C(10))
		lfo2 := sc.LFCub{Freq: lfo1}.Rate(sc.KR).MulAdd(sc.C(400), sc.C(800))
		sig := sc.LFCub{Freq: lfo2}.Rate(sc.AR).Mul(gain)
		return sc.Out{bus, sig}.Rate(sc.AR)
	})
	if err := client.SendDef(def); err != nil {
		log.Fatal(err)
	}

	synthID := client.NextSynthID()
	if _, err := defaultGroup.Synth(synthName, synthID, sc.AddToTail, nil); err != nil {
		log.Fatal(err)
	}
	log.Printf("created synth %d\n", synthID)
}
Ejemplo n.º 5
0
func main() {
	const synthName = "FSinOscExample"

	client, err := sc.NewClient("udp", "127.0.0.1:57110", "127.0.0.1:57120", 5*time.Second)
	if err != nil {
		log.Fatal(err)
	}
	defaultGroup, err := client.AddDefaultGroup()
	if err != nil {
		log.Fatal(err)
	}
	def := sc.NewSynthdef(synthName, func(p sc.Params) sc.Ugen {
		bus := sc.C(0)
		line := sc.XLine{sc.C(4), sc.C(401), sc.C(8), 0}.Rate(sc.KR)
		sin1 := sc.SinOsc{line, sc.C(0)}.Rate(sc.AR).MulAdd(sc.C(200), sc.C(800))
		sin2 := sc.SinOsc{Freq: sin1}.Rate(sc.AR).Mul(sc.C(0.2))
		return sc.Out{bus, sin2}.Rate(sc.AR)
	})
	err = client.SendDef(def)
	if err != nil {
		log.Fatal(err)
	}
	synthID := client.NextSynthID()
	_, err = defaultGroup.Synth(synthName, synthID, sc.AddToTail, nil)
	log.Printf("created synth %d\n", synthID)
}
Ejemplo n.º 6
0
func main() {
	const synthName = "Balance2Example"

	client, err := sc.NewClient("udp", "127.0.0.1:57110", "127.0.0.1:57120", 5*time.Second)
	if err != nil {
		log.Fatal(err)
	}
	defaultGroup, err := client.AddDefaultGroup()
	if err != nil {
		log.Fatal(err)
	}
	def := sc.NewSynthdef(synthName, func(p sc.Params) sc.Ugen {
		bus, gain := sc.C(0), sc.C(0.1)
		l, r := sc.LFSaw{Freq: sc.C(44)}.Rate(sc.AR), sc.Pulse{Freq: sc.C(33)}.Rate(sc.AR)
		pos := sc.SinOsc{Freq: sc.C(0.5)}.Rate(sc.KR)
		sig := sc.Balance2{L: l, R: r, Pos: pos, Level: gain}.Rate(sc.AR)
		return sc.Out{bus, sig}.Rate(sc.AR)
	})
	if err := client.SendDef(def); err != nil {
		log.Fatal(err)
	}

	synthID := client.NextSynthID()
	if _, err := defaultGroup.Synth(synthName, synthID, sc.AddToTail, nil); err != nil {
		log.Fatal(err)
	}
	log.Printf("created synth %d\n", synthID)
}
Ejemplo n.º 7
0
// Run runs the command from command-line arguments.
func (poly *Poly) Run(args []string) error {
	if err := poly.Options().Parse(args); err != nil {
		return err
	}
	// Connect to scsynth.
	client, err := sc.NewClient("udp", poly.local, poly.scsynth)
	if err != nil {
		return err
	}
	poly.client = client
	group, err := client.AddDefaultGroup()
	if err != nil {
		return err
	}
	poly.group = group
	if poly.rpc == "" {
		// Listen for MIDI events.
		if err := portmidi.Initialize(); err != nil {
			return err
		}
		err = poly.ListenMIDI()
		_ = portmidi.Terminate() // Best effort.
		return err
	}
	// Listen for RPC events.
	return poly.ListenRPC([]string{})
}
Ejemplo n.º 8
0
func main() {
	const synthName = "FormletExample"

	client, err := sc.NewClient("udp", "127.0.0.1:57110", "127.0.0.1:57120", 5*time.Second)
	if err != nil {
		log.Fatal(err)
	}
	defaultGroup, err := client.AddDefaultGroup()
	if err != nil {
		log.Fatal(err)
	}
	def := sc.NewSynthdef(synthName, func(p sc.Params) sc.Ugen {
		bus, sine := sc.C(0), sc.SinOsc{Freq: sc.C(5)}.Rate(sc.KR).MulAdd(sc.C(20), sc.C(300))
		blip := sc.Blip{Freq: sine, Harm: sc.C(1000)}.Rate(sc.AR).Mul(sc.C(0.1))
		line := sc.XLine{Start: sc.C(1500), End: sc.C(700), Dur: sc.C(8)}.Rate(sc.KR)
		sig := sc.Formlet{
			In:         blip,
			Freq:       line,
			AttackTime: sc.C(0.005),
			DecayTime:  sc.C(0.4),
		}.Rate(sc.AR)
		return sc.Out{bus, sig}.Rate(sc.AR)
	})

	if err := client.SendDef(def); err != nil {
		log.Fatal(err)
	}

	synthID := client.NextSynthID()
	if _, err := defaultGroup.Synth(synthName, synthID, sc.AddToTail, nil); err != nil {
		log.Fatal(err)
	}
	log.Printf("created synth %d\n", synthID)
}
Ejemplo n.º 9
0
func main() {
	const synthName = "Decay2Example"

	client, err := sc.NewClient("udp", "127.0.0.1:57110", "127.0.0.1:57120", 5*time.Second)
	if err != nil {
		log.Fatal(err)
	}
	defaultGroup, err := client.AddDefaultGroup()
	if err != nil {
		log.Fatal(err)
	}
	def := sc.NewSynthdef(synthName, func(p sc.Params) sc.Ugen {
		bus := sc.C(0)
		line := sc.XLine{Start: sc.C(1), End: sc.C(50), Dur: sc.C(20)}.Rate(sc.KR)
		pulse := sc.Impulse{Freq: line, Phase: sc.C(0.25)}.Rate(sc.AR)
		sig := sc.Decay2{In: pulse, Attack: sc.C(0.01), Decay: sc.C(0.2)}.Rate(sc.AR)
		gain := sc.SinOsc{Freq: sc.C(600)}.Rate(sc.AR)
		return sc.Out{bus, sig.Mul(gain)}.Rate(sc.AR)
	})
	if err := client.SendDef(def); err != nil {
		log.Fatal(err)
	}

	synthID := client.NextSynthID()
	if _, err := defaultGroup.Synth(synthName, synthID, sc.AddToTail, nil); err != nil {
		log.Fatal(err)
	}
}
Ejemplo n.º 10
0
func main() {
	const synthName = "BRFExample"

	client, err := sc.NewClient("udp", "127.0.0.1:57110", "127.0.0.1:57120", 5*time.Second)
	if err != nil {
		log.Fatal(err)
	}
	defaultGroup, err := client.AddDefaultGroup()
	if err != nil {
		log.Fatal(err)
	}
	def := sc.NewSynthdef(synthName, func(p sc.Params) sc.Ugen {
		line := sc.XLine{sc.C(0.7), sc.C(300), sc.C(20), 0}.Rate(sc.KR)
		saw := sc.Saw{sc.C(200)}.Rate(sc.AR).Mul(sc.C(0.5))
		sine := sc.SinOsc{line, sc.C(0)}.Rate(sc.KR).MulAdd(sc.C(3800), sc.C(4000))
		bpf := sc.BRF{saw, sine, sc.C(0.3)}.Rate(sc.AR)
		return sc.Out{sc.C(0), bpf}.Rate(sc.AR)
	})
	if err := client.SendDef(def); err != nil {
		log.Fatal(err)
	}

	synthID := client.NextSynthID()
	if _, err := defaultGroup.Synth(synthName, synthID, sc.AddToTail, nil); err != nil {
		log.Fatal(err)
	}
	log.Printf("created synth %d\n", synthID)
}
Ejemplo n.º 11
0
func main() {
	// create a client and connect to the server
	client, err := sc.NewClient("udp", "127.0.0.1:57121", "127.0.0.1:57120", 5*time.Second)
	if err != nil {
		log.Fatal(err)
	}
	// create a synthdef
	def := sc.NewSynthdef("Envgen1", func(p sc.Params) sc.Ugen {
		bus := sc.C(0)
		attack, release := sc.C(0.01), sc.C(1)
		level, curveature := sc.C(1), sc.C(-4)
		perc := sc.EnvPerc{attack, release, level, curveature}
		gate, levelScale, levelBias, timeScale := sc.C(1), sc.C(1), sc.C(0), sc.C(1)
		ampEnv := sc.EnvGen{perc, gate, levelScale, levelBias, timeScale, sc.FreeEnclosing}.Rate(sc.KR)
		noise := sc.PinkNoise{}.Rate(sc.AR).Mul(ampEnv)
		return sc.Out{bus, noise}.Rate(sc.AR)
	})
	if err := client.SendDef(def); err != nil {
		log.Fatal(err)
	}

	time.Sleep(1000 * time.Millisecond)

	id := client.NextSynthID()
	if _, err = client.Synth("Envgen1", id, sc.AddToTail, sc.DefaultGroupID, nil); err != nil {
		log.Fatal(err)
	}
	time.Sleep(5000 * time.Millisecond)
}
Ejemplo n.º 12
0
func main() {
	const synthName = "sineTone"
	var synthID int32
	var note int
	var gain, dur float32

	// setup supercollider client
	client, err := sc.NewClient("udp", "127.0.0.1:57111", "127.0.0.1:57110", 5*time.Second)
	if err != nil {
		log.Fatal(err)
	}
	defaultGroup, err := client.AddDefaultGroup()
	if err != nil {
		log.Fatal(err)
	}
	err = client.DumpOSC(int32(1))
	if err != nil {
		log.Fatal(err)
	}
	def := sc.NewSynthdef(synthName, func(p sc.Params) sc.Ugen {
		freq := p.Add("freq", 440)
		gain := p.Add("gain", 0.5)
		dur := p.Add("dur", 1)
		bus := sc.C(0)
		env := sc.EnvGen{
			Env:        sc.EnvPerc{Release: dur},
			LevelScale: gain,
			Done:       sc.FreeEnclosing,
		}.Rate(sc.KR)
		sig := sc.SinOsc{Freq: freq}.Rate(sc.AR).Mul(env)
		return sc.Out{bus, sig}.Rate(sc.AR)
	})
	err = client.SendDef(def)
	if err != nil {
		log.Fatal(err)
	}

	ticker := time.NewTicker(125 * time.Millisecond)

	for _ = range ticker.C {
		synthID = client.NextSynthID()
		note = rand.Intn(128)
		gain = rand.Float32()
		dur = rand.Float32()
		ctls := map[string]float32{
			"freq": sc.Midicps(float32(note)),
			"gain": gain,
			"dur":  dur,
		}
		_, err = defaultGroup.Synth(synthName, synthID, sc.AddToTail, ctls)
	}
}
Ejemplo n.º 13
0
// Request status from scsynth
func main() {
	client, err := sc.NewClient("udp", "127.0.0.1:57121", "127.0.0.1:57120", 5*time.Second)
	if err != nil {
		log.Fatal(err)
	}
	status, err := client.Status(time.Minute)
	if err != nil {
		log.Fatal(err)
	}
	enc := json.NewEncoder(os.Stdout)
	err = enc.Encode(status)
	if err != nil {
		log.Fatal(err)
	}
}
Ejemplo n.º 14
0
func main() {
	const synthName = "playbufExample"
	const wavFile = "kalimba_mono.wav"
	var synthID int32
	var speed, gain float32

	// setup supercollider client
	client, err := sc.NewClient("udp", "127.0.0.1:57110", "127.0.0.1:57120", 5*time.Second)
	if err != nil {
		panic(err)
	}
	defaultGroup, err := client.AddDefaultGroup()
	if err != nil {
		panic(err)
	}
	// read a wav file
	buf, err := client.ReadBuffer(wavFile, 1)
	if err != nil {
		panic(err)
	}
	// send a synthdef
	def := sc.NewSynthdef(synthName, func(p sc.Params) sc.Ugen {
		speed := p.Add("speed", 1.0)
		gain := p.Add("gain", 0.5)
		bus := sc.C(0)
		sig := sc.PlayBuf{
			NumChannels: 1,
			BufNum:      sc.C(float32(buf.Num)),
			Speed:       speed,
			Done:        sc.FreeEnclosing,
		}.Rate(sc.AR).Mul(gain)
		return sc.Out{bus, sig}.Rate(sc.AR)
	})
	err = client.SendDef(def)
	if err != nil {
		panic(err)
	}

	ticker := time.NewTicker(125 * time.Millisecond)

	for _ = range ticker.C {
		synthID = client.NextSynthID()
		speed = (float32(8.0) * rand.Float32()) + float32(0.5)
		gain = rand.Float32()
		ctls := map[string]float32{"speed": speed, "gain": gain}
		_, err = defaultGroup.Synth(synthName, synthID, sc.AddToTail, ctls)
	}
}
Ejemplo n.º 15
0
func main() {
	formatp := flag.String("format", "json", "output format")
	flag.Parse()
	client, err := sc.NewClient("127.0.0.1", 57120)
	if err != nil {
		log.Fatal(err)
	}
	if *formatp == "json" {
		err = client.WriteGroupJSON(int32(1), os.Stdout)
	} else if *formatp == "xml" {
		err = client.WriteGroupXML(int32(1), os.Stdout)
	}
	if err != nil {
		log.Fatal(err)
	}
}
Ejemplo n.º 16
0
func main() {
	const synthName = "COscExample"

	client, err := sc.NewClient("udp", "127.0.0.1:57110", "127.0.0.1:57120", 5*time.Second)
	if err != nil {
		log.Fatal(err)
	}
	defaultGroup, err := client.AddDefaultGroup()
	if err != nil {
		log.Fatal(err)
	}
	buf, err := client.AllocBuffer(512, 1)
	if err != nil {
		log.Fatal(err)
	}

	var (
		bufRoutine = sc.BufferRoutineSine1
		bufFlags   = sc.BufferFlagNormalize | sc.BufferFlagWavetable | sc.BufferFlagClear
		partials   = []float32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	)
	for i, p := range partials {
		partials[i] = 1 / p
	}

	if err := buf.Gen(bufRoutine, bufFlags, partials...); err != nil {
		log.Fatal(err)
	}
	def := sc.NewSynthdef(synthName, func(p sc.Params) sc.Ugen {
		bus, gain := sc.C(0), sc.C(0.25)
		freq, beats := sc.C(200), sc.C(0.7)
		sig := sc.COsc{
			BufNum: sc.C(float32(buf.Num)),
			Freq:   freq,
			Beats:  beats,
		}.Rate(sc.AR)
		return sc.Out{bus, sig.Mul(gain)}.Rate(sc.AR)
	})
	if err := client.SendDef(def); err != nil {
		log.Fatal(err)
	}

	synthID := client.NextSynthID()
	if _, err := defaultGroup.Synth(synthName, synthID, sc.AddToTail, nil); err != nil {
		log.Fatal(err)
	}
}
Ejemplo n.º 17
0
Archivo: def.go Proyecto: scgolang/poly
// Run runs the command from command-line arguments.
func (def *Def) Run(args []string) error {
	if err := def.Options().Parse(args); err != nil {
		return err
	}
	// Connect to scsynth.
	client, err := sc.NewClient("udp", def.local, def.scsynth)
	if err != nil {
		return err
	}
	// Send synthdefs.
	for _, d := range def.defs {
		if err := client.SendDef(d); err != nil {
			return err
		}
	}
	return nil
}
Ejemplo n.º 18
0
func main() {
	// create a client and connect to the server
	client, err := sc.NewClient("udp", "127.0.0.1:57121", "127.0.0.1:57120", 5*time.Second)
	if err != nil {
		log.Fatal(err)
	}
	def := sc.NewSynthdef("SineTone", func(p sc.Params) sc.Ugen {
		bus, chaos := sc.C(0), sc.Line{sc.C(1.0), sc.C(2.0), sc.C(10), sc.DoNothing}.Rate(sc.KR)
		sig := sc.Crackle{chaos}.Rate(sc.AR).MulAdd(sc.C(0.5), sc.C(0.5))
		return sc.Out{bus, sig}.Rate(sc.AR)
	})
	if err := client.SendDef(def); err != nil {
		log.Fatal(err)
	}

	id := client.NextSynthID()
	if _, err = client.Synth("SineTone", id, sc.AddToTail, sc.DefaultGroupID, nil); err != nil {
		log.Fatal(err)
	}
}
Ejemplo n.º 19
0
Archivo: rpc.go Proyecto: scgolang/poly
// ListenRPC listens for commands from the network.
func (poly *Poly) ListenRPC(args []string) error {
	var (
		fs      = flag.NewFlagSet("listen", flag.ExitOnError)
		addr    = fs.String("addr", DefaultRPCAddr, "rpc listening address")
		local   = fs.String("local", DefaultLocalAddr, "local address")
		scsynth = fs.String("scsynth", DefaultScsynthAddr, "scsynth address")
	)
	if err := fs.Parse(args); err != nil {
		return err
	}
	// Connect to scsynth.
	_, err := sc.NewClient("udp", *local, *scsynth)
	if err != nil {
		return err
	}
	// Start listening for RPC commands.
	laddr, err := net.ResolveTCPAddr("tcp", *addr)
	if err != nil {
		return err
	}
	listener, err := net.ListenTCP("tcp", laddr)
	if err != nil {
		return err
	}
	server := rpc.NewServer()
	if err := server.Register(poly); err != nil {
		return err
	}

	conn, err := listener.Accept()
	if err != nil {
		return err
	}

	server.ServeConn(conn)

	for conn, err = listener.Accept(); err == nil; conn, err = listener.Accept() {
		server.ServeConn(conn)
	}
	return err
}
Ejemplo n.º 20
0
func main() {
	var err error
	// create a client and connect to the server
	client, err := sc.NewClient("udp", "127.0.0.1:57121", "127.0.0.1:57120", 5*time.Second)
	if err != nil {
		log.Fatal(err)
	}
	formatp := flag.String("format", "json", "output format")
	flag.Parse()
	// query the root node
	grp, err := client.QueryGroup(0)
	if err != nil {
		log.Fatal(err)
	}
	if *formatp == "json" {
		err = grp.WriteJSON(os.Stdout)
	} else if *formatp == "xml" {
		err = grp.WriteXML(os.Stdout)
	}
	if err != nil {
		log.Fatal(err)
	}
}
Ejemplo n.º 21
0
func main() {
	const synthName = "CombCExample"

	client, err := sc.NewClient("udp", "127.0.0.1:57110", "127.0.0.1:57120", 5*time.Second)
	if err != nil {
		log.Fatal(err)
	}
	defaultGroup, err := client.AddDefaultGroup()
	if err != nil {
		log.Fatal(err)
	}
	def := sc.NewSynthdef(synthName, func(p sc.Params) sc.Ugen {
		bus := sc.C(0)
		line := sc.XLine{
			Start: sc.C(0.0001),
			End:   sc.C(0.01),
			Dur:   sc.C(20),
		}.Rate(sc.KR)
		sig := sc.Comb{
			Interpolation: sc.InterpolationCubic,
			In:            sc.WhiteNoise{}.Rate(sc.AR).Mul(sc.C(0.01)),
			MaxDelayTime:  sc.C(0.01),
			DelayTime:     line,
			DecayTime:     sc.C(0.2),
		}.Rate(sc.AR)
		return sc.Out{bus, sig}.Rate(sc.AR)
	})
	if err := client.SendDef(def); err != nil {
		log.Fatal(err)
	}

	synthID := client.NextSynthID()
	if _, err := defaultGroup.Synth(synthName, synthID, sc.AddToTail, nil); err != nil {
		log.Fatal(err)
	}
}
Ejemplo n.º 22
0
func main() {
	var synthID int32
	const synthName = "sineTone"

	// Set up SuperCollider client.
	client, err := sc.NewClient("udp", "127.0.0.1:57121", "127.0.0.1:57120", 5*time.Second)
	if err != nil {
		log.Fatal(err)
	}
	_, err = client.AddDefaultGroup()
	if err != nil {
		log.Fatal(err)
	}
	err = client.DumpOSC(int32(1))
	if err != nil {
		log.Fatal(err)
	}
	def := sc.NewSynthdef(synthName, func(p sc.Params) sc.Ugen {
		freq := p.Add("freq", 440)
		gain := p.Add("gain", 0.5)
		bus := sc.C(0)
		env := sc.EnvGen{
			Env:        sc.EnvPerc{},
			Done:       sc.FreeEnclosing,
			LevelScale: gain,
		}.Rate(sc.KR)
		sig := sc.SinOsc{Freq: freq}.Rate(sc.AR).Mul(env)
		return sc.Out{bus, sig}.Rate(sc.AR)
	})
	err = client.SendDef(def)
	if err != nil {
		log.Fatal(err)
	}

	// initialize midi
	portmidi.Initialize()
	// this code can be uncommented to discover the
	// device ID's portmidi comes up with
	// deviceCount := portmidi.CountDevices()
	// enc := json.NewEncoder(os.Stdout)
	// for i := 0; i < deviceCount; i++ {
	// 	info := portmidi.GetDeviceInfo(portmidi.DeviceId(i))
	// 	log.Printf("device %d - ", i)
	// 	err = enc.Encode(info)
	// 	if err != nil {
	// 		log.Fatal(err)
	// 	}
	// }

	// setup midi input stream and listen for midi events
	in, err := portmidi.NewInputStream(3, 1024)
	if err != nil {
		log.Fatal(err)
	}
	ch := in.Listen()
	for event := range ch {
		if event.Status == 144 {
			// MIDI note
			log.Printf("Note %-3d Velocity %-3d\n", event.Data1, event.Data2)
			if event.Data2 > 0 {
				// Note On
				synthID = client.NextSynthID()
				ctls := map[string]float32{
					"freq": sc.Midicps(float32(event.Data1)),
					"gain": float32(event.Data2 / 127.0),
				}
				_, err = client.Synth(synthName, synthID, sc.AddToTail, sc.DefaultGroupID, ctls)
			}
		}
	}
	portmidi.Terminate()
}
Ejemplo n.º 23
0
func main() {
	const (
		synthName = "GrainFMExample"
	)
	server := &sc.Server{
		Network: "udp",
		Port:    57120,
	}

	stdout, stderr, err := server.Start(5 * time.Second)
	if err != nil {
		log.Fatalf("Could not start scsynth: %s", err)
	}
	defer func() { _ = server.Process.Kill() }()

	go func() {
		if _, err := io.Copy(os.Stderr, stderr); err != nil {
			log.Fatalf("Could not pipe scsynth stderr to terminal: %s", err)
		}
	}()

	go func() {
		if _, err := io.Copy(os.Stdout, stdout); err != nil {
			log.Fatalf("Could not pipe scsynth stdout to terminal: %s", err)
		}
	}()

	client, err := sc.NewClient("udp", "127.0.0.1:57110", "127.0.0.1:57120", 5*time.Second)
	if err != nil {
		log.Fatal(err)
	}

	defaultGroup, err := client.AddDefaultGroup()
	if err != nil {
		log.Fatal(err)
	}

	def := sc.NewSynthdef(synthName, func(p sc.Params) sc.Ugen {
		var (
			gate    = p.Add("gate", 1)
			amp     = p.Add("amp", 1)
			bus     = sc.C(0)
			mousey  = sc.MouseY{Min: sc.C(0), Max: sc.C(400)}.Rate(sc.KR)
			freqdev = sc.WhiteNoise{}.Rate(sc.KR).Mul(mousey)
		)
		env := sc.Env{
			Levels:      []sc.Input{sc.C(0), sc.C(1), sc.C(0)},
			Times:       []sc.Input{sc.C(1), sc.C(1)},
			Curve:       []string{"sine", "sine"},
			ReleaseNode: sc.C(1),
		}
		ampenv := sc.EnvGen{
			Env:        env,
			Gate:       gate,
			LevelScale: amp,
			Done:       sc.FreeEnclosing,
		}.Rate(sc.KR)

		var (
			trigFreq = sc.MouseY{Min: sc.C(8), Max: sc.C(64)}.Rate(sc.KR)
			trig     = sc.Impulse{Freq: trigFreq}.Rate(sc.KR)
			modIndex = sc.LFNoise{Interpolation: sc.NoiseLinear}.Rate(sc.KR).MulAdd(sc.C(5), sc.C(5))
			pan      = sc.MouseX{Min: sc.C(-1), Max: sc.C(1)}.Rate(sc.KR)
		)
		sig := sc.GrainFM{
			NumChannels: 2,
			Trigger:     trig,
			Dur:         sc.C(0.1),
			CarFreq:     sc.C(440),
			ModFreq:     sc.C(200).Add(freqdev),
			ModIndex:    modIndex,
			Pan:         pan,
		}.Rate(sc.AR)

		return sc.Out{bus, sig.Mul(ampenv)}.Rate(sc.AR)
	})
	if err := client.SendDef(def); err != nil {
		log.Fatal(err)
	}
	synthID := client.NextSynthID()
	if _, err := defaultGroup.Synth(synthName, synthID, sc.AddToTail, nil); err != nil {
		log.Fatal(err)
	}

	if err := server.Wait(); err != nil {
		log.Fatal(err)
	}
}