Example #1
0
        public void PcmIsSameAfterChangingNonCriticalParametersAndRebuildingChannel()
        {
            GcAdpcmChannel channel  = GetBuilder().WithLoop(true, 10, 100).Build();
            GcAdpcmChannel channel2 = channel.GetCloneBuilder().WithLoop(true, 20, 100).WithSamplesPerSeekTableEntry(20).Build();

            Assert.Same(channel.GetPcmAudio(), channel2.GetPcmAudio());
        }
Example #2
0
 private void WriteHeader(BinaryWriter writer)
 {
     for (int i = 0; i < ChannelCount; i++)
     {
         GcAdpcmChannel channel = Adpcm.Channels[i];
         writer.BaseStream.Position = HeaderSize * i;
         writer.Write(SampleCount);
         writer.Write(SampleCountToNibbleCount(SampleCount));
         writer.Write(Adpcm.SampleRate);
         writer.Write((short)(Adpcm.Looping ? 1 : 0));
         writer.Write(Format);
         writer.Write(StartAddr);
         writer.Write(EndAddr);
         writer.Write(CurAddr);
         writer.Write(channel.Coefs.ToByteArray(Endianness.BigEndian));
         writer.Write(channel.Gain);
         channel.StartContext.Write(writer);
         if (Adpcm.Looping)
         {
             channel.LoopContext.Write(writer);
         }
         else
         {
             writer.Write(new byte[3 * sizeof(short)]);
         }
         writer.Write((short)(ChannelCount == 1 ? 0 : ChannelCount));
         writer.Write((short)(ChannelCount == 1 ? 0 : FramesPerInterleave));
     }
 }
Example #3
0
        private void WriteHeader(BinaryWriter writer)
        {
            writer.WriteUTF8("IDSP");
            writer.Write(0);
            writer.Write(ChannelCount);
            writer.Write(Adpcm.SampleRate);
            writer.Write(SampleCount);
            writer.Write(LoopStart);
            writer.Write(LoopEnd);
            writer.Write(Configuration.BlockSize);
            writer.Write(StreamInfoSize);
            writer.Write(ChannelInfoSize);
            writer.Write(HeaderSize);
            writer.Write(AudioDataSize);

            for (int i = 0; i < ChannelCount; i++)
            {
                writer.BaseStream.Position = StreamInfoSize + i * ChannelInfoSize;
                GcAdpcmChannel channel = Adpcm.Channels[i];

                writer.Write(channel.SampleCount);
                writer.Write(SampleCountToNibbleCount(channel.SampleCount));
                writer.Write(Adpcm.SampleRate);
                writer.Write(Looping);
                writer.Write((short)0);
                writer.Write(StartAddr);
                writer.Write(EndAddr);
                writer.Write(CurAddr);
                writer.Write(channel.Coefs.ToByteArray(Endianness.BigEndian));
                writer.Write(channel.Gain);
                channel.StartContext.Write(writer);
                channel.LoopContext.Write(writer);
            }
        }
Example #4
0
        private static GcAdpcmFormat ToAdpcmStream(BxstmStructure structure)
        {
            StreamInfo streamInfo = structure.StreamInfo;
            List <GcAdpcmChannelInfo> channelInfo = structure.ChannelInfo.Channels;
            var channels = new GcAdpcmChannel[streamInfo.ChannelCount];

            for (int c = 0; c < channels.Length; c++)
            {
                var channelBuilder = new GcAdpcmChannelBuilder(structure.AudioData[c], channelInfo[c].Coefs, streamInfo.SampleCount)
                {
                    Gain         = channelInfo[c].Gain,
                    StartContext = channelInfo[c].Start
                };

                channelBuilder.WithLoop(streamInfo.Looping, streamInfo.LoopStart, streamInfo.SampleCount)
                .WithLoopContext(streamInfo.LoopStart, channelInfo[c].Loop.PredScale,
                                 channelInfo[c].Loop.Hist1, channelInfo[c].Loop.Hist2);

                if (structure.SeekTable != null)
                {
                    channelBuilder.WithSeekTable(structure.SeekTable[c], streamInfo.SamplesPerSeekTableEntry);
                }

                channels[c] = channelBuilder.Build();
            }

            return(new GcAdpcmFormatBuilder(channels, streamInfo.SampleRate)
                   .WithTracks(structure.TrackInfo?.Tracks)
                   .WithLoop(streamInfo.Looping, streamInfo.LoopStart, streamInfo.SampleCount)
                   .Build());
        }
