/// <summary> /// Converts audio data into a binary format by squaring off the data. /// </summary> /// /// <param name="data">The data to convert.</param> /// <returns>The converted data.</returns> public BinaryData ConvertToSquare(SoundData data) { Int16[] sample = new Int16[4]; List<bool> binData = new List<bool>(); IEnumerator<Int16> audio = data.GetEnumerator(); bool end = false; while (true) { for (int i = 0; i < 4; ++i) { if (audio.MoveNext()) { sample[i] = audio.Current; } else { end = true; break; } } if (end) { break; } bool[] sizes = NormalizeSample(audio, sample); // After all that, all we really need is the third value. binData.Add(sizes[2]); } return new BinaryData(binData); }
public void TestEnumerator() { Int16[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; SoundData audio = new SoundData(new List<Int16>(data)); IEnumerator<Int16> ie = audio.GetEnumerator(); for (int i = 0; i < data.Length; ++i) { ie.MoveNext(); Assert.AreEqual(data[i], ie.Current, "Enumerator values."); } }
public CassetteData[] ChunkData(SoundData data) { IEnumerator<Int16> ie = data.GetEnumerator(); float average = 0; int count = 0; ie.MoveNext(); List<CassetteData> chunks = new List<CassetteData>(); MetaData meta = null; do { Int16 level = ie.Current, abs = Math.Abs(level); int rate; // Eat a little first to even out the average, then start checking. if (count >= IgnoreHead) { if (level > 5 * average && (rate = FindLeader(ie)) > 0) { if (meta == null) { MetaParser parser = new MetaParser(ie, rate); meta = (MetaData) parser.Parse(); } else { ProgramParser parser = new ProgramParser(ie, rate, meta.ProgramSize); ProgramData program = (ProgramData) parser.Parse(); chunks.Add(new CassetteData(meta, program)); meta = null; } rate = count = 0; } } // Don't let data or spikes screw up our noise muffler. if (count < IgnoreHead || !(abs > 5 * average)) { average = (average * count + abs) / ++count; } else { count++; } } while (ie.MoveNext()); CassetteData[] output = new CassetteData[chunks.Count]; for (int i = 0; i < output.Length; ++i) { output[i] = chunks[i]; } return output; }