// Utilities /// ======================================================================================================= static void WAVInteractivePlay(string input, string folder = "") { foreach (string filename in input.Split(';')) { string updatedfilename = filename; if (!File.Exists(updatedfilename) && !filename.Contains(".wav")) { updatedfilename = filename + ".wav"; } if (!File.Exists(updatedfilename)) { updatedfilename = folder + @"\" + updatedfilename; } bool exists = File.Exists(updatedfilename); CAT.InteractiveAddOverlay(updatedfilename + " exists:" + exists, exists ? API.CAT.Status.PASS : API.CAT.Status.FAIL); if (exists) { new Thread(() => WavPlay(updatedfilename)).Start(); } } }
/// ======================================================================================================= 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 Control COMPOSE(string source_path, string timebase_ms, string resolution, string output_folder) { int numlines = 1; string[] sourcelines = null; string sourcepath = API.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 } else { // Build Example to use File.WriteAllText(sourcepath, "Example1.wav\t1\r\n0," + timebase_ms + "\r\n" + output_folder + @"\Example2" + "\r\n0"); Thread.Sleep(1000); sourcelines = File.ReadAllLines(sourcepath); API.CAT.AddOverlay(API.CAT.InteractiveControlsFormName, "File did not exist so created example for " + sourcepath); } double timebasems = string.IsNullOrWhiteSpace(timebase_ms) ? 250 : Convert.ToDouble(timebase_ms); int res = string.IsNullOrWhiteSpace(resolution) ? 32 : Convert.ToInt16(resolution); string outputpath = output_folder + (output_folder.Last() == '\\' ? "" : @"\") + Path.GetFileNameWithoutExtension(sourcepath) + ".wav"; // Load data API.CAT.Status status = File.Exists(outputpath) ? API.CAT.Status.PASS : API.CAT.Status.FAIL; GroupBox gb = API.CAT.GetGroupBox("Source:" + sourcepath + " Timebase:" + timebasems + "ms Resolution:" + res + " Output:" + outputpath, status); // Main Menu int menuleft = 0; Label startpos = API.CAT.GetLabel("1", status, "StartPos", DockStyle.None, menuleft, MENU_TOP, MENU_ITEM_WIDTH, MENU_ITEM_HEIGHT, autosize: false); gb.Controls.Add(startpos); menuleft += MENU_ITEM_WIDTH; TrackBar starttb = CAT.GetTrackBar("1", 1, 1, res, "StartTrackBar", Orientation.Horizontal, CAT.Status.BUSY, DockStyle.None, menuleft, MENU_TOP, MENU_TRACKBAR_WIDTH, MENU_ITEM_HEIGHT); menuleft += MENU_TRACKBAR_WIDTH; starttb.ValueChanged += new EventHandler((object o, EventArgs e) => { CAT.InteractiveAddOverlay("Start position updated", API.CAT.Status.WARN); startpos.Text = starttb.Value.ToString(); }); gb.Controls.Add(starttb); Label endpos = API.CAT.GetLabel(res.ToString(), status, "EndPos", DockStyle.None, menuleft, MENU_TOP, MENU_ITEM_WIDTH, MENU_ITEM_HEIGHT, autosize: false); gb.Controls.Add(endpos); menuleft += MENU_ITEM_WIDTH; TrackBar endtb = CAT.GetTrackBar(res.ToString(), 1, res, res, "EndTrackBar", Orientation.Horizontal, CAT.Status.BUSY, DockStyle.None, menuleft, MENU_TOP, MENU_TRACKBAR_WIDTH, MENU_ITEM_HEIGHT); menuleft += MENU_TRACKBAR_WIDTH; endtb.ValueChanged += new EventHandler((object o, EventArgs e) => { CAT.InteractiveAddOverlay("End position updated", API.CAT.Status.WARN); endpos.Text = endtb.Value.ToString(); }); gb.Controls.Add(endtb); Label timebaser = API.CAT.GetLabel("1", status, "Timebaser", DockStyle.None, menuleft, MENU_TOP, MENU_ITEM_WIDTH, MENU_ITEM_HEIGHT, autosize: false); gb.Controls.Add(timebaser); menuleft += MENU_ITEM_WIDTH; TrackBar timebasertb = CAT.GetTrackBar("1", 1, 32, 64, "TimebaserTrackBar", Orientation.Horizontal, CAT.Status.BUSY, DockStyle.None, menuleft, MENU_TOP, MENU_TRACKBAR_WIDTH, MENU_ITEM_HEIGHT); menuleft += MENU_TRACKBAR_WIDTH; timebasertb.ValueChanged += new EventHandler((object o, EventArgs e) => { CAT.InteractiveAddOverlay("Timebase updated", API.CAT.Status.WARN); double value = timebasertb.Value < 32 ? ((double)(1.0 / (32.0 - timebasertb.Value + 1))) : (timebasertb.Value - 32.0 + 1.0); timebaser.Text = value.ToString(); }); gb.Controls.Add(timebasertb); Label scaler = API.CAT.GetLabel("1", status, "Scaler", DockStyle.None, menuleft, MENU_TOP, MENU_ITEM_WIDTH, MENU_ITEM_HEIGHT, autosize: false); gb.Controls.Add(scaler); menuleft += MENU_ITEM_WIDTH; TrackBar scalertb = CAT.GetTrackBar("1", 1, 32, 64, "ScalarTrackBar", Orientation.Horizontal, CAT.Status.BUSY, DockStyle.None, menuleft, MENU_TOP, MENU_TRACKBAR_WIDTH, MENU_ITEM_HEIGHT); menuleft += MENU_TRACKBAR_WIDTH; scalertb.ValueChanged += new EventHandler((object o, EventArgs e) => { CAT.InteractiveAddOverlay("Vertical scale updated", API.CAT.Status.WARN); double value = scalertb.Value < 32 ? ((double)(1.0 / (32.0 - scalertb.Value + 1))) : (scalertb.Value - 32.0 + 1.0); scaler.Text = value.ToString(); }); gb.Controls.Add(scalertb); AddItem(ref gb, "♫", () => WAVInteractivePlay(outputpath), status, ref menuleft); AddItem(ref gb, "۞", () => { BUILD(source_path, timebase_ms, resolution, output_folder); GroupBox newgb = (GroupBox)COMPOSE(source_path, timebase_ms, resolution, output_folder); CAT.InteractiveUpdate(gb.Name, newgb); }, status, ref menuleft); AddItem(ref gb, "<", () => Save(sourcepath, res, timebasems, gb, numlines), status, ref menuleft, true); AddItem(ref gb, "+", () => { Save(sourcepath, res, timebasems, gb, numlines + 1); GroupBox newgb = (GroupBox)COMPOSE(source_path, timebase_ms, resolution, output_folder); CAT.InteractiveUpdate(gb.Name, newgb); }, status, ref menuleft); AddItem(ref gb, "$", () => CAT.InteractiveShowReport(() => GraphItem(outputpath, "", (timebasems * Convert.ToDouble(timebaser.Text)).ToString(), (timebasems * Convert.ToDouble(timebaser.Text) * (Convert.ToInt16(startpos.Text) - 1)).ToString(), (timebasems * Convert.ToDouble(timebaser.Text) * Convert.ToInt16(endpos.Text)).ToString()), Convert.ToDouble(scaler.Text)), status, ref menuleft, true); AddItem(ref gb, "h", () => CAT.InteractiveShowReport(() => GraphItems(sourcepath, output_folder, (timebasems * Convert.ToDouble(timebaser.Text)).ToString()), Convert.ToDouble(scaler.Text)), status, ref menuleft, true); // Rows int row = 0; for (int line = 0; line < numlines * 2 - 1; line += 2) { Color rowcolor = LightColorMap[row % LightColorMap.Length]; row++; int rowtop = (row - 1) * ROW_HEIGHT + MENU_ITEM_HEIGHT + MENU_TOP + 2; int colpos = 0; string filestouse = sourcelines[line].Split('\t')[0]; string pathname = PathName(row); /*Play*/ AddRowButton(ref gb, "♪", "Play" + row, () => WAVInteractivePlay(((TextBox)gb.Controls.Find(pathname, true)[0]).Text, output_folder), rowcolor, ROW_PLAY_WIDTH, colpos, rowtop, false); colpos += ROW_PLAY_WIDTH; /*Path*/ gb.Controls.Add(API.CAT.GetTextBox(filestouse, PathName(row), rowcolor, height: ROW_HEIGHT, width: ROW_PATH_WIDTH, autosize: false, left: colpos, top: rowtop)); colpos += ROW_PATH_WIDTH; /*Vol*/ gb.Controls.Add(API.CAT.GetTextBox(sourcelines[line].Contains("\t") ? sourcelines[line].Split('\t')[1] : "", VolName(row), rowcolor, height: ROW_HEIGHT, width: ROW_VOL_WIDTH, autosize: false, left: colpos, top: rowtop)); colpos += ROW_VOL_WIDTH; // Row Delete Row Button btndelete = API.CAT.GetButton("3", "Delete" + row, forecolor: rowcolor, height: ROW_HEIGHT, width: ROW_DELETE_WIDTH, left: colpos, top: rowtop, symbol: true); btndelete.Click += new EventHandler((object o, EventArgs e) => { new Thread(() => { Save(sourcepath, res, timebasems, gb, numlines, Convert.ToUInt16(btndelete.Name.Substring(6))); CAT.InteractiveUpdate(gb.Name, (GroupBox)COMPOSE(source_path, timebase_ms, resolution, output_folder)); }).Start(); }); gb.Controls.Add(btndelete); colpos += ROW_DELETE_WIDTH; // Row Move Up Button btnup = API.CAT.GetButton("▲", "Up" + row, forecolor: rowcolor, height: ROW_HEIGHT, width: ROW_UP_WIDTH, left: colpos, top: rowtop); btnup.Click += new EventHandler((object o, EventArgs e) => { new Thread(() => { Save(sourcepath, res, timebasems, gb, numlines, Convert.ToUInt16(btnup.Name.Substring(2)), true); CAT.InteractiveUpdate(gb.Name, (GroupBox)COMPOSE(source_path, timebase_ms, resolution, output_folder)); }).Start(); }); gb.Controls.Add(btnup); colpos += ROW_UP_WIDTH; // Row Move Down Button btndown = API.CAT.GetButton("▼", "Down" + row, forecolor: rowcolor, height: ROW_HEIGHT, width: ROW_DOWN_WIDTH, left: colpos, top: rowtop); btndown.Click += new EventHandler((object o, EventArgs e) => { new Thread(() => { Save(sourcepath, res, timebasems, gb, numlines, Convert.ToUInt16(btndown.Name.Substring(4)), false); CAT.InteractiveUpdate(gb.Name, (GroupBox)COMPOSE(source_path, timebase_ms, resolution, output_folder)); }).Start(); }); gb.Controls.Add(btndown); colpos += ROW_DOWN_WIDTH; /*Row Graph*/ AddRowButton(ref gb, "h", "Graph" + row, () => CAT.InteractiveShowReport(() => GraphItem(((TextBox)gb.Controls.Find(pathname, true)[0]).Text, output_folder, (timebasems * Convert.ToDouble(timebaser.Text)).ToString(), (timebasems * (Convert.ToInt16(startpos.Text) - 1)).ToString(), (timebasems * Convert.ToInt16(endpos.Text)).ToString()), Convert.ToDouble(scaler.Text)), rowcolor, ROW_GRAPH_WIDTH, colpos, rowtop, true); colpos += ROW_GRAPH_WIDTH; /*Row Checkboxes*/ for (int col = 1; col <= res; col++) { gb.Controls.Add(API.CAT.GetCheckBox(rowcolor, col.ToString("00"), CheckBoxName(row, col), CheckState(sourcelines, line, col, timebasems, res), height: ROW_HEIGHT, width: ROW_CHECKBOX_WIDTH, autosize: false, left: colpos + 1, top: rowtop)); colpos += ROW_CHECKBOX_WIDTH + 2; } } return(gb); }
// Utilities /// ======================================================================================================= static string Save(string source_path, int res, double timebase, GroupBox gb, int rowcount, int?excluded_row = null, bool?up_down = null) { CAT.InteractiveAddOverlay("Saving " + source_path, API.CAT.Status.WARN); WriteModes mode = WriteModes.Normal; if (excluded_row != null && up_down != null && excluded_row != rowcount && !(bool)up_down) { mode = WriteModes.Down; } if (excluded_row != null && up_down != null && excluded_row != 1 && (bool)up_down) { mode = WriteModes.Up; } if (excluded_row != null && up_down == null) { mode = WriteModes.Exclude; } string newtext = ""; for (int row = 1; row <= rowcount; row++) { switch (mode) { case WriteModes.Normal: newtext += RowString(gb, row, res, timebase); break; case WriteModes.Exclude: if (row != excluded_row) { newtext += RowString(gb, row, res, timebase); } break; case WriteModes.Up: if (row == excluded_row - 1) { newtext += RowString(gb, row + 1, res, timebase); newtext += RowString(gb, row, res, timebase); row++; } else { newtext += RowString(gb, row, res, timebase); } break; case WriteModes.Down: if (row == excluded_row) { newtext += RowString(gb, row + 1, res, timebase); newtext += RowString(gb, row, res, timebase); row++; } else { newtext += RowString(gb, row, res, timebase); } break; } } try { if (!string.IsNullOrWhiteSpace(newtext)) { newtext = newtext.Remove(newtext.Length - 1, 1); File.WriteAllText(source_path, newtext); CAT.InteractiveAddOverlay("Saved " + source_path + " with data:\r\n" + newtext, API.CAT.Status.PASS); } } catch (Exception e) { CAT.InteractiveAddOverlay("Saving failed:\r\n" + API.CAT.ErrorMsg(e), API.CAT.Status.FAIL); } return(source_path); }