private void timer_Tick(object sender, System.EventArgs e) { if (channel != null) { FMOD.RESULT result; float[] spectrum = new float[SPECTRUMSIZE]; float dominanthz = 0; float max; int dominantnote = 0; int count = 0; result = channel.getSpectrum(spectrum, SPECTRUMSIZE, 0, FMOD.DSP_FFT_WINDOW.TRIANGLE); ERRCHECK(result); max = 0; for (count = 0; count < SPECTRUMSIZE; count++) { if (spectrum[count] > 0.01f && spectrum[count] > max) { max = spectrum[count]; bin = count; } } dominanthz = (float)bin * BINSIZE; /* dominant frequency min */ dominantnote = 0; for (count = 0; count < 120; count++) { if (dominanthz >= notefreq[count] && dominanthz < notefreq[count + 1]) { // which is it closer to. This note or the next note if (Math.Abs(dominanthz - notefreq[count]) < Math.Abs(dominanthz - notefreq[count + 1])) { dominantnote = count; } else { dominantnote = count + 1; } break; } } statusBar.Text = "Detected rate : " + dominanthz + " -> " + (((float)bin + 0.99f) * BINSIZE) + " Detected musical note : " + note[dominantnote] + " (" + notefreq[dominantnote] + ")"; } if (system != null) { system.update(); } }
protected override void OnPaint(PaintEventArgs e) { RectangleF srcRect = new RectangleF(0, y, this.Width, this.Height); float scrollY = this.Height / 2; /*( ( y + this.Height ) / ( img.Height + this.Height ) ) * this.Height;*/ int widSet = 8; int rightSet = this.Width - widSet - 4; int leftSet = widSet; int guideBarLen = 20; int guideBarWid = 1; int mainLen = 3; int mainWid = 3; int spectrumAccuracy = 64 * 1; float[] spectrum = new float[spectrumAccuracy]; FMOD.RESULT result = soundChannel.getSpectrum(spectrum, spectrumAccuracy, 0, FMOD.DSP_FFT_WINDOW.BLACKMAN); float avg = spectrum.Average(); avg *= 100; float[] bassSpec = spectrum.Take(spectrumAccuracy / 8).ToArray(); float bassAvg = bassSpec.Average(); bassAvg *= 25; Color baseColor = Color.LightBlue; int colorRange = 40; int rOff = rnd.Next(-colorRange, colorRange + 1); int gOff = rnd.Next(-colorRange, colorRange + 1); int bOff = rnd.Next(-colorRange, colorRange + 1); int newRed = Math.Min(baseColor.R + rOff, 255); int newGreen = Math.Min(baseColor.G + gOff, 255); int newBlue = Math.Min(baseColor.B + bOff, 255); baseColor = Color.FromArgb(newRed, newGreen, newBlue); Pen basePen = new Pen(baseColor, bassAvg); RectangleF scrollRect = new RectangleF(rightSet, scrollY, mainWid, mainLen); RectangleF topRect = new RectangleF(rightSet + ((scrollRect.Width / 2.0f) - (guideBarWid / 2.0f)), scrollY - guideBarLen, guideBarWid, guideBarLen); RectangleF bottomRect = new RectangleF(rightSet + ((scrollRect.Width / 2.0f) - (guideBarWid / 2.0f)), scrollY + scrollRect.Height, guideBarWid, guideBarLen); RectangleF scrollRect2 = new RectangleF(leftSet, scrollY, mainWid, mainLen); RectangleF topRect2 = new RectangleF(leftSet + ((scrollRect.Width / 2.0f) - (guideBarWid / 2.0f)), scrollY - guideBarLen, guideBarWid, guideBarLen); RectangleF bottomRect2 = new RectangleF(leftSet + ((scrollRect.Width / 2.0f) - (guideBarWid / 2.0f)), scrollY + scrollRect.Height, guideBarWid, guideBarLen); Color textColor = customSpectrum[specValue]; ColorMap[] map = new ColorMap[1]; map[0] = new ColorMap(); map[0].OldColor = Color.Magenta; map[0].NewColor = textColor; ImageAttributes attr = new ImageAttributes(); attr.SetRemapTable(map); e.Graphics.DrawImage(img, new Rectangle(0, 0, this.Width, this.Height), srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height, GraphicsUnit.Pixel, attr); PointF midRight = new PointF((scrollRect.X + (scrollRect.Width / 2.0f)), (scrollRect.Y + (scrollRect.Height / 2.0f))); e.Graphics.FillRectangle(Brushes.White, scrollRect); e.Graphics.FillRectangle(Brushes.WhiteSmoke, topRect); e.Graphics.FillRectangle(Brushes.WhiteSmoke, bottomRect); PointF midLeft = new PointF((scrollRect2.X + (scrollRect2.Width / 2.0f)), (scrollRect2.Y + (scrollRect2.Height / 2.0f))); e.Graphics.FillRectangle(Brushes.White, scrollRect2); e.Graphics.FillRectangle(Brushes.WhiteSmoke, topRect2); e.Graphics.FillRectangle(Brushes.WhiteSmoke, bottomRect2); if (avg > 1.4) { // lightning int subDivs = rnd.Next(2, 10); float subWid = this.Width / ( float )subDivs; float range = 20; // +-30 pixels from the origin if (rnd.Next(0, 2) != 0) { return; } lightningPoints.Clear(); lightningPoints.Add(midLeft); for (int i = 1; i < subDivs; i++) { float currX = midLeft.X; float currY = midLeft.Y; float yMin = currY - (range * avg); float yMax = currY + (range * avg); float pointY = rnd.Next(( int )yMin, ( int )yMax + 1); float xOffset = rnd.Next(-25, 26); PointF lightPoint = new PointF((subWid * i) + xOffset, pointY); lightningPoints.Add(lightPoint); } lightningPoints.Add(midRight); e.Graphics.DrawLines(basePen, lightningPoints.ToArray()); } else { if (rnd.Next(0, 3) != 0) { return; } // no connection, lets just spark int numSparks = rnd.Next(1, 4); float maxDist = rnd.Next(20, this.Width / 6); int direction = rnd.Next(-115, 116); maxDist *= avg; sparks.Clear(); for (int i = 0; i < numSparks; i++) { var spark = new List <PointF>(); float origX = midLeft.X; float origY = midLeft.Y; spark.Add(new PointF(origX, origY)); origX += maxDist * ( float )Math.Cos(DegreeToRadian(direction)); origY += maxDist * ( float )Math.Sin(DegreeToRadian(direction)); spark.Add(new PointF(origX, origY)); maxDist = rnd.Next(20, this.Width / 6); maxDist *= avg; direction = rnd.Next(-90, 91); origX += maxDist * ( float )Math.Cos(DegreeToRadian(direction)); origY += maxDist * ( float )Math.Sin(DegreeToRadian(direction)); spark.Add(new PointF(origX, origY)); sparks.Add(spark); } foreach (var spark in sparks) { e.Graphics.DrawLines(basePen, spark.ToArray()); } sparks.Clear(); for (int i = 0; i < numSparks; i++) { var spark = new List <PointF>(); float origX = midRight.X; float origY = midRight.Y; spark.Add(new PointF(origX, origY)); origX -= maxDist * ( float )Math.Cos(DegreeToRadian(direction)); origY -= maxDist * ( float )Math.Sin(DegreeToRadian(direction)); spark.Add(new PointF(origX, origY)); maxDist = rnd.Next(20, this.Width / 6); maxDist *= avg; direction = rnd.Next(-90, 91); origX -= maxDist * ( float )Math.Cos(DegreeToRadian(direction)); origY -= maxDist * ( float )Math.Sin(DegreeToRadian(direction)); spark.Add(new PointF(origX, origY)); sparks.Add(spark); } foreach (var spark in sparks) { e.Graphics.DrawLines(basePen, spark.ToArray()); } } }