예제 #1
0
파일: WaveDataChunk.cs 프로젝트: avs009/gsf
        /// <summary>Reads a new WAVE format section from the specified stream.</summary>
        /// <param name="preRead">Pre-parsed <see cref="RiffChunk"/> header.</param>
        /// <param name="source">Source stream to read data from.</param>
        /// <param name="waveFormat">Format of the data section to be parsed.</param>
        /// <exception cref="InvalidOperationException">WAVE format or extra parameters section too small, wave file corrupted.</exception>
        public WaveDataChunk(RiffChunk preRead, Stream source, WaveFormatChunk waveFormat)
            : base(preRead, RiffTypeID)
        {
            m_waveFormat = waveFormat;
            m_sampleBlocks = new List<LittleBinaryValue[]>();

            int blockSize = waveFormat.BlockAlignment;
            int sampleSize = waveFormat.BitsPerSample / 8;
            byte[] buffer = new byte[blockSize];
            int channels = waveFormat.Channels;
            TypeCode sampleTypeCode = m_waveFormat.GetSampleTypeCode();
            LittleBinaryValue[] sampleBlock;

            int bytesRead = source.Read(buffer, 0, blockSize);

            while (bytesRead == blockSize)
            {
                // Create a new sample block, one little-endian formatted binary sample value for each channel
                sampleBlock = new LittleBinaryValue[channels];

                for (int x = 0; x < channels; x++)
                {
                    sampleBlock[x] = new LittleBinaryValue(sampleTypeCode, buffer, x * sampleSize, sampleSize);
                }

                m_sampleBlocks.Add(sampleBlock);

                bytesRead = source.Read(buffer, 0, blockSize);
            }
        }
예제 #2
0
파일: WaveDataChunk.cs 프로젝트: avs009/gsf
 /// <summary>
 /// Constructs a new WAVE data chunk for the specified format.
 /// </summary>
 /// <param name="waveFormat"><see cref="WaveFormatChunk"/> that describes this <see cref="WaveDataChunk"/>.</param>
 public WaveDataChunk(WaveFormatChunk waveFormat)
     : base(RiffTypeID)
 {
     m_waveFormat = waveFormat;
     m_sampleBlocks = new List<LittleBinaryValue[]>();
 }
예제 #3
0
 /// <summary>
 /// Generate a basic extensible object based on the <see cref="WaveFormatChunk"/> settings.
 /// </summary>
 public WaveFormatExtensible(WaveFormatChunk waveFormat)
 {
     m_waveFormat = waveFormat;
     m_sampleValue = (ushort)m_waveFormat.BitsPerSample;
     m_channelMask = Speakers.All;
     m_subFormat = DataFormatSubType.PCM;
 }
예제 #4
0
 public WaveFormatExtensible(WaveFormatChunk waveFormat, ushort sampleValue, Speakers channelMask, Guid subFormat)
 {
     m_sampleValue = sampleValue;
     m_channelMask = channelMask;
     m_subFormat = subFormat;
 }
예제 #5
0
 public new WaveFormatChunk Clone()
 {
     WaveFormatChunk waveFormatChunk = new WaveFormatChunk(m_sampleRate, m_bitsPerSample, m_channels, m_audioFormat);
     waveFormatChunk.ExtraParameters = m_extraParameters;
     return waveFormatChunk;
 }
예제 #6
0
파일: WaveFile.cs 프로젝트: avs009/gsf
        /// <summary>Creates a new in-memory wave loaded from an existing wave audio stream.</summary>
        /// <param name="source">Stream of WAV formatted audio data to load.</param>
        /// <returns>In-memory representation of wave file.</returns>
        public static WaveFile Load(Stream source)
        {
            RiffChunk riffChunk;
            RiffHeaderChunk waveHeader = null;
            WaveFormatChunk waveFormat = null;
            WaveDataChunk waveData = null;

            while (waveData == null)
            {
                riffChunk = RiffChunk.ReadNext(source);

                switch (riffChunk.TypeID)
                {
                    case RiffHeaderChunk.RiffTypeID:
                        waveHeader = new RiffHeaderChunk(riffChunk, source, "WAVE");
                        break;
                    case WaveFormatChunk.RiffTypeID:
                        if (waveHeader == null)
                            throw new InvalidDataException("WAVE format section encountered before RIFF header, wave file corrupted.");

                        waveFormat = new WaveFormatChunk(riffChunk, source);
                        break;
                    case WaveDataChunk.RiffTypeID:
                        if (waveFormat == null)
                            throw new InvalidDataException("WAVE data section encountered before format section, wave file corrupted.");

                        waveData = new WaveDataChunk(riffChunk, source, waveFormat);
                        break;
                    default:
                        // Skip unidentified section
                        source.Seek(riffChunk.ChunkSize, SeekOrigin.Current);
                        break;
                }
            }

            return new WaveFile(waveHeader, waveFormat, waveData);
        }
예제 #7
0
파일: WaveFile.cs 프로젝트: avs009/gsf
 public WaveFile(RiffHeaderChunk waveHeader, WaveFormatChunk waveFormat, WaveDataChunk waveData)
 {
     m_waveHeader = waveHeader;
     m_waveFormat = waveFormat;
     m_waveData = waveData;
 }
예제 #8
0
파일: WaveFile.cs 프로젝트: avs009/gsf
 public WaveFile(int sampleRate, short bitsPerSample, short channels, ushort audioFormat)
 {
     m_waveHeader = new RiffHeaderChunk("WAVE");
     m_waveFormat = new WaveFormatChunk(sampleRate, bitsPerSample, channels, audioFormat);
     m_waveData = new WaveDataChunk(m_waveFormat);
 }
예제 #9
0
파일: WaveFile.cs 프로젝트: avs009/gsf
 /// <summary>Creates a new empty in-memory wave file using standard CD quality settings</summary>
 public WaveFile()
 {
     m_waveHeader = new RiffHeaderChunk("WAVE");
     m_waveFormat = new WaveFormatChunk(44100, 16, 2, 0x1);
     m_waveData = new WaveDataChunk(m_waveFormat);
 }