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

BGM and voice file replacement #51

Merged
merged 9 commits into from
Mar 26, 2023
Merged
Show file tree
Hide file tree
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
19 changes: 14 additions & 5 deletions src/SerialLoops.Lib/Items/BackgroundMusicItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace SerialLoops.Lib.Items
{
public class BackgroundMusicItem : Item, ISoundItem
{
private string _bgmFile;

public string BgmFile { get; set; }
public int Index { get; set; }
public string BgmName { get; set; }
Expand All @@ -20,7 +22,8 @@ public class BackgroundMusicItem : Item, ISoundItem

public BackgroundMusicItem(string bgmFile, int index, ExtraFile extras, Project project) : base(Path.GetFileNameWithoutExtension(bgmFile), ItemType.BGM)
{
BgmFile = bgmFile;
BgmFile = Path.GetRelativePath(project.IterativeDirectory, bgmFile);
_bgmFile = bgmFile;
Index = index;
BgmName = extras.Bgms.FirstOrDefault(b => b.Index == Index).Name?.GetSubstitutedString(project) ?? "";
DisplayName = string.IsNullOrEmpty(BgmName) ? Name : BgmName;
Expand All @@ -39,23 +42,29 @@ public void PopulateScriptUses(Project project)
sec.Objects.Where(c => c.Command.Mnemonic == EventFile.CommandVerb.BGM_PLAY.ToString()).Select(c => (e.Name[0..^1], c))))
.Where(t => t.c.Parameters[0] == Index).ToArray();
}

public void Replace(string wavFile, string baseDirectory, string iterativeDirectory)
{
AdxUtil.EncodeWav(wavFile, Path.Combine(baseDirectory, BgmFile), false);
File.Copy(Path.Combine(baseDirectory, BgmFile), Path.Combine(iterativeDirectory, BgmFile), true);
}

