From fbea792fe402f90a4d4ad7ea4c49cbca4d308559 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 2 Aug 2023 01:20:57 +0900 Subject: [PATCH] audio: rename Seek -> SetPosition and Current -> Position Seek and Current are not removed but marked as deprecated. Closes #2698 --- audio/audio.go | 28 +++++++++++++++++++++------- audio/player.go | 6 +++--- examples/audio/main.go | 4 ++-- examples/audioinfiniteloop/main.go | 4 ++-- examples/audiopanning/main.go | 2 +- 5 files changed, 29 insertions(+), 15 deletions(-) diff --git a/audio/audio.go b/audio/audio.go index e3bb8611d..535951fe4 100644 --- a/audio/audio.go +++ b/audio/audio.go @@ -368,13 +368,20 @@ func (p *Player) Rewind() error { return p.p.Rewind() } +// SetPosition sets the position with the given offset. +// +// The passed source to NewPlayer must be io.Seeker, or SetPosition panics. +// +// SetPosition returns error when seeking the source stream returns an error. +func (p *Player) SetPosition(offset time.Duration) error { + return p.p.SetPosition(offset) +} + // Seek seeks the position with the given offset. // -// The passed source to NewPlayer must be io.Seeker, or Seek panics. -// -// Seek returns error when seeking the source stream returns error. +// Deprecated: as of v2.6. Use SetPosition instead. func (p *Player) Seek(offset time.Duration) error { - return p.p.Seek(offset) + return p.SetPosition(offset) } // Pause pauses the playing. @@ -382,12 +389,19 @@ func (p *Player) Pause() { p.p.Pause() } +// Position returns the current position in time. +// +// As long as the player continues to play, Position's returning value is increased monotonically, +// even though the source stream loops and its position goes back. +func (p *Player) Position() time.Duration { + return p.p.Position() +} + // Current returns the current position in time. // -// As long as the player continues to play, Current's returning value is increased monotonically, -// even though the source stream loops and its position goes back. +// Deprecated: as of v2.6. Use Position instead. func (p *Player) Current() time.Duration { - return p.p.Current() + return p.Position() } // Volume returns the current volume of this player [0-1]. diff --git a/audio/player.go b/audio/player.go index 5b0b991db..7b844a0fc 100644 --- a/audio/player.go +++ b/audio/player.go @@ -243,7 +243,7 @@ func (p *playerImpl) Close() error { return nil } -func (p *playerImpl) Current() time.Duration { +func (p *playerImpl) Position() time.Duration { p.m.Lock() defer p.m.Unlock() if err := p.ensurePlayer(); err != nil { @@ -256,10 +256,10 @@ func (p *playerImpl) Current() time.Duration { } func (p *playerImpl) Rewind() error { - return p.Seek(0) + return p.SetPosition(0) } -func (p *playerImpl) Seek(offset time.Duration) error { +func (p *playerImpl) SetPosition(offset time.Duration) error { p.m.Lock() defer p.m.Unlock() diff --git a/examples/audio/main.go b/examples/audio/main.go index e446d6d7b..a4e167c7b 100644 --- a/examples/audio/main.go +++ b/examples/audio/main.go @@ -199,7 +199,7 @@ func (p *Player) update() error { } if p.audioPlayer.IsPlaying() { - p.current = p.audioPlayer.Current() + p.current = p.audioPlayer.Position() } if err := p.seekBarIfNeeded(); err != nil { return err @@ -327,7 +327,7 @@ func (p *Player) seekBarIfNeeded() error { } pos := time.Duration(x-bx) * p.total / time.Duration(bw) p.current = pos - if err := p.audioPlayer.Seek(pos); err != nil { + if err := p.audioPlayer.SetPosition(pos); err != nil { return err } return nil diff --git a/examples/audioinfiniteloop/main.go b/examples/audioinfiniteloop/main.go index b50cb7cb6..e444c80eb 100644 --- a/examples/audioinfiniteloop/main.go +++ b/examples/audioinfiniteloop/main.go @@ -73,9 +73,9 @@ func (g *Game) Update() error { } func (g *Game) Draw(screen *ebiten.Image) { - pos := g.player.Current() + pos := g.player.Position() if pos > 5*time.Second { - pos = (g.player.Current()-5*time.Second)%(4*time.Second) + 5*time.Second + pos = (g.player.Position()-5*time.Second)%(4*time.Second) + 5*time.Second } msg := fmt.Sprintf(`TPS: %0.2f This is an example using diff --git a/examples/audiopanning/main.go b/examples/audiopanning/main.go index 12e018178..6ee5fbe43 100644 --- a/examples/audiopanning/main.go +++ b/examples/audiopanning/main.go @@ -104,7 +104,7 @@ func (g *Game) Update() error { } func (g *Game) Draw(screen *ebiten.Image) { - pos := g.player.Current() + pos := g.player.Position() msg := fmt.Sprintf(`TPS: %0.2f This is an example using stereo audio panning.