# Automatic Piano Harmony Arrangement System Based on Deep Learning

Authors: Jianhua Li
Journal: Journal of Sensors
 (2022)

---

## Abstract

Harmony, which plays an important role in enriching melody expression, is a combination of multiple notes. Melody coordination involves adding harmony effect to a single note of melody, which involves professional knowledge of basic music theory and harmony rules, and requires a high technical threshold. Under the macro background of deep learning and neural network technology, artificial intelligence is widely used in music retrieval, music creation, and music teaching. In this article, we provide a powerful tool for piano music creation by manually arranging melody and harmony instead of using deep learning. In this paper, harmonic elimination is divided into three subtasks: note detection, measurement, and multifundamental frequency estimation and model training. The music signal is divided into several segments by note detection, and the main notes and harmonic components of each segment are extracted by multifundamental frequency estimation, which are used as the features and labels of the neural network, so as to give a model with the ability of arrangement and harmony.


---

## Body

## 1. Introduction

Music is a kind of art in which sound is a form of expression, which can express human emotions, regulate emotions, and promote exchange of ideas and culture, and is an indispensable part of people’s daily life [1]. Harmony is a combination of multiple tones, which can effectively enhance expressive and infectious power of music, and plays an important role in musical accompaniment, including vertical structure and horizontal structure [2]. The piano is a multimusical instrument that produces harmonic effects by pressing several keys at the same time, and because of its wide range and many playing techniques, piano is known as “king of musical instruments” and occupies an important position in music performance [3]. Because of the importance of harmony in music, harmonic arrangement is a crucial step in music composition, as a single melody without harmony cannot express musical image well. Harmonization for the main melody is based on the relationship between tones before and after melody, adding harmonic effects to certain single tones in melody; this work involves basic music theory, harmonic theory, and other professional knowledge, which not only requires staff to have a solid basic knowledge of music theory but also needs to have the ability to think and perceive harmonic sound effects, which is a high technical threshold for ordinary people or amateurs. Even those who have received musical training may not be proficient in applying harmonic theory to actual music performance [4–6].In the period of classical music school, the main tone music became the main system. The simple and clear characteristics of the ideological content and structural form pursued at that time were also reflected in the simplicity of harmonic techniques. The major and minor system became the basis of harmony, and the medieval mode lost its influence. The tonal meaning of harmony is more clear and concentrated, emphasizing the three main chords of master, subordinate, and subordinate. Digital bass is no longer used in creation, and bass has also got rid of the shackles of flowing lines. Due to the squareness of the structure and the absence of the complicated voice parts and rhythms in polyphonic music, the rhythm of harmony is regular and rhythmic, with symmetrical and balanced harmony as the main body. Out of tune, shifting mode forward, decreasing seventh chord, increasing sixth chord, and contraposition with major and minor tones are all widely used. We began to apply the harmonic major mode and applied the obstruction of the descending VI major triad. With the application of the semitone outside the chord, the decorative semitone has also developed and become a colorful technique. In the musical forms of the main music, especially in large-scale musical forms, such as sonata form, the structural role of harmony has been brought into full play and has become one of the elements of the structure of the main music.With improvement of computer performance and development of signal processing technology, the connection between computer science and musicology is getting closer and closer, and more and more researchers are using digital signal processing technology to extract information from music, such as pitch, pitch length, chord, and other characteristics [3, 7, 8]. With advent of artificial intelligence, deep learning, neural networks, and other theoretical knowledge related to machine learning further promote development of music systems in direction of intelligence [9]. On the other hand, music signals change with time, different music states or characteristics change with time, and the states at different times are interrelated. In this sense, recurrent neural networks are good at processing time-related sequence data.


## 2. Related Work

