Beispiel #1
0
        // ReSharper restore MemberCanBeProtected.Global
        // ReSharper restore UnusedAutoPropertyAccessor.Global
        // ReSharper restore MemberCanBePrivate.Global
        // ReSharper restore AutoPropertyCanBeMadeGetOnly.Global

        // ReSharper disable once UnusedMember.Global
        public VgmFile()
        {
            // Empty file
            _stream = new MemoryStream();
            Header  = new VgmHeader();
            Gd3Tag  = new Gd3Tag();
        }
Beispiel #2
0
        public static IEnumerable <VgmCommand> Read(VgmHeader header, ISequentialReader reader)
        {
            var vgmDataOffset = GetVGMDataAbsoluteOffset(header);

            reader.SkipToAbsoluteOffset(vgmDataOffset);
            return(ReadSequence(reader));
        }
Beispiel #3
0
        public VgmFile(string filename)
        {
            _stream = new MemoryStream();
            Header  = new VgmHeader();
            Gd3Tag  = new Gd3Tag();

            LoadFromFile(filename);
        }
Beispiel #4
0
        private static void CalculateCommandsStatistics(string fileName, VgmHeader header, IEnumerable <VgmCommand> commands)
        {
            var stat = commands.GroupBy(x => x.Code)
                       .Select(x => new { Count = x.Count(), Code = x.Key })
                       .OrderBy(x => x.Code)
                       .ToList();

            File.WriteAllLines(fileName + ".txt", stat.Select(x => $"0x{x.Code:X2}: {x.Count}"));
        }
Beispiel #5
0
        private static uint GetVGMDataAbsoluteOffset(VgmHeader header)
        {
            const uint vgmDataOffsetPriorToV150 = 0x40;

            if (header.V150.VGMDataOffset == 0)
            {
                return(vgmDataOffsetPriorToV150);
            }
            else
            {
                var relativeOffset = header.V150.VGMDataOffset;
                return(relativeOffset + V150HeaderPart.VGMDataOffsetOffset);
            }
        }
Beispiel #6
0
        public static YM2612Settings GetYM2612Settings(this VgmHeader header)
        {
            if (GetDevicePresence(header, DeviceType.YM2612) == DevicePresence.Absent)
            {
                throw new InvalidOperationException();
            }

            var clock = header.Common.Version <= FormatVersion.V101 ?
                        header.V100.YM2413Clock : header.V110.YM2612Clock;

            return(new YM2612Settings
            {
                Clock = (int)clock
            });
        }
Beispiel #7
0
        public static VgmHeader Read(ISequentialReader reader)
        {
            var header = new VgmHeader();
            var unused = new V151HeaderPart();

            ReadCommon(reader, ref header.Common);

            if (header.Common.VgmIdentification != CommonHeaderPart.VgmIdentificationValue)
            {
                throw new VgmException("Vgm magic number does not found");
            }

            var version = header.Common.Version;

            if (version != FormatVersion.V100 &&
                version != FormatVersion.V101 &&
                version != FormatVersion.V110 &&
                version != FormatVersion.V150)
            {
                throw new VgmException("Unsupported version");
            }

            Read100(reader, ref header.V100);

            if (version == FormatVersion.V100)
            {
                return(header);
            }

            Read101(reader, ref header.V101);

            if (version == FormatVersion.V101)
            {
                return(header);
            }

            Read110Part1(reader, ref header.V110);
            Read151Part1(reader, ref unused);
            Read110Part2(reader, ref header.V110);

            if (version == FormatVersion.V110)
            {
                return(header);
            }

            Read150(reader, ref header.V150);
            return(header);
        }
Beispiel #8
0
        private static void RenderVgmToWav(string fileName, VgmHeader header, IEnumerable <VgmCommand> commands)
        {
            var wave         = CreateWave(header.GetSamplesCount());
            var sampleNumber = 0;
            var amplitude    = 15000f;
            var values       = Renderer.Render(commands);

            foreach (var value in values)
            {
                wave.Samples[sampleNumber++] = (short)(amplitude * value);
            }

            using (var outputStream = new FileStream(fileName + ".wav", FileMode.Create))
            {
                using (var writer = new BinaryWriter(outputStream))
                {
                    WavWriter.Write(wave, writer);
                }
            }
        }
Beispiel #9
0
        public static DevicePresence GetDevicePresence(this VgmHeader header, DeviceType deviceType)
        {
            switch (deviceType)
            {
            case DeviceType.SN76489:
                return(header.V100.SN76489Clock == 0 ? DevicePresence.Absent : DevicePresence.Present);

            case DeviceType.YM2612:
                if (header.Common.Version <= FormatVersion.V101)
                {
                    return(header.V100.YM2413Clock == 0 ? DevicePresence.Absent : DevicePresence.ProbablyPresent);
                }
                else
                {
                    return(header.V110.YM2612Clock == 0 ? DevicePresence.Absent : DevicePresence.Present);
                }

            default:
                throw new ArgumentOutOfRangeException(nameof(deviceType), deviceType, null);
            }
        }
Beispiel #10
0
        public static SN76489Settings GetSN76489Settings(this VgmHeader header)
        {
            if (GetDevicePresence(header, DeviceType.SN76489) == DevicePresence.Absent)
            {
                throw new InvalidOperationException();
            }

            var clock        = header.V100.SN76489Clock;
            var feedbackMask = header.Common.Version <= FormatVersion.V101
                ? 0x0009
                : header.V110.SN76489Feedback;

            var shiftRegisterWidth = header.Common.Version <= FormatVersion.V101
                ? 16
                : header.V110.SN76489ShiftRegisterWidth;

            return(new SN76489Settings
            {
                Clock = (int)clock,
                FeedbackMask = feedbackMask,
                ShiftRegisterWidth = shiftRegisterWidth
            });
        }
Beispiel #11
0
 public static long GetSamplesCount(this VgmHeader header) => header.V100.TotalNumberOfSamples;
Beispiel #12
0
 public static FormatVersion GetFormatVersion(this VgmHeader header) => header.Common.Version;