コード例 #1
0
        public static byte[] CompressToMsAdpcm(byte[] data, uint overwriteSampleRate, out int uncompressedSize)
        {
            WaveFormat      pcmFormat   = new WaveFormat((int)overwriteSampleRate, 16, 1);
            AdpcmWaveFormat adpcmFormat = new AdpcmWaveFormat((int)overwriteSampleRate, 1);

            using (var inStream = new MemoryStream(data))
                using (var anyWaveStream = new WaveFileReader(inStream))
                    using (var pcmStream = new RawSourceWaveStream(anyWaveStream, pcmFormat))
                    {
                        int sampleSize = ((pcmStream.WaveFormat.BitsPerSample * pcmStream.WaveFormat.Channels) / 8);
                        int uncompressedSampleCount = (int)(pcmStream.Length / sampleSize);
                        uncompressedSampleCount = AlignTo(uncompressedSampleCount, adpcmFormat.SamplesPerBlock);
                        uncompressedSize        = uncompressedSampleCount * 2; // Time 2 because 16 bit mono samples (2 byte per sample)

                        // We have to align the wave data to the wave block size
                        // otherise NAudio will just cut off some samples!
                        using (var alignedPcmStream = new AlignStream {
                            _baseStream = pcmStream, _extendedLengthInBytes = uncompressedSize
                        })
                            using (var adpcmStream = new WaveFormatConversionStream(adpcmFormat, alignedPcmStream))
                                using (var outStream = new MemoryStream())
                                {
                                    using (WaveFileWriter outWaveFileformat = new WaveFileWriter(outStream, adpcmFormat))
                                    {
                                        byte[] buffer = new byte[8192];
                                        int    bytesRead;
                                        while ((bytesRead = adpcmStream.Read(buffer, 0, buffer.Length)) != 0)
                                        {
                                            outWaveFileformat.Write(buffer, 0, bytesRead);
                                        }
                                    }
                                    return(outStream.ToArray());
                                }
                    }
        }
コード例 #2
0
        public void StructureContentsAreCorrect()
        {
            AdpcmWaveFormat adpcmWaveFormat = new AdpcmWaveFormat(8000, 1);

            Assert.AreEqual(WaveFormatEncoding.Adpcm, adpcmWaveFormat.Encoding, "Encoding");
            Assert.AreEqual(8000, adpcmWaveFormat.SampleRate, "Sample Rate");
            Assert.AreEqual(1, adpcmWaveFormat.Channels, "Channels");
            Assert.AreEqual(4, adpcmWaveFormat.BitsPerSample, "Bits Per Sample");
            Assert.AreEqual(4096, adpcmWaveFormat.AverageBytesPerSecond, "Average Bytes Per Second");
            Assert.AreEqual(32, adpcmWaveFormat.ExtraSize, "Extra Size");
            Assert.AreEqual(256, adpcmWaveFormat.BlockAlign, "Block Align");
            Assert.AreEqual(500, adpcmWaveFormat.SamplesPerBlock, "Channels");
            Assert.AreEqual(7, adpcmWaveFormat.NumCoefficients, "NumCoefficients");
            Assert.AreEqual(256, adpcmWaveFormat.Coefficients[0], "Coefficient 0");
            Assert.AreEqual(0, adpcmWaveFormat.Coefficients[1], "Coefficient 1");
            Assert.AreEqual(512, adpcmWaveFormat.Coefficients[2], "Coefficient 2");
            Assert.AreEqual(-256, adpcmWaveFormat.Coefficients[3], "Coefficient 3");
            Assert.AreEqual(0, adpcmWaveFormat.Coefficients[4], "Coefficient 4");
            Assert.AreEqual(0, adpcmWaveFormat.Coefficients[5], "Coefficient 5");
            Assert.AreEqual(192, adpcmWaveFormat.Coefficients[6], "Coefficient 6");
            Assert.AreEqual(64, adpcmWaveFormat.Coefficients[7], "Coefficient 7");
            Assert.AreEqual(240, adpcmWaveFormat.Coefficients[8], "Coefficient 8");
            Assert.AreEqual(0, adpcmWaveFormat.Coefficients[9], "Coefficient 9");
            Assert.AreEqual(460, adpcmWaveFormat.Coefficients[10], "Coefficient 10");
            Assert.AreEqual(-208, adpcmWaveFormat.Coefficients[11], "Coefficient 11");
            Assert.AreEqual(392, adpcmWaveFormat.Coefficients[12], "Coefficient 12");
            Assert.AreEqual(-232, adpcmWaveFormat.Coefficients[13], "Coefficient 13");
        }
コード例 #3
0
        public void StructureSizeIsCorrect()
        {
            WaveFormat waveFormat = new WaveFormat(8000, 16, 1);

            Assert.AreEqual(18, Marshal.SizeOf(waveFormat), "WaveFormat Size");
            AdpcmWaveFormat adpcmWaveFormat = new AdpcmWaveFormat(8000, 1);

            Assert.AreEqual(18 + 32, Marshal.SizeOf(adpcmWaveFormat), "WaveFormat Size");
        }
