main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2016 Hajime Hoshi
  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. "fmt"
  17. "log"
  18. "math"
  19. "github.com/hajimehoshi/ebiten/v2"
  20. "github.com/hajimehoshi/ebiten/v2/audio"
  21. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  22. )
  23. const (
  24. screenWidth = 640
  25. screenHeight = 480
  26. sampleRate = 48000
  27. frequency = 440
  28. )
  29. // stream is an infinite stream of 440 Hz sine wave.
  30. type stream struct {
  31. pos int64
  32. }
  33. // Read is io.Reader's Read.
  34. //
  35. // Read fills the data with sine wave samples.
  36. func (s *stream) Read(buf []byte) (int, error) {
  37. const bytesPerSample = 8
  38. n := len(buf) / bytesPerSample * bytesPerSample
  39. const length = sampleRate / frequency
  40. for i := 0; i < n/bytesPerSample; i++ {
  41. v := math.Float32bits(float32(math.Sin(2 * math.Pi * float64(s.pos/bytesPerSample+int64(i)) / length)))
  42. buf[8*i] = byte(v)
  43. buf[8*i+1] = byte(v >> 8)
  44. buf[8*i+2] = byte(v >> 16)
  45. buf[8*i+3] = byte(v >> 24)
  46. buf[8*i+4] = byte(v)
  47. buf[8*i+5] = byte(v >> 8)
  48. buf[8*i+6] = byte(v >> 16)
  49. buf[8*i+7] = byte(v >> 24)
  50. }
  51. s.pos += int64(n)
  52. s.pos %= length * bytesPerSample
  53. return n, nil
  54. }
  55. // Close is io.Closer's Close.
  56. func (s *stream) Close() error {
  57. return nil
  58. }
  59. type Game struct {
  60. audioContext *audio.Context
  61. player *audio.Player
  62. }
  63. func (g *Game) Update() error {
  64. if g.audioContext == nil {
  65. g.audioContext = audio.NewContext(sampleRate)
  66. }
  67. if g.player == nil {
  68. // Pass the (infinite) stream to NewPlayer.
  69. // After calling Play, the stream never ends as long as the player object lives.
  70. var err error
  71. g.player, err = g.audioContext.NewPlayerF32(&stream{})
  72. if err != nil {
  73. return err
  74. }
  75. g.player.Play()
  76. }
  77. return nil
  78. }
  79. func (g *Game) Draw(screen *ebiten.Image) {
  80. msg := fmt.Sprintf("TPS: %0.2f\nThis is an example using infinite audio stream.", ebiten.ActualTPS())
  81. ebitenutil.DebugPrint(screen, msg)
  82. }
  83. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  84. return screenWidth, screenHeight
  85. }
  86. func main() {
  87. ebiten.SetWindowSize(screenWidth, screenHeight)
  88. ebiten.SetWindowTitle("Sine Wave (Ebitengine Demo)")
  89. if err := ebiten.RunGame(&Game{}); err != nil {
  90. log.Fatal(err)
  91. }
  92. }