Skip to content

Commit 55069e8

Browse files
committed
core: add some error handling and custom extensions
1 parent 0f9c379 commit 55069e8

File tree

1 file changed

+53
-29
lines changed

1 file changed

+53
-29
lines changed

extensions/core.pyx

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,20 @@ from cpython cimport Py_DECREF, Py_INCREF
33
cimport ga
44
cimport gau
55
cimport gc_common
6-
cimport gc_thread
6+
7+
8+
class SoundIOError(IOError):
9+
"""
10+
Raised when gorilla-audio can not load the sound from some reason
11+
(e.g. wrong format, file does not exist)
12+
"""
13+
14+
15+
class DeviceNotSupportedError(RuntimeError):
16+
"""
17+
Raised on attempt to access not supported device type
18+
"""
19+
720

821
def initialize():
922
gc_common.initialize(NULL)
@@ -19,6 +32,7 @@ def update():
1932

2033
cdef on_finish_callback(ga.Handle* in_handle, void* in_context):
2134
cdef CallbackContext context = <CallbackContext> in_context
35+
2236
context.callback(context.sound)
2337
# note: it was casted on void* so we need to manually decrease
2438
# reference counter
@@ -27,6 +41,7 @@ cdef on_finish_callback(ga.Handle* in_handle, void* in_context):
2741

2842

2943
cdef class CallbackContext(object):
44+
# python objects
3045
cdef object callback
3146
cdef Sound sound
3247

@@ -37,7 +52,7 @@ cdef class CallbackContext(object):
3752

3853
cdef class Manager(object):
3954
# c types
40-
cdef gau.Manager* p_manager
55+
cdef gau.gau_Manager* p_manager
4156
DEF DEFAULT_BUFFERS_NUMBER = 4
4257
DEF DEFAULT_BUFFER_SAMPLES = 512
4358