The detection of note starting point is a very critical part in the field of music signal processing, and detection performance will directly affect results of later experiments. Abd-AlGalil et al. [10] extract MFCC coefficient (mel frequency cepstral coefficient, MFCC) from signal using auditory characteristics of the human ear and then detect the starting point based on cepstral distance with an accuracy of 96%. Schönberger [11] records a moment of note onset based on the result of phase difference, while Blaszke and Kostek [12] first preprocess music signal in full phase and then detect note onset using a feature of phase difference mutation, and experimental results show that this type of method is more suitable for note detection of a slow rhythm music. Alqahtani et al. [13] combined wavelet domain and time domain features for note slicing and achieved 96% accuracy for detecting the starting point of piano music, but the number of missed notes was high, resulting in a low recall rate [14]. Firstly, a constant Q-transform was applied to music signal, then energy value of high frequency weighting was calculated, and finally, the energy value was differenced and peak value was detected to get a detection result, and detection accuracy of this method reached 85%. Other scholars have applied machine learning techniques to note detection, which requires a large number of training samples and high computational complexity, but results are more satisfactory.The goal of multipitch estimation is to accurately estimate the pitch of all notes in a music signal, which mainly includes two stages: single-pitch estimation and multipitch estimation. The main algorithms of single fundamental frequency estimation include the autocorrelation function method, average amplitude difference method, inverse spectrum method, and harmonic product spectrum method. Goto and Dannenberg [15] proposed an autocorrelation algorithm for summation by considering characteristics of the human ear, using a cochlear filter set as well as an autocorrelation algorithm. Wu et al. [16] used a short-time spectrum of music signal for modeling and designed a weighted hybrid tone model, but usually, only the primary fundamental frequency can be estimated. Saghezchi et al. [17] improved the system by using the constrained hybrid Gaussian model to model a set of harmonics as a whole and proposed harmonic time series structured clustering algorithm. In the twenty-first century, multibasic frequency estimation has been incorporated with new techniques and developed more rapidly. For example, Weng and Chen [18] proposed the classical selected generation spectral subtraction method, in which fundamental frequency and its harmonic frequency information are removed from the spectrum by the selected generation, and subsequently improved by using gammatone filter banks, which are more perfect and have a higher accuracy rate. In [19], mel inverse spectral coefficients based on spectral envelope are used as features, and spectral features of signal to be measured are expressed as a weighted sum of spectral features of each note by the least squares method; Radwan et al. [20] propose a nonnegative matrix decomposition algorithm based on average energy spectral envelope for multiple note recognitions; Pachet and Roy [21] build a polyatomic note dictionary, and the dictionary is used for spectral decomposition of music signals. With the rapid development of Internet technology, music has been widely spread. The effective method of extracting, retrieving, and organizing music information, that is, the research of music information retrieval, has been widely concerned by the academic and information circles. Multifundamental frequency estimation is one of the research hotspots in the field of music information retrieval. The basic task is to estimate the number of simultaneous notes in polyphonic music, so as to obtain the fundamental frequency value, start time, and end time of each note. The current multifundamental frequency estimation methods cannot meet the actual needs, so the further research on multifundamental frequency estimation methods is particularly important.In this paper, we study piano music and combine techniques of basic music theory, digital signal processing, and recurrent neural network to study and improve note detection and multibasic frequency estimation, respectively, and use the main notes and harmonic components obtained by multibasic frequency estimation as features and labels of neural network to train model, so that it has the ability to add harmonic effects to melodic monophonic notes, thus realizing an automatic harmonic arrangement system.


## 3. Piano Automatic Harmony System Architecture

