コード例 #1
0
        private static void Playback(object sender)
        {
            int line = 0;

            while (syncQue.Count != 0)
            {
                int       time = (int)TimeConverter.ConvertFrom(((Playback)sender).GetCurrentTime <ITimeSpan>(), ((Playback)sender).TempoMap);
                TickEvent peek = syncQue.Peek();
                if (peek.tick <= time)
                {
                    TickEvent tick = syncQue.Dequeue();
                    if (tick.cmd == 0x01)
                    {
                        if (line != tick.lineNumber)
                        {
                            line = tick.lineNumber;
                            Console.WriteLine();
                        }
                        Console.Write(tick.str);
                    }
                }
                Thread.Sleep(1);
            }
        }
コード例 #2
0
        private void LoadData(Stream fstream, int country)
        {
            using (fstream)
            {
                byte[] buffer = new byte[0x10];
                fstream.Read(buffer, 0, 0x10);
                if (!ByteArrayCompare(magic, buffer))
                {
                    throw new Exception("The file header has been corrupted.");
                }
                fstream.Position += 0x10;
                buffer            = new byte[4];

                fstream.Read(buffer, 0, 4);
                MidiSize = BitConverter.ToInt32(buffer, 0);
                fstream.Read(buffer, 0, 4);
                XcgSize = BitConverter.ToInt32(buffer, 0);
                fstream.Read(buffer, 0, 4);
                ChordSize         = BitConverter.ToInt32(buffer, 0);
                fstream.Position += 8;
                MidiData          = new byte[MidiSize];
                fstream.Read(MidiData, 0, MidiSize);
                XcgData = new byte[XcgSize];
                fstream.Read(XcgData, 0, XcgSize);
                ChordData = new byte[ChordSize];
                fstream.Read(ChordData, 0, ChordSize);

                using (Stream stream = new MemoryStream(XcgData))
                {
                    buffer = new byte[8];
                    stream.Read(buffer, (int)stream.Position, 8);
                    if (!ByteArrayCompare(buffer, xcgmagic))
                    {
                        throw new Exception();
                    }
                    buffer = new byte[2];
                    stream.Read(buffer, 0, 2);
                    LyricsSize = BitConverter.ToInt16(buffer, 0);
                    stream.Read(buffer, 0, 2);
                    SyncSize   = BitConverter.ToInt16(buffer, 0);
                    LyricsData = new byte[LyricsSize];
                    stream.Read(LyricsData, 0, LyricsSize);
                    SyncData = new byte[SyncSize];
                    stream.Read(SyncData, 0, SyncSize);
                }

                switch (country)
                {
                case 0:
                    Encoding = Encoding.GetEncoding("ks_c_5601-1987");
                    break;

                case 1:
                case 4:
                case 6:
                case 7:
                case 0x14:
                    Encoding = Encoding.GetEncoding("EUC-KR");
                    break;

                case 2:
                    //Encoding = Encoding.GetEncoding("CP936");
                    Encoding = Encoding.GetEncoding("EUC-CN");
                    break;

                case 3:
                    Encoding = Encoding.GetEncoding("Shift_JIS");
                    break;

                case 5:
                    Encoding = Encoding.GetEncoding("windows-1258");
                    break;

                case 8:
                    Encoding = Encoding.GetEncoding("Windows-1252");
                    break;

                case 9:
                    Encoding = Encoding.GetEncoding("windows-874");
                    break;

                case 10:
                    Encoding = Encoding.GetEncoding("windows-1251");
                    break;

                default:
                    throw new NotImplementedException();
                }
                Lyrics            = new SongInfo();
                Lyrics.tickEvents = new List <TickEvent>();
                var tickEvents = Lyrics.tickEvents;
                using (Stream syncStream = new MemoryStream(SyncData))
                {
                    using (Stream lyricsStream = new MemoryStream(LyricsData))
                    {
                        string            line              = "";
                        Queue <char>      lyricsQue         = new Queue <char>();
                        Queue <TickEvent> lyricsEventsQueue = new Queue <TickEvent>();
                        using (StreamReader streamReader = new StreamReader(lyricsStream, Encoding))
                        {
                            Lyrics.kNumber = int.Parse(streamReader.ReadLine().Split('#')[1]);
                            streamReader.ReadLine();
                            Lyrics.title    = streamReader.ReadLine();
                            Lyrics.subTitle = streamReader.ReadLine();
                            streamReader.ReadLine();
                            Lyrics.lyricist = streamReader.ReadLine();
                            Lyrics.composer = streamReader.ReadLine();
                            Lyrics.singer   = streamReader.ReadLine();
                            streamReader.ReadLine();
                            while (true)
                            {
                                line = streamReader.ReadLine();
                                if (line == "HANGUL" || streamReader.EndOfStream)
                                {
                                    break;
                                }
                                foreach (char item in line)
                                {
                                    lyricsQue.Enqueue(item);
                                }
                                lyricsQue.Enqueue('\n');
                            }
                        }
                        int       gasaline  = 0;
                        int       gasaindex = 0;
                        int       mode      = 0;
                        bool      cut       = true; //연속을 자를 것인가, 처음에 영어가 나올 경우를 대비에서 true로 설정
                        bool      skip      = false;
                        PronGuide pronGuide = null;
                        char      prev      = '\r';
                        int       part      = 3;
                        while (lyricsQue.Count != 0)
                        {
                            char c = lyricsQue.Dequeue();

                            if (c == '\n')
                            {
                                cut = true;
                                gasaline++;
                                gasaindex = 0;
                                mode      = 0;
                                skip      = true;
                            }
                            else if (c == '_') //_는 무조건 끊어주야 하기에 cut=true
                            {
                                cut  = true;
                                skip = true;
                            }
                            else if (c == '[')
                            {
                                string temp = "";
                                while ((c = lyricsQue.Dequeue()) != ']')
                                {
                                    temp += c;
                                }
                                if (temp.Length == 1)
                                {
                                    part = int.Parse(temp);
                                }
                                else
                                {
                                    part = 3;
                                }
                                skip = true;
                            }
                            else if (c == '<')
                            {
                                cut       = true;
                                mode      = 1;
                                pronGuide = new PronGuide();
                                skip      = true;
                            }
                            else if (c == '>')
                            {
                                mode      = 0;
                                cut       = false;
                                pronGuide = null;
                                skip      = true;
                            }
                            else if (c == '「')
                            {
                                mode = 2;
                                skip = true;
                            }
                            else if (c == '」')
                            {
                                mode      = 0;
                                pronGuide = null;
                                skip      = true;
                            }
                            else if (specialCharsc3.Contains(prev)) //전 문자가 (인 경우에는 무조건 붙여줘야 함
                            {
                                lyricsEventsQueue.LastOrDefault().str += c.ToString();
                                prev = c;
                                skip = true;
                            }
                            else if (mode == 2) //발음기호 추가해줌
                            {
                                if (lyricsEventsQueue.LastOrDefault().PronGuide == null)
                                {
                                    lyricsEventsQueue.LastOrDefault().PronGuide = new PronGuide();
                                }
                                lyricsEventsQueue.LastOrDefault().PronGuide.Pron += c.ToString();
                                skip = true;
                            }
                            else if (!cut && specialCharsc.Contains(c))
                            {
                                lyricsEventsQueue.LastOrDefault().str += c.ToString();
                                if (specialCharsc2.Contains(c)) //띄어쓰기인 경우에는 뒤에 영어가 나와도 잘라줌
                                {
                                    cut = true;
                                }
                                prev = c;
                                skip = true;
                            }

                            if (!skip && c == 'ん') //이게 최선인걸까
                            {
                                TickEvent temp = lyricsEventsQueue.LastOrDefault();
                                if (temp.PronGuide == null || temp.PronGuide.Ref.Count == 1)
                                {
                                    temp.str += c.ToString();
                                    skip      = true;
                                }
                                prev = c;
                            }
                            else if (!skip && c == 'っ')
                            {
                                TickEvent temp = lyricsEventsQueue.LastOrDefault();
                                if (temp.PronGuide == null || temp.PronGuide.Ref.Count == 1)
                                {
                                    temp.str += c.ToString();
                                    skip      = true;
                                }
                                prev = c;
                            }



                            if (skip)
                            {
                                skip = false;
                            }
                            else if (!cut && specialCharsc4.Contains(prev) && specialCharsc4.Contains(c))
                            {
                                lyricsEventsQueue.LastOrDefault().str += c.ToString();
                                if (specialCharsc2.Contains(c)) //띄어쓰기인 경우에는 뒤에 영어가 나와도 잘라줌
                                {
                                    cut = true;
                                }
                                prev = c;
                            }
                            else
                            {
                                TickEvent tickEvent = new TickEvent();
                                tickEvent.str         = c.ToString();
                                tickEvent.indexOfLine = gasaindex++;
                                tickEvent.lineNumber  = gasaline;
                                tickEvent.PronGuide   = pronGuide; //모드가 1인 경우에는 같은pronguide, 0일 경우에는 null이 들어감
                                tickEvent.partNum     = part;
                                lyricsEventsQueue.Enqueue(tickEvent);
                                cut  = false;
                                prev = c;
                            }
                        }

                        /*foreach (var item in lyricsEventsQueue)
                         * {
                         *  Console.WriteLine(item.str);
                         * }*/
                        buffer = new byte[4];
                        while (syncStream.Position != syncStream.Length)
                        {
                            byte temp = (byte)syncStream.ReadByte();
                            if (temp == 0x01)
                            {
                                syncStream.Read(buffer, 0, 4);
                                TickEvent t;
                                if (lyricsEventsQueue.Count != 0)
                                {
                                    t = lyricsEventsQueue.Dequeue();
                                }
                                else
                                {
                                    t = new TickEvent()
                                    {
                                        str = "ERR"
                                    };                                  //임시 오류처리
                                }
                                Console.WriteLine(t.str);
                                t.tick = BitConverter.ToInt32(buffer, 0);
                                t.cmd  = temp;
                                tickEvents.Add(t);
                            }
                            else
                            {
                                syncStream.Read(buffer, 0, 4);
                                TickEvent t = new TickEvent();
                                t.tick       = BitConverter.ToInt32(buffer, 0);
                                t.cmd        = temp;
                                t.lineNumber = tickEvents.LastOrDefault() == null ? 0 : tickEvents.LastOrDefault().lineNumber;
                                tickEvents.Add(t);
                            }
                        }
                    }
                }
                using (Stream lyricsStream = new MemoryStream(LyricsData))
                {
                    using (StreamReader streamReader = new StreamReader(lyricsStream, Encoding.GetEncoding("ks_c_5601-1987")))
                    {
                        while (streamReader.ReadLine() != "HANGUL" && !streamReader.EndOfStream)
                        {
                        }
                        while (!streamReader.EndOfStream)
                        {
                            Lyrics.HangulLyrics = streamReader.ReadToEnd();
                        }
                    }
                }
            }
        }