static void RunSpeachFrequencyFilters(Vector it, int j, double vol = 0.01, int dbMin = -20, int dbMax = +20) { bool IsAudible(double f) { if ((f >= 8.1 && f <= 16743.9)) { return(true); } return(false); } bool pass = true; if (!IsAudible(it.Axis[j].Im)) { pass = false; } if (it.Axis[j].Re <= 0.01) { pass = false; } if (pass) { var n = Envelopes.MIDI2NOTE(Envelopes.FREQ2MIDI(it.Axis[j].Im)); if (string.IsNullOrWhiteSpace(n)) { pass = false; } if (n == null || (!n.Contains("3") && !n.Contains("4") && !n.Contains("5") && !n.Contains("6"))) { pass = false; } if (pass && it.Axis[j].Re > 0) { var dB = Envelopes.dB(it.Axis[j].Re); if (dB <= dbMin || dB >= dbMax) { pass = false; } } } if (!pass) { it.Axis[j].Re = 0; } }
static void RunSpeachFrequencyFilters(Vector vec, int j) { bool IsAudible(double f) { if ((f >= 8.1 && f <= 16743.9)) { return(true); } return(false); } if (!IsAudible(vec.Axis[j].Im)) { vec.Axis[j].Re = 0; } var n = Envelopes.MIDI2NOTE(Envelopes.FREQ2MIDI(vec.Axis[j].Im)); if (string.IsNullOrWhiteSpace(n) // || n.Contains("#") // || n.Contains("0") // || n.Contains("1") // || n.Contains("2") // || n.Contains("3") // || n.Contains("4") // || n.Contains("5") // || n == "C6" // || n == "E6" // || n == "D6" // || n == "A6" // || n == "F6" // || n == "G6" // || n == "B6" // || n.Contains("7") // || n.Contains("8") // || n.Contains("9") ) { vec.Axis[j].Re = 0; } }
public static void SaveMidi(Vector[] Model, string fmt, string outputFilePath, int offset, int len) { Console.Write($"\r\nSaving: {outputFilePath}...\r\n"); using (var stream = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write, FileShare.Read)) { int i = 0; string s; s = $"{fmt} " + $"⁞ {System.Ai.CBOW.DIMS}\r\n"; byte[] bytes = Encoding.UTF8.GetBytes(s); stream.Write(bytes, 0, bytes.Length); for (int sample = offset; sample < (offset + len); sample++) { Vector it = Model[sample]; if (it == null) { // s = "𝆕 ⁞ 0.00000±i0\r\n"; // bytes = Encoding.UTF8.GetBytes(s); // stream.Write( // bytes, // 0, // bytes.Length); // i++; } else { var line = new StringBuilder(); Complex[] axis = it.Axis; if (axis != null) { for (var j = 0; j < axis.Length; j++) { var n = Envelopes.MIDI2NOTE(j); if (string.IsNullOrWhiteSpace(n)) { n = axis[j].Im.ToString(); } if (axis[j].Re > 0) { if (line.Length > 0) { line.Append(" "); } var dB = Envelopes.dB(axis[j].Re); var sign = dB < 0 ? "-" : "+"; line.Append(n + sign + "i" + Math.Abs(dB).ToString()); } } } string score = it.Score.ToString(); if (line.Length > 0) { s = $"{it.Id} ⁞ {score} ⁞ {line.ToString()}\r\n"; } else { s = $"{it.Id} ⁞ {score}\r\n"; } bytes = Encoding.UTF8.GetBytes(s); stream.Write( bytes, 0, bytes.Length); i++; } } } Console.Write("\r\nReady!\r\n"); }
public static Matrix ShortTimeFourierTransform(Complex[] sig, Set filter, double vol = 0.01, int dbMin = -20, int dbMax = +20) { Console.Write($"\r\nBuilding a new Mel model...\r\n"); double norm = 0.0, cc = 0; var Model = new Matrix((int)Math.Ceiling(sig.Length / (double)2048)); foreach (var STFT in Complex.ShortTimeFourierTransform(sig)) { int i = Model.Count; if (i >= Model.Capacity) { throw new OutOfMemoryException(); } Vector it; Debug.Assert(Model[i] == null); Model[i] = it = MelFromFourier(STFT); for (var j = 0; j < it.Axis.Length; j++) { norm += it.Axis[j].Re; cc++; } } if (cc > 0) { norm = norm /= cc; if (norm > 0) { norm = 1d / norm; } } for (int i = 0; i < Model.Count; i++) { var it = Model[i]; if (it != null) { for (var j = 0; j < it.Axis.Length; j++) { it.Axis[j].Re = Math.Round(norm /* Note that this surface is really wierd... i.e. There are * positive and negative decibeles */ * it.Axis[j].Re / (it.Axis.Length * 0.1), 3); it.Axis[j].Re = it.Axis[j].Re; if (it.Axis[j].Re > 0) { RunSpeachFrequencyFilters(it, j, vol, dbMin, dbMax); } if (it.Axis[j].Re > 0 && filter != null) { /* Filter out the specified pitches */ var n = Envelopes.MIDI2NOTE(Envelopes.FREQ2MIDI(it.Axis[j].Im)); if (string.IsNullOrWhiteSpace(n) || filter.Has(n)) { it.Axis[j].Re = 0; } } double finalMag = Math.Round(it.Axis[j].Re, 2); it.Axis[j].Re = finalMag; } double dot = 0.0; for (var j = 0; j < it.Axis.Length; j++) { dot += it.Axis[j].Re * 1; } it.Score.Im = Math.Round( Tanh.f(dot), 2); } } return(Model); }