Triforce v0.6.0 "Sage Of Power"

Faster frames (the replay's memory work, held submits, helper threads, collection and capture cuts), the game letting old areas go (QueryMemoryInfo), the 0.5.2 stutter fixed, AMD RX 5000/6000 and integrated-GPU fixes, the GPU device chosen by name, the in-game menus and launcher reworked, controller, Linux and benchmark fixes.
This commit is contained in:
ruckus7382sfgsgdf
2026-11-20 17:06:21 -06:00
parent b0e46adb48
commit 6c7117635c
265 changed files with 26893 additions and 2549 deletions

View File

@@ -4,7 +4,11 @@
//
// Layout: "BWAV", a byte order mark, a version, a CRC32, a prefetch flag (u16) and the channel count (u16 at 0x0E), then
// 0x4C bytes per channel: codec (u16: 0 PCM16, 1 DSP-ADPCM), pan (u16: 0 left, 1 right, 2 middle), sample rate (u32),
// sample count (u32), the count again, 16 DSP coefficients (s16), the place of the samples in the file (u32 at 0x30), ...
// sample count (u32), the count again, 16 DSP coefficients (s16), the place of the samples in the file (u32 at 0x30) and
// again (0x34), a u32 that is 1 in every file, looped or not (0x38), the loop's end (u32 at 0x3C: the sample after its
// last one, 0xFFFFFFFF for a sound that does not loop) and its start (u32 at 0x40), then the ADPCM state at the loop.
// The music loops that way: Saria's Song, the Fairy Fountain and the Temple of Time play an intro once and then loop to
// their end from a sample after it; the item fanfare and the interface sounds have no loop.
// DSP-ADPCM is frames of 8 bytes: a header (the coefficient pair's index, the scale's exponent) and 14 samples of four bits.
// All numbers little-endian. Other codecs (the title music's codec 2) are not decoded.
namespace Triforce.Core.GameFiles;
@@ -17,6 +21,10 @@ public sealed class Pcm
public int Frames => Samples.Length / 2;
public double Seconds => Rate > 0 ? (double)Frames / Rate : 0;
/// <summary>The game's loop, in frames: from Start to just before End, which is at most Frames. Null for a sound that
/// plays once (or a WAV file without a loop).</summary>
public (int Start, int End)? Loop { get; init; }
/// <summary>The loudest sample's size (0 for silence).</summary>
public int Peak
{
@@ -76,7 +84,21 @@ public static class Bwav
samples[2 * i] = (short)Math.Clamp((int)l, -32768, 32767);
samples[2 * i + 1] = (short)Math.Clamp((int)r, -32768, 32767);
}
return new Pcm { Rate = (int)rate, Samples = samples };
return new Pcm { Rate = (int)rate, Samples = samples, Loop = LoopOf(b, 0x10, frames) };
}
/// <summary>The loop a channel's header gives (its end at 0x3C, its start at 0x40), cut to the sound's length; null for
/// a sound that does not loop (end 0xFFFFFFFF) or a loop that is none (its start not before its end). The first
/// channel's: the game's files give every channel the same.</summary>
static (int Start, int End)? LoopOf(byte[] b, long info, long frames)
{
long end = U32(b, info + 0x3C), start = U32(b, info + 0x40);
if (end == 0xFFFFFFFF)
return null;
end = Math.Min(end, frames);
if (start >= end)
return null;
return ((int)start, (int)end);
}
/// <summary>Is the channel one of the two codecs, with all of its samples (as many as the sound has, at most) inside the
@@ -131,7 +153,8 @@ public static class Bwav
/// <summary>
/// Tracks of the same rate summed sample by sample (a shorter one is silence after its end), then scaled so the loudest
/// sample is at `peak` of full scale, each sample cut toward zero: mixwav.py. One track in gives that track normalized.
/// Saria's Song is the eight BGM_LostWood_0 .. _7 layers mixed this way.
/// Saria's Song is the eight BGM_LostWood_0 .. _7 layers mixed this way. The mix keeps the tracks' loop when they all
/// have the same one (the eight layers do), else it has none.
/// </summary>
public static Pcm Mix(double peak, params Pcm[] tracks)
{
@@ -155,7 +178,10 @@ public static class Bwav
var output = new short[length];
for (var i = 0; i < length; i++)
output[i] = (short)Math.Clamp((int)(sum[i] * gain), -32768, 32767);
return new Pcm { Rate = rate, Samples = output };
var loop = tracks[0].Loop;
if (tracks.Any(t => t.Loop != loop))
loop = null;
return new Pcm { Rate = rate, Samples = output, Loop = loop };
}
/// <summary>Mix at the default peak (70 % of full scale).</summary>
@@ -171,51 +197,87 @@ public static class Bwav
var output = new short[s.Length];
for (var i = 0; i < s.Length; i++)
output[i] = (short)Math.Clamp((int)(s[i] * gain), -32768, 32767);
return new Pcm { Rate = track.Rate, Samples = output };
return new Pcm { Rate = track.Rate, Samples = output, Loop = track.Loop };
}
/// <summary>The sound as a WAV file's bytes: 16-bit stereo PCM with the usual 44-byte header.</summary>
/// <summary>The size of the smpl chunk ToWav writes for a loop: its 8-byte head, 36 bytes and one loop of 24.</summary>
public const int SmplChunkSize = 8 + 36 + 24;
/// <summary>
/// The sound as a WAV file's bytes: 16-bit stereo PCM with the usual 44-byte header. A sound with a loop gets a smpl
/// chunk after its data (the samples stay at byte 44, as in a file without one): one forward loop that plays for ever,
/// from the loop's start to its last sample (smpl counts the end in), which is what the launcher's page loops between.
/// </summary>
public static byte[] ToWav(Pcm pcm)
{
var data = pcm.Samples.Length * 2;
var wav = new byte[44 + data];
var smpl = pcm.Loop.HasValue ? SmplChunkSize : 0;
var wav = new byte[44 + data + smpl];
void Tag(int at, string tag) { for (var i = 0; i < 4; i++) wav[at + i] = (byte)tag[i]; }
void Put(int at, int value, int bytes) { for (var i = 0; i < bytes; i++) wav[at + i] = (byte)(value >> 8 * i); }
Tag(0, "RIFF"); Put(4, 36 + data, 4); Tag(8, "WAVE");
Tag(0, "RIFF"); Put(4, 36 + data + smpl, 4); Tag(8, "WAVE");
Tag(12, "fmt "); Put(16, 16, 4); Put(20, 1, 2); Put(22, 2, 2); Put(24, pcm.Rate, 4); Put(28, pcm.Rate * 4, 4); Put(32, 4, 2); Put(34, 16, 2);
Tag(36, "data"); Put(40, data, 4);
System.Runtime.InteropServices.MemoryMarshal.AsBytes(pcm.Samples.AsSpan()).CopyTo(wav.AsSpan(44));
if (!BitConverter.IsLittleEndian)
for (var i = 44; i + 1 < wav.Length; i += 2)
for (var i = 44; i + 1 < 44 + data; i += 2)
(wav[i], wav[i + 1]) = (wav[i + 1], wav[i]);
if (pcm.Loop is { } loop)
{
// (manufacturer, product, the sample period in ns, MIDI note 60, no pitch fraction, no SMPTE, one loop, no
// sampler data; then the loop: cue 0, type 0 = forward, start, last sample, no fraction, play count 0 = for ever)
var at = 44 + data;
Tag(at, "smpl"); Put(at + 4, 36 + 24, 4);
Put(at + 16, pcm.Rate > 0 ? 1_000_000_000 / pcm.Rate : 0, 4); Put(at + 20, 60, 4); Put(at + 36, 1, 4);
Put(at + 52, loop.Start, 4); Put(at + 56, loop.End - 1, 4);
}
return wav;
}
/// <summary>A 16-bit stereo WAV file read back (for comparing with references; the data chunk is found by its tag).</summary>
/// <summary>The first loop of a WAV file's smpl chunk (its body at `at`, `n` bytes), with its end made the sample after
/// the last (null without a loop, or with a loop that ends before it starts).</summary>
static (int Start, int End)? SmplLoop(byte[] wav, int at, int n)
{
if (n < 36 + 24 || at + 36 + 24 > wav.Length || U32(wav, at + 28) == 0)
return null;
long start = U32(wav, at + 36 + 8), last = U32(wav, at + 36 + 12);
if (start > last || last >= int.MaxValue)
return null;
return ((int)start, (int)last + 1);
}
/// <summary>A 16-bit stereo WAV file read back (for comparing with references; the chunks are found by their tags), with
/// the loop of its smpl chunk when it has one inside the sound.</summary>
public static Pcm FromWav(byte[] wav)
{
if (wav.Length < 12 || wav[0] != 'R' || wav[8] != 'W')
throw new InvalidDataException("not a WAV file");
int rate = 0, at = 12;
short[]? samples = null;
(int Start, int End)? loop = null;
while (at + 8 <= wav.Length)
{
var tag = System.Text.Encoding.ASCII.GetString(wav, at, 4);
var n = (int)U32(wav, at + 4);
var n = (int)Math.Min(U32(wav, at + 4), wav.Length - at - 8);
if (tag == "fmt ")
{
if (U16(wav, at + 8 + 2) != 2 || U16(wav, at + 8 + 14) != 16)
throw new NotSupportedException("only 16-bit stereo WAV files are read");
rate = (int)U32(wav, at + 8 + 4);
}
else if (tag == "data")
else if (tag == "data" && samples == null)
{
n = Math.Min(n, wav.Length - at - 8);
var samples = new short[n / 2];
samples = new short[n / 2];
System.Runtime.InteropServices.MemoryMarshal.Cast<byte, short>(wav.AsSpan(at + 8, n / 2 * 2)).CopyTo(samples);
return new Pcm { Rate = rate, Samples = samples };
}
else if (tag == "smpl")
loop = SmplLoop(wav, at + 8, n);
at += 8 + n + (n & 1);
}
throw new InvalidDataException("WAV without data");
if (samples == null)
throw new InvalidDataException("WAV without data");
if (loop is { } l && l.End > samples.Length / 2)
loop = null;
return new Pcm { Rate = rate, Samples = samples, Loop = loop };
}
}

View File

@@ -16,6 +16,10 @@ public sealed class GameMedia
/// <summary>Changes whenever a decoder's output would change, so older cache files are not used.</summary>
const string CacheVersion = "gamemedia-1";
/// <summary>The same for the sounds alone (the art's files keep their names): "loop-1" since the WAV files carry the
/// game's loop in a smpl chunk, so a cache made before is decoded again once, its old files removed.</summary>
const string SoundVersion = CacheVersion + "+loop-1";
/// <summary>The title screen's texture archive.</summary>
public const string TitleArchive = "UI/Layout2Archive/Title.Product.100.Nin_Ounce_NVN2.bntx.zs";
@@ -97,7 +101,7 @@ public sealed class GameMedia
var tracks = new Pcm[paths.Length];
Parallel.For(0, paths.Length, i => tracks[i] = Bwav.Decode(source.Read(paths[i])));
return Bwav.ToWav(Bwav.Mix(tracks));
});
}, SoundVersion);
}
/// <summary>The names of the textures in an archive (to check a name, or to pick another piece of art).</summary>
@@ -114,9 +118,9 @@ public sealed class GameMedia
/// source files' stamps. Made by `make` when it is not there (written beside and then moved into place, so a reader
/// never sees half a file); other versions of the same item are removed then.
/// </summary>
string Cached(string fileName, string[] inputs, Func<byte[]> make)
string Cached(string fileName, string[] inputs, Func<byte[]> make, string version = CacheVersion)
{
var identity = new StringBuilder(CacheVersion).Append('|').Append(fileName);
var identity = new StringBuilder(version).Append('|').Append(fileName);
foreach (var input in inputs)
identity.Append('|').Append(input).Append('=').Append(source.Stamp(input)
?? throw new FileNotFoundException("The game has no " + input + " (" + source.Describe + ")", input));