mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 10:48:53 +01:00
audio: Add Volume/SetVolume
This commit is contained in:
parent
55d61b8c67
commit
0662e1a1de
@ -68,6 +68,7 @@ var (
|
|||||||
seCh = make(chan *wav.Stream)
|
seCh = make(chan *wav.Stream)
|
||||||
mouseButtonState = map[ebiten.MouseButton]int{}
|
mouseButtonState = map[ebiten.MouseButton]int{}
|
||||||
keyState = map[ebiten.Key]int{}
|
keyState = map[ebiten.Key]int{}
|
||||||
|
volume128 = 128
|
||||||
)
|
)
|
||||||
|
|
||||||
func playerBarRect() (x, y, w, h int) {
|
func playerBarRect() (x, y, w, h int) {
|
||||||
@ -105,6 +106,26 @@ func (p *Player) updateSE() error {
|
|||||||
return sePlayer.Play()
|
return sePlayer.Play()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Player) updateVolume() error {
|
||||||
|
if p.audioPlayer == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if ebiten.IsKeyPressed(ebiten.KeyZ) {
|
||||||
|
volume128--
|
||||||
|
}
|
||||||
|
if ebiten.IsKeyPressed(ebiten.KeyX) {
|
||||||
|
volume128++
|
||||||
|
}
|
||||||
|
if volume128 < 0 {
|
||||||
|
volume128 = 0
|
||||||
|
}
|
||||||
|
if 128 < volume128 {
|
||||||
|
volume128 = 128
|
||||||
|
}
|
||||||
|
p.audioPlayer.SetVolume(float64(volume128) / 128)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Player) updatePlayPause() error {
|
func (p *Player) updatePlayPause() error {
|
||||||
if p.audioPlayer == nil {
|
if p.audioPlayer == nil {
|
||||||
return nil
|
return nil
|
||||||
@ -171,6 +192,9 @@ func update(screen *ebiten.Image) error {
|
|||||||
if err := musicPlayer.updateSE(); err != nil {
|
if err := musicPlayer.updateSE(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if err := musicPlayer.updateVolume(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
op := &ebiten.DrawImageOptions{}
|
op := &ebiten.DrawImageOptions{}
|
||||||
@ -198,6 +222,7 @@ func update(screen *ebiten.Image) error {
|
|||||||
msg := fmt.Sprintf(`FPS: %0.2f
|
msg := fmt.Sprintf(`FPS: %0.2f
|
||||||
Press S to toggle Play/Pause
|
Press S to toggle Play/Pause
|
||||||
Press P to play SE
|
Press P to play SE
|
||||||
|
Press Z or X to change volume of the music
|
||||||
%s`, ebiten.CurrentFPS(), currentTimeStr)
|
%s`, ebiten.CurrentFPS(), currentTimeStr)
|
||||||
if musicPlayer == nil {
|
if musicPlayer == nil {
|
||||||
msg += "\nNow Loading..."
|
msg += "\nNow Loading..."
|
||||||
|
@ -153,6 +153,7 @@ type Player struct {
|
|||||||
src io.ReadSeeker
|
src io.ReadSeeker
|
||||||
buf []byte
|
buf []byte
|
||||||
pos int64
|
pos int64
|
||||||
|
volume float64
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPlayer creates a new player with the given data to the given channel.
|
// NewPlayer creates a new player with the given data to the given channel.
|
||||||
@ -168,6 +169,7 @@ func (c *Context) NewPlayer(src io.ReadSeeker) (*Player, error) {
|
|||||||
context: c,
|
context: c,
|
||||||
src: src,
|
src: src,
|
||||||
buf: []byte{},
|
buf: []byte{},
|
||||||
|
volume: 1,
|
||||||
}
|
}
|
||||||
// Get the current position of the source.
|
// Get the current position of the source.
|
||||||
pos, err := p.src.Seek(0, 1)
|
pos, err := p.src.Seek(0, 1)
|
||||||
@ -191,6 +193,7 @@ func (p *Player) bufferToInt16(lengthInBytes int) []int16 {
|
|||||||
r := make([]int16, lengthInBytes/2)
|
r := make([]int16, lengthInBytes/2)
|
||||||
for i := 0; i < lengthInBytes/2; i++ {
|
for i := 0; i < lengthInBytes/2; i++ {
|
||||||
r[i] = int16(p.buf[2*i]) | (int16(p.buf[2*i+1]) << 8)
|
r[i] = int16(p.buf[2*i]) | (int16(p.buf[2*i+1]) << 8)
|
||||||
|
r[i] = int16(float64(r[i]) * p.volume)
|
||||||
}
|
}
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
@ -245,5 +248,19 @@ func (p *Player) Current() time.Duration {
|
|||||||
return time.Duration(p.pos/bytesPerSample/channelNum) * time.Second / time.Duration(p.context.sampleRate)
|
return time.Duration(p.pos/bytesPerSample/channelNum) * time.Second / time.Duration(p.context.sampleRate)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Volume / SetVolume?
|
func (p *Player) Volume() float64 {
|
||||||
|
return p.volume
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Player) SetVolume(volume float64) {
|
||||||
|
// TODO: What if volume is NaN?
|
||||||
|
if 1 < volume {
|
||||||
|
panic("audio: volume must <= 1")
|
||||||
|
}
|
||||||
|
if volume < 0 {
|
||||||
|
panic("audio: volume must >= 0")
|
||||||
|
}
|
||||||
|
p.volume = volume
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Panning
|
// TODO: Panning
|
||||||
|
Loading…
Reference in New Issue
Block a user