Skip to content

Commit a287bf9

Browse files
Created set_position method.
1 parent b5f4afe commit a287bf9

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

utils/audio_player/bass_player.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,40 @@ def decrease_volume(self, step: float = 0.01) -> None:
102102
new_volume = max(self.volume - step, 0.0)
103103
self.set_volume(new_volume)
104104

105-
def forward(self, seconds: float = 10) -> None:
105+
def set_position(self, new_seconds: float) -> None:
106+
"""Sets the playback position to the specified number of seconds."""
107+
if self.current_channel:
108+
new_seconds = max(0.0, new_seconds)
109+
110+
duration = bass.BASS_ChannelBytes2Seconds(self.current_channel, bass.BASS_ChannelGetLength( self.current_channel, 0))
111+
new_seconds = min(new_seconds, duration)
112+
new_position = bass.BASS_ChannelSeconds2Bytes(self.current_channel, new_seconds, 0)
113+
bass.BASS_ChannelSetPosition(self.current_channel, new_position, 0)
114+
115+
def forward(self, seconds: float = 0.5) -> None:
106116
"""Forwards the audio playback by the specified number of seconds."""
107117
if self.current_channel:
108-
current_position = bass.BASS_ChannelGetPosition(self.current_channel)
109-
new_position = current_position + seconds
110-
bass.BASS_ChannelSetPosition(self.current_channel, new_position)
118+
current_position = bass.BASS_ChannelGetPosition(self.current_channel, 0)
119+
if current_position == -1:
120+
print("Error getting current position.")
121+
return
122+
123+
current_seconds = bass.BASS_ChannelBytes2Seconds(self.current_channel, current_position)
124+
new_seconds = current_seconds + seconds
125+
new_seconds = 111
126+
self.set_position(new_seconds)
111127

112-
def rewind(self, seconds: float = 10) -> None:
128+
def rewind(self, seconds: float = 0.5) -> None:
113129
"""Rewinds the audio playback by the specified number of seconds."""
114130
if self.current_channel:
115-
current_position = bass.BASS_ChannelGetPosition(self.current_channel)
116-
new_position = max(0.0, current_position - seconds)
117-
bass.BASS_ChannelSetPosition(self.current_channel, new_position)
131+
current_position = bass.BASS_ChannelGetPosition(self.current_channel, 0)
132+
if current_position == -1:
133+
print("Error getting current position.")
134+
return
135+
136+
current_seconds = bass.BASS_ChannelBytes2Seconds(self.current_channel, current_position)
137+
new_seconds = current_seconds - seconds
138+
self.set_position(new_seconds)
118139

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

0 commit comments

Comments
 (0)