Esempio n. 1
0
        static void Main(string[] args)
        {
            var          pipe = new NamedPipeClientStream(".", Connection.ConnectionString, PipeDirection.Out, PipeOptions.None);
            StreamWriter sw   = new StreamWriter(pipe);

            pipe.Connect();

            //Init serializer for speed later.
            {
                PianoKeyPressEvent kpe = new PianoKeyPressEvent(new PianoKey(1, Note.A), true);
                kpe.Serialize();
            }

            var piano = new PianoController();

            piano.KeyChangeAction = (PianoKey key, bool down) =>
            {
                if (pipe.IsConnected)
                {
                    PianoKeyPressEvent kpe = new PianoKeyPressEvent(key, down);
                    sw.WriteLine(kpe.Serialize());
                    sw.Flush();
                    Console.Write(down ? "D: " : "U: ");
                    Console.WriteLine(kpe.Key.Note + " : " + kpe.Key.Octave);
                }
            };
            piano.Start();

            while (pipe.IsConnected)
            {
                System.Threading.Thread.Sleep(200);
            }
            Console.WriteLine("Done");
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Song song = SongLoader.Load("..\\..\\..\\..\\songs\\moonlight_sonata.mid");

            Console.WriteLine("Starting Server");

            ConcurrentDictionary <PianoKey, byte> pressedKeys = new ConcurrentDictionary <PianoKey, byte>();

            var pianoPlayer = new PianoPlayer();

            Task.Run(() =>
            {
                var pipe        = new NamedPipeServerStream(Connection.ConnectionString, PipeDirection.In, 1);
                StreamReader sr = new StreamReader(pipe);
                Console.WriteLine("Waiting for connection....");

                pipe.WaitForConnection();

                //Init deserializer for speed later.
                {
                    PianoKeyPressEvent.Deserialize(new PianoKeyPressEvent(new PianoKey(1, Note.A), true).Serialize());
                }
                Console.WriteLine("Connected");

                while (pipe.IsConnected)
                {
                    var kpe = PianoKeyPressEvent.Deserialize(sr.ReadLine());
                    if (kpe.Pressed)
                    {
                        pressedKeys.AddOrUpdate(kpe.Key, 0, (x, y) => 0);
                    }
                    else
                    {
                        pressedKeys.Remove(kpe.Key, out byte x);
                    }
                }
                Console.WriteLine("Connection closed.");
            });

            using (DisplayWindow visualizer = new DisplayWindow(512, 512, "Piano"))
            {
                visualizer.SoundPlayer = pianoPlayer;
                visualizer.CurrentSong = song;
                visualizer.PressedKeys = pressedKeys;
                visualizer.Run(30, 30);
            }

            Console.ReadKey();
        }