main.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2020 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. "fmt"
  18. "image"
  19. _ "image/png"
  20. "io"
  21. "log"
  22. "math"
  23. "time"
  24. "github.com/hajimehoshi/ebiten/v2"
  25. "github.com/hajimehoshi/ebiten/v2/audio"
  26. "github.com/hajimehoshi/ebiten/v2/audio/vorbis"
  27. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  28. raudio "github.com/hajimehoshi/ebiten/v2/examples/resources/audio"
  29. "github.com/hajimehoshi/ebiten/v2/examples/resources/images"
  30. )
  31. const (
  32. screenWidth = 640
  33. screenHeight = 480
  34. sampleRate = 48000
  35. )
  36. var ebitenImage *ebiten.Image
  37. type Game struct {
  38. player *audio.Player
  39. panstream *StereoPanStream
  40. // panning goes from -1 to 1
  41. // -1: 100% left channel, 0% right channel
  42. // 0: 100% both channels
  43. // 1: 0% left channel, 100% right channel
  44. panning float64
  45. count int
  46. xpos float64
  47. audioContext *audio.Context
  48. }
  49. func (g *Game) initAudioIfNeeded() {
  50. if g.player != nil {
  51. return
  52. }
  53. if g.audioContext == nil {
  54. g.audioContext = audio.NewContext(sampleRate)
  55. }
  56. // Decode an Ogg file.
  57. // oggS is a decoded io.ReadCloser and io.Seeker.
  58. oggS, err := vorbis.DecodeF32(bytes.NewReader(raudio.Ragtime_ogg))
  59. if err != nil {
  60. log.Fatal(err)
  61. }
  62. // Wrap the raw audio with the StereoPanStream
  63. g.panstream = NewStereoPanStream(audio.NewInfiniteLoop(oggS, oggS.Length()))
  64. g.panstream.SetPan(g.panning)
  65. g.player, err = g.audioContext.NewPlayerF32(g.panstream)
  66. if err != nil {
  67. log.Fatal(err)
  68. }
  69. // Play the infinite-length stream. This never ends.
  70. g.player.Play()
  71. }
  72. // time is within the 0 ... 1 range
  73. func lerp(a, b, t float64) float64 {
  74. return a*(1-t) + b*t
  75. }
  76. func (g *Game) Update() error {
  77. g.count++
  78. r := float64(g.count) * ((1.0 / 60.0) * 2 * math.Pi) * 0.1 // full cycle every 10 seconds
  79. g.xpos = (float64(screenWidth) / 2) + math.Cos(r)*(float64(screenWidth)/2)
  80. g.panning = lerp(-1, 1, g.xpos/float64(screenWidth))
  81. // Initialize the audio after the panning is determined.
  82. g.initAudioIfNeeded()
  83. g.panstream.SetPan(g.panning)
  84. return nil
  85. }
  86. func (g *Game) Draw(screen *ebiten.Image) {
  87. pos := g.player.Position()
  88. msg := fmt.Sprintf(`TPS: %0.2f
  89. This is an example using
  90. stereo audio panning.
  91. Current: %0.2f[s]
  92. Panning: %.2f`, ebiten.ActualTPS(), float64(pos)/float64(time.Second), g.panning)
  93. ebitenutil.DebugPrint(screen, msg)
  94. // draw image to show where the sound is at related to the screen
  95. op := &ebiten.DrawImageOptions{}
  96. op.GeoM.Translate(g.xpos-float64(ebitenImage.Bounds().Dx()/2), screenHeight/2)
  97. screen.DrawImage(ebitenImage, op)
  98. }
  99. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  100. return screenWidth, screenHeight
  101. }
  102. func main() {
  103. // Decode an image from the image file's byte slice.
  104. img, _, err := image.Decode(bytes.NewReader(images.Ebiten_png))
  105. if err != nil {
  106. log.Fatal(err)
  107. }
  108. ebitenImage = ebiten.NewImageFromImage(img)
  109. ebiten.SetWindowSize(screenWidth, screenHeight)
  110. ebiten.SetWindowTitle("Audio Panning Loop (Ebitengine Demo)")
  111. g := &Game{}
  112. if err := ebiten.RunGame(g); err != nil {
  113. log.Fatal(err)
  114. }
  115. }
  116. // StereoPanStream is an audio buffer that changes the stereo channel's signal
  117. // based on the Panning.
  118. type StereoPanStream struct {
  119. io.ReadSeeker
  120. pan float64 // -1: left; 0: center; 1: right
  121. buf []byte
  122. }
  123. func (s *StereoPanStream) Read(p []byte) (int, error) {
  124. // If the stream has a buffer that was read in the previous time, use this first.
  125. var bufN int
  126. if len(s.buf) > 0 {
  127. bufN = copy(p, s.buf)
  128. s.buf = s.buf[bufN:]
  129. }
  130. readN, err := s.ReadSeeker.Read(p[bufN:])
  131. if err != nil && err != io.EOF {
  132. return 0, err
  133. }
  134. // Align the buffer size in multiples of 4. The extra part is pushed to the buffer for the
  135. // next time.
  136. totalN := bufN + readN
  137. extra := totalN - totalN/8*8
  138. s.buf = append(s.buf, p[totalN-extra:totalN]...)
  139. alignedN := totalN - extra
  140. // This implementation uses a linear scale, ranging from -1 to 1, for stereo or mono sounds.
  141. // If pan = 0.0, the balance for the sound in each speaker is at 100% left and 100% right.
  142. // When pan is -1.0, only the left channel of the stereo sound is audible, when pan is 1.0,
  143. // only the right channel of the stereo sound is audible.
  144. // https://docs.unity3d.com/ScriptReference/AudioSource-panStereo.html
  145. ls := float32(math.Min(s.pan*-1+1, 1))
  146. rs := float32(math.Min(s.pan+1, 1))
  147. for i := 0; i < alignedN; i += 8 {
  148. lc := math.Float32frombits(uint32(p[i])|(uint32(p[i+1])<<8)|(uint32(p[i+2])<<16)|(uint32(p[i+3])<<24)) * ls
  149. rc := math.Float32frombits(uint32(p[i+4])|(uint32(p[i+5])<<8)|(uint32(p[i+6])<<16)|(uint32(p[i+7])<<24)) * rs
  150. lcBits := math.Float32bits(lc)
  151. rcBits := math.Float32bits(rc)
  152. p[i] = byte(lcBits)
  153. p[i+1] = byte(lcBits >> 8)
  154. p[i+2] = byte(lcBits >> 16)
  155. p[i+3] = byte(lcBits >> 24)
  156. p[i+4] = byte(rcBits)
  157. p[i+5] = byte(rcBits >> 8)
  158. p[i+6] = byte(rcBits >> 16)
  159. p[i+7] = byte(rcBits >> 24)
  160. }
  161. return alignedN, err
  162. }
  163. func (s *StereoPanStream) SetPan(pan float64) {
  164. s.pan = math.Min(math.Max(-1, pan), 1)
  165. }
  166. func (s *StereoPanStream) Pan() float64 {
  167. return s.pan
  168. }
  169. // NewStereoPanStream returns a new StereoPanStream with a buffered src.
  170. //
  171. // The src's format must be linear PCM (16bits little endian, 2 channel stereo)
  172. // without a header (e.g. RIFF header). The sample rate must be same as that
  173. // of the audio context.
  174. func NewStereoPanStream(src io.ReadSeeker) *StereoPanStream {
  175. return &StereoPanStream{
  176. ReadSeeker: src,
  177. }
  178. }