/// ======================================================================================================= public static void WAV_STRETCH(string path, string factor) { byte[] alldata = File.ReadAllBytes(path); WAVFormat fmt = ReadFormat(alldata); double sampletofactor = Convert.ToDouble(factor); int startofdata = GetStartOfDataIndex(alldata); uint newdatasize = (uint)((double)alldata.Length * sampletofactor); long newfilesize = newdatasize + startofdata; byte[] newdata = new byte[newdatasize]; Array.Copy(alldata, 0, newdata, 0, startofdata); // copy header SetDataSize(ref newdata, startofdata, (int)newdatasize); SetRiffSize(ref newdata, (int)newfilesize - 8); for (int i = 0; i < newdatasize; i += (int)fmt.ByteDepth) { double newid = i / sampletofactor; int ni = (int)Math.Round(newid); int newi = ni / ((int)fmt.ByteDepth * 2); newi = newi * ((int)fmt.ByteDepth * 2); if (startofdata + newi < alldata.Length - 100) { int sample = Deserialise(alldata, startofdata + newi, (int)fmt.ByteDepth); Serialise(ref newdata, i + startofdata, (int)sample, (int)fmt.ByteDepth); } } File.WriteAllBytes(DISK.AutoIncrementFilename(path), newdata); }
/// ======================================================================================================= public static void WAV_CUT_ALL(string path, string start_offset_ms, string cut_size_ms, string shift_length_ms) { byte[] alldata = File.ReadAllBytes(path); WAVFormat fmt = ReadFormat(alldata); uint startoffset = ConvertMsToNumberOfBytes(Convert.ToUInt32(start_offset_ms), fmt); uint cutsize = ConvertMsToNumberOfBytes(Convert.ToUInt32(cut_size_ms), fmt); uint shiftlength = ConvertMsToNumberOfBytes(Convert.ToUInt32(shift_length_ms), fmt); int datasize; int headersize = GetStartOfDataIndex(alldata, out datasize); long newfilesize = cutsize + headersize; long newdatasize = cutsize; while (datasize > shiftlength && datasize > cutsize) // ensure reduced alldata is larger than cutsize and shiftlength { // Build newdata, as cutsize from startoffset GetStartOfDataIndex(alldata, out datasize); byte[] newdata = new byte[newfilesize]; Array.Copy(alldata, 0, newdata, 0, headersize); // copy header SetDataSize(ref newdata, headersize, (int)newdatasize); SetRiffSize(ref newdata, (int)newfilesize - 8); Array.Copy(alldata, headersize + startoffset, newdata, headersize, newdatasize); // copy wave data // Reduce alldata by shift length byte[] bufdata = new byte[alldata.Length - shiftlength]; Array.Copy(alldata, 0, bufdata, 0, headersize); // copy header Array.Copy(alldata, headersize + shiftlength, bufdata, headersize, bufdata.Length - headersize); // copy wave data from shiftlength alldata = bufdata; SetDataSize(ref alldata, headersize, bufdata.Length - headersize); SetRiffSize(ref newdata, bufdata.Length - 8); path = DISK.AutoIncrementFilename(path); File.WriteAllBytes(DISK.AutoIncrementFilename(path), newdata); } }
/// ======================================================================================================= public static void WAV_MAKE(string length_ms, string tone_path, string tone_note, string new_note, string output_folder, string decay_norm, string number_of_channels, string sample_rate, string byte_depth) { WAVFormat fmt = new WAVFormat(number_of_channels, sample_rate, byte_depth); uint length = Convert.ToUInt32(length_ms); byte[] newdata = CreateBlankWave(length, fmt); string[] alllines = File.ReadAllLines(tone_path); double tone_frequency = GetFrequency(tone_note); // Relative base frequency double new_frequency = GetFrequency(new_note); double decay = string.IsNullOrWhiteSpace(decay_norm) ? 0.99999 : Convert.ToDouble(decay_norm); foreach (string line in alllines) { string[] split = line.Split('\t'); AddFrequency(new_frequency * Convert.ToDouble(split[0]) / tone_frequency, ref newdata, fmt, Convert.ToDouble(split[1]), decay); } if (!Directory.Exists(output_folder)) { Directory.CreateDirectory(output_folder); } File.WriteAllBytes(DISK.AutoIncrementFilename(output_folder + @"\" + new_note + ".wav"), newdata); }
// FEATURES /// ======================================================================================================= public static void WAV_CUT(string path, string start_offset_ms, string cut_size_ms, string over_write) { byte[] alldata = File.ReadAllBytes(path); WAVFormat fmt = ReadFormat(alldata); bool overwrite = Convert.ToBoolean(over_write); uint startoffset = ConvertMsToNumberOfBytes(Convert.ToUInt32(start_offset_ms), fmt); uint cutsize = ConvertMsToNumberOfBytes(Convert.ToUInt32(cut_size_ms), fmt); int startofdata = GetStartOfDataIndex(alldata); int newfilesize = (int)cutsize + startofdata; int newdatasize = (int)cutsize; byte[] newdata = new byte[newfilesize]; Array.Copy(alldata, 0, newdata, 0, startofdata); // copy header SetDataSize(ref newdata, startofdata, newdatasize); SetRiffSize(ref newdata, newfilesize - 8); Array.Copy(alldata, startofdata + startoffset, newdata, startofdata, newdatasize); if (overwrite) { File.WriteAllBytes(path, newdata); } else { File.WriteAllBytes(DISK.AutoIncrementFilename(path), newdata); } }
/// ======================================================================================================= public static void WAV_TRIM(string path, string front_ms, string back_ms, string over_write) { byte[] alldata = File.ReadAllBytes(path); WAVFormat fmt = ReadFormat(alldata); bool overwrite = Convert.ToBoolean(over_write); uint front_trim_length = ConvertMsToNumberOfBytes(Convert.ToUInt32(front_ms), fmt); uint back_trim_length = ConvertMsToNumberOfBytes(Convert.ToUInt32(back_ms), fmt); int datasize; int headersize = GetStartOfDataIndex(alldata, out datasize); while (front_trim_length % fmt.ByteDepth != 0) { front_trim_length++; } long newfilesize = alldata.Length - front_trim_length - back_trim_length; long newdatasize = datasize - front_trim_length - back_trim_length; byte[] newdata = new byte[newfilesize]; Array.Copy(alldata, 0, newdata, 0, headersize); // copy header SetDataSize(ref newdata, headersize, (int)newdatasize); SetRiffSize(ref newdata, (int)newfilesize - 8); Array.Copy(alldata, headersize + front_trim_length, newdata, headersize, newdatasize); if (overwrite) { File.WriteAllBytes(path, newdata); } else { File.WriteAllBytes(DISK.AutoIncrementFilename(path), newdata); } }
/// ======================================================================================================= public static void WAV_VOL(string path, string vol_0_to_1) { byte[] alldata = File.ReadAllBytes(path); WAVFormat fmt = ReadFormat(alldata); int startofdata = GetStartOfDataIndex(alldata); for (int i = startofdata; i < alldata.Length - startofdata; i += (int)fmt.ByteDepth) { int sample = Deserialise(alldata, i, (int)fmt.ByteDepth); double temp = (double)sample * Convert.ToDouble(vol_0_to_1); Serialise(ref alldata, i, (int)temp, (int)fmt.ByteDepth); } File.WriteAllBytes(DISK.AutoIncrementFilename(path), alldata); }
// FEATURES /// ======================================================================================================= public static void WAV_LP_FILTER(string path, string cut_off_hz, string start_ms, string end_ms) { byte[] alldata = File.ReadAllBytes(path); double cutoff = Convert.ToDouble(cut_off_hz); int startofdata = GetStartOfDataIndex(alldata); WAVFormat fmt = ReadFormat(alldata); uint total = (uint)(alldata.Length - startofdata) / (fmt.NumberOfChannels * fmt.ByteDepth); uint start = 0; try { start = ConvertMsToNumberOfBytes(Convert.ToUInt32(start_ms), fmt) / (fmt.NumberOfChannels * fmt.ByteDepth); } catch { } uint end = total; try { end = ConvertMsToNumberOfBytes(Convert.ToUInt32(end_ms), fmt) / (fmt.NumberOfChannels * fmt.ByteDepth); } catch { } double[] left; double[] right; GetDoubleArrayFromBytes(alldata, out left, out right); double[] leftsection = new double[end - start]; double[] rightsection = new double[end - start]; Array.Copy(left, start, leftsection, 0, leftsection.Length); Array.Copy(right, start, rightsection, 0, rightsection.Length); LPFilter.Butterworth(ref leftsection, fmt.SampleRate, cutoff); LPFilter.Butterworth(ref rightsection, fmt.SampleRate, cutoff); Array.Copy(leftsection, 0, left, start, leftsection.Length); Array.Copy(rightsection, 0, right, start, rightsection.Length); double[,] allsamples = new double[fmt.NumberOfChannels, left.Length]; for (int i = 0; i < fmt.NumberOfChannels; i++) { for (int j = 0; j < left.Length; j++) { if (i == 0) { allsamples[i, j] = left[j]; } else { allsamples[i, j] = right[j]; } } } byte[] data = GetBytesFromDoubleArray(allsamples, fmt); Array.Copy(data, 0, alldata, startofdata, data.Length); File.WriteAllBytes(DISK.AutoIncrementFilename(path), alldata); }
/// ======================================================================================================= public static void WAV_SNIP(string path, string from_start_offset_ms, string from_end_offset_ms, string over_write) { byte[] alldata = File.ReadAllBytes(path); WAVFormat fmt = ReadFormat(alldata); bool overwrite = Convert.ToBoolean(over_write); uint start = Convert.ToUInt32(from_start_offset_ms); uint end = Convert.ToUInt32(from_end_offset_ms); if (overwrite) { File.WriteAllBytes(path, WavSnip(alldata, start, end)); } else { File.WriteAllBytes(DISK.AutoIncrementFilename(path), WavSnip(alldata, start, end)); } }
// FEATURES /// ======================================================================================================= public static void WAV_FADE(string file_path, string front_ms, string back_ms, string over_write) { byte[] alldata = File.ReadAllBytes(file_path); WAVFormat fmt = ReadFormat(alldata); uint front_fade_length = ConvertMsToNumberOfBytes(Convert.ToUInt32(front_ms), fmt); uint back_fade_length = ConvertMsToNumberOfBytes(Convert.ToUInt32(back_ms), fmt); bool overwrite = string.IsNullOrWhiteSpace(over_write) ? false : Convert.ToBoolean(over_write); int datasize; int headersize = GetStartOfDataIndex(alldata, out datasize); long endoffile = headersize + datasize; double plusser = front_fade_length == 0 ? 1.0 : 0.0; uint stepsize = fmt.ByteDepth; for (int i = headersize; i < endoffile - stepsize; i += (int)stepsize) { int sample = Deserialise(alldata, i, (int)fmt.ByteDepth); if (i < front_fade_length) { double temp = plusser * (double)sample; plusser = plusser + ((double)stepsize / front_fade_length); sample = (int)temp; Serialise(ref alldata, i, sample, (int)fmt.ByteDepth); } else if (i > endoffile - back_fade_length) { double temp = plusser * (double)sample; plusser = plusser - ((double)stepsize / back_fade_length); sample = (int)temp; Serialise(ref alldata, i, sample, (int)fmt.ByteDepth); } else if (i > front_fade_length) { plusser = 1.0; } } if (overwrite) { File.WriteAllBytes(file_path, alldata); } else { File.WriteAllBytes(DISK.AutoIncrementFilename(file_path), alldata); } }
public void timer1_Tick(object sender, EventArgs e) { float processorCpu = CPU.NextValue(); float memoriaRam = RAM.NextValue(); float logicalDisk = DISK.NextValue(); circularProgressBar1.Value = (int)processorCpu; circularProgressBar1.Text = string.Format("{0:0.00}%", processorCpu); circularProgressBar2.Value = (int)memoriaRam; circularProgressBar2.Text = string.Format("{0:0.00}%", memoriaRam); circularProgressBar3.Value = (int)logicalDisk; circularProgressBar3.Text = string.Format("{0:0.00}%", logicalDisk); if (memoriaRam >= 75.0 || processorCpu >= 75.0 || logicalDisk >= 90.0) { try { MySqlConnection objcon = new MySqlConnection("server=localhost; port=3306; User Id=root; database=dbProject; password=rkprado*"); objcon.Open(); Console.Write("Conectado ao Banco\n"); MySqlCommand cmdObj = new MySqlCommand("insert into tblrecursos(memoria_ram, cpu, disco, data_tmp) values (?, ?, ?,now());", objcon); cmdObj.Parameters.Add("@memoria_ram", MySqlDbType.Double).Value = memoriaRam; cmdObj.Parameters.Add("@cpu", MySqlDbType.Double).Value = processorCpu; cmdObj.Parameters.Add("@disco", MySqlDbType.Double).Value = logicalDisk; cmdObj.ExecuteNonQuery(); } catch { Console.Write("Não conectado!\n"); } /*if (logicalDisk >= 95) * { * Console.WriteLine("Disco: " + string.Format("{0:0.00}%", logicalDisk)); * } */ } }
/// ======================================================================================================= public static void WAV_VOL_STERIO(string path, string vol_left_0_to_1, string vol_right_0_to_1) { byte[] alldata = File.ReadAllBytes(path); WAVFormat fmt = ReadFormat(alldata); int startofdata = GetStartOfDataIndex(alldata); bool alternator = true; double volleft = Convert.ToDouble(vol_left_0_to_1); double volright = Convert.ToDouble(vol_right_0_to_1); for (int i = startofdata; i < alldata.Length - startofdata; i += (int)fmt.ByteDepth) { int sample = Deserialise(alldata, i, (int)fmt.ByteDepth); double vol = alternator ? volleft : volright; alternator = !alternator; double temp = (double)sample * vol; Serialise(ref alldata, i, (int)temp, (int)fmt.ByteDepth); } File.WriteAllBytes(DISK.AutoIncrementFilename(path), alldata); }
/// ======================================================================================================= public static API.Return GraphItems(string source_path, string output_folder_for_autofind, string timebase_ms) { int numlines = 1; string[] sourcelines = null; string sourcepath = DISK.AutoAddExt(source_path, ".txt"); // Auto add format extension if (File.Exists(sourcepath)) { sourcelines = File.ReadAllLines(sourcepath); int num = sourcelines.Length; while (num % 2 != 0) { num++; } numlines = (int)Math.Round(num / 2.0); // Rounds } List <ChartData> allcharts = new List <ChartData>(); List <CATMessage> msgs = new List <API.CATMessage>(); for (int line = 0; line < numlines * 2 - 1; line += 2) { List <API.ChartData> charts = new List <API.ChartData>(); string files = sourcelines[line].Split('\t')[0]; foreach (string filename in files.Split(';')) { string updatedfilename = AutoResolve(filename, output_folder_for_autofind); if (File.Exists(updatedfilename)) { charts.Add(WAV_GRAPH(updatedfilename, "", "", timebase_ms).Charts[0]); } else { msgs.Add(new CATMessage(updatedfilename + " could not be found", API.CAT.Status.FAIL)); } } foreach (ChartData chart in charts) { allcharts.Add(chart); } } return(new Return(allcharts, msgs)); }
/// ======================================================================================================= public static void WAV_STERIO_SWAP(string path) { byte[] alldata = File.ReadAllBytes(path); WAVFormat fmt = ReadFormat(alldata); if (fmt.NumberOfChannels == 2) { int startofdata = GetStartOfDataIndex(alldata); for (int i = startofdata; i < alldata.Length - startofdata - fmt.ByteDepth; i += (int)fmt.ByteDepth * 2) { int sampleleft = Deserialise(alldata, i, (int)fmt.ByteDepth); int sampleright = Deserialise(alldata, i + (int)fmt.ByteDepth, (int)fmt.ByteDepth); int newleft = sampleright; int newright = sampleleft; Serialise(ref alldata, i, (int)newleft, (int)fmt.ByteDepth); Serialise(ref alldata, i + (int)fmt.ByteDepth, (int)newright, (int)fmt.ByteDepth); } File.WriteAllBytes(DISK.AutoIncrementFilename(path), alldata); } }
// FEATURES /// ======================================================================================================= public static string WAV_REC(string output_path, string length_ms) { mciSendString("open new type waveaudio alias recsound", null, 0, 0); mciSendString("record recsound", null, 0, 0); int number = 0; try { StringBuilder newpath = new StringBuilder(DISK.AutoIncrementFilename(output_path)); number = Convert.ToInt16(length_ms); DateTime starttime = DateTime.Now; while (!CAT.AbortAll && (DateTime.Now - starttime).TotalMilliseconds < number) { Thread.Sleep(1); // 1 ms resolution } mciSendString("save recsound " + newpath, null, 0, 0); mciSendString("close recsound", newpath, 0, 0); return("Saved recording at " + newpath); } catch { return("Wave recording not saved"); } }
// FEATURES /// ======================================================================================================= public static void WAV_MAKE_ALL(string length_ms, string tone_path, string tone_note, string output_folder, string decay_norm, string number_of_channels, string sample_rate, string byte_depth) { WAVFormat fmt = new WAVFormat(number_of_channels, sample_rate, byte_depth); uint length = Convert.ToUInt32(length_ms); string[] alltonelines = File.ReadAllLines(tone_path); double tone_frequency = GetFrequency(tone_note); // Relative base frequency double decay = string.IsNullOrWhiteSpace(decay_norm) ? 0.99999 : Convert.ToDouble(decay_norm); for (int i = 1; i < 7; i++) // Above the 7th octave, the sound becomes too high pitched for music { foreach (KeyValuePair <string, double> note in Notes) { string new_note = note.Key + i; string filename = DISK.AutoAddBaseDirectory(output_folder + "\\" + new_note + ".wav"); if (!File.Exists(filename)) { byte[] newdata = CreateBlankWave(length, fmt); double new_frequency = GetFrequency(new_note); foreach (string line in alltonelines) { string[] split = line.Split('\t'); AddFrequency(new_frequency * Convert.ToDouble(split[0]) / tone_frequency, ref newdata, fmt, Convert.ToDouble(split[1]), decay); } if (!Directory.Exists(output_folder)) { Directory.CreateDirectory(output_folder); } File.WriteAllBytes(filename, newdata); } } } }
// FEATURES /// ======================================================================================================= public static void WAV_SHIFT_RIGHT(string path, string offset_ms) // This adds presence by offsetting right from left { byte[] alldata = File.ReadAllBytes(path); WAVFormat fmt = new WAVFormat(1, 96000, 3); // Use the highest quality possible uint offset = ConvertMsToNumberOfBytes(Convert.ToUInt32(offset_ms), fmt); while (offset % fmt.ByteDepth != 0) { offset++; } int startofdata = GetStartOfDataIndex(alldata); int datasize = (int)GetRiffSize(alldata) - startofdata; byte[] newdata = CreateBlankWave(ConvertNumberOfBytesToMs((uint)datasize, fmt), fmt); int newdatapos = 44; for (int alldatapos = startofdata; alldatapos < alldata.Length - (int)fmt.ByteDepth - (2 * offset); alldatapos += (int)(fmt.NumberOfChannels * fmt.ByteDepth)) { Serialise(ref newdata, newdatapos, Deserialise(alldata, alldatapos, (int)fmt.ByteDepth), (int)fmt.ByteDepth); Serialise(ref newdata, newdatapos + (int)fmt.ByteDepth, Deserialise(alldata, alldatapos + (int)fmt.ByteDepth + (int)offset, (int)fmt.ByteDepth), (int)fmt.ByteDepth); newdatapos += (int)(fmt.NumberOfChannels * fmt.ByteDepth); } File.WriteAllBytes(DISK.AutoIncrementFilename(path), newdata); }
/// ======================================================================================================= public static void WAV_TONE(string path, string output_path, string activation_amplitude_norm) { byte[] alldata = File.ReadAllBytes(path); double amplitude = Convert.ToDouble(activation_amplitude_norm) - 0.5; WAVFormat fmt = ReadFormat(alldata); List <List <Xy> > waves = GetWaves(alldata); List <List <Xy> > fft = null; if (fmt.NumberOfChannels == 2) { fft = GetWAVFFT(waves[0], waves[1], 3000); } else { fft = GetWAVFFT(waves[0], null, 3000); } string output_text = ""; foreach (Section section in WAV.GetActivatedFrequencies(fft, amplitude)) { output_text += section.X1 + "\t" + (section.Peak + 0.5) + "\n"; } File.WriteAllText(DISK.AutoIncrementFilename(output_path), output_text); }
/// ======================================================================================================= public static void WAV_DELAY(string path, string distance_ms, string fade_duration_ms, string fade_in_duration_ms, string fade_out_duration_ms, string delay_level_norm) { byte[] alldata = File.ReadAllBytes(path); WAVFormat fmt = ReadFormat(alldata); uint distance_length = ConvertMsToNumberOfBytes(Convert.ToUInt32(distance_ms), fmt); uint duration_length = ConvertMsToNumberOfBytes(Convert.ToUInt32(fade_duration_ms), fmt); uint fade_in_duration_length = ConvertMsToNumberOfBytes(Convert.ToUInt32(fade_in_duration_ms), fmt); uint fade_out_duration_length = ConvertMsToNumberOfBytes(Convert.ToUInt32(fade_out_duration_ms), fmt); double delaylevel = Convert.ToDouble(delay_level_norm); while (duration_length % fmt.ByteDepth != 0) { duration_length++; } while (distance_length % fmt.ByteDepth != 0) { distance_length++; } int datasize; int headersize = GetStartOfDataIndex(alldata, out datasize); int newdatasize = datasize + (int)(distance_length + duration_length); int newfilesize = headersize + newdatasize; byte[] newdata = new byte[newfilesize]; Array.Copy(alldata, 0, newdata, 0, alldata.Length); // copy everything SetDataSize(ref newdata, headersize, newdatasize); SetRiffSize(ref newdata, newfilesize - 8); long endoffile = headersize + datasize; uint bytedepth = fmt.ByteDepth; for (int i = headersize; i < endoffile - bytedepth; i += (int)distance_length) { double fade_factor = (double)bytedepth / (double)duration_length; double fade = delaylevel; double fade_out = 1.0; double fade_out_factor = (double)bytedepth / (double)fade_out_duration_length; double fade_in = 0.0; double fade_in_factor = (double)bytedepth / (double)fade_in_duration_length; for (int j = 0; j < duration_length - bytedepth; j += (int)bytedepth) { int location = j % (int)distance_length; if (i + location < endoffile) { int sample = Deserialise(alldata, i + location, (int)bytedepth); fade = fade > fade_factor ? fade - fade_factor : 0.0; sample = (int)((double)sample * fade); if (location < fade_in_duration_length) { fade_in += fade_in_factor; sample = (int)((double)sample * fade_in); } else if (location > distance_length - fade_out_duration_length) { fade_out -= fade_out_factor; sample = (int)((double)sample * fade_out); } else { fade_in = 0.0; fade_out = 1.0; } int newsample = Deserialise(newdata, i + j + (int)distance_length, (int)bytedepth); Serialise(ref newdata, i + j + (int)distance_length, ((newsample + sample / 2)), (int)bytedepth); } } } File.WriteAllBytes(DISK.AutoIncrementFilename(path), newdata); }
/// ======================================================================================================= public static Return BUILD(string source_path, string timebase_ms, string resolution, string output_folder) { CAT.InteractiveAddNamedOverlay("WAVBUILD", "Preparing for build of " + source_path); string sourcepath = DISK.AutoAddExt(source_path, ".txt"); string[] sourcelines = File.ReadAllLines(DISK.AutoAddBaseDirectory(sourcepath)); DateTime starttime = DateTime.Now; WAVFormat masterfmt = new WAVFormat(2, 96000, 3); // Ideal format double timebase = string.IsNullOrWhiteSpace(timebase_ms) ? 250 : Convert.ToDouble(timebase_ms); int res = string.IsNullOrWhiteSpace(resolution) ? 32 : Convert.ToInt16(resolution); int totaltimems = res == 1 ? Convert.ToInt32(timebase * res) : Convert.ToInt32(timebase * res) + 5000; // 5 seconds decay uint masterdatasize = ConvertMsToNumberOfBytes(Convert.ToUInt32(totaltimems), masterfmt); byte[] masteralldata = CreateBlankWave(Convert.ToUInt32(totaltimems), masterfmt); int masterstartofdata = GetStartOfDataIndex(masteralldata); uint masterlocation = 0; int rowcount = 0; int rowmax = sourcelines.Length / 2; CAT.InteractiveUpdateNamedOverlay("WAVBUILD", "Starting build of " + source_path); for (int linenumber = 0; linenumber < sourcelines.Length - 1; linenumber += 2) { rowcount++; string files; byte[] newalldata; double volumeleft = 1.0; double volumeright = 1.0; if (sourcelines[linenumber].Contains("\t")) { files = sourcelines[linenumber].Split('\t')[0]; try { string volstring = sourcelines[linenumber].Split('\t')[1]; if (volstring.Contains(";")) { volumeleft = Convert.ToDouble(volstring.Split(';')[0]); volumeright = Convert.ToDouble(volstring.Split(';')[1]); } else if (volstring.Contains(":")) { volumeleft = Convert.ToDouble(volstring.Split(':')[0]); volumeright = Convert.ToDouble(volstring.Split(':')[1]); } else { volumeleft = Convert.ToDouble(volstring); volumeright = Convert.ToDouble(volstring); } } catch { /* use default */ } } else { files = sourcelines[linenumber]; } int filecount = 0; foreach (string filename in files.Split(';')) { filecount++; string updatedfilename = filename; if (!filename.Contains(".wav")) { updatedfilename = filename + ".wav"; } // Auto add extension if (!File.Exists(updatedfilename)) { updatedfilename = output_folder + @"\" + updatedfilename; } // auto add output folder if (File.Exists(updatedfilename)) { newalldata = File.ReadAllBytes(updatedfilename); WAVFormat newfmt = ReadFormat(newalldata); int newstartofdata = GetStartOfDataIndex(newalldata); int newdatasize = newalldata.Length - newstartofdata; byte[] newdata = new byte[newdatasize]; Array.Copy(newalldata, newstartofdata, newdata, 0, newdatasize); // copy data - only data is used from new file (header is discarded) string argument = sourcelines[linenumber + 1]; if (!string.IsNullOrWhiteSpace(argument)) { string[] arguments = argument.Split(','); int locationcount = 0; foreach (string location in arguments) { locationcount++; uint locationtouse = Convert.ToUInt32(Convert.ToDouble(location)); if (location.Contains("+")) { masterlocation += locationtouse; } else { masterlocation = locationtouse; } string currentbuilditem = " (row:" + rowcount + @"/" + rowmax + " file:" + filecount + @"/" + files.Split(';').Length + " location:" + locationcount + @"/" + arguments.Length + ")"; CAT.InteractiveUpdateNamedOverlay("WAVBUILD", source_path + ": Adding " + updatedfilename + " at " + masterlocation + currentbuilditem, API.CAT.Status.PASS); uint masteroffset = ConvertMsToNumberOfBytes(masterlocation, masterfmt); bool isleft = true; // Scroll through each of the new data indeces for (int newindex = 0; newindex < newdatasize - (int)newfmt.ByteDepth; newindex += (int)(newfmt.NumberOfChannels * newfmt.ByteDepth)) { double masterkhz = ((ulong)masterfmt.ByteDepth * (ulong)masterfmt.NumberOfChannels * (ulong)masterfmt.SampleRate) / 1000.0; double newkhz = ((ulong)newfmt.ByteDepth * (ulong)newfmt.NumberOfChannels * (ulong)newfmt.SampleRate) / 1000.0; double ratio = masterkhz / newkhz; int masterindex = (int)(newindex * ratio); if (masterindex + masteroffset < masterdatasize) { // Scroll to (left channel) sample start byte while ((masterindex + masteroffset) % (masterfmt.NumberOfChannels * masterfmt.ByteDepth) != 0) { masterindex--; } for (int i = 0; i < newfmt.NumberOfChannels; i++) { for (int j = 0; j < (int)Math.Ceiling((double)(masterfmt.SampleRate / newfmt.SampleRate)); j++) // Fill in gaps { int mastersamplelocation = masterstartofdata + (int)masteroffset + masterindex + i * (int)masterfmt.ByteDepth + j * ((int)masterfmt.NumberOfChannels * (int)masterfmt.ByteDepth); int mastersample = Deserialise(masteralldata, mastersamplelocation, (int)masterfmt.ByteDepth); int newsample = Deserialise(newdata, newindex + i * (int)newfmt.ByteDepth, (int)newfmt.ByteDepth); double masternorm = Normalise(mastersample, (int)masterfmt.ByteDepth); double volume = isleft ? volumeleft : volumeright; isleft = !isleft; double combined = (masternorm + volume * Normalise(newsample, (int)newfmt.ByteDepth)); // Limiting proved highly effective if (combined > 0.499999) { combined = 0.499999; // returndata += "Clip (positive) detected at " + ConvertNumberOfBytesToMs((uint)j, newfmt); } if (combined < -0.499999) { combined = -0.499999; // returndata += "Clip (negative) detected at " + ConvertNumberOfBytesToMs((uint)j, newfmt); } Serialise(ref masteralldata, mastersamplelocation, Denormalise(combined, (int)masterfmt.ByteDepth), (int)masterfmt.ByteDepth); // Copy to both channels if (newfmt.NumberOfChannels == 1) { Serialise(ref masteralldata, masterstartofdata + (int)masteroffset + masterindex + (int)masterfmt.ByteDepth, Denormalise(combined, (int)masterfmt.ByteDepth), (int)masterfmt.ByteDepth); } } } } } } } else { CAT.InteractiveAddOverlay(updatedfilename + " ignored - no times specified", API.CAT.Status.WARN); } } else { CAT.InteractiveAddOverlay(updatedfilename + " does not exist... continued", API.CAT.Status.FAIL); } } } if (!Directory.Exists(output_folder)) { Directory.CreateDirectory(output_folder); CAT.InteractiveAddOverlay("Created " + output_folder + " directory", API.CAT.Status.WARN); } try { string outputfilename = output_folder + "\\" + Path.GetFileNameWithoutExtension(sourcepath) + ".wav"; File.WriteAllBytes(outputfilename, masteralldata); CAT.InteractiveAddOverlay("Built " + outputfilename, API.CAT.Status.PASS); CAT.InteractiveAddOverlay("Build time " + (DateTime.Now - starttime).TotalMilliseconds + "ms", API.CAT.Status.INFO); } catch (Exception e) { CAT.InteractiveAddOverlay("Failed build:\r\n" + API.CAT.ErrorMsg(e), API.CAT.Status.FAIL); } return(new Return("Done")); }
/// ======================================================================================================= public static string WAV_SHIFT(string path, string left_right_both, string start_ms, string end_ms, string forward_backward, string shift_ms, string over_write) { // Inputs byte[] alldata = File.ReadAllBytes(path); byte[] newdata = new byte[alldata.Length]; WAVFormat fmt = ReadFormat(alldata); uint start = ConvertMsToNumberOfBytes(Convert.ToUInt32(start_ms), fmt); uint end = ConvertMsToNumberOfBytes(Convert.ToUInt32(end_ms), fmt); uint shift = ConvertMsToNumberOfBytes(Convert.ToUInt32(shift_ms), fmt); bool overwrite = string.IsNullOrWhiteSpace(over_write) ? false : Convert.ToBoolean(over_write); // Copy header int startofdata = GetStartOfDataIndex(alldata); Array.Copy(alldata, 0, newdata, 0, startofdata); // Manage channel selection uint stepsize = fmt.ByteDepth; uint offset = 0; switch (left_right_both) { case "both": stepsize = fmt.ByteDepth * fmt.NumberOfChannels; break; case "right": offset = fmt.ByteDepth; break; case "left": break; default: return("left_right_both is invalid: " + left_right_both); } // Position markers to grid while (start % (fmt.ByteDepth * fmt.NumberOfChannels) != 0) { start++; } while (shift % (fmt.ByteDepth * fmt.NumberOfChannels) != 0) { shift++; } while (end % (fmt.ByteDepth * fmt.NumberOfChannels) != 0) { end++; } // Perform backward or forward shift if (forward_backward == "forward") { for (int i = startofdata + (int)start + (int)offset; i < startofdata + (int)end - shift; i += (int)stepsize) { // move by shift newdata[i + shift] = alldata[i]; } for (int i = startofdata + (int)start + (int)offset; i < startofdata + (int)start + (int)shift; i += (int)stepsize) { // set bytes to 0 newdata[i] = 0; } } else if (forward_backward == "backward") { for (int i = startofdata + (int)start + (int)offset; i < startofdata + (int)end; i += (int)stepsize) { // move by shift newdata[i - shift] = alldata[i]; } for (int i = startofdata + (int)end - (int)shift + (int)offset; i < startofdata + (int)end; i += (int)stepsize) { // set bytes to 0 newdata[i] = 0; } } else { return("forward_backward is invalid: " + forward_backward); } if (overwrite) { File.WriteAllBytes(path, newdata); } else { File.WriteAllBytes(DISK.AutoIncrementFilename(path), newdata); } return("Success"); }
public void AutoLoad() { Invoke((Action) delegate { Name = "CAT" + AutoAlias; DISK.CreateFolder(AutoFolder()); if (!File.Exists(AutoPath())) { AutoData.Rows.Clear(); } else { LoadAutoData(); } if (!File.Exists(BatchPath(0))) { BatchData.Rows.Clear(); } else /**/ } { BatchRowCount = BatchData.RowCount; GlobalResize(); SelectAutoRow(0); LoadSettings(); Text = AutoAlias + " " + autoVarsInput.Text; AddOverlay("Loaded Auto: " + AutoFolder(), API.CAT.Status.INFO); }); } public void LoadSettings() { Invoke((Action) delegate { loadAutomationMenuItem.DropDownItems.Clear(); string[] lines = File.ReadAllLines(SettingsPath()); int count = 0; foreach (string line in lines) { if (line.Split('\t')[0] != AutoAlias) { ToolStripItem menuitem = new ToolStripMenuItem(line.Split('\t')[0]) { Name = "BuiltMenuItem" + count++ }; loadAutomationMenuItem.DropDownItems.Add(menuitem); menuitem.Click += new EventHandler(loadAutomationMenuItem_Click); } else { string autovars = ""; if (line.Split('\t').Length > 1) { autovars = line.Split('\t')[1]; } autoVarsInput.Text = autovars; AddOverlay("Loaded Auto-Vars: " + autovars, API.CAT.Status.INFO); showReportMenuItem.Checked = !autoVarsInput.Text.Contains(API.CAT.COMMAND_MODE_NO_REPORT); } } ToolStripItem newmenuitem = new ToolStripMenuItem("New") { Name = NewLoadMenu }; loadAutomationMenuItem.DropDownItems.Add(newmenuitem); newmenuitem.Click += new EventHandler(loadAutomationMenuItem_Click); }); } public void AbortTasks() { Invoke((Action) delegate { API.CAT.AbortAll = true; }); } public void AbortCurrentTask() { Invoke((Action) delegate { API.CAT.AbortCurrent = true; }); } public void SetCommandTimeout(int to = 1 /*s*/, int?rowNumber = null) { Data.SetCell(ref BatchData, to.ToString(), (int)API.CAT.BatchFields.Timeout, rowNumber); }