This content originally appeared on Level Up Coding - Medium and was authored by Aishwarya Sharma
Spotify creates a unique barcode and album cover image for every song, album, artist, and playlist. The users can use these scannable codes to share or access a piece of content within Spotify quickly. But I was more intrigued by how this code works, and will Spotify ever run out of the “unique” codes for its media content?
A typical Spotify code contains 23 bars, and each code starts with the Spotify logo. According to the Spotify Codes website, Spotify Codes need the Spotify logo at the front so the scanner can read them. It means that the Spotify logo helps to identify the area having machine-readable code in the image.
The code also defines a start and end marker to define the start and end of the code. The scanning method includes identifying this start and endpoint, an axis between them, and a reference mark that indicates maximum offset. The offset is divided by a step size for possible length values, seven in this case. Each bar in the code is measured in length from the axis point to the end of the bar’s optical mark, rounded to the nearest step size. The algorithm can thus convert this length of the bar to corresponding Gray code.
Gray Code
A Gray code is the ordering of the binary numerical system such that two successive values differ by only one bit.
But why use Gray code and not a binary encoding? While reading the machine-readable code, there are chances of error. The code may measure an optical bar between the distance 3 and 4. By using gray code, only one bit will be uncertain; the encoded bits can either be 010 or 110 (gray code for 3 and 4). If we use binary encoding, all three bits would have been uncertain (the possible bits are 011 or 100). Each measured distance thus generates 3 bits with potentially one bit with less than 100% certainty.
Let’s look at the “Highway to Hell” Spotify code and convert the bars into the gray code, shall we?
First, we need to perform some Image Enhancement to our image so that the results are more suitable for further analysis. We’ll use Skimage to load the image, convert it into grayscale, and then into binary.
Next, we need to detect the bars in the image. First, we need to label the image and then measure the labelled image regions’ properties using regionprops. Each element of dimensions would be a tuple representing (min_row, min_col, max_row, max_col).
We need the max height as a reference to calculate the step size of all the bars.
The label returns the ndarray of dtype int. We need to preserve the ordering of the labels. Thus we’ll need to sort the dimensions by min_col.
The optical bar’s step size can vary between 0 and 7; thus, there can be eight possible values. As I mentioned before, the first and the last bar act as the start and end markers, while the middle bar acts as a reference for other bars.
The order gives us the step size of each bar in the image.
[0, 5, 3, 2, 4, 6, 3, 3, 4, 2, 1, 7, 5, 4, 3, 6, 4, 7, 7, 6, 4, 2, 0]
Let’s visualize a bit.
Here’s the code for how I achieved this.
According to Spotify’s patent, Each distance apart from the start and stop markers’ fixed lengths and the reference mark encodes 3 bits. So that gives us a total of 60 bits.
110010011110101010010110011001110110010101110100100101110011
The bits are shuffled to spread out the errors. Instead of having the 3 bits be consecutive, the lost bits are non-consecutive in the codeword. This improves the chances for the forward error correction to work when a whole bar/distance is lost.
Decoding
The new ordered bits are then sent for decoding. The patent suggests that they’re maybe using convolution code for forward error correction(FEC) and a Viterbi decoder that performs a maximum likelihood decoding of the forward error-corrected code, extracting the error corrected 45 bits (37 bit for media reference, and 8 for checksum).
We’re not going much into the algorithm for decoding.
The checksum is verified, and a 37-bit reference is sent to the server.
While creating media references, a random number may have been allocated on the server and stored in a table linking the media URI. For example, a song or a playlist. The server then returns the respective media to the device that scanned the image.
The whole process of generating the media reference, converting it to the Spotify code, and then reading these codes to extract the media reference is quite sophisticated. I really liked the idea to make the code look like a sound wave, though.
Will Spotify ever run out of these unique codes?
The answer is no. If you look at the media URI for Spotify, it follows the following format.
spotify:<type of media>:<media reference>
spotify:track:3Dv1eDb0MEgF93GpLXlucZ
The media reference is 22 characters composed of alphabets and numbers A-Z, a-z, and 0–9 (26 + 26 + 10 = 62)
The total number of combinations possible are 6²²² = 2.7078036478026614e+39 (that’s a lot). Suffice to say that Spotify is not going to run out of it soon.
Google’s spot code
Google pay has a spot code that’s unique for every user. A Spot Code is a Google-branded visual code that works similar to a QR code but is unique to Google Pay India. You can share and scan a Spot Code to find a peer or a Spot.
These codes are definitely one step ahead of common QR codes or bar codes. It gives a glimpse of the scope of creativity that comes with image processing. The patent that Spotify published had enough insight to understand the basic processing behind it all, although I couldn’t find the same for Google’s spot code.
This article took a long time to write, mostly because I had no experience with image processing, but Skimage had enough demos and tutorials to get started. If you have any questions or feedback, please let me know in the comments. If you found it helpful, please share it with other folks!
Spotify Codes and How They Work was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Aishwarya Sharma
Aishwarya Sharma | Sciencx (2021-04-15T15:20:48+00:00) Spotify Codes and How They Work. Retrieved from https://www.scien.cx/2021/04/15/spotify-codes-and-how-they-work/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.