Beispiel #1
0
func socketHandler(w http.ResponseWriter, r *http.Request) {
	c, err := websocket.Upgrade(w, r, nil, 1024, 1024)
	if err != nil {
		log.Println(err)
		return
	}

	u, err := ui.New()
	if err != nil {
		log.Println(err)
		return
	}
	defer u.Close()

	go func() {
		for m := range u.M {
			if err := c.WriteJSON(m); err != nil {
				log.Println(err)
				return
			}
		}
	}()

	for {
		m := new(ui.Message)
		if err := c.ReadJSON(m); err != nil {
			log.Println(err)
			return
		}
		if err := u.Handle(m); err != nil {
			log.Println(err)
		}
	}
}
Beispiel #2
0
func NewSession() (*Session, error) {
	m := make(chan *Message, 1)
	s := &Session{M: m, m: m}
	u := ui.New(s)
	if err := u.Start(); err != nil {
		return nil, err
	}
	s.u = u
	return s, nil
}
Beispiel #3
0
func audioHandler(w http.ResponseWriter, r *http.Request) {
	if b := getCache(r); b != nil {
		w.Header().Set("Content-type", "audio/mp3")
		if _, err := w.Write(b); err != nil {
			log.Println(err)
		}
		return
	}

	u := ui.New(noopHandler{})
	if err := u.Load(inFile); err != nil {
		log.Println(err)
		http.Error(w, "error processing audio", 500)
		return
	}

	u.Set("value19", floatValue(r, "speed"))
	u.Set("value70", floatValue(r, "slew"))
	u.Set("value77", floatValue(r, "root"))
	u.Set("value4", floatValue(r, "square"))
	u.Set("value39", floatValue(r, "amount"))
	u.Set("value46", floatValue(r, "offset"))
	u.Set("value13", floatValue(r, "attack"))
	u.Set("value12", floatValue(r, "decay"))
	u.Set("value29", floatValue(r, "time"))
	u.Set("value27", floatValue(r, "feedback"))

	scale, perm := intValue(r, "scale"), intValue(r, "perm")
	if 0 >= scale || scale >= len(scales) {
		scale = 0
	}
	notes := notesFromScale(scales[scale], 8, perm)
	for i, n := range notes {
		u.Set(seqNotes[i], float64(n)/120)
	}

	frames := int(length / time.Second * 44100 / 256)
	samp := u.Render(frames)
	normalize(samp)
	fadeout(samp, 44100)
	b, err := mp3(pcm(samp))
	if err != nil {
		log.Println(err)
		http.Error(w, "error processing audio", 500)
		return
	}

	w.Header().Set("Content-type", "audio/mp3")
	if _, err := w.Write(b); err != nil {
		log.Println(err)
	}

	putCache(r, b)
}
Beispiel #4
0
func audioHandler(w http.ResponseWriter, r *http.Request) {
	u := ui.New(noopHandler{})
	if err := u.Load(inFile); err != nil {
		log.Println(err)
		http.Error(w, "error processing audio", 500)
		return
	}

	u.Set("value19", floatValue(r, "speed"))
	//u.Set("x", floatValue(r, "scale"))
	//u.Set("x", floatValue(r, "perm"))
	u.Set("value70", floatValue(r, "slew"))
	u.Set("value77", floatValue(r, "root"))
	u.Set("value4", floatValue(r, "square"))
	u.Set("value39", floatValue(r, "amount"))
	u.Set("value46", floatValue(r, "offset"))
	u.Set("value13", floatValue(r, "attack"))
	u.Set("value12", floatValue(r, "decay"))
	u.Set("value29", floatValue(r, "time"))
	u.Set("value27", floatValue(r, "feedback"))

	frames := int(length / time.Second * 44100 / 256)
	samp := u.Render(frames)
	normalize(samp)
	b, err := mp3(pcm(samp))
	if err != nil {
		log.Println(err)
		http.Error(w, "error processing audio", 500)
		return
	}

	w.Header().Set("Content-type", "audio/mp3")
	if _, err := w.Write(b); err != nil {
		log.Println(err)
	}
}
Beispiel #5
0
func demo() error {
	e := audio.NewEngine()
	e.Input("in", audio.NewSin())
	if err := e.Start(); err != nil {
		return err
	}
	time.Sleep(time.Second)
	if err := e.Stop(); err != nil {
		return err
	}

	u, err := ui.New()
	if err != nil {
		return err
	}
	for _, m := range []*ui.Message{
		{Action: "new", Name: "sin1", Kind: "sin"},
		{Action: "new", Name: "sin2", Kind: "sin"},
		{Action: "new", Name: "sum1", Kind: "sum"},
		{Action: "new", Name: "mul1", Kind: "mul"},
		{Action: "new", Name: "mul2", Kind: "mul"},
		{Action: "new", Name: "val1", Kind: "value", Value: 0.1},
		{Action: "connect", From: "sin1", To: "engine", Input: "in"},
		{Action: "connect", From: "mul1", To: "sin1", Input: "pitch"},
		{Action: "connect", From: "sin2", To: "mul1", Input: "a"},
		{Action: "connect", From: "val1", To: "mul1", Input: "b"},
		{Action: "connect", From: "val1", To: "sin2", Input: "pitch"},
	} {
		if err := u.Handle(m); err != nil {
			return err
		}
	}
	time.Sleep(time.Second)
	for _, m := range []*ui.Message{
		{Action: "set", Name: "val1", Value: 0.15},
	} {
		if err := u.Handle(m); err != nil {
			return err
		}
	}
	time.Sleep(time.Second)
	if err := u.Close(); err != nil {
		return err
	}

	e = audio.NewEngine()

	sinMod := audio.NewSin()
	sinMod.Input("pitch", audio.Value(-0.1))

	sinModMul := audio.NewMul()
	sinModMul.Input("a", sinMod)
	sinModMul.Input("b", audio.Value(0.1))

	sin := audio.NewSin()
	sin.Input("pitch", sinModMul)

	envMod := audio.NewSin()
	envMod.Input("pitch", audio.Value(-1))

	envModMul := audio.NewMul()
	envModMul.Input("a", envMod)
	envModMul.Input("b", audio.Value(0.02))

	envModSum := audio.NewSum()
	envModSum.Input("a", envModMul)
	envModSum.Input("b", audio.Value(0.021))

	sin2 := audio.NewSin()
	sin2.Input("pitch", audio.Value(-0.6))

	env := audio.NewEnv()
	env.Input("trig", sin2)
	env.Input("att", audio.Value(0.0001))
	env.Input("dec", envModSum)

	mul := audio.NewMul()
	mul.Input("a", sin)
	mul.Input("b", env)

	mulMul := audio.NewMul()
	mulMul.Input("a", mul)
	mulMul.Input("b", audio.Value(0.5))

	e.Input("in", mulMul)

	if err := e.Start(); err != nil {
		return err
	}

	os.Stdout.Write([]byte("Press enter to stop...\n"))
	os.Stdin.Read([]byte{0})

	return e.Stop()
}