Пример #1
0
        static void Test_WaveWriter()
        {
            var format = new WaveFormat {
                bytesPerSample = 2,
                sampleRate     = 44100,
                channels       = 1,
            };

            byte[] buffer = new byte[format.bytesPerSample * format.sampleRate]; // for 1 sec

            string file = "test1.wav";

            using (var w = new WaveWriter(format, file))
            {
#if false
                WaveFormat.Clear(buffer);
                w.Write(buffer);
#else
                var provider = new PartialProvider();
                provider.Initialize(format);

                //provider.AddFrequency(440.0, 1500, 0.5f); // without envelope
                provider.AddPartial(440, 100, 2000, 0.5f, -4f);
                provider.FlushPartials();

                while (!provider.IsEmpty())
                {
                    provider.Fill(buffer);
                    w.Write(buffer);
                }
#endif
            }
        }
Пример #2
0
        static void Test_PartialProvider_DoS()
        {
            var format = new WaveFormat {
                bytesPerSample =
                    2,
                //1, // performing same as 16
                sampleRate =
                    44100,
                //22050,
                channels = 1
            };

            using (var engine = new WaveEngine(format, bufferLengthMs: 60))
            {
                var partialProvider = new PartialProvider();

                engine.SetSampleProvider(partialProvider); // Init

                engine.Play();

                int i = 0;
                while (engine.IsPlaying())
                {
                    double freqHz = 440.0 + (i++) * 10;
                    partialProvider.AddPartial(freqHz, 100, 60000, 0.05f, -2f);
                    partialProvider.FlushPartials();
                    Thread.Sleep(50);
                }

                Debug.WriteLine("Ending. Provider status: {0}", (object)partialProvider.FormatStatus());
            }
        }
Пример #3
0
 static void AddNote(PartialProvider pp, double cents, Partial[] partials)
 {
     foreach (Partial p in partials)
     {
         double c     = cents + p.rational.ToCents();
         double hz    = Partials.CentsToHz(c);
         double level = Math.Pow(p.harmonicity, 7.0f);
         pp.AddPartial(
             hz,
             10, (int)(2000 * p.harmonicity),
             (float)(level / partials.Length),
             -4f
             );
     }
     pp.FlushPartials();
 }
Пример #4
0
        static void AddNote(PartialProvider pp, Rational r0)
        {
            string[] rs = new string[] { "1", "2", "3", "4", "81/16", "6" };
            float [] ls = new float [] { 1f, .04f, .8f, .08f, .8f, .1f };

            double c0 = r0.ToCents();

            for (int i = 0; i < rs.Length; ++i)
            {
                Rational r  = Rational.Parse(rs[i]);
                double   c  = c0 + r.ToCents();
                double   hz = Partials.CentsToHz(c);
                pp.AddPartial(
                    hz,
                    10, (int)(2000 * ls[i]),
                    ls[i] * .1f,
                    -4f
                    );
            }
            pp.FlushPartials();
        }
Пример #5
0
        static void Test_PartialProvider()
        {
            var format = new WaveFormat {
                bytesPerSample = 2,
                sampleRate     = 44100,
                channels       = 1
            };

            using (var engine = new WaveEngine(format, bufferLengthMs: 60))
            {
                var partialProvider = new PartialProvider();

                engine.SetSampleProvider(partialProvider); // Initialize called

                partialProvider.AddFrequency(440.0 * 2, 2000, 0.5f);
                //partialProvider.AddPartial(440.0 * 2, 100, 2000, 0.5f, -2f);
                partialProvider.FlushPartials();

                engine.Play();
                Thread.Sleep(3000);

                Debug.WriteLine("Ending. Provider status: {0}", (object)partialProvider.FormatStatus());
            }
        }
Пример #6
0
        static void Test_PartialProvider_Piano()
        {
            var format = new WaveFormat {
                bytesPerSample = 2,
                sampleRate     = 44100,
                //sampleRate = 22050,
                channels = 1,
            };

            string harmonicity = "Barlow";

            Rational[] subgroup = Rational.ParseRationals("2.3.5.11");
            Partial[]  partials = MakePartials(harmonicity, subgroup, 15);

            Debug.WriteLine("Subgroup {0}", Rational.FormatRationals(subgroup));
            foreach (Partial p in partials)
            {
                Debug.WriteLine("Partial {0} harm: {1}", p.rational, p.harmonicity);
            }


            using (var engine = new WaveEngine(format, bufferLengthMs: 30, restartOnFailure: true))
            {
                var partialProvider = new PartialProvider();

                engine.SetSampleProvider(partialProvider);
                engine.Play();

                Console.WriteLine("1-9 to play note\nEsc to exit");

                while (true)
                {
                    bool playing = true;
                    while (true)
                    {
                        playing = engine.IsPlaying();
                        if (!playing)
                        {
                            break;
                        }
                        if (Console.KeyAvailable)
                        {
                            break;
                        }
                        Thread.Sleep(1); // sleep here
                    }
                    if (!playing)
                    {
                        break;
                    }
                    var k = Console.ReadKey(true);
                    if (k.Key == ConsoleKey.Escape)
                    {
                        break; // engine stopped on dispose
                    }
                    else if (ConsoleKey.D1 <= k.Key && k.Key <= ConsoleKey.D9)
                    {
                        int n = (int)k.Key - (int)ConsoleKey.D1 + 1; // 1..9
                        var r = new Rational(1 + n, 2);              // 2/2, 3/2, 4/2, 5/2,..
                        if (k.Modifiers.HasFlag(ConsoleModifiers.Shift))
                        {
                            r *= 4;
                        }
                        if (k.Modifiers.HasFlag(ConsoleModifiers.Control))
                        {
                            r /= 4;
                        }
                        //AddNote(partialProvider, r);
                        AddNote(partialProvider, r.ToCents(), partials);
                    }
                }

                Debug.WriteLine("Ending. Provider status: {0}", (object)partialProvider.FormatStatus());
            }
        }