public virtual bool SelectFirst(double onset, ref EdfPlusAnnotation annotation) { _lastError = TalError.None; // TODO: Check if the standard Find method can be used // Perform binary search int indexLeft = -1; int indexRight = Count; lock (AnnotationsList) { while (indexRight - indexLeft > 1) { int index = (indexLeft + indexRight) / 2; if (this[index].Onset < onset) { indexLeft = index; } else { indexRight = index; } } } // IdxRight now contains Index of first occurance of AOnset or where it should be if (indexRight < Count) { annotation = this[indexRight]; return(true); } return(false); }
public int Add(int blockNr, double onset, double duration, string annotation, int annotationSignalNr) { _lastError = TalError.None; try { int result; EdfPlusAnnotation newAnnotation = new EdfPlusAnnotation(this, blockNr, onset, duration, annotation, annotationSignalNr); lock (AnnotationsList) { AnnotationsList.Add(newAnnotation); result = AnnotationsList.IndexOf(newAnnotation); } Block(blockNr).Modified = true; if (UpdateEnabled) { DoSortAnnotations(); } return(result); } catch { _lastError = TalError.AddToList; return(-1); } }
public virtual bool SelectLast(ref EdfPlusAnnotation annotation) { _lastError = TalError.None; if (Count > 0) { annotation = this[Count - 1]; return(true); } return(false); }
public bool ReadFromBuffer(ref short[] buffer, int bufferOffset, int bufferSize, int annotationSignalNr) { Debug.Assert(annotationSignalNr >= 0, TALConsts.AnnotationSignalNrError); _lastError = TalError.None; bool result = true; int p = bufferOffset; int q = bufferOffset + bufferSize / sizeof(short); StringBuilder s = new StringBuilder(); bool isFirst = true; int count = 0; while (result && (p < q)) { byte[] b = BitConverter.GetBytes(buffer[p]); if (b[0] == char.MinValue) { if (s.Length != 0) { result = SplitAndAdd(s.ToString(), annotationSignalNr, isFirst, count); count++; isFirst = false; s = new StringBuilder(); } } else { s.Append((char)b[0]); } if (b[1] == char.MinValue) { if (s.Length != 0) { result = SplitAndAdd(s.ToString(), annotationSignalNr, isFirst, count); count++; isFirst = false; s = new StringBuilder(); } } else { s.Append((char)b[1]); } p++; } if (s.ToString() != string.Empty) { result = false; AddToLastError(annotationSignalNr, string.Empty, TalError.InvalidFormat); } _modified = false; _dataReadFromBuffer = true; return(result); }
public int Add(double onset, double duration, string annotation, int annotationSignalNr) { _lastError = TalError.None; int index = GetBlockByOnset(onset); if (index >= 0) { return(Add(index, onset, duration, annotation, annotationSignalNr)); } _lastError = TalError.BlockNotFound; return(-1); }
protected virtual EdfPlusAnnotationDataBlock GetBlock(int index) { lock (BlocksList) { Debug.Assert(index >= 0, TALConsts.InvalidBlockIndex); _lastError = TalError.None; if (index < BlocksList.Count) { return(BlocksList[index]); } return(null); } }
public bool AddBlocks(int blockNr, double onset, int nrBlocks) { _lastError = TalError.None; Debug.Assert(blockNr >= 0, TALConsts.InvalidBlockIndex); Debug.Assert(onset >= 0, TALConsts.InvalidBlockOnset); Debug.Assert(BlockDuration > 0, TALConsts.InvalidBlockDuration); // Pre-add items to list to avoid multiple resizes of _blocksList DoSetBlocksListCapacity(blockNr + nrBlocks); bool result = true; int i = 0; while (result && (i < nrBlocks)) { result = AddBlock(blockNr + i, onset + i * BlockDuration); i++; } return(result); }
public virtual bool SelectPrev(ref EdfPlusAnnotation annotation) { _lastError = TalError.None; lock (AnnotationsList) { if (annotation != null) { int index = IndexOf(annotation); if (index > 0) { annotation = this[index - 1]; return(true); } } else { _lastError = TalError.StartOfList; } } return(false); }
public virtual bool SelectNext(ref EdfPlusAnnotation annotation) { _lastError = TalError.None; lock (AnnotationsList) { if (annotation != null) { int index = IndexOf(annotation); if (index < Count - 1) { annotation = this[index + 1]; return(true); } } else { _lastError = TalError.EndOfList; } } return(false); }
public bool RedistributeAnnotations() { _lastError = TalError.None; lock (AnnotationsList) { foreach (var annotation in this) { int index = GetBlockByOnset(annotation.Onset); if (index >= 0) { annotation.DataRecNr = GetBlockByOnset(Current.Onset); } else { _lastError = TalError.BlockNotFound; break; } } } return(_lastError == TalError.None); }
public bool AddBlock(int blockNr, double onset) { Debug.Assert(blockNr >= 0, TALConsts.InvalidBlockIndex); Debug.Assert(double.IsNaN(onset) || (onset >= 0), TALConsts.InvalidBlockOnset); _lastError = TalError.None; DoSetBlocksListCapacity(blockNr); while (blockNr >= BlocksList.Count) { BlocksList.Add(null); } if (BlocksList[blockNr] != null) { Block(blockNr).DataRecOnset = onset; } else { EdfPlusAnnotationDataBlock newBlock = new EdfPlusAnnotationDataBlock(this, blockNr, onset) { Modified = true }; BlocksList[blockNr] = newBlock; } return(Block(blockNr) != null); }
private int DoWriteToBuffer(ref short[] buffer, int bufferOffset, int bufferSize, int annotationSignalNr) { Debug.Assert(annotationSignalNr >= 0, TALConsts.AnnotationSignalNrError); _lastError = TalError.None; int result = 0; //byte[] byteBuffer = new byte[bufferSize - bufferOffset*2]; byte[] byteBuffer = new byte[bufferSize]; int p = 0; int q = byteBuffer.Length; //int p = bufferOffset; //int q = bufferOffset + bufferSize / sizeof(short); double lastDuration = 0; double lastOnset; StringBuilder s = new StringBuilder(); if (annotationSignalNr == 0) { s.AppendFormat("{0}{1}{2}{3}", (_dataRecOnset >= 0) ? "+" : "", _dataRecOnset.ToString(TALConsts.ciEnglishUS), (char)20, (char)20); lastOnset = DataRecOnset; } else { lastOnset = double.MinValue; } int i = 0; while (i < Owner.Count) { var annotation = Owner[i]; if ((annotation.DataRecNr == DataRecNr) && (annotation.AnnotationSignalNr == annotationSignalNr)) { if (annotation.Onset == lastOnset) { if (annotation.Duration == lastDuration) { s.Append(annotation.Annotation); s.Append((char)20); } else { if (s.Length > 0) { result += AddToBuffer(ref byteBuffer, annotationSignalNr, ref p, ref q, s.ToString()); } s = new StringBuilder(annotation.ToString()); lastOnset = annotation.Onset; lastDuration = annotation.Duration; } } else { if (s.Length > 0) { result += AddToBuffer(ref byteBuffer, annotationSignalNr, ref p, ref q, s.ToString()); } s = new StringBuilder(annotation.ToString()); lastOnset = annotation.Onset; lastDuration = annotation.Duration; } } i++; } if (s.Length > 0) { result += AddToBuffer(ref byteBuffer, annotationSignalNr, ref p, ref q, s.ToString()); } while (p < q) { byteBuffer[p] = 0; p++; } p = bufferOffset; for (int j = 0; j < byteBuffer.Length; j += 2) { buffer[p] = BitConverter.ToInt16(byteBuffer, j); p++; } return(result); }
public bool ReadFromBuffer(ref short[] buffer, int bufferOffset, int bufferSize, int annotationSignalNr) { Debug.Assert(annotationSignalNr >= 0, TALConsts.AnnotationSignalNrError); _lastError = TalError.None; bool result = true; int p = bufferOffset; int q = bufferOffset + bufferSize / sizeof(short); StringBuilder s = new StringBuilder(); bool isFirst = true; int count = 0; while (result && (p < q)) { byte[] b = BitConverter.GetBytes(buffer[p]); if (b[0] == char.MinValue) { if (s.Length != 0) { result = SplitAndAdd(s.ToString(), annotationSignalNr, isFirst, count); count++; isFirst = false; s = new StringBuilder(); } } else s.Append((char)b[0]); if (b[1] == char.MinValue) { if (s.Length != 0) { result = SplitAndAdd(s.ToString(), annotationSignalNr, isFirst, count); count++; isFirst = false; s = new StringBuilder(); } } else s.Append((char)b[1]); p++; } if (s.ToString() != string.Empty) { result = false; AddToLastError(annotationSignalNr, string.Empty, TalError.InvalidFormat); } _modified = false; _dataReadFromBuffer = true; return result; }
private static void AddToLastError(int annotationSignalNr, string annotationToAdd, TalError error) { }
private static void AddToLastError(int annotationSignalNr, string annotationToAdd, TalError error) { }
protected virtual int GetBlockCount() { _lastError = TalError.None; return(BlocksList.Count); }
private int DoWriteToBuffer(ref short[] buffer, int bufferOffset, int bufferSize, int annotationSignalNr) { Debug.Assert(annotationSignalNr >= 0, TALConsts.AnnotationSignalNrError); _lastError = TalError.None; int result = 0; //byte[] byteBuffer = new byte[bufferSize - bufferOffset*2]; byte[] byteBuffer = new byte[bufferSize]; int p = 0; int q = byteBuffer.Length; //int p = bufferOffset; //int q = bufferOffset + bufferSize / sizeof(short); double lastDuration = 0; double lastOnset; StringBuilder s = new StringBuilder(); if (annotationSignalNr == 0) { s.AppendFormat("{0}{1}{2}{3}", (_dataRecOnset >= 0) ? "+" : "", _dataRecOnset.ToString(TALConsts.ciEnglishUS), (char)20, (char)20); lastOnset = DataRecOnset; } else lastOnset = double.MinValue; int i = 0; while (i < Owner.Count) { var annotation = Owner[i]; if ((annotation.DataRecNr == DataRecNr) && (annotation.AnnotationSignalNr == annotationSignalNr)) { if (annotation.Onset == lastOnset) { if (annotation.Duration == lastDuration) { s.Append(annotation.Annotation); s.Append((char)20); } else { if (s.Length > 0) result += AddToBuffer(ref byteBuffer, annotationSignalNr, ref p, ref q, s.ToString()); s = new StringBuilder(annotation.ToString()); lastOnset = annotation.Onset; lastDuration = annotation.Duration; } } else { if (s.Length > 0) result += AddToBuffer(ref byteBuffer, annotationSignalNr, ref p, ref q, s.ToString()); s = new StringBuilder(annotation.ToString()); lastOnset = annotation.Onset; lastDuration = annotation.Duration; } } i++; } if (s.Length > 0) result += AddToBuffer(ref byteBuffer, annotationSignalNr, ref p, ref q, s.ToString()); while (p < q) { byteBuffer[p] = 0; p++; } p = bufferOffset; for (int j = 0; j < byteBuffer.Length; j += 2) { buffer[p] = BitConverter.ToInt16(byteBuffer, j); p++; } return result; }