main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2017 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "bytes"
  17. "flag"
  18. "io"
  19. "log"
  20. "github.com/hajimehoshi/ebiten/v2"
  21. "github.com/hajimehoshi/ebiten/v2/audio"
  22. "github.com/hajimehoshi/ebiten/v2/audio/wav"
  23. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  24. raudio "github.com/hajimehoshi/ebiten/v2/examples/resources/audio"
  25. "github.com/hajimehoshi/ebiten/v2/inpututil"
  26. )
  27. const (
  28. screenWidth = 640
  29. screenHeight = 480
  30. sampleRate = 48000
  31. )
  32. var (
  33. flagBitsPerSample = flag.Int("bits", 16, "bits per sample")
  34. )
  35. type Game struct {
  36. audioContext *audio.Context
  37. audioPlayer *audio.Player
  38. }
  39. func NewGame() (*Game, error) {
  40. g := &Game{}
  41. var err error
  42. // Initialize audio context.
  43. g.audioContext = audio.NewContext(sampleRate)
  44. // In this example, embedded resource "Jab_wav" is used.
  45. //
  46. // If you want to use a wav file, open this and pass the file stream to wav.Decode.
  47. // Note that file's Close() should not be closed here
  48. // since audio.Player manages stream state.
  49. //
  50. // f, err := os.Open("jab.wav")
  51. // if err != nil {
  52. // return err
  53. // }
  54. //
  55. // d, err := wav.DecodeF32(f)
  56. // ...
  57. // Decode wav-formatted data and retrieve decoded PCM stream.
  58. var r io.Reader
  59. switch *flagBitsPerSample {
  60. case 8:
  61. r = bytes.NewReader(raudio.Jab8_wav)
  62. default:
  63. r = bytes.NewReader(raudio.Jab_wav)
  64. }
  65. d, err := wav.DecodeF32(r)
  66. if err != nil {
  67. return nil, err
  68. }
  69. // Create an audio.Player that has one stream.
  70. g.audioPlayer, err = g.audioContext.NewPlayerF32(d)
  71. if err != nil {
  72. return nil, err
  73. }
  74. return g, nil
  75. }
  76. func (g *Game) Update() error {
  77. if inpututil.IsKeyJustPressed(ebiten.KeyP) {
  78. // As audioPlayer has one stream and remembers the playing position,
  79. // rewinding is needed before playing when reusing audioPlayer.
  80. if err := g.audioPlayer.Rewind(); err != nil {
  81. return err
  82. }
  83. g.audioPlayer.Play()
  84. }
  85. return nil
  86. }
  87. func (g *Game) Draw(screen *ebiten.Image) {
  88. if g.audioPlayer.IsPlaying() {
  89. ebitenutil.DebugPrint(screen, "Bump!")
  90. } else {
  91. ebitenutil.DebugPrint(screen, "Press P to play the wav")
  92. }
  93. }
  94. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  95. return screenWidth, screenHeight
  96. }
  97. func main() {
  98. flag.Parse()
  99. g, err := NewGame()
  100. if err != nil {
  101. log.Fatal(err)
  102. }
  103. ebiten.SetWindowSize(screenWidth, screenHeight)
  104. ebiten.SetWindowTitle("WAV (Ebitengine Demo)")
  105. if err := ebiten.RunGame(g); err != nil {
  106. log.Fatal(err)
  107. }
  108. }