1
0

main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. "fmt"
  18. "log"
  19. "time"
  20. "github.com/hajimehoshi/ebiten/v2"
  21. "github.com/hajimehoshi/ebiten/v2/audio"
  22. "github.com/hajimehoshi/ebiten/v2/audio/vorbis"
  23. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  24. raudio "github.com/hajimehoshi/ebiten/v2/examples/resources/audio"
  25. )
  26. const (
  27. screenWidth = 640
  28. screenHeight = 480
  29. sampleRate = 48000
  30. bytesPerSample = 8 // 2 channels * 4 bytes (32 bit float)
  31. introLengthInSecond = 5
  32. loopLengthInSecond = 4
  33. )
  34. type Game struct {
  35. player *audio.Player
  36. audioContext *audio.Context
  37. }
  38. func (g *Game) Update() error {
  39. if g.player != nil {
  40. return nil
  41. }
  42. if g.audioContext == nil {
  43. g.audioContext = audio.NewContext(sampleRate)
  44. }
  45. // Decode an Ogg file.
  46. // oggS is a decoded io.ReadCloser and io.Seeker.
  47. oggS, err := vorbis.DecodeF32(bytes.NewReader(raudio.Ragtime_ogg))
  48. if err != nil {
  49. return err
  50. }
  51. // Create an infinite loop stream from the decoded bytes.
  52. // s is still an io.ReadCloser and io.Seeker.
  53. s := audio.NewInfiniteLoopWithIntroF32(oggS, introLengthInSecond*bytesPerSample*sampleRate, loopLengthInSecond*bytesPerSample*sampleRate)
  54. g.player, err = g.audioContext.NewPlayerF32(s)
  55. if err != nil {
  56. return err
  57. }
  58. // Play the infinite-length stream. This never ends.
  59. g.player.Play()
  60. return nil
  61. }
  62. func (g *Game) Draw(screen *ebiten.Image) {
  63. pos := g.player.Position()
  64. if pos > 5*time.Second {
  65. pos = (g.player.Position()-5*time.Second)%(4*time.Second) + 5*time.Second
  66. }
  67. msg := fmt.Sprintf(`TPS: %0.2f
  68. This is an example using
  69. audio.NewInfiniteLoopWithIntroF32.
  70. Intro: 0[s] - %[2]d[s]
  71. Loop: %[2]d[s] - %[3]d[s]
  72. Current: %0.2[4]f[s]`, ebiten.ActualTPS(), introLengthInSecond, introLengthInSecond+loopLengthInSecond, float64(pos)/float64(time.Second))
  73. ebitenutil.DebugPrint(screen, msg)
  74. }
  75. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  76. return screenWidth, screenHeight
  77. }
  78. func main() {
  79. ebiten.SetWindowSize(screenWidth, screenHeight)
  80. ebiten.SetWindowTitle("Audio Infinite Loop (Ebitengine Demo)")
  81. if err := ebiten.RunGame(&Game{}); err != nil {
  82. log.Fatal(err)
  83. }
  84. }