~ubuntu-branches/ubuntu/trusty/python-sfml/trusty

« back to all changes in this revision

Viewing changes to .pc/spelling-length.patch/src/sfml/audio.pyx

  • Committer: Package Import Robot
  • Author(s): James Cowgill
  • Date: 2013-12-09 17:50:52 UTC
  • mfrom: (1.1.3) (8.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20131209175052-11v6drpb6g3yksst
Tags: 1.5.1.is.1.3+dfsg-1
* New upstream version 1.3 (from python-sfml.org)
  - This is a complete rewrite of the Python bindings for SFML2, and
    the new maintainer is using a different version numbering scheme.
* Added myself to the list of uploaders
* Change package priority from extra to optional
* Bumped standards version (to 3.9.5) and debhelper compat (to 9)
* Added Python 3 and documentation packages
* Improve package description for debug packages

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python3
 
2
# -*- coding: utf-8 -*-
 
3
#
 
4
# pySFML - Python bindings for SFML
 
5
# Copyright 2012-2013, Jonathan De Wachter <dewachter.jonathan@gmail.com>
 
6
#
 
7
# This software is released under the LGPLv3 license.
 
8
# You should have received a copy of the GNU Lesser General Public License
 
9
# along with this program. If not, see <http://www.gnu.org/licenses/>.
 
10
 
 
11
#from libc.stdlib cimport malloc, free
 
12
#from cython.operator cimport preincrement as preinc, dereference as deref
 
13
 
 
14
cimport cython
 
15
from libc.stdlib cimport malloc, free
 
16
from libc.string cimport memcpy
 
17
 
 
18
cimport libcpp.sfml as sf
 
19
from libcpp.sfml cimport Int8, Int16, Int32, Int64
 
20
from libcpp.sfml cimport Uint8, Uint16, Uint32, Uint64
 
21
from libcpp.sfml cimport Vector3f
 
22
 
 
23
 
 
24
cdef extern from "pysfml/system_api.h":
 
25
        object popLastErrorMessage()
 
26
        int import_sfml__system()
 
27
import_sfml__system()
 
28
 
 
29
cdef extern from "DerivableSoundStream.hpp":
 
30
        cdef cppclass DerivableSoundStream:
 
31
                DerivableSoundStream(void*)
 
32
                void initialize(unsigned int, unsigned int)
 
33
 
 
34
cdef extern from "DerivableSoundRecorder.hpp":
 
35
        cdef cppclass DerivableSoundRecorder:
 
36
                DerivableSoundRecorder(void*)
 
37
 
 
38
from pysfml.system cimport Vector3, Time
 
39
from pysfml.system cimport to_vector3
 
40
 
 
41
cdef Time wrap_time(sf.Time* p):
 
42
        cdef Time r = Time.__new__(Time)
 
43
        r.p_this = p
 
44
        return r
 
45
 
 
46
 
 
47
cdef class Listener:
 
48
        def __init__(self):
 
49
                NotImplementedError("This class is not meant to be instanciated!")
 
50
 
 
51
        @classmethod
 
52
        def get_global_volume(cls):
 
53
                return sf.listener.getGlobalVolume()
 
54
 
 
55
        @classmethod
 
56
        def set_global_volume(cls, float volume):
 
57
                sf.listener.setGlobalVolume(volume)
 
58
 
 
59
        @classmethod
 
60
        def get_position(cls):
 
61
                cdef Vector3f v = sf.listener.getPosition()
 
62
                return to_vector3(&v)
 
63
 
 
64
        @classmethod
 
65
        def set_position(cls, position):
 
66
                x, y, z = position
 
67
                sf.listener.setPosition(x, y, z)
 
68
 
 
69
        @classmethod
 
70
        def get_direction(cls):
 
71
                cdef Vector3f v = sf.listener.getDirection()
 
72
                return to_vector3(&v)
 
73
 
 
74
        @classmethod
 
75
        def set_direction(cls, direction):
 
76
                x, y, z = direction
 
77
                sf.listener.setDirection(x, y, z)
 
78
 
 
79
cdef class Chunk:
 
80
        cdef Int16* m_samples
 
81
        cdef size_t m_sampleCount
 
82
        cdef bint   delete_this
 
83
 
 
84
        def __cinit__(self):
 
85
                self.m_samples = NULL
 
86
                self.m_sampleCount = 0
 
87
                self.delete_this = False
 
88
 
 
89
        def __dealloc__(self):
 
90
                if self.delete_this:
 
91
                        free(self.m_samples)
 
92
 
 
93
        def __len__(self):
 
94
                return self.m_sampleCount
 
95
 
 
96
        def __getitem__(self, size_t key):
 
97
                return self.m_samples[key]
 
98
 
 
99
        def __setitem__(self, size_t key, Int16 other):
 
100
                self.m_samples[key] = other
 
101
 
 
102
        property data:
 
103
                def __get__(self):
 
104
                        return (<char*>self.m_samples)[:len(self)*2]
 
105
 
 
106
                def __set__(self, bdata):
 
107
                        cdef char* data = <bytes>bdata
 
108
 
 
109
                        if len(bdata) % 2:
 
110
                                raise ValueError("Chunk data lenght must be even as it represents a 16bit array")
 
111
 
 
112
                        if self.delete_this:
 
113
                                free(self.m_samples)
 
114
                                self.m_sampleCount = 0
 
115
 
 
116
                        self.m_samples = <Int16*>malloc(len(bdata))
 
117
                        memcpy(self.m_samples, data, len(bdata))
 
118
                        self.m_sampleCount = len(bdata) // 2
 
119
 
 
120
                        self.delete_this = True
 
121
 
 
122
cdef api object create_chunk():
 
123
        cdef Chunk r = Chunk.__new__(Chunk)
 
124
        r.m_samples = NULL
 
125
        r.m_sampleCount = 0
 
126
        r.delete_this = False
 
127
        return r
 
128
 
 
129
cdef api Int16* terminate_chunk(chunk):
 
130
        cdef Chunk p = <Chunk>chunk
 
131
        p.delete_this = False
 
132
        return p.m_samples
 
133
 
 
134
cdef api object wrap_chunk(Int16* samples, unsigned int sample_count, bint delete):
 
135
        cdef Chunk r = Chunk.__new__(Chunk)
 
136
        r.m_samples = samples
 
137
        r.m_sampleCount = sample_count
 
138
        r.delete_this = delete
 
139
        return r
 
140
 
 
141
cdef class SoundBuffer:
 
142
        cdef sf.SoundBuffer *p_this
 
143
        cdef bint                delete_this
 
144
 
 
145
        def __init__(self):
 
146
                raise UserWarning("Use specific methods")
 
147
 
 
148
        def __dealloc__(self):
 
149
                if self.delete_this: del self.p_this
 
150
 
 
151
        def __repr__(self): pass
 
152
        def __str__(self): pass
 
153
 
 
154
        @classmethod
 
155
        def from_file(cls, filename):
 
156
                cdef sf.SoundBuffer *p = new sf.SoundBuffer()
 
157
                cdef char* encoded_filename
 
158
 
 
159
                encoded_filename_temporary = filename.encode('UTF-8')
 
160
                encoded_filename = encoded_filename_temporary
 
161
 
 
162
                if p.loadFromFile(encoded_filename): return wrap_soundbuffer(p)
 
163
 
 
164
                del p
 
165
                raise IOError(popLastErrorMessage())
 
166
 
 
167
        @classmethod
 
168
        def from_memory(cls, bytes data):
 
169
                cdef sf.SoundBuffer *p = new sf.SoundBuffer()
 
170
 
 
171
                if p.loadFromMemory(<char*>data, len(data)): return wrap_soundbuffer(p)
 
172
 
 
173
                del p
 
174
                raise IOError(popLastErrorMessage())
 
175
 
 
176
        @classmethod
 
177
        def from_samples(cls, Chunk samples, unsigned int channel_count, unsigned int sample_rate):
 
178
                cdef sf.SoundBuffer *p = new sf.SoundBuffer()
 
179
 
 
180
                if p.loadFromSamples(samples.m_samples, samples.m_sampleCount, channel_count, sample_rate):
 
181
                        return wrap_soundbuffer(p)
 
182
 
 
183
                del p
 
184
                raise IOError(popLastErrorMessage())
 
185
 
 
186
        def to_file(self, filename):
 
187
                cdef char* encoded_filename
 
188
 
 
189
                encoded_filename_temporary = filename.encode('UTF-8')
 
190
                encoded_filename = encoded_filename_temporary
 
191
 
 
192
                self.p_this.saveToFile(encoded_filename)
 
193
 
 
194
        property samples:
 
195
                def __get__(self):
 
196
                        cdef Chunk r = Chunk.__new__(Chunk)
 
197
                        r.m_samples = <Int16*>self.p_this.getSamples()
 
198
                        r.m_sampleCount = self.p_this.getSampleCount()
 
199
                        return r
 
200
 
 
201
        property sample_rate:
 
202
                def __get__(self):
 
203
                        return self.p_this.getSampleRate()
 
204
 
 
205
        property channel_count:
 
206
                def __get__(self):
 
207
                        return self.p_this.getChannelCount()
 
208
 
 
209
        property duration:
 
210
                def __get__(self):
 
211
                        cdef sf.Time* p = new sf.Time()
 
212
                        p[0] = self.p_this.getDuration()
 
213
                        return wrap_time(p)
 
214
 
 
215
cdef SoundBuffer wrap_soundbuffer(sf.SoundBuffer *p, bint delete_this=True):
 
216
        cdef SoundBuffer r = SoundBuffer.__new__(SoundBuffer)
 
217
        r.p_this = p
 
218
        r.delete_this = delete_this
 
219
        return r
 
220
 
 
221
 
 
222
cdef class SoundSource:
 
223
        STOPPED = sf.soundsource.Stopped
 
224
        PAUSED = sf.soundsource.Paused
 
225
        PLAYING = sf.soundsource.Playing
 
226
 
 
227
        cdef sf.SoundSource *p_soundsource
 
228
 
 
229
        def __init__(self, *args, **kwargs):
 
230
                raise UserWarning("This class is not meant to be used directly")
 
231
 
 
232
        property pitch:
 
233
                def __get__(self):
 
234
                        return self.p_soundsource.getPitch()
 
235
 
 
236
                def __set__(self, float pitch):
 
237
                        self.p_soundsource.setPitch(pitch)
 
238
 
 
239
        property volume:
 
240
                def __get__(self):
 
241
                        return self.p_soundsource.getVolume()
 
242
 
 
243
                def __set__(self, float volume):
 
244
                        self.p_soundsource.setVolume(volume)
 
245
 
 
246
        property position:
 
247
                def __get__(self):
 
248
                        cdef Vector3f v = self.p_soundsource.getPosition()
 
249
                        return to_vector3(&v)
 
250
 
 
251
                def __set__(self, position):
 
252
                        x, y, z = position
 
253
                        self.p_soundsource.setPosition(x, y, z)
 
254
 
 
255
        property relative_to_listener:
 
256
                def __get__(self):
 
257
                        return self.p_soundsource.isRelativeToListener()
 
258
 
 
259
                def __set__(self, bint relative):
 
260
                        self.p_soundsource.setRelativeToListener(relative)
 
261
 
 
262
        property min_distance:
 
263
                def __get__(self):
 
264
                        return self.p_soundsource.getMinDistance()
 
265
 
 
266
                def __set__(self, float distance):
 
267
                        self.p_soundsource.setMinDistance(distance)
 
268
 
 
269
        property attenuation:
 
270
                def __get__(self):
 
271
                        return self.p_soundsource.getAttenuation()
 
272
 
 
273
                def __set__(self, float attenuation):
 
274
                        self.p_soundsource.setAttenuation(attenuation)
 
275
 
 
276
 
 
277
cdef class Sound(SoundSource):
 
278
        cdef sf.Sound *p_this
 
279
        cdef SoundBuffer   m_buffer
 
280
 
 
281
        def __init__(self, SoundBuffer buffer=None):
 
282
                self.p_this = new sf.Sound()
 
283
                self.p_soundsource = <sf.SoundSource*>self.p_this
 
284
 
 
285
                if buffer: self.buffer = buffer
 
286
 
 
287
        def __dealloc__(self):
 
288
                del self.p_this
 
289
 
 
290
        def __repr__(self):
 
291
                return "sf.Sound()"
 
292
 
 
293
        def play(self):
 
294
                self.p_this.play()
 
295
 
 
296
        def pause(self):
 
297
                self.p_this.pause()
 
298
 
 
299
        def stop(self):
 
300
                self.p_this.stop()
 
301
 
 
302
        property buffer:
 
303
                def __get__(self):
 
304
                        return self.m_buffer
 
305
 
 
306
                def __set__(self, SoundBuffer buffer):
 
307
                        self.p_this.setBuffer(buffer.p_this[0])
 
308
                        self.m_buffer = buffer
 
309
 
 
310
        property loop:
 
311
                def __get__(self):
 
312
                        return self.p_this.getLoop()
 
313
 
 
314
                def __set__(self, bint loop):
 
315
                        self.p_this.setLoop(loop)
 
316
 
 
317
        property playing_offset:
 
318
                def __get__(self):
 
319
                        cdef sf.Time* p = new sf.Time()
 
320
                        p[0] = self.p_this.getPlayingOffset()
 
321
                        return wrap_time(p)
 
322
 
 
323
                def __set__(self, Time time_offset):
 
324
                        self.p_this.setPlayingOffset(time_offset.p_this[0])
 
325
 
 
326
        property status:
 
327
                def __get__(self):
 
328
                        return self.p_this.getStatus()
 
329
 
 
330
 
 
331
cdef class SoundStream(SoundSource):
 
332
        cdef sf.SoundStream *p_soundstream
 
333
 
 
334
        def __init__(self):
 
335
                if self.__class__ == SoundStream:
 
336
                        raise NotImplementedError("SoundStream is abstract")
 
337
 
 
338
                elif self.__class__ not in [Music]:
 
339
                        self.p_soundstream = <sf.SoundStream*> new DerivableSoundStream(<void*>self)
 
340
                        self.p_soundsource = <sf.SoundSource*>self.p_soundstream
 
341
 
 
342
        def play(self):
 
343
                self.p_soundstream.play()
 
344
 
 
345
        def pause(self):
 
346
                self.p_soundstream.pause()
 
347
 
 
348
        def stop(self):
 
349
                self.p_soundstream.stop()
 
350
 
 
351
        property channel_count:
 
352
                def __get__(self):
 
353
                        return self.p_soundstream.getChannelCount()
 
354
 
 
355
        property sample_rate:
 
356
                def __get__(self):
 
357
                        return self.p_soundstream.getSampleRate()
 
358
 
 
359
        property status:
 
360
                def __get__(self):
 
361
                        return self.p_soundstream.getStatus()
 
362
 
 
363
        property playing_offset:
 
364
                def __get__(self):
 
365
                        cdef sf.Time* p = new sf.Time()
 
366
                        p[0] = self.p_soundstream.getPlayingOffset()
 
367
                        return wrap_time(p)
 
368
 
 
369
                def __set__(self, Time time_offset):
 
370
                        self.p_soundstream.setPlayingOffset(time_offset.p_this[0])
 
371
 
 
372
        property loop:
 
373
                def __get__(self):
 
374
                        return self.p_soundstream.getLoop()
 
375
 
 
376
                def __set__(self, bint loop):
 
377
                        self.p_soundstream.setLoop(loop)
 
378
 
 
379
        def initialize(self, unsigned int channel_count, unsigned int sample_rate):
 
380
                if self.__class__ not in [Music]:
 
381
                        (<DerivableSoundStream*>self.p_soundstream).initialize(channel_count, sample_rate)
 
382
 
 
383
        def on_get_data(self, data): pass
 
384
        def on_seek(self, time_offset): pass
 
385
 
 
386
cdef class Music(SoundStream):
 
387
        cdef sf.Music *p_this
 
388
 
 
389
        def __init__(self):
 
390
                raise UserWarning("Use specific constructor")
 
391
 
 
392
        def __dealloc__(self):
 
393
                del self.p_this
 
394
 
 
395
        @classmethod
 
396
        def from_file(cls, filename):
 
397
                cdef sf.Music *p = new sf.Music()
 
398
                cdef char* encoded_filename
 
399
 
 
400
                encoded_filename_temporary = filename.encode('UTF-8')
 
401
                encoded_filename = encoded_filename_temporary
 
402
 
 
403
                if p.openFromFile(encoded_filename): return wrap_music(p)
 
404
 
 
405
                del p
 
406
                raise IOError(popLastErrorMessage())
 
407
 
 
408
        @classmethod
 
409
        def from_memory(cls, bytes data):
 
410
                cdef sf.Music *p = new sf.Music()
 
411
 
 
412
                if p.openFromMemory(<char*>data, len(data)): return wrap_music(p)
 
413
 
 
414
                del p
 
415
                raise IOError(popLastErrorMessage())
 
416
 
 
417
        property duration:
 
418
                def __get__(self):
 
419
                        cdef sf.Time* p = new sf.Time()
 
420
                        p[0] = self.p_this.getDuration()
 
421
                        return wrap_time(p)
 
422
 
 
423
 
 
424
cdef Music wrap_music(sf.Music *p):
 
425
        cdef Music r = Music.__new__(Music)
 
426
        r.p_this = p
 
427
        r.p_soundstream = <sf.SoundStream*>p
 
428
        r.p_soundsource = <sf.SoundSource*>p
 
429
        return r
 
430
 
 
431
 
 
432
cdef class SoundRecorder:
 
433
        cdef sf.SoundRecorder *p_soundrecorder
 
434
 
 
435
        def __init__(self):
 
436
                if self.__class__ == SoundRecorder:
 
437
                        raise NotImplementedError("SoundRecorder is abstract")
 
438
 
 
439
                elif self.__class__ is not SoundBufferRecorder:
 
440
                        self.p_soundrecorder = <sf.SoundRecorder*>new DerivableSoundRecorder(<void*>self)
 
441
 
 
442
        def __dealloc__(self):
 
443
                if self.__class__ is SoundRecorder:
 
444
                        del self.p_soundrecorder
 
445
 
 
446
        def start(self, unsigned int sample_rate=44100):
 
447
                self.p_soundrecorder.start(sample_rate)
 
448
 
 
449
        def stop(self):
 
450
                with nogil: self.p_soundrecorder.stop()
 
451
 
 
452
        property sample_rate:
 
453
                def __get__(self):
 
454
                        return self.p_soundrecorder.getSampleRate()
 
455
 
 
456
        @classmethod
 
457
        def is_available(cls):
 
458
                return sf.soundrecorder.isAvailable()
 
459
 
 
460
        def on_start(self):
 
461
                return True
 
462
 
 
463
        def on_process_samples(self, chunk):
 
464
                return True
 
465
 
 
466
        def on_stop(self):
 
467
                pass
 
468
 
 
469
cdef class SoundBufferRecorder(SoundRecorder):
 
470
        cdef sf.SoundBufferRecorder *p_this
 
471
        cdef SoundBuffer                 m_buffer
 
472
 
 
473
        def __init__(self):
 
474
                self.p_this = new sf.SoundBufferRecorder()
 
475
                self.p_soundrecorder = <sf.SoundRecorder*>self.p_this
 
476
 
 
477
                self.m_buffer = wrap_soundbuffer(<sf.SoundBuffer*>&self.p_this.getBuffer(), False)
 
478
 
 
479
        def __dealloc__(self):
 
480
                del self.p_this
 
481
 
 
482
        property buffer:
 
483
                def __get__(self):
 
484
                        return self.m_buffer