# A New Method for Detecting Onset and Offset for Singing in Real-Time and Offline Environments
## 3 The Proposed Algorithm
### Comparing the Current Slope with the Mean and Standard Deviation  
In this step, all the required information is prepared to determine if a significant change has occurred in the fundamental frequency trajectory.  
Each of the points in the pitch contour can have only one of the following statuses:  
1. Onset: this means the point is an onset.
2. Offset: this means the point is an offset.
3. StartTransition: this means a transition will follow, and this point is the start of the transition.
4. EndTransition: this means it is the end of the transition.
5. None: this means this point is neither an event's start nor the end.  
These statuses are illustrated in the diagram in Figure 4. The red and green lines show offset and onset events respectively, while the orange lines denote a transition from a note to the following note, i.e., the points between an offset and its subsequent onset.  
Figure 7 illustrates the algorithm for finding each point's status. This algorithm works based on the values calculated by the algorithm illustrated in Figure 5. This algorithm is run iteratively on each of the estimated pitch values.  
First, a _Threshold_ for the local pitch contour's slope must be calculated. This is completed by adding the mean of the local slopes at \(Point_{i}\) to the product of the standard deviation of the local slopes at \(Point_{i}\) and \(t\) coefficients. The \(t\) is a user-specified value that indicates which range of frequencies, based on their variation from the mean, should be considered as belonging to the same note. The value \(t\) does not define a fixed variation from the mean but is derived based on the singer's techniques. For instance, when the singer uses vibrato, the variation is higher than singing in an unmodulated tone. This study selected a threshold of 5 for the Erkomaishvili dataset and 2 for the SVNote1 dataset.  
Figure 7: The algorithm for finding a significant change to find onset, offset, and transition.  
Second, if the slope at \(Point_{i}\) is bigger than the _Threshold_, it means that a trajectory change has happened. This significant change should be an \(Onset\), \(Offset\), or \(StartTransition\). If it is the first trajectory change after a silence (see Branch B in Figure 7), it is a movement to reach an \(Onset\); otherwise (see Branch A in Figure 7), the current point is an \(Offset\). Based on each of these situations, Onset, Offset, StartTransition, and EndTransition statuses will be marked. The start and end of transitions are consecutively after and before an Offset and an Onset, respectively. In other words, the start and end of transitions are one point apart from the Offset and Onset points.  
When the algorithm finds a trajectory change at \(point_{i}\), all the events between \(Point_{i}\) and \(Point_{i+j}\) will be labeled; thus, the following point that needs to be checked is \(i+j+1\). Therefore, there is a jump with a size of \(j+1\) at the end of the algorithm to set the \(i\) value for the next iteration.  
In the beginning, the \(FirstTime\) variable is set to true, and when a rest is reached (when \(F0_{i}\) equals zero), a \(True\) value will be assigned to this variable.  
A full implementation of the algorithm has been released to provide all the details at [[https://github.com/BehnamFaghihMusicTech/Onset-Detection](https://github.com/BehnamFaghihMusicTech/Onset-Detection)]([https://github.com/BehnamFaghihMusicTech/Onset-Detection](https://github.com/BehnamFaghihMusicTech/Onset-Detection)), accessed on 15 July 2022.