Skip to content

Commit bac5312

Browse files
committed
Bring test files back briefly
This reverts commit f5f75f7.
1 parent 90ae365 commit bac5312

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

examples/play_example.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from pymp3decoder import Decoder
2+
import contextlib
3+
import io
4+
import math
5+
6+
import pyaudio
7+
8+
CHUNK_SIZE = 4096
9+
10+
11+
def read_chunk(file_buffer):
12+
""" Read chunks from a file """
13+
14+
while 1:
15+
content = file_buffer.read(CHUNK_SIZE)
16+
17+
if content:
18+
yield content
19+
else:
20+
return
21+
22+
23+
def take_chunk(content):
24+
""" Split a buffer of data into chunks """
25+
26+
num_blocks = int(math.ceil(1.0*len(content)/CHUNK_SIZE))
27+
28+
for start in xrange(num_blocks):
29+
yield content[CHUNK_SIZE*start:CHUNK_SIZE*(start+1)]
30+
31+
32+
class ExamplePlayer:
33+
@contextlib.contextmanager
34+
def start(self):
35+
try:
36+
p = pyaudio.PyAudio()
37+
38+
self.decoder = Decoder(CHUNK_SIZE*20)
39+
40+
self.stream = p.open(format=p.get_format_from_width(2),
41+
channels=2,
42+
rate=44100,
43+
output=True)
44+
45+
yield self.stream
46+
finally:
47+
self.stream.stop_stream()
48+
self.stream.close()
49+
50+
p.terminate()
51+
52+
53+
def example_file(self):
54+
""" Open a file and decode it """
55+
56+
with open("example.mp3", "rb") as in_file, self.start():
57+
content = in_file.read()
58+
59+
for chunk in self.decoder.decode_iter(take_chunk(content)):
60+
self.stream.write(chunk)
61+
62+
63+
def example_file_stream(self):
64+
""" Open a file in buffered mode and read/decode a chunk at a time """
65+
66+
with io.open("example.mp3", "rb", buffering=CHUNK_SIZE) as in_file, self.start():
67+
for chunk in self.decoder.decode_iter(read_chunk(in_file)):
68+
self.stream.write(chunk)
69+
70+
71+
def example_tcp_stream(self):
72+
""" Stream an mp3 from a url and decode it """
73+
74+
import requests
75+
remote_mp3 = requests.get("http://localhost:8000/example.mp3", stream=True)
76+
77+
for chunk in self.decoder.decode_iter(take_chunk(remote_mp3)), self.start():
78+
self.stream.write(chunk)

tests/test.mp3

524 KB
Binary file not shown.

tests/test_decoder.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from pymp3decoder import Decoder
2+
import contextlib
3+
import os
4+
import math
5+
6+
import pyaudio
7+
8+
CHUNK_SIZE = 4096
9+
10+
11+
def take_chunk(content):
12+
""" Split a buffer of data into chunks """
13+
14+
num_blocks = int(math.ceil(1.0*len(content)/CHUNK_SIZE))
15+
16+
for start in range(num_blocks):
17+
yield content[CHUNK_SIZE*start:CHUNK_SIZE*(start+1)]
18+
19+
20+
class TestPlayer:
21+
@contextlib.contextmanager
22+
def start(self):
23+
try:
24+
p = pyaudio.PyAudio()
25+
26+
self.decoder = Decoder(CHUNK_SIZE*20)
27+
28+
self.stream = p.open(format=p.get_format_from_width(2),
29+
channels=2,
30+
rate=44100,
31+
output=True)
32+
33+
yield self.stream
34+
finally:
35+
self.stream.stop_stream()
36+
self.stream.close()
37+
38+
p.terminate()
39+
40+
41+
def test_file(self):
42+
""" Open a file and decode it """
43+
44+
abs_location = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test.mp3")
45+
46+
with open(abs_location, "rb") as in_file, self.start():
47+
content = in_file.read()
48+
49+
for chunk in self.decoder.decode_iter(take_chunk(content)):
50+
self.stream.write(chunk)

0 commit comments

Comments
 (0)