private void ExportSnippetButton_Click(object sender, EventArgs e) { try { // My programs never crash! xd // Get Save Path SaveFileDialog Dialog = new SaveFileDialog { Filter = "DTX SimFiles (*.dtx)|*.dtx|All files (*.*)|*.*" }; Dialog.ShowDialog(); // Generate Amplitude List List <float> AmplitudeList = new List <float>(); AudioFileReader Wave = new AudioFileReader(LoadFileTextBox.Text); float[] Buffer = new float[1024]; int SamplesRead = -1; while (SamplesRead != 0) { SamplesRead = Wave.Read(Buffer, 0, 1024); foreach (float Amplitude in Buffer) { AmplitudeList.Add(Amplitude); } } // Generate Beat Time List //List<double> BeatList = GenerateBeatList(AmplitudeList, Wave.WaveFormat); // Generate BPM List //List<double> BPMList = GenerateBPMList(BeatList); // Generate Beat Sample Index List List <int> BeatSampleIndexList = GenerateBeatSampleList(AmplitudeList, Wave.WaveFormat); // Generate BPM List from Beat Indexes List <double> BPMList = GenerateBPMListFromSamples(BeatSampleIndexList, Wave.WaveFormat); // Generate DTX Simfile DTXSimFile SimFile = new DTXSimFile { BPMList = BPMList, BeatsPerBar = double.Parse(BarLengthTextbox.Text), SaveFilePath = Dialog.FileName }; GenerateDTX(SimFile); } catch (Exception ex) { WriteConsoleLine(ex.Message, DateTime.Now.ToString("HH:mm:ss") + " Error"); } }
private void GenerateDTX(DTXSimFile SimFile) { StreamWriter Writer = new StreamWriter(SimFile.SaveFilePath); Writer.WriteLine("#00002: " + (SimFile.BeatsPerBar / 4) + " ;" + SimFile.BeatsPerBar + " beats per bar"); // Bar 0 Time Signature // Build BPM Table Dictionary <double, string> BPMLookup = new Dictionary <double, string>(); // <Tempo Value, BPM Chip ID> foreach (double BPM in SimFile.BPMList) { if (!BPMLookup.ContainsKey(BPM)) { string Index36 = Base10ToBase36(BPMLookup.Keys.Count + 1).PadLeft(2, '0'); BPMLookup[BPM] = Index36; Writer.WriteLine("#BPM" + Index36 + ": " + BPM); } } // Write DTX SimFile int Bar = 1; int PositionInBar = 1; // Always less than or equal to SimFile.BeatsPerBar string PreviousBPMChip = ""; int ChipCounter = 0; foreach (double BPM in SimFile.BPMList) { // Place BPM Chip if (PositionInBar == 1) { Writer.Write("#" + Bar.ToString("000") + "08: "); // Write BPM Chip prefix and bar number } if (PreviousBPMChip != BPMLookup[BPM] || (Bar == 1 && PositionInBar == 1)) { Writer.Write(BPMLookup[BPM]); // Write the actual BPM Chip here ChipCounter++; } else { Writer.Write("00"); // Don't put two of the same BPM chips in a row } PreviousBPMChip = BPMLookup[BPM]; // Move to next beat PositionInBar++; if (PositionInBar > SimFile.BeatsPerBar) // Move to next bar { Writer.Write('\n'); PositionInBar = 1; Bar++; } } WriteConsoleLine("Wrote " + ChipCounter + " BPM chips."); // Fill rest of last bar with null if (PositionInBar != 1) { while (PositionInBar <= SimFile.BeatsPerBar) { Writer.Write("00"); PositionInBar++; } Writer.Write('\n'); } Writer.Close(); WriteConsoleLine("Exported to " + SimFile.SaveFilePath); }