Skip to content

Commit

Permalink
Fix volume control (pythonarcade#610)
Browse files Browse the repository at this point in the history
* add soloud dynamic library support for linux

* add soloud dynamic library support for linux

* update

* fix stop_sound

* update import

* add stop method to Sound class

* add soloud to setup.py

* update package data in setup.py

* add __init__ for soloud

* update import

* add voice_handle and fix volume methods

* Add demonstration of volume control to sound demo

Co-authored-by: Paul V Craven <paul@cravenfamily.com>
  • Loading branch information
chpurdy and pvcraven committed Mar 16, 2020
1 parent f551ab2 commit 57ee9f0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
18 changes: 17 additions & 1 deletion arcade/examples/sound_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def play(self):
""" Play """
self.sound.play(pan=self.pan, volume=self.volume)




class AudioStreamButton(arcade.SpriteSolidColor):
""" Button, click-for-streaming-sound """
Expand All @@ -41,6 +43,14 @@ def play(self):
""" Play """
self.sound.play(pan=self.pan, volume=self.volume)

def volume_up(self):
vol = self.sound.get_volume()
self.sound.set_volume(vol+.1)
print(f"Volume: {self.sound.get_volume()}")

def stream_position(self):
print(f"Current position: {self.sound.get_stream_position()}")


class MyGame(arcade.Window):
"""
Expand Down Expand Up @@ -197,7 +207,13 @@ def on_mouse_press(self, x, y, button, key_modifiers):
hit_sprites = arcade.get_sprites_at_point((x, y), self.button_sprites)
for sprite in hit_sprites:
button_sprite = typing.cast(SoundButton, sprite)
button_sprite.play()
if button == arcade.MOUSE_BUTTON_LEFT:
button_sprite.play()
elif button == arcade.MOUSE_BUTTON_RIGHT: # right click to increase volume on currently playing sound
if button_sprite.sound.voice_handle:
button_sprite.volume_up()
button_sprite.stream_position()


def on_mouse_release(self, x, y, button, key_modifiers):
"""
Expand Down
54 changes: 28 additions & 26 deletions arcade/sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def __init__(self, file_name: str, streaming: bool = False):
self.file_name: str = ""
self.wav_file:Union[soloud.WavStream, soloud.Wav]

self.voice_handle = None # needed for volume control

if not _audiolib:
return

Expand Down Expand Up @@ -52,11 +54,11 @@ def play(self, volume=1.0, pan=0.0):
if not _audiolib:
return

_audiolib.play(self.wav_file,
aVolume=volume,
aPan=pan,
aPaused=0,
aBus=0)
self.voice_handle = _audiolib.play(self.wav_file,
aVolume=volume,
aPan=pan,
aPaused=0,
aBus=0)

def stop(self):
"""
Expand All @@ -75,27 +77,27 @@ def get_length(self):
# --- These functions should work, but instead just return zero or otherwise
# don't appear functional.

# def get_volume(self):
# """ Get the current volume """
# if not _audiolib:
# return 0
# return _audiolib.get_volume(self.wav_file.objhandle)
#
# def set_volume(self, volume):
# """ Set the current volume. Doesn't seem to work. """
# if not _audiolib:
# return
# self.wav_file.set_volume(volume)
#
# def set_left_right_volume(self, left_volume, right_volume):
# """ Set absolute left/right volume """
# if not _audiolib:
# return
# _audiolib.set_pan_absolute(self.wav_file.objhandle, left_volume, right_volume)
#
# def get_stream_position(self):
# """ This always returns zero for some unknown reason. """
# return _audiolib.get_stream_position(self.wav_file.objhandle)
def get_volume(self):
""" Get the current volume """
if not _audiolib:
return 0
return _audiolib.get_volume(self.voice_handle)

def set_volume(self, volume):
""" Set the current volume. """
if not _audiolib:
return
_audiolib.set_volume(self.voice_handle, volume)

def set_left_right_volume(self, left_volume, right_volume):
""" Set absolute left/right volume """
if not _audiolib:
return
_audiolib.set_pan_absolute(self.voice_handle, left_volume, right_volume)

def get_stream_position(self):
""" This always returns zero for some unknown reason. """
return _audiolib.get_stream_position(self.voice_handle)


def load_sound(file_name: str):
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def execfile(filepath, globals=None, locals=None):
"Programming Language :: Python :: Implementation :: CPython",
"Topic :: Software Development :: Libraries :: Python Modules",
],

package_data={'arcade': ['resources/gui_themes/Fantasy/Buttons/*',
'resources/gui_themes/Fantasy/DialogueBox/*',
'resources/gui_themes/Fantasy/Menu/*',
Expand Down

0 comments on commit 57ee9f0

Please sign in to comment.