main.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2022 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. "log"
  17. "math"
  18. "sync"
  19. "time"
  20. "github.com/hajimehoshi/ebiten/v2"
  21. "github.com/hajimehoshi/ebiten/v2/audio"
  22. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  23. )
  24. const (
  25. screenWidth = 640
  26. screenHeight = 480
  27. sampleRate = 48000
  28. )
  29. type Game struct {
  30. audioContext *audio.Context
  31. player *audio.Player
  32. sineWave *SineWave
  33. }
  34. type SineWave struct {
  35. frequency int
  36. minFrequency int
  37. maxFrequency int
  38. // pos is the position in the wave length in the range of [0, 1).
  39. pos float64
  40. m sync.Mutex
  41. }
  42. func NewSineWave() *SineWave {
  43. return &SineWave{
  44. frequency: 440,
  45. minFrequency: 440,
  46. maxFrequency: 880,
  47. }
  48. }
  49. func (s *SineWave) Update(raisePitch bool) {
  50. s.m.Lock()
  51. defer s.m.Unlock()
  52. if raisePitch {
  53. if s.frequency < s.maxFrequency {
  54. s.frequency += 10
  55. }
  56. } else {
  57. if s.frequency > s.minFrequency {
  58. s.frequency -= 10
  59. }
  60. }
  61. }
  62. func (s *SineWave) Read(buf []byte) (int, error) {
  63. s.m.Lock()
  64. defer s.m.Unlock()
  65. const bytesPerSample = 8
  66. n := len(buf) / bytesPerSample * bytesPerSample
  67. buf = buf[:n]
  68. length := sampleRate / float64(s.frequency)
  69. p := float64(length * s.pos)
  70. for i := 0; i < n/bytesPerSample; i++ {
  71. v := math.Float32bits(float32(math.Sin(2 * math.Pi * p / length)))
  72. buf[8*i] = byte(v)
  73. buf[8*i+1] = byte(v >> 8)
  74. buf[8*i+2] = byte(v >> 16)
  75. buf[8*i+3] = byte(v >> 24)
  76. buf[8*i+4] = byte(v)
  77. buf[8*i+5] = byte(v >> 8)
  78. buf[8*i+6] = byte(v >> 16)
  79. buf[8*i+7] = byte(v >> 24)
  80. p++
  81. }
  82. _, s.pos = math.Modf(p / length)
  83. return n, nil
  84. }
  85. func NewGame() *Game {
  86. return &Game{
  87. audioContext: audio.NewContext(sampleRate),
  88. }
  89. }
  90. func (g *Game) Update() error {
  91. if g.audioContext == nil {
  92. g.audioContext = audio.NewContext(sampleRate)
  93. }
  94. if g.player == nil {
  95. g.sineWave = NewSineWave()
  96. p, err := g.audioContext.NewPlayerF32(g.sineWave)
  97. if err != nil {
  98. return err
  99. }
  100. g.player = p
  101. g.player.Play()
  102. // Adjust the buffer size to reflect the audio source changes in real time.
  103. // Note that Ebitengine doesn't guarantee the audio quality when the buffer size is modified.
  104. // 1/20[s] should work in most cases, but this might cause glitches in some environments.
  105. g.player.SetBufferSize(time.Second / 20)
  106. }
  107. g.sineWave.Update(ebiten.IsKeyPressed(ebiten.KeyA))
  108. return nil
  109. }
  110. func (g *Game) Draw(screen *ebiten.Image) {
  111. ebitenutil.DebugPrint(screen, "This is an example of a real time PCM.\nPress and hold the A key.")
  112. }
  113. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  114. return screenWidth, screenHeight
  115. }
  116. func main() {
  117. ebiten.SetWindowSize(screenWidth, screenHeight)
  118. ebiten.SetWindowTitle("Real Time PCM (Ebitengine Demo)")
  119. if err := ebiten.RunGame(NewGame()); err != nil {
  120. log.Fatal(err)
  121. }
  122. }