-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sound code total remake #74
Changes from 23 commits
750e006
b159bf0
c995b5a
b780cc5
ad60499
4455dc5
c951fb4
8ac2387
95d0477
8dc6d37
f03d213
f0194ec
7228f4f
f30d1a5
674144f
6f2abfb
7671296
a9c6483
33bbaed
94df9de
a4dcee3
b6c09e1
5e17139
85bdf4c
aefaa26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,8 +208,44 @@ def __iter__(self): return iter(self._shape) | |
def __len__(self): return len(self._shape._shapes) | ||
|
||
class Sound(object): | ||
number_of_sounds = 0 | ||
def __init__(self, url): | ||
self.sound = slNewSound(url) | ||
if not pygame.mixer.get_init(): | ||
pygame.mixer.init() | ||
pygame.mixer.set_num_channels(1) | ||
|
||
if not isinstance(url, str): | ||
callSpec = '{className}.{attr}'.format(className=t('Sound'), attr=t('url')) | ||
err = t( | ||
'{{error}}: {{callSpec}} should be {{typeName}} (but {{value}} is of type {{valueType}})', | ||
{'error': t('TypeError'), 'callSpec': callSpec, 'typeName': 'string', 'value': repr(url), 'valueType': type(url).__name__} | ||
) | ||
raise Exception(err) | ||
|
||
Sound.number_of_sounds += 1 | ||
if pygame.mixer.get_num_channels()==Sound.number_of_sounds: | ||
pygame.mixer.set_num_channels(Sound.number_of_sounds * 2) | ||
|
||
if url.startswith('file://'): | ||
url = url.split('//')[-1] | ||
if url.startswith('http'): | ||
for i in range(10): | ||
try: | ||
response = webrequest.get(url) | ||
self.sound = io.BytesIO(response.read()) | ||
except: | ||
if i<9: | ||
continue | ||
else: | ||
raise Exception('Failed to load sound data') | ||
break | ||
Comment on lines
+232
to
+241
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Request change: I don't think this is a good way to handle failed requests; something like the image fetching logic might be more appropriate here (also, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The rationale behind this code is to make the cmu_graphics more robust; if a network error happens, we don't just want to directly crash the program and instead try again a couple of times. In this spirit, I actually think that the sound fetching logic should be updated to reflect the behaviour seen here pyThrow isn't defined in cmu_graphics.py nor was it ever used there; instead, normal exception was used. pyThrow is only defined inside shape_logic.py and is merely a simple wrapper of the exception class. We could do something about this, but if we are going to, we need to be consistent with it. So, any new thoughts on this? |
||
elif hasattr(__main__, '__file__'): | ||
self.sound = os.path.abspath(os.path.join(__main__.__file__, "..", url)) | ||
else: | ||
self.sound = os.path.abspath(os.path.join(os.getcwd(), url)) | ||
|
||
self.sound = pygame.mixer.Sound(self.sound) | ||
self.channel = None | ||
|
||
def play(self, **kwargs): | ||
default_kwargs = {'loop': False, 'restart': False} | ||
|
@@ -227,10 +263,34 @@ def play(self, **kwargs): | |
raise Exception('The loop argument to Sound.play must be True or False, got ' + repr(loop)) | ||
if not isinstance(restart, bool): | ||
raise Exception('The restart argument to Sound.play must be True or False, got ' + repr(restart)) | ||
self.sound.play(loop, restart) | ||
|
||
loop = -1 if loop else 0 | ||
if not self.channel or not self.channel.get_busy(): | ||
self.channel = self.sound.play(loops=loop) | ||
elif restart and self.channel.get_sound() != self.sound: | ||
self.channel = self.sound.play(loops=loop) | ||
elif restart: | ||
self.channel.stop() | ||
self.channel = self.sound.play(loops=loop) | ||
else: | ||
self.channel.unpause() | ||
|
||
def pause(self): | ||
self.sound.pause() | ||
self.channel.pause() | ||
|
||
def setVolume(self, volume: float): | ||
""" | ||
Volume in the range of 0.0 to 1.0 (inclusive)\n | ||
If value < 0.0, the volume will not be changed\n | ||
If value > 1.0, the volume will be set to 1.0 | ||
""" | ||
self.sound.set_volume(volume) | ||
|
||
def getVolume(self): | ||
""" | ||
Returns the volume (range: 0.0 - 1.0 (inclusive)) | ||
""" | ||
return self.sound.get_volume() | ||
|
||
SHAPES = [ Arc, Circle, Image, Label, Line, Oval, | ||
Polygon, Rect, RegularPolygon, Star, ] | ||
|
@@ -295,7 +355,6 @@ def translateKeyName(keyName, originalLanguage): | |
return KeyName(TRANSLATED_KEY_NAMES[originalLanguage].get(keyName, keyName)) | ||
|
||
def cleanAndClose(): | ||
shape_logic.cleanSoundProcesses() | ||
try: | ||
app._app.callUserFn('onAppStop', (), redraw=False) | ||
except: | ||
|
@@ -1012,6 +1071,7 @@ def interact(self): | |
|
||
import os | ||
import sys | ||
import io | ||
from datetime import datetime | ||
from datetime import timedelta | ||
import json | ||
|
@@ -1122,7 +1182,6 @@ def print_debug_info(): | |
slGet = sli.slGet | ||
rgb = sli.rgb | ||
gradient = sli.gradient | ||
slNewSound = sli.newSound | ||
toEnglish = sli.toEnglish | ||
accentCombinations = sli.accentCombinations | ||
t = sli.t | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Request change: code style (whitespace around operators)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done