Пример #1
0
        public void Play(string rttlData)
        {
            const int oneBpmWholeNote = 60 * 4 * 1000; //one bpm whole note in ms

            Rttl rttl      = Rttl.Parse(rttlData);
            int  wholeNote = oneBpmWholeNote / rttl.Bpm;

            foreach (string tone in rttl.Tones)
            {
                bool   specialDuration;
                string durationStr, noteStr, scaleStr;
                ParseCommand(tone, out durationStr, out noteStr, out scaleStr, out specialDuration);

                int duration = rttl.Duration;
                if (durationStr.Length > 0)
                {
                    duration = Int32.Parse(durationStr);
                }

                duration = specialDuration ? (3 * wholeNote) / (2 * duration) : wholeNote / duration;

                int freqIndex;
                switch (noteStr[0].ToLower())
                {
                case 'c':
                    freqIndex = 0;
                    break;

                case 'd':
                    freqIndex = 2;
                    break;

                case 'e':
                    freqIndex = 4;
                    break;

                case 'f':
                    freqIndex = 5;
                    break;

                case 'g':
                    freqIndex = 7;
                    break;

                case 'a':
                    freqIndex = 9;
                    break;

                case 'b':
                    freqIndex = 11;
                    break;

                default:
                    freqIndex = -1;
                    break;
                }

                if (noteStr.Length > 1) //#
                {
                    freqIndex++;
                }

                int scale = rttl.Octave;
                if (scaleStr.Length > 0)
                {
                    scale = Int32.Parse(scaleStr);
                }

                if (freqIndex >= 0)
                {
                    double freq = Scales[scale - 4][freqIndex];

                    Debug.Print("Playing: (" + tone + ")" + freq + " " + duration);
                    _speaker.Play(freq);
                    Thread.Sleep(duration);
                    _speaker.Pause();
                }
                else
                {
                    Debug.Print("Pausing: (" + tone + ") " + duration);
                    _speaker.Pause();
                    Thread.Sleep(duration);
                }
            }
        }