The rapid development of artificial intelligence has made various fields to have corresponding connections with it, and music plays an important role in daily life. One of the applications of artificial intelligence in the field of art is to use music generation algorithms to endow machines with the function of generating melody. This ability can provide certain assistance to musicians when composing music, so that music creators can inspire inspiration in the creative process. Researchers have done a lot of work in the automatic generation of music. The piano is widely used in the field of automatic accompaniment and has strong versatility. The main purpose of this paper is to design a piano-based automatic accompaniment system, to think of melody and harmony as a machine learning-like task. By training on a selected series of samples, a database of phonomorphic metastructures is constructed, to systematically collect the original piano accompaniment patterns by building a sound pattern database, and convert the collected original sound patterns into the original sound pattern structure and store in the database. Then, by establishing two Hidden Markov Model (HMM) systems to simulate the thinking mode of the composer’s piano accompaniment process, a melody style related to a certain collection of samples is formed. Finally, the Viterbi algorithm is used to select the appropriate piano accompaniment metastructure in the database to generate the piano accompaniment of the melody section. The experimental results of this paper show that, as far as the accompaniment generation effect is concerned, although the generation effect of this paper is slightly different, the overall difference does not change much. It shows that the effect generated by the method in this paper is relatively stable.