forked from michaelboulton/python_mp3_decoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_decoder.py
More file actions
50 lines (33 loc) · 1.21 KB
/
test_decoder.py
File metadata and controls
50 lines (33 loc) · 1.21 KB
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
from pymp3decoder import Decoder
import contextlib
import os
import math
import pyaudio
CHUNK_SIZE = 4096
def take_chunk(content):
""" Split a buffer of data into chunks """
num_blocks = int(math.ceil(1.0*len(content)/CHUNK_SIZE))
for start in range(num_blocks):
yield content[CHUNK_SIZE*start:CHUNK_SIZE*(start+1)]
class TestPlayer:
@contextlib.contextmanager
def start(self):
try:
p = pyaudio.PyAudio()
self.decoder = Decoder(CHUNK_SIZE*20)
self.stream = p.open(format=p.get_format_from_width(2),
channels=2,
rate=44100,
output=True)
yield self.stream
finally:
self.stream.stop_stream()
self.stream.close()
p.terminate()
def test_file(self):
""" Open a file and decode it """
abs_location = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test.mp3")
with open(abs_location, "rb") as in_file, self.start():
content = in_file.read()
for chunk in self.decoder.decode_iter(take_chunk(content)):
self.stream.write(chunk)