Beispiel #1
0
 public IChunk ReadNextChunk()
 {
     if (BR.BaseStream.Position + 4 >= BR.BaseStream.Length)
     {
         BR.BaseStream.Position += 4;
         return null;
     }
     string chkid = (System.Text.UTF8Encoding.UTF8.GetString(BR.ReadBytes(4), 0, 4)).ToLower();
     switch (chkid)
     {
         case "riff":
             MasterChunk mc = new MasterChunk();
             mc.chkID = new char[] { 'R', 'I', 'F', 'F' };
             mc.chksize = BitConverter.ToInt32(BR.ReadBytes(4), 0);
             mc.WAVEID = BR.ReadChars(4);
             return mc;
         case "fact":
             FactChunk fc = new FactChunk();
             fc.chkID = new char[] { 'f', 'a', 'c', 't' };
             fc.chksize = BitConverter.ToInt32(BR.ReadBytes(4), 0);
             fc.dwSampleLength = BitConverter.ToInt32(BR.ReadBytes(4), 0);
             return fc;
         case "data":
             DataChunk dc = new DataChunk();
             dc.chkID = new char[] { 'd', 'a', 't', 'a' };
             dc.chksize = BitConverter.ToInt32(BR.ReadBytes(4), 0);
             if (dc.chksize % 2 == 0)
                 dc.pad = 0;
             else
                 dc.pad = 1;
             dc.sampled_data = BR.ReadBytes(dc.chksize);
             return dc;
         case "fmt ":
             FormatChunk fc2 = new FormatChunk();
             fc2.chkID = new char[] { 'f', 'm', 't', ' ' };
             fc2.chksize = BitConverter.ToInt32(BR.ReadBytes(4), 0);
             fc2.wFormatTag = BitConverter.ToInt16(BR.ReadBytes(2), 0);
             fc2.nChannels = BitConverter.ToInt16(BR.ReadBytes(2), 0);
             fc2.nSamplesPerSec = BitConverter.ToInt32(BR.ReadBytes(4), 0);
             fc2.nAvgBytesPerSec = BitConverter.ToInt32(BR.ReadBytes(4), 0);
             fc2.nBlockAlign = BitConverter.ToInt16(BR.ReadBytes(2), 0);
             fc2.wBitsPerSample = BitConverter.ToInt16(BR.ReadBytes(2), 0);
             if (fc2.wFormatTag != (short)WaveHelper.Format_Code.WAVE_FORMAT_PCM)
             {
                 fc2.cbSize = BitConverter.ToInt16(BR.ReadBytes(2), 0);
             }
             if ((int)fc2.wFormatTag == (int)WaveHelper.Format_Code.WAVE_FORMAT_EXTENSIBLE)
             {
                 fc2.wValidBitsPerSample = BitConverter.ToInt16(BR.ReadBytes(2), 0);
                 fc2.dwChannelMask = BitConverter.ToInt32(BR.ReadBytes(4), 0);
                 fc2.SubFormat = BR.ReadChars(16);
             }
             return fc2;
         default:
             break;
     }
     return null;
 }
Beispiel #2
0
        public static void ChangeBitsPerSample(FormatChunk fmtchk, DataChunk datachk, int bitsPerSample)
        {
            if (fmtchk.wBitsPerSample == bitsPerSample)
                return;

            float[,] samples = GetSampleData(fmtchk, datachk);
            datachk.sampled_data = GetRawData(samples, bitsPerSample);
            datachk.chksize = datachk.sampled_data.Length;
            fmtchk.wBitsPerSample = (short)bitsPerSample;
            fmtchk.nBlockAlign = (short)(fmtchk.nChannels * (bitsPerSample / 8));
            fmtchk.nAvgBytesPerSec = fmtchk.nBlockAlign * fmtchk.nSamplesPerSec;
        }
Beispiel #3
0
 public static float[,] GetSampleData(FormatChunk fmtchk, DataChunk datachk)
 {
     int bytesPerSample = fmtchk.wBitsPerSample / 8;
     int channels = fmtchk.nChannels;
     int samplePerChannel = (datachk.sampled_data.Length / bytesPerSample) / channels;
     float[,] samples = new float[channels, samplePerChannel];
     for (int x = 0; x < samplePerChannel; x++)
     {
         for (int i = 0; i < channels; i++)
         {
             switch (bytesPerSample)
             {
                 case 1:
                     samples[i, x] = (datachk.sampled_data[(x * channels) + i] - 128) / 128.0f;
                     break;
                 case 2:
                     samples[i, x] = BitConverter.ToInt16(datachk.sampled_data,(x * 2 * channels) + (i * 2)) / (float)Int16.MaxValue;
                     break;
                 case 3:
                     samples[i, x] = ToInt24(datachk.sampled_data, (x * 3 * channels) + (i * 3)) / (float)8388607;
                     break;
                 case 4:
                     samples[i, x] = BitConverter.ToInt32(datachk.sampled_data, (x * 4 * channels) + (i * 4)) / (float)Int32.MaxValue;
                     break;
             }
         }
     }
     return samples;
 }