public void CreatingAudioDataWithPcm() { Pcm16Format pcm = GenerateAudio.GeneratePcmSineWave(200, 2, 48000); var audio = new AudioData(pcm); Assert.Same(pcm, audio.GetFormat <Pcm16Format>()); }
private AdxKeyConfidence GetReencodeConfidence(CriAdxKey key, string filename) { CriAdxFormat adx; using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read)) { var reader = new AdxReader { EncryptionKey = key }; adx = (CriAdxFormat)reader.ReadFormat(stream); } Pcm16Format pcm = adx.ToPcm16(); CriAdxFormat adx2 = new CriAdxFormat().EncodeFromPcm16(pcm); var confidence = new AdxKeyConfidence(); int toCompareFull = adx.Channels[0].Audio.Length; int toCompareShort = Math.Min(adx.FrameSize * 100, toCompareFull); for (int i = 0; i < adx.ChannelCount; i++) { confidence.TotalBytesFull += toCompareFull; confidence.TotalBytesShort += toCompareShort; confidence.IdenticalBytesFull += Common.DiffArrays(adx.Channels[i].Audio, adx2.Channels[i].Audio, toCompareFull); confidence.IdenticalBytesShort += Common.DiffArrays(adx.Channels[i].Audio, adx2.Channels[i].Audio, toCompareShort); } return(confidence); }
public override OpusFormat EncodeFromPcm16(Pcm16Format pcm16, OpusParameters config) { const int frameSize = 960; var encoder = new OpusEncoder(pcm16.SampleRate, pcm16.ChannelCount, OpusApplication.OPUS_APPLICATION_AUDIO); if (config.Bitrate > 0) { encoder.Bitrate = config.Bitrate; } int inPos = 0; int remaining = pcm16.SampleCount; short[] pcmData = pcm16.Channels.Interleave(1); // Encoded data shouldn't be larger than the input pcm var buffer = new byte[frameSize * pcm16.ChannelCount]; var frames = new List <OpusFrame>(); short[] encodeInput = pcmData; while (remaining >= 0) { int encodeCount = Math.Min(frameSize, remaining); if (remaining < frameSize) { encodeInput = new short[frameSize * pcm16.ChannelCount]; Array.Copy(pcmData, inPos, encodeInput, 0, encodeCount); inPos = 0; } int frameLength = encoder.Encode(encodeInput, inPos, frameSize, buffer, 0, buffer.Length); var frame = new OpusFrame(); frame.Length = frameLength; frame.Data = new byte[frameLength]; frame.FinalRange = encoder.FinalRange; frame.SampleCount = frameSize; Array.Copy(buffer, frame.Data, frameLength); frames.Add(frame); if (remaining == 0) { break; } remaining -= encodeCount; inPos += encodeCount * pcm16.ChannelCount; } OpusFormat format = new OpusFormatBuilder(pcm16.SampleCount, pcm16.SampleRate, pcm16.ChannelCount, encoder.Lookahead, frames) .WithLoop(pcm16.Looping, pcm16.LoopStart, pcm16.LoopEnd) .Build(); return(format); }
public void GettingSecondFormatFromFirst() { Pcm16Format pcm = GenerateAudio.GeneratePcmSineWave(200, 2, 48000); var audio = new AudioData(pcm); var adpcm = audio.GetFormat <GcAdpcmFormat>(); Assert.Same(adpcm, audio.GetFormat <GcAdpcmFormat>()); }
public void AddingFormatsTogether() { short[][] channels = GetChannels(100, 2); Pcm16Format pcm = Pcm16Format.GetBuilder(new[] { channels[0] }, 32000).Build(); Pcm16Format pcm2 = Pcm16Format.GetBuilder(new[] { channels[1] }, 32000).Build(); Pcm16Format combined = pcm.Add(pcm2); Assert.Equal(channels, combined.Channels); }
public void Decoding() { short[][] channels = GetChannels(100, 2); Pcm16Format pcm = Pcm16Format.GetBuilder(channels, 32000).Build(); Pcm16Format pcm2 = pcm.ToPcm16(); Assert.NotSame(pcm, pcm2); Assert.Equal(pcm.Channels, pcm2.Channels); }
public void GettingChannelsThrowsWhenOutOfBounds() { short[][] channels = GetChannels(100, 4); Pcm16Format pcm = Pcm16Format.GetBuilder(channels, 32000).Build(); Exception ex = Record.Exception(() => pcm.GetChannels(4, 0)); Assert.IsType <ArgumentException>(ex); }
public void BrstmBuildAndParseEqualPcm16(int numChannels) { Pcm16Format audio = GenerateAudio.GeneratePcmSineWave(BuildParseTestOptions.Samples, numChannels, BuildParseTestOptions.SampleRate); var writer = new BrstmWriter { Configuration = { Codec = NwCodec.Pcm16Bit } }; BuildParseTests.BuildParseCompareAudio(audio, writer, new BrstmReader()); }
public void GetAllFormats() { Pcm16Format pcm = GenerateAudio.GeneratePcmSineWave(100, 1, 48000); var audio = new AudioData(pcm); Assert.Collection(audio.GetAllFormats(), x => Assert.True(x is Pcm16Format)); audio.GetFormat <GcAdpcmFormat>(); Assert.Collection(audio.GetAllFormats(), x => Assert.True(x is Pcm16Format), x => Assert.True(x is GcAdpcmFormat)); }
public override CriHcaFormat EncodeFromPcm16(Pcm16Format pcm16) { var config = new CriHcaParameters { ChannelCount = pcm16.ChannelCount, SampleRate = pcm16.SampleRate }; return(EncodeFromPcm16(pcm16, config)); }
public void FormatCreationWorksProperly() { short[][] channels = GetChannels(100, 2); Pcm16Format pcm = Pcm16Format.GetBuilder(channels, 32000).WithLoop(true, 10, 80).Build(); Assert.Equal(channels, pcm.Channels); Assert.True(pcm.Looping); Assert.Equal(10, pcm.LoopStart); Assert.Equal(80, pcm.LoopEnd); }
public void LoopsProperlyAfterEncoding(int sampleCount, bool looping, int loopStart, int loopEnd) { Pcm16Format pcm = GenerateAudio.GeneratePcmSineWave(sampleCount, 1, 48000) .WithLoop(looping, loopStart, loopEnd); IAudioFormat adpcm = new GcAdpcmFormat().EncodeFromPcm16(pcm); Assert.Equal(looping, adpcm.Looping); Assert.Equal(loopStart, adpcm.LoopStart); Assert.Equal(loopEnd, adpcm.LoopEnd); }
public void AddingFormatsOfDifferentLengthThrows() { short[][] channels = GetChannels(100, 2); short[][] channels2 = GetChannels(200, 2); Pcm16Format pcm = Pcm16Format.GetBuilder(channels, 32000).Build(); Pcm16Format pcm2 = Pcm16Format.GetBuilder(channels2, 32000).Build(); Exception ex = Record.Exception(() => pcm.Add(pcm2)); Assert.IsType <ArgumentException>(ex); }
public override CriHcaFormat EncodeFromPcm16(Pcm16Format pcm16, CriHcaParameters config) { config.ChannelCount = pcm16.ChannelCount; config.SampleRate = pcm16.SampleRate; config.SampleCount = pcm16.SampleCount; config.Looping = pcm16.Looping; config.LoopStart = pcm16.LoopStart; config.LoopEnd = pcm16.LoopEnd; IProgressReport progress = config.Progress; CriHcaEncoder encoder = CriHcaEncoder.InitializeNew(config); short[][] pcm = pcm16.Channels; var pcmBuffer = Helpers.CreateJaggedArray <short[][]>(pcm16.ChannelCount, SamplesPerFrame); progress?.SetTotal(encoder.Hca.FrameCount); var audio = Helpers.CreateJaggedArray <byte[][]>(encoder.Hca.FrameCount, encoder.FrameSize); int frameNum = 0; for (int i = 0; frameNum < encoder.Hca.FrameCount; i++) { int samplesToCopy = Math.Min(pcm16.SampleCount - i * SamplesPerFrame, SamplesPerFrame); for (int c = 0; c < pcm.Length; c++) { Array.Copy(pcm[c], SamplesPerFrame * i, pcmBuffer[c], 0, samplesToCopy); } int framesWritten = encoder.Encode(pcmBuffer, audio[frameNum]); if (framesWritten == 0) { throw new NotSupportedException("Encoder returned no audio. This should not happen."); } if (framesWritten > 0) { frameNum++; framesWritten--; progress?.ReportAdd(1); } while (framesWritten > 0) { audio[frameNum] = encoder.GetPendingFrame(); frameNum++; framesWritten--; progress?.ReportAdd(1); } } var builder = new CriHcaFormatBuilder(audio, encoder.Hca); return(builder.Build()); }
public void GettingSpecificChannels() { short[][] channels = GetChannels(100, 4); Pcm16Format pcm = Pcm16Format.GetBuilder(channels, 32000).Build(); Pcm16Format partial = pcm.GetChannels(2, 0, 2); Assert.Equal(100, partial.SampleCount); Assert.Equal(3, partial.ChannelCount); Assert.Equal(channels[2], partial.Channels[0]); Assert.Equal(channels[0], partial.Channels[1]); Assert.Equal(channels[2], partial.Channels[2]); }
public void AddSameFormats() { Pcm16Format pcm = GenerateAudio.GeneratePcmSineWave(100, 1, 48000); Pcm16Format pcm2 = GenerateAudio.GeneratePcmSineWave(100, 1, 48000); var audio = new AudioData(pcm); var audio2 = new AudioData(pcm2); AudioData combined = AudioData.Combine(audio, audio2); var pcmCombined = combined.GetFormat <Pcm16Format>(); Assert.Same(pcm.Channels[0], pcmCombined.Channels[0]); Assert.Same(pcm2.Channels[0], pcmCombined.Channels[1]); Assert.Equal(2, pcmCombined.ChannelCount); }
public void ListAvailableFormats() { var expected = new List <Type> { typeof(Pcm16Format) }; Pcm16Format pcm = GenerateAudio.GeneratePcmSineWave(100, 1, 48000); var audio = new AudioData(pcm); Assert.Equal(expected, audio.ListAvailableFormats()); expected.Add(typeof(GcAdpcmFormat)); audio.GetFormat <GcAdpcmFormat>(); Assert.Equal(expected, audio.ListAvailableFormats()); }
public void AddDifferentFormats() { Pcm16Format pcm = GenerateAudio.GeneratePcmSineWave(100, 1, 48000); GcAdpcmFormat adpcm = GenerateAudio.GenerateAdpcmSineWave(100, 1, 48000); var audio = new AudioData(pcm); var audio2 = new AudioData(adpcm); AudioData combined = AudioData.Combine(audio, audio2); Assert.Collection(combined.GetAllFormats(), x => Assert.True(x is Pcm16Format)); var pcmCombined = combined.GetFormat <Pcm16Format>(); Assert.Same(pcm.Channels[0], pcmCombined.Channels[0]); Assert.Equal(2, pcmCombined.ChannelCount); }
private GcAdpcmFormat GetAscendingAdpcm(int samplesPerEntry, int count, params int[] starts) { short[][] pcm = starts.Select(start => GenerateAudio.GenerateAscendingShorts(start, count)).ToArray(); Pcm16Format pcmFormat = Pcm16Format.GetBuilder(pcm, 48000).Build(); GcAdpcmFormat adpcm = new GcAdpcmFormat().EncodeFromPcm16(pcmFormat); for (int i = 0; i < adpcm.ChannelCount; i++) { adpcm.Channels[i] = adpcm.Channels[i] .GetCloneBuilder() .WithSamplesPerSeekTableEntry(samplesPerEntry) .Build(); } return(adpcm); }
public ParallelQuery <Result> Run() { return(Files.AsParallel().SelectMany(path => { try { byte[] wave = File.ReadAllBytes(path); Pcm16Format pcm = GetReader().ReadFormat(wave).ToPcm16(); return pcm.Channels.Select((x, i) => ComparePcm(x, path, i)); } catch (Exception ex) { return new[] { new Result { Filename = path, Channel = -1, Exception = ex } }; } })); }
public void AddSameFormatsAfterEncodingChoosesEncodedFormat() { Pcm16Format pcm = GenerateAudio.GeneratePcmSineWave(100, 1, 48000); Pcm16Format pcm2 = GenerateAudio.GeneratePcmSineWave(100, 1, 48000); var audio = new AudioData(pcm); var audio2 = new AudioData(pcm2); var adpcm = audio.GetFormat <GcAdpcmFormat>(); var adpcm2 = audio2.GetFormat <GcAdpcmFormat>(); AudioData combined = AudioData.Combine(audio, audio2); Assert.Collection(combined.GetAllFormats(), x => Assert.True(x is GcAdpcmFormat)); var pcmCombined = combined.GetFormat <Pcm16Format>(); var adpcmCombined = combined.GetFormat <GcAdpcmFormat>(); Assert.Same(adpcm.Channels[0].GetAdpcmAudio(), adpcmCombined.Channels[0].GetAdpcmAudio()); Assert.Same(adpcm2.Channels[0].GetAdpcmAudio(), adpcmCombined.Channels[1].GetAdpcmAudio()); Assert.NotSame(pcm.Channels[0], pcmCombined.Channels[0]); Assert.NotSame(pcm2.Channels[0], pcmCombined.Channels[1]); Assert.Equal(2, adpcmCombined.ChannelCount); }
public void AddDifferentFormatsMultipleInputs() { Pcm16Format pcmFormat = GenerateAudio.GeneratePcmSineWave(100, 4, 48000); Pcm16Format pcm0 = pcmFormat.GetChannels(0); Pcm16Format pcm1 = pcmFormat.GetChannels(1, 2); Pcm16Format pcm2 = pcmFormat.GetChannels(3); var audio0 = new AudioData(pcm0); var audio1 = new AudioData(pcm1); var audio2 = new AudioData(pcm2); audio0.GetFormat <GcAdpcmFormat>(); audio2.GetFormat <GcAdpcmFormat>(); AudioData combined = AudioData.Combine(audio0, audio1, audio2); Assert.Collection(combined.GetAllFormats(), x => Assert.True(x is Pcm16Format)); var pcmCombined = combined.GetFormat <Pcm16Format>(); Assert.Equal(4, pcmCombined.ChannelCount); Assert.Same(pcm0.Channels[0], pcmCombined.Channels[0]); Assert.Same(pcm1.Channels[0], pcmCombined.Channels[1]); Assert.Same(pcm1.Channels[1], pcmCombined.Channels[2]); Assert.Same(pcm2.Channels[0], pcmCombined.Channels[3]); }
public override Atrac9Format EncodeFromPcm16(Pcm16Format pcm16) { throw new NotImplementedException(); }
public override OpusFormat EncodeFromPcm16(Pcm16Format pcm16) => EncodeFromPcm16(pcm16, new OpusParameters());
private void CreateFormat <T>(CodecParameters configuration = null) where T : class, IAudioFormat, new() { Pcm16Format pcm = GetAudioFormat <Pcm16Format>(); AddFormat(new T().EncodeFromPcm16(pcm, configuration)); }
public virtual TFormat EncodeFromPcm16(Pcm16Format pcm16, TConfig config) => EncodeFromPcm16(pcm16);
public abstract TFormat EncodeFromPcm16(Pcm16Format pcm16);
IAudioFormat IAudioFormat.EncodeFromPcm16(Pcm16Format pcm16, CodecParameters config) => EncodeFromPcm16(pcm16, GetDerivedParameters(config));
IAudioFormat IAudioFormat.EncodeFromPcm16(Pcm16Format pcm16) => EncodeFromPcm16(pcm16);
public static GcAdpcmFormat GenerateAdpcmSineWave(int sampleCount, int channelCount, int sampleRate) { Pcm16Format pcm = GeneratePcmSineWave(sampleCount, channelCount, sampleRate); return(new GcAdpcmFormat().EncodeFromPcm16(pcm)); }