예제 #1
0
파일: key.go 프로젝트: dskinner/snd
func NewPianoKey(idx int) Key {
	const phasefac float64 = 0.5063999999999971

	k := &PianoKey{}

	k.freq = notes[idx]
	k.mod = snd.NewOscil(sqsine, k.freq/2, nil)
	k.osc = snd.NewOscil(sawtooth, k.freq, k.mod)
	k.phs = snd.NewOscil(square, k.freq*phasefac, nil)
	k.osc.SetPhase(k.phs)

	freql := k.freq * math.Pow(2, -10.0/1200)
	k.modl = snd.NewOscil(sqsine, freql/2, nil)
	k.oscl = snd.NewOscil(sawtooth, freql, k.modl)
	k.phsl = snd.NewOscil(square, freql*phasefac, nil)
	k.oscl.SetPhase(k.phsl)

	freqr := k.freq * math.Pow(2, 10.0/1200)
	k.modr = snd.NewOscil(sqsine, freqr/2, nil)
	k.oscr = snd.NewOscil(sawtooth, freqr, k.modr)
	k.phsr = snd.NewOscil(square, freqr*phasefac, nil)
	k.oscr.SetPhase(k.phsr)

	oscmix := snd.NewMixer(k.osc, k.oscl, k.oscr)

	k.reldur = 1050 * ms
	k.dur = 280*ms + k.reldur
	k.adsr0 = snd.NewADSR(30*ms, 50*ms, 200*ms, k.reldur, 0.4, 1, oscmix)
	k.adsr1 = snd.NewADSR(1*ms, 278*ms, 1*ms, k.reldur, 0.4, 1, oscmix)
	adsrmix := snd.NewMixer(k.adsr0, k.adsr1)

	k.gain = snd.NewGain(snd.Decibel(-10).Amp(), adsrmix)

	k.Instrument = snd.NewInstrument(k.gain)
	k.Off()

	return k
}
예제 #2
0
파일: main.go 프로젝트: dskinner/snd
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)
}