As shown in Figure1, piano harmony automatic arrangement system designed in this paper contains three main subtasks: note detection, multibasic frequency estimation, and model training, and all research and innovation work is centered on these three subtasks.Figure 1
Flow chart of piano harmony automatic composition system.Most of the previous starting point detection methods do not take into account a priori knowledge of instrument pitch. The end of note will be detected between two note start points according to energy reading value, and note start point and note end point will form a note segment, which contains one or more notes. The diagram of note detection described in this paper is shown in Figure2. This paper takes the polyphonic music of piano, a multipart musical instrument, as the research object. In the framework of multifundamental frequency estimation method based on nonnegative matrix decomposition, the time-frequency representation of music signal, the construction of note dictionary, and spectral decomposition algorithm are analyzed, and l_P,q~q norm block sparsity constrained nonnegative matrix decomposition algorithm effectively improves the accuracy of multifundamental frequency estimation of a single-frame signal. Finally, based on the nonnegative matrix decomposition, the multifundamental frequency estimation method is studied directly at the note event level rather than at the signal frame level.Figure 2
Schematic diagram of note detection.Note start detection refers to the detection of the start time of all notes in a piece of music. Generally, from the beginning to end of note will go through four stages, namely, Attack, Decay, Sustain, and Release (ADSRS), and change of amplitude of each stage is shown in Figure3.Figure 3
Four stages of notes.The above process of multibasic frequency estimation is shown in Figure4.Figure 4
Schematic diagram of multifundamental frequency estimation process.The structure of neurons is shown in Figure5.Figure 5
Neuron structure.In Figure5, x1,x2,⋯,xn represents the input signal, and w1,w2,⋯,wn represents the weight of each input signal and neuron connection; in general, neural network will have multiple layers, except for input and output layers. Specific structure is shown in Figure 6.Figure 6
Neural network structure.Different from ordinary neural networks, the hidden neurons in recurrent neural networks are interconnected; that is, the output of each hidden layer is not only related to the input at the current time but also related to the output state of the hidden layer at the previous time. Its structure is shown in Figure7.Figure 7
Structure diagram of cyclic neural network.In practice, the length of input sequence is not necessarily equal to output sequence, and the length of input and output sequences can be divided into the following three modes: one-to-many mode, many-to-one mode, and many-to-many mode. Figure8 is a diagram of one-to-many mode, in which there is only one input and multiple outputs, and this mode is generally used in automatic composition, text generation, etc.Figure 8
One-to-many mode diagram.Figure9 is a schematic diagram of the many-to-one model, in which there are multiple inputs and only one output, and this model is generally used in classification-related problems.Figure 9
Schematic diagram of many-to-one mode.Figure10 shows a schematic diagram of the many-to-many model, in which there are multiple inputs and outputs, and this model has a wide range of applications, such as speech recognition and machine translation. There is another common variant of the many-to-many model, which is called the encoder-decoder model, which works by first encoding input data into an intermediate vector c and then decoding output according to this intermediate vector as shown in Figure 11.Figure 10
Schematic diagram of many-to-one mode.Figure 11
Schematic diagram of the encoding-decoding model.


## 4. Improved Neural Network Algorithm

