func (p *Player) updateSE() error { if seStream == nil { return nil } if !ebiten.IsKeyPressed(ebiten.KeyP) { keyState[ebiten.KeyP] = 0 return nil } keyState[ebiten.KeyP]++ if keyState[ebiten.KeyP] != 1 { return nil } if sePlayer == nil { var err error sePlayer, err = audio.NewPlayer(audioContext, seStream) if err != nil { return err } } if sePlayer.IsPlaying() { return nil } if err := sePlayer.Rewind(); err != nil { return err } return sePlayer.Play() }
func main() { wavF, err := ebitenutil.OpenFile("_resources/audio/jab.wav") if err != nil { log.Fatal(err) } oggF, err := ebitenutil.OpenFile("_resources/audio/ragtime.ogg") if err != nil { log.Fatal(err) } const sampleRate = 22050 const bytesPerSample = 4 // TODO: This should be defined in audio package audioContext, err = audio.NewContext(sampleRate) if err != nil { log.Fatal(err) } go func() { s, err := wav.Decode(audioContext, wavF) if err != nil { log.Fatal(err) return } seCh <- s close(seCh) }() go func() { s, err := vorbis.Decode(audioContext, oggF) if err != nil { log.Fatal(err) return } p, err := audio.NewPlayer(audioContext, s) if err != nil { log.Fatal(err) return } musicCh <- &Player{ audioPlayer: p, total: time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate, } close(musicCh) // TODO: Is this goroutine-safe? p.Play() }() if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio (Ebiten Demo)"); err != nil { log.Fatal(err) } if musicPlayer != nil { if err := musicPlayer.close(); err != nil { log.Fatal(err) } } }
func update(screen *ebiten.Image) error { if player == nil { var err error player, err = audio.NewPlayer(audioContext, &stream{}) if err != nil { return err } if err := player.Play(); err != nil { return err } } ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS())) if err := audioContext.Update(); err != nil { return err } return nil }
func update(screen *ebiten.Image) error { if player == nil { var err error player, err = audio.NewPlayer(audioContext, &stream{}) if err != nil { return err } if err := player.Play(); err != nil { return err } } msg := fmt.Sprintf("FPS: %0.2f\nThis is an example using infinite audio stream.", ebiten.CurrentFPS()) if err := ebitenutil.DebugPrint(screen, msg); err != nil { return err } if err := audioContext.Update(); err != nil { return err } return nil }
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) s := &srcStream{bytes.NewReader(b)} p, err := audio.NewPlayer(audioContext, s) if err != nil { return err } p.Play() return nil }
func main() { wavF, err := ebitenutil.OpenFile("_resources/audio/jab.wav") if err != nil { log.Fatal(err) } oggF, err := ebitenutil.OpenFile("_resources/audio/game.ogg") if err != nil { log.Fatal(err) } // This sample rate doesn't match with wav/ogg's sample rate, // but decoders adjust them. const sampleRate = 48000 const bytesPerSample = 4 // TODO: This should be defined in audio package audioContext, err = audio.NewContext(sampleRate) if err != nil { log.Fatal(err) } go func() { s, err := wav.Decode(audioContext, wavF) if err != nil { log.Fatal(err) return } b, err := ioutil.ReadAll(s) if err != nil { log.Fatal(err) return } seCh <- b close(seCh) }() go func() { s, err := vorbis.Decode(audioContext, oggF) if err != nil { log.Fatal(err) return } p, err := audio.NewPlayer(audioContext, s) if err != nil { log.Fatal(err) return } musicCh <- &Player{ audioPlayer: p, total: time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate, } close(musicCh) // TODO: Is this goroutine-safe? if err := p.Play(); err != nil { log.Fatal(err) return } }() if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio (Ebiten Demo)"); err != nil { log.Fatal(err) } if musicPlayer != nil { if err := musicPlayer.close(); err != nil { log.Fatal(err) } } }