{"id":4766,"date":"2022-12-17T22:17:02","date_gmt":"2022-12-17T22:17:02","guid":{"rendered":"https:\/\/dalelane.co.uk\/blog\/?p=4766"},"modified":"2024-01-29T20:27:37","modified_gmt":"2024-01-29T20:27:37","slug":"spotify-extension-for-scratch","status":"publish","type":"post","link":"https:\/\/dalelane.co.uk\/blog\/?p=4766","title":{"rendered":"Spotify extension for Scratch"},"content":{"rendered":"<p><strong>In this post, I want to share a new Scratch extension that I made this week, explain <a href=\"https:\/\/dalelane.co.uk\/blog\/?p=4766#blocks\">what it does<\/a>, and <a href=\"https:\/\/dalelane.co.uk\/blog\/?p=4766#projectideas\">suggest a few ideas<\/a> for the sorts of ways that it could be used.<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.dropbox.com\/s\/8ofpi7e99n0w38m\/spotify-blocks.png?raw=1\" style=\"border: thin black solid\"\/><\/p>\n<h3>Overview<\/h3>\n<p>The extension makes some of the data from the <a href=\"https:\/\/developer.spotify.com\/documentation\/web-api\/reference\/#\/operations\/get-audio-features\">Spotify Audio Features API<\/a> available as blocks in Scratch.<\/p>\n<p>It means you can get numeric values representing different characteristics of songs, directly into a Scratch project.<\/p>\n<p><!--more--><br \/>\n<a name=\"blocks\"><\/a><\/p>\n<h3>The blocks<\/h3>\n<p><em>Adapted from the <a href=\"https:\/\/developer.spotify.com\/documentation\/web-api\/reference\/#\/operations\/get-audio-features\">Spotify API docs<\/a>.<\/em><\/p>\n<p><code style=\"background-color: #ffffc0; font-weight: 600; color: #770000;\">search for<\/code><\/p>\n<p>This searches Spotify&#8217;s library for a track. You can put artist and title in any order, and it&#8217;s fairly tolerant of spelling mistakes.<\/p>\n<p><code style=\"background-color: #ffffc0; font-weight: 600; color: #770000;\">play preview<\/code><\/p>\n<p>This plays a 30-second preview of the last track that you searched for. The block will stay active until the song preview has completed, so any blocks attached to the bottom of this block will be run after the preview has finished playing.<\/p>\n<p><code style=\"background-color: #ffffc0; font-weight: 600; color: #770000;\">acousticness score<\/code><\/p>\n<p>This returns a confidence score of whether the track is acoustic. The score is between 0.0 and 1.0, with 1.0 representing high confidence the track is acoustic.<\/p>\n<p><code style=\"background-color: #ffffc0; font-weight: 600; color: #770000;\">danceability<\/code><\/p>\n<p>This describes how suitable a track is for dancing &#8211; using a combination of musical elements including tempo, rhythm stability, beat strength, and overall regularity. The score is between 0.0 and 1.0, with 0.0 being least danceable and 1.0 most danceable.<\/p>\n<p><code style=\"background-color: #ffffc0; font-weight: 600; color: #770000;\">duration<\/code><\/p>\n<p>The length of the full track (not the preview clip) in seconds.<\/p>\n<p><code style=\"background-color: #ffffc0; font-weight: 600; color: #770000;\">energy<\/code><\/p>\n<p>This returns a perceptual measure of intensity and activity, between 0.0 and 1.0.<\/p>\n<blockquote><p>Typically, energetic tracks feel fast, loud, and noisy. For example, death metal has high energy, while a Bach prelude scores low on the scale. Perceptual features contributing to this attribute include dynamic range, perceived loudness, timbre, onset rate, and general entropy.<\/p><\/blockquote>\n<p><code style=\"background-color: #ffffc0; font-weight: 600; color: #770000;\">instrumentalness<\/code><\/p>\n<p>This describes whether the track has any vocals. The score is between 0.0 and 1.0.<\/p>\n<blockquote><p>&#8220;Ooh&#8221; and &#8220;aah&#8221; sounds are treated as instrumental in this context. Rap or spoken word tracks are clearly &#8220;vocal&#8221;. The closer the instrumentalness value is to 1.0, the greater likelihood the track contains no vocal content. Values above 0.5 are intended to represent instrumental tracks, but confidence is higher as the value approaches 1.0.<\/p><\/blockquote>\n<p><code style=\"background-color: #ffffc0; font-weight: 600; color: #770000;\">key (code)<\/code><\/p>\n<p>This returns a number representing the key the track is in, using <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pitch_class\">pitch class notation<\/a>. If no key was detected, the value is -1.<\/p>\n<p><code style=\"background-color: #ffffc0; font-weight: 600; color: #770000;\">key (name)<\/code><\/p>\n<p>This returns a string with the name of the key that the track is in. Names are described using sharps rather than flats (e.g. B? will be returned as A#)<\/p>\n<p><code style=\"background-color: #ffffc0; font-weight: 600; color: #770000;\">liveness<\/code><\/p>\n<p>This returns a prediction of whether the track was performed live. This is done by detecting the presence of an audience in the recording. The value is between 0.0 and 1.0, with higher values representing a higher probability that the track was live.<\/p>\n<p><code style=\"background-color: #ffffc0; font-weight: 600; color: #770000;\">loudness<\/code><\/p>\n<p>This returns the overall loudness of the track in decibels.<\/p>\n<blockquote><p>Loudness values are averaged across the entire track and are useful for comparing relative loudness of tracks. Loudness is the quality of a sound that is the primary psychological correlate of physical strength (amplitude). Values typically range between -60 and 0 db.<\/p><\/blockquote>\n<p><code style=\"background-color: #ffffc0; font-weight: 600; color: #770000;\">mode<\/code><\/p>\n<p>This returns the modality (major or minor) of the track. Major is represented by 1, minor is represented by 0.<\/p>\n<p><code style=\"background-color: #ffffc0; font-weight: 600; color: #770000;\">speechiness<\/code><\/p>\n<p>This detects the presence of spoken words in a track.<\/p>\n<blockquote><p>The more exclusively speech-like the recording (e.g. talk show, audio book, poetry), the closer to 1.0 the attribute value. Values above 0.66 describe tracks that are probably made entirely of spoken words. Values between 0.33 and 0.66 describe tracks that may contain both music and speech, either in sections or layered, including such cases as rap music. Values below 0.33 most likely represent music and other non-speech-like tracks.<\/p><\/blockquote>\n<p><code style=\"background-color: #ffffc0; font-weight: 600; color: #770000;\">beats per minute<\/code><\/p>\n<p>This returns an estimate for the tempo of the track.<\/p>\n<p><code style=\"background-color: #ffffc0; font-weight: 600; color: #770000;\">beats per bar<\/code><\/p>\n<p>This returns an estimate for the time signature of the track.<\/p>\n<p><code style=\"background-color: #ffffc0; font-weight: 600; color: #770000;\">happiness<\/code><\/p>\n<p>This returns a number describing the musical positiveness conveyed by a track. The score is between 0.0 and 1.0, with higher scores representing more positive tracks (e.g. happy, cheerful, euphoric), and lower scores representing negative tracks (e.g. sad, depressed, angry).<br \/>\n<a name=\"projectideas\"><\/a><\/p>\n<h3>Project ideas<\/h3>\n<p>The reason for creating the extension was to try creating some machine learning projects, training a machine learning model using some or all of this data. I&#8217;ve not tried doing that yet, so in the meantime, here are a few quick ideas for the sorts of things that can simply be made using the extension.<\/p>\n<h4>Sprites that dance in time to music<\/h4>\n<p>You can animate a sprite by switching costumes. Using the <code style=\"background-color: #ffffc0; font-weight: 600; color: #770000;\">beats per minute<\/code> block, you can animate a sprite in time to music, so it looks like they&#8217;re dancing.<\/p>\n<p>You can include the <code style=\"background-color: #ffffc0; font-weight: 600; color: #770000;\">danceability<\/code> block as well to make your sprite refuse to dance to songs that aren&#8217;t danceable enough!<\/p>\n<p><iframe loading=\"lazy\" width=\"450\" height=\"253\" style=\"border: thin black solid\" src=\"https:\/\/www.youtube.com\/embed\/8T2k2LhAgms\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen=\"\"><\/iframe><br \/>\n<small><a href=\"https:\/\/youtu.be\/8T2k2LhAgms\">Demo video at youtu.be\/8T2k2LhAgms<\/a><\/small><\/p>\n<p>This is the simplest project I can think of, in terms of the amount of Scratch coding needed.<\/p>\n<p>And if you skip the danceability check, it&#8217;s even simpler.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.dropbox.com\/s\/10kvse1v9p9bn2i\/dancer-code.png?raw=1\" style=\"border: thin black solid\"\/><\/p>\n<p><a href=\"https:\/\/www.dropbox.com\/s\/46kuyfgu7tc4w0l\/dancer.sb3?dl=0\">Download this Scratch project here<\/a> &#8211; give it a try!<\/p>\n<h4>Higher or lower<\/h4>\n<p>There are a whole range of variations on the &#8220;higher or lower&#8221; game that you could make in Scratch, using one of the values that Spotify can give.<\/p>\n<p>To make it into a game, you could choose the songs ahead of time, and get the person playing the game to guess whether the next song is higher or lower. That feels a bit restrictive, so I tried turning this on it&#8217;s head &#8211; and getting the game to choose higher or lower, and getting the person playing the game to think of a song that matches.<\/p>\n<p>For example, I tried this with the <code style=\"background-color: #ffffc0; font-weight: 600; color: #770000;\">happiness<\/code> block.<\/p>\n<p><iframe loading=\"lazy\" width=\"450\" height=\"253\" style=\"border: thin black solid\" src=\"https:\/\/www.youtube.com\/embed\/-otSUzdTM5o\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen=\"\"><\/iframe><br \/>\n<small><a href=\"https:\/\/youtu.be\/-otSUzdTM5o\">Demo video at youtu.be\/-otSUzdTM5o<\/a><\/small><\/p>\n<p>Choose a song. It&#8217;s happiness score is 47.<br \/>\nCan you think of a song that sounds more happy? You choose a song with a happiness score of 73.<br \/>\nCan you think of a song that sounds less happy?<br \/>\nAnd so on.<\/p>\n<p><a href=\"https:\/\/www.dropbox.com\/s\/u47k3zrdc22cven\/higher-or-lower.sb3?dl=0\">Download this Scratch project here<\/a> &#8211; give it a try! I managed a high score of 15 when I recorded the demo video above. Can you beat that?<\/p>\n<p>I think the idea could work with other values, too. For example, you could do this with the <code style=\"background-color: #ffffc0; font-weight: 600; color: #770000;\">beats per minute<\/code> block. Starting from one song, can you think of a song that is faster? Then can you think of a song that is slower? And so on.<\/p>\n<h4>Try to guess the tempo of a song<\/h4>\n<p>The other type of project I thought of was getting the user to guess what value Spotify will return for a song, and then showing them how close they are to the value from the Spotify API.<\/p>\n<p>For example, you could make a project where the user has to estimate the tempo of a song.<\/p>\n<p><iframe loading=\"lazy\" width=\"450\" height=\"253\" style=\"border: thin black solid\" src=\"https:\/\/www.youtube.com\/embed\/t8IUDJMGPK0\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen=\"\"><\/iframe><br \/>\n<small><a href=\"https:\/\/youtu.be\/t8IUDJMGPK0\">Demo video at youtu.be\/t8IUDJMGPK0 <\/a><\/small><\/p>\n<p>You could add the <code style=\"background-color: #ffffc0; font-weight: 600; color: #770000;\">play preview<\/code> block as well if you want to make it easier, and let them listen to the song while they&#8217;re trying to work out the tempo.<\/p>\n<p><a href=\"https:\/\/www.dropbox.com\/s\/7fhaf74e9fx9w9a\/guess-the-tempo.sb3?dl=0\">Download this Scratch project here<\/a> &#8211; give it a try!<\/p>\n<h3>How to try this out<\/h3>\n<p>I&#8217;ve added this extension to the version of Scratch that I host at <a href=\"https:\/\/scratch.machinelearningforkids.co.uk\">scratch.machinelearningforkids.co.uk<\/a> so you&#8217;ll need to go there to try it out.<\/p>\n<p>To access the extensions library, click on the extensions button in the bottom left.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/github.com\/IBM\/taxinomitis\/blob\/master\/mlforkids-api\/public\/images\/scratch3-extensions.png?raw=true\" style=\"border: thin black solid\"\/><\/p>\n<h3>Other ideas<\/h3>\n<p>I&#8217;m sure there are loads of other things that could be made with these blocks &#8211; these are just a few early ideas. As I mentioned above, the original motivation for making it was to try using the values in a <a href=\"https:\/\/machinelearningforkids.co.uk\/worksheets\">machine learning project<\/a>, but it&#8217;s been fun to try a few other ideas ahead of that, too.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, I want to share a new Scratch extension that I made this week, explain what it does, and suggest a few ideas for the sorts of ways that it could be used. Overview The extension makes some of the data from the Spotify Audio Features API available as blocks in Scratch. It [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4773,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[587,536],"class_list":["post-4766","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-code","tag-mlforkids-tech","tag-scratch"],"_links":{"self":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4766","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4766"}],"version-history":[{"count":0,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4766\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/media\/4773"}],"wp:attachment":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}