Programmers refuse to play a single song on loop: the song is only 5 minutes long, but it must be played for more than 450 days without repeating
Qian Ming from Aofei Temple
Quantum Bit Report | Public Account QbitAI
If a programmer likes something, you never know what "crazy" things he might do.
Today I want to talk about a programmer named Alex Bainter who particularly likes a certain song.
This song, called Aisatsana , comes from the electronic music master Aphex Twin and is only over 5 minutes long.
What bothers him is that listening to a song on repeat is easy to get boring, so he wants to play this song over and over again without repeating himself.
Then, he chose Markov chains...
Now this song can be played non-stop for 451 days without repeating... (Listen to the song at the end of the article)
Bette wrote an article about how he accomplished this feat and shared it with others. The code has also been made open source .
It quickly gained over 400 hits and over 200 comments on Hacker News.
Many people commented that it is really enjoyable to listen to music in this way. It is simple to implement, but the effect is very good!
How does it do this? Let's take a look.
Understanding a song from an algorithmic perspective
If you want to play a song without repeating yourself, the first thing you need to do is to understand the structure of the song.
Generally speaking, the speed of playing a song can be measured in BPM (beats per minute).
The song "Aisatsana" is very simple, with a total of 102 BPM. If you start counting from the first note, every 16 beats contains a series of notes, which he calls a phrase . There are a total of 32 phrases in the whole song.
To create a system that can play aisatsana without interruption and repetition, the algorithm needs to meet the following two requirements:
-
Know when a phrase ends.
-
Play different phrases.
The first requirement is relatively simple, just timing. The second requirement is the tricky part.
Keep the song playing
A simple strategy is to write a program that roughly splits up the 32 phrases and plays another phrase randomly after each one.
Although this method has no problem in connecting with each other and sounds more flexible and interesting than a single loop, it is also repetitive in nature.
As long as you are familiar with these 32 phrases, the music will still not be boring.
What to do? There is only one answer:
Create a system to generate new phrases that sound very similar to the original phrases.
The most direct method is deep learning technology. But with only 32 samples of musical phrases, it is obviously not realistic.
Finally, Bethe turned his attention to the "old method" of Markov chain .
A Markov chain is a chain that records the probability of transitioning from one state to another in a set of states.
For example, you only go to three places in your life: home, work, and store, that is, you are either at home, at work, or at the store.
Want to know where you are going next? The first thing is to record your visits to these three places over a period of time, and analyze the relevant possibilities based on the recorded data.
For example, when you are at home, you have an 80% chance of going to the company. When you are at the company, the probability of going home and going to the store is 50%. If you are at the store, the probability of going home next is 95%.
That’s all we need to create a Markov chain: states, and the probability of going from one state to another.
How do we apply this to music?
Markov chain generation of new phrases
Let’s take a look at a picture first:
The picture shows two phrases, each with 4 beats. Next to each note is the name of the note, and at the top is the beat. The order in which these phrases are played is from left to right.
Then, put the two phrases into the Markov chain, and the state is named as the combination of beat and note, for example, 1A means playing the A note on the first beat.
In addition to these, there are also start and end states to indicate the beginning and end of a phrase. Arrows are used to indicate the transition from one state to another.
In the picture above, the first phrase is start → 1A → 2F → 3A → 4F → end. The second phrase is start → 1E → 2C → 3A → 4C → end.
There are 9 states in total, namely start, 1A, 1E, 2F, 2C, 3A, 4F, 4C and end. The probability of going from one state to another is:
Start → 1A: 50%
Start → 1E: 50%
1A→2F: 100%
1E→2C: 100%
2F → 3A: 100%
2C → 3A: 100%
3A→4F: 50%
3A→4C: 50%
4F → End: 100%
4C → End: 100%
In this way, a Markov chain is established. From the beginning to the end, all possible paths can generate 4 phrases:
Start → 1A → 2F → 3A → 4F → End (Original phrase 1)
Start → 1A → 2F → 3A → 4C → End (new phrase)
Start → 1E → 2C → 3A → 4C → End (Original Phrase 2)
Start → 1E → 2C → 3A → 4F → End (new phrase)
Just like that, the Markov chain has generated two new phrases! Since they are built from the same chain, they will sound very similar.
In this example, only 2 phrases are used, each with 4 beats, while in aisatsana there are 32 phrases, each with 16 beats.
Following this logic, Bate finally generated more than 4 million unique phrases that can be played for more than 451 days, and all the phrases will never be repeated...
The entire implementation code has been open sourced by Beite. The project address is:
https://github.com/generative-music/pieces-alex-bainter/blob/master/packages/piece-aisatsana/src/piece.js
If you are interested, you can try it yourself~
Finally, attach the generated aisatsana address:
https://generative.fm/music/alex-bainter-aisatsana
-over-
Quantumbit AI+ Salon Series--Smart City
Join the community
The QuantumBit AI community has started recruiting. The QuantumBit community is divided into: AI discussion group, AI+ industry group, and AI technology group;
Students who are interested in AI are welcome to reply to the keyword "WeChat group" in the dialogue interface of the Quantum Bit public account (QbitAI) to obtain the group entry method. (The technical group and AI+ industry group need to be reviewed and the review is strict, please understand)
Sincere recruitment
Qbit is recruiting editors/reporters, and the work location is Beijing Zhongguancun. We look forward to talented and enthusiastic students to join us! For relevant details, please reply to the word "recruitment" in the dialogue interface of the Qbit public account (QbitAI).
Quantum Bit QbitAI · Toutiao signed author
Tracking new trends in AI technology and products
If you like it, click here!