2008-10-13 19:25:11 +00:00
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using FLACDotNet;
|
|
|
|
|
using WavPackDotNet;
|
|
|
|
|
using APEDotNet;
|
2008-11-04 01:23:30 +00:00
|
|
|
using ALACDotNet;
|
|
|
|
|
using AudioCodecsDotNet;
|
2008-10-13 19:25:11 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
|
2008-10-17 20:23:33 +00:00
|
|
|
namespace CUEToolsLib {
|
2008-10-13 19:25:11 +00:00
|
|
|
public static class AudioReadWrite {
|
|
|
|
|
public static IAudioSource GetAudioSource(string path) {
|
|
|
|
|
switch (Path.GetExtension(path).ToLower()) {
|
|
|
|
|
case ".wav":
|
2008-11-10 09:31:14 +00:00
|
|
|
return new WAVReader(path, null);
|
2008-10-19 17:58:48 +00:00
|
|
|
#if !MONO
|
2008-10-13 19:25:11 +00:00
|
|
|
case ".flac":
|
2008-11-10 09:31:14 +00:00
|
|
|
return new FLACReader(path, null);
|
2008-10-13 19:25:11 +00:00
|
|
|
case ".wv":
|
2008-11-10 12:39:07 +00:00
|
|
|
return new WavPackReader(path, null, null);
|
2008-10-13 19:25:11 +00:00
|
|
|
case ".ape":
|
|
|
|
|
return new APEReader(path);
|
2008-11-04 01:23:30 +00:00
|
|
|
case ".m4a":
|
2008-11-10 10:31:35 +00:00
|
|
|
return new ALACReader(path, null);
|
2008-10-19 17:58:48 +00:00
|
|
|
#endif
|
2008-10-13 19:25:11 +00:00
|
|
|
default:
|
|
|
|
|
throw new Exception("Unsupported audio type.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-11-10 08:42:42 +00:00
|
|
|
public static IAudioSource GetAudioSource(string path, Stream IO)
|
|
|
|
|
{
|
|
|
|
|
switch (Path.GetExtension(path).ToLower())
|
|
|
|
|
{
|
2008-11-10 09:31:14 +00:00
|
|
|
case ".wav":
|
|
|
|
|
return new WAVReader(path, IO);
|
2008-11-10 08:42:42 +00:00
|
|
|
#if !MONO
|
|
|
|
|
case ".flac":
|
|
|
|
|
return new FLACReader(path, IO);
|
2008-11-10 12:39:07 +00:00
|
|
|
case ".wv":
|
|
|
|
|
return new WavPackReader(path, IO, null);
|
2008-11-10 10:31:35 +00:00
|
|
|
case ".m4a":
|
|
|
|
|
return new ALACReader(path, IO);
|
2008-11-10 08:42:42 +00:00
|
|
|
#endif
|
|
|
|
|
default:
|
|
|
|
|
throw new Exception("Unsupported audio type in archive.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-10-13 19:25:11 +00:00
|
|
|
|
|
|
|
|
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;
|
2008-10-19 17:58:48 +00:00
|
|
|
#if !MONO
|
2008-10-13 19:25:11 +00:00
|
|
|
case ".flac":
|
|
|
|
|
dest = new FLACWriter(path, bitsPerSample, channelCount, sampleRate); break;
|
|
|
|
|
case ".wv":
|
|
|
|
|
dest = new WavPackWriter(path, bitsPerSample, channelCount, sampleRate); break;
|
2008-10-17 18:21:59 +00:00
|
|
|
case ".ape":
|
|
|
|
|
dest = new APEWriter(path, bitsPerSample, channelCount, sampleRate); break;
|
2008-10-13 19:25:11 +00:00
|
|
|
case ".dummy":
|
|
|
|
|
dest = new DummyWriter(path, bitsPerSample, channelCount, sampleRate); break;
|
2008-10-19 17:58:48 +00:00
|
|
|
#endif
|
2008-10-13 19:25:11 +00:00
|
|
|
default:
|
|
|
|
|
throw new Exception("Unsupported audio type.");
|
|
|
|
|
}
|
|
|
|
|
dest.FinalSampleCount = finalSampleCount;
|
|
|
|
|
return dest;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-19 17:58:48 +00:00
|
|
|
#if !MONO
|
2008-10-13 19:25:11 +00:00
|
|
|
class FLACReader : IAudioSource {
|
|
|
|
|
FLACDotNet.FLACReader _flacReader;
|
|
|
|
|
int[,] _sampleBuffer;
|
|
|
|
|
uint _bufferOffset, _bufferLength;
|
|
|
|
|
|
2008-11-10 08:42:42 +00:00
|
|
|
public FLACReader(string path, Stream IO)
|
|
|
|
|
{
|
|
|
|
|
_flacReader = new FLACDotNet.FLACReader(path, IO);
|
2008-10-13 19:25:11 +00:00
|
|
|
_bufferOffset = 0;
|
|
|
|
|
_bufferLength = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Close() {
|
|
|
|
|
_flacReader.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NameValueCollection Tags
|
|
|
|
|
{
|
|
|
|
|
get { return _flacReader.Tags; }
|
|
|
|
|
set { _flacReader.Tags = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-25 19:14:44 +00:00
|
|
|
public bool UpdateTags(bool preserveTime)
|
2008-10-13 19:25:11 +00:00
|
|
|
{
|
2008-10-25 19:14:44 +00:00
|
|
|
return _flacReader.UpdateTags(preserveTime);
|
2008-10-13 19:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-11-07 22:43:26 +00:00
|
|
|
public uint Read(int [,] buff, uint sampleCount) {
|
2008-10-13 19:25:11 +00:00
|
|
|
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);
|
2008-11-07 22:43:26 +00:00
|
|
|
Array.Copy(_sampleBuffer, _bufferOffset * chanCount, buff, buffOffset * chanCount, copyCount * chanCount);
|
2008-10-13 19:25:11 +00:00
|
|
|
|
|
|
|
|
samplesNeeded -= copyCount;
|
2008-11-07 22:43:26 +00:00
|
|
|
buffOffset += copyCount;
|
2008-10-13 19:25:11 +00:00
|
|
|
_bufferOffset += copyCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sampleCount;
|
|
|
|
|
}
|
2008-10-25 18:42:28 +00:00
|
|
|
|
|
|
|
|
public string Path { get { return _flacReader.Path; } }
|
2008-10-13 19:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class FLACWriter : IAudioDest {
|
|
|
|
|
FLACDotNet.FLACWriter _flacWriter;
|
|
|
|
|
int _bitsPerSample;
|
|
|
|
|
int _channelCount;
|
|
|
|
|
int _sampleRate;
|
|
|
|
|
|
|
|
|
|
public FLACWriter(string path, int bitsPerSample, int channelCount, int sampleRate) {
|
2008-11-04 01:23:30 +00:00
|
|
|
if (bitsPerSample != 16 && bitsPerSample != 24) {
|
|
|
|
|
throw new Exception("Bits per sample must be 16 or 24.");
|
2008-10-13 19:25:11 +00:00
|
|
|
}
|
|
|
|
|
_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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-15 02:25:21 +00:00
|
|
|
public bool SetTags(NameValueCollection tags)
|
2008-10-13 19:25:11 +00:00
|
|
|
{
|
|
|
|
|
_flacWriter.SetTags (tags);
|
2008-10-15 02:25:21 +00:00
|
|
|
return true;
|
2008-10-13 19:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Close() {
|
|
|
|
|
_flacWriter.Close();
|
|
|
|
|
}
|
|
|
|
|
|
2008-11-04 01:23:30 +00:00
|
|
|
public void Write(int[,] buff, uint sampleCount)
|
2008-10-13 19:25:11 +00:00
|
|
|
{
|
2008-11-04 01:23:30 +00:00
|
|
|
_flacWriter.Write(buff, (int) sampleCount);
|
2008-10-13 19:25:11 +00:00
|
|
|
}
|
2008-10-25 18:42:28 +00:00
|
|
|
|
|
|
|
|
public string Path { get { return _flacWriter.Path; } }
|
2008-10-13 19:25:11 +00:00
|
|
|
}
|
2008-10-19 17:58:48 +00:00
|
|
|
#endif
|
2008-10-13 19:25:11 +00:00
|
|
|
|
2008-10-19 17:58:48 +00:00
|
|
|
#if !MONO
|
2008-10-13 19:25:11 +00:00
|
|
|
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; }
|
|
|
|
|
}
|
|
|
|
|
|
2008-11-07 22:43:26 +00:00
|
|
|
private unsafe void APESamplesToFLACSamples(int[,] inSamples, uint inSampleOffset,
|
|
|
|
|
int[,] outSamples, uint outSampleOffset, uint sampleCount, int channelCount)
|
2008-10-13 19:25:11 +00:00
|
|
|
{
|
|
|
|
|
uint loopCount = sampleCount * (uint) channelCount;
|
|
|
|
|
|
|
|
|
|
if ((inSamples.GetLength(0) - inSampleOffset < sampleCount) ||
|
2008-11-07 22:43:26 +00:00
|
|
|
(outSamples.GetLength(0) - outSampleOffset < sampleCount))
|
2008-10-13 19:25:11 +00:00
|
|
|
{
|
|
|
|
|
throw new IndexOutOfRangeException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fixed (int* pInSamplesFixed = &inSamples[inSampleOffset, 0]) {
|
2008-11-07 22:43:26 +00:00
|
|
|
fixed (int * pOutSamplesFixed = &outSamples[outSampleOffset, 0]) {
|
2008-10-13 19:25:11 +00:00
|
|
|
int* pInSamples = pInSamplesFixed;
|
2008-11-07 22:43:26 +00:00
|
|
|
int* pOutSamples = pOutSamplesFixed;
|
2008-10-13 19:25:11 +00:00
|
|
|
|
|
|
|
|
for (int i = 0; i < loopCount; i++) {
|
2008-11-07 22:43:26 +00:00
|
|
|
*(pOutSamples++) = *(pInSamples++);
|
2008-10-13 19:25:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-11-07 22:43:26 +00:00
|
|
|
public uint Read(int[,] buff, uint sampleCount) {
|
2008-10-13 19:25:11 +00:00
|
|
|
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);
|
|
|
|
|
|
2008-11-09 14:01:15 +00:00
|
|
|
|
|
|
|
|
Array.Copy(_sampleBuffer, _bufferOffset * chanCount, buff, buffOffset * chanCount, copyCount * chanCount);
|
|
|
|
|
//APESamplesToFLACSamples(_sampleBuffer, _bufferOffset, buff, buffOffset,
|
|
|
|
|
// copyCount, chanCount);
|
2008-10-13 19:25:11 +00:00
|
|
|
|
|
|
|
|
samplesNeeded -= copyCount;
|
2008-11-07 22:43:26 +00:00
|
|
|
buffOffset += copyCount;
|
2008-10-13 19:25:11 +00:00
|
|
|
_bufferOffset += copyCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sampleCount;
|
|
|
|
|
}
|
2008-10-25 18:42:28 +00:00
|
|
|
|
|
|
|
|
public string Path { get { return _apeReader.Path; } }
|
2008-10-13 19:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
2008-10-17 18:21:59 +00:00
|
|
|
class APEWriter : IAudioDest
|
|
|
|
|
{
|
|
|
|
|
APEDotNet.APEWriter _apeWriter;
|
2008-11-04 01:23:30 +00:00
|
|
|
byte[] _sampleBuffer;
|
2008-10-17 18:21:59 +00:00
|
|
|
int _bitsPerSample;
|
|
|
|
|
int _channelCount;
|
|
|
|
|
int _sampleRate;
|
2008-11-04 01:23:30 +00:00
|
|
|
int _blockAlign;
|
2008-10-17 18:21:59 +00:00
|
|
|
|
|
|
|
|
public APEWriter(string path, int bitsPerSample, int channelCount, int sampleRate)
|
|
|
|
|
{
|
2008-11-04 01:23:30 +00:00
|
|
|
if (bitsPerSample != 16 && bitsPerSample != 24)
|
2008-10-17 18:21:59 +00:00
|
|
|
{
|
2008-11-04 01:23:30 +00:00
|
|
|
throw new Exception("Bits per sample must be 16 or 24.");
|
2008-10-17 18:21:59 +00:00
|
|
|
}
|
|
|
|
|
_bitsPerSample = bitsPerSample;
|
|
|
|
|
_channelCount = channelCount;
|
|
|
|
|
_sampleRate = sampleRate;
|
2008-11-04 01:23:30 +00:00
|
|
|
_blockAlign = _channelCount * ((_bitsPerSample + 7) / 8);
|
2008-10-17 18:21:59 +00:00
|
|
|
_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();
|
|
|
|
|
}
|
2008-11-04 01:23:30 +00:00
|
|
|
public void Write(int [,] buff, uint sampleCount)
|
2008-10-17 18:21:59 +00:00
|
|
|
{
|
2008-11-04 01:23:30 +00:00
|
|
|
if (_sampleBuffer == null || _sampleBuffer.Length < sampleCount * _blockAlign)
|
|
|
|
|
_sampleBuffer = new byte[sampleCount * _blockAlign];
|
|
|
|
|
AudioCodecsDotNet.AudioCodecsDotNet.FLACSamplesToBytes (buff, 0, _sampleBuffer, 0, sampleCount, _channelCount, _bitsPerSample);
|
|
|
|
|
_apeWriter.Write(_sampleBuffer, sampleCount);
|
2008-10-17 18:21:59 +00:00
|
|
|
}
|
2008-10-25 18:42:28 +00:00
|
|
|
public string Path { get { return _apeWriter.Path; } }
|
2008-10-17 18:21:59 +00:00
|
|
|
}
|
2008-10-19 17:58:48 +00:00
|
|
|
#endif
|
2008-10-13 19:25:11 +00:00
|
|
|
|
2008-10-19 17:58:48 +00:00
|
|
|
#if !MONO
|
2008-10-13 19:25:11 +00:00
|
|
|
class WavPackReader : IAudioSource {
|
|
|
|
|
WavPackDotNet.WavPackReader _wavPackReader;
|
|
|
|
|
|
2008-11-10 12:39:07 +00:00
|
|
|
public WavPackReader(string path, Stream IO, Stream IO_WVC) {
|
|
|
|
|
_wavPackReader = new WavPackDotNet.WavPackReader(path, IO, IO_WVC);
|
2008-10-13 19:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
}
|
|
|
|
|
|
2008-11-07 22:43:26 +00:00
|
|
|
public uint Read(int[,] buff, uint sampleCount) {
|
2008-10-13 19:25:11 +00:00
|
|
|
if (_wavPackReader.BitsPerSample != 16) {
|
|
|
|
|
throw new Exception("Reading is only supported for 16 bit sample depth.");
|
|
|
|
|
}
|
2008-11-07 22:43:26 +00:00
|
|
|
_wavPackReader.Read(buff, (int) sampleCount);
|
2008-10-13 19:25:11 +00:00
|
|
|
return sampleCount;
|
|
|
|
|
}
|
2008-10-25 18:42:28 +00:00
|
|
|
|
|
|
|
|
public string Path { get { return _wavPackReader.Path; } }
|
2008-10-13 19:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class WavPackWriter : IAudioDest {
|
|
|
|
|
WavPackDotNet.WavPackWriter _wavPackWriter;
|
|
|
|
|
int _bitsPerSample;
|
|
|
|
|
int _channelCount;
|
|
|
|
|
int _sampleRate;
|
2008-11-04 01:23:30 +00:00
|
|
|
int _blockAlign;
|
|
|
|
|
byte[] _sampleBuffer;
|
2008-10-13 19:25:11 +00:00
|
|
|
|
|
|
|
|
public WavPackWriter(string path, int bitsPerSample, int channelCount, int sampleRate) {
|
2008-11-04 01:23:30 +00:00
|
|
|
if (bitsPerSample != 16 && bitsPerSample != 24)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Bits per sample must be 16 or 24.");
|
2008-10-13 19:25:11 +00:00
|
|
|
}
|
|
|
|
|
_bitsPerSample = bitsPerSample;
|
|
|
|
|
_channelCount = channelCount;
|
|
|
|
|
_sampleRate = sampleRate;
|
2008-11-04 01:23:30 +00:00
|
|
|
_blockAlign = _channelCount * ((_bitsPerSample + 7) / 8);
|
2008-10-13 19:25:11 +00:00
|
|
|
_wavPackWriter = new WavPackDotNet.WavPackWriter(path, bitsPerSample, channelCount, sampleRate);
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-15 02:25:21 +00:00
|
|
|
public bool SetTags(NameValueCollection tags)
|
|
|
|
|
{
|
|
|
|
|
_wavPackWriter.SetTags(tags);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-13 19:25:11 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-11-04 01:23:30 +00:00
|
|
|
public bool MD5Sum
|
2008-10-13 19:25:11 +00:00
|
|
|
{
|
2008-11-04 01:23:30 +00:00
|
|
|
get
|
2008-10-13 19:25:11 +00:00
|
|
|
{
|
2008-11-04 01:23:30 +00:00
|
|
|
return _wavPackWriter.MD5Sum;
|
2008-10-13 19:25:11 +00:00
|
|
|
}
|
2008-11-04 01:23:30 +00:00
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_wavPackWriter.MD5Sum = value;
|
2008-10-13 19:25:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-11-04 01:23:30 +00:00
|
|
|
public void Close() {
|
|
|
|
|
_wavPackWriter.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Write(int[,] sampleBuffer, uint sampleCount) {
|
|
|
|
|
if (MD5Sum)
|
|
|
|
|
{
|
|
|
|
|
if (_sampleBuffer == null || _sampleBuffer.Length < sampleCount * _blockAlign)
|
|
|
|
|
_sampleBuffer = new byte[sampleCount * _blockAlign];
|
|
|
|
|
AudioCodecsDotNet.AudioCodecsDotNet.FLACSamplesToBytes(sampleBuffer, 0, _sampleBuffer, 0, sampleCount, _channelCount, _bitsPerSample);
|
|
|
|
|
_wavPackWriter.UpdateHash(_sampleBuffer, (int) sampleCount * _blockAlign);
|
2008-10-13 19:25:11 +00:00
|
|
|
}
|
2008-11-04 01:23:30 +00:00
|
|
|
_wavPackWriter.Write(sampleBuffer, (int) sampleCount);
|
2008-10-13 19:25:11 +00:00
|
|
|
}
|
2008-10-25 18:42:28 +00:00
|
|
|
|
|
|
|
|
public string Path { get { return _wavPackWriter.Path; } }
|
2008-10-13 19:25:11 +00:00
|
|
|
}
|
2008-10-19 17:58:48 +00:00
|
|
|
#endif
|
2008-10-13 19:25:11 +00:00
|
|
|
}
|