コード例 #1
0
    public void Synth(string filename)
    {
        var landmarks = Finder.EnumerateAllLandmarks().ToArray();

        var stripeCount = 1 + landmarks.Max(l => l.StripeIndex);
        var wave        = new float[stripeCount * Analysis.CHUNK_SIZE];

        foreach (var l in landmarks)
        {
            var startSample = Analysis.CHUNK_SIZE * (l.StripeIndex - 1);
            var endSample   = startSample + 2 * Analysis.CHUNK_SIZE;

            for (var t = startSample; t < endSample; t++)
            {
                wave[t] += MathF.Sin(2 * MathF.PI * Analysis.BinToFreq(l.InterpolatedBin) * t / Analysis.SAMPLE_RATE)
                           * MathF.Exp(l.InterpolatedLogMagnitude / UInt16.MaxValue)
                           * Envelope[t - startSample];
            }
        }

        var maxSample = wave.Max(s => Math.Abs(s));

        var bytes = new List <byte>(2 * wave.Length);

        foreach (var s in wave)
        {
            bytes.AddRange(BitConverter.GetBytes(Convert.ToInt16(Int16.MaxValue * s / maxSample)));
        }

        File.WriteAllBytes(filename, bytes.ToArray());
    }
コード例 #2
0
    public void Paint(string filename)
    {
        var w     = Analysis.StripeCount;
        var h     = Analysis.BIN_COUNT;
        var gamma = 0.25f;
        var maxMagnitudeSquared = Analysis.FindMaxMagnitudeSquared();

        using (var bitmap = new Bitmap(w, h)) {
            for (var x = 0; x < w; x++)
            {
                for (var y = 0; y < h; y++)
                {
                    var magnitudeSquared = Analysis.GetMagnitudeSquared(x, y);
                    var shade            = Convert.ToByte(255 * MathF.Pow(magnitudeSquared / maxMagnitudeSquared, gamma / 2));
                    bitmap.SetPixel(x, h - y - 1, Color.FromArgb(shade, shade, shade));
                }
            }

            using (var g = Graphics.FromImage(bitmap)) {
                g.SmoothingMode = SmoothingMode.AntiAlias;
                foreach (var l in Finder.EnumerateAllLandmarks())
                {
                    var radius = 8 * l.InterpolatedLogMagnitude / UInt16.MaxValue;
                    var cx     = l.StripeIndex;
                    var cy     = h - l.InterpolatedBin - 1;
                    g.FillEllipse(Brushes.Yellow, cx - radius, cy - radius, 2 * radius, 2 * radius);
                }
            }

            bitmap.Save(filename);
        }
    }