|
3 | 3 | # GitHub: https://github.com/Baekalfen/PyBoy
|
4 | 4 | #
|
5 | 5 |
|
6 |
| -import io |
7 | 6 | import os
|
8 | 7 | from pathlib import Path
|
9 | 8 |
|
|
16 | 15 | from pyboy import PyBoy
|
17 | 16 | from pyboy.utils import PyBoyFeatureDisabledError
|
18 | 17 |
|
| 18 | +from PIL import Image, ImageDraw |
| 19 | + |
19 | 20 | OVERWRITE_PNGS = False
|
20 | 21 | odds = None
|
21 | 22 |
|
@@ -43,43 +44,64 @@ def test_swoosh(default_rom, sampling):
|
43 | 44 |
|
44 | 45 | left_channel = buffers[:, 0]
|
45 | 46 | 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 |
65 | 68 |
|
66 | 69 | name = "sampling" if sampling else ("oddsampling" if sampling is odds else "nosampling")
|
67 | 70 | png_path = Path(f"tests/test_results/sound_swoosh_{name}.png")
|
| 71 | + |
68 | 72 | if OVERWRITE_PNGS:
|
69 | 73 | png_path.parents[0].mkdir(parents=True, exist_ok=True)
|
70 |
| - plt.savefig(png_path) |
| 74 | + image.save(png_path) |
71 | 75 | else:
|
72 | 76 | # 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") |
77 | 78 | old_image = PIL.Image.open(png_path).convert("RGB")
|
78 | 79 | diff = PIL.ImageChops.difference(image, old_image)
|
79 | 80 | if diff.getbbox() and os.environ.get("TEST_VERBOSE_IMAGES"):
|
80 | 81 | image.show()
|
81 | 82 | old_image.show()
|
82 | 83 | 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() |
83 | 105 | plt.show()
|
84 | 106 | assert not diff.getbbox(), "Images are different!"
|
85 | 107 |
|
|
0 commit comments