Exemple #1
0
func main() {
	const buffers = 1
	if err := al.OpenDevice(buffers); err != nil {
		log.Fatal(err)
	}
	al.Start()

	sine := snd.Sine()
	// mod is a modulator; try replacing the nil argument to
	// the oscillator with this.
	// mod := snd.NewOscil(sine, 200, nil)
	osc := snd.NewOscil(sine, 440, nil) // oscillator
	al.AddSource(osc)

	for range time.Tick(time.Second) {
		log.Printf("underruns=%-4v buflen=%-4v tickavg=%-12s drift=%s\n",
			al.Underruns(), al.BufLen(), al.TickAverge(), al.DriftApprox())
	}
}
Exemple #2
0
func onStart(ctx gl.Context) {
	ctx.Enable(gl.BLEND)
	ctx.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	ctx.Enable(gl.CULL_FACE)
	ctx.CullFace(gl.BACK)

	if err := al.OpenDevice(buffers); err != nil {
		log.Fatal(err)
	}

	env.Load(ctx)
	env.LoadIcons(ctx)
	env.LoadGlyphs(ctx)

	toolbar = env.NewToolbar(ctx)
	toolbar.Title.SetText("piano")
	toolbar.Title.SetTextColor(material.White)
	toolbar.Nav.SetIconColor(material.White)

	btnLoop := env.NewButton(ctx)
	toolbar.AddAction(btnLoop)
	btnLoop.SetIcon(icon.AvFiberSmartRecord)
	btnLoop.SetIconColor(material.White)
	btnLoop.OnPress = func() {
		if loop.Recording() {
			btnLoop.SetIconColor(material.White)
			loop.Stop()
		} else {
			btnLoop.SetIconColor(env.Palette().Accent)
			loop.Record()
		}
	}

	btnMetronome := env.NewButton(ctx)
	toolbar.AddAction(btnMetronome)
	btnMetronome.SetIcon(icon.AvSlowMotionVideo)
	btnMetronome.SetIconColor(material.White)
	btnMetronome.OnPress = func() {
		if metronome.IsOff() {
			metronome.On()
			btnMetronome.SetIconColor(env.Palette().Accent)
		} else {
			metronome.Off()
			btnMetronome.SetIconColor(material.White)
		}
	}

	btnLowpass := env.NewButton(ctx)
	toolbar.AddAction(btnLowpass)
	btnLowpass.SetIcon(icon.AvSubtitles)
	btnLowpass.SetIconColor(env.Palette().Accent)
	btnLowpass.OnPress = func() {
		lowpass.SetPassthrough(!lowpass.Passthrough())
		if lowpass.Passthrough() {
			btnLowpass.SetIconColor(material.White)
		} else {
			btnLowpass.SetIconColor(env.Palette().Accent)
		}
	}

	btnReverb := env.NewButton(ctx)
	toolbar.AddAction(btnReverb)
	btnReverb.SetIcon(icon.AvSurroundSound)
	btnReverb.SetIconColor(env.Palette().Accent)
	btnReverb.OnPress = func() {
		if reverb.IsOff() {
			reverb.On()
			keygain.SetAmp(snd.Decibel(-3).Amp())
			btnReverb.SetIconColor(env.Palette().Accent)
		} else {
			reverb.Off()
			keygain.SetAmp(snd.Decibel(3).Amp())
			btnReverb.SetIconColor(material.White)
		}
	}

	btnNext = env.NewFloatingActionButton(ctx)
	btnNext.Mini = true
	btnNext.SetColor(env.Palette().Accent)
	btnNext.SetIcon(icon.AvSkipNext)
	btnNext.OnPress = func() {
		sndbankpos = (sndbankpos + 1) % len(sndbank)
		go makekeys()
	}

	decor = env.NewMaterial(ctx)
	decor.SetColor(material.BlueGrey900)

	tseq := make(map[touch.Sequence]int)
	for i := range btnkeys {
		btnkeys[i] = env.NewButton(ctx)
		j := i
		btnkeys[i].OnTouch = func(ev touch.Event) {
			switch ev.Type {
			case touch.TypeBegin:
				keys[j].Press()
				tseq[ev.Sequence] = j
			case touch.TypeMove:
				// TODO drag finger off piano and it still plays, should stop
				if last, ok := tseq[ev.Sequence]; ok {
					if j != last {
						keys[last].Release()
						keys[j].Press()
						tseq[ev.Sequence] = j
					}
				}
			case touch.TypeEnd:
				keys[j].Release()
				delete(tseq, ev.Sequence)
			}
		}
	}

	var err error

	keymix = snd.NewMixer()
	go makekeys()
	lowpass = snd.NewLowPass(773, keymix)
	keygain = snd.NewGain(snd.Decibel(-3).Amp(), lowpass)

	dly := snd.NewDelay(29*time.Millisecond, keygain)
	tap0 := snd.NewTap(19*time.Millisecond, dly)
	tap1 := snd.NewTap(13*time.Millisecond, dly)
	tap2 := snd.NewTap(7*time.Millisecond, dly)
	cmb := snd.NewComb(snd.Decibel(-3).Amp(), 11*time.Millisecond, snd.NewMixer(dly, tap0, tap1, tap2))
	reverb = snd.NewLowPass(2000, cmb)
	dlymix := snd.NewMixer(reverb, keygain)

	loop = snd.NewLoopFrames(loopdur, dlymix)
	loop.SetBPM(bpm)
	loopmix := snd.NewMixer(dlymix, loop)

	master = snd.NewMixer(loopmix)
	mastergain = snd.NewGain(snd.Decibel(-6).Amp(), master)
	mixwf, err = NewWaveform(ctx, 2, mastergain)
	mixwf.SetColor(material.BlueGrey700)
	if err != nil {
		log.Fatal(err)
	}
	pan := snd.NewPan(0, mixwf)

	mtrosc := snd.NewOscil(sine, 440, nil)
	mtrdmp := snd.NewDamp(bpm.Dur(), mtrosc)
	metronome = snd.NewMixer(mtrdmp)
	metronome.Off()
	master.Append(metronome)

	al.AddSource(pan)
}