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 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) } } }