78 lines
2.5 KiB
C#
78 lines
2.5 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using JD.LogService;
|
|
using TeaBox.Macros;
|
|
|
|
namespace JDSongConverter.Tasks
|
|
{
|
|
/// <summary>
|
|
/// Cleans a wave file from Lyn by removing dirty header if necessary
|
|
/// </summary>
|
|
class CleanWaveHeaderTask : Task
|
|
{
|
|
public string InputFile { get; set; }
|
|
public string OutputFile { get; set; }
|
|
|
|
private const int DirtyHeaderLength = 20;
|
|
|
|
public void Init(string inputFile, string outputFile)
|
|
{
|
|
InputFile = inputFile;
|
|
OutputFile = outputFile;
|
|
}
|
|
|
|
public override string Name
|
|
{
|
|
get { return "Clean wave files with dirty headers"; }
|
|
set { }
|
|
}
|
|
|
|
public override string ProgressText
|
|
{
|
|
get { return String.Format("Cleaning wave file '{0}'", InputFile); }
|
|
}
|
|
|
|
public override TaskResult Process(IExecutionContext executionContext)
|
|
{
|
|
if (string.IsNullOrEmpty(InputFile) || !File.Exists(InputFile))
|
|
return TaskResult.ParametersError;
|
|
|
|
try
|
|
{
|
|
byte[] wave;
|
|
|
|
using (var input = new FileStream(InputFile, FileMode.Open, FileAccess.Read))
|
|
{
|
|
var riffHeader = new byte[4];
|
|
input.Read(riffHeader, 0, 4);
|
|
if (riffHeader.SequenceEqual(new[] { (byte)'R', (byte)'I', (byte)'F', (byte)'F' }))
|
|
{
|
|
executionContext.LogMessage("No need to clean wave header.");
|
|
input.Seek(0, SeekOrigin.Begin);
|
|
wave = new byte[input.Length];
|
|
}
|
|
else
|
|
{
|
|
input.Seek(DirtyHeaderLength, SeekOrigin.Begin);
|
|
wave = new byte[input.Length - DirtyHeaderLength];
|
|
}
|
|
input.Read(wave, 0, wave.Length);
|
|
}
|
|
if (File.Exists(OutputFile))
|
|
{
|
|
File.SetAttributes(OutputFile, FileAttributes.Normal);
|
|
}
|
|
using (FileStream output = System.IO.File.Open(OutputFile, FileMode.Create))
|
|
{
|
|
output.Write(wave, 0, wave.Length);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return TaskResult.Error;
|
|
}
|
|
return TaskResult.Ok;
|
|
}
|
|
}
|
|
}
|