static void Main(string[] args)
        {
            var s = new Sample(0.25f, 8000);
            for (int i = 0; i < s.Count; i++)
            {
                s[i].Frequency = 220.0f;
                s[i].Volume = 0.50f;
            }

            var s2 = new Sample(0.25f, 8000);
            for (int i = 0; i < s2.Count; i++)
            {
                s2[i].Frequency = 150.0f;
                s2[i].Volume = 0.90f;
            }

            var s3 = new Sample(0.25f, 8000);
            for (int i = 0; i < s3.Count; i++)
            {
                s3[i].Frequency = 250.0f;
                s3[i].Volume = 0.50f;
            }

            var transformedSample = Transforms.GenerateSquareWaveform(s);
            var transformedSample2 = Transforms.GenerateSineWaveform(s2);
            var transformedSample3 = Transforms.GenerateSawtoothWaveform(s3);

            int j = 0;
            using (var m = new JTAudioMixer())
            {
                while (true)
                {
                    if(GetAsyncKeyState( System.Windows.Forms.Keys.A) < 0)
                        m.SetData(j++, transformedSample);

                    if (GetAsyncKeyState(System.Windows.Forms.Keys.S) < 0)
                        m.SetData(j++, transformedSample2);

                    if (GetAsyncKeyState(System.Windows.Forms.Keys.D) < 0)
                        m.SetData(j++, transformedSample3);

                    System.Threading.Thread.Sleep(10);

                    if(j >=15)j = 0;
                }

            }
        }
        public static void Main(string[] args)
        {
            var instrumentSampleMappings = new ConcurrentDictionary<IPAddress, ClientInstruments>();

            Task.Factory.StartNew(() =>
            {
                using (var httpServer = new HttpListener())
                {
                    httpServer.Prefixes.Add("http://*:8989/");
                    httpServer.Start();

                    while (true)
                    {
                        var c = httpServer.GetContext();
                        var expression = new StreamReader(c.Request.InputStream).ReadToEnd().Split(',');

                        string message = "";
                        try
                        {
                            int instrumentId = int.Parse( c.Request.QueryString["instrument"]);
                            bool state = c.Request.QueryString["state"] == "1";

                            var endpoint = c.Request.RemoteEndPoint;

                            ClientInstruments ci = null;
                            if (!instrumentSampleMappings.TryGetValue(endpoint.Address, out ci))
                            {
                                ci = new ClientInstruments();
                                instrumentSampleMappings[endpoint.Address] = ci;
                            }

                            ci.States[instrumentId] = state;

                            message = "OK";
                        }

                        catch
                        {
                            message = "Bad request. Expecting format `<instrument #>,<1|0>`.";
                        }

                        byte[] buffer = System.Text.Encoding.UTF8.GetBytes(message);

                        var resp = c.Response;

                        System.IO.Stream output = resp.OutputStream;
                        var writer = new StreamWriter(output);
                        writer.WriteLine(message);
                        output.Close();
                    }

                }
            });

            using (var mixer = new JTAudioMixer())
            {

                int activeChannelIndex = 0;
                while (true)
                {
                    foreach (var instrument in instrumentSampleMappings.Values)
                    {
                        for (int i = 0; i < 16; i++)
                        {
                            if (instrument.States[i])
                                mixer.SetData(activeChannelIndex++, instrument.Samples[i]);

                            if (activeChannelIndex >= mixer.MaxChannels)
                                activeChannelIndex = 0;
                        }
                    }

                    // Sleep so we don't flood the mixer
                    Thread.Sleep(20);
                }
            }
        }