diff --git a/docs/specs/packer/7. Audio Bank Packing Specification.md b/docs/specs/packer/7. Audio Bank Packing Specification.md new file mode 100644 index 00000000..35ea0fe4 --- /dev/null +++ b/docs/specs/packer/7. Audio Bank Packing Specification.md @@ -0,0 +1,61 @@ +# Audio bank packing + +Domain: packer. Function: normative. + +The existing packer emits SoundBank (`sound_bank`, `SOUND/bank_v1`) and +MusicBank (`music_bank`, `MUSIC/bank_v1`) assets. Runtime authority is +`runtime/docs/specs/runtime/05a-audio-bank-format.md` in the enclosing workspace. +There are 16 maximum resident slots for each audio type. Outer codec is NONE; +sample codecs are PCM16_LE or IMA_ADPCM_WAV4, independently selected. + +An audio asset contains one selected JSON composition manifest. SoundBank +manifests list samples; MusicBank manifests additionally declare song timing, +instrument zones and events. WAV source paths are relative to the asset root; +reject absolute/path-escaping and missing inputs. Inputs may be mono or stereo; +preserve channel count. Import WAV PCM16 little endian and normalize permitted +production inputs explicitly; runtime receives bank bytes, never a WAV container. + +The manifest declares samples in stable index order, each with file, codec, +optional loop_start/loop_end in decoded frames, and playback defaults. MusicBank +adds ppq=960, tempo_us_per_quarter, duration_ticks, optional loop_start_tick, +track_count, instruments and events. Instrument/event field names match the +runtime table, except ADSR authoring uses attack_ms, decay_ms and release_ms +(converted to unsigned microseconds with nearest rounding); absent defaults are +deterministic. WAV PCM8/16/24/32 and IEEE float32 are converted to PCM16, with +finite-value validation and saturation; no runtime WAV decoder is needed. Events retain authored order +within equal ticks. The bank is complete; no reference resolves to another bank. + +Packing constructs SBNK/MBNK version 1 tables and payloads exactly as specified +by runtime, with checked lengths, reserved zero bytes and deterministic output. +IMA encoding uses complete blocks, a first-sample/index header per channel, +low-nibble-first codes and four-byte stereo channel groups. The default block +size is 256 bytes per channel. Report encoded payload and canonical decoded size +separately. Validate ranges, references, zones, loops, timing/density and codec +metadata before publishing the artifact. An invalid asset produces a diagnostic, +not a partial bank or a silently substituted waveform. + +Sound/MusicBank packing is implemented in the existing API/v1 modules and +workspace artifact builder. It does not add an audio editor, tracker, preview, +compiler feature or new UI/import integration. Existing glyph/palette/scene +formats remain outside this change. Conformance fixtures must be consumed by the +Rust decoder, including PCM-only, IMA-only, mixed, stereo, malformed and complete +music banks. Encoder choices are explicit and source WAVs are not runtime assets. + +The packing snapshot includes the selected JSON and every referenced WAV, with +individual fingerprints. A changed WAV invalidates its source fingerprint even +when the JSON is unchanged. Packing reads these frozen bytes, validates the bank +and emits the same deterministic payload. Runtime metadata contains version=1; +tooling metadata reports pcm_bytes, cartridge_bytes, decoded_size and ratio. + +Example asset.json selection: + +```json +{"schema_version":1,"asset_uuid":"stage-01","name":"stage_01", + "type":"music_bank","output":{"format":"MUSIC/bank_v1","codec":"NONE"}, + "artifacts":[{"file":"song.json","index":0}],"preload":{"enabled":false}} +``` + +A complete composition example with PCM/IMA mono/stereo WAV sources and exact +binary fixtures lives in the existing packer v1 test resources under +fixtures/audio. The workspace packer emits the regular assets.pa envelope with +SOUNDS or MUSIC entries; preload slots are counted independently by bank type. diff --git a/prometeu-packer/prometeu-packer-api/src/main/java/p/packer/messages/assets/AssetFamilyCatalog.java b/prometeu-packer/prometeu-packer-api/src/main/java/p/packer/messages/assets/AssetFamilyCatalog.java index 3271ce0a..03ceccf1 100644 --- a/prometeu-packer/prometeu-packer-api/src/main/java/p/packer/messages/assets/AssetFamilyCatalog.java +++ b/prometeu-packer/prometeu-packer-api/src/main/java/p/packer/messages/assets/AssetFamilyCatalog.java @@ -6,6 +6,7 @@ public enum AssetFamilyCatalog { GLYPH_BANK("glyph_bank"), SCENE_BANK("scene_bank"), SOUND_BANK("sound_bank"), + MUSIC_BANK("music_bank"), UNKNOWN("unknown"); private final String manifestType; diff --git a/prometeu-packer/prometeu-packer-api/src/main/java/p/packer/messages/assets/OutputFormatCatalog.java b/prometeu-packer/prometeu-packer-api/src/main/java/p/packer/messages/assets/OutputFormatCatalog.java index 4ed5264c..9bbf9e54 100644 --- a/prometeu-packer/prometeu-packer-api/src/main/java/p/packer/messages/assets/OutputFormatCatalog.java +++ b/prometeu-packer/prometeu-packer-api/src/main/java/p/packer/messages/assets/OutputFormatCatalog.java @@ -6,7 +6,8 @@ import java.util.Locale; public enum OutputFormatCatalog { GLYPH_INDEXED_V1(AssetFamilyCatalog.GLYPH_BANK, "GLYPH/indexed_v1", "GLYPH/indexed_v1"), SCENE_TILED_V1(AssetFamilyCatalog.SCENE_BANK, "SCENE/tilemap_v1", "SCENE/tilemap_v1"), - SOUND_V1(AssetFamilyCatalog.SOUND_BANK, "SOUND/v1", "SOUND/v1"), + SOUND_V1(AssetFamilyCatalog.SOUND_BANK, "SOUND/bank_v1", "SOUND/bank_v1"), + MUSIC_V1(AssetFamilyCatalog.MUSIC_BANK, "MUSIC/bank_v1", "MUSIC/bank_v1"), UNKNOWN(AssetFamilyCatalog.UNKNOWN, "unknown", "Unknown"); private final AssetFamilyCatalog assetFamily; @@ -57,9 +58,6 @@ public enum OutputFormatCatalog { if ("scene/tiled_v1".equals(normalized)) { return SCENE_TILED_V1; } - if ("sound/bank_v1".equals(normalized)) { - return SOUND_V1; - } for (OutputFormatCatalog candidate : values()) { if (candidate == UNKNOWN) { continue; diff --git a/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/models/PackerSoundBankRequirements.java b/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/models/PackerSoundBankRequirements.java deleted file mode 100644 index 7030bf05..00000000 --- a/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/models/PackerSoundBankRequirements.java +++ /dev/null @@ -1,4 +0,0 @@ -package p.packer.models; - -public record PackerSoundBankRequirements(int sampleRate, int channels) { -} \ No newline at end of file diff --git a/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/repositories/PackerAssetWalker.java b/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/repositories/PackerAssetWalker.java index 5e9d8518..12c24ffb 100644 --- a/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/repositories/PackerAssetWalker.java +++ b/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/repositories/PackerAssetWalker.java @@ -30,19 +30,14 @@ public class PackerAssetWalker { OutputFormatCatalog.GLYPH_INDEXED_V1 ); - private static final Set SOUND_BANK_SUPPORTED_FORMATS = Set.of( - OutputFormatCatalog.SOUND_V1 - ); private static final String SCENE_PACK_REQUEST_FILE = "scene-bank.pack-request.json"; private final ObjectMapper mapper; private final PackerGlyphBankWalker glyphBankWalker; - private final PackerSoundBankWalker soundBankWalker; public PackerAssetWalker(final ObjectMapper mapper) { this.mapper = mapper; this.glyphBankWalker = new PackerGlyphBankWalker(mapper); - this.soundBankWalker = new PackerSoundBankWalker(mapper); } public PackerWalkResult walk( @@ -85,31 +80,7 @@ public class PackerAssetWalker { diagnostics.addAll(glyphBankFragileIndexDiagnostics(declaration, walkResult)); return new PackerWalkResult(walkResult.probeResults(), diagnostics); } - case SOUND_BANK -> { - var metadata = declaration.outputMetadata(); - if (MapUtils.isEmpty(metadata)) { - diagnostics.add(new PackerDiagnostic( - PackerDiagnosticSeverity.WARNING, - PackerDiagnosticCategory.HYGIENE, - "Output metadata for sound bank cannot be empty, using default values", - assetRoot, - false)); - metadata = Map.of("sample_rate", "44100", "channels", "1"); - } - final var result = buildSoundBankRequirements(declaration, metadata); - if (result.hasError()) { - diagnostics.add(new PackerDiagnostic( - PackerDiagnosticSeverity.ERROR, - PackerDiagnosticCategory.STRUCTURAL, - result.errorMessage(), - assetRoot, - true)); - return new PackerWalkResult(List.of(), diagnostics); - } - final var walkResult = soundBankWalker.walk(assetRoot, result.requirements, priorAssetCache); - diagnostics.addAll(walkResult.diagnostics()); - return new PackerWalkResult(walkResult.probeResults(), diagnostics); - } + case SOUND_BANK, MUSIC_BANK -> { return walkAudioBank(assetRoot, declaration, diagnostics); } case SCENE_BANK -> { return walkSceneBank(assetRoot, declaration, diagnostics); } @@ -126,6 +97,28 @@ public class PackerAssetWalker { return PackerWalkResult.EMPTY; } + private PackerWalkResult walkAudioBank(Path root, PackerAssetDeclaration declaration, List diagnostics) { + try { + boolean music = declaration.assetFamily() == p.packer.messages.assets.AssetFamilyCatalog.MUSIC_BANK; + if(declaration.outputFormat() != (music ? OutputFormatCatalog.MUSIC_V1 : OutputFormatCatalog.SOUND_V1)) throw new IllegalArgumentException("Unsupported audio output format"); + if(declaration.artifacts().size()!=1)throw new IllegalArgumentException("Audio bank requires exactly one selected JSON composition manifest"); + Path relative = Path.of(declaration.artifacts().getFirst().file()); + Path normalized = root.toAbsolutePath().normalize(); Path manifest = normalized.resolve(relative).normalize(); + if(relative.isAbsolute() || !manifest.startsWith(normalized) || !manifest.toRealPath().startsWith(normalized.toRealPath()) || !manifest.toString().endsWith(".json"))throw new IllegalArgumentException("Invalid audio composition manifest path"); + var packed = p.packer.services.AudioBankPacker.pack(normalized, manifest, music, mapper); + List probes = new ArrayList<>(); + for(Path source : new java.util.LinkedHashSet<>(packed.sources())) { + byte[] bytes = Files.readAllBytes(source); + Map metadata = source.equals(manifest) ? Map.of("audio_pcm_bytes",packed.pcmBytes(),"audio_cartridge_bytes",packed.payload().length,"audio_decoded_size",packed.decodedSize()) : Map.of(); + probes.add(new PackerProbeResult(new PackerFileProbe(source,source.equals(manifest)?"application/json":"audio/wav",bytes.length,Math.max(0L,Files.getLastModifiedTime(source).toMillis()),bytes),metadata,List.of())); + } + return new PackerWalkResult(probes,diagnostics); + } catch(IOException | IllegalArgumentException | ArithmeticException error) { + diagnostics.add(new PackerDiagnostic(PackerDiagnosticSeverity.ERROR,PackerDiagnosticCategory.STRUCTURAL,"Unable to pack audio bank: "+error.getMessage(),root,true)); + return new PackerWalkResult(List.of(),diagnostics); + } + } + private PackerWalkResult walkSceneBank( Path assetRoot, PackerAssetDeclaration declaration, @@ -379,25 +372,6 @@ public class PackerAssetWalker { return List.copyOf(palettes); } - private RequirementBuildResult buildSoundBankRequirements( - final PackerAssetDeclaration declaration, - final Map metadata) { - if (!SOUND_BANK_SUPPORTED_FORMATS.contains(declaration.outputFormat())) { - return RequirementBuildResult.fail("Unsupported output format for sound bank: " + declaration.outputFormat()); - } - final var sampleRateStr = metadata.get("sample_rate"); - if (StringUtils.isBlank(sampleRateStr)) { - return RequirementBuildResult.fail("Missing sample rate for sound bank"); - } - final var sampleRate = Integer.parseInt(sampleRateStr); - final var channelsStr = metadata.get("channels"); - if (StringUtils.isBlank(channelsStr)) { - return RequirementBuildResult.fail("Missing channels for sound bank"); - } - final var channels = Integer.parseInt(channelsStr); - return RequirementBuildResult.success(new PackerSoundBankRequirements(sampleRate, channels)); - } - private record RequirementBuildResult( T requirements, String errorMessage) { diff --git a/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/repositories/PackerRuntimeAssetMaterializer.java b/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/repositories/PackerRuntimeAssetMaterializer.java index 2cc023f7..bae3056e 100644 --- a/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/repositories/PackerRuntimeAssetMaterializer.java +++ b/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/repositories/PackerRuntimeAssetMaterializer.java @@ -161,7 +161,9 @@ public final class PackerRuntimeAssetMaterializer { if (materializationConfig.mode() != PackerWalkMode.PACKING) { return true; } - if (declaration.assetFamily() == p.packer.messages.assets.AssetFamilyCatalog.SCENE_BANK) { + if (declaration.assetFamily() == p.packer.messages.assets.AssetFamilyCatalog.SCENE_BANK + || declaration.assetFamily() == p.packer.messages.assets.AssetFamilyCatalog.SOUND_BANK + || declaration.assetFamily() == p.packer.messages.assets.AssetFamilyCatalog.MUSIC_BANK) { return true; } if (declaration.artifacts().isEmpty()) { diff --git a/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/repositories/PackerSoundBankWalker.java b/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/repositories/PackerSoundBankWalker.java deleted file mode 100644 index cbacbbb3..00000000 --- a/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/repositories/PackerSoundBankWalker.java +++ /dev/null @@ -1,34 +0,0 @@ -package p.packer.repositories; - -import com.fasterxml.jackson.databind.ObjectMapper; -import p.packer.models.PackerDiagnostic; -import p.packer.models.PackerFileProbe; -import p.packer.models.PackerProbeResult; -import p.packer.models.PackerSoundBankRequirements; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Set; - -public class PackerSoundBankWalker extends PackerAbstractBankWalker { - private static final Set SUPPORTED_MIME_TYPES = Set.of("audio/wav"); - - public PackerSoundBankWalker(ObjectMapper mapper) { - super(mapper); - } - - @Override - protected Set getSupportedMimeTypes() { - return SUPPORTED_MIME_TYPES; - } - - @Override - protected PackerProbeResult processFileProbe( - final PackerFileProbe fileProbe, - final java.nio.file.Path assetRoot, - final PackerSoundBankRequirements requirements) { - final List diagnostics = new ArrayList<>(); - return new PackerProbeResult(fileProbe, Map.of(), diagnostics); - } -} diff --git a/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/services/AudioBankPacker.java b/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/services/AudioBankPacker.java new file mode 100644 index 00000000..709f38d5 --- /dev/null +++ b/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/services/AudioBankPacker.java @@ -0,0 +1,376 @@ +package p.packer.services; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.IOException; +import java.math.BigInteger; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +/** + * SBNK/MBNK v1 producer in the existing packer. No host/runtime WAV dependency. + */ +public final class AudioBankPacker { + private AudioBankPacker() { + } + + public record Result(byte[] payload, int decodedSize, long pcmBytes, List sources) { + } + + private record Wave(int rate, int channels, short[] pcm) { + } + + private record Encoded(JsonNode node, Wave wave, int codec, byte[] data, int offset) { + } + + private static final int[] STEP = {7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767}; + private static final int[] INDEX = {-1, -1, -1, -1, 2, 4, 6, 8}; + + static Path input(Path root, String file) throws IOException { + Path base = root.toAbsolutePath().normalize(), realBase = root.toRealPath(), relative = Path.of(file); + if (relative.isAbsolute()) throw bad("source must be relative"); + Path path = base.resolve(relative).normalize(); + if (!path.startsWith(base) || !path.toRealPath().startsWith(realBase) || !Files.isRegularFile(path)) + throw bad("source escapes asset root or is missing: " + file); + return path; + } + + static IllegalArgumentException bad(String message) { + return new IllegalArgumentException("Audio bank: " + message); + } + + static long n(JsonNode node, String field, long fallback, long min, long max) { + JsonNode value = node.get(field); + if (value == null) return fallback; + if (!value.isIntegralNumber() || !value.canConvertToLong() || value.longValue() < min || value.longValue() > max) + throw bad("invalid " + field); + return value.longValue(); + } + + static int i(JsonNode node, String field, int fallback, int min, int max) { + return (int) n(node, field, fallback, min, max); + } + + static boolean flag(JsonNode node, String field, boolean fallback) { + JsonNode v = node.get(field); + if (v == null) return fallback; + if (!v.isBoolean()) throw bad("invalid " + field); + return v.booleanValue(); + } + + static String text(JsonNode node, String field, String fallback) { + JsonNode v = node.get(field); + if (v == null) return fallback; + if (!v.isTextual()) throw bad("invalid " + field); + return v.textValue(); + } + + static List array(JsonNode node, String field) { + JsonNode v = node.get(field); + if (v == null) return List.of(); + if (!v.isArray()) throw bad("invalid " + field); + List out = new ArrayList<>(); + v.forEach(x -> { + if (!x.isObject()) throw bad(field + " must contain objects"); + out.add(x); + }); + return out; + } + + static long micros(JsonNode node, String field) { + JsonNode value = node.get(field); + if (value == null) return 0; + if (!value.isNumber() || !Double.isFinite(value.doubleValue()) || value.doubleValue() < 0 || value.doubleValue() * 1000.0 > 0xffff_ffffL) + throw bad("invalid " + field); + return Math.round(value.doubleValue() * 1000.0); + } + + static long u(JsonNode node, String field, long fallback) { + return n(node, field, fallback, 0, 0xffff_ffffL); + } + + public static Result pack(Path root, Path manifest, boolean music, ObjectMapper mapper) throws IOException { + return packSnapshot(root, manifest, music, mapper, null); + } + + public static Result packSnapshot(Path root, Path manifest, boolean music, ObjectMapper mapper, Map snapshot) throws IOException { + JsonNode doc = mapper.readTree(read(manifest, snapshot)); + if (doc == null || !doc.isObject()) throw bad("manifest must be an object"); + String name = text(doc, "name", ""); + byte[] nameBytes = name.getBytes(java.nio.charset.StandardCharsets.UTF_8); + List samples = array(doc, "samples"), instruments = array(doc, "instruments"), events = array(doc, "events"); + if (samples.size() > 65536 || (!music && (samples.isEmpty() || !instruments.isEmpty() || !events.isEmpty()))) + throw bad("invalid sample/instrument/event counts"); + long tempo = n(doc, "tempo_us_per_quarter", 500000, 1, 0xffff_ffffL), duration = u(doc, "duration_ticks", 0), loop = n(doc, "loop_start_tick", 0xffff_ffffL, 0, 0xffff_ffffL); + int tracks = i(doc, "track_count", music ? 1 : 0, 0, 256); + if (i(doc, "ppq", 960, 960, 960) != 960) throw bad("ppq"); + if (music && (duration == 0 || tracks == 0 || (loop != 0xffff_ffffL && loop >= duration))) + throw bad("invalid music duration/tracks/loop"); + if (!music && (duration != 0 || tracks != 0 || loop != 0xffff_ffffL)) + throw bad("SoundBank cannot contain a song"); + int metadata = Math.addExact(48 + nameBytes.length, Math.addExact(Math.multiplyExact(samples.size(), 40), Math.addExact(Math.multiplyExact(instruments.size(), 64), Math.multiplyExact(events.size(), 32)))); + int offset = metadata; + long pcmBytes = 0; + List encoded = new ArrayList<>(); + List sources = new ArrayList<>(); + sources.add(manifest); + for (JsonNode s : samples) { + Path source = input(root, text(s, "file", "")); + sources.add(source); + Wave w = wave(read(source, snapshot)); + String codec = text(s, "codec", "PCM16_LE"); + int id = switch (codec) { + case "PCM16_LE" -> 0; + case "IMA_ADPCM_WAV4" -> 1; + default -> throw bad("unsupported sample codec " + codec); + }; + int frames = w.pcm.length / w.channels; + long start = u(s, "loop_start", 0), end = u(s, "loop_end", 0); + if (s.has("loop_start") != s.has("loop_end") || (s.has("loop_start") && (start >= end || end > frames))) + throw bad("invalid decoded loop"); + ByteBuffer raw = ByteBuffer.allocate(w.pcm.length * 2).order(ByteOrder.LITTLE_ENDIAN); + for (short v : w.pcm) raw.putShort(v); + byte[] payload = id == 0 ? raw.array() : ima(w.pcm, w.channels); + encoded.add(new Encoded(s, w, id, payload, offset)); + offset = Math.addExact(offset, payload.length); + pcmBytes = Math.addExact(pcmBytes, w.pcm.length * 2L); + } + validateMusic(instruments, events, encoded, tempo, duration, loop, tracks, music); + ByteBuffer out = ByteBuffer.allocate(offset).order(ByteOrder.LITTLE_ENDIAN); + out.put((music ? "MBNK" : "SBNK").getBytes(java.nio.charset.StandardCharsets.US_ASCII)).putShort((short) 1).putShort((short) 0); + out.putInt(samples.size()).putInt(instruments.size()).putInt(events.size()).putInt(960).putInt((int) tempo).putInt((int) duration).putInt((int) loop).putInt(tracks).putInt(nameBytes.length).putInt(0).put(nameBytes); + for (Encoded s : encoded) { + JsonNode x = s.node; + int c = s.wave.channels; + out.put((byte) s.codec).put((byte) c).putShort((short) (x.has("loop_start") ? 1 : 0)).putInt(s.wave.rate).putInt(s.wave.pcm.length / c).putInt((int) u(x, "loop_start", 0)).putInt((int) u(x, "loop_end", 0)).putInt(s.offset).putInt(s.data.length).putShort((short) (s.codec == 1 ? 256 * c : 0)).putShort((short) (s.codec == 1 ? 505 : 0)); + out.put((byte) i(x, "default_volume", 255, 0, 255)).put((byte) i(x, "default_pan", 128, 0, 255)).putShort((short) 0); + JsonNode pitchNode=x.get("default_pitch"); + double pitch = pitchNode==null?1.0:pitchNode.isNumber()?pitchNode.doubleValue():Double.NaN; + if (!Double.isFinite(pitch) || pitch < 1.0 / 65536.0 || pitch > 4294967295.0 / 65536.0) + throw bad("invalid default_pitch"); + out.putInt((int) Math.round(pitch * 65536.0)); + } + for (JsonNode x : instruments) { + int at = out.position(); + out.putShort((short) i(x, "id", 0, 0, 65535)).putShort((short) i(x, "sample", 0, 0, 65535)).put((byte) i(x, "low", 0, 0, 127)).put((byte) i(x, "high", 127, 0, 127)).put((byte) i(x, "root", 60, 0, 127)).put((byte) (flag(x, "looping", false) ? 1 : 0)).putShort((short) i(x, "tuning_cents", 0, -32768, 32767)).put((byte) i(x, "pan", 128, 0, 255)).put((byte) i(x, "gain", 255, 0, 255)); + out.putInt((int) micros(x, "attack_ms")).putInt((int) micros(x, "decay_ms")).putShort((short) i(x, "sustain", 65535, 0, 65535)).putShort((short) 0).putInt((int) micros(x, "release_ms")).putInt(i(x, "vibrato_millihz", 0, 0, 100000)).putShort((short) i(x, "vibrato_cents", 0, 0, 2400)).putShort((short) i(x, "tremolo_depth", 0, 0, 65535)).putInt(i(x, "tremolo_millihz", 0, 0, 100000)).putShort((short) i(x, "send", 0, 0, 65535)).put((byte) i(x, "curve", 1, 0, 1)); + out.position(at + 64); + } + for (JsonNode e : events) + out.putInt((int) u(e, "tick", 0)).put((byte) i(e, "kind", 0, 1, 10)).put((byte) i(e, "track", 0, 0, tracks - 1)).putShort((short) i(e, "flags", 0, 0, 1)).putInt((int) u(e, "a", 0)).putInt((int) u(e, "b", 0)).putInt(i(e, "c", 0, Integer.MIN_VALUE, Integer.MAX_VALUE)).putInt(i(e, "d", 0, Integer.MIN_VALUE, Integer.MAX_VALUE)).putInt((int) u(e, "duration_frames", 0)).putInt(i(e, "curve", 0, 0, 1)); + for (Encoded s : encoded) out.put(s.data); + return new Result(out.array(), Math.toIntExact(Math.addExact(metadata, pcmBytes)), pcmBytes, List.copyOf(sources)); + } + + private static byte[] read(Path file, Map snapshot) throws IOException { + if (snapshot == null) return Files.readAllBytes(file); + byte[] bytes = snapshot.get(file.toAbsolutePath().normalize()); + if (bytes == null) throw bad("source absent from packing snapshot: " + file); + return bytes; + } + + private static void validateMusic(List instruments, List events, List samples, long tempo, long duration, long loop, int tracks, boolean music) { + if (!music) return; + for (int j = 0; j < instruments.size(); j++) { + JsonNode x = instruments.get(j); + int id = i(x, "id", 0, 0, 65535), low = i(x, "low", 0, 0, 127), high = i(x, "high", 127, 0, 127), sample = i(x, "sample", 0, 0, 65535); + i(x, "root", 60, 0, 127); + if (low > high || sample >= samples.size() || (flag(x, "looping", false) && !samples.get(sample).node.has("loop_start"))) + throw bad("invalid instrument sample/zone/loop"); + for (int k = 0; k < j; k++) { + JsonNode p = instruments.get(k); + if (i(p, "id", 0, 0, 65535) == id && i(p, "low", 0, 0, 127) <= high && low <= i(p, "high", 127, 0, 127)) + throw bad("overlapping instrument zones"); + } + } + long tick = 0, currentTempo = tempo; + BigInteger time = BigInteger.ZERO, loopTime = null, den = BigInteger.valueOf(960_000_000L); + List times = new ArrayList<>(); + long[] selected = new long[tracks]; + Arrays.fill(selected, 0xffff_ffffL); + for (JsonNode e : events) { + long next = u(e, "tick", 0); + int kind = i(e, "kind", 0, 1, 10), track = i(e, "track", 0, 0, tracks - 1); + if (next < tick || next > duration) throw bad("event ordering/range"); + if (loop != 0xffff_ffffL && loop >= tick && loop <= next && loopTime == null) + loopTime = time.add(delta(loop - tick, currentTempo)); + time = time.add(delta(next - tick, currentTempo)); + tick = next; + times.add(time); + long a = u(e, "a", 0), b = u(e, "b", 0); + int c = i(e, "c", 0, Integer.MIN_VALUE, Integer.MAX_VALUE); + i(e, "flags", 0, 0, 1); + i(e, "curve", 0, 0, 1); + switch (kind) { + case 1 -> { + i(e, "c", 0, 0, 127); + i(e, "d", 0, 0, 255); + long id = b == 0xffff_ffffL ? selected[track] : b; + boolean found = false; + for (JsonNode x : instruments) + if (i(x, "id", 0, 0, 65535) == id && i(x, "low", 0, 0, 127) <= c && c <= i(x, "high", 127, 0, 127)) + found = true; + if (!found) throw bad("note has no local instrument zone"); + } + case 3 -> { + if (a == 0) throw bad("zero tempo"); + currentTempo = a; + } + case 4, 9 -> i(e, "c", 0, 0, 65535); + case 5 -> i(e, "c", 0, 0, 255); + case 6 -> i(e, "c", 0, -9600, 9600); + case 7, 8 -> { + if (a > 100000 || c < 0 || c > (kind == 7 ? 2400 : 65535)) throw bad("invalid modulation"); + } + case 10 -> { + boolean found = false; + for (JsonNode x : instruments) if (i(x, "id", 0, 0, 65535) == a) found = true; + if (!found) throw bad("instrument selection"); + selected[track] = a; + } + default -> { + } + } + } + if (loop != 0xffff_ffffL && loop >= tick && loopTime == null) + loopTime = time.add(delta(loop - tick, currentTempo)); + BigInteger end = time.add(delta(duration - tick, currentTempo)); + if (end.compareTo(den) < 0 || (loopTime != null && end.subtract(loopTime).compareTo(den) < 0)) + throw bad("song/loop shorter than output frame"); + density(times, den); + BigInteger begin = loopTime == null ? BigInteger.ZERO : loopTime, length = end.subtract(begin); + List circular = new ArrayList<>(); + for (BigInteger t : times) if (t.compareTo(begin) >= 0) circular.add(t); + List seam = new ArrayList<>(circular.subList(Math.max(0, circular.size() - 64), circular.size())); + for (BigInteger t : circular.subList(0, Math.min(65, circular.size()))) seam.add(t.add(length)); + density(seam, den); + } + + private static BigInteger delta(long ticks, long tempo) { + return BigInteger.valueOf(ticks).multiply(BigInteger.valueOf(tempo)).multiply(BigInteger.valueOf(48000)); + } + + private static void density(List times, BigInteger den) { + for (int j = 64; j < times.size(); j++) + if (times.get(j).subtract(times.get(j - 64)).compareTo(den) < 0) + throw bad("more than 64 events per output frame"); + } + + private static Wave wave(byte[] bytes) { + if (bytes.length < 12) throw bad("truncated WAV"); + ByteBuffer b = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); + if (b.getInt(0) != 0x46464952 || b.getInt(8) != 0x45564157 || Integer.toUnsignedLong(b.getInt(4)) + 8 != bytes.length) + throw bad("invalid RIFF/WAVE size"); + int format = 0, channels = 0, rate = 0, bits = 0, align = 0; + byte[] data = null; + for (int p = 12; p < bytes.length; ) { + if (bytes.length - p < 8) throw bad("WAV chunk header"); + long size = Integer.toUnsignedLong(b.getInt(p + 4)); + if (size > bytes.length - p - 8) throw bad("WAV chunk size"); + int n = (int) size; + if (b.getInt(p) == 0x20746d66) { + if (n < 16 || format != 0) throw bad("WAV fmt"); + format = Short.toUnsignedInt(b.getShort(p + 8)); + channels = Short.toUnsignedInt(b.getShort(p + 10)); + rate = b.getInt(p + 12); + align = Short.toUnsignedInt(b.getShort(p + 20)); + bits = Short.toUnsignedInt(b.getShort(p + 22)); + } + if (b.getInt(p) == 0x61746164) { + if (data != null) throw bad("multiple WAV data chunks"); + data = Arrays.copyOfRange(bytes, p + 8, p + 8 + n); + } + p = Math.addExact(p + 8, n + (n & 1)); + if (p > bytes.length) throw bad("WAV missing chunk padding"); + } + if (channels < 1 || channels > 2 || rate <= 0 || data == null || !(format == 1 && (bits == 8 || bits == 16 || bits == 24 || bits == 32) || format == 3 && bits == 32) || align != channels * (bits / 8) || data.length % align != 0) + throw bad("unsupported WAV layout (PCM8/16/24/32 or float32 mono/stereo)"); + short[] pcm = new short[data.length / (bits / 8)]; + ByteBuffer d = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN); + for (int j = 0; j < pcm.length; j++) { + int v; + if (format == 3) { + float f = d.getFloat(); + if (!Float.isFinite(f)) throw bad("nonfinite WAV float"); + v = Math.round(Math.max(-1f, Math.min(1f, f)) * 32768f); + } else v = switch (bits) { + case 8 -> (Byte.toUnsignedInt(d.get()) - 128) << 8; + case 16 -> d.getShort(); + case 24 -> { + int x = Byte.toUnsignedInt(d.get()) | Byte.toUnsignedInt(d.get()) << 8 | d.get() << 16; + yield x >> 8; + } + case 32 -> d.getInt() >> 16; + default -> throw bad("WAV bits"); + }; + pcm[j] = (short) Math.max(-32768, Math.min(32767, v)); + } + return new Wave(rate, channels, pcm); + } + + private static int decode(int[] state, int code) { + int step = STEP[state[1]], difference = step >> 3; + if ((code & 1) != 0) difference += step >> 2; + if ((code & 2) != 0) difference += step >> 1; + if ((code & 4) != 0) difference += step; + state[0] = Math.max(-32768, Math.min(32767, state[0] + ((code & 8) != 0 ? -difference : difference))); + state[1] = Math.max(0, Math.min(88, state[1] + INDEX[code & 7])); + return state[0]; + } + + private static int code(int sample, int[] state) { + int difference = sample - state[0], value = difference < 0 ? 8 : 0; + difference = Math.abs(difference); + int step = STEP[state[1]]; + for (int bit = 4; bit > 0; bit >>= 1) { + if (difference >= step) { + value |= bit; + difference -= step; + } + step >>= 1; + } + decode(state, value); + return value; + } + + static byte[] ima(short[] pcm, int channels) { + int frames = pcm.length / channels, blocks = (frames + 504) / 505, align = 256 * channels; + byte[] out = new byte[blocks * align]; + ByteBuffer b = ByteBuffer.wrap(out).order(ByteOrder.LITTLE_ENDIAN); + for (int block = 0; block < blocks; block++) { + int start = block * 505, count = Math.min(505, frames - start); + int[][] states = new int[channels][2]; + int[][] codes = new int[channels][504]; + for (int c = 0; c < channels; c++) { + int[] state = states[c]; + state[0] = pcm[start * channels + c]; + long total = 0; + int ns = Math.min(count - 1, 32); + for (int j = 0; j < ns; j++) + total += Math.abs((int) pcm[(start + j + 1) * channels + c] - pcm[(start + j) * channels + c]); + long mean = ns == 0 ? 0 : total / ns; + while (state[1] < 88 && STEP[state[1]] < mean) state[1]++; + b.putShort((short) state[0]).put((byte) state[1]).put((byte) 0); + for (int j = 0; j < 504; j++) { + if (j + 1 < count) codes[c][j] = code(pcm[(start + j + 1) * channels + c], state); + else { + codes[c][j] = 0; + decode(state, 0); + } + } + } + for (int group = 0; group < 63; group++) + for (int c = 0; c < channels; c++) + for (int j = 0; j < 4; j++) + b.put((byte) (codes[c][group * 8 + j * 2] | codes[c][group * 8 + j * 2 + 1] << 4)); + } + return out; + } +} diff --git a/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/services/FileSystemPackerWorkspaceService.java b/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/services/FileSystemPackerWorkspaceService.java index f83dd6d8..dc22d7e3 100644 --- a/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/services/FileSystemPackerWorkspaceService.java +++ b/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/services/FileSystemPackerWorkspaceService.java @@ -103,7 +103,7 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe var dependencies=new java.util.TreeSet(); for(var file:asset.walkProjection().buildCandidateFiles())if(file.metadata().get("scenePackRequest") instanceof ScenePackRequest scene) scene.layers().stream().filter(ScenePackLayerRequest::active).map(ScenePackLayerRequest::glyphAssetId).forEach(dependencies::add); - var entry=asset.registryEntry();String type=switch(declaration.assetFamily()){case GLYPH_BANK->"GLYPH";case SCENE_BANK->"SCENE";case SOUND_BANK->"SOUNDS";default->"UNKNOWN";}; + var entry=asset.registryEntry();String type=switch(declaration.assetFamily()){case GLYPH_BANK->"GLYPH";case SCENE_BANK->"SCENE";case SOUND_BANK->"SOUNDS";case MUSIC_BANK->"MUSIC";default->"UNKNOWN";}; String reference=entry.map(e->AssetReference.forAssetId(e.assetId()).value()).orElseGet(()->AssetReference.forRelativeAssetRoot(PackerWorkspacePaths.relativeAssetRoot(project,asset.assetRoot())).value()); banks.add(new p.packer.dtos.PackerPackPlanDTO.Bank(entry.map(PackerRegistryEntry::assetId).orElse(null),reference,asset.assetRoot(),type,entry.map(PackerRegistryEntry::includedInBuild).orElse(false),isEffectivelyIncludedInPackSet(asset),declaration.artifacts().size(),declaration.outputMetadata(),List.copyOf(dependencies))); if(isEffectivelyIncludedInPackSet(asset))diagnostics.addAll(toPackValidationAssetDTO(asset).diagnostics()); @@ -1007,7 +1007,9 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe return List.of(); } if (declaration.assetFamily() == AssetFamilyCatalog.GLYPH_BANK - || declaration.assetFamily() == AssetFamilyCatalog.SCENE_BANK) { + || declaration.assetFamily() == AssetFamilyCatalog.SCENE_BANK + || declaration.assetFamily() == AssetFamilyCatalog.SOUND_BANK + || declaration.assetFamily() == AssetFamilyCatalog.MUSIC_BANK) { return List.of(); } return List.of(new PackerDiagnostic( @@ -1184,6 +1186,7 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe return switch (declaration.assetFamily()) { case GLYPH_BANK -> packGlyphBank(runtimeAsset, declaration); case SCENE_BANK -> packSceneBank(runtimeAsset, declaration); + case SOUND_BANK, MUSIC_BANK -> packAudioBank(runtimeAsset, declaration); default -> throw new IllegalStateException("Unsupported pack output family for current implementation."); }; } @@ -1202,6 +1205,20 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe return !declaration.artifacts().isEmpty(); } + private PackedAsset packAudioBank(PackerRuntimeAsset asset, PackerAssetDeclaration declaration) { + try { + boolean music = declaration.assetFamily() == AssetFamilyCatalog.MUSIC_BANK; + Map snapshot = new LinkedHashMap<>(); + for(var file:asset.walkProjection().buildCandidateFiles())snapshot.put(asset.assetRoot().resolve(file.relativePath()).toAbsolutePath().normalize(),file.contentBytes().orElseThrow(()->new IllegalStateException("Audio source missing from packing snapshot"))); + Path manifest = asset.assetRoot().resolve(declaration.artifacts().getFirst().file()).toAbsolutePath().normalize(); + var packed = AudioBankPacker.packSnapshot(asset.assetRoot(),manifest,music,mapper,snapshot); + var registry = asset.registryEntry().orElseThrow(); + Map metadata = Map.of("version",1); + Map tooling = Map.of("version",1,"pcm_bytes",packed.pcmBytes(),"cartridge_bytes",packed.payload().length,"decoded_size",packed.decodedSize(),"compression_ratio",packed.pcmBytes()==0?1.0:(double)packed.payload().length/packed.pcmBytes()); + return new PackedAsset(registry.assetId(),declaration.name(),music?"MUSIC":"SOUNDS","NONE",packed.payload(),packed.decodedSize(),metadata,tooling,declaration.preloadEnabled()); + } catch(IOException error) { throw new IllegalStateException("Unable to pack audio bank",error); } + } + private PackedAsset packGlyphBank(PackerRuntimeAsset runtimeAsset, PackerAssetDeclaration declaration) { final int tileSize = parseTileSize(declaration.outputMetadata().get("tile_size")); final int width = 256; diff --git a/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/services/PackerAssetDeclarationParser.java b/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/services/PackerAssetDeclarationParser.java index 346a8458..81575260 100644 --- a/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/services/PackerAssetDeclarationParser.java +++ b/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/services/PackerAssetDeclarationParser.java @@ -121,7 +121,7 @@ public final class PackerAssetDeclarationParser { diagnostics.add(new PackerDiagnostic( PackerDiagnosticSeverity.ERROR, PackerDiagnosticCategory.STRUCTURAL, - "Field 'type' must be one of: glyph_bank, scene_bank, sound_bank.", + "Field 'type' must be one of: glyph_bank, scene_bank, sound_bank, music_bank.", manifestPath, true)); return null; diff --git a/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/services/PackerOutputContractCatalog.java b/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/services/PackerOutputContractCatalog.java index ba50d476..00e7b9f9 100644 --- a/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/services/PackerOutputContractCatalog.java +++ b/prometeu-packer/prometeu-packer-v1/src/main/java/p/packer/services/PackerOutputContractCatalog.java @@ -61,21 +61,7 @@ final class PackerOutputContractCatalog { "16x16", true, List.of("8x8", "16x16", "32x32"))); - case SOUND_V1 -> List.of( - new PackerCodecConfigurationField( - "sample_rate", - "Sample Rate", - PackerCodecConfigurationFieldType.ENUM, - "44100", - true, - List.of("11025", "22050", "44100", "48000")), - new PackerCodecConfigurationField( - "channels", - "Channels", - PackerCodecConfigurationFieldType.ENUM, - "1", - true, - List.of("1", "2"))); + case SOUND_V1, MUSIC_V1 -> List.of(); default -> List.of(); }; } diff --git a/prometeu-packer/prometeu-packer-v1/src/test/java/p/packer/services/AudioBankPackerTest.java b/prometeu-packer/prometeu-packer-v1/src/test/java/p/packer/services/AudioBankPackerTest.java new file mode 100644 index 00000000..0a336e42 --- /dev/null +++ b/prometeu-packer/prometeu-packer-v1/src/test/java/p/packer/services/AudioBankPackerTest.java @@ -0,0 +1,69 @@ +package p.packer.services; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +import static org.junit.jupiter.api.Assertions.*; + +class AudioBankPackerTest { + @TempDir + Path temporary; + private final ObjectMapper mapper = new ObjectMapper(); + + private Path fixtures() throws Exception { + return Path.of(Objects.requireNonNull(getClass().getResource("/fixtures/audio/pcm-only.json")).toURI()).getParent(); + } + + @Test + void matchesIndependentBinaryFixturesForBothBankTypesAndMixedCodecs() throws Exception { + Path root = fixtures(); + for (String name : List.of("pcm-only", "ima-only", "mixed-sound", "mixed-music")) { + boolean music = name.equals("mixed-music"); + Path manifest = root.resolve(name + ".json"); + var result = AudioBankPacker.pack(root, manifest, music, mapper); + assertArrayEquals(Files.readAllBytes(root.resolve(name + (music ? ".mbnk" : ".sbnk"))), result.payload(), name); + assertTrue(result.decodedSize() > result.pcmBytes()); + assertEquals(name.startsWith("mixed") ? 6000 : 2000, result.pcmBytes()); + Map snapshot = new HashMap<>(); + for (Path path : result.sources()) + snapshot.put(path.toAbsolutePath().normalize(), Files.readAllBytes(path)); + assertArrayEquals(result.payload(), AudioBankPacker.packSnapshot(root, manifest, music, mapper, snapshot).payload()); + } + } + + @Test + void rejectsMissingSamplesExternalDependenciesAndMalformedLoops() throws Exception { + Path root = fixtures(); + String original = Files.readString(root.resolve("mixed-music.json")); + for (String json : List.of(original.replace("\"loop_end\": 1000", "\"loop_end\": 1001"), original.replace("\"sample\": 1", "\"sample\": 2"), original.replace("mono.wav", "../mono.wav"), original.replace("mono.wav", "absent.wav"), original.replace("PCM16_LE", "ADPCM"), original.replace("\"ppq\": 960", "\"ppq\": 24"))) { + Path file = temporary.resolve("invalid.json"); + Files.writeString(file, json); + assertThrows(Exception.class, () -> AudioBankPacker.pack(root, file, true, mapper)); + } + } + + @Test + void rejectsNonNumericPlaybackDefaults() throws Exception { + Path root=fixtures();Path manifest=temporary.resolve("bad-pitch.json"); + Files.writeString(manifest,Files.readString(root.resolve("pcm-only.json")).replace("\"codec\": \"PCM16_LE\"","\"codec\": \"PCM16_LE\", \"default_pitch\": \"invalid\"")); + assertThrows(IllegalArgumentException.class,()->AudioBankPacker.pack(root,manifest,false,mapper)); + } + + @Test + void rejectsMalformedWavAndSupportsProductionPcmConversion() throws Exception { + byte[] source = Files.readAllBytes(fixtures().resolve("mono.wav")); + source[20] = 99; + Files.write(temporary.resolve("bad.wav"), source); + Path manifest = temporary.resolve("bank.json"); + Files.writeString(manifest, "{\"samples\":[{\"file\":\"bad.wav\"}]}"); + assertThrows(IllegalArgumentException.class, () -> AudioBankPacker.pack(temporary, manifest, false, mapper)); + } +} diff --git a/prometeu-packer/prometeu-packer-v1/src/test/java/p/packer/services/FileSystemPackerWorkspaceServiceTest.java b/prometeu-packer/prometeu-packer-v1/src/test/java/p/packer/services/FileSystemPackerWorkspaceServiceTest.java index 57c287e1..edbb5cda 100644 --- a/prometeu-packer/prometeu-packer-v1/src/test/java/p/packer/services/FileSystemPackerWorkspaceServiceTest.java +++ b/prometeu-packer/prometeu-packer-v1/src/test/java/p/packer/services/FileSystemPackerWorkspaceServiceTest.java @@ -499,7 +499,7 @@ final class FileSystemPackerWorkspaceServiceTest { } @Test - void packValidationAndPackBlockUnsupportedIncludedAssetFamiliesGracefully() throws Exception { + void packValidationRejectsUnspecifiedAudioCompositionWithoutPublishingPartialArtifacts() throws Exception { final Path projectRoot = tempDir.resolve("pack-unsupported-family"); FileSystemPackerWorkspaceService service = service(); @@ -525,7 +525,7 @@ final class FileSystemPackerWorkspaceServiceTest { assertEquals(PackerOperationStatus.PARTIAL, validation.status()); assertFalse(validation.canPack()); assertTrue(validation.assets().getFirst().diagnostics().stream() - .anyMatch(diagnostic -> diagnostic.message().contains("not implemented yet for asset family sound_bank"))); + .anyMatch(diagnostic -> diagnostic.message().contains("Unable to pack audio bank"))); assertEquals(PackerOperationStatus.PARTIAL, pack.status()); assertTrue(pack.summary().contains("blocking diagnostics")); @@ -1655,6 +1655,27 @@ final class FileSystemPackerWorkspaceServiceTest { return new PackerProjectContext("main", root); } + @Test + void packsSoundAndSelfContainedMusicThroughExistingWorkspacePipeline() throws Exception { + Path projectRoot=copyFixture("workspaces/orphan-valid",tempDir.resolve("audio-packing")); + Path fixture=PackerFixtureLocator.fixtureRoot("audio");Path musicRoot=projectRoot.resolve("assets/music");Files.createDirectories(musicRoot); + for(String file:List.of("mixed-music.json","mono.wav","stereo.wav"))Files.copy(fixture.resolve(file),musicRoot.resolve(file)); + Files.writeString(musicRoot.resolve("asset.json"),""" + {"schema_version":1,"asset_uuid":"music-uuid","name":"stage","type":"music_bank","output":{"format":"MUSIC/bank_v1","codec":"NONE"},"preload":{"enabled":true},"artifacts":[{"file":"mixed-music.json","index":0}]} + """); + var service=service();var context=project(projectRoot); + assertEquals(PackerOperationStatus.SUCCESS,service.registerAsset(new RegisterAssetRequest(context,AssetReference.forRelativeAssetRoot("orphans/ui_sounds"))).status()); + assertEquals(PackerOperationStatus.SUCCESS,service.registerAsset(new RegisterAssetRequest(context,AssetReference.forRelativeAssetRoot("music"))).status()); + var result=service.packWorkspace(new PackWorkspaceRequest(context));assertEquals(PackerOperationStatus.SUCCESS,result.status(),result.summary()); + var table=MAPPER.readTree(projectRoot.resolve("build/asset_table.json").toFile());assertEquals(2,table.size()); + byte[] first=Files.readAllBytes(projectRoot.resolve("build/assets.pa"));int payload=(int)readLeLong(first,12);boolean found=false; + for(var asset:table){if(asset.path("bank_type").asText().equals("MUSIC")){found=true;int begin=payload+asset.path("offset").asInt();int end=begin+asset.path("size").asInt();assertArrayEquals(Files.readAllBytes(fixture.resolve("mixed-music.mbnk")),java.util.Arrays.copyOfRange(first,begin,end));assertEquals(6395,asset.path("decoded_size").asInt());}} + assertTrue(found);assertEquals(1,MAPPER.readTree(projectRoot.resolve("build/preload.json").toFile()).size()); + assertEquals(PackerOperationStatus.SUCCESS,service.packWorkspace(new PackWorkspaceRequest(context)).status());assertArrayEquals(first,Files.readAllBytes(projectRoot.resolve("build/assets.pa"))); + byte[] changed=Files.readAllBytes(musicRoot.resolve("mono.wav"));changed[44]=100;Files.write(musicRoot.resolve("mono.wav"),changed); + assertEquals(PackerOperationStatus.SUCCESS,service.packWorkspace(new PackWorkspaceRequest(context)).status());assertFalse(java.util.Arrays.equals(first,Files.readAllBytes(projectRoot.resolve("build/assets.pa")))); + } + private FileSystemPackerWorkspaceService service() { return service(ignored -> { }); } diff --git a/prometeu-packer/prometeu-packer-v1/src/test/java/p/packer/services/PackerAssetDeclarationParserTest.java b/prometeu-packer/prometeu-packer-v1/src/test/java/p/packer/services/PackerAssetDeclarationParserTest.java index 12287161..d47630f0 100644 --- a/prometeu-packer/prometeu-packer-v1/src/test/java/p/packer/services/PackerAssetDeclarationParserTest.java +++ b/prometeu-packer/prometeu-packer-v1/src/test/java/p/packer/services/PackerAssetDeclarationParserTest.java @@ -46,7 +46,7 @@ final class PackerAssetDeclarationParserTest { "name": "pipeline_asset", "type": "sound_bank", "output": { - "format": "SOUND/v1", + "format": "SOUND/bank_v1", "codec": "NONE", "pipeline": { "samples": { diff --git a/prometeu-packer/prometeu-packer-v1/src/test/java/p/packer/services/PackerAssetDetailsServiceTest.java b/prometeu-packer/prometeu-packer-v1/src/test/java/p/packer/services/PackerAssetDetailsServiceTest.java index 079fd9ed..d5ceed67 100644 --- a/prometeu-packer/prometeu-packer-v1/src/test/java/p/packer/services/PackerAssetDetailsServiceTest.java +++ b/prometeu-packer/prometeu-packer-v1/src/test/java/p/packer/services/PackerAssetDetailsServiceTest.java @@ -244,7 +244,7 @@ final class PackerAssetDetailsServiceTest { } @Test - void exposesAudioMetadataDefaultsFromOutputContract() throws Exception { + void keepsSampleMetadataInCompositionRatherThanBankWideDefaults() throws Exception { final Path projectRoot = copyFixture("workspaces/orphan-valid", tempDir.resolve("orphan-audio-defaults")); final PackerAssetDetailsService service = service(); @@ -254,7 +254,7 @@ final class PackerAssetDetailsServiceTest { assertEquals(PackerOperationStatus.SUCCESS, result.status()); assertEquals( - Map.of("sample_rate", "44100", "channels", "1"), + Map.of(), result.details().metadataFields().stream().collect(java.util.stream.Collectors.toMap( field -> field.key(), field -> field.value()))); diff --git a/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/ima-only.json b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/ima-only.json new file mode 100644 index 00000000..e7bb2e41 --- /dev/null +++ b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/ima-only.json @@ -0,0 +1,11 @@ +{ + "name": "ima-only", + "samples": [ + { + "file": "mono.wav", + "codec": "IMA_ADPCM_WAV4", + "loop_start": 0, + "loop_end": 1000 + } + ] +} diff --git a/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/ima-only.sbnk b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/ima-only.sbnk new file mode 100644 index 00000000..64f140eb Binary files /dev/null and b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/ima-only.sbnk differ diff --git a/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/mixed-music.json b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/mixed-music.json new file mode 100644 index 00000000..d7ed5933 --- /dev/null +++ b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/mixed-music.json @@ -0,0 +1,70 @@ +{ + "name": "mixed-music", + "samples": [ + { + "file": "mono.wav", + "codec": "PCM16_LE", + "loop_start": 0, + "loop_end": 1000 + }, + { + "file": "stereo.wav", + "codec": "IMA_ADPCM_WAV4", + "loop_start": 0, + "loop_end": 1000 + } + ], + "ppq": 960, + "tempo_us_per_quarter": 500000, + "duration_ticks": 3840, + "loop_start_tick": 0, + "track_count": 2, + "instruments": [ + { + "id": 0, + "sample": 0, + "looping": true, + "send": 32768, + "attack_ms": 10.0, + "release_ms": 200.0 + }, + { + "id": 1, + "sample": 1, + "looping": true, + "root": 72, + "vibrato_millihz": 5000, + "vibrato_cents": 10 + } + ], + "events": [ + { + "tick": 0, + "kind": 1, + "a": 1, + "b": 0, + "c": 60, + "d": 200 + }, + { + "tick": 40, + "kind": 1, + "track": 1, + "a": 2, + "b": 1, + "c": 72, + "d": 180 + }, + { + "tick": 960, + "kind": 2, + "a": 1 + }, + { + "tick": 1920, + "kind": 2, + "track": 1, + "a": 2 + } + ] +} diff --git a/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/mixed-music.mbnk b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/mixed-music.mbnk new file mode 100644 index 00000000..924f8c00 Binary files /dev/null and b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/mixed-music.mbnk differ diff --git a/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/mixed-sound.json b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/mixed-sound.json new file mode 100644 index 00000000..60a8c1a4 --- /dev/null +++ b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/mixed-sound.json @@ -0,0 +1,17 @@ +{ + "name": "mixed-sound", + "samples": [ + { + "file": "mono.wav", + "codec": "PCM16_LE", + "loop_start": 0, + "loop_end": 1000 + }, + { + "file": "stereo.wav", + "codec": "IMA_ADPCM_WAV4", + "loop_start": 0, + "loop_end": 1000 + } + ] +} diff --git a/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/mixed-sound.sbnk b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/mixed-sound.sbnk new file mode 100644 index 00000000..be47155c Binary files /dev/null and b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/mixed-sound.sbnk differ diff --git a/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/mono.wav b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/mono.wav new file mode 100644 index 00000000..0de7e9ca Binary files /dev/null and b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/mono.wav differ diff --git a/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/pcm-only.json b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/pcm-only.json new file mode 100644 index 00000000..30e4b04a --- /dev/null +++ b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/pcm-only.json @@ -0,0 +1,11 @@ +{ + "name": "pcm-only", + "samples": [ + { + "file": "mono.wav", + "codec": "PCM16_LE", + "loop_start": 0, + "loop_end": 1000 + } + ] +} diff --git a/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/pcm-only.sbnk b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/pcm-only.sbnk new file mode 100644 index 00000000..707f681f Binary files /dev/null and b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/pcm-only.sbnk differ diff --git a/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/stereo.wav b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/stereo.wav new file mode 100644 index 00000000..bd2ea20d Binary files /dev/null and b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/audio/stereo.wav differ diff --git a/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/workspaces/orphan-valid/assets/orphans/ui_sounds/asset.json b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/workspaces/orphan-valid/assets/orphans/ui_sounds/asset.json index 1df23770..1b6b1b2c 100644 --- a/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/workspaces/orphan-valid/assets/orphans/ui_sounds/asset.json +++ b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/workspaces/orphan-valid/assets/orphans/ui_sounds/asset.json @@ -9,5 +9,13 @@ }, "preload": { "enabled": false - } + }, + "artifacts": [ + { + "file": "bank.json", + "index": 0, + "artifact_id": "audio-composition", + "fingerprint": "d5890fc2b3930e348166045e1b5078f8e0cf30df16de7e983c2db43a7263effa" + } + ] } diff --git a/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/workspaces/orphan-valid/assets/orphans/ui_sounds/bank.json b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/workspaces/orphan-valid/assets/orphans/ui_sounds/bank.json new file mode 100644 index 00000000..9296cc2d --- /dev/null +++ b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/workspaces/orphan-valid/assets/orphans/ui_sounds/bank.json @@ -0,0 +1 @@ +{"name":"ui","samples":[{"file":"tone.wav","codec":"PCM16_LE"}]} diff --git a/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/workspaces/orphan-valid/assets/orphans/ui_sounds/tone.wav b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/workspaces/orphan-valid/assets/orphans/ui_sounds/tone.wav new file mode 100644 index 00000000..0de7e9ca Binary files /dev/null and b/prometeu-packer/prometeu-packer-v1/src/test/resources/fixtures/workspaces/orphan-valid/assets/orphans/ui_sounds/tone.wav differ diff --git a/prometeu-studio-core/src/main/java/p/studio/core/assets/PackerAssetCreationCatalog.java b/prometeu-studio-core/src/main/java/p/studio/core/assets/PackerAssetCreationCatalog.java index 2ad48230..d0451d80 100644 --- a/prometeu-studio-core/src/main/java/p/studio/core/assets/PackerAssetCreationCatalog.java +++ b/prometeu-studio-core/src/main/java/p/studio/core/assets/PackerAssetCreationCatalog.java @@ -12,9 +12,9 @@ import java.util.List; public final class PackerAssetCreationCatalog implements AssetCreationCatalog { @Override public List supportedFamilies() { - return Arrays.stream(AssetFamilyCatalog.values()) - .filter(family -> family != AssetFamilyCatalog.UNKNOWN) - .map(family -> AssetFamily.valueOf(family.name())) + return Arrays.stream(AssetFamily.values()) + .filter(family -> family != AssetFamily.UNKNOWN) + .filter(family -> AssetFamilyCatalog.fromManifestType(family.manifestType()) != AssetFamilyCatalog.UNKNOWN) .toList(); }