public void BitsPerSampleIsSerialized() { var formatter = new BinaryFormatter(); using (var stream = new MemoryStream()) { formatter.Serialize(stream, AudioInfo.CreateForLossless("Test", 2, 16, 44100)); stream.Position = 0; Assert.Equal(16, ((AudioInfo)formatter.Deserialize(stream)).BitsPerSample); } }
public void PathInvalidEncodingThrowsException() { var mock = new Mock <ITaggedAudioFile>(); mock.SetupGet(audioFile => audioFile.Info) .Returns(AudioInfo.CreateForLossless("Test", 2, 16, 44100, 100)); using (var ps = PowerShell.Create()) { ps.Runspace = _moduleFixture.Runspace; ps.AddCommand("Export-AudioFile") .AddParameter("Encoder", "Wave") .AddParameter("AudioFile", mock.Object) .AddParameter("Path", "{Invalid}"); Assert.IsType <ArgumentException>( Assert.Throws <CmdletInvocationException>(() => ps.Invoke()) .InnerException); } }
public AudioInfo ReadAudioInfo(Stream stream) { try { var mp4 = new Mp4Model(stream); mp4.DescendToAtom("moov", "trak", "mdia", "minf", "stbl", "stts"); var stts = new SttsAtom(mp4.ReadAtom(mp4.CurrentAtom)); var sampleCount = stts.PacketCount * stts.PacketSize; mp4.DescendToAtom("moov", "trak", "mdia", "minf", "stbl", "stsd", "mp4a", "esds"); var esds = new EsdsAtom(mp4.ReadAtom(mp4.CurrentAtom)); if (esds.IsAac) { mp4.Reset(); return(AudioInfo.CreateForLossy("AAC", esds.Channels, (int)esds.SampleRate, sampleCount, CalculateBitRate(mp4.GetChildAtomInfo().Single(atom => atom.FourCc.Equals("mdat", StringComparison.Ordinal)).Size, sampleCount, esds.SampleRate))); } // Apple Lossless files have their own atom for storing audio info if (!mp4.DescendToAtom("moov", "trak", "mdia", "minf", "stbl", "stsd", "alac")) { throw new AudioUnsupportedException("Only AAC and ALAC MP4 streams are supported."); } var alac = new AlacAtom(mp4.ReadAtom(mp4.CurrentAtom)); return(AudioInfo.CreateForLossless( "ALAC", alac.Channels, alac.BitsPerSample, (int)alac.SampleRate, sampleCount)); } catch (EndOfStreamException e) { throw new AudioInvalidException(e.Message); } }
public AudioInfo ReadAudioInfo(Stream stream) { using (var reader = new RiffReader(stream)) { try { reader.Initialize(); if (stream.Length != reader.RiffChunkSize + 8) { throw new AudioInvalidException("Stream is unexpectedly truncated."); } reader.BaseStream.Position = 8; if (!reader.ReadFourCc().Equals("WAVE", StringComparison.Ordinal)) { throw new AudioInvalidException("Not a Wave stream."); } var fmtChunkSize = reader.SeekToChunk("fmt "); if (fmtChunkSize == 0) { throw new AudioInvalidException("Missing 'fmt' chunk."); } var isExtensible = false; switch (reader.ReadUInt16()) { // WAVE_FORMAT_PCM case 1: break; // WAVE_FORMAT_EXTENSIBLE case 0xFFFE: isExtensible = true; break; default: throw new AudioUnsupportedException("Only PCM wave streams are supported."); } var channels = reader.ReadUInt16(); var sampleRate = reader.ReadUInt32(); // Ignore nAvgBytesPerSec stream.Seek(4, SeekOrigin.Current); var blockAlign = reader.ReadUInt16(); // Use wValidBitsPerSample if this is WAVE_FORMAT_EXTENSIBLE if (isExtensible) { stream.Seek(4, SeekOrigin.Current); } return(AudioInfo.CreateForLossless( "LPCM", channels, reader.ReadUInt16(), (int)sampleRate, reader.SeekToChunk("data") / blockAlign)); } catch (EndOfStreamException e) { // The end of the stream was unexpectedly reached throw new AudioInvalidException(e.Message); } } }
public void BitsPerSampleTooLowThrowsException() { Assert.Throws <AudioInvalidException>(() => AudioInfo.CreateForLossless("Test", 2, 0, 44100)); }
public void BitsPerSampleTooHighThrowsException() { Assert.Throws <AudioUnsupportedException>(() => AudioInfo.CreateForLossless("Test", 2, 33, 44100)); }
public void ChannelsTooHighThrowsException() { Assert.Throws <AudioUnsupportedException>(() => AudioInfo.CreateForLossless("Test", 3, 16, 44100)); }
public void ChannelsTooLowThrowsException() { Assert.Throws <AudioInvalidException>(() => AudioInfo.CreateForLossless("Test", 0, 16, 44100)); }
public void FormatNullThrowsException() { Assert.Throws <ArgumentNullException>(() => AudioInfo.CreateForLossless(null, 2, 16, 44100)); }
public void FrameCountNegativeThrowsException() { Assert.Throws <AudioInvalidException>(() => AudioInfo.CreateForLossless("Test", 2, 16, 44100, -1)); }
public void SampleRateTooLowThrowsException() { Assert.Throws <AudioInvalidException>(() => AudioInfo.CreateForLossless("Test", 2, 16, 0)); }