@@ -3,7 +3,20 @@ from cpython cimport Py_DECREF, Py_INCREF
3
3
cimport ga
4
4
cimport gau
5
5
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
+
7
20
8
21
def initialize ():
9
22
gc_common.initialize(NULL )
@@ -19,6 +32,7 @@ def update():
19
32
20
33
cdef on_finish_callback(ga.Handle* in_handle, void * in_context):
21
34
cdef CallbackContext context = < CallbackContext> in_context
35
+
22
36
context.callback(context.sound)
23
37
# note: it was casted on void* so we need to manually decrease
24
38
# reference counter
@@ -27,6 +41,7 @@ cdef on_finish_callback(ga.Handle* in_handle, void* in_context):
27
41
28
42
29
43
cdef class CallbackContext(object ):
44
+ # python objects
30
45
cdef object callback
31
46
cdef Sound sound
32
47
@@ -37,7 +52,7 @@ cdef class CallbackContext(object):
37
52
38
53
cdef class Manager(object ):
39
54
# c types
40
- cdef gau.Manager * p_manager
55
+ cdef gau.gau_Manager * p_manager
41
56
DEF DEFAULT_BUFFERS_NUMBER = 4
42
57
DEF DEFAULT_BUFFER_SAMPLES = 512
43
58
@@ -50,13 +65,17 @@ cdef class Manager(object):
50
65
):
51
66
# note: we do not use gau.manager_create()
52
67
# 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(
54
70
device,
55
71
thread_policy,
56
72
buffers_number,
57
73
buffer_samples,
58
74
)
59
75
76
+ if gau.manager_device(self .p_manager) == NULL :
77
+ raise DeviceNotSupportedError(" This device is not supoprted" )
78
+
60
79
def update (self ):
61
80
gau.manager_update(self .p_manager)
62
81
@@ -67,7 +86,8 @@ cdef class Manager(object):
67
86
cdef class Mixer(object ):
68
87
# c types
69
88
cdef gau.Mixer* p_mixer
70
- # cython objects
89
+
90
+ # python objects
71
91
cdef Manager manager
72
92
73
93
def __cinit__ (self , Manager manager ):
@@ -81,10 +101,10 @@ cdef class Mixer(object):
81
101
cdef class Voice(object ):
82
102
# c types
83
103
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
86
106
87
- # cython types
107
+ # python objects
88
108
cdef Sound sound
89
109
cdef Mixer mixer
90
110
@@ -109,39 +129,39 @@ cdef class Voice(object):
109
129
# control reference counters
110
130
Py_INCREF(context)
111
131
112
- self .handle = gau.create_handle_sound(
132
+ self .p_handle = gau.create_handle_sound(
113
133
self .mixer.p_mixer,
114
- self .sound.sound ,
134
+ self .sound.p_sound ,
115
135
< ga.FinishCallback> & on_finish_callback,
116
136
< void * > context,
117
- & self .loop_src if self .loop else NULL
137
+ & self .p_loop_src if self .loop else NULL
118
138
)
119
139
120
140
else :
121
- self .handle = gau.create_handle_sound(
141
+ self .p_handle = gau.create_handle_sound(
122
142
self .mixer.p_mixer,
123
- self .sound.sound ,
143
+ self .sound.p_sound ,
124
144
< ga.FinishCallback> & gau.on_finish_destroy,
125
145
NULL ,
126
- & self .loop_src if self .loop else NULL
146
+ & self .p_loop_src if self .loop else NULL
127
147
)
128
148
129
149
def play (self ):
130
- ga.handle_play(self .handle )
150
+ ga.handle_play(self .p_handle )
131
151
132
152
@property
133
153
def playing (self ):
134
- return bool (ga.handle_playing(self .handle ))
154
+ return bool (ga.handle_playing(self .p_handle ))
135
155
136
156
def stop (self ):
137
- ga.handle_stop(self .handle )
157
+ ga.handle_stop(self .p_handle )
138
158
139
159
@property
140
160
def stopped (self ):
141
- return bool (ga.handle_stopped(self .handle ))
161
+ return bool (ga.handle_stopped(self .p_handle ))
142
162
143
163
def __del__ (self ):
144
- ga.handle_destroy(self .handle )
164
+ ga.handle_destroy(self .p_handle )
145
165
146
166
def toggle (self ):
147
167
if self .playing:
@@ -153,44 +173,48 @@ cdef class Voice(object):
153
173
def __get__ (self ):
154
174
cdef ga.float32 value
155
175
156
- ga.handle_getParamf(self .handle , ga.HANDLE_PARAM_PITCH, & value)
176
+ ga.handle_getParamf(self .p_handle , ga.HANDLE_PARAM_PITCH, & value)
157
177
return value
158
178
159
179
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)
161
181
162
182
property gain :
163
183
def __get__ (self ):
164
184
cdef ga.float32 value
165
185
166
- ga.handle_getParamf(self .handle , ga.HANDLE_PARAM_GAIN, & value)
186
+ ga.handle_getParamf(self .p_handle , ga.HANDLE_PARAM_GAIN, & value)
167
187
return value
168
188
169
189
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)
171
191
172
192
property pan :
173
193
def __get__ (self ):
174
194
cdef ga.float32 value
175
195
176
- ga.handle_getParamf(self .handle , ga.HANDLE_PARAM_PAN, & value)
196
+ ga.handle_getParamf(self .p_handle , ga.HANDLE_PARAM_PAN, & value)
177
197
return value
178
198
179
199
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
+
181
202
182
203
cdef class Sound(object ):
183
- cdef ga.Sound* sound
204
+ cdef ga.Sound* p_sound
184
205
185
206
def __cinit__ (self ):
186
- self .sound = NULL
207
+ self .p_sound = NULL
187
208
188
209
def __init__ (self , filename , ext , stream = False ):
189
210
if not stream:
190
- self .sound = gau.load_sound_file(filename, ext)
211
+ self .p_sound = gau.load_sound_file(filename, ext)
191
212
else :
192
213
raise NotImplementedError (" streams not implemented yet" )
193
214
215
+ if self .p_sound == NULL :
216
+ raise SoundIOError(" could not load sound file %s as %s " % (filename, ext))
217
+
194
218
def play (self , on_finish = None , mixer = None ):
195
219
cdef Voice voice
196
220
@@ -200,8 +224,8 @@ cdef class Sound(object):
200
224
return voice
201
225
202
226
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 )
205
229
206
230
# default manager and mixer
207
231
default_manager = Manager()
0 commit comments