func NewBeatsKey(idx int) Key { osc := snd.NewOscil(sawsine, notes[idx], snd.NewOscil(triangle, 4, nil)) dmp := snd.NewDamp(bpm.Dur(), osc) d := snd.BPM(float64(bpm) * 1.25).Dur() dmp1 := snd.NewDamp(d, osc) drv := snd.NewDrive(d, osc) mix := snd.NewMixer(dmp, dmp1, drv) frz := snd.NewFreeze(bpm.Dur()*4, mix) adsr := snd.NewADSR(250*ms, 500*ms, 300*ms, 400*ms, 0.85, 1.0, frz) key := &BeatsKey{snd.NewInstrument(adsr), adsr} key.Off() return key }
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) }