-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound_synthizer.py
44 lines (33 loc) · 1.21 KB
/
sound_synthizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import synthizer
buffer_cache={}
class sound_synthizer:
def __init__(self, filename, context):
self.filename = filename
self.context = context
self.context.default_panner_strategy.value = synthizer.PannerStrategy.HRTF
if filename in buffer_cache:
self.buffer=buffer_cache[filename]
else:
self.buffer = synthizer.Buffer.from_file(filename)
buffer_cache[filename]=self.buffer
self.generator = synthizer.BufferGenerator(context)
self.generator.buffer.value = self.buffer
self.source = synthizer.Source3D(context)
self.source.add_generator(self.generator)
self.source.pause()
self.generator.playback_position.value = 0
def play(self):
self.source.play()
def pause(self):
self.source.pause()
def position(self, x, y=0, z=0):
self.source.position.value = (x, y, z)
def seek(self, position):
self.generator.playback_position.value = position
def gain(self, volume):
gain = 10**(volume/20)
self.source.gain.value = gain
def __del__(self):
self.source.dec_ref()
self.generator.dec_ref()
self.buffer.dec_ref()