Skip to content

Commit

Permalink
Updates to sound library
Browse files Browse the repository at this point in the history
  • Loading branch information
pvcraven committed Mar 9, 2020
1 parent e7b8769 commit 94b9b6a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 33 deletions.
61 changes: 32 additions & 29 deletions arcade/sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@


from pathlib import Path

from typing import Union

_audiolib = None
try:
soloud = None

import arcade.soloud.soloud as soloud

_audiolib = soloud.Soloud()
Expand All @@ -23,7 +21,10 @@ class Sound:
""" This class represents a sound you can play."""
def __init__(self, file_name: str, streaming: bool = False):
""" Create and load the sound. """
if not soloud:
self.file_name: str = ""
self.wav_file:Union[soloud.WavStream, soloud.Wav]

if not _audiolib:
return

# If we should pull from local resources, replace with proper path
Expand All @@ -40,7 +41,6 @@ def __init__(self, file_name: str, streaming: bool = False):
else:
self.wav_file = soloud.Wav()
self.wav_file.load(self.file_name)
self.handle = 0

def play(self, volume=1.0, pan=0.0):
"""
Expand All @@ -49,14 +49,14 @@ def play(self, volume=1.0, pan=0.0):
:param float volume: Volume, from 0=quiet to 1=loud
:param float pan: Pan, from -1=left to 0=centered to 1=right
"""
if not soloud:
if not _audiolib:
return

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

def stop(self):
"""
Expand All @@ -66,30 +66,33 @@ def stop(self):
return
self.wav_file.stop()

def get_volume(self):
""" Get the current volume """
if not soloud:
return
_audiolib.get_volume(self.handle)

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

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

def get_length(self):
""" Get length of audio in seconds """
if not soloud:
return 0
return self.wav_file.get_length()

# --- 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)
Expand Down
11 changes: 7 additions & 4 deletions tests/unit2/test_sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ def update(self, dt):
arcade.play_sound(self.laser_wav)

if self.frame_count == 60:
arcade.play_sound(self.laser_ogg)
self.laser_ogg.play()

if self.frame_count == 90:
arcade.play_sound(self.laser_mp3)
if self.frame_count == 180:
self.laser_mp3.play()

if self.frame_count == 200:
self.laser_mp3.stop()

def on_draw(self):
"""
Expand All @@ -43,5 +46,5 @@ def on_draw(self):
def test_main():
""" Main method """
window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT)
window.test(90)
window.test(240)
window.close()

0 comments on commit 94b9b6a

Please sign in to comment.