/// <summary> /// Clone current instance to a new one. /// </summary> /// <returns>A WaveSegment object.</returns> public WaveSegment Clone() { WaveSegment segment = new WaveSegment(); segment._startTime = _startTime; segment._endTime = _endTime; segment._label = _label; segment._confidence = _confidence; segment._averagePitch = _averagePitch; segment._pitchRange = _pitchRange; segment._rootMeanSquare = _rootMeanSquare; segment._tag = _tag; return segment; }
/// <summary> /// Load segment data from text reader stream. /// </summary> /// <param name="tr">Text reader to read segment from.</param> /// <param name="setting">Setting.</param> /// <returns>Wave segment collection.</returns> public static Collection<WaveSegment> ReadAllData(TextReader tr, SegmentSetting setting) { if (tr == null) { throw new ArgumentNullException("tr"); } if (setting == null) { throw new ArgumentNullException("setting"); } Collection<WaveSegment> segs = new Collection<WaveSegment>(); string line = null; while ((line = tr.ReadLine()) != null) { if (string.IsNullOrEmpty(line)) { continue; } if (line == ".") { // end of section break; } string[] items = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); if (items.Length < 2 && !setting.HasEndTime) { string message = string.Format(CultureInfo.InvariantCulture, "The normal segment line of alignment file shoud be (timestamp) (label) [confidence score]. But '{0}' is found.", line); throw new InvalidDataException(message); } if (items.Length < 3 && setting.HasEndTime) { string message = string.Format(CultureInfo.InvariantCulture, "The normal segment line of alignment file should be (timestamp) (timestamp) (label) [confidence score]. But '{0}' is found.", line); throw new InvalidDataException(message); } WaveSegment seg = new WaveSegment(); try { seg.StartTime = float.Parse(items[0], CultureInfo.InvariantCulture); if (!setting.HasEndTime) { if (items.Length == 3) { seg.Confidence = float.Parse(items[2], CultureInfo.InvariantCulture); } } else { seg.EndTime = float.Parse(items[1], CultureInfo.InvariantCulture); } } catch (FormatException) { string message = string.Format(CultureInfo.InvariantCulture, "Malformed line found as '{0}'", line); throw new InvalidDataException(message); } seg.Label = Phoneme.ToOffline(items[setting.HasEndTime ? 2 : 1]); segs.Add(seg); } RemoveDuplicatedSilence(segs); for (int i = 0; i < segs.Count - 1; i++) { if (!setting.HasEndTime) { segs[i].EndTime = segs[i + 1].StartTime; } else { if (segs[i].StartTime > segs[i].EndTime) { string message = string.Format(CultureInfo.InvariantCulture, "The start time of the {0}(th) segment [{1}] must not be later the end time of it.", i, segs[i].Label); throw new InvalidDataException(message); } } if (segs[i].StartTime > segs[i + 1].StartTime) { string message = string.Format(CultureInfo.InvariantCulture, "The start time of the {0}(th) segment [{1}] must not be later than the start time of the following segment [{2}].", i, segs[i].Label, segs[i + 1].Label); throw new InvalidDataException(message); } } return segs; }
/// <summary> /// Insert a new short pause phone in the given index. /// The duration of the short pause will be equally splitted from the left and right adjacent phones. /// </summary> /// <param name="index">The new short pause phone will be inserted into the position represented by 'index'.</param> public void InsertShortPauseSegment(int index) { // Can not insert short pause in the first/last position Debug.Assert(index > 0 && index < _waveSegments.Count - 1); const float SplittingRatio = 0.25f; WaveSegment newShortPauseSegment = new WaveSegment(); _waveSegments[index - 1].EndTime = _waveSegments[index - 1].StartTime + (_waveSegments[index - 1].Duration * (1 - SplittingRatio)); _waveSegments[index].StartTime = _waveSegments[index].EndTime - (_waveSegments[index].Duration * (1 - SplittingRatio)); newShortPauseSegment.StartTime = _waveSegments[index - 1].EndTime; newShortPauseSegment.EndTime = _waveSegments[index].StartTime; newShortPauseSegment.Label = Phoneme.ShortPausePhone; _waveSegments.Insert(index, newShortPauseSegment); }