Example #5
0
        public void PcmIsSameAfterRebuildingChannel()
        {
            GcAdpcmChannel channel  = GetBuilder().WithLoop(true, 10, 100).Build();
            GcAdpcmChannel channel2 = channel.GetCloneBuilder().Build();

            Assert.Same(channel.GetPcmAudio(), channel2.GetPcmAudio());
        }
Example #6
0
        public void ReturnsSameSeekTableAsAssigned()
        {
            var            seekTable = new short[100];
            GcAdpcmChannel channel   = GetBuilder().WithSeekTable(seekTable, 10).Build();

            Assert.Same(seekTable, channel.GetSeekTable());
        }
Example #7
0
        public void SeekTableIsSameAfterRebuilding()
        {
            GcAdpcmChannel channel  = GetBuilder().WithSamplesPerSeekTableEntry(10).Build();
            GcAdpcmChannel channel2 = channel.GetCloneBuilder().WithLoop(true, 10, 100).Build();

            Assert.Same(channel.GetSeekTable(), channel2.GetSeekTable());
        }
Example #8
0
        private void WriteHeadBlock3(BinaryWriter writer)
        {
            writer.Write((byte)ChannelCount);
            writer.Write((byte)0);  //padding
            writer.Write((short)0); //padding

            int baseOffset      = HeadBlockTableSize + HeadBlock1Size + HeadBlock2Size + 4;
            int offsetTableSize = ChannelCount * 8;

            for (int i = 0; i < ChannelCount; i++)
            {
                writer.Write(OffsetMarker);
                writer.Write(baseOffset + offsetTableSize + ChannelInfoSize * i);
            }

            for (int i = 0; i < ChannelCount; i++)
            {
                writer.Write(OffsetMarker);
                if (Codec != NwCodec.GcAdpcm)
                {
                    writer.Write(0);
                    continue;
                }

                GcAdpcmChannel channel     = Adpcm.Channels[i];
                GcAdpcmContext loopContext = Adpcm.Looping ? channel.LoopContext : channel.StartContext;

                writer.Write(baseOffset + offsetTableSize + ChannelInfoSize * i + 8);
                writer.Write(channel.Coefs.ToByteArray(Endianness.BigEndian));
                writer.Write(channel.Gain);
                channel.StartContext.Write(writer);
                loopContext.Write(writer);
                writer.Write((short)0);
            }
        }
Example #9
0
        private void WriteDspMcAdpcmHeader(BinaryWriter writer, int i)
        {
            writer.BaseStream.Position = McAdpcmHeaderSize + (DspHeaderSize + AudioDataSize) * i;

            GcAdpcmChannel channel = Adpcm.Channels[i];

            writer.Write(SampleCount);
            writer.Write(SampleCountToNibbleCount(SampleCount));
            writer.Write(Adpcm.SampleRate);
            writer.Write((short)(Adpcm.Looping ? 1 : 0));
            writer.Write(Format);
            writer.Write(StartAddr);
            writer.Write(EndAddr);
            writer.Write(CurAddr);
            writer.Write(channel.Coefs.ToByteArray());
            writer.Write(channel.Gain);
            channel.StartContext.Write(writer);
            if (!Adpcm.Looping)
            {
                channel.LoopContext.Write(writer);
            }
            else
            {
                writer.Write(new byte[3 * sizeof(short)]);
            }
            writer.Write((short)(0));
            writer.Write((short)(0));
        }
Example #10
0
        public void AdpcmIsSameAfterAligningAndRebuilding()
        {
            GcAdpcmChannel channel  = GetBuilder(100).WithLoop(true, 10, 100).WithLoopAlignment(15).Build();
            GcAdpcmChannel channel2 = channel.GetCloneBuilder().WithSamplesPerSeekTableEntry(10).Build();

            Assert.Same(channel.GetAdpcmAudio(), channel2.GetAdpcmAudio());
        }
Example #11
0
        public void AdpcmLengthIsCorrectAfterRealignment()
        {
            GcAdpcmChannel channel  = GetBuilder(100).WithLoop(true, 10, 100).WithLoopAlignment(15).Build();
            GcAdpcmChannel channel2 = channel.GetCloneBuilder().WithLoopAlignment(20).Build();

            Assert.Equal(GcAdpcmMath.SampleCountToByteCount(110), channel2.GetAdpcmAudio().Length);
        }
