ebiten/examples/audioinfiniteloop/main.go

98 lines
2.4 KiB
Go
Raw Normal View History

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.
// +build example jsgo
2017-09-07 04:27:51 +02:00
package main
import (
"fmt"
"log"
"time"
2017-09-07 04:27:51 +02:00
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/audio/vorbis"
2017-09-07 04:27:51 +02:00
"github.com/hajimehoshi/ebiten/ebitenutil"
2018-03-13 19:05:59 +01:00
raudio "github.com/hajimehoshi/ebiten/examples/resources/audio"
2017-09-07 04:27:51 +02:00
)
const (
screenWidth = 320
screenHeight = 240
sampleRate = 22050
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)
}
}
var player *audio.Player
func update(screen *ebiten.Image) error {
if player == nil {
2018-01-22 15:07:55 +01:00
// Decode the wav file.
// wavS is a decoded io.ReadCloser and io.Seeker.
oggS, err := vorbis.Decode(audioContext, audio.BytesReadSeekCloser(raudio.Ragtime_ogg))
2017-09-07 04:27:51 +02:00
if err != nil {
return err
}
2018-01-22 15:07:55 +01:00
// 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)
2017-09-07 04:27:51 +02:00
player, err = audio.NewPlayer(audioContext, s)
if err != nil {
return err
}
2018-01-22 15:07:55 +01:00
// Play the infinite-length stream. This never ends.
2017-09-07 04:27:51 +02:00
player.Play()
}
if ebiten.IsDrawingSkipped() {
2017-09-07 04:27:51 +02:00
return nil
}
pos := player.Current()
if pos > 5*time.Second {
pos = (player.Current()-5*time.Second)%(4*time.Second) + 5*time.Second
}
msg := fmt.Sprintf(`TPS: %0.2f
This is an example using
audio.NewInfiniteLoopWithIntro.
Intro: 0[s] - %[2]d[s]
Loop: %[2]d[s] - %[3]d[s]
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)
return nil
}
func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio Infinite Loop (Ebiten Demo)"); err != nil {
log.Fatal(err)
}
}