Beispiel #1
0
        /// <summary>
        /// Convert file to RIFF.
        /// </summary>
        /// <returns></returns>
        public RIFF toRIFF()
        {
            RIFF r = new RIFF();

            r.fmt.bitsPerSample = 8;
            r.fmt.chunkFormat   = 1;
            r.fmt.numChannels   = head.numChannel;
            r.fmt.restOfData    = new byte[0];
            r.fmt.sampleRate    = head.nSampleRate;

            switch (head.waveType)
            {
            case 0:
                r.data.pcm8 = new byte[r.fmt.numChannels][];
                for (int i = 0; i < data.pcm8.Length; i++)
                {
                    r.data.pcm8[i] = new byte[data.pcm8[i].Length];
                    for (int j = 0; j < data.pcm8[i].Length; j++)
                    {
                        r.data.pcm8[i][j] = (byte)(data.pcm8[i][j] + 0x80);
                    }
                }
                r.unpackPCMChannels();
                break;

            case 1:
                r.fmt.bitsPerSample = 16;
                r.data.pcm16        = new short[r.fmt.numChannels][];
                for (int i = 0; i < data.pcm16.Length; i++)
                {
                    r.data.pcm16[i] = new short[data.pcm16[i].Length];
                    for (int j = 0; j < data.pcm16[i].Length; j++)
                    {
                        r.data.pcm16[i][j] = data.pcm16[i][j];
                    }
                }
                r.unpackPCMChannels();
                break;

            case 2:
                r.fmt.bitsPerSample = 16;
                r.data.pcm16        = new short[r.fmt.numChannels][];

                //Convert IMA-ADPCM to PCM16.
                short[][] newSamples = new short[r.fmt.numChannels][];
                for (int i = 0; i < r.fmt.numChannels; i++)
                {
                    List <short> samples    = new List <short>();
                    int          blockCount = 0;
                    foreach (dataBlock.imaAdpcmBlock a in data.imaAdpcm[i])
                    {
                        IMAADPCMDecoder d           = new IMAADPCMDecoder(a.data, 0);
                        int             sampleCount = 0;
                        if (blockCount != head.nBlock - 1)
                        {
                            foreach (byte b in a.data)
                            {
                                if (sampleCount < head.nBlockSample)
                                {
                                    try { samples.Add(d.GetSample()); } catch { }
                                }
                                sampleCount += 1;
                                if (sampleCount < head.nBlockSample)
                                {
                                    try { samples.Add(d.GetSample()); } catch { }
                                }
                                sampleCount += 1;
                            }
                        }
                        else
                        {
                            foreach (byte b in a.data)
                            {
                                if (sampleCount < head.nLastBlockSample)
                                {
                                    try { samples.Add(d.GetSample()); } catch { }
                                }
                                sampleCount += 1;
                                if (sampleCount < head.nLastBlockSample)
                                {
                                    try { samples.Add(d.GetSample()); } catch { }
                                }
                                sampleCount += 1;
                            }
                        }
                        blockCount += 1;
                    }

                    newSamples[i] = samples.ToArray();
                }

                r.data.pcm16 = newSamples;
                r.unpackPCMChannels();
                break;
            }

            //Make loop.
            if (head.loop == 1)
            {
                r.smpl           = new RIFF.smplBlock();
                r.smpl.loopStart = head.nLoopOffset;
                r.smpl.loopEnd   = head.nSample;
            }

            r.update();
            return(r);
        }
Beispiel #2
0
        /// <summary>
        /// Convert to RIFF.
        /// </summary>
        /// <returns></returns>
        public RIFF toRIFF()
        {
            RIFF r = new RIFF();

            r.data = new RIFF.dataBlock();
            r.fmt  = new RIFF.fmtBlock();
            r.fmt.bitsPerSample = 8;
            r.fmt.sampleRate    = (UInt32)data.info.nSampleRate;
            r.fmt.restOfData    = new byte[0];
            r.fmt.chunkFormat   = 1;
            r.fmt.numChannels   = 1;

            switch (data.info.waveType)
            {
            case 0:
                r.data.data = new byte[data.pcm8.Length];
                for (int i = 0; i < r.data.data.Length; i++)
                {
                    r.data.data[i] = (byte)((data.pcm8[i] + 0x80));
                }
                break;

            case 1:
                r.fmt.bitsPerSample = 16;
                MemoryStream o  = new MemoryStream();
                BinaryWriter bw = new BinaryWriter(o);
                foreach (short s in data.pcm16)
                {
                    bw.Write(s);
                }
                r.data.data = o.ToArray();
                break;

            case 2:
                MemoryStream o2  = new MemoryStream();
                BinaryWriter bw2 = new BinaryWriter(o2);
                r.fmt.bitsPerSample = 16;
                List <short>    std = new List <short>();
                IMAADPCMDecoder d   = new IMAADPCMDecoder(data.imaAdpcm, 0);
                foreach (byte b in data.imaAdpcm)
                {
                    try { std.Add(d.GetSample()); } catch { }
                    try { std.Add(d.GetSample()); } catch { }
                }
                foreach (short s in std)
                {
                    bw2.Write(s);
                }
                r.data.data = o2.ToArray();
                break;
            }

            //Make looping RIFF.
            if (data.info.loopFlag == 1)
            {
                r.smpl = new RIFF.smplBlock();

                switch (data.info.waveType)
                {
                case 0:
                    r.smpl.loopStart = (UInt32)data.info.nloopOffset * 4;
                    r.smpl.loopEnd   = (UInt32)r.data.data.Length;
                    break;

                case 1:
                    r.smpl.loopStart = (UInt32)data.info.nloopOffset * 2;
                    r.smpl.loopEnd   = (UInt32)r.data.data.Length / 2;
                    break;

                case 2:
                    r.smpl.loopStart = (UInt32)data.info.nloopOffset * 8;
                    r.smpl.loopEnd   = (UInt32)r.data.data.Length / 2;
                    break;
                }
            }

            r.update();

            return(r);
        }