Example #12
0
        protected override IAudioFormat ToAudioStream(HpsStructure structure)
        {
            var channels = new GcAdpcmChannel[structure.ChannelCount];

            for (int c = 0; c < structure.ChannelCount; c++)
            {
                int audioLength     = structure.Blocks.Sum(x => x.Channels[c].AudioData.Length);
                var audio           = new byte[audioLength];
                int currentPosition = 0;

                foreach (HpsBlock block in structure.Blocks)
                {
                    byte[] source = block.Channels[c].AudioData;
                    Array.Copy(source, 0, audio, currentPosition, source.Length);
                    currentPosition += source.Length;
                }

                var channelBuilder = new GcAdpcmChannelBuilder(audio, structure.Channels[c].Coefs, structure.Channels[c].SampleCount)
                {
                    Gain         = structure.Channels[c].Gain,
                    StartContext = structure.Channels[c].Start
                };

                channelBuilder.WithLoop(structure.Looping, structure.LoopStart, structure.SampleCount)
                .WithLoopContext(structure.LoopStart, structure.Channels[c].Loop?.PredScale ?? 0,
                                 structure.Channels[c].Loop?.Hist1 ?? 0, structure.Channels[c].Loop?.Hist2 ?? 0);

                channels[c] = channelBuilder.Build();
            }

            return(new GcAdpcmFormatBuilder(channels, structure.SampleRate)
                   .WithLoop(structure.Looping, structure.LoopStart, structure.SampleCount)
                   .Build());
        }
Example #13
0
        public void PcmIsNotSameAfterChangingAndRebuildingChannel()
        {
            GcAdpcmChannel channel  = GetBuilder().WithLoop(true, 10, 100).Build();
            GcAdpcmChannel channel2 = channel.GetCloneBuilder().WithLoopAlignment(15).Build();

            Assert.NotSame(channel.GetPcmAudio(), channel2.GetPcmAudio());
        }
Example #14
0
        public void PcmIsSetWhenLoopPointsNeedAlignment()
        {
            //A new pcm array is generated if it doesn't exist, so check that the array returned
            //is the same when calling multiple times
            GcAdpcmChannel channel = GetBuilder().WithLoop(true, 20, 40).WithLoopAlignment(15).WithLoopContext(20, 0, 0, 0).Build();

            Assert.Same(channel.GetPcmAudio(), channel.GetPcmAudio());
        }
Example #15
0
        public void PcmLengthIsCorrectAfterAlignmentAndUnalignment()
        {
            GcAdpcmChannel channel  = GetBuilder(100).WithLoop(true, 10, 100).WithLoopAlignment(15).Build();
            GcAdpcmChannel channel2 = channel.GetCloneBuilder().WithLoopAlignment(0).Build();

            Assert.Equal(105, channel.GetPcmAudio().Length);
            Assert.Equal(100, channel2.GetPcmAudio().Length);
        }
Example #16
0
        public void AdpcmIsSameAfterAligningAndUnaligning()
        {
            GcAdpcmChannel channel  = GetBuilder(100).WithLoop(true, 10, 100).Build();
            GcAdpcmChannel channel2 = channel.GetCloneBuilder().WithLoopAlignment(15).Build();
            GcAdpcmChannel channel3 = channel2.GetCloneBuilder().WithLoopAlignment(0).Build();

            Assert.Same(channel.GetAdpcmAudio(), channel3.GetAdpcmAudio());
            Assert.NotSame(channel.GetAdpcmAudio(), channel2.GetAdpcmAudio());
        }
Example #17
0
 private void WriteChannelInfo(BinaryWriter writer, GcAdpcmChannel channel)
 {
     writer.Write(MaxBlockSize);
     writer.Write(SampleToNibble(0));
     writer.Write(SampleToNibble(SampleCount - 1));
     writer.Write(SampleToNibble(0));
     writer.Write(channel.Coefs.ToByteArray(Endianness.BigEndian));
     writer.Write(channel.Gain);
     channel.StartContext.Write(writer);
 }
Example #18
0
        public static GcAdpcmChannel[] GenerateAdpcmChannelsEmpty(int sampleCount, int channelCount)
        {
            var channels = new GcAdpcmChannel[channelCount];

            for (int i = 0; i < channelCount; i++)
            {
                var adpcm = new byte[GcAdpcmMath.SampleCountToByteCount(sampleCount)];
                var coefs = new short[16];
                channels[i] = new GcAdpcmChannelBuilder(adpcm, coefs, sampleCount).Build();
            }
            return(channels);
        }
