Advertisement
EESweetieBot

RAWR

Mar 25th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.14 KB | None | 0 0
  1. /*
  2.  * Created by SharpDevelop.
  3.  * User: boota
  4.  * Date: 3/25/2017
  5.  * Time: 11:46 AM
  6.  *
  7.  * To change this template use Tools | Options | Coding | Edit Standard Headers.
  8.  */
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Text;
  12. using System.IO;
  13. using System.Linq;
  14. using NotITGSpectrum;
  15.  
  16. namespace NotITGSpectrum
  17. {
  18.     public class Spectrum {
  19.         // From:
  20.         // https://github.com/CristianChPuma/SBWriter/blob/master/Sbter/Spectrum.cs
  21.         // bcuz im nub and cant understand mlg 5/9 cod
  22.         private int stream, start, end, bpm, bars;
  23.         private string filename;
  24.        
  25.         public Spectrum(string filename, int bars, int startTime, int endTime, int bpm)
  26.         {
  27.             this.filename = filename;
  28.             this.bars = bars;
  29.             start = startTime;
  30.             end = endTime;
  31.             this.bpm = bpm;
  32.             BeginStream();
  33.         }
  34.        
  35.         List<SpectrumData> data = new List<SpectrumData>();
  36.  
  37.         List<string> values = new List<string>();
  38.         StringBuilder _fft = new StringBuilder();
  39.         List<double> fft_list = new List<double>();
  40.         public List<SpectrumData> GenerateData()
  41.         {
  42.  
  43.             for (int position = start; position < end; position += bpm / 4)
  44.             {
  45.                 Bass.BASS_ChannelSetPosition(stream, Bass.BASS_ChannelSeconds2Bytes(stream, position * 0.001), BASSMode.BASS_POS_BYTES);
  46.                 float[] fft = new float[2048];
  47.                 Bass.BASS_ChannelGetData(stream, fft, (int)BASSData.BASS_DATA_FFT4096);
  48.                 double[] fft_values = new double[bars];
  49.                 for (int i = 0; i < bars; i++)
  50.                 {
  51.                     fft_values[i]= Math.Round(double.Parse((((fft[i] * 2000 / 40) + 0.1).ToString()).Split('E')[0]),2);
  52.                 }
  53.  
  54.                 data.Add(new SpectrumData() {StartTime = position, EndTime = (position + (bpm / 4)), Frecuencies = fft_values });
  55.  
  56.                 _fft.Clear();
  57.             }    
  58.  
  59.             return data;
  60.  
  61.         }
  62.        
  63.         private void BeginStream()
  64.         {
  65.            
  66.             Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_UPDATEPERIOD, 0);
  67.             Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_3D, IntPtr.Zero);
  68.             stream = Bass.BASS_StreamCreateFile(filename, 0, 0, BASSFlag.BASS_STREAM_DECODE);
  69.         }
  70.        
  71.         public class SpectrumData
  72.         {
  73.         public int StartTime { get; set; }
  74.         public int EndTime { get; set; }
  75.         public string FFT { get; set; }
  76.         public double [] Frecuencies { get; set; }
  77.         public int Line { get; set; }
  78.         public int Bar { get; set; }
  79.         }
  80.     }
  81.     class Program
  82.     {
  83.         public static void Main(string[] args)
  84.         {
  85.             if (!Directory.Exists(Directory.GetCurrentDirectory() + @"\spectrums\")) {
  86.                 Directory.CreateDirectory(Directory.GetCurrentDirectory() + @"\spectrums\");
  87.             }
  88.             int bars = 50;
  89.             int st = 0;
  90.             int end = 64529;
  91.             Spectrum sp = new Spectrum(Directory.GetCurrentDirectory() + @"eggs.mp3", bars, st, end, 150);
  92.             List<Spectrum.SpectrumData> fft = sp.GenerateData();
  93.             Console.ReadKey();
  94.             for (int i = 0; i < bars; i++)
  95.             {
  96.                 List<object[]> asdf = new List<object[]>();
  97.                 // Going to edit this to ITG-friendly XML Cod
  98.                 for (int j = 0; j < fft.Count - 2; j++)
  99.                 {
  100.                     double value1 = fft[j].Frecuencies[i];
  101.                     double value2 = fft[j + 1].Frecuencies[i];
  102.                     /*values.Add(new Event {
  103.                         V = new ScaleVec { Start = fft[j].StartTime, End = fft[j].EndTime, Vxi = 0.5, Vyi = value1, Vxf = 0.5, Vyf = value2 }
  104.                    } );*/
  105.                     object[] thing =  {fft[j].StartTime - fft[j].EndTime,value2};
  106.                     asdf.Add(thing);
  107.                 }
  108.                 CreateBar(asdf,i);
  109.                 asdf.Clear();
  110.                 Write("Wrote Bar " + i);
  111.             }
  112.             Write("");
  113.             Write("[==]");
  114.             Write("");
  115.             Write("Completed! Press any key to exit.");
  116.             Console.ReadKey();
  117.         }
  118.         public static void CreateBar(List<object[]> things, int num) {
  119.             List<string> info = new List<string>() {"<ActorFrame><children>","<Layer","Type=\"Quad\"","InitCommand=\"hidden,1\"","OnCommand=\"%function(self)"};
  120.            
  121.             #region Cod
  122.             // Bar[i]Info = {
  123.             info.Add("Bar"+num+"Info = {");
  124.             //     Array Info
  125.             foreach(object[] o in things) {
  126.                 int off = int.Parse(o[1].ToString());
  127.                 int val = int.Parse(o[2].ToString());
  128.                 info.Add("{"+off+","+val+"},");
  129.             }
  130.             // }
  131.             info.Add("}");
  132.             #endregion
  133.            
  134.             info.Add("/>");
  135.             info.Add("</children></ActorFrame");
  136.            
  137.             if (!File.Exists(Directory.GetCurrentDirectory() + @"\spectrums\bar"+num+".xml")) {
  138.                 File.Create(Directory.GetCurrentDirectory() + @"\spectrums\bar"+num+".xml");
  139.             }
  140.            
  141.             using(StreamWriter sw = new StreamWriter(Directory.GetCurrentDirectory() + @"\bar"+num+".xml")) {
  142.                 foreach(string s in info) {
  143.                     sw.WriteLine(s);
  144.                 }
  145.             }
  146.         }
  147.         public static void Write(string msg) {
  148.             Console.WriteLine(msg);
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement