2017-09-07 04:27:51 +02:00
|
|
|
// Copyright 2017 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-03-14 17:26:21 +01:00
|
|
|
// +build example jsgo
|
2017-09-07 04:27:51 +02:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2018-09-27 18:45:40 +02:00
|
|
|
"time"
|
2017-09-07 04:27:51 +02:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio/vorbis"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
|
|
|
raudio "github.com/hajimehoshi/ebiten/v2/examples/resources/audio"
|
2017-09-07 04:27:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
screenWidth = 320
|
|
|
|
screenHeight = 240
|
2018-09-27 20:55:28 +02:00
|
|
|
sampleRate = 22050
|
2018-09-27 18:52:30 +02:00
|
|
|
|
|
|
|
introLengthInSecond = 5
|
|
|
|
loopLengthInSecond = 4
|
2017-09-07 04:27:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var audioContext *audio.Context
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var err error
|
|
|
|
audioContext, err = audio.NewContext(sampleRate)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 11:18:45 +02:00
|
|
|
type Game struct {
|
|
|
|
player *audio.Player
|
|
|
|
}
|
2017-09-07 04:27:51 +02:00
|
|
|
|
2020-10-04 10:42:54 +02:00
|
|
|
func (g *Game) Update() error {
|
2020-04-12 11:18:45 +02:00
|
|
|
if g.player != nil {
|
2017-09-07 04:27:51 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-22 14:57:15 +02:00
|
|
|
// Decode an Ogg file.
|
|
|
|
// oggS is a decoded io.ReadCloser and io.Seeker.
|
2020-04-12 11:18:45 +02:00
|
|
|
oggS, err := vorbis.Decode(audioContext, audio.BytesReadSeekCloser(raudio.Ragtime_ogg))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an infinite loop stream from the decoded bytes.
|
|
|
|
// s is still an io.ReadCloser and io.Seeker.
|
|
|
|
s := audio.NewInfiniteLoopWithIntro(oggS, introLengthInSecond*4*sampleRate, loopLengthInSecond*4*sampleRate)
|
|
|
|
|
|
|
|
g.player, err = audio.NewPlayer(audioContext, s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Play the infinite-length stream. This never ends.
|
|
|
|
g.player.Play()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
|
|
|
pos := g.player.Current()
|
2018-09-27 18:45:40 +02:00
|
|
|
if pos > 5*time.Second {
|
2020-04-12 11:18:45 +02:00
|
|
|
pos = (g.player.Current()-5*time.Second)%(4*time.Second) + 5*time.Second
|
2018-09-27 18:45:40 +02:00
|
|
|
}
|
2018-09-29 19:27:33 +02:00
|
|
|
msg := fmt.Sprintf(`TPS: %0.2f
|
2018-10-13 07:46:13 +02:00
|
|
|
This is an example using
|
|
|
|
audio.NewInfiniteLoopWithIntro.
|
|
|
|
|
2018-09-27 18:52:30 +02:00
|
|
|
Intro: 0[s] - %[2]d[s]
|
|
|
|
Loop: %[2]d[s] - %[3]d[s]
|
2018-09-29 19:27:33 +02:00
|
|
|
Current: %0.2[4]f[s]`, ebiten.CurrentTPS(), introLengthInSecond, introLengthInSecond+loopLengthInSecond, float64(pos)/float64(time.Second))
|
2017-09-07 04:27:51 +02:00
|
|
|
ebitenutil.DebugPrint(screen, msg)
|
2020-04-12 11:18:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
|
|
|
return screenWidth, screenHeight
|
2017-09-07 04:27:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2020-04-12 11:18:45 +02:00
|
|
|
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
|
|
|
|
ebiten.SetWindowTitle("Audio Infinite Loop (Ebiten Demo)")
|
|
|
|
if err := ebiten.RunGame(&Game{}); err != nil {
|
2017-09-07 04:27:51 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|