Example #19
0
        private bool CompareEncodingCoarse(short[] pcm)
        {
            GcAdpcmChannel adpcmA = null;
            GcAdpcmChannel adpcmB = null;

            Parallel.Invoke(
                () => adpcmA = DllA.EncodeChannel(pcm),
                () => adpcmB = DllB.EncodeChannel(pcm)
                );

            return(ArraysEqual(adpcmA.Coefs, adpcmB.Coefs) == -1 &&
                   ArraysEqual(adpcmA.GetAdpcmAudio(), adpcmB.GetAdpcmAudio()) == -1);
        }
Example #20
0
        public static GcAdpcmFormat GenerateAdpcmEmpty(int sampleCount, int channelCount, int sampleRate, int samplesPerSeekTableEntry = 0x3800)
        {
            var channels = new GcAdpcmChannel[channelCount];

            for (int i = 0; i < channelCount; i++)
            {
                var builder = new GcAdpcmChannelBuilder(new byte[GcAdpcmMath.SampleCountToByteCount(sampleCount)], new short[16], sampleCount);
                builder.WithSeekTable(new short[sampleCount.DivideByRoundUp(samplesPerSeekTableEntry) * 2], samplesPerSeekTableEntry, true);
                channels[i] = builder.Build();
            }

            var adpcm = new GcAdpcmFormat(channels, sampleRate);

            return(adpcm);
        }
Example #21
0
        protected override IAudioFormat ToAudioStream(GenhStructure structure)
        {
            var channels = new GcAdpcmChannel[structure.ChannelCount];

            for (int c = 0; c < structure.ChannelCount; c++)
            {
                GcAdpcmChannel channel =
                    new GcAdpcmChannelBuilder(structure.AudioData[c], structure.Channels[c].Coefs, structure.SampleCount)
                    .WithLoop(structure.Looping, structure.LoopStart, structure.LoopEnd)
                    .Build();

                channels[c] = channel;
            }

            return(new GcAdpcmFormatBuilder(channels, structure.SampleRate)
                   .WithLoop(structure.Looping, structure.LoopStart, structure.LoopEnd)
                   .Build());
        }
Example #22
0
        protected override IAudioFormat ToAudioStream(DspStructure structure)
        {
            var channels = new GcAdpcmChannel[structure.ChannelCount];

            for (int c = 0; c < structure.ChannelCount; c++)
            {
                var channelBuilder = new GcAdpcmChannelBuilder(structure.AudioData[c], structure.Channels[c].Coefs, structure.SampleCount)
                {
                    Gain         = structure.Channels[c].Gain,
                    StartContext = structure.Channels[c].Start
                };

                channelBuilder.WithLoop(structure.Looping, structure.LoopStart, structure.LoopEnd)
                .WithLoopContext(structure.LoopStart, structure.Channels[c].Loop.PredScale,
                                 structure.Channels[c].Loop.Hist1, structure.Channels[c].Loop.Hist2);

                channels[c] = channelBuilder.Build();
            }

            return(new GcAdpcmFormatBuilder(channels, structure.SampleRate)
                   .WithLoop(structure.Looping, structure.LoopStart, structure.LoopEnd)
                   .Build());
        }
Example #23
0
        public void ReturnsEmptyArrayWhenNoSeekTable()
        {
            GcAdpcmChannel channel = GetBuilder().Build();

            Assert.Empty(channel.GetSeekTable());
        }
Example #24
0
        public void PcmIsNotSetWhenSeekTableAndLoopContextNotNeeded()
        {
            GcAdpcmChannel channel = GetBuilder().WithLoop(true, 20, 40).WithLoopContext(20, 0, 0, 0).Build();

            Assert.NotSame(channel.GetPcmAudio(), channel.GetPcmAudio());
        }
Example #25
0
        public void AdpcmLengthIsCorrectAfterBuilding()
        {
            GcAdpcmChannel channel = GetBuilder(100).WithLoop(true, 10, 100).Build();

            Assert.Equal(GcAdpcmMath.SampleCountToByteCount(100), channel.GetAdpcmAudio().Length);
        }
Example #26
0
 public short[] DecodeChannel(GcAdpcmChannel channel)
 {
     return(DecodeAdpcm(channel.GetAdpcmAudio(), channel.Coefs, channel.SampleCount));
 }
Example #27
0
        public void PcmLengthIsCorrectAfterBuilding()
        {
            GcAdpcmChannel channel = GetBuilder(100).WithLoop(true, 10, 100).Build();

            Assert.Equal(100, channel.GetPcmAudio().Length);
        }