-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmpg123.py
301 lines (241 loc) · 9.55 KB
/
mpg123.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import ctypes
from ctypes.util import find_library
import sys
VERBOSE = 0
OK = 0
NEED_MORE = -10
NEW_FORMAT = -11
DONE = -12
MONO = 1
STEREO = 2
ENC_8 = 0x00f
ENC_16 = 0x040
ENC_24 = 0x4000
ENC_32 = 0x100
ENC_SIGNED = 0x080
ENC_FLOAT = 0xe00
ENC_SIGNED_16 = (ENC_16 | ENC_SIGNED | 0x10)
ENC_UNSIGNED_16 = (ENC_16 | 0x20)
ENC_UNSIGNED_8 = 0x01
ENC_SIGNED_8 = (ENC_SIGNED | 0x02)
ENC_ULAW_8 = 0x04
ENC_ALAW_8 = 0x08
ENC_SIGNED_32 = (ENC_32 | ENC_SIGNED | 0x1000)
ENC_UNSIGNED_32 = (ENC_32 | 0x2000)
ENC_SIGNED_24 = (ENC_24 | ENC_SIGNED | 0x1000)
ENC_UNSIGNED_24 = (ENC_24 | 0x2000)
ENC_FLOAT_32 = 0x200
ENC_FLOAT_64 = 0x400
class ID3v1(ctypes.Structure):
_fields_ = [
('tag', ctypes.c_char * 3),
('title', ctypes.c_char * 30),
('artist', ctypes.c_char * 30),
('album', ctypes.c_char * 30),
('year', ctypes.c_char * 4),
('comment', ctypes.c_char * 30),
('genre', ctypes.c_ubyte),
]
class Mpg123:
_lib = None
class LibInitializationException(Exception):
pass
class OpenFeedException(Exception):
pass
class CloseException(Exception):
pass
class OpenFileException(Exception):
pass
class NotFeedException(Exception):
pass
class FeedingException(Exception):
pass
class FormatException(Exception):
pass
class DecodeException(Exception):
pass
class NeedMoreException(Exception):
pass
class DoneException(Exception):
pass
class LengthException(Exception):
pass
class ID3Exception(Exception):
pass
def plain_strerror(self, errcode):
self._lib.mpg123_plain_strerror.restype = ctypes.c_char_p
return self._lib.mpg123_plain_strerror(errcode).decode()
def init_library(self, library_path=None):
if not library_path:
library_path = find_library('mpg123')
if not library_path:
library_path = find_library('libmpg123-0')
if not library_path:
raise self.LibInitializationException('libmpg123 not found')
lib = ctypes.CDLL(library_path)
errcode = lib.mpg123_init()
if errcode != OK:
raise self.LibInitializationException(self.plain_strerror(errcode))
return lib
def __init__(self, filename=None, library_path=None):
self.handle = None
if not self._lib:
self._lib = self.init_library(library_path)
self._lib.mpg123_new.restype = ctypes.c_void_p
self.c_handle = self._lib.mpg123_new(ctypes.c_char_p(None), None)
self.handle = ctypes.c_void_p(self.c_handle)
self.is_feed = filename is None
self.offset = ctypes.c_size_t(0)
if self.is_feed:
errcode = self._lib.mpg123_open_feed(self.handle)
if errcode != OK:
raise self.OpenFeedException(self.plain_strerror(errcode))
else:
errcode = self._lib.mpg123_open(self.handle, filename.encode())
if errcode != OK:
raise self.OpenFileException(self.plain_strerror(errcode))
def feed(self, data):
if not self.is_feed:
raise self.NotFeedException('instance is not in feed mode')
# encode string to bytes in modern python
if sys.version_info[0] >= 3 and isinstance(data, str):
data = data.encode()
data = memoryview(data)
errcode = self._lib.mpg123_feed(self.handle,
ctypes.c_char_p(data.tobytes()),
len(data))
if errcode != OK:
raise self.FeedingException(self.plain_strerror(errcode))
def get_id3(self):
v1 = ctypes.c_void_p()
v2 = ctypes.c_void_p()
errcode = self._lib.mpg123_id3(self.handle,
ctypes.pointer(v1),
ctypes.pointer(v2))
if errcode != OK:
raise self.ID3Exception(self.plain_strerror(errcode))
if v1.value is None:
raise self.ID3Exception(self.plain_strerror(errcode))
return ctypes.cast(v1, ctypes.POINTER(ID3v1)).contents
def get_format(self):
rate = ctypes.c_int(0)
channels = ctypes.c_int(0)
encoding = ctypes.c_int(0)
errcode = self._lib.mpg123_getformat(self.handle,
ctypes.pointer(rate),
ctypes.pointer(channels),
ctypes.pointer(encoding))
if errcode != OK:
if errcode == NEED_MORE:
raise self.NeedMoreException(self.plain_strerror(errcode))
raise self.FormatException(self.plain_strerror(errcode))
return (rate.value, channels.value, encoding.value)
def get_width_by_encoding(self, encoding):
return self._lib.mpg123_encsize(encoding)
def length(self):
errcode = self._lib.mpg123_length(self.handle)
if errcode <= 0:
if errcode == NEED_MORE:
raise self.NeedMoreException(self.plain_strerror(errcode))
raise self.LengthException(self.plain_strerror(errcode))
return errcode
def frame_length(self):
errcode = self._lib.mpg123_framelength(self.handle)
if errcode <= 0:
if errcode == NEED_MORE:
raise self.NeedMoreException(self.plain_strerror(errcode))
raise self.LengthException(self.plain_strerror(errcode))
return errcode
def decode_frame(self):
audio = ctypes.c_char_p()
done = ctypes.c_size_t(0)
errcode = self._lib.mpg123_decode_frame(self.handle,
ctypes.pointer(self.offset),
ctypes.pointer(audio),
ctypes.pointer(done))
if errcode == OK:
return ctypes.string_at(audio, done.value)
if errcode == NEED_MORE:
raise self.NeedMoreException(self.plain_strerror(errcode))
if errcode == NEW_FORMAT:
return self.decode_frame()
if errcode == DONE:
raise self.DoneException(self.plain_strerror(errcode))
raise self.DecodeException(self.plain_strerror(errcode))
def iter_frames(self, new_format_callback=None):
self.offset = ctypes.c_size_t(0)
audio = ctypes.c_char_p()
done = ctypes.c_size_t(0)
while True:
errcode = self._lib.mpg123_decode_frame(
self.handle,
ctypes.pointer(self.offset),
ctypes.pointer(audio),
ctypes.pointer(done))
if errcode == OK:
yield ctypes.string_at(audio, done.value)
else:
if errcode in (NEED_MORE, DONE):
break
if errcode == NEW_FORMAT:
if new_format_callback:
new_format_callback(*self.get_format())
continue
raise self.DecodeException(self.plain_strerror(errcode))
def __del__(self):
if not self.handle:
return
errcode = self._lib.mpg123_close(self.handle)
if errcode != OK:
raise self.CloseException(self.plain_strerror(errcode))
class Out123:
_lib = None
class LibInitializationException(Exception):
pass
class OpenException(Exception):
pass
class CloseException(Exception):
pass
class StartException(Exception):
pass
class PlayingException(Exception):
pass
def init_library(self, library_path=None):
if not library_path:
library_path = find_library('out123')
if not library_path:
library_path = find_library('libout123-0')
if not library_path:
raise self.LibInitializationException('libout123 not found')
return ctypes.CDLL(library_path)
def plain_strerror(self, errcode):
self._lib.out123_plain_strerror.restype = ctypes.c_char_p
return self._lib.out123_plain_strerror(errcode).decode()
def __init__(self, library_path=None):
self.handle = None
if not self._lib:
self._lib = self.init_library(library_path)
self._lib.out123_new.restype = ctypes.c_void_p
self.c_handle = self._lib.out123_new()
self.handle = ctypes.c_void_p(self.c_handle)
errcode = self._lib.out123_open(self.handle,
ctypes.c_char_p(None),
ctypes.c_char_p(None))
if errcode != OK:
raise self.OpenException(self.plain_strerror(errcode))
def start(self, rate, channels, encoding):
errcode = self._lib.out123_start(self.handle, rate, channels, encoding)
if errcode != OK:
raise self.StartException(self.plain_strerror(errcode))
def play(self, data):
# encode string to bytes in modern python
if sys.version_info[0] >= 3 and isinstance(data, str):
data = data.encode()
data = memoryview(data)
return self._lib.out123_play(self.handle,
ctypes.c_char_p(data.tobytes()),
len(data))
def __del__(self):
if not self.handle:
return
self._lib.out123_close(self.handle)