forked from Fornoth/spotify-connect-web
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalsa_sink.py
193 lines (149 loc) · 5.44 KB
/
alsa_sink.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import Queue
import re
from threading import Thread, Event
import alsaaudio as alsa
from spotifyconnect import Sink
RATE = 44100
CHANNELS = 2
PERIODSIZE = 44100 / 40 # 0.025s
SAMPLESIZE = 2 # 16 bit integer
MAXPERIODS = int(0.5 * RATE / PERIODSIZE) # 0.5s Buffer
pending_data = str()
class AlsaSink(Sink):
def __init__(self, device, rate=RATE, channels=CHANNELS, periodsize=PERIODSIZE, buffer_length=MAXPERIODS):
self.device = None
self.device_name = device
self.rate = rate
self.channels = channels
self.periodsize = periodsize
self.mixer = None
self.queue = Queue.Queue(maxsize=buffer_length)
self.t = Thread()
self.t.name = "AlsaSinkLoop"
self.on()
def _on_music_delivery(self, audio_format, samples, num_samples, pending, session):
global pending_data
buf = pending_data + samples
try:
total = 0
while len(buf) >= PERIODSIZE * CHANNELS * SAMPLESIZE:
self.write(buf[:PERIODSIZE * CHANNELS * SAMPLESIZE])
buf = buf[PERIODSIZE * CHANNELS * SAMPLESIZE:]
total += PERIODSIZE * CHANNELS
pending_data = buf
return num_samples
except BufferFull:
return total
finally:
pending[0] = self.buffer_length() * PERIODSIZE * CHANNELS
def mixer_load(self, mixer="", volmin=0, volmax=100):
# List cardindex for all devices
card_info = {}
for device_number, card_name in enumerate(alsa.cards()):
card_info[card_name] = device_number
# get Card Index for the device
pattern = r'(\w+)+?:?(?:card=(\w+))?,?(?:dev=(\w+))?'
result = re.match(pattern, self.device_name, re.IGNORECASE)
cardname = result.group(2)
device = result.group(3)
if cardname is None:
cardindex = -1
else:
cardindex = card_info[cardname]
if device is None:
device = 'default'
if not mixer:
try:
device_mixers = alsa.mixers(device=device, cardindex=cardindex)
except alsa.ALSAAudioError as error:
raise PlayerError("PlayerError: {}".format(error))
if len(device_mixers) > 0:
mixer = device_mixers[0]
else:
raise PlayerError("PlayerError: Device has no mixers")
try:
self.mixer = alsa.Mixer(mixer, device=device, cardindex=cardindex)
except alsa.ALSAAudioError as error:
raise PlayerError("PlayerError: {}".format(error))
self.volmin = volmin
self.volmax = volmax
def mixer_unload(self):
self.mixer.close()
self.mixer = None
def mixer_loaded(self):
if self.mixer is not None:
return True
else:
return False
def acquire(self):
try:
self.device = alsa.PCM(alsa.PCM_PLAYBACK, device=self.device_name)
self.device.setchannels(self.channels)
self.device.setrate(self.rate)
self.device.setperiodsize(self.periodsize)
self.device.setformat(alsa.PCM_FORMAT_S16_LE)
except alsa.ALSAAudioError as error:
raise PlayerError("PlayerError: {}".format(error))
def release(self):
self.device.close()
self.device = None
def acquired(self):
if self.device is not None:
return True
else:
return False
def playback_thread(self, q, e):
while not e.is_set():
data = q.get()
if data:
self.device.write(data)
q.task_done()
def play(self):
self.t_stop = Event()
self.t = Thread(args=(self.queue, self.t_stop), target=self.playback_thread)
self.t.daemon = True
self.t.start()
def pause(self):
self.t_stop.set()
if self.queue.empty():
self.queue.put(str())
self.t.join()
def playing(self):
if self.t.isAlive():
return True
else:
return False
def write(self, data):
try:
self.queue.put(data, block=False)
except Queue.Full:
raise BufferFull()
def buffer_flush(self):
while not self.queue.empty():
self.queue.get()
self.queue.task_done()
def buffer_length(self):
return self.queue.qsize()
def volrange_set(self, volmin, volmax):
self.volmin = volmin
self.volmax = volmax
def volume_get(self):
mixer_volume = self.mixer.getvolume()[0]
if mixer_volume > self.volmax:
mixer_volume = self.volmax
elif mixer_volume < self.volmin:
mixer_volume = self.volmin
volume = int(round((mixer_volume - self.volmin) / float(self.volmax - self.volmin) * 100))
return volume
def volume_set(self, volume):
if volume == 0:
self.mixer.setmute(1)
else:
if self.mixer.getmute()[0] == 1:
self.mixer.setmute(0)
mixer_volume = int(round((self.volmax - self.volmin) * volume / 100.0 + self.volmin))
self.mixer.setvolume(mixer_volume)
class PlayerError(Exception):
pass
class BufferFull(Exception):
pass