@@ -50,13 +65,17 @@ cdef class Manager(object):
5065
):
5166
# note: we do not use gau.manager_create()
5267
# instead of that we use the same defaults
53-
self.p_manager = gau.manager_create_custom(
68+
# as this function
69+
self.p_manager = <gau.Manager*> gau.manager_create_custom(
5470
device,
5571
thread_policy,
5672
buffers_number,
5773
buffer_samples,
5874
)
5975

76+
if gau.manager_device(self.p_manager) == NULL:
77+
raise DeviceNotSupportedError("This device is not supoprted")
78+
6079
def update(self):
6180
gau.manager_update(self.p_manager)
6281

@@ -67,7 +86,8 @@ cdef class Manager(object):
6786
cdef class Mixer(object):
6887
# c types
6988
cdef gau.Mixer* p_mixer
70-
# cython objects
89+
90+
# python objects
7191
cdef Manager manager
7292

7393
def __cinit__(self, Manager manager):
@@ -81,10 +101,10 @@ cdef class Mixer(object):
81101
cdef class Voice(object):
82102
# c types
83103
cdef int loop
84-
cdef gau.SampleSourceLoop* loop_src
85-
cdef ga.Handle* handle
104+
cdef gau.SampleSourceLoop* p_loop_src
105+
cdef ga.Handle* p_handle
86106

87-
# cython types
107+
# python objects
88108
cdef Sound sound
89109
cdef Mixer mixer
90110

@@ -109,39 +129,39 @@ cdef class Voice(object):
109129
# control reference counters
110130
Py_INCREF(context)
111131

112-
self.handle = gau.create_handle_sound(
132+
self.p_handle = gau.create_handle_sound(
113133
self.mixer.p_mixer,
114-
self.sound.sound,
134+
self.sound.p_sound,
115135
<ga.FinishCallback>&on_finish_callback,
116136
<void*>context,
117-
&self.loop_src if self.loop else NULL
137+
&self.p_loop_src if self.loop else NULL
118138
)
119139

120140
else:
121-
self.handle = gau.create_handle_sound(
141+
self.p_handle = gau.create_handle_sound(
122142
self.mixer.p_mixer,
123-
self.sound.sound,
143+
self.sound.p_sound,
124144
<ga.FinishCallback>&gau.on_finish_destroy,
125145
NULL,
126-
&self.loop_src if self.loop else NULL
146+
&self.p_loop_src if self.loop else NULL
127147
)
128148

129149
def play(self):
130-
ga.handle_play(self.handle)
150+
ga.handle_play(self.p_handle)
131151

132152
@property
133153
def playing(self):
134-
return bool(ga.handle_playing(self.handle))
154+
return bool(ga.handle_playing(self.p_handle))
135155

136156
def stop(self):
137-
ga.handle_stop(self.handle)
157+
ga.handle_stop(self.p_handle)
138158

139159
@property
140160
def stopped(self):
141-
return bool(ga.handle_stopped(self.handle))
161+
return bool(ga.handle_stopped(self.p_handle))
142162

143163
def __del__(self):
144-
ga.handle_destroy(self.handle)
164+
ga.handle_destroy(self.p_handle)
145165

146166
def toggle(self):
147167
if self.playing:
@@ -153,44 +173,48 @@ cdef class Voice(object):
153173
def __get__(self):
154174
cdef ga.float32 value
155175

156-
ga.handle_getParamf(self.handle, ga.HANDLE_PARAM_PITCH, &value)
176+
ga.handle_getParamf(self.p_handle, ga.HANDLE_PARAM_PITCH, &value)
157177
return value
158178

159179
def __set__(self, ga.float32 value):
160-
ga.handle_setParamf(self.handle, ga.HANDLE_PARAM_PITCH, value)
180+
ga.handle_setParamf(self.p_handle, ga.HANDLE_PARAM_PITCH, value)
161181

162182
property gain:
163183
def __get__(self):
164184
cdef ga.float32 value
165185

166-
ga.handle_getParamf(self.handle, ga.HANDLE_PARAM_GAIN, &value)
186+
ga.handle_getParamf(self.p_handle, ga.HANDLE_PARAM_GAIN, &value)
167187
return value
168188

169189
def __set__(self, ga.float32 value):
170-
ga.handle_setParamf(self.handle, ga.HANDLE_PARAM_GAIN, value)
190+
ga.handle_setParamf(self.p_handle, ga.HANDLE_PARAM_GAIN, value)
171191

172192
property pan:
173193
def __get__(self):
174194
cdef ga.float32 value
175195

176-
ga.handle_getParamf(self.handle, ga.HANDLE_PARAM_PAN, &value)
196+
ga.handle_getParamf(self.p_handle, ga.HANDLE_PARAM_PAN, &value)
177197
return value
178198

179199
def __set__(self, ga.float32 value):
180-
ga.handle_setParamf(self.handle, ga.HANDLE_PARAM_PAN, value)
200+
ga.handle_setParamf(self.p_handle, ga.HANDLE_PARAM_PAN, value)
201+
181202

182203
cdef class Sound(object):
183-
cdef ga.Sound* sound
204+
cdef ga.Sound* p_sound
184205

185206
def __cinit__(self):
186-
self.sound = NULL
207+
self.p_sound = NULL
187208

188209
def __init__(self, filename, ext, stream=False):
189210
if not stream:
190-
self.sound = gau.load_sound_file(filename, ext)
211+
self.p_sound = gau.load_sound_file(filename, ext)
191212
else:
192213
raise NotImplementedError("streams not implemented yet")
193214

215+
if self.p_sound == NULL:
216+
raise SoundIOError("could not load sound file %s as %s" % (filename, ext))
217+
194218
def play(self, on_finish=None, mixer=None):
195219
cdef Voice voice
196220

@@ -200,8 +224,8 @@ cdef class Sound(object):
200224
return voice
201225

202226
def __del__(self):
203-
"""Release sound (gorilla uses refcounting for that)"""
204-
ga.sound_release(self.sound)
227+
"""Release sound (gorilla-audio uses refcounting for that)"""
228+
ga.sound_release(self.p_sound)
205229

206230
# default manager and mixer
207231
default_manager = Manager()

0 commit comments

Comments
 (0)