2020-07-01 16:03:47 +02:00
|
|
|
// Copyright 2020 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.
|
|
|
|
|
2020-10-06 17:45:54 +02:00
|
|
|
// +build example
|
2020-07-01 16:03:47 +02:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
_ "image/png"
|
2020-10-07 16:09:02 +02:00
|
|
|
"io"
|
2020-07-01 16:03:47 +02:00
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"time"
|
|
|
|
|
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"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/examples/resources/images"
|
2020-07-01 16:03:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
screenWidth = 320
|
|
|
|
screenHeight = 240
|
|
|
|
sampleRate = 22050
|
|
|
|
)
|
|
|
|
|
|
|
|
var img *ebiten.Image
|
|
|
|
|
2020-10-04 19:25:11 +02:00
|
|
|
var audioContext = audio.NewContext(sampleRate)
|
2020-07-01 16:03:47 +02:00
|
|
|
|
|
|
|
type Game struct {
|
|
|
|
player *audio.Player
|
|
|
|
panstream *StereoPanStream
|
|
|
|
|
|
|
|
// panning goes from -1 to 1
|
|
|
|
// -1: 100% left channel, 0% right channel
|
|
|
|
// 0: 100% both channels
|
|
|
|
// 1: 0% left channel, 100% right channel
|
|
|
|
panning float64
|
|
|
|
|
|
|
|
count int
|
|
|
|
xpos float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) initAudio() {
|
|
|
|
if g.player != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode an Ogg file.
|
|
|
|
// oggS is a decoded io.ReadCloser and io.Seeker.
|
2020-10-07 16:09:02 +02:00
|
|
|
oggS, err := vorbis.Decode(audioContext, bytes.NewReader(raudio.Ragtime_ogg))
|
2020-07-01 16:03:47 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap the raw audio with the StereoPanStream
|
2021-02-21 18:00:49 +01:00
|
|
|
g.panstream = NewStereoPanStreamFromReader(audio.NewInfiniteLoop(oggS, oggS.Length()))
|
2020-07-01 16:03:47 +02:00
|
|
|
|
2020-07-01 16:14:45 +02:00
|
|
|
g.player, err = audio.NewPlayer(audioContext, g.panstream)
|
2020-07-01 16:03:47 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Play the infinite-length stream. This never ends.
|
|
|
|
g.player.Play()
|
|
|
|
}
|
|
|
|
|
|
|
|
// time is whithin the 0 ... 1 range
|
|
|
|
func lerp(a, b, t float64) float64 {
|
|
|
|
return a*(1-t) + b*t
|
|
|
|
}
|
|
|
|
|
2020-10-04 10:42:54 +02:00
|
|
|
func (g *Game) Update() error {
|
2020-07-01 16:03:47 +02:00
|
|
|
g.initAudio()
|
|
|
|
g.count++
|
|
|
|
r := float64(g.count) * ((1.0 / 60.0) * 2 * math.Pi) * 0.1 // full cycle every 10 seconds
|
|
|
|
g.xpos = (float64(screenWidth) / 2) + math.Cos(r)*(float64(screenWidth)/2)
|
|
|
|
g.panning = lerp(-1, 1, g.xpos/float64(screenWidth))
|
|
|
|
g.panstream.SetPan(g.panning)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
|
|
|
pos := g.player.Current()
|
|
|
|
msg := fmt.Sprintf(`TPS: %0.2f
|
|
|
|
This is an example using
|
|
|
|
stereo audio panning.
|
2020-07-01 16:14:45 +02:00
|
|
|
Current: %0.2f[s]
|
|
|
|
Panning: %.2f`, ebiten.CurrentTPS(), float64(pos)/float64(time.Second), g.panning)
|
2020-07-01 16:03:47 +02:00
|
|
|
ebitenutil.DebugPrint(screen, msg)
|
|
|
|
|
|
|
|
// draw image to show where the sound is at related to the screen
|
|
|
|
op := &ebiten.DrawImageOptions{}
|
|
|
|
op.GeoM.Translate(g.xpos-float64(img.Bounds().Dx()/2), screenHeight/2)
|
|
|
|
screen.DrawImage(img, op)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
|
|
|
return screenWidth, screenHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Decode image from a byte slice instead of a file so that
|
|
|
|
// this example works in any working directory.
|
|
|
|
// If you want to use a file, there are some options:
|
|
|
|
// 1) Use os.Open and pass the file to the image decoder.
|
|
|
|
// This is a very regular way, but doesn't work on browsers.
|
|
|
|
// 2) Use ebitenutil.OpenFile and pass the file to the image decoder.
|
|
|
|
// This works even on browsers.
|
|
|
|
// 3) Use ebitenutil.NewImageFromFile to create an ebiten.Image directly from a file.
|
|
|
|
// This also works on browsers.
|
|
|
|
rawimg, _, err := image.Decode(bytes.NewReader(images.Ebiten_png))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-10-05 18:03:30 +02:00
|
|
|
img = ebiten.NewImageFromImage(rawimg)
|
2020-07-01 16:03:47 +02:00
|
|
|
|
|
|
|
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
|
|
|
|
ebiten.SetWindowTitle("Audio Panning Loop (Ebiten Demo)")
|
|
|
|
g := &Game{}
|
|
|
|
if err := ebiten.RunGame(g); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StereoPanStream is an audio buffer that changes the stereo channel's signal
|
|
|
|
// based on the Panning.
|
|
|
|
type StereoPanStream struct {
|
2020-10-07 16:09:02 +02:00
|
|
|
io.ReadSeeker
|
2020-07-01 16:03:47 +02:00
|
|
|
pan float64 // -1: left; 0: center; 1: right
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StereoPanStream) Read(p []byte) (n int, err error) {
|
2020-10-07 16:09:02 +02:00
|
|
|
n, err = s.ReadSeeker.Read(p)
|
2020-07-01 16:03:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// This implementation uses a linear scale, ranging from -1 to 1, for stereo or mono sounds.
|
|
|
|
// If pan = 0.0, the balance for the sound in each speaker is at 100% left and 100% right.
|
|
|
|
// When pan is -1.0, only the left channel of the stereo sound is audible, when pan is 1.0,
|
|
|
|
// only the right channel of the stereo sound is audible.
|
|
|
|
// https://docs.unity3d.com/ScriptReference/AudioSource-panStereo.html
|
|
|
|
ls := math.Min(s.pan*-1+1, 1)
|
|
|
|
rs := math.Min(s.pan+1, 1)
|
|
|
|
for i := 0; i < len(p); i += 4 {
|
|
|
|
lc := int16(float64(int16(p[i])|int16(p[i+1])<<8) * ls)
|
|
|
|
rc := int16(float64(int16(p[i+2])|int16(p[i+3])<<8) * rs)
|
|
|
|
|
|
|
|
p[i] = byte(lc)
|
|
|
|
p[i+1] = byte(lc >> 8)
|
|
|
|
p[i+2] = byte(rc)
|
|
|
|
p[i+3] = byte(rc >> 8)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StereoPanStream) SetPan(pan float64) {
|
|
|
|
s.pan = math.Min(math.Max(-1, pan), 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StereoPanStream) Pan() float64 {
|
|
|
|
return s.pan
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStereoPanStreamFromReader returns a new StereoPanStream with a buffered src.
|
|
|
|
//
|
|
|
|
// The src's format must be linear PCM (16bits little endian, 2 channel stereo)
|
|
|
|
// without a header (e.g. RIFF header). The sample rate must be same as that
|
|
|
|
// of the audio context.
|
2020-10-07 16:09:02 +02:00
|
|
|
func NewStereoPanStreamFromReader(src io.ReadSeeker) *StereoPanStream {
|
2020-07-01 16:03:47 +02:00
|
|
|
return &StereoPanStream{
|
2020-10-07 16:09:02 +02:00
|
|
|
ReadSeeker: src,
|
2020-07-01 16:03:47 +02:00
|
|
|
}
|
|
|
|
}
|