-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsound.py
124 lines (105 loc) · 3.17 KB
/
sound.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# -*- coding: utf-8 -*-
#BGT-ish Sound_lib wrapper
#Original author: Carter Temm
#Edited by Yukio Nozawa <[email protected]>
#Modified by me, [email protected]
import math, ctypes
import sound_lib
from sound_lib import output
from sound_lib import stream
o=output.Output()
class sound():
def __init__(self, filename="", vol=0, playing=False, looping=False):
self.handle=None
self.freq=44100
if len(filename)>0:
self.load(filename)
self.handle.looping=looping
self.volume=vol
self.looping=looping
if playing==True:
self.play(ignore_loop=False)
def load(self,filename=""):
if self.handle:
self.close()
#end close previous
self.handle =stream.FileStream(file=filename)
self.freq=self.handle.get_frequency()
def __del__(self):
self.stop()
self.close()
def play(self, ignore_loop=True):
if ignore_loop==True:
self.handle.looping=False
return self.handle.play()
def play_wait(self):
self.handle.looping=False
return self.handle.play_blocking()
def play_looped(self):
self.handle.looping=True
return self.handle.play()
def stop(self):
if self.handle and self.handle.is_playing:
self.handle.stop()
self.handle.set_position(0)
def fadeout(self, fadetime, amount=0.05):
"""The faded sound might be kept playing internally. Make sure that you call stop() before fading in or playing again. Fading will be performed by BASS's internal thread, so playing this instance after calling fadeout() may sound strangely."""
if self.handle and self.handle.is_playing:
self.handle.slide_attribute("volume",amount,fadetime)
@property
def volume(self):
if not self.handle:
return False
return round(math.log10(self.handle.volume)*20)
@volume.setter
def volume(self,value):
if not self.handle:
return False
self.handle.set_volume(10**(float(value)/20))
@property
def pitch(self):
if not self.handle:
return False
return (self.handle.get_frequency()/self.freq)*100
@pitch.setter
def pitch(self, value):
if not self.handle:
return False
self.handle.set_frequency((float(value)/100)*self.freq)
@property
def pan(self):
if not self.handle:
return False
return self.handle.get_pan()*100
@pan.setter
def pan(self, value):
if not self.handle:
return False
if value<-100 or value>100:
raise ValueError("The pan value is not between -100 and 100")
self.handle.set_pan(float(value)/100)
@property
def playing(self):
if not self.handle:
return False
return self.handle.is_playing
def close(self):
if self.handle:
self.handle.free()
self.handle=None
def seek(self, position):
return self.handle.set_position(position)
@property
def position(self):
return self.handle.get_position()/1000000
@position.setter
def position(self, val):
self.handle.set_position(val)/1000000
def sound_from_bytes(bytes):
"""Takes some bytes and returns a sound() object that you can play, pan, ETC"""
snd=sound()
snd.__buffer=ctypes.create_string_buffer(bytes)
addr=ctypes.addressof(snd.__buffer)
h=stream.FileStream(mem=True, file=addr, length=len(snd.__buffer))
snd.handle=h
return snd