Example #1
0
        public static void ABCNotationTest()
        {
            var bpm = 120D;
            double bps = bpm / 60D;
            uint m = (uint)(1000D / bps);

            var abcnotation = @"X:1
                                T:The Legacy Jig
                                M:6/8
                                L:1/8
                                R:jig
                                K:G
                                GFG BAB | gfg gab | GFG BAB | d2A AFD |
                                GFG BAB | gfg gab | age edB |1 dBA AFD :|2 dBA ABd |:
                                efe edB | dBA ABd | efe edB | gdB ABd |
                                efe edB | d2d def | gfe edB |1 dBA ABd :|2 dBA AFD |]";

            var song = new Song();
            foreach (var line in abcnotation.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
            {
                var temp = line.Trim();

                if (temp.StartsWith("X:"))
                {
                    song.Index = temp.Split(':')[1];
                }

                else if (temp.StartsWith("T:"))
                {
                    song.Title = temp.Split(':')[1];
                }

                else if (temp.StartsWith("M:"))
                {
                    song.TimeSignature = temp.Split(':')[1];
                }

                else if (temp.StartsWith("L:"))
                {
                    song.DefaultLength = temp.Split(':')[1];

                    var a = song.DefaultLength.Split('/')[0];
                    var div = song.DefaultLength.Split('/')[1];
                    var d = Double.Parse(a) / Double.Parse(div);
                    var r = m * d;
                    m = (uint)r;
                }

                else if (temp.StartsWith("R:"))
                {
                    song.Type = temp.Split(':')[1];
                }

                else if (temp.StartsWith("K:"))
                {
                    song.key = new Key(Scales.MajorScale, (Notes)Enum.Parse(typeof(Notes), temp.Split(':')[1]));
                }

                else
                {
                    Notes note = default(Notes);
                    bool writenote = false;
                    bool divide = false;

                    foreach (var bar in temp.Split('|'))
                    {
                        foreach (var elem in bar)
                        {
                            if (!writenote)
                            {
                                if (EnumExtensions.TryParse<Notes>(elem.ToString().ToUpper(), out note))
                                {
                                    writenote = true;
                                    continue;
                                }
                            }
                            else
                            {
                                if (elem == ':' || elem == ']')
                                    continue;

                                if (elem == '/')
                                {
                                    divide = true;
                                    continue;
                                }

                                if (string.IsNullOrWhiteSpace(elem.ToString()))
                                {
                                    continue;
                                }

                                double duration = 1;
                                if (Double.TryParse(elem.ToString(), out duration))
                                {
                                    //Currently discarding duration
                                    if(divide)
                                    {
                                        duration = m /duration;
                                        divide = false;
                                    }
                                    else
                                    {
                                        duration = m * duration;
                                    }
                                }
                                song.Pitches.Add(new Tuple<int, Pitch>((int)duration,
                                    new Pitch(note, Octaves.O4)));
                                writenote = false;
                            }
                        }
                    }
                }
            }

            var wavSound = new WavSoundProvider("songSynth.wav");
            using (Mixer mix = new Mixer(wavSound))
            {
                foreach (var pitch in song.Pitches)
                {
                    mix.PlaySound(pitch.Item2.GetFrequency(), (uint)pitch.Item1);

                }
            }
        }
Example #2
0
        public static void SynthConsole()
        {
            var go = true;
            Debug.Listeners.Add(new ConsoleTraceListener());

            var bpm = 120D;
            double bps = bpm / 60D;
            uint m = (uint)(1000D / bps);

            var sound = new SystemSoundProvider();
            var sound2 = new NetSoundProvider();
            var wavSound = new WavSoundProvider("wavSynth.wav");
            var debugsound = new DebugSoundProvider();

            Octaves o = Octaves.O4;

            using (Mixer mix = new Mixer(debugsound, wavSound, sound))
            {
                while (go)
                {
                    Console.Write(">");

                    Notes n;
                    var keypress = Console.ReadKey().Key;
                    Console.WriteLine();

                    if (keypress == ConsoleKey.UpArrow && !o.Equals(Octaves.O8))
                    {
                        o++;
                        Console.WriteLine("Octave={0}", o);
                    }
                    if (keypress == ConsoleKey.DownArrow && !o.Equals(Octaves.O1))
                    {
                        o--;
                        Console.WriteLine("Octave={0}", o);
                    }

                    bool isNote = Enum.TryParse<Notes>(Enum.GetName(typeof(ConsoleKey),
                        keypress), out n);
                    if (isNote)
                        mix.PlaySound(new Pitch(n, o), m);

                    if (keypress.Equals(ConsoleKey.X))
                        go = false;
                }
            }
            Console.ReadLine();
        }