Previous algorithms created for the harmonization of a melody have highlighted difficulties in finding a solution that could be significant and important not only from the point of view of listening but also from that of composition, that is, also respecting the rules of musical grammar that tradition handed down to us. This article describes a model for the harmonization of melody (non-modulating) derived from common concepts in music theory (such as Schenker’s theory) applied in compliance with the rules of musical grammar. The fundamental structure is characterized by a self-learning system based: on the Markovian stochastic process, for the definition of rules both for the concatenation of the chords and for the correct melodic movement of the sounds between two consecutive chords; on the Viterbi algorithm, for identifying the correct chord for each sound of the melody. The core of the algorithm allows the sounds of each chord to follow a correct trend (ascending or descending) such as to give each of them the real decisive impulse (which every listener is able to recognize). Examples of musical fragments harmonized in this way show that the apparatus of the composition rules and that of the listening rules must be thought of as coinciding (or at least partially in possession of common elements).