Skip to content

Commit 08b4943

Browse files
committed
Stablize test_swoosh by drawing lines in Pillow instead of matplotlib
1 parent 0e93b95 commit 08b4943

File tree

4 files changed

+47
-25
lines changed

4 files changed

+47
-25
lines changed
-21.6 KB
Loading
Loading
-27.9 KB
Loading

tests/test_sound.py

+47-25
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# GitHub: https://github.com/Baekalfen/PyBoy
44
#
55

6-
import io
76
import os
87
from pathlib import Path
98

@@ -16,6 +15,8 @@
1615
from pyboy import PyBoy
1716
from pyboy.utils import PyBoyFeatureDisabledError
1817

18+
from PIL import Image, ImageDraw
19+
1920
OVERWRITE_PNGS = False
2021
odds = None
2122

@@ -43,43 +44,64 @@ def test_swoosh(default_rom, sampling):
4344

4445
left_channel = buffers[:, 0]
4546
right_channel = buffers[:, 1]
46-
time = np.linspace(0, len(left_channel) / sample_rate, num=len(left_channel))
47-
48-
# Plot the channels
49-
plt.figure(figsize=(12, 6))
50-
plt.subplot(2, 1, 1)
51-
plt.plot(time, left_channel, label="Left Channel", color="blue")
52-
plt.title("Left Channel")
53-
plt.xlabel("Time (s)")
54-
plt.ylabel("Amplitude")
55-
plt.ylim(-0.4, 15.4)
56-
57-
plt.subplot(2, 1, 2)
58-
plt.plot(time, right_channel, label="Right Channel", color="red")
59-
plt.title("Right Channel")
60-
plt.xlabel("Time (s)")
61-
plt.ylabel("Amplitude")
62-
plt.ylim(-0.4, 15.4)
63-
64-
plt.tight_layout()
47+
48+
height = 16
49+
50+
# Create a new image with white background
51+
image = Image.new("RGB", (buffers.shape[0], height * 2), "white")
52+
draw = ImageDraw.Draw(image)
53+
54+
# Plot the samples for each channel
55+
prev_x = 0
56+
prev_y1 = height - left_channel[0] - 1
57+
prev_y2 = height - right_channel[0] - 1 + height
58+
for i in range(buffers.shape[0]):
59+
y1 = height - left_channel[i] - 1
60+
y2 = height - right_channel[i] - 1 + height
61+
62+
draw.line([(prev_x, prev_y1), (i, y1)], fill="blue")
63+
draw.line([(prev_x, prev_y2), (i, y2)], fill="red")
64+
65+
prev_x = i
66+
prev_y1 = y1
67+
prev_y2 = y2
6568

6669
name = "sampling" if sampling else ("oddsampling" if sampling is odds else "nosampling")
6770
png_path = Path(f"tests/test_results/sound_swoosh_{name}.png")
71+
6872
if OVERWRITE_PNGS:
6973
png_path.parents[0].mkdir(parents=True, exist_ok=True)
70-
plt.savefig(png_path)
74+
image.save(png_path)
7175
else:
7276
# Converting to RGB as ImageChops.difference cannot handle Alpha: https://github.com/python-pillow/Pillow/issues/4849
73-
plt_data = io.BytesIO()
74-
plt.savefig(plt_data, format="png")
75-
plt_data.seek(0)
76-
image = PIL.Image.open(plt_data).convert("RGB")
77+
image = image.convert("RGB")
7778
old_image = PIL.Image.open(png_path).convert("RGB")
7879
diff = PIL.ImageChops.difference(image, old_image)
7980
if diff.getbbox() and os.environ.get("TEST_VERBOSE_IMAGES"):
8081
image.show()
8182
old_image.show()
8283
diff.show()
84+
85+
if diff.getbbox():
86+
time = np.linspace(0, len(left_channel) / sample_rate, num=len(left_channel))
87+
88+
# Plot the channels
89+
plt.figure(figsize=(12, 6))
90+
plt.subplot(2, 1, 1)
91+
plt.plot(time, left_channel, label="Left Channel", color="blue")
92+
plt.title("Left Channel")
93+
plt.xlabel("Time (s)")
94+
plt.ylabel("Amplitude")
95+
plt.ylim(-0.4, 15.4)
96+
97+
plt.subplot(2, 1, 2)
98+
plt.plot(time, right_channel, label="Right Channel", color="red")
99+
plt.title("Right Channel")
100+
plt.xlabel("Time (s)")
101+
plt.ylabel("Amplitude")
102+
plt.ylim(-0.4, 15.4)
103+
104+
plt.tight_layout()
83105
plt.show()
84106
assert not diff.getbbox(), "Images are different!"
85107

0 commit comments

Comments
 (0)