/// ======================================================================================================= public static void WAV_SNIP_ALL(string path, string activation_amplitude_norm, string activation_duration_ms, string activation_negative_offset_ms) { // o activation_amplitude_norm = 0.1 // o activation_duration_ms = 200 // o activation_negative_offset_ms = 100 byte[] alldata = File.ReadAllBytes(path); double amp = string.IsNullOrWhiteSpace(activation_amplitude_norm) ? 0.1 : Convert.ToDouble(activation_amplitude_norm); uint duration = string.IsNullOrWhiteSpace(activation_duration_ms) ? 200 : Convert.ToUInt32(activation_duration_ms); uint negativeoffsetms = string.IsNullOrWhiteSpace(activation_negative_offset_ms) ? 100 : Convert.ToUInt32(activation_negative_offset_ms); WavSnipper(path, WAV.GetActivatedSections(alldata, amp, duration, negativeoffsetms)); }
// Utilities /// ======================================================================================================= public static List <Section> GetActivatedSections(byte[] alldata, double activation_amplitude_norm, uint activation_duration_ms, uint activation_negative_offset_ms) { WAVFormat fmt = ReadFormat(alldata); uint duration = WAV.ConvertMsToNumberOfBytes(activation_duration_ms, fmt); int startofdata = GetStartOfDataIndex(alldata); int newfilesize = alldata.Length; List <Section> sections = new List <Section>(); int activator = 0; bool activated = false; double sectionstart = 0.0; double sectionpeak = 0.0; for (int i = startofdata; i < newfilesize - startofdata; i += (int)fmt.ByteDepth) { int sample = WAV.Deserialise(alldata, i, (int)fmt.ByteDepth); double norm_sample = WAV.Normalise(sample, (int)fmt.ByteDepth); if (Math.Abs(norm_sample) > activation_amplitude_norm) { activated = true; if (activator == 0) { sectionstart = WAV.ConvertNumberOfBytesToMs((uint)i, fmt); if (sectionstart > activation_negative_offset_ms) { sectionstart = sectionstart - activation_negative_offset_ms; } } sectionpeak = norm_sample > sectionpeak ? norm_sample : sectionpeak; activator = (int)duration; } else { if (activated && activator == 0) { uint sectionend = WAV.ConvertNumberOfBytesToMs((uint)i, fmt); sections.Add(new Section(sectionstart, sectionend, sectionpeak)); sectionpeak = 0.0; activated = false; } } activator = activator - (int)fmt.ByteDepth <= 0 ? 0 : activator - (int)fmt.ByteDepth; } if (activated) { sections.Add(new Section(sectionstart, WAV.ConvertNumberOfBytesToMs((uint)(newfilesize - startofdata), fmt), sectionpeak)); } return(sections); }
/// ======================================================================================================= 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 void SnipSelected(object sender, EventArgs e) { int number = Convert.ToInt32((sender as MenuItem).Text); WAV.WAV_SNIP(GraphControl.ChartGraph.Titles[0].Text, GraphControl.Sections[number].X1.ToString(), GraphControl.Sections[number].X2.ToString(), "True"); }
/// ======================================================================================================= public void SnipAll(object sender, EventArgs e) { WAV.WavSnipper(GraphControl.ChartGraph.Titles[0].Text, GraphControl.Sections); }
// Simultaneously graph time and frequency data /// ======================================================================================================= public static API.Return WAV_SCOPE(string paths, string start_ms, string end_ms, string timebase_ms, string max_frequency) { List <API.ChartData> allcharts = new List <API.ChartData> { }; List <CATMessage> allmsgs = new List <CATMessage>(); foreach (string p in paths.Split(';')) { int reduction = 20; // Note this is adequate for reporting and fourier transforms string path = API.DISK.AutoAddExt(p, ".wav"); if (File.Exists(path)) { byte[] header = new byte[1024]; FileStream fs = new FileStream(path, FileMode.Open); fs.Read(header, 0, header.Length); WAVFormat fmt = WAV.ReadFormat(header); int startofdata = GetStartOfDataIndex(header); // Cut uint start = string.IsNullOrWhiteSpace(start_ms) ? 0 : ConvertMsToNumberOfBytes(Convert.ToUInt32(start_ms), fmt); uint end = string.IsNullOrWhiteSpace(end_ms) ? fmt.Length : ConvertMsToNumberOfBytes((Convert.ToUInt32(end_ms)), fmt); uint timebase = string.IsNullOrWhiteSpace(timebase_ms) ? 0 : Convert.ToUInt32(timebase_ms); int maxfrequency = string.IsNullOrWhiteSpace(max_frequency) ? 1000 : Convert.ToInt32(max_frequency); byte[] alldata = new byte[end - start]; fs.Read(alldata, (int)start + startofdata, (int)(end - start - startofdata)); fs.Close(); // Create time wave List <List <API.Xy> > timewaves = BytesToWave(alldata, fmt, reduction); if (timebase == 0) { allcharts.Add(new API.ChartData(timewaves, new API.TimeData(ConvertNumberOfBytesToMs(end - start, fmt)), path)); } else { double sample_rate = fmt.SampleRate / reduction; int samplespersnippet = (int)(timebase * sample_rate / 1000); int numbersnippets = timewaves[0].Count / samplespersnippet; for (int i = 0; i < numbersnippets; i++) { List <API.Xy> subsetleft = timewaves[0].GetRange(i * samplespersnippet, samplespersnippet); if (fmt.NumberOfChannels == 2) { List <API.Xy> subsetright = timewaves[1].GetRange(i * samplespersnippet, samplespersnippet); List <List <API.Xy> > subset = new List <List <API.Xy> >() { subsetleft, subsetright }; allcharts.Add(new API.ChartData(subset, new API.TimeData(timebase))); List <List <API.Xy> > frequency = new List <List <API.Xy> >(); frequency = GetWAVDFT(subsetleft, subsetright, maxfrequency); allcharts.Add(new API.ChartData(frequency, new API.FrequencyData(timebase))); } else { allcharts.Add(new API.ChartData(new List <List <API.Xy> >() { subsetleft }, new API.TimeData(timebase))); List <List <API.Xy> > frequency = new List <List <API.Xy> >(); frequency = GetWAVDFT(subsetleft, null, maxfrequency); allcharts.Add(new API.ChartData(frequency, new API.FrequencyData(timebase))); } } } fs.Close(); } else { allmsgs.Add(new CATMessage(path + " does not exist", CAT.Status.FAIL)); } } return(new API.Return(allcharts, allmsgs)); }