(1)
First define error of each layer:(1)δl=∂L∂Zl,l=1,2,⋯,N,where l stands for loss and δl represents the error of layer L.(2)
Calculate error of each layer from the last layer forward according to the chain rule:(2)δil=∑j∂L∂zjl+1∂zjl+1∂zil=∑jδjl+1wijl+1gl′zil,where δil represents the error of th neuron in layer.(3)
Calculate gradient of each parameter according to the error of each layer:(3)∇wijl=∂L∂zjl∂zjl∂wijl=δjlail−1,∇bil=∂L∂zil∂zil∂bil=δil.(4)
Update each weight parameter according to the gradient:(4)wijl⟵wijl−γ∇wijl,bil⟵bil−γ∇bil,where wijl represents the j-th weight parameter of th neuron in layer, a bias term of th neuron in layer bil, and γ represents the learning rate, also known as step size. The larger the γ, the faster the weight parameter updates, but too large to converge. On the contrary, the smaller the γ, the newer and the slower fork weight parameter, which can avoid oscillation or divergence, but the number of iterations will increase.There are 88 single-tone samples of piano keys. Each sample obtains logarithmic energy spectrum after Fourier transform and uses timbre filter bank for filtering; that is, logarithmic energy spectrum and amplitude spectrum of timbre filter bank are multiplied and accumulated according to the corresponding frequency points, and output is shown in the following equation:(5)Ak,m=∑ωXmωGkω,where Xmω represents the logarithmic energy spectrum of the mth single-tone sample. The value range of M is 1,88, and Gkω represents the frequency response of the kth filter in timbre filter bank. Since the number of filters is 88, the value range of K is 1,88. Ak,m is the output of the logarithmic energy spectrum of the mth single-tone sample through the kth filter and represents the sum of fundamental frequency and harmonic energy of the kth key contained in the mth single-tone sample. Therefore, a single-tone sample corresponds to an 88 dimensional column vector, and 88 single-tone samples of 88 keys can obtain 88 dimensional vectors, forming 88×88 timbre matrix a8b8, where each element is Ak,m, as shown in the following equation.
(6)A88×88=Ak,1Ak,2⋯Ak,88=A1,1A1,2⋯A1,88⋮⋮⋱⋮A88,1A88,1⋯A88,88.For note segment requiring multifundamental frequency estimation, energy matrix is calculated. Similar to training tone matrix, note segment to be measured is first Fourier transformed and then filtered by tone filter bank:(7)Ek=∑ωYωGkω,where Yω is the logarithmic energy spectrum representing musical segments, Gkω represents the frequency response of kth filter in timbre filter bank. The value range of K is 1,88. E represents the energy value of note segment filtered by the kth filter. E1E2⋯E88 can be spliced to form 88×1 energy matrix E88×1, as shown in the following equation.
(8)E88×1=E1E2⋯E88T=E1E2⋮E88.Since each element in energy matrixE88×1 represents the sum of energy of fundamental frequency of corresponding piano key and its harmonics and also represents the possibility of corresponding piano key, greater its value, greater possibility of occurrence, and number of notes in harmony generally do not exceed 5, and timbre matrix A88×88 reduces dimension from energy matrix E88×1. Find out first the n larger values in X1 to get their corresponding piano key serial numbers n1,n2,⋯,nN, and then from timbre matrix A88×88, find out the column of the corresponding key serial number and reduce the dimension of timbre matrix a×8 to 88×N. Obtain timbre dimension reduction matrix B88×N, as shown in the following equation.
(9)B88×N=Ak,n1Ak,n2⋯Ak,nN=A1,n1A1,n2⋯A1,nN⋮⋮⋱⋮A88,n1A88,n2⋯A88,nN.For the calculated energy matrixE88×1 and tone dimension reduction matrix B88×N, calculate harmony coefficient vector WN×1 so that the product of tone dimension reduction matrix B88×N and harmony coefficient vector WN×1 approximates energy matrix E88×1:
(10)E88×1≈E^88×1=B88×N×WN×1,where E88×1 represents the energy matrix, B88×N represents the dimension reduction matrix of timbre, and WN×1 represents the harmonic coefficient vector. Its physical meaning is that energy matrix E88×1 of symbol segment is expressed as a combination of energy distribution of N piano key single-tone samples, and the elements in harmony coefficient vector WN×1 represent the volume of corresponding piano key. In this paper, gradient descent method is used to solve harmonic coefficient vector WN×1. Since volume of piano keys cannot be negative, that is, elements in WN×1 must be greater than or equal to zero, multiplicative update rule is used to ensure that elements in WN×1 are positive in the process of solving. Firstly, a loss function J based on Euclidean distance is defined. The process of solving is to change value of WN×1 so that J decreases continuously. The definition of J is shown in the following equation:
(11)J=12∑i∑jEij−E∧ij2.According to Equation (11),
(12)∂J∂E^ij=−∑iEij−E^ij,E^ij=∑kBik,Wkj∂E^ij∂Wkj=Bik.According to Equation (12),
(13)∂J∂Wkj=∂J∂E^ij∂E^ij∂Wkj=−∑iEij−E^ijBik=∑iE^ijBik−∑iEijBik=BTE^kj−BTEkj=BTBWkj−BTEkj.Then, update the parameters according to the descending direction of the gradient:(14)Wkj=Wkj−γ×BTBWkj−BTEkj,where γ is the updated step size and Wkj is negative when iterating according to the following equation so that
(15)γ=WkjBTBWkj.Substitute Equation (15) into Equation (14) to obtain the following:
(16)Wkj=Wk1BTEkjBTBWkj,where the value of K is 1,n and the value of J is 1, because the dimensions of matrix E and matrix W are 88×1 and N×1, respectively. Updating parameters according to Equation (16) ensures that each element in the entire update process wN+1 is a positive number. You need to set a threshold before updating δ. When loss/less than threshold δ, stop the parameter update.After parameter update is completed, there will be a positive number very close to zero inwN+1. At this time, it is necessary to set a threshold and set elements less than threshold to zero. The specific method is to take the maximum value from wN+1 and multiply the maximum value by a coefficient μ, and the obtained product result is used as a threshold to filter result. Because elements in wN+1 represent the volume of corresponding keys, keys corresponding to the filtered nonzero elements are combined into a harmony, and the note of the key corresponding to the maximum element value is the main note. In the experiment, N=20.6×10−3, and μ=0.1.


