Skip to content

Commit

Permalink
sound_archive: implement android ogg
Browse files Browse the repository at this point in the history
  • Loading branch information
UlyssesWu committed Oct 22, 2024
1 parent e3c5868 commit f6d438d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
27 changes: 24 additions & 3 deletions FreeMote.Plugins/Audio/OpusFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace FreeMote.Plugins.Audio
[Export(typeof(IPsbAudioFormatter))]
[ExportMetadata("Name", "FreeMote.NxOpus")]
[ExportMetadata("Author", "Ulysses")]
[ExportMetadata("Comment", "NX Opus support via VGAudio.")]
[ExportMetadata("Comment", "NX/Android Opus support via VGAudio.")]
class OpusFormatter : IPsbAudioFormatter
{
public List<string> Extensions { get; } = new List<string> {".opus"};
Expand All @@ -29,9 +29,13 @@ public bool CanToArchData(byte[] wave, Dictionary<string, object> context = null
return wave != null;
}

public static bool IsOgg(byte[] data)
{
return data != null && data.Length > 4 && data[0] == 'O' && data[1] == 'g' && data[2] == 'g' && data[3] == 'S';
}

public byte[] ToWave(AudioMetadata md, IArchData archData, string fileName = null, Dictionary<string, object> context = null)
{
NxOpusReader reader = new NxOpusReader();
byte[] rawData = archData.Data?.Data;

if (archData is OpusArchData data)
Expand All @@ -44,7 +48,7 @@ public byte[] ToWave(AudioMetadata md, IArchData archData, string fileName = nul
{
rawData = data.Body.Data.Data;
}
else
else if (rawData == null)
{
rawData = data.Body?.Data?.Data ?? data.Intro?.Data?.Data;
}
Expand All @@ -55,6 +59,12 @@ public byte[] ToWave(AudioMetadata md, IArchData archData, string fileName = nul
return null;
}

if (IsOgg(rawData))
{
return rawData;
}

NxOpusReader reader = new NxOpusReader();
var audioData = reader.Read(rawData);
using MemoryStream oms = new MemoryStream();
WaveWriter writer = new WaveWriter();
Expand All @@ -68,6 +78,13 @@ public bool ToArchData(AudioMetadata md, IArchData archData, in byte[] wave, str
{
return false;
}

if (IsOgg(wave))
{
data.Data.Data = wave; //must link to existed PsbResource
return true;
}

WaveReader reader = new WaveReader();
var rawData = reader.Read(wave);
using MemoryStream oms = new MemoryStream();
Expand Down Expand Up @@ -157,6 +174,10 @@ public bool TryGetArchData(AudioMetadata md, PsbDictionary channel, out IArchDat
if (archDic["data"] is PsbResource res)
{
opus.Data = res;
if (IsOgg(res.Data))
{
opus.WaveExtension = ".ogg";
}
}

//if (opus.Body != null && opus.Intro == null)
Expand Down
9 changes: 9 additions & 0 deletions FreeMote.Psb/Resources/ArchDatas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,15 @@ public IList<PsbResource> DataList {

public IPsbValue ToPsbArchData()
{
if (Data != null && PsbArchData != null) //android raw ogg
{
if (PsbArchData.ContainsKey("archData"))
{
return PsbArchData["archData"];
}
return PsbArchData;
}

var archData = new PsbDictionary
{
{"channelCount", ChannelCount.ToPsbNumber()},
Expand Down
2 changes: 1 addition & 1 deletion FreeMote.Psb/Resources/AudioMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void Link(string fullPath, FreeMountContext context)
{
return;
}
if (ChannelList.Count > 1 && AudioFormat != PsbAudioFormat.Unknown && AudioFormat != PsbAudioFormat.ADPCM)
if (ChannelList.Count > 1 && Pan != PsbAudioPan.LeftRight && AudioFormat != PsbAudioFormat.Unknown && AudioFormat != PsbAudioFormat.ADPCM)
{
Logger.LogWarn("[WARN] Audio with multiple channels is not supported. Send me the sample for research.");
}
Expand Down

0 comments on commit f6d438d

Please sign in to comment.