Bash Script for Editing Playlist Files

music_knowledgeOver the weekend I was working on a playlist for a personal event coming up later this week. The playlist had about 5 hours of music–around 80 songs–that had been purchased from various sources over the years. In order to have a backup of the playlist on another machine I needed to get all of the music files in one place, so I used iTunes’ “Create AAC Version” tool.

The problem with this was that many of the new files were named in the format “#{song_number} #{song_title}.m4a”. For instance, “Dustland Fairytale” by The Killers was “05 Dustland Fairlytale.m4a.” I could’ve spent my Saturday night manually clicking through and editing the filenames, but fortunately I knew that with a little bash scripting I could automate the whole process.

Rather than show the entire script right away, I want to go through the process of composing a bash script to solve this type of problem. First, we know that we can rename files using the mv command:

mv oldfilename newfilename

Next, it’s important to know that you can loop through all .m4a files (or whatever other extension) in a given directory by:

for i in *.m4a
do
  [do stuff here]
done

Within the for loop we access the filename by "${i}". Your code inside the do block could be something simple like echo "${i}" or something more complicated spanning multiple lines. We can also index the filename strings in the form "${string:startindex:endindex}". If we leave off the last index it defaults to the end of the string. (You can also index from right to left, but we omit that for simplicity here.) All of the numbers I was dealing with in the playlist file were two-digits with a space separating them from the song title. So basically I wanted to drop the first three characters of the string (indices 0, 1, and 2). We can print out the shortened filenames by:

for i in *.m4a
do
  echo "${i:3}"
done

But if you do that and some of your files don’t start with numbers, you will see that it chops off the beginnings of those filenames. To avoid this, we need to use an if then statement to check whether the files begin with a number. For this it is sufficient to check whether the first character of the filename is “0”:

for i in *.m4a
do
	if [ "${i:0:1}" == "0" ]
	then 
		echo "${i:3}"
	fi
done

We just print the modified filenames in order to check whether our script operates as intended. This is an important caveat to bash scripting–check whether your script does what you want before you run it. Bash scripts are like sharp blades: in the hands of a master they are a wonderful tool, but in the hands of an amateur they can be deadly. I’m closer to the amateur end of the spectrum so I prefer to be careful. Once we are satisfied that our little tool is only chopping off what we want it to, then we are ready to compose the whole script with that mv command:

for i in *.m4a
do
	if [ "${i:0:1}" == "0" ]
	then 
		mv "${i}" "${i:3}"
	fi
done

If you put this in a script.sh file in the same directory as your playlist then you can just run bash script.sh from your terminal.

OK, so that may not have saved me that much time but it was way more fun than clicking through all those files!

the_general_problem

Note: There are probably many other ways to accomplish the same outcome described in this post. This may include other music file managers, other export methods from iTunes, or even handy bash one-liners. The point was not to give an optimal method for organizing playlists but to show the process by which a bash script evolves to solve a simple one-off task. 

Coughing at Classical Concerts

concert_2464934bNot being an opera fan myself I will take their word for it:

Classical concerts comes with a set of very strict rules for the public: you cannot applaud while the music plays (the only exception being after opera arias), you are supposed to dress up, and there should be complete silence from the audience during the performance. And that urge to cough should be repressed until an applause. Yet, it turns out that coughing is more frequent during the performance.

Here’s the abstract from Andreas Wagener’s paper on the topic:

Concert etiquette demands that audiences of classical concerts avoid inept noises such as coughs. Yet, coughing in concerts occurs more frequently than elsewhere, implying a widespread and intentional breach of concert etiquette. Using the toolbox of (behavioral) economics, we study the social costs and benefits of concert etiquette and the motives and implications of individually disobeying such social norms. Both etiquette and its breach arise from the fact that music and its “proper” perception form parts of individual and group identities, convey prestige and status, allow for demarcation and inclusion, produce conformity, and affirm individual and social values.

Micro-institutions indeed.

See also: Miller and Page on the “Standing Ovation Problem”

Wednesday Nerd Fun: Games (and More) in Stata

Stata is a software program for running statistical analysis, as readers who have been to grad school in the social sciences in the last couple of decades will know. Compared to R Stata is like an old TI-83 calculator, but it remains popular with those who spent the best years of their lives typing commands into its green-on-black interface. I recently discovered that Stata shares one important feature with the TI-83 calculator: the ability to play games. (For TI-83 games, see here and here.)

Eric Booth of Texas A&M shares this implementation of Blackjack in Stata:

The game is played by typing -blackjack- into the command window and then the game prompts the user for the amount she wants to bet (default is $500 which replenishes after you lose it all or you exit Stata), and whether to hit or stay.  It doesn’t accurately represent all the rules and scenarios of a real game a blackjack (e.g., no doubling down), so don’t use it to prep for your run at taking down a Vegas casino.

Booth’s blog provides other fun, unconventional uses of Stata as well. There’s a script that lets you Google from the Stata interface, one that lets you control iTunes, and even one for running commands from an iPhone.

This post is probably less “general interest” than most of the nerd fun posts, but I hope you enjoyed it.

Micro-Institutions Everywhere: Top Forty Radio

Top Forty radio was invented by Todd Storz and Bill Stewart, the operator and program director, respectively, of KOWH, an AM station in Omaha, Nebraska, in the early fifties. Like most music programmers of the day, Storz and Stewart provided a little something for everyone. As Marc Fisher writes in his book “Something in the Air” (2007), “The gospel in radio in those days was that no tune ought to be repeated within twenty-four hours of its broadcast—surely listeners would resent having to hear the same song twice in one day.” The eureka moment, as Ben Fong-Torres describes it in “The Hits Just Keep on Coming” (1998), occurred in a restaurant across from the station, where Storz and Stewart would often wait for Storz’s girlfriend, a waitress, to get off work. They noticed that even though the waitresses listened to the same handful of songs on the jukebox all day long, played by different customers, when the place finally cleared out and the staff had the jukebox to themselves they played the very same songs. The men asked the waitresses to identify the most popular tunes on the jukebox, and they went back to the station and started playing them, in heavy rotation. Ratings soared.

By the end of the decade, Top Forty was the most popular format in the nation. It thrived in the sixties, but began to struggle with the popularity of FM radio, and the rise of album-oriented rock, in the seventies.  [B]y the eighties it [top forty radio] could no longer claim to be America’s soundtrack.

In the past decade, however, Top Forty has come back stronger than ever….  Paradoxically, in an age when an unprecedented range of musical genres is easily available via the Internet, the public’s appetite for hits has never been greater.… In New York City, contemporary hit radio now dominates FM stations, a remarkable turn of events for anyone old enough to remember when FM radio was the antithesis of Top Forty.

From The New Yorker, an article worth reading in full. For more on media strategy, see this post from February.

[via Cheap Talk]

Wednesday Nerd Fun: Emojicons

Ever feel the need to express yourself with say, a bear flipping a table, and think ‘I wish there was an emoticon for that’? Well, look no further. Emojicons is a website dedicated to providing a repository of texto-graphical expressions for your every whim.

Here’s the aforementioned bear:

ʕノ•ᴥ•ʔノ ︵ ┻━┻

or maybe you’d rather chill out with head phones:

d-_-b

Whatever your fancy this week, take a minute to browse their site and see if you can come up with some other creative expressions. Bonus points for the first person who can sneak these into a footnote or the body text of a published journal article.