示例#1
0
func (p *Player) updateSE() error {
	if seBytes == nil {
		return nil
	}
	if !ebiten.IsKeyPressed(ebiten.KeyP) {
		keyState[ebiten.KeyP] = 0
		return nil
	}
	keyState[ebiten.KeyP]++
	if keyState[ebiten.KeyP] != 1 {
		return nil
	}
	sePlayer, err := audio.NewPlayerFromBytes(audioContext, seBytes)
	if err != nil {
		return err
	}
	return sePlayer.Play()
}
示例#2
0
func addNote() error {
	size := sampleRate / ebiten.FPS
	notes := []float64{freqC, freqD, freqE, freqF, freqG, freqA * 2, freqB * 2}

	defer func() {
		scoreIndex++
		scoreIndex %= len(score)
	}()
	l := make([]int16, size*30)
	r := make([]int16, size*30)
	note := score[scoreIndex]
	for note == ' ' {
		scoreIndex++
		scoreIndex %= len(score)
		note = score[scoreIndex]
	}
	freq := 0.0
	switch {
	case note == 'R':
		freq = 0
	case note <= 'B':
		freq = notes[int(note)+len(notes)-int('C')]
	default:
		freq = notes[note-'C']
	}
	vol := 1.0 / 16.0
	square(l, vol, freq, 0.25)
	square(r, vol, freq, 0.25)
	b := toBytes(l, r)
	p, err := audio.NewPlayerFromBytes(audioContext, b)
	if err != nil {
		return err
	}
	if err := p.Play(); err != nil {
		return err
	}
	return nil
}