A Go program displays song lyrics line by line

Programming Snapshot – Go Lyrics

© Lead Image © U.P. Images, Fotolia.com

© Lead Image © U.P. Images, Fotolia.com

Article from Issue 241/2020
Author(s):

Bathtub singer Mike Schilli builds a Go tool that manages song lyrics from YAML files and helps him learn them by heart, line by line.

Anyone can strum three chords. What I really admire about musicians is their ability to sing all of their often lengthy lyrics by heart. Having said this, there are some hilarious examples of the massive divide between what the artists originally sang and what the fans thought they heard.

Take the Eurythmics song "Sweet Dreams," for example; although some people's sweet dreams may be made of cheese, it's not what Annie Lennox and Dave Stewart had in mind. Or, keeping to a foodie theme, there's the hilarious mishearing of the 1980s Starship classic "We built this city on sausage rolls," – the city in question being San Francisco, my own place of residence, which of course is more famous for rock and roll.

As a portable tool to help budding singers learn lyrics by heart, the command-line tool in Go in this issue shows a list of lyrics stored as YAML files for selection. After pressing the Enter key to select a song, you can also press Enter to click through the lyrics line by line and try to remember what comes next before you continue.

Retro Look

The tool runs on the command line, so stressed sys admins can have a quick sing while a lengthy command is running in another window. You may notice that the aesthetics are reminiscent of the '80s with MS-DOS. Just like '80s cars like the Scirocco, a lot from that era is making a comeback in 2020.

As in some previous issues, I will be using the termui package, which is based on curses and runs identically on Linux and macOS. After the program launches, the tool reads all the YAML files in the data/ directory. As shown in Listing 1, the lyrics files define fields for artist, song, and text. The latter is a multi-line field initiated by a pipe character, and the field data continues until the text indentations stop, or the file ends.

Listing 1

zztop.yaml

 

You can create these files by copying and pasting lyrics from websites that come up when you search for a track on Google. The terminal UI of the compiled lyrics program first displays a list of tracks and their artists (Figure 1). Using the arrow keys (Vim enthusiasts can use K and J if they prefer), you can then scroll through the list and press Enter to open the selected title.

Figure 1: The list box in the main menu provides a selection of tracks.

Line by Line

After you make a selection, the UI enters lyrics mode, displaying the first line of the song, and moving down one line each time the Enter key is pressed (Figure  2). If you get tired of the song, pressing Esc takes you back to the main menu. The same thing happens when you press Enter after the last line of the song.

Figure 2: The ltext widget, which is also implemented as a list box, reveals the lyrics line by line.

Listing 2 defines the views for the two different modes as two list boxes from the termui widget collection; they both use SetRect() to claim exactly the same fully-sized rectangle within the terminal window. The UI later detects the current mode and brings the correct list box to the front. The size of the active terminal is determined by the TerminalDimensions() function in line 29. The UI uses the values for width (w) and height (h) obtained from the call to spread out over all available screen real estate.

Listing 2

lyrics.go

 

As its first action, Listing 2 calls the songFinder() function in line 11, which collects the YAML files and returns them as a data structure in lyrics. It then initializes the UI with ui.Init() and uses the following defer statement to make sure that Go closes down the whole enchilada again as soon as the main program terminates. This is important, because if the terminal stayed in graphic mode after the program abruptly terminated itself, the user would see garbled characters and be unable to use it for typing future shell commands.

Organizing Structures

The lyrics data structure is a Go map that references the YAML data of individual tracks for fast lookups, using a string key consisting of a combination of the artist and title. Both the data structures of the individual songs and the map of the song collection will be defined later in Listing 3, but since all three listings implement the same Go package main, they are allowed mutual access to each other's constructs.

Listing 3

find.go

 

In Listing 2, the for loop starting in line 24 assembles the list of entries in the main menu as an array slice of strings that it generates from the lyrics map's keys. The keys in the map are by definition unsorted; therefore, the sort.Strings() function from the standard library puts the string list in alphabetical order in line 27. Go's built-in Sort() function sorts an array slice of strings in place (i.e., it actually modifies the input array instead of producing a new, sorted version).

Listing 2 now only needs to take care of defining bright colors for active and passive list box entries and handing over the downstream processing of input and UI display to the handleUI() function shown in Listing 4. When the function returns, it's because the user has pressed Q and wants to end their singing lesson. The main program reaches the end of the code, dismantles the UI based on the previously defined defer statement, and terminates.

Listing 4

uihandler.go

 

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Waxing Lyrical

    Whether you listen to music on Spotify or a classic audio player like Rhythmbox, Lollypop, or Audacious, a tool named Lyrics-in-terminal will let you read the lyrics for the track you are currently playing.

  • ChordPro

    If you play a stringed instrument and want to record lyrics and chord changes, ChordPro gives you an elegant and convenient approach to getting it all on paper.

  • Introduction

    This month in Linux Voice.

  • YouTube Players

    YouTube offers more than just funny kitten movies; you will also find more than 60 million music videos. With a native YouTube client for Linux, you can use this online jukebox as conveniently as your local music collection.

  • Sayonara Player

    For a simple audio player, check out Sayonara Player, a great choice for enjoying all your favorite music, Internet radio, and podcasts.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News