mirror of
https://github.com/claunia/cuetools.net.git
synced 2025-12-16 18:14:25 +00:00
CUETools split into class library and GUI project.
ArCueDotNet: console verification tool.
This commit is contained in:
968
CUEToolsLib/AudioReadWrite.cs
Normal file
968
CUEToolsLib/AudioReadWrite.cs
Normal file
@@ -0,0 +1,968 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using FLACDotNet;
|
||||
using WavPackDotNet;
|
||||
using APEDotNet;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
|
||||
namespace CUEToolsLib {
|
||||
public interface IAudioSource {
|
||||
uint Read(byte[] buff, uint sampleCount);
|
||||
ulong Length { get; }
|
||||
ulong Position { get; set; }
|
||||
NameValueCollection Tags { get; set; }
|
||||
ulong Remaining { get; }
|
||||
void Close();
|
||||
int BitsPerSample { get; }
|
||||
int ChannelCount { get; }
|
||||
int SampleRate { get; }
|
||||
}
|
||||
|
||||
public interface IAudioDest {
|
||||
void Write(byte[] buff, uint sampleCount);
|
||||
bool SetTags(NameValueCollection tags);
|
||||
void Close();
|
||||
long FinalSampleCount { set; }
|
||||
}
|
||||
|
||||
public static class AudioReadWrite {
|
||||
public static IAudioSource GetAudioSource(string path) {
|
||||
switch (Path.GetExtension(path).ToLower()) {
|
||||
case ".wav":
|
||||
return new WAVReader(path);
|
||||
case ".flac":
|
||||
return new FLACReader(path);
|
||||
case ".wv":
|
||||
return new WavPackReader(path);
|
||||
case ".ape":
|
||||
return new APEReader(path);
|
||||
default:
|
||||
throw new Exception("Unsupported audio type.");
|
||||
}
|
||||
}
|
||||
|
||||
public static IAudioDest GetAudioDest(string path, int bitsPerSample, int channelCount, int sampleRate, long finalSampleCount) {
|
||||
IAudioDest dest;
|
||||
switch (Path.GetExtension(path).ToLower()) {
|
||||
case ".wav":
|
||||
dest = new WAVWriter(path, bitsPerSample, channelCount, sampleRate); break;
|
||||
case ".flac":
|
||||
dest = new FLACWriter(path, bitsPerSample, channelCount, sampleRate); break;
|
||||
case ".wv":
|
||||
dest = new WavPackWriter(path, bitsPerSample, channelCount, sampleRate); break;
|
||||
case ".ape":
|
||||
dest = new APEWriter(path, bitsPerSample, channelCount, sampleRate); break;
|
||||
case ".dummy":
|
||||
dest = new DummyWriter(path, bitsPerSample, channelCount, sampleRate); break;
|
||||
default:
|
||||
throw new Exception("Unsupported audio type.");
|
||||
}
|
||||
dest.FinalSampleCount = finalSampleCount;
|
||||
return dest;
|
||||
}
|
||||
}
|
||||
|
||||
public class DummyWriter : IAudioDest {
|
||||
|
||||
public DummyWriter (string path, int bitsPerSample, int channelCount, int sampleRate) {
|
||||
}
|
||||
|
||||
public bool SetTags(NameValueCollection tags)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Close() {
|
||||
}
|
||||
|
||||
public long FinalSampleCount {
|
||||
set {
|
||||
}
|
||||
}
|
||||
|
||||
public void Write(byte[] buff, uint sampleCount) {
|
||||
}
|
||||
}
|
||||
|
||||
public class WAVReader : IAudioSource {
|
||||
FileStream _fs;
|
||||
BinaryReader _br;
|
||||
ulong _dataOffset, _dataLen;
|
||||
ulong _samplePos, _sampleLen;
|
||||
int _bitsPerSample, _channelCount, _sampleRate, _blockAlign;
|
||||
bool _largeFile;
|
||||
|
||||
public WAVReader(string path) {
|
||||
_fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
_br = new BinaryReader(_fs);
|
||||
|
||||
ParseHeaders();
|
||||
|
||||
_sampleLen = _dataLen / (uint)_blockAlign;
|
||||
Position = 0;
|
||||
}
|
||||
|
||||
public void Close() {
|
||||
_br.Close();
|
||||
|
||||
_br = null;
|
||||
_fs = null;
|
||||
}
|
||||
|
||||
private void ParseHeaders() {
|
||||
const long maxFileSize = 0x7FFFFFFEL;
|
||||
const uint fccRIFF = 0x46464952;
|
||||
const uint fccWAVE = 0x45564157;
|
||||
const uint fccFormat = 0x20746D66;
|
||||
const uint fccData = 0x61746164;
|
||||
|
||||
uint lenRIFF;
|
||||
long fileEnd;
|
||||
bool foundFormat, foundData;
|
||||
|
||||
if (_br.ReadUInt32() != fccRIFF) {
|
||||
throw new Exception("Not a valid RIFF file.");
|
||||
}
|
||||
|
||||
lenRIFF = _br.ReadUInt32();
|
||||
fileEnd = (long)lenRIFF + 8;
|
||||
|
||||
if (_br.ReadUInt32() != fccWAVE) {
|
||||
throw new Exception("Not a valid WAVE file.");
|
||||
}
|
||||
|
||||
_largeFile = false;
|
||||
foundFormat = false;
|
||||
foundData = false;
|
||||
|
||||
while (_fs.Position < fileEnd) {
|
||||
uint ckID, ckSize, ckSizePadded;
|
||||
long ckEnd;
|
||||
|
||||
ckID = _br.ReadUInt32();
|
||||
ckSize = _br.ReadUInt32();
|
||||
ckSizePadded = (ckSize + 1U) & ~1U;
|
||||
ckEnd = _fs.Position + (long)ckSizePadded;
|
||||
|
||||
if (ckID == fccFormat) {
|
||||
foundFormat = true;
|
||||
|
||||
if (_br.ReadUInt16() != 1) {
|
||||
throw new Exception("WAVE must be PCM format.");
|
||||
}
|
||||
_channelCount = _br.ReadInt16();
|
||||
_sampleRate = _br.ReadInt32();
|
||||
_br.ReadInt32();
|
||||
_blockAlign = _br.ReadInt16();
|
||||
_bitsPerSample = _br.ReadInt16();
|
||||
}
|
||||
else if (ckID == fccData) {
|
||||
foundData = true;
|
||||
|
||||
_dataOffset = (ulong) _fs.Position;
|
||||
if (_fs.Length <= maxFileSize) {
|
||||
_dataLen = ckSize;
|
||||
}
|
||||
else {
|
||||
_largeFile = true;
|
||||
_dataLen = ((ulong)_fs.Length) - _dataOffset;
|
||||
}
|
||||
}
|
||||
|
||||
if ((foundFormat & foundData) || _largeFile) {
|
||||
break;
|
||||
}
|
||||
|
||||
_fs.Seek(ckEnd, SeekOrigin.Begin);
|
||||
}
|
||||
|
||||
if ((foundFormat & foundData) == false) {
|
||||
throw new Exception("Format or data chunk not found.");
|
||||
}
|
||||
|
||||
if (_channelCount <= 0) {
|
||||
throw new Exception("Channel count is invalid.");
|
||||
}
|
||||
if (_sampleRate <= 0) {
|
||||
throw new Exception("Sample rate is invalid.");
|
||||
}
|
||||
if (_blockAlign != (_channelCount * ((_bitsPerSample + 7) / 8))) {
|
||||
throw new Exception("Block align is invalid.");
|
||||
}
|
||||
if ((_bitsPerSample <= 0) || (_bitsPerSample > 32)) {
|
||||
throw new Exception("Bits per sample is invalid.");
|
||||
}
|
||||
}
|
||||
|
||||
public ulong Position {
|
||||
get {
|
||||
return _samplePos;
|
||||
}
|
||||
set {
|
||||
ulong seekPos;
|
||||
|
||||
if (value > _sampleLen) {
|
||||
_samplePos = _sampleLen;
|
||||
}
|
||||
else {
|
||||
_samplePos = value;
|
||||
}
|
||||
|
||||
seekPos = _dataOffset + (_samplePos * (uint)_blockAlign);
|
||||
_fs.Seek((long) seekPos, SeekOrigin.Begin);
|
||||
}
|
||||
}
|
||||
|
||||
public ulong Length {
|
||||
get {
|
||||
return _sampleLen;
|
||||
}
|
||||
}
|
||||
|
||||
public ulong Remaining {
|
||||
get {
|
||||
return _sampleLen - _samplePos;
|
||||
}
|
||||
}
|
||||
|
||||
public int ChannelCount {
|
||||
get {
|
||||
return _channelCount;
|
||||
}
|
||||
}
|
||||
|
||||
public int SampleRate {
|
||||
get {
|
||||
return _sampleRate;
|
||||
}
|
||||
}
|
||||
|
||||
public int BitsPerSample {
|
||||
get {
|
||||
return _bitsPerSample;
|
||||
}
|
||||
}
|
||||
|
||||
public int BlockAlign {
|
||||
get {
|
||||
return _blockAlign;
|
||||
}
|
||||
}
|
||||
|
||||
public NameValueCollection Tags {
|
||||
get {
|
||||
return new NameValueCollection();
|
||||
}
|
||||
set {
|
||||
}
|
||||
}
|
||||
|
||||
public void GetTags(out List<string> names, out List<string> values)
|
||||
{
|
||||
names = new List<string>();
|
||||
values = new List<string>();
|
||||
}
|
||||
|
||||
public uint Read(byte[] buff, uint sampleCount) {
|
||||
if (sampleCount > Remaining)
|
||||
sampleCount = (uint) Remaining;
|
||||
|
||||
uint byteCount = sampleCount * (uint) _blockAlign;
|
||||
|
||||
if (sampleCount != 0) {
|
||||
if (_fs.Read(buff, 0, (int) byteCount) != byteCount) {
|
||||
throw new Exception("Incomplete file read.");
|
||||
}
|
||||
_samplePos += sampleCount;
|
||||
}
|
||||
|
||||
return sampleCount;
|
||||
}
|
||||
}
|
||||
|
||||
public class WAVWriter : IAudioDest {
|
||||
FileStream _fs;
|
||||
BinaryWriter _bw;
|
||||
int _bitsPerSample, _channelCount, _sampleRate, _blockAlign;
|
||||
long _sampleLen;
|
||||
|
||||
public WAVWriter(string path, int bitsPerSample, int channelCount, int sampleRate) {
|
||||
_bitsPerSample = bitsPerSample;
|
||||
_channelCount = channelCount;
|
||||
_sampleRate = sampleRate;
|
||||
_blockAlign = _channelCount * ((_bitsPerSample + 7) / 8);
|
||||
|
||||
_fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read);
|
||||
_bw = new BinaryWriter(_fs);
|
||||
|
||||
WriteHeaders();
|
||||
}
|
||||
|
||||
public bool SetTags(NameValueCollection tags)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
private void WriteHeaders() {
|
||||
const uint fccRIFF = 0x46464952;
|
||||
const uint fccWAVE = 0x45564157;
|
||||
const uint fccFormat = 0x20746D66;
|
||||
const uint fccData = 0x61746164;
|
||||
|
||||
_bw.Write(fccRIFF);
|
||||
_bw.Write((uint)0);
|
||||
_bw.Write(fccWAVE);
|
||||
|
||||
_bw.Write(fccFormat);
|
||||
_bw.Write((uint)16);
|
||||
_bw.Write((ushort)1);
|
||||
_bw.Write((ushort)_channelCount);
|
||||
_bw.Write((uint)_sampleRate);
|
||||
_bw.Write((uint)(_sampleRate * _blockAlign));
|
||||
_bw.Write((ushort)_blockAlign);
|
||||
_bw.Write((ushort)_bitsPerSample);
|
||||
|
||||
_bw.Write(fccData);
|
||||
_bw.Write((uint)0);
|
||||
}
|
||||
|
||||
public void Close() {
|
||||
const long maxFileSize = 0x7FFFFFFEL;
|
||||
long dataLen, dataLenPadded;
|
||||
|
||||
dataLen = _sampleLen * _blockAlign;
|
||||
|
||||
if ((dataLen & 1) == 1) {
|
||||
_bw.Write((byte)0);
|
||||
}
|
||||
|
||||
if ((dataLen + 44) > maxFileSize) {
|
||||
dataLen = ((maxFileSize - 44) / _blockAlign) * _blockAlign;
|
||||
}
|
||||
|
||||
dataLenPadded = ((dataLen & 1) == 1) ? (dataLen + 1) : dataLen;
|
||||
|
||||
_bw.Seek(4, SeekOrigin.Begin);
|
||||
_bw.Write((uint)(dataLenPadded + 36));
|
||||
|
||||
_bw.Seek(40, SeekOrigin.Begin);
|
||||
_bw.Write((uint)dataLen);
|
||||
|
||||
_bw.Close();
|
||||
|
||||
_bw = null;
|
||||
_fs = null;
|
||||
}
|
||||
|
||||
public long Position {
|
||||
get {
|
||||
return _sampleLen;
|
||||
}
|
||||
}
|
||||
|
||||
public long FinalSampleCount {
|
||||
set {
|
||||
}
|
||||
}
|
||||
|
||||
public void Write(byte[] buff, uint sampleCount) {
|
||||
if (sampleCount < 0) {
|
||||
sampleCount = 0;
|
||||
}
|
||||
|
||||
if (sampleCount != 0) {
|
||||
_fs.Write(buff, 0, (int) sampleCount * _blockAlign);
|
||||
_sampleLen += sampleCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class FLACReader : IAudioSource {
|
||||
FLACDotNet.FLACReader _flacReader;
|
||||
int[,] _sampleBuffer;
|
||||
uint _bufferOffset, _bufferLength;
|
||||
|
||||
public FLACReader(string path) {
|
||||
_flacReader = new FLACDotNet.FLACReader(path);
|
||||
_bufferOffset = 0;
|
||||
_bufferLength = 0;
|
||||
}
|
||||
|
||||
public void Close() {
|
||||
_flacReader.Close();
|
||||
}
|
||||
|
||||
public NameValueCollection Tags
|
||||
{
|
||||
get { return _flacReader.Tags; }
|
||||
set { _flacReader.Tags = value; }
|
||||
}
|
||||
|
||||
public bool UpdateTags()
|
||||
{
|
||||
return _flacReader.UpdateTags();
|
||||
}
|
||||
|
||||
public ulong Length {
|
||||
get {
|
||||
return (ulong) _flacReader.Length;
|
||||
}
|
||||
}
|
||||
|
||||
public ulong Remaining {
|
||||
get {
|
||||
return (ulong) _flacReader.Remaining + SamplesInBuffer;
|
||||
}
|
||||
}
|
||||
|
||||
public ulong Position {
|
||||
get {
|
||||
return (ulong) _flacReader.Position - SamplesInBuffer;
|
||||
}
|
||||
set {
|
||||
_flacReader.Position = (long) value;
|
||||
_bufferOffset = 0;
|
||||
_bufferLength = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private uint SamplesInBuffer {
|
||||
get {
|
||||
return (uint) (_bufferLength - _bufferOffset);
|
||||
}
|
||||
}
|
||||
|
||||
public int BitsPerSample {
|
||||
get {
|
||||
return _flacReader.BitsPerSample;
|
||||
}
|
||||
}
|
||||
|
||||
public int ChannelCount {
|
||||
get {
|
||||
return _flacReader.ChannelCount;
|
||||
}
|
||||
}
|
||||
|
||||
public int SampleRate {
|
||||
get {
|
||||
return _flacReader.SampleRate;
|
||||
}
|
||||
}
|
||||
|
||||
private unsafe void FLACSamplesToBytes_16(int[,] inSamples, uint inSampleOffset,
|
||||
byte[] outSamples, uint outByteOffset, uint sampleCount, int channelCount)
|
||||
{
|
||||
uint loopCount = sampleCount * (uint) channelCount;
|
||||
|
||||
if ((inSamples.GetLength(0) - inSampleOffset < sampleCount) ||
|
||||
(outSamples.Length - outByteOffset < loopCount * 2))
|
||||
{
|
||||
throw new IndexOutOfRangeException();
|
||||
}
|
||||
|
||||
fixed (int* pInSamplesFixed = &inSamples[inSampleOffset, 0]) {
|
||||
fixed (byte* pOutSamplesFixed = &outSamples[outByteOffset]) {
|
||||
int* pInSamples = pInSamplesFixed;
|
||||
short* pOutSamples = (short*)pOutSamplesFixed;
|
||||
|
||||
for (int i = 0; i < loopCount; i++) {
|
||||
*(pOutSamples++) = (short)*(pInSamples++);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public uint Read(byte[] buff, uint sampleCount) {
|
||||
if (_flacReader.BitsPerSample != 16) {
|
||||
throw new Exception("Reading is only supported for 16 bit sample depth.");
|
||||
}
|
||||
int chanCount = _flacReader.ChannelCount;
|
||||
uint copyCount;
|
||||
uint buffOffset = 0;
|
||||
uint samplesNeeded = sampleCount;
|
||||
|
||||
while (samplesNeeded != 0) {
|
||||
if (SamplesInBuffer == 0) {
|
||||
_bufferOffset = 0;
|
||||
_bufferLength = (uint) _flacReader.Read(out _sampleBuffer);
|
||||
}
|
||||
|
||||
copyCount = Math.Min(samplesNeeded, SamplesInBuffer);
|
||||
|
||||
FLACSamplesToBytes_16(_sampleBuffer, _bufferOffset, buff, buffOffset,
|
||||
copyCount, chanCount);
|
||||
|
||||
samplesNeeded -= copyCount;
|
||||
buffOffset += copyCount * (uint) chanCount * 2;
|
||||
_bufferOffset += copyCount;
|
||||
}
|
||||
|
||||
return sampleCount;
|
||||
}
|
||||
}
|
||||
|
||||
class FLACWriter : IAudioDest {
|
||||
FLACDotNet.FLACWriter _flacWriter;
|
||||
int[,] _sampleBuffer;
|
||||
int _bitsPerSample;
|
||||
int _channelCount;
|
||||
int _sampleRate;
|
||||
|
||||
public FLACWriter(string path, int bitsPerSample, int channelCount, int sampleRate) {
|
||||
if (bitsPerSample != 16) {
|
||||
throw new Exception("Bits per sample must be 16.");
|
||||
}
|
||||
_bitsPerSample = bitsPerSample;
|
||||
_channelCount = channelCount;
|
||||
_sampleRate = sampleRate;
|
||||
_flacWriter = new FLACDotNet.FLACWriter(path, bitsPerSample, channelCount, sampleRate);
|
||||
}
|
||||
|
||||
public long FinalSampleCount {
|
||||
get {
|
||||
return _flacWriter.FinalSampleCount;
|
||||
}
|
||||
set {
|
||||
_flacWriter.FinalSampleCount = value;
|
||||
}
|
||||
}
|
||||
|
||||
public int CompressionLevel {
|
||||
get {
|
||||
return _flacWriter.CompressionLevel;
|
||||
}
|
||||
set {
|
||||
_flacWriter.CompressionLevel = value;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Verify {
|
||||
get {
|
||||
return _flacWriter.Verify;
|
||||
}
|
||||
set {
|
||||
_flacWriter.Verify = value;
|
||||
}
|
||||
}
|
||||
|
||||
public bool SetTags(NameValueCollection tags)
|
||||
{
|
||||
_flacWriter.SetTags (tags);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Close() {
|
||||
_flacWriter.Close();
|
||||
}
|
||||
|
||||
private unsafe void BytesToFLACSamples_16(byte[] inSamples, int inByteOffset,
|
||||
int[,] outSamples, int outSampleOffset, uint sampleCount, int channelCount)
|
||||
{
|
||||
uint loopCount = sampleCount * (uint) channelCount;
|
||||
|
||||
if ((inSamples.Length - inByteOffset < loopCount * 2) ||
|
||||
(outSamples.GetLength(0) - outSampleOffset < sampleCount))
|
||||
{
|
||||
throw new IndexOutOfRangeException();
|
||||
}
|
||||
|
||||
fixed (byte* pInSamplesFixed = &inSamples[inByteOffset]) {
|
||||
fixed (int* pOutSamplesFixed = &outSamples[outSampleOffset, 0]) {
|
||||
short* pInSamples = (short*)pInSamplesFixed;
|
||||
int* pOutSamples = pOutSamplesFixed;
|
||||
|
||||
for (int i = 0; i < loopCount; i++) {
|
||||
*(pOutSamples++) = (int)*(pInSamples++);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Write(byte[] buff, uint sampleCount) {
|
||||
if ((_sampleBuffer == null) || (_sampleBuffer.GetLength(0) < sampleCount)) {
|
||||
_sampleBuffer = new int[sampleCount, _channelCount];
|
||||
}
|
||||
BytesToFLACSamples_16(buff, 0, _sampleBuffer, 0, sampleCount, _channelCount);
|
||||
_flacWriter.Write(_sampleBuffer, (int) sampleCount);
|
||||
}
|
||||
}
|
||||
|
||||
class APEReader : IAudioSource {
|
||||
APEDotNet.APEReader _apeReader;
|
||||
int[,] _sampleBuffer;
|
||||
uint _bufferOffset, _bufferLength;
|
||||
|
||||
public APEReader(string path) {
|
||||
_apeReader = new APEDotNet.APEReader(path);
|
||||
_bufferOffset = 0;
|
||||
_bufferLength = 0;
|
||||
}
|
||||
|
||||
public void Close() {
|
||||
_apeReader.Close();
|
||||
}
|
||||
|
||||
public ulong Length {
|
||||
get {
|
||||
return (ulong) _apeReader.Length;
|
||||
}
|
||||
}
|
||||
|
||||
public ulong Remaining {
|
||||
get {
|
||||
return (ulong) _apeReader.Remaining + SamplesInBuffer;
|
||||
}
|
||||
}
|
||||
|
||||
public ulong Position {
|
||||
get {
|
||||
return (ulong) _apeReader.Position - SamplesInBuffer;
|
||||
}
|
||||
set {
|
||||
_apeReader.Position = (long) value;
|
||||
_bufferOffset = 0;
|
||||
_bufferLength = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private uint SamplesInBuffer {
|
||||
get {
|
||||
return (uint) (_bufferLength - _bufferOffset);
|
||||
}
|
||||
}
|
||||
|
||||
public int BitsPerSample {
|
||||
get {
|
||||
return _apeReader.BitsPerSample;
|
||||
}
|
||||
}
|
||||
|
||||
public int ChannelCount {
|
||||
get {
|
||||
return _apeReader.ChannelCount;
|
||||
}
|
||||
}
|
||||
|
||||
public int SampleRate {
|
||||
get {
|
||||
return _apeReader.SampleRate;
|
||||
}
|
||||
}
|
||||
|
||||
public NameValueCollection Tags
|
||||
{
|
||||
get { return _apeReader.Tags; }
|
||||
set { _apeReader.Tags = value; }
|
||||
}
|
||||
|
||||
private unsafe void APESamplesToBytes_16(int[,] inSamples, uint inSampleOffset,
|
||||
byte[] outSamples, uint outByteOffset, uint sampleCount, int channelCount)
|
||||
{
|
||||
uint loopCount = sampleCount * (uint) channelCount;
|
||||
|
||||
if ((inSamples.GetLength(0) - inSampleOffset < sampleCount) ||
|
||||
(outSamples.Length - outByteOffset < loopCount * 2))
|
||||
{
|
||||
throw new IndexOutOfRangeException();
|
||||
}
|
||||
|
||||
fixed (int* pInSamplesFixed = &inSamples[inSampleOffset, 0]) {
|
||||
fixed (byte* pOutSamplesFixed = &outSamples[outByteOffset]) {
|
||||
int* pInSamples = pInSamplesFixed;
|
||||
short* pOutSamples = (short*)pOutSamplesFixed;
|
||||
|
||||
for (int i = 0; i < loopCount; i++) {
|
||||
*(pOutSamples++) = (short)*(pInSamples++);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public uint Read(byte[] buff, uint sampleCount) {
|
||||
if (_apeReader.BitsPerSample != 16) {
|
||||
throw new Exception("Reading is only supported for 16 bit sample depth.");
|
||||
}
|
||||
int chanCount = _apeReader.ChannelCount;
|
||||
uint samplesNeeded, copyCount, buffOffset;
|
||||
|
||||
buffOffset = 0;
|
||||
samplesNeeded = sampleCount;
|
||||
|
||||
while (samplesNeeded != 0) {
|
||||
if (SamplesInBuffer == 0) {
|
||||
_bufferOffset = 0;
|
||||
_bufferLength = (uint) _apeReader.Read(out _sampleBuffer);
|
||||
}
|
||||
|
||||
copyCount = Math.Min(samplesNeeded, SamplesInBuffer);
|
||||
|
||||
APESamplesToBytes_16(_sampleBuffer, _bufferOffset, buff, buffOffset,
|
||||
copyCount, chanCount);
|
||||
|
||||
samplesNeeded -= copyCount;
|
||||
buffOffset += copyCount * (uint) chanCount * 2;
|
||||
_bufferOffset += copyCount;
|
||||
}
|
||||
|
||||
return sampleCount;
|
||||
}
|
||||
}
|
||||
|
||||
class APEWriter : IAudioDest
|
||||
{
|
||||
APEDotNet.APEWriter _apeWriter;
|
||||
//int[,] _sampleBuffer;
|
||||
int _bitsPerSample;
|
||||
int _channelCount;
|
||||
int _sampleRate;
|
||||
|
||||
public APEWriter(string path, int bitsPerSample, int channelCount, int sampleRate)
|
||||
{
|
||||
if (bitsPerSample != 16)
|
||||
{
|
||||
throw new Exception("Bits per sample must be 16.");
|
||||
}
|
||||
_bitsPerSample = bitsPerSample;
|
||||
_channelCount = channelCount;
|
||||
_sampleRate = sampleRate;
|
||||
_apeWriter = new APEDotNet.APEWriter(path, bitsPerSample, channelCount, sampleRate);
|
||||
}
|
||||
|
||||
public long FinalSampleCount
|
||||
{
|
||||
get { return _apeWriter.FinalSampleCount; }
|
||||
set { _apeWriter.FinalSampleCount = (int) value; }
|
||||
}
|
||||
public int CompressionLevel
|
||||
{
|
||||
get { return _apeWriter.CompressionLevel; }
|
||||
set { _apeWriter.CompressionLevel = value; }
|
||||
}
|
||||
public bool SetTags(NameValueCollection tags)
|
||||
{
|
||||
_apeWriter.SetTags(tags);
|
||||
return true;
|
||||
}
|
||||
public void Close()
|
||||
{
|
||||
_apeWriter.Close();
|
||||
}
|
||||
private unsafe void BytesToAPESamples_16(byte[] inSamples, int inByteOffset,
|
||||
int[,] outSamples, int outSampleOffset, uint sampleCount, int channelCount)
|
||||
{
|
||||
uint loopCount = sampleCount * (uint)channelCount;
|
||||
|
||||
if ((inSamples.Length - inByteOffset < loopCount * 2) ||
|
||||
(outSamples.GetLength(0) - outSampleOffset < sampleCount))
|
||||
{
|
||||
throw new IndexOutOfRangeException();
|
||||
}
|
||||
|
||||
fixed (byte* pInSamplesFixed = &inSamples[inByteOffset])
|
||||
{
|
||||
fixed (int* pOutSamplesFixed = &outSamples[outSampleOffset, 0])
|
||||
{
|
||||
short* pInSamples = (short*)pInSamplesFixed;
|
||||
int* pOutSamples = pOutSamplesFixed;
|
||||
|
||||
for (int i = 0; i < loopCount; i++)
|
||||
{
|
||||
*(pOutSamples++) = (int)*(pInSamples++);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public void Write(byte[] buff, uint sampleCount)
|
||||
{
|
||||
//if ((_sampleBuffer == null) || (_sampleBuffer.GetLength(0) < sampleCount))
|
||||
//{
|
||||
// _sampleBuffer = new int[sampleCount, _channelCount];
|
||||
//}
|
||||
//BytesToAPESamples_16(buff, 0, _sampleBuffer, 0, sampleCount, _channelCount);
|
||||
//_apeWriter.Write(_sampleBuffer, (int)sampleCount);
|
||||
_apeWriter.Write (buff, sampleCount);
|
||||
}
|
||||
}
|
||||
|
||||
class WavPackReader : IAudioSource {
|
||||
WavPackDotNet.WavPackReader _wavPackReader;
|
||||
|
||||
public WavPackReader(string path) {
|
||||
_wavPackReader = new WavPackDotNet.WavPackReader(path);
|
||||
}
|
||||
|
||||
public void Close() {
|
||||
_wavPackReader.Close();
|
||||
}
|
||||
|
||||
public ulong Length {
|
||||
get {
|
||||
return (ulong) _wavPackReader.Length;
|
||||
}
|
||||
}
|
||||
|
||||
public ulong Remaining {
|
||||
get {
|
||||
return (ulong) _wavPackReader.Remaining;
|
||||
}
|
||||
}
|
||||
|
||||
public ulong Position {
|
||||
get {
|
||||
return (ulong) _wavPackReader.Position;
|
||||
}
|
||||
set {
|
||||
_wavPackReader.Position = (int) value;
|
||||
}
|
||||
}
|
||||
|
||||
public int BitsPerSample {
|
||||
get {
|
||||
return _wavPackReader.BitsPerSample;
|
||||
}
|
||||
}
|
||||
|
||||
public int ChannelCount {
|
||||
get {
|
||||
return _wavPackReader.ChannelCount;
|
||||
}
|
||||
}
|
||||
|
||||
public int SampleRate {
|
||||
get {
|
||||
return _wavPackReader.SampleRate;
|
||||
}
|
||||
}
|
||||
|
||||
public NameValueCollection Tags
|
||||
{
|
||||
get { return _wavPackReader.Tags; }
|
||||
set { _wavPackReader.Tags = value; }
|
||||
}
|
||||
|
||||
private unsafe void WavPackSamplesToBytes_16(int[,] inSamples, uint inSampleOffset,
|
||||
byte[] outSamples, uint outByteOffset, uint sampleCount, int channelCount)
|
||||
{
|
||||
uint loopCount = sampleCount * (uint) channelCount;
|
||||
|
||||
if ((inSamples.GetLength(0) - inSampleOffset < sampleCount) ||
|
||||
(outSamples.Length - outByteOffset < loopCount * 2))
|
||||
{
|
||||
throw new IndexOutOfRangeException();
|
||||
}
|
||||
|
||||
fixed (int* pInSamplesFixed = &inSamples[inSampleOffset, 0]) {
|
||||
fixed (byte* pOutSamplesFixed = &outSamples[outByteOffset]) {
|
||||
int* pInSamples = pInSamplesFixed;
|
||||
short* pOutSamples = (short*)pOutSamplesFixed;
|
||||
|
||||
for (int i = 0; i < loopCount; i++) {
|
||||
*(pOutSamples++) = (short)*(pInSamples++);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public uint Read(byte[] buff, uint sampleCount) {
|
||||
if (_wavPackReader.BitsPerSample != 16) {
|
||||
throw new Exception("Reading is only supported for 16 bit sample depth.");
|
||||
}
|
||||
int chanCount = _wavPackReader.ChannelCount;
|
||||
int[,] sampleBuffer;
|
||||
|
||||
sampleBuffer = new int[sampleCount * 2, chanCount];
|
||||
_wavPackReader.Read(sampleBuffer, (int) sampleCount);
|
||||
WavPackSamplesToBytes_16(sampleBuffer, 0, buff, 0, sampleCount, chanCount);
|
||||
|
||||
return sampleCount;
|
||||
}
|
||||
}
|
||||
|
||||
class WavPackWriter : IAudioDest {
|
||||
WavPackDotNet.WavPackWriter _wavPackWriter;
|
||||
int[,] _sampleBuffer;
|
||||
int _bitsPerSample;
|
||||
int _channelCount;
|
||||
int _sampleRate;
|
||||
|
||||
public WavPackWriter(string path, int bitsPerSample, int channelCount, int sampleRate) {
|
||||
if (bitsPerSample != 16) {
|
||||
throw new Exception("Bits per sample must be 16.");
|
||||
}
|
||||
_bitsPerSample = bitsPerSample;
|
||||
_channelCount = channelCount;
|
||||
_sampleRate = sampleRate;
|
||||
_wavPackWriter = new WavPackDotNet.WavPackWriter(path, bitsPerSample, channelCount, sampleRate);
|
||||
}
|
||||
|
||||
public bool SetTags(NameValueCollection tags)
|
||||
{
|
||||
_wavPackWriter.SetTags(tags);
|
||||
return true;
|
||||
}
|
||||
|
||||
public long FinalSampleCount {
|
||||
get {
|
||||
return _wavPackWriter.FinalSampleCount;
|
||||
}
|
||||
set {
|
||||
_wavPackWriter.FinalSampleCount = (int)value;
|
||||
}
|
||||
}
|
||||
|
||||
public int CompressionMode {
|
||||
get {
|
||||
return _wavPackWriter.CompressionMode;
|
||||
}
|
||||
set {
|
||||
_wavPackWriter.CompressionMode = value;
|
||||
}
|
||||
}
|
||||
|
||||
public int ExtraMode {
|
||||
get {
|
||||
return _wavPackWriter.ExtraMode;
|
||||
}
|
||||
set {
|
||||
_wavPackWriter.ExtraMode = value;
|
||||
}
|
||||
}
|
||||
|
||||
public void Close() {
|
||||
_wavPackWriter.Close();
|
||||
}
|
||||
|
||||
private unsafe void BytesToWavPackSamples_16(byte[] inSamples, int inByteOffset,
|
||||
int[,] outSamples, int outSampleOffset, uint sampleCount, int channelCount)
|
||||
{
|
||||
uint loopCount = sampleCount * (uint) channelCount;
|
||||
|
||||
if ((inSamples.Length - inByteOffset < loopCount * 2) ||
|
||||
(outSamples.GetLength(0) - outSampleOffset < sampleCount))
|
||||
{
|
||||
throw new IndexOutOfRangeException();
|
||||
}
|
||||
|
||||
fixed (byte* pInSamplesFixed = &inSamples[inByteOffset]) {
|
||||
fixed (int* pOutSamplesFixed = &outSamples[outSampleOffset, 0]) {
|
||||
short* pInSamples = (short*)pInSamplesFixed;
|
||||
int* pOutSamples = pOutSamplesFixed;
|
||||
|
||||
for (int i = 0; i < loopCount; i++) {
|
||||
*(pOutSamples++) = (int)*(pInSamples++);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Write(byte[] buff, uint sampleCount) {
|
||||
if ((_sampleBuffer == null) || (_sampleBuffer.GetLength(0) < sampleCount)) {
|
||||
_sampleBuffer = new int[sampleCount, _channelCount];
|
||||
}
|
||||
BytesToWavPackSamples_16(buff, 0, _sampleBuffer, 0, sampleCount, _channelCount);
|
||||
_wavPackWriter.Write(_sampleBuffer, (int) sampleCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
113
CUEToolsLib/CUEToolsLib.csproj
Normal file
113
CUEToolsLib/CUEToolsLib.csproj
Normal file
@@ -0,0 +1,113 @@
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProductVersion>8.0.50727</ProductVersion>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{4911BD82-49EF-4858-8B51-5394F86739A4}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>CUEToolsLib</RootNamespace>
|
||||
<AssemblyName>CUEToolsLib</AssemblyName>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>..\bin\win32\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<OutputPath>..\bin\win32\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<DebugType>full</DebugType>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<CodeAnalysisRuleAssemblies>C:\Program Files (x86)\Microsoft Visual Studio 8\Team Tools\Static Analysis Tools\FxCop\\rules</CodeAnalysisRuleAssemblies>
|
||||
<CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression>
|
||||
<CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
|
||||
<OutputPath>..\bin\win32\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<Optimize>true</Optimize>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<CodeAnalysisRuleAssemblies>C:\Program Files (x86)\Microsoft Visual Studio 8\Team Tools\Static Analysis Tools\FxCop\\rules</CodeAnalysisRuleAssemblies>
|
||||
<CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression>
|
||||
<CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<OutputPath>..\bin\x64\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<DebugType>full</DebugType>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<CodeAnalysisRuleAssemblies>C:\Program Files (x86)\Microsoft Visual Studio 8\Team Tools\Static Analysis Tools\FxCop\\rules</CodeAnalysisRuleAssemblies>
|
||||
<CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression>
|
||||
<CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
|
||||
<OutputPath>bin\x64\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<Optimize>true</Optimize>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<CodeAnalysisRuleAssemblies>C:\Program Files (x86)\Microsoft Visual Studio 8\Team Tools\Static Analysis Tools\FxCop\\rules</CodeAnalysisRuleAssemblies>
|
||||
<CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression>
|
||||
<CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AudioReadWrite.cs" />
|
||||
<Compile Include="Main.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Settings.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\APEDotNet\APEDotNet.vcproj">
|
||||
<Project>{9AE965C4-301E-4C01-B90F-297AF341ACC6}</Project>
|
||||
<Name>APEDotNet</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\FLACDotNet\FLACDotNet.vcproj">
|
||||
<Project>{E70FA90A-7012-4A52-86B5-362B699D1540}</Project>
|
||||
<Name>FLACDotNet</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\WavPackDotNet\WavPackDotNet.vcproj">
|
||||
<Project>{CC2E74B6-534A-43D8-9F16-AC03FE955000}</Project>
|
||||
<Name>WavPackDotNet</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
2395
CUEToolsLib/Main.cs
Normal file
2395
CUEToolsLib/Main.cs
Normal file
File diff suppressed because it is too large
Load Diff
35
CUEToolsLib/Properties/AssemblyInfo.cs
Normal file
35
CUEToolsLib/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("CUEToolsLib")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("CUEToolsLib")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2006-2008 Moitah, Gregory S. Chudov")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("d3afb938-f35e-4e5a-b650-7d7a4e885aff")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Revision and Build Numbers
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
93
CUEToolsLib/Settings.cs
Normal file
93
CUEToolsLib/Settings.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
// ****************************************************************************
|
||||
//
|
||||
// CUE Tools
|
||||
// Copyright (C) 2006-2007 Moitah (moitah@yahoo.com)
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
//
|
||||
// ****************************************************************************
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace CUEToolsLib
|
||||
{
|
||||
static class SettingsShared {
|
||||
public static string GetMyAppDataDir(string appName) {
|
||||
string appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
|
||||
string myAppDataDir = Path.Combine(appDataDir, appName);
|
||||
|
||||
if (Directory.Exists(myAppDataDir) == false) {
|
||||
Directory.CreateDirectory(myAppDataDir);
|
||||
}
|
||||
|
||||
return myAppDataDir;
|
||||
}
|
||||
}
|
||||
|
||||
public class SettingsReader {
|
||||
Dictionary<string, string> _settings;
|
||||
|
||||
public SettingsReader(string appName, string fileName) {
|
||||
_settings = new Dictionary<string, string>();
|
||||
|
||||
string path = Path.Combine(SettingsShared.GetMyAppDataDir(appName), fileName);
|
||||
if (!File.Exists(path)) {
|
||||
return;
|
||||
}
|
||||
|
||||
using (StreamReader sr = new StreamReader(path, Encoding.UTF8)) {
|
||||
string line, name, val;
|
||||
int pos;
|
||||
|
||||
while ((line = sr.ReadLine()) != null) {
|
||||
pos = line.IndexOf('=');
|
||||
if (pos != -1) {
|
||||
name = line.Substring(0, pos);
|
||||
val = line.Substring(pos + 1);
|
||||
|
||||
if (!_settings.ContainsKey(name)) {
|
||||
_settings.Add(name, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string Load(string name) {
|
||||
return _settings.ContainsKey(name) ? _settings[name] : null;
|
||||
}
|
||||
}
|
||||
|
||||
public class SettingsWriter {
|
||||
StreamWriter _sw;
|
||||
|
||||
public SettingsWriter(string appName, string fileName) {
|
||||
string path = Path.Combine(SettingsShared.GetMyAppDataDir(appName), fileName);
|
||||
|
||||
_sw = new StreamWriter(path, false, Encoding.UTF8);
|
||||
}
|
||||
|
||||
public void Save(string name, string value) {
|
||||
_sw.WriteLine(name + "=" + value);
|
||||
}
|
||||
|
||||
public void Close() {
|
||||
_sw.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user