コード例 #4
0
        static public MicrosoftXWB Read(Stream source)
        {
            MicrosoftXWB result = new MicrosoftXWB();

            using (BinaryReader reader = new BinaryReader(source))
            {
                int             sampleCount = 0;
                WaveBankHeader  header      = new WaveBankHeader();
                WaveBankData    bank        = new WaveBankData();
                WaveBankEntry[] entries     = { };
                string[]        names       = { };
                MemoryStream    dataChunk   = new MemoryStream();

                header = WaveBankHeader.Read(source);

                for (int i = 0; i < (int)WaveBankSegIdx.Count; i++)
                {
                    WaveBankRegion region = header.Segments[i];
                    if (region.Length > 0)
                    {
                        source.Position = region.Offset;
                        MemoryStream mem       = new MemoryStream(reader.ReadBytes(region.Length));
                        BinaryReader memReader = new BinaryReader(mem);
                        switch (i)
                        {
                        case (int)WaveBankSegIdx.BankData:
                            bank        = WaveBankData.Read(mem);
                            sampleCount = bank.EntryCount;
                            entries     = new WaveBankEntry[sampleCount];
                            names       = new string[sampleCount];
                            mem.Dispose();
                            break;

                        case (int)WaveBankSegIdx.EntryMetaData:
                            for (int j = 0; j < sampleCount; j++)
                            {
                                entries[j] = WaveBankEntry.Read(mem);
                            }
                            mem.Dispose();
                            break;

                        case (int)WaveBankSegIdx.EntryNames:
                            for (int j = 0; j < sampleCount; j++)
                            {
                                names[j] = Util.TrimNulls(new string(memReader.ReadChars(Constants.WavebankEntrynameLength)));
                            }
                            mem.Dispose();
                            break;

                        case (int)WaveBankSegIdx.EntryWaveData:
                            dataChunk = mem;
                            break;

                        case (int)WaveBankSegIdx.SeekTables:
                            mem.Dispose();
                            break;

                        default:
                            mem.Dispose();
                            break;
                        }
                    }
                }

                if (sampleCount > 0)
                {
                    List <Sound> sounds = new List <Sound>();
                    for (int i = 0; i < sampleCount; i++)
                    {
                        WaveBankEntry entry   = entries[i];
                        byte[]        rawData = new byte[entry.PlayRegion.Length];
                        dataChunk.Position = entry.PlayRegion.Offset;
                        dataChunk.Read(rawData, 0, rawData.Length);

                        WaveFormat dataFormat;
                        switch (entry.Format.FormatTag)
                        {
                        case Constants.WavebankminiformatTagPcm:
                            dataFormat = WaveFormat.CreateCustomFormat(WaveFormatEncoding.Pcm, entry.Format.SamplesPerSec, entry.Format.Channels, entry.Format.AvgBytesPerSec, entry.Format.BlockAlign, entry.Format.BitsPerSample);
                            break;

                        case Constants.WavebankminiformatTagXma:
                            dataFormat = WaveFormat.CreateCustomFormat(WaveFormatEncoding.DviAdpcm, entry.Format.SamplesPerSec, entry.Format.Channels, entry.Format.AvgBytesPerSec, entry.Format.BlockAlign, entry.Format.BitsPerSample);
                            break;

                        case Constants.WavebankminiformatTagAdpcm:
                            dataFormat = new AdpcmWaveFormat(entry.Format.SamplesPerSec, entry.Format.Channels, (short)entry.Format.BlockAlign, entry.Format.AvgBytesPerSec, (short)entry.Format.AdpcmSamplesPerBlock);
                            //dataFormat = WaveFormat.CreateCustomFormat(WaveFormatEncoding.Adpcm, entry.Format.SamplesPerSec, entry.Format.Channels, entry.Format.AvgBytesPerSec, entry.Format.BlockAlign, entry.Format.BitsPerSample);
                            break;

                        case Constants.WavebankminiformatTagWma:
                            dataFormat = WaveFormat.CreateCustomFormat(WaveFormatEncoding.WindowsMediaAudio, entry.Format.SamplesPerSec, entry.Format.Channels, entry.Format.AvgBytesPerSec, entry.Format.BlockAlign, entry.Format.BitsPerSample);
                            break;

                        default:
                            dataFormat = null;
                            break;
                        }

                        if (dataFormat != null)
                        {
                            Sound baseSound = new Sound(rawData, dataFormat);
                            baseSound.Name = names[i];
                            sounds.Add(baseSound);
                        }
                    }
                    result.sounds = sounds.ToArray();
                    result.Name   = bank.BankName;
                }
            }
            return(result);
        }