public IWaveProvider GetWaveProvider(ILogger log)
{
byte[] adxBytes = Array.Empty<byte>();
try
{
adxBytes = File.ReadAllBytes(BgmFile);
adxBytes = File.ReadAllBytes(_bgmFile);
}
catch
{
if (!File.Exists(BgmFile))
if (!File.Exists(_bgmFile))
{
log.LogError($"Failed to load BGM file {BgmFile}: file not found.");
log.LogError($"Failed to load BGM file {_bgmFile}: file not found.");
}
else
{
log.LogError($"Failed to load BGM file {BgmFile}: file invalid.");
log.LogError($"Failed to load BGM file {_bgmFile}: file invalid.");
}
}

Expand Down
19 changes: 14 additions & 5 deletions src/SerialLoops.Lib/Items/VoicedLineItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ namespace SerialLoops.Lib.Items
{
public class VoicedLineItem : Item, ISoundItem
{
private string _vceFile;

public string VoiceFile { get; set; }
public int Index { get; set; }
public AdxEncoding AdxType { get; set; }
public (string ScriptName, ScriptCommandInvocation command)[] ScriptUses { get; set; }

public VoicedLineItem(string voiceFile, int index, Project project) : base(Path.GetFileNameWithoutExtension(voiceFile), ItemType.Voice)
{
VoiceFile = voiceFile;
VoiceFile = Path.GetRelativePath(project.IterativeDirectory, voiceFile);
_vceFile = voiceFile;
Index = index;
PopulateScriptUses(project);
}
Expand All @@ -27,17 +30,17 @@ public IWaveProvider GetWaveProvider(ILogger log)
byte[] adxBytes = Array.Empty<byte>();
try
{
adxBytes = File.ReadAllBytes(VoiceFile);
adxBytes = File.ReadAllBytes(_vceFile);
}
catch
{
if (!File.Exists(VoiceFile))
if (!File.Exists(_vceFile))
{
log.LogError($"Failed to load voice file {VoiceFile}: file not found.");
log.LogError($"Failed to load voice file {_vceFile}: file not found.");
}
else
{
log.LogError($"Failed to load voice file {VoiceFile}: file invalid.");
log.LogError($"Failed to load voice file {_vceFile}: file invalid.");
}
}

Expand All @@ -56,6 +59,12 @@ public IWaveProvider GetWaveProvider(ILogger log)
return new AdxWaveProvider(decoder);
}

public void Replace(string wavFile, string baseDirectory, string iterativeDirectory)
{
AdxUtil.EncodeWav(wavFile, Path.Combine(baseDirectory, VoiceFile), true);
File.Copy(Path.Combine(baseDirectory, VoiceFile), Path.Combine(iterativeDirectory, VoiceFile), true);
}

public override void Refresh(Project project)
{
PopulateScriptUses(project);
Expand Down
4 changes: 2 additions & 2 deletions src/SerialLoops.Lib/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,15 @@ public void LoadArchives(ILogger log, IProgressTracker tracker)
tracker.Finished++;
}

string[] bgmFiles = Directory.GetFiles(Path.Combine(IterativeDirectory, "original", "bgm")).OrderBy(s => s).ToArray();
string[] bgmFiles = Directory.GetFiles(Path.Combine(IterativeDirectory, "rom", "data", "bgm")).OrderBy(s => s).ToArray();
tracker.Focus("BGM Tracks", bgmFiles.Length);
for (int i = 0; i < bgmFiles.Length; i++)
{
Items.Add(new BackgroundMusicItem(bgmFiles[i], i, extras, this));
tracker.Finished++;
}

string[] voiceFiles = Directory.GetFiles(Path.Combine(IterativeDirectory, "original", "vce")).OrderBy(s => s).ToArray();
string[] voiceFiles = Directory.GetFiles(Path.Combine(IterativeDirectory, "rom", "data", "vce")).OrderBy(s => s).ToArray();
tracker.Focus("Voiced Lines", voiceFiles.Length);
for (int i = 0; i < voiceFiles.Length; i++)
{
Expand Down
4 changes: 2 additions & 2 deletions src/SerialLoops.Lib/SerialLoops.Lib.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
Expand All @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="HaruhiChokuretsuLib" Version="0.20.2" />
<PackageReference Include="HaruhiChokuretsuLib" Version="0.22.1" />
<PackageReference Include="NitroPacker.Core" Version="2.0.1" />
<PackageReference Include="QuikGraph" Version="2.5.0" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/SerialLoops/Controls/EditorTabsPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ internal DocumentPage CreateTab(ItemDescription item, Project project, ILogger l
case ItemDescription.ItemType.Background:
return new BackgroundEditor((BackgroundItem)project.Items.First(i => i.Name == item.Name), log);
case ItemDescription.ItemType.BGM:
return new BackgroundMusicEditor((BackgroundMusicItem)project.Items.First(i => i.Name == item.Name), log);
return new BackgroundMusicEditor((BackgroundMusicItem)project.Items.First(i => i.Name == item.Name), project, log);
case ItemDescription.ItemType.Character_Sprite:
return new CharacterSpriteEditor((CharacterSpriteItem)project.Items.First(i => i.Name == item.Name), project, log);
case ItemDescription.ItemType.Chibi:
Expand All @@ -89,7 +89,7 @@ internal DocumentPage CreateTab(ItemDescription item, Project project, ILogger l
case ItemDescription.ItemType.Topic:
return new TopicEditor((TopicItem)project.Items.First(i => i.Name == item.Name), project, log);
case ItemDescription.ItemType.Voice:
return new VoicedLineEditor((VoicedLineItem)project.Items.First(i => i.Name == item.Name), log);
return new VoicedLineEditor((VoicedLineItem)project.Items.First(i => i.Name == item.Name), project, log);
default:
log.LogError("Invalid item type!");
return null;
Expand Down
54 changes: 52 additions & 2 deletions src/SerialLoops/Editors/BackgroundMusicEditor.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using Eto.Forms;
using HaruhiChokuretsuLib.Util;
using NAudio.Wave;
using SerialLoops.Controls;
using SerialLoops.Lib;
using SerialLoops.Lib.Items;
using SerialLoops.Utility;
using System.IO;

namespace SerialLoops.Editors
{
Expand All @@ -11,7 +14,7 @@ public class BackgroundMusicEditor : Editor
private BackgroundMusicItem _bgm;
public SoundPlayerPanel BgmPlayer { get; set; }

public BackgroundMusicEditor(BackgroundMusicItem item, ILogger log) : base(item, log)
public BackgroundMusicEditor(BackgroundMusicItem item, Project project, ILogger log) : base(item, log, project)
{
}

Expand All @@ -20,9 +23,56 @@ public override Container GetEditorPanel()
_bgm = (BackgroundMusicItem)Description;
BgmPlayer = new(_bgm, _log);

Button extractButton = new() { Text = "Extract" };
extractButton.Click += (obj, args) =>
{
SaveFileDialog saveFileDialog = new() { Title = "Save BGM as WAV" };
saveFileDialog.Filters.Add(new() { Name = "WAV File", Extensions = new string[] { ".wav" } });
if (saveFileDialog.ShowAndReportIfFileSelected(this))
{
WaveFileWriter.CreateWaveFile(saveFileDialog.FileName, _bgm.GetWaveProvider(_log));
}
};

Button replaceButton = new() { Text = "Replace" };
replaceButton.Click += (obj, args) =>
{
OpenFileDialog openFileDialog = new() { Title = "Replace BGM" };
openFileDialog.Filters.Add(new() { Name = "WAV File", Extensions = new string[] { ".wav" } });
if (openFileDialog.ShowAndReportIfFileSelected(this))
{
LoopyProgressTracker tracker = new();
BgmPlayer.Stop();
_ = new ProgressDialog(() => _bgm.Replace(openFileDialog.FileName, _project.BaseDirectory, _project.IterativeDirectory), () =>
{
Content = GetEditorPanel();
}, tracker, "Replace BGM track");
}
};

Button restoreButton = new() { Text = "Restore" };
restoreButton.Click += (obj, args) =>
{
BgmPlayer.Stop();
File.Copy(Path.Combine(_project.BaseDirectory, "original", "bgm", Path.GetFileName(_bgm.BgmFile)), Path.Combine(_project.BaseDirectory, _bgm.BgmFile), true);
File.Copy(Path.Combine(_project.IterativeDirectory, "original", "bgm", Path.GetFileName(_bgm.BgmFile)), Path.Combine(_project.IterativeDirectory, _bgm.BgmFile), true);
Content = GetEditorPanel();
jonko0493 marked this conversation as resolved.
Show resolved Hide resolved
};

return new TableLayout(
new TableRow(ControlGenerator.GetPlayerStackLayout(BgmPlayer, _bgm.BgmName, _bgm.Name)),
new TableRow() // todo extract / replace buttons
new TableRow(new StackLayout
{
Orientation = Orientation.Horizontal,
Spacing = 3,
Items =
{
replaceButton,
extractButton,
restoreButton,
}
}),
new TableRow()
);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/SerialLoops/Editors/TopicEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ public override Container GetEditorPanel()
Spacing = 5,
Items =
{
ControlGenerator.GetControlWithLabel("Unknown 00", new TextBox { Text = _topic.Topic.UnknownShort00.ToString() }),
ControlGenerator.GetControlWithLabel("Unknown 01", new TextBox { Text = _topic.Topic.UnknownShort01.ToString() }),
ControlGenerator.GetControlWithLabel("Type", new TextBox { Text = _topic.Topic.Type.ToString() }),
ControlGenerator.GetControlWithLabel("Episode Group", new TextBox { Text = _topic.Topic.EpisodeGroup.ToString() }),
ControlGenerator.GetControlWithLabel("Group Selection", new TextBox { Text = _topic.Topic.GroupSelection.ToString() }),
ControlGenerator.GetControlWithLabel("Unknown 03", new TextBox { Text = _topic.Topic.UnknownShort03.ToString() }),
ControlGenerator.GetControlWithLabel("Unknown 04", new TextBox { Text = _topic.Topic.UnknownShort04.ToString() }),
ControlGenerator.GetControlWithLabel("Unknown 09", new TextBox { Text = _topic.Topic.UnknownShort09.ToString() }),
Expand Down
54 changes: 52 additions & 2 deletions src/SerialLoops/Editors/VoicedLineEditor.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using Eto.Forms;
using HaruhiChokuretsuLib.Util;
using NAudio.Wave;
using SerialLoops.Controls;
using SerialLoops.Lib;
using SerialLoops.Lib.Items;
using SerialLoops.Utility;
using System.IO;

namespace SerialLoops.Editors
{
Expand All @@ -11,7 +14,7 @@ public class VoicedLineEditor : Editor
private VoicedLineItem _vce;
public SoundPlayerPanel VcePlayer { get; set; }

public VoicedLineEditor(VoicedLineItem vce, ILogger log) : base(vce, log)
public VoicedLineEditor(VoicedLineItem vce, Project project, ILogger log) : base(vce, log, project)
{
}

Expand All @@ -20,9 +23,56 @@ public override Container GetEditorPanel()
_vce = (VoicedLineItem)Description;
VcePlayer = new(_vce, _log);

Button extractButton = new() { Text = "Extract" };
extractButton.Click += (obj, args) =>
{
SaveFileDialog saveFileDialog = new() { Title = "Save voiced line as WAV" };
saveFileDialog.Filters.Add(new() { Name = "WAV File", Extensions = new string[] { ".wav" } });
if (saveFileDialog.ShowAndReportIfFileSelected(this))
{
WaveFileWriter.CreateWaveFile(saveFileDialog.FileName, _vce.GetWaveProvider(_log));
}
};

Button replaceButton = new() { Text = "Replace" };
replaceButton.Click += (obj, args) =>
{
OpenFileDialog openFileDialog = new() { Title = "Replace voiced line" };
openFileDialog.Filters.Add(new() { Name = "WAV File", Extensions = new string[] { ".wav" } });
if (openFileDialog.ShowAndReportIfFileSelected(this))
{
LoopyProgressTracker tracker = new();
VcePlayer.Stop();
_ = new ProgressDialog(() => _vce.Replace(openFileDialog.FileName, _project.BaseDirectory, _project.IterativeDirectory), () =>
{
Content = GetEditorPanel();
}, tracker, "Replace voiced line");
}
};

Button restoreButton = new() { Text = "Restore" };
restoreButton.Click += (obj, args) =>
{
VcePlayer.Stop();
File.Copy(Path.Combine(_project.BaseDirectory, "original", "vce", Path.GetFileName(_vce.VoiceFile)), Path.Combine(_project.BaseDirectory, _vce.VoiceFile), true);
File.Copy(Path.Combine(_project.IterativeDirectory, "original", "vce", Path.GetFileName(_vce.VoiceFile)), Path.Combine(_project.IterativeDirectory, _vce.VoiceFile), true);
Content = GetEditorPanel();
};

return new TableLayout(
new TableRow(ControlGenerator.GetPlayerStackLayout(VcePlayer, _vce.Name, _vce.AdxType.ToString())),
new TableRow() // todo extract / replace buttons
new TableRow(new StackLayout
{
Orientation = Orientation.Horizontal,
Spacing = 3,
Items =
{
replaceButton,
extractButton,
restoreButton,
}
}),
new TableRow()
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/SerialLoops.Tests/CoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public void SetUp()
_project = new("Tester", "en", _config, _log);

string archivesDir = Path.Combine("original", "archives");
string bgmDir = Path.Combine("original", "bgm");
string vceDir = Path.Combine("original", "vce");
string bgmDir = Path.Combine("rom", "data", "bgm");
string vceDir = Path.Combine("rom", "data", "vce");
Directory.CreateDirectory(Path.Combine(_project.BaseDirectory, archivesDir));
Directory.CreateDirectory(Path.Combine(_project.IterativeDirectory, archivesDir));
Directory.CreateDirectory(Path.Combine(_project.BaseDirectory, bgmDir));
Expand Down