Music utility

Domo is required to play music from a local library of song files. The reason why we are using a local library is because music APIs only allow a 30 second song preview, not the entire song.  Metadata (artist, album, genre, etc) for each song is inputted by the user and stored for future use. Before each song can be played, the user will use ‘add_music.py’ to store metadata about the song.

song_name = raw_input("Please enter song name: ")
artist_name = raw_input("Please enter artist name: ")
album_name = raw_input("Please enter album name: ")
genre = raw_input("Please enter song genre: ")
metadata = '{\'genre\': \'' + genre + '\', \'song_name\': \'' + song_name + '\', \'album_name\': \'' + album_name + '\', \'artist_name\': \'' + genre + '\'}'

with open('catkin_ws/src/music/scripts/metadata.txt', 'a') as inputfile:
    inputfile.write(metadata)

The code above takes in user input for each metadata field and then writes to the file ‘metadata.txt’ to store this information. Now, ‘metadata.txt’ can be accessed in the future to retrieve information about the song.

If the user realizes they made a mistake of misspelling an artist name or listing the incorrect genre of music, they can edit the metadata text file as well.

song_name = raw_input("Please enter new song name: ")
artist_name = raw_input("Please enter new artist name: ")
album_name = raw_input("Please enter new album name: ")
genre = raw_input("Please enter new song genre: ")
metadata = '{\'genre\': \'' + genre + '\', \'song_name\': \'' + song_name + metadata = '{\'genre\': \'' + genre + '\', \'song_name\': \'' + song_name + 'artist_name\': \'' + genre + '\'}'
f = open('/home/alex/catkin_ws/src/music/scripts/metadata.txt','w')
for line in lines:
    data = ast.literal_eval(line)
    if data['song_name'] != song:
        f.write(line)
f.write(metadata)

The code above reads in a new line of metadata input from the user. It then iterates through the current list of songs in ‘metadata.txt’, keeping every line of song information except for the one containing the song that we are trying to edit. Then we read into the file the new line of metadata that the user just inputted, so we have now replaced the old metadata for that song with the new metadata.

with open('metadata.txt') as inputfile:
    for line in inputfile:
        dictionary = ast.literal_eval(line)
        if(dictionary['song_name'] == song and os.path.exists("catkin_ws/src/music/scripts/" + song + ".wav")):
            print 'Song: ' + dictionary['song_name']
            print 'Artist: ' + dictionary['artist_name']
            print 'Album: ' + dictionary['album_name']
            print 'Genre: ' + dictionary['genre']
            song_found = True
            pygame.mixer.init()
            pygame.mixer.music.load('catkin_ws/src/music/scripts/' + song + '.wav')
            pygame.mixer.music.play()
            while pygame.mixer.music.get_busy() == True:
                continue

The code above iterates through ‘metadata.txt’ until it finds the desired song name and prints all available information about the song. To play the actual song, I used pygame to play a ‘.wav’ music file which continues playing until either the song ends or the computer is interrupted.

If the user wants to play their music on shuffle, songs are randomly selected from the ‘metadata.txt’ file.

if(song == 'Random' or song == 'random'):
    with open('/catkin_ws/src/music/scripts/metadata.txt') as inputfile:
        for line in inputfile:
            lines += 1
            num = randint(0, lines - 1)
    with open('/catkin_ws/src/music/scripts/metadata.txt') as inputfile:
        for line in inputfile:
        dictionary = ast.literal_eval(line)
        if(counter == num):
            song = dictionary['song_name']
            break;
        counter += 1

The code above counts the number of songs the user has by counting how many different lines there are in the metadata file and then randomly selects a number which corresponds to the song that will be played. It then reiterates through the metadata file to reassign the song name to the corresponding randomly selected song.