/// ======================================================================================================= 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); }
/// ======================================================================================================= 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_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); } }
// 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); } }
// 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_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); }
/// ======================================================================================================= 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 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 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_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 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"); }