2016-03-07 20:20:59 +01:00
|
|
|
// Copyright 2016 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten"
|
|
|
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
|
|
|
"github.com/hajimehoshi/ebiten/exp/audio"
|
2016-03-12 19:00:05 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/exp/audio/vorbis"
|
2016-03-07 20:20:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
screenWidth = 320
|
|
|
|
screenHeight = 240
|
|
|
|
)
|
|
|
|
|
2016-03-08 18:45:56 +01:00
|
|
|
var (
|
|
|
|
audioContext *audio.Context
|
|
|
|
audioLoadingDone chan struct{}
|
|
|
|
audioLoaded bool
|
|
|
|
)
|
2016-03-07 20:20:59 +01:00
|
|
|
|
|
|
|
func update(screen *ebiten.Image) error {
|
2016-03-10 16:01:00 +01:00
|
|
|
audioContext.Update()
|
2016-03-08 18:45:56 +01:00
|
|
|
if !audioLoaded {
|
|
|
|
select {
|
|
|
|
case <-audioLoadingDone:
|
|
|
|
audioLoaded = true
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msg := fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS())
|
|
|
|
if !audioLoaded {
|
|
|
|
msg += "\nNow Loading..."
|
|
|
|
}
|
|
|
|
ebitenutil.DebugPrint(screen, msg)
|
2016-03-07 20:20:59 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Use a FLAC file so far: I couldn't find any good OGG/Vorbis decoder in pure Go.
|
|
|
|
f, err := ebitenutil.OpenFile("_resources/audio/ragtime.ogg")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
// TODO: sampleRate should be obtained from the ogg file.
|
|
|
|
audioContext = audio.NewContext(22050)
|
2016-03-08 18:45:56 +01:00
|
|
|
audioLoadingDone = make(chan struct{})
|
|
|
|
// TODO: This doesn't work synchronously on browsers because of decoding. Fix this.
|
|
|
|
go func() {
|
2016-03-12 19:00:05 +01:00
|
|
|
s, err := vorbis.Decode(audioContext, f)
|
2016-03-08 18:45:56 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p, err := audioContext.NewPlayer(s)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
close(audioLoadingDone)
|
|
|
|
p.Play()
|
|
|
|
}()
|
2016-03-09 19:02:55 +01:00
|
|
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio (Ebiten Demo)"); err != nil {
|
2016-03-07 20:20:59 +01:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|