## 5. Results

Therefore, we can find the following conclusions: on the one hand, due to the characteristics of the piano’s own structure, the basic components of the sound emitted by the keys in the bass area are few; on the other hand, there are many harmonic components, while the keys in the high-pitched area have more basic components and less harmonic components. This difference is shown in Figure12.Figure 12
Spectrum comparison of keys in bass and treble areas.Loss of fundamental frequency means that in spectrum of a piano note, fundamental frequency component is very small compared to harmonic component, or even completely lost, which occurs mostly in bass region. Take bass note A0 for example, loss of fundamental frequency is shown in Figure13.Figure 13
Spectrum diagram corresponding to note A0.Therefore, compared with other studies, the innovations of this study are as follows: (1) the common multiresolution time-frequency representation constantQ-transform (CQT) in music signal analysis is studied. It is found that although CQT has a high frequency resolution at low frequencies, it also leads to a reduction in time resolution. The variable Q-transform is introduced for the first time as a tool for estimating the time-frequency representation of music signals with multifundamental frequencies, which has a better time resolution than CQT at the same frequency resolution and efficient coefficient calculation. (2) The spectral decomposition method based on monatomic and polyatomic note dictionary is studied. Study uses L_P,q~q norm sparse constrained multifundamental frequency estimation. The experiment of spectral decomposition of monatomic dictionary shows that the effect of PPL norm is better than that of common 1L norm. In view of the obvious spectrum changes of notes at different times, it is pointed out that the single-atom note dictionary does not take into account the dynamic changes of the base atoms of the note spectrum, and then, the construction methods of multiatom note dictionary are introduced from the perspectives of modeling and learning. Finally, based on the multiatom note dictionary, L_P,q~q norm block sparsity constrained nonnegative matrix decomposition algorithm. The experimental results show that when the number of atoms is 2, the F value of the algorithm for multiple fundamental frequency estimations of single-frame signal of music clips in maps database reaches nearly 78%. (3) The multifundamental frequency estimation methods based on nonnegative matrix decomposition are all for single-frame signal processing. It does not detect the note start point in advance, but obtains the note start point through postprocessing detection results, which may lead to false start points and the error of dividing a note into multiple notes between two note start points.Harmonic overlap means that fundamental frequency or harmony of a note overlaps with that of other notes. For example, when the ratio of fundamental frequencies of two notes satisfiesf1/f2=m/n relationship, Nth harmonic of note f1 will overlap with Nth harmonic of note f2. The smaller the ratio of m to N, the more serious overlap. Taking note A3 and note A4 as examples, a phenomenon of harmonic overlap is shown in Figure 14:Figure 14
Harmonic overlap of note A3 and note A4.The amplitude-frequency characteristics of 49th note filter are shown in Figure15 for piano key number 49, A4.Figure 15
Spectrum of the 49th tone filter.As shown in Figure15, the 49th tone filter has only one passband, because fundamental frequency of note A4 with key number 49 is 440 Hz, so center frequency of passband is 440 Hz, and according to relative pitch of twelve mean rhythms, frequencies of semitones before and after 440 Hz can be calculated as 415.3 Hz and 466.1 Hz, which correspond to boundary frequencies of passband.The amplitude-frequency characteristics of 88 band-pass filters of tone rhythm filter set were superimposed together to obtain amplitude-frequency characteristics shown in Figure16.Figure 16
Spectrum diagram of tone filter bank.Select a piece of music from 30 pieces of music and intercept segment for about 2 seconds. Extract spectrum features through the three methods, respectively. The detection results are shown in Figure17.Figure 17
Detection diagram of various note detection methods.In Figure17, time domain diagrams of music fragments and detection diagrams of the three methods are presented, respectively. One of the reasons is that the MFCC method can be interfered by frequencies outside pitch range of keys, and these redundant frequency information may lead to appearance of pseudo-peaks, and the result of treating pseudo-peaks as peaks is false detection. The detection diagram based on the MFCC inverse spectral distance method has clearer changes. False peaks may appear in the spectrum, some of which are not very obvious, and the amplitude between the peaks is very different. It is easy to ignore the peaks, which may easily lead to missed detection and errors.The results are shown in Table1.Table 1
Test results of note starting point detection.
Recall rateRAccuracypF-measureCorrect numberNumber of missed inspectionsNumber of false inspectionsPaper method86.54%97.28%91.60%354555520994Spectral difference method85.49%94.77%89.90%3503359401935Based on MFCC cepstrum distance78.33%96.07%86.32%3260983661328Rand subset contains 300 wav format files, each of which has only one group of multiple fundamental frequencies composed of random notes. Therefore, there are 300 groups in total. The number of combinations ranges from 2 to 7. According to the number, it can be divided into six categories, and the number in each category is 50. The results are shown in Table2.Table 2
Multifundamental frequency estimation results of Rand subset.
Detection result234567TotalTotal number of notes10015020250303501350Correct number911351762112472741136Number of missed inspections91521405575215Number of false inspections1125395082100306Recall91.0990.6689.4984.4082.1078.2984.06Accuracy rate84.3982.5080.5475.2272.9078.7278.90F-measure87.1585.8482.4078.4575.4176.1281.30From Table2, it can be seen that recall and precision rates decrease gradually with an increase of number of notes, which leads to decrease of F-measure. In general, the proposed method for Rand subset data with multibasic frequency estimation experiments results in 84.07%, 78.71%, and 81.30% recall, accuracy, and F-measure, respectively. The results were proposed in [13], and comparison results are shown in Figure 18.Figure 18
Comparison of multifundamental frequency estimation results based on Rand subset.Considering the large WAV, I extracted about 30 seconds of music clips from the synthetic WAV, compressed them into MP3 format, and then uploaded them to the ECS. The score is 5 levels, with a minimum of 1 point and a maximum of 5 points. From low to high, it corresponds to poor quality, barely acceptable, acceptable, relatively satisfied, and very satisfied. After listening to the music, the listener can evaluate the effect. I invited 20 music lovers to evaluate the effect, as shown in Table3.Table 3
Scoring results.
Example 1Example 2Example 3Example 4Example 5Example 6Example 7Example 8Example 9Example 10Score4.54.23.54.43.84.33.93.23.23.8From the score, the highest score of 1 is 4.6, and the lowest score of 9 is 3.1. Most of the average scores are above 3.5, and the overall average score is about 4, which means that listeners are generally satisfied with the effect of automatic harmony arrangement.


## 6. Conclusions

This paper studies a piano music automatic harmony arrangement system based on deep learning. The system uses machine instead of human as the core design idea and adds a harmony effect to melody monophones to realize automatic harmony arrangement. The recursive neural network structure is adopted, and the model mechanism based on codec is introduced to build a network scoring platform. The original music and voice-arranged music are uploaded to the cloud server for fans to listen to through the browser, and the effect is evaluated. Most of the songs have high scores. The results show that the listener is very satisfied with the performance of the system, which verifies the effectiveness of the proposed harmonic automatic configuration system and provides a theoretical basis for machine learning.



---

*Source: 7662443-2022-07-25.xml*