Example #1
0
        public static TzxFile ReadTzxFile(Stream inputStream, string fileName)
        {
            TzxFile resultFile = new TzxFile(fileName);

            using (BinaryReader fileReader = new BinaryReader(inputStream, Encoding.GetEncoding("ISO-8859-1")))
            {
                string signature = Encoding.GetEncoding("ISO-8859-1").GetString(fileReader.ReadBytes(7));
                if (signature != "ZXTape!")
                {
                    throw new InvalidDataException("The signature of the file does not match the standard TZX file format");
                }
                else
                {
                    resultFile.Signature = signature;
                }

                resultFile.EndOfTextFileMarker = fileReader.ReadByte();
                resultFile.MajorRevisionNumber = fileReader.ReadByte();
                resultFile.MinorRevisionNumber = fileReader.ReadByte();

                TapeSection currentGroupSection = null;

                while (fileReader.PeekChar() >= 0)
                {
                    TzxDataBlock tzxBlock = ReadTzxDataBlock(fileReader);
                    resultFile.DataBlocks.Add(tzxBlock);

                    switch (tzxBlock.Type)
                    {
                    case TzxDataBlockType.StandardSpeedDataBlock:
                    case TzxDataBlockType.TurboSpeedDataBlock:
                    case TzxDataBlockType.PureTone:
                    case TzxDataBlockType.PulseSequence:
                    case TzxDataBlockType.PureDataBlock:
                    case TzxDataBlockType.DirectRecording:
                        TapeSection section = new TapeSection(tzxBlock);
                        if (currentGroupSection == null)
                        {
                            resultFile.Sections.Add(section);
                        }
                        else
                        {
                            foreach (TapeSoundSequence soundSequence in section.SoundSequences)
                            {
                                currentGroupSection.SoundSequences.Add(soundSequence);
                            }
                        }
                        break;

                    case TzxDataBlockType.PauseOrStopTheTape:
                        PauseOrStopTheTape pauseBlock = (PauseOrStopTheTape)tzxBlock;
                        if (pauseBlock.PauseInMs > 0)
                        {
                            section = new TapeSection(tzxBlock);
                            if (currentGroupSection == null)
                            {
                                resultFile.Sections.Add(section);
                            }
                            else
                            {
                                foreach (TapeSoundSequence soundSequence in section.SoundSequences)
                                {
                                    currentGroupSection.SoundSequences.Add(soundSequence);
                                }
                            }
                        }
                        break;

                    case TzxDataBlockType.TextDescription:
                        TextDescription descriptionBlock = (TextDescription)tzxBlock;
                        resultFile.Description += descriptionBlock.Text;
                        break;

                    case TzxDataBlockType.ArchiveInfo:
                        ArchiveInfo archiveInfo = (ArchiveInfo)tzxBlock;
                        resultFile.Description += archiveInfo.ToString();
                        break;

                    case TzxDataBlockType.GroupStart:
                        currentGroupSection = new TapeSection(tzxBlock);
                        break;

                    case TzxDataBlockType.GroupEnd:
                        foreach (TapeSoundSequence soundSequence in currentGroupSection.SoundSequences)
                        {
                            currentGroupSection.Duration += soundSequence.Duration;
                        }
                        resultFile.Sections.Add(currentGroupSection);
                        currentGroupSection = null;
                        break;
                    }
                }
            }
            TapeSection       lastSection       = resultFile.Sections[resultFile.Sections.Count - 1];
            TapeSoundSequence lastSoundSequence = lastSection.SoundSequences[lastSection.SoundSequences.Count - 1];

            if (lastSoundSequence.GetType() != typeof(PauseSequence))
            {
                lastSection.SoundSequences.Add(new PauseSequence("End of tape", 1));
            }

            foreach (TapeSection section in resultFile.Sections)
            {
                resultFile.Duration += section.Duration;
            }

            return(resultFile);
        }
        public static TzxFile ReadTzxFile(Stream inputStream, string fileName)
        {
            TzxFile resultFile = new TzxFile(fileName);

            using (BinaryReader fileReader = new BinaryReader(inputStream, Encoding.GetEncoding("ISO-8859-1")))
            {
                string signature = Encoding.GetEncoding("ISO-8859-1").GetString(fileReader.ReadBytes(7));
                if (signature != "ZXTape!")
                {
                    throw new InvalidDataException("The signature of the file does not match the standard TZX file format");
                }
                else
                {
                    resultFile.Signature = signature;
                }

                resultFile.EndOfTextFileMarker = fileReader.ReadByte();
                resultFile.MajorRevisionNumber = fileReader.ReadByte();
                resultFile.MinorRevisionNumber = fileReader.ReadByte();

                TapeSection currentGroupSection = null;

                while (fileReader.PeekChar() >= 0)
                {
                    TzxDataBlock tzxBlock = ReadTzxDataBlock(fileReader);
                    resultFile.DataBlocks.Add(tzxBlock);

                    switch(tzxBlock.Type)
                    {
                        case TzxDataBlockType.StandardSpeedDataBlock:
                        case TzxDataBlockType.TurboSpeedDataBlock:
                        case TzxDataBlockType.PureTone:
                        case TzxDataBlockType.PulseSequence:
                        case TzxDataBlockType.PureDataBlock:
                        case TzxDataBlockType.DirectRecording:
                            TapeSection section = new TapeSection(tzxBlock);
                            if (currentGroupSection == null)
                            {
                                resultFile.Sections.Add(section);
                            }
                            else
                            {
                                foreach (TapeSoundSequence soundSequence in section.SoundSequences)
                                {
                                    currentGroupSection.SoundSequences.Add(soundSequence);
                                }
                            }
                            break;
                        case TzxDataBlockType.PauseOrStopTheTape:
                            PauseOrStopTheTape pauseBlock = (PauseOrStopTheTape)tzxBlock;
                            if (pauseBlock.PauseInMs > 0)
                            {
                                section = new TapeSection(tzxBlock);
                                if (currentGroupSection == null)
                                {
                                    resultFile.Sections.Add(section);
                                }
                                else
                                {
                                    foreach (TapeSoundSequence soundSequence in section.SoundSequences)
                                    {
                                        currentGroupSection.SoundSequences.Add(soundSequence);
                                    }
                                }
                            }
                            break;
                        case TzxDataBlockType.TextDescription:
                            TextDescription descriptionBlock = (TextDescription)tzxBlock;
                            resultFile.Description += descriptionBlock.Text;
                            break;
                        case TzxDataBlockType.ArchiveInfo:
                            ArchiveInfo archiveInfo = (ArchiveInfo)tzxBlock;
                            resultFile.Description += archiveInfo.ToString();
                            break;
                        case TzxDataBlockType.GroupStart:
                            currentGroupSection = new TapeSection(tzxBlock);
                            break;
                        case TzxDataBlockType.GroupEnd:
                            foreach (TapeSoundSequence soundSequence in currentGroupSection.SoundSequences)
                            {
                                currentGroupSection.Duration += soundSequence.Duration;
                            }
                            resultFile.Sections.Add(currentGroupSection);
                            currentGroupSection = null;
                            break;
                    }
                }
            }
            TapeSection lastSection = resultFile.Sections[resultFile.Sections.Count-1];
            TapeSoundSequence lastSoundSequence = lastSection.SoundSequences[lastSection.SoundSequences.Count - 1];
            if(lastSoundSequence.GetType() != typeof(PauseSequence))
            {
                lastSection.SoundSequences.Add(new PauseSequence("End of tape", 1));
            }

            foreach (TapeSection section in resultFile.Sections)
            {
                resultFile.Duration += section.Duration;
            }

            return resultFile;
        }