mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2025-02-05 16:17:45 -05:00
Stereo panning for sound effects (#654)
Randomly pan event sounds Adapted from ebiten example
This commit is contained in:
parent
42cd1e1a3b
commit
c840e140f7
@ -4,6 +4,7 @@ package d2interface
|
|||||||
type SoundEffect interface {
|
type SoundEffect interface {
|
||||||
Play()
|
Play()
|
||||||
Stop()
|
Stop()
|
||||||
|
SetPan(pan float64)
|
||||||
IsPlaying() bool
|
IsPlaying() bool
|
||||||
SetVolume(volume float64)
|
SetVolume(volume float64)
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package ebiten
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"math"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/audio"
|
"github.com/hajimehoshi/ebiten/audio"
|
||||||
"github.com/hajimehoshi/ebiten/audio/wav"
|
"github.com/hajimehoshi/ebiten/audio/wav"
|
||||||
@ -10,10 +11,45 @@ import (
|
|||||||
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type panStream struct {
|
||||||
|
audio.ReadSeekCloser
|
||||||
|
pan float64 // -1: left; 0: center; 1: right
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPanStreamFromReader(src audio.ReadSeekCloser) *panStream {
|
||||||
|
return &panStream{
|
||||||
|
ReadSeekCloser: src,
|
||||||
|
pan: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *panStream) Read(p []byte) (n int, err error) {
|
||||||
|
n, err = s.ReadSeekCloser.Read(p)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
// SoundEffect represents an ebiten implementation of a sound effect
|
// SoundEffect represents an ebiten implementation of a sound effect
|
||||||
type SoundEffect struct {
|
type SoundEffect struct {
|
||||||
player *audio.Player
|
player *audio.Player
|
||||||
volumeScale float64
|
volumeScale float64
|
||||||
|
panStream *panStream
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateSoundEffect creates a new instance of ebiten's sound effect implementation.
|
// CreateSoundEffect creates a new instance of ebiten's sound effect implementation.
|
||||||
@ -49,9 +85,11 @@ func CreateSoundEffect(sfx string, context *audio.Context, volume float64, loop
|
|||||||
|
|
||||||
if loop {
|
if loop {
|
||||||
s := audio.NewInfiniteLoop(d, d.Length())
|
s := audio.NewInfiniteLoop(d, d.Length())
|
||||||
player, err = audio.NewPlayer(context, s)
|
result.panStream = NewPanStreamFromReader(s)
|
||||||
|
player, err = audio.NewPlayer(context, result.panStream)
|
||||||
} else {
|
} else {
|
||||||
player, err = audio.NewPlayer(context, d)
|
result.panStream = NewPanStreamFromReader(d)
|
||||||
|
player, err = audio.NewPlayer(context, result.panStream)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -66,6 +104,10 @@ func CreateSoundEffect(sfx string, context *audio.Context, volume float64, loop
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v *SoundEffect) SetPan(pan float64) {
|
||||||
|
v.panStream.pan = pan
|
||||||
|
}
|
||||||
|
|
||||||
func (v *SoundEffect) SetVolume(volume float64) {
|
func (v *SoundEffect) SetVolume(volume float64) {
|
||||||
v.player.SetVolume(volume * v.volumeScale)
|
v.player.SetVolume(volume * v.volumeScale)
|
||||||
}
|
}
|
||||||
|
@ -56,6 +56,11 @@ func (s *Sound) update(elapsed float64) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetPan sets the stereo pan, range -1 to 1
|
||||||
|
func (s *Sound) SetPan(pan float64) {
|
||||||
|
s.effect.SetPan(pan)
|
||||||
|
}
|
||||||
|
|
||||||
// Play the sound
|
// Play the sound
|
||||||
func (s *Sound) Play() {
|
func (s *Sound) Play() {
|
||||||
log.Println("starting sound", s.entry.Handle)
|
log.Println("starting sound", s.entry.Handle)
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package d2audio
|
package d2audio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math/rand"
|
||||||
|
|
||||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -50,6 +52,11 @@ func (s *SoundEnvironment) Advance(elapsed float64) {
|
|||||||
s.eventTimer -= elapsed
|
s.eventTimer -= elapsed
|
||||||
if s.eventTimer < 0 {
|
if s.eventTimer < 0 {
|
||||||
s.eventTimer = float64(s.environment.EventDelay) / 25
|
s.eventTimer = float64(s.environment.EventDelay) / 25
|
||||||
s.engine.PlaySoundID(s.environment.DayEvent)
|
|
||||||
|
snd := s.engine.PlaySoundID(s.environment.DayEvent)
|
||||||
|
if snd != nil {
|
||||||
|
pan := (rand.Float64() * 2) - 1
|
||||||
|
snd.SetPan(pan)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user