Skip to content

Commit

Permalink
[hcaenc] Added quality support.
Browse files Browse the repository at this point in the history
  • Loading branch information
hozuki committed Feb 26, 2018
1 parent 402c7f4 commit 5974a6c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
5 changes: 5 additions & 0 deletions Apps/Hcaenc/DereTore.Apps.Hcaenc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,19 @@
<LangVersion>6</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="CommandLine, Version=2.2.1.0, Culture=neutral, PublicKeyToken=de6f01bd326f8c32, processorArchitecture=MSIL">
<HintPath>..\..\packages\CommandLineParser.2.2.1\lib\net40\CommandLine.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="Options.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
16 changes: 16 additions & 0 deletions Apps/Hcaenc/Options.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using CommandLine;

namespace DereTore.Apps.Hcaenc {
public sealed class Options {

[Option('q', "quality", HelpText = "Audio quality (1 to 5)", Default = 1, Required = false)]
public int Quaility { get; set; } = 1;

[Value(0, HelpText = "Input file name", Required = true)]
public string InputFileName { get; set; }

[Value(1, HelpText = "Output HCA file name", Default = null, Required = false)]
public string OutputFileName { get; set; }

}
}
28 changes: 20 additions & 8 deletions Apps/Hcaenc/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using CommandLine;

namespace DereTore.Apps.Hcaenc {
internal static class Program {
Expand All @@ -11,29 +12,40 @@ private static int Main(string[] args) {
return -2;
}

if (args.Length < 1) {
var parser = new Parser(settings => { settings.IgnoreUnknownArguments = true; });

var parserResult = parser.ParseArguments<Options>(args);

if (parserResult.Tag == ParserResultType.NotParsed) {
Console.WriteLine(HelpMessage);
return -1;
}

var inputFile = args[0];
var options = ((Parsed<Options>)parserResult).Value;

inputFile = Path.GetFullPath(inputFile);
var inputFile = Path.GetFullPath(options.InputFileName);

string outputFile;

if (args.Length >= 2) {
outputFile = args[1];
if (!string.IsNullOrEmpty(options.OutputFileName)) {
outputFile = options.OutputFileName;
} else {
var inputFileInfo = new FileInfo(inputFile);
var inputFileDir = inputFileInfo.DirectoryName;
outputFile = Path.Combine(inputFileDir, inputFileInfo.Name.Substring(0, inputFileInfo.Name.Length - inputFileInfo.Extension.Length) + ".hca");
}

Console.WriteLine("Encoding {0} to {1} ...", inputFile, outputFile);
var quality = options.Quaility;

if (quality < 1 || quality > 5) {
Console.WriteLine("Warning: Quality should be 1 to 5. Using q=1.");
quality = 1;
}

Console.WriteLine("Encoding {0} to {1} (q={2}) ...", inputFile, outputFile, quality);

// Quality = 3 (~128 Kbps) for MLTD, 1 (~256 Kbps) for CGSS
int quality = 3, cutoff = 0;
int cutoff = 0;
ulong key = 0;

return hcaencEncodeToFile(inputFile, outputFile, quality, cutoff, key);
Expand All @@ -42,7 +54,7 @@ private static int Main(string[] args) {
[DllImport("hcaenc_lite", CallingConvention = CallingConvention.StdCall)]
private static extern int hcaencEncodeToFile([MarshalAs(UnmanagedType.LPStr)] string lpstrInputFile, [MarshalAs(UnmanagedType.LPStr)] string lpstrOutputFile, int nQuality, int nCutoff, ulong ullKey);

private static readonly string HelpMessage = "Usage: hcaenc.exe <input WAVE> [<output HCA>]";
private static readonly string HelpMessage = "Usage: hcaenc.exe <input WAVE> [<output HCA> -q <quality>]";
private static readonly string UnsupportedBuildMessage = "hcaenc only has 32-bit version due to the limits of hcaenc_lite.dll.";

}
Expand Down
4 changes: 4 additions & 0 deletions Apps/Hcaenc/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CommandLineParser" version="2.2.1" targetFramework="net40" />
</packages>

0 comments on commit 5974a6c

Please sign in to comment.