Skip to content

Commit

Permalink
Merge pull request #61 from libertyernie/vgaudio-reread
Browse files Browse the repository at this point in the history
Clean up PCM16Audio and re-read original data from file instead
  • Loading branch information
libertyernie authored Dec 20, 2021
2 parents 494f89c + 1b82e77 commit 8bbd54d
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 77 deletions.
34 changes: 12 additions & 22 deletions LoopingAudioConverter/About.html
Original file line number Diff line number Diff line change
Expand Up @@ -432,30 +432,20 @@ <h1>Looping Audio Converter 2.4</h1>
start until the loop end. The text in "suffix" will be appended
to the filename.</li>
<li><b>Skip re-encoding for similar formats when possible:</b>
This option takes effect when no audio effects are selected and
the input/output formats line up.</li>
Choosing this option
allows Looping Audio Converter to skip re-encoding audio in
certain circumstances:</li>
<ul>
<li>Input/output are both VGAudio supported formats:</li>
<ul>
<li>If the VGAudio decoder is being used, and the encoding
(e.g. ADPCM) is the same, VGAudio can copy the original
encoded data between these formats without re-encoding. This
provides a lossless conversion and is much faster.<br>
</li>
</ul>
<li>Input/output are both Ogg Vorbis:</li>
<ul>
<li>The file will be copied, but loop information will still
be added (or removed) as appropriate. (Note: if the input
file does not have a comment header, you may run into an
error.)</li>
</ul>
<li>
A VGAudio output format is used and VGAudio also supports the input format
</li>
<li>
An output format is used that uses ffmpeg / SoX as the converter (such as Ogg Vorbis or MP3) and the input and output file extensions match
</li>
<li>
The "MP3" output format is used and the input file is an MSF file with MP3 data
</li>
</ul>
<li><b>Use VGAudio decoder</b>: This option uses VGAudio to decode
when possible, allowing the "skip re-encoding" option for
formats that it supports. If you turn this off, vgmstream will
be used instead.<br>
</li>
<li><b>Number of simultaneous encoding/decoding tasks</b>: Audio
encoding can take a long time, so this program lets you run
several encoding processes in parallel. This number represents
Expand Down
12 changes: 11 additions & 1 deletion LoopingAudioConverter/Exporters/EffectEngineExporter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using MSFContainerLib;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -19,6 +20,15 @@ public EffectEngineExporter(IEffectEngine effectEngine, string encoding_paramete
public async Task WriteFileAsync(PCM16Audio lwav, string output_dir, string original_filename_no_ext) {
string output_filename = Path.Combine(output_dir, original_filename_no_ext + output_extension);

if (Path.GetExtension(lwav.OriginalPath ?? "").Equals(".msf", StringComparison.InvariantCultureIgnoreCase) && output_extension == ".mp3") {
byte[] data = File.ReadAllBytes(lwav.OriginalPath);
IPcmAudioSource<short> msf = MSF.Parse(data);
if (msf is MSF_MP3 mp3) {
File.WriteAllBytes(output_filename, mp3.Body);
return;
}
}

if (lwav.OriginalPath != null && output_extension.Equals(Path.GetExtension(lwav.OriginalPath), StringComparison.InvariantCultureIgnoreCase)) {
File.Copy(lwav.OriginalPath, output_filename, true);
} else {
Expand Down
5 changes: 0 additions & 5 deletions LoopingAudioConverter/Exporters/MP3Exporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ public async Task WriteFileAsync(PCM16Audio lwav, string output_dir, string orig
throw new AudioExporterException("Invalid character (\") found in output filename");
}

if (lwav.OriginalMP3 != null) {
File.WriteAllBytes(outPath, lwav.OriginalMP3);
return;
}

string infile = TempFiles.Create("wav");
File.WriteAllBytes(infile, lwav.Export());

Expand Down
15 changes: 11 additions & 4 deletions LoopingAudioConverter/Exporters/VGAudioExporter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VGAudio.Containers.Wave;
using VGAudio.Formats;
Expand All @@ -12,8 +9,18 @@ public abstract class VGAudioExporter : IAudioExporter {
protected abstract byte[] GetData(AudioData audio);
protected abstract string GetExtension();

private static AudioData Read(PCM16Audio lwav) {
try {
if (lwav.OriginalPath != null) {
return VGAudioImporter.Read(File.ReadAllBytes(lwav.OriginalPath), lwav.OriginalPath);
}
} catch (NotImplementedException) { }

return new WaveReader().Read(lwav.Export());
}

public void WriteFile(PCM16Audio lwav, string output_dir, string original_filename_no_ext) {
AudioData audio = lwav.OriginalAudioData ?? new WaveReader().Read(lwav.Export());
AudioData audio = Read(lwav);
audio.SetLoop(lwav.Looping, lwav.LoopStart, lwav.LoopEnd);
byte[] data = GetData(audio);
File.WriteAllBytes(Path.Combine(output_dir, original_filename_no_ext + GetExtension()), data);
Expand Down
4 changes: 1 addition & 3 deletions LoopingAudioConverter/Importers/MP3Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public async Task<PCM16Audio> ReadFileAsync(string filename) {

byte[] array = output.ToArray();
short[] samples = ToUInt16Array(array);
return new PCM16Audio(mp3.ChannelCount, mp3.Frequency, samples) {
OriginalMP3 = mp3data
};
return new PCM16Audio(mp3.ChannelCount, mp3.Frequency, samples);
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions LoopingAudioConverter/Importers/MSFImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ public Task<PCM16Audio> ReadFileAsync(string filename) {
msf.LoopStartSample,
msf.LoopStartSample + msf.LoopSampleCount,
!msf.IsLooping);
if (msf is MSF_MP3 mp3) {
lwav.OriginalMP3 = mp3.Body;
}
return Task.FromResult(lwav);
} catch (NotSupportedException) {
throw new AudioImporterException("Cannot read MSF file (unsupported codec?)");
Expand Down
18 changes: 13 additions & 5 deletions LoopingAudioConverter/Importers/VGAudioImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using VGAudio.Containers.Hps;
using VGAudio.Containers.Idsp;
using VGAudio.Containers.NintendoWare;
using VGAudio.Containers.Wave;
using VGAudio.Formats;

namespace LoopingAudioConverter {
Expand Down Expand Up @@ -40,7 +41,7 @@ public bool SupportsExtension(string extension) {
}
}

private static AudioData Read(byte[] data, string filename) {
public static AudioData Read(byte[] data, string filename) {
string extension = Path.GetExtension(filename).ToLowerInvariant();
if (extension.StartsWith("."))
extension = extension.Substring(1);
Expand Down Expand Up @@ -81,16 +82,23 @@ public async Task<PCM16Audio> ReadFileAsync(string filename) {
throw new AudioImporterException("File paths with double quote marks (\") are not supported");
}

byte[] data = File.ReadAllBytes(filename);
if (data.Length == 0) {
byte[] indata = File.ReadAllBytes(filename);
if (indata.Length == 0) {
throw new AudioImporterException("Empty input file");
}

try {
await Task.Yield();
return PCM16Factory.FromAudioData(Read(data, filename));

AudioData a = Read(indata, filename);
byte[] wavedata = new WaveWriter().GetFile(a);
var w = PCM16Factory.FromByteArray(wavedata);
if (a.GetAllFormats().All(f => !f.Looping)) {
w.NonLooping = true;
}
return w;
} catch (Exception e) {
throw new AudioImporterException("Could not convert from B" + (char)data[0] + "STM: " + e.Message);
throw new AudioImporterException("Could not convert using VGAudio: " + e.Message);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion LoopingAudioConverter/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public class Options {
public bool ExportLoop { get; set; }
public string LoopSuffix { get; set; }
public bool ShortCircuit { get; set; }
public bool VGAudioDecoder { get; set; }
public int NumSimulTasks { get; set; }
}
}
15 changes: 0 additions & 15 deletions LoopingAudioConverter/OptionsForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion LoopingAudioConverter/OptionsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ public Options GetOptions() {
LoopSuffix = txtStartEndFilenamePattern.Text,
NumSimulTasks = (int)numSimulTasks.Value,
ShortCircuit = chkShortCircuit.Checked,
VGAudioDecoder = chkVGAudioDecoder.Checked
};
}

Expand Down
3 changes: 0 additions & 3 deletions LoopingAudioConverter/PCM16Audio.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using MSFContainerLib;
using VGAudio.Formats;

namespace LoopingAudioConverter {
/// <summary>
Expand Down Expand Up @@ -38,8 +37,6 @@ public int LoopLength {
}

public string OriginalPath { get; set; }
public AudioData OriginalAudioData { get; set; }
public byte[] OriginalMP3 { get; set; }

IEnumerable<short> IPcmAudioSource<short>.SampleData => Samples;
int IPcmAudioSource<short>.Channels => Channels;
Expand Down
10 changes: 0 additions & 10 deletions LoopingAudioConverter/PCM16Factory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,6 @@ public static PCM16Audio FromByteArray(byte[] p) {
}
}

public static PCM16Audio FromAudioData(AudioData a) {
byte[] data = new WaveWriter().GetFile(a);
var w = FromByteArray(data);
if (!a.GetAllFormats().Any(f => f.Looping)) {
w.NonLooping = true;
}
w.OriginalAudioData = a;
return w;
}

/// <summary>
/// Reads RIFF WAVE data from a file.
/// This method opens a read-only FileStream and sends the stream to the FromStream method.
Expand Down
5 changes: 1 addition & 4 deletions LoopingAudioConverter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ public static async Task RunAsync(Options o, bool showEndDialog = true, IWin32Wi

IEnumerable <IAudioImporter> buildImporters() {
yield return new WAVImporter();
if (o.VGAudioDecoder)
yield return new VGAudioImporter();
yield return new VGAudioImporter();
yield return new MP3Importer();
if (ConfigurationManager.AppSettings["faad_path"] is string faad_path)
yield return new MP4Importer(faad_path);
Expand Down Expand Up @@ -311,8 +310,6 @@ IAudioExporter getExporter() {

if (!o.ShortCircuit) {
toExport.Audio.OriginalPath = null;
toExport.Audio.OriginalAudioData = null;
toExport.Audio.OriginalMP3 = null;
}
if (!o.WriteLoopingMetadata) {
toExport.Audio.Looping = false;
Expand Down

0 comments on commit 8bbd54d

Please sign in to comment.