Skip to content

Commit

Permalink
Created set_position method.
Browse files Browse the repository at this point in the history
  • Loading branch information
MahmoudAtef999 committed Dec 10, 2024
1 parent b5f4afe commit a287bf9
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions utils/audio_player/bass_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,40 @@ def decrease_volume(self, step: float = 0.01) -> None:
new_volume = max(self.volume - step, 0.0)
self.set_volume(new_volume)

def forward(self, seconds: float = 10) -> None:
def set_position(self, new_seconds: float) -> None:
"""Sets the playback position to the specified number of seconds."""
if self.current_channel:
new_seconds = max(0.0, new_seconds)

duration = bass.BASS_ChannelBytes2Seconds(self.current_channel, bass.BASS_ChannelGetLength( self.current_channel, 0))
new_seconds = min(new_seconds, duration)
new_position = bass.BASS_ChannelSeconds2Bytes(self.current_channel, new_seconds, 0)
bass.BASS_ChannelSetPosition(self.current_channel, new_position, 0)

def forward(self, seconds: float = 0.5) -> None:
"""Forwards the audio playback by the specified number of seconds."""
if self.current_channel:
current_position = bass.BASS_ChannelGetPosition(self.current_channel)
new_position = current_position + seconds
bass.BASS_ChannelSetPosition(self.current_channel, new_position)
current_position = bass.BASS_ChannelGetPosition(self.current_channel, 0)
if current_position == -1:
print("Error getting current position.")
return

current_seconds = bass.BASS_ChannelBytes2Seconds(self.current_channel, current_position)
new_seconds = current_seconds + seconds
new_seconds = 111
self.set_position(new_seconds)

def rewind(self, seconds: float = 10) -> None:
def rewind(self, seconds: float = 0.5) -> None:
"""Rewinds the audio playback by the specified number of seconds."""
if self.current_channel:
current_position = bass.BASS_ChannelGetPosition(self.current_channel)
new_position = max(0.0, current_position - seconds)
bass.BASS_ChannelSetPosition(self.current_channel, new_position)
current_position = bass.BASS_ChannelGetPosition(self.current_channel, 0)
if current_position == -1:
print("Error getting current position.")
return

current_seconds = bass.BASS_ChannelBytes2Seconds(self.current_channel, current_position)
new_seconds = current_seconds - seconds
self.set_position(new_seconds)

def get_playback_status(self) -> PlaybackStatus:
"""Returns the current playback status."""
Expand Down

0 comments on commit a287bf9

Please sign in to comment.