This is a guest post by Jeremy London. Jeremy is a lawyer, father and hobby programmer who recently discovered the convenience of ACRCloud.
I work as a lawyer but I’ve always been mathematically minded, and learning programming had been on my To Do list for a while. In mid-2016, my wife and I had our second child. Naturally, I had to spend a lot of time with the baby, soothing and calming, shushing and patting, rocking and pacing — mind-numbing and repetitive.
I wanted to make the most of this time. And I don’t mean treasuring it so I can always look back fondly on hours spent walking in circles to the sound of wailing. I meant finding something else useful to do at the same time. At first, I played some brain training games on my phone in my free hand. But then I remembered my goal to one day learn programming. After a little bit of research, I chose Python, because it is high level and pretty easy for beginners to get into. So I downloaded an app to learn from.
I gradually gained experience by finding increasingly complex personal projects that interested me — from basic cyphers to text-based versions of simple board games to web scraping. I was always on the lookout for new project ideas that matched my capability.
In about the middle of 2017, I heard a radio station promoting the fact that it has a “no repeat workday”, meaning that it would not play any song in full more than once between the specified hours — other than on rare occasions when it would repeat a song “accidentally”, in which case listeners could call in to try win prize money. My brain cogs started whirring. Wouldn’t it be a great use of my programming ability to automatically listen to that station all day with perfect recall? It would be my biggest challenge yet, but if I could pull it off, it would be a cinch to spot a repeated song. The only part I couldn’t guarantee was being the first person to call in. But if I could send myself a text message then I’d be able to call in each time there was a repeated song, so I’d surely win eventually, right? I also realised there are at least two radio stations in my city that had no repeat workday competitions. Even better! I wondered if I could listen to both at once and double my chance of success…
I started working on how to break down the programming challenge into sub-programs. I needed four components:
- Providing the radio station’s broadcast as input to my program;
- Analysing the radio station’s sound to identify the relevant songs;
- Determining whether a song had been played already that day; and
- Sending myself a text message if my program found a repeat.
Hmm, it looked like a mix of pretty easy tasks and very hard tasks. I was already aware of an online service that made sending myself a text message easy, and I’d tried it out before with success, so Step 4 wouldn’t be a problem. It was also clear that once I’d identified a song in Step 2, it would be easy to record the song’s details in a variable then check if a later song was already recorded, so Step 3 wouldn’t give me any trouble either. No, it was the first two steps I was worried about… until I found ACRCloud and it neatly solved my problems…
I knew from the outset that the part of my program that would analyse sound to identify songs — Step 2 — would be doing the program’s heavy lifting. I also knew this task was way beyond my capabilities. However, while I had never interacted with an API before, I had a vague idea about what they are, and I hoped I could find one to use for this purpose. I knew I would have no chance of success otherwise.
My first idea was to leave my phone next to a radio all day with the Shazam app open, which was obviously far from ideal. I knew most radio stations played their broadcasts online. Could I download Shazam or an equivalent service onto my PC and directly feed it a radio station’s digital broadcast? I didn’t think I could do that with Shazam. Would I have to have Shazam running and listening through the PC’s microphone to the PC’s speakers playing the online radio station’s broadcast? That didn’t sound much better than having a radio playing next to my phone.
I started searching for an alternative to Shazam that could listen to online radio stations. I found ACRCloud and Gracenote. I looked into both and ended up choosing ACRCloud because it seemed easier to understand and work with. It also seemed to have plenty of recommendations online.
Learning to use ACRCloud, but being inexperienced in this field, I was initially confused by some of the terminologies — what’s the difference between music recognition and broadcast monitoring? Between channels, streams, buckets and projects? But the documentation is well set out, so I quickly overcame this issue. I needed the broadcast monitoring service; I had one project, using the default ACRCloud Music bucket (ie database); channels are irrelevant to me (they’re for detecting live TV stations); and I had two streams, one for each online radio station I wanted to monitor.
Especially helpful in getting to grips with ACRCloud’s API was ACRCloud’s GitHub page, which has example scripts in several programming languages of how to use various features of the API. Later, once I really got my head around it, I modified the demos that I had incorporated into my program by creating an ACRCloudConsole class (object) that contained all the functions that relate to interacting with my ACRCloud account, such as removing, adding, pausing or restarting streams (radio stations), and an ACRCloudStreamMonitor class that contained all the functions that relate to querying the broadcast monitoring service, such checking the last song played. The advantage of this is that my program can accept an instance of the ACRCloudConsole class, which contained my account access key and password, and an instance of the ACRCloudStreamMonitor class, which contained a stream’s identification number. This saved me having to pass these variables each time I called a function.
As to Step 1, obtaining the broadcast, part of that step was resolved by discovering ACRCloud, which helped me determine that I would indeed be using an online radio station, as mentioned above. That should have been the end of my difficulties with that step, but I was surprised to find that I ended up spending far longer than I’d expected in trying to find the right URLs for the online radio stations to pass to ACRCloud for monitoring. That was just a matter of inexperience though — I now know what to look for in an online radio station’s webpage’s source code.
Eventually, my program was almost ready to launch. It’s divided into three files. The first one contains the ACRCloud classes mentioned above. The second one contains a class representing a No Repeat Workday competition, with methods to check whether the competition is currently running, and if so, to call the appropriate ACRCloud function to switch on the stream’s monitoring if it’s not running, then record the songs as they’re played and contact me if there’s a repeat. The third one is an interface to let me run multiple competitions at once.
However, because there is, understandably, a fee for using ACRCloud, I wanted my program to be as complete and perfect as I could get it before subscribing and going live. This included wanting to ensure that my program properly logged its activity so I could run it while at work and review its actions at the end of the day. I’d done basic logging before but my usage here was a big step up, and really bogged me down. I also had some personal issues taking up my attention around this time, such as being burgled. I was under some time pressure though, because both radio stations’ competitions ended at the end of 2017, so I wanted to get up and running as soon as possible to maximise my chance of winning. Even then, it took me until the end of October 2017 to get ready.
And it worked! Sort of. On my first day, I got many emails (I’d realised emails were a good enough, and cheaper, substitute for text messages) from myself throughout the day, altering me to supposedly repeated songs — more than one an hour for each station. I called the stations on and off during the day but never got through, presumably because these were false positives, so no one was answering the competition lines because there was no reason to call in.
After spending a little bit of time looking more closely at ACRCloud’s documentation, I realised my mistake and how to fix it. I was aware that radio stations often play a few seconds of an upcoming song but had tried to prevent this causing false positives by having my program poll the API every 20 seconds and only count a song as having been played if it was still playing 20 seconds later. However, ACRCloud’s broadcast monitoring API can provide the Current Song or the Last Song. I hadn’t been very clear on the difference between the two and had used Last Song because I understood that Current Song would not always give a result. When I revisited this issue, I realised that that was exactly what I wanted. Last Song will keep responding with the last song identified until a new one is identified, but Current Song responds with “no result” if no recognisable song is playing. By using Last Song, my program had thought a song was still playing after 20 seconds, even if the station had actually stopped playing the song and was, say, broadcasting an advertisement or talkback. Switching to Current Song neatly solved this issue, because “no result” prevents misleading consecutive identical results. My program has been running well ever since.
So, did I win? I thought it was almost a sure thing, since I’d be able to call in immediately every single time either of the stations repeated a song, and I’m the only person who was “listening” to two radio stations at once, every minute of the day. I figured that even if I wasn’t always quick enough to call in straight away, I’d have a strong chance of winning eventually. But I overestimated how often the stations break their guarantees. At the time of writing, three workdays before the end of 2017, I’m confident that neither station has broken its guarantee over the last two months. Surely my time will come in the next day or two…
If not, I bet stations in other cities have similar competitions…