Repository navigation
fix(vromtool): keep ADPCM-A samples within 1 MiB pages #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
52e14de
vromtool: store samples shared by several songs once
dogruis 9792744
fix(vromtool): keep ADPCM-A samples within 1 MiB pages
imshah-id 4270b34
refactor(vromtool): name ADPCM-A page placement helper
imshah-id 71fa3d7
Merge sample sharing prerequisite and clarify page allocation
imshah-id File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| """Regression tests for ADPCM-A page placement.""" | ||
|
|
||
| import io | ||
| from pathlib import Path | ||
| import sys | ||
| import tempfile | ||
| import unittest | ||
|
|
||
| sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tools")) | ||
| import vromtool | ||
|
|
||
| PAGE = 1024 * 1024 | ||
|
|
||
|
|
||
| def sample(kind, name, size, value=0x55): | ||
| result = kind(name) | ||
| result.data = bytes([value]) * size | ||
| return result | ||
|
|
||
|
|
||
| class AdpcmAPagePlacementTests(unittest.TestCase): | ||
| def test_adpcm_a_crossing_page_moves_to_next_page(self): | ||
| samples = [sample(vromtool.adpcm_a, "first", 0xfe300), | ||
| sample(vromtool.adpcm_a, "earthquake", 0x2300), | ||
| sample(vromtool.adpcm_a, "duplicate", 0x2300), | ||
| sample(vromtool.adpcm_a, "next", 0x100, 0xaa)] | ||
| vromtool.allocate_samples(samples, 2 * PAGE, "sampleX.rom") | ||
| self.assertEqual([s.start for s in samples], [0, PAGE, PAGE, PAGE + 0x2300]) | ||
| output = io.StringIO() | ||
| vromtool.generate_asm_defines(samples, output) | ||
| self.assertIn("[100000..1022ff] ADPCM-A", output.getvalue()) | ||
|
|
||
| def test_exact_page_end_does_not_add_padding(self): | ||
| samples = [sample(vromtool.adpcm_a, "first", PAGE - 0x100), | ||
| sample(vromtool.adpcm_a, "end", 0x100), | ||
| sample(vromtool.adpcm_a, "next", 0x100, 0xaa)] | ||
| vromtool.allocate_samples(samples, 2 * PAGE, "sampleX.rom") | ||
| self.assertEqual([s.start for s in samples], [0, PAGE - 0x100, PAGE]) | ||
|
|
||
| def test_alignment_at_rom_end_allocates_next_rom(self): | ||
| samples = [sample(vromtool.adpcm_a, "first", PAGE), | ||
| sample(vromtool.adpcm_a, "second", PAGE - 0x100), | ||
| sample(vromtool.adpcm_a, "next", 0x200)] | ||
| vromtool.allocate_samples(samples, 2 * PAGE, "sampleX.rom") | ||
| self.assertEqual(samples[2].out, "sample2.rom") | ||
| self.assertEqual(samples[2].vrom_start, 0) | ||
| self.assertEqual(samples[2].start, 2 * PAGE) | ||
|
|
||
| def test_adpcm_a_larger_than_page_is_rejected(self): | ||
| with self.assertRaises(SystemExit): | ||
| vromtool.allocate_samples([sample(vromtool.adpcm_a, "large", PAGE + 0x100)], | ||
| 2 * PAGE, "sampleX.rom") | ||
|
|
||
| def test_sample_larger_than_rom_is_rejected(self): | ||
| with self.assertRaises(SystemExit): | ||
| vromtool.allocate_samples([sample(vromtool.adpcm_a, "large", PAGE)], | ||
| PAGE // 2, "sampleX.rom") | ||
|
|
||
| def test_generated_rom_contains_padding_and_samples_at_reported_offsets(self): | ||
| samples = [sample(vromtool.adpcm_a, "first", PAGE - 0x100), | ||
| sample(vromtool.adpcm_a, "second", 0x200, 0xaa)] | ||
| with tempfile.TemporaryDirectory() as directory: | ||
| pattern = str(Path(directory) / "sampleX.rom") | ||
| vromtool.allocate_samples(samples, 2 * PAGE, pattern) | ||
| vromtool.generate_vroms(samples, 2 * PAGE, pattern, 1) | ||
| data = Path(samples[0].out).read_bytes() | ||
| self.assertEqual(len(data), 2 * PAGE) | ||
| self.assertEqual(data[:PAGE - 0x100], samples[0].data) | ||
| self.assertEqual(data[PAGE - 0x100:PAGE], bytes(0x100)) | ||
| self.assertEqual(data[PAGE:PAGE + 0x200], samples[1].data) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,6 +85,35 @@ def validate(s): | |
| val["uri"].startswith("data:;base64,") | ||
|
|
||
|
|
||
| def align_adpcm_a_sample(s, start): | ||
| if isinstance(s, adpcm_a): | ||
| if len(s.data) > 0x100000: | ||
| error("ADPCM-A sample '%s' is larger than one 1 MiB page" % s.name) | ||
| if s.data and start // 0x100000 != (start + len(s.data) - 1) // 0x100000: | ||
| return start + 0x100000 - start % 0x100000 | ||
| return start | ||
|
|
||
|
|
||
| def record_sample(sample, recorded_samples): | ||
| data_key = (type(sample), bytes(sample.data)) | ||
| recorded_sample = recorded_samples.get(data_key) | ||
| if recorded_sample is None: | ||
| recorded_samples[data_key] = sample | ||
| return True | ||
| sample.out = recorded_sample.out | ||
| sample.vrom_start = recorded_sample.vrom_start | ||
| sample.start = recorded_sample.start | ||
| sample.length = recorded_sample.length | ||
| sample.start_lsb = recorded_sample.start_lsb | ||
| sample.start_msb = recorded_sample.start_msb | ||
| sample.stop_lsb = recorded_sample.stop_lsb | ||
| sample.stop_msb = recorded_sample.stop_msb | ||
| dbg(" [%06x..%06x] %s (same data as %s)" % | ||
| (sample.start, sample.start+sample.length, | ||
| sample.name, recorded_sample.name)) | ||
| return False | ||
|
|
||
|
|
||
| # Basic sample packing: allocate ADPCM samples in sequence | ||
| # until a ROM is full. Assume ADPCM A and B share the same ROM | ||
| def allocate_samples(smp, vrom_size, out_vrom_pattern): | ||
|
|
@@ -93,15 +122,22 @@ def allocate_samples(smp, vrom_size, out_vrom_pattern): | |
| absolute_pos = 0 | ||
| vrom = 0 | ||
| f = None | ||
| recorded_samples = {} | ||
| for s in smp: | ||
| if vrom_pos+len(s.data) > vrom_size: | ||
| if not record_sample(s, recorded_samples): | ||
| continue | ||
| if len(s.data) > vrom_size: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of leaving all that logic in the for loop you could put this in a method and name it accordingly. |
||
| error("Sample '%s' is larger than one VROM" % s.name) | ||
| vrom_pos = align_adpcm_a_sample(s, absolute_pos + vrom_pos) - absolute_pos | ||
| while vrom_pos + len(s.data) > vrom_size: | ||
| if f: | ||
| f.close() | ||
| vrom_pos = 0 | ||
| absolute_pos = vrom * vrom_size | ||
| vrom += 1 | ||
| out = out_vrom_pattern.replace("X", str(vrom)) | ||
| dbg(" New VROM '%s'" % out) | ||
| vrom_pos = align_adpcm_a_sample(s, absolute_pos) - absolute_pos | ||
| s.out = out | ||
| s.vrom_start = vrom_pos | ||
| s.start = absolute_pos + vrom_pos | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That should be the only test in this test class if the owner of the library wants it. The rest is testing random vromtool features and out of scope for this ticket. They should land in a separate ticket.