The tune was, in fact, dreamed up by a musical AI program developed at Google. And the program’s latest compositions show how combining a powerful machine-learning approach with simple musical rules can produce creative works that sound remarkably human.

Music composition is an enigmatic form of human creativity. Songwriting programs already exist, but they typically follow a specific set of rules, and they tend to produce tunes that feel rigid and mechanical. The same is true of software that recommends music based on your listening habits (see “The Hit Charade”). Teaching computers to be more musically inventive may point to ways that machines can help with other creative acts, from designing products to writing eloquent text.

Google has previously demonstrated its music-generating AI songsmith, which is part of a project called Magenta that’s aimed at fostering artificial creativity (see “OK, Computer, Write Me a Song”). A large neural network is fed tens of thousands of songs and is trained to predict the next note in a sequence. Such a network can also generate new music when given a starting point, although the results tend to lack structure and grace.

Douglas Eck, a research scientist at Google who’s leading the development of the music-generating AI, together with Natasha Jaques, an intern at the company, recently devised a way to make the songwriting systems produce much more elegant and catchy tunes. They use an approach known as reinforcement learning to add simple principles of music theory—avoid repeating a refrain too often, do not play too quickly or slowly, and so on—to the overall learning process. The network receives a positive reward every time it produces a sequence of notes that not only resembles the patterns seen in previous songs, but also adheres to the musical rules it has been given.

“These are simple rules taken from a music composition textbook,” Eck says. “The combination of these rules with reinforcement learning, and the variance of the real world coming from thousands of human compositions, gives us songs that are so catchy—they scratch some itch.”

The new approach, described in a research paper and a blog post, certainly seems to improve automated music generation. Another snippet of music shows how the program fares without these rules to follow. The piece feels flat, repetitive, and mechanical. Eck and Jaques also conducted a user study and found that people much preferred the compositions produced using the new technique.

Eck says the ability to embed rules in reinforcement learning will be useful in many areas, including robotics, recommendation systems, and language translation.