Skip to content
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

[Reopen] Write wavSmpl section #66

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion Exchange/DereTore.Exchange.Audio.HCA/HcaDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public int GetMinWaveHeaderBufferSize() {
}
var sizeNeeded = Marshal.SizeOf(typeof(WaveRiffSection));
if (hcaInfo.LoopFlag) {
// FIXME currently it's unknown what wavSmpl section means and how its values should be set.
// Maybe it's just ignored by common media players.
sizeNeeded += Marshal.SizeOf(typeof(WaveSampleSection));
}
if (hcaInfo.Comment != null && hcaInfo.Comment.Length > 0) {
Expand Down Expand Up @@ -72,6 +74,7 @@ public int WriteWaveHeader(byte[] stream, AudioParams audioParams) {
var sampleBits = GetSampleBitsFromParams();
var wavRiff = WaveRiffSection.CreateDefault();
var wavNote = WaveNoteSection.CreateDefault();
var wavSmpl = WaveSampleSection.CreateDefault();
var wavData = WaveDataSection.CreateDefault();
wavRiff.FmtType = (ushort)(_decodeParams.Mode != SamplingMode.R32 ? 1 : 3);
wavRiff.FmtChannels = (ushort)hcaInfo.ChannelCount;
Expand All @@ -85,13 +88,30 @@ public int WriteWaveHeader(byte[] stream, AudioParams audioParams) {
wavNote.NoteSize += 4 - (wavNote.NoteSize & 3);
}
}
if (hcaInfo.LoopFlag)
{
// FIXME I see "※計算方法不明" here:
// https://github.com/Nyagamon/HCADecoder/blob/e26b4d3a8bb450224ede3527d522c0330f7bf02b/clHCA.cpp#L556
// so ... I don't know how to handle this either.
// However this "smpl" header section seems to be unrecognized so it shouldn't matter.
wavSmpl.SamplePeriod = (1 / hcaInfo.SamplingRate * 1000000000);
wavSmpl.LoopStart = hcaInfo.LoopStart * 0x80 * 8 + hcaInfo.FmtR01;
wavSmpl.LoopEnd = (hcaInfo.LoopEnd + 1) * 0x80 * 8 - 1;
// LoopR01 should be "loop count", when it equals to 0x80 it means infinite loop
wavSmpl.LoopPlayCount = (hcaInfo.LoopR01 == 0x80) ? (ushort) 0 : hcaInfo.LoopR01;
}

var totalBlockCount = hcaInfo.BlockCount;
if (hcaInfo.LoopFlag) {
totalBlockCount += (hcaInfo.LoopEnd - hcaInfo.LoopStart) * audioParams.SimulatedLoopCount;
}
wavData.DataSize = totalBlockCount * 0x80 * 8 * wavRiff.FmtSamplingSize;
wavRiff.RiffSize = (uint)(0x1c + (hcaInfo.Comment != null ? wavNote.NoteSize : 0) + Marshal.SizeOf(wavData) + wavData.DataSize);
wavRiff.RiffSize = (uint)(
0x1c
+ (hcaInfo.Comment != null ? wavNote.NoteSize : 0)
+ (hcaInfo.LoopFlag ? Marshal.SizeOf(typeof(WaveSampleSection)) : 0)
+ Marshal.SizeOf(wavData) + wavData.DataSize
);

var bytesWritten = stream.Write(wavRiff, 0);
if (hcaInfo.Comment != null) {
Expand All @@ -101,6 +121,10 @@ public int WriteWaveHeader(byte[] stream, AudioParams audioParams) {
bytesWritten = address + 8 + (int)wavNote.NoteSize;
bytesWritten += 8 + (int)wavNote.NoteSize;
}
if (hcaInfo.LoopFlag)
{
bytesWritten += stream.Write(wavSmpl, bytesWritten);
}
bytesWritten += stream.Write(wavData, bytesWritten);
return bytesWritten;
}
Expand Down