RetroPie forum home
    • Recent
    • Tags
    • Popular
    • Home
    • Docs
    • Register
    • Login

    Quick and easy guide for adding -EMULATED- music to Emulationstation on RetroPie

    Scheduled Pinned Locked Moved General Discussion and Gaming
    bgmguidemusicmusic ripsvgm
    22 Posts 11 Posters 6.7k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Z
      zerojay
      last edited by zerojay

      I noticed a lot of people who really enjoy using game music soundtracks as background music for EmulationStation. It's really awesome however the filesize of mp3s can add up pretty quickly especially if you like having an extensive list of random tracks and each are 3-7MB. That can be a problem when you're running RetroPie on an SD card where space is at a premium. This guide is for playing VGM files. VGM files are basically like MIDI files, essentially a list of instructions that are used to tell the sound card on your computer which instruments to use, how loud to play, and because none of the actual instrument sound samples are in the MIDI file, they can be really long while being really tiny.

      VGM files are in the same vein as MIDI files except that instead of playing through your soundcard directly, they are passed on to emulated versions of the audio chips from various game consoles and arcade machines. The VGM files themselves are essentially recordings - not of the audio itself - but of the instructions passed to the audio hardware by the games. This allows them to stay tiny and small while still sounding exactly like the originals that you are expecting.

      This instruction guide is basically a version of https://retropie.org.uk/forum/topic/9133/quick-and-easy-guide-for-adding-music-to-emulatonstation-on-retropie-noob-friendly/36 but we'll be using a different application instead of mpg123. Shoutouts and credits to @MapleStory and @synack for their work on the above instructions. As they work really well, I'll just be making adjustments to their instructions where needed. (Why fix something if isn't broken, right?)

      Everything written in code should be done in terminal (or via putty) unless otherwise stated.

      Now on to the guide!

      Step 1 - Install vgmplay script from the RetroPie-Extra repo.

      sudo wget https://raw.githubusercontent.com/zerojay/RetroPie-Extra/master/scriptmodules/ports/vgmplay.sh -O /home/pi/RetroPie-Setup/scriptmodules/ports/vgmplay.sh
      

      Step 2 - Install vgmplay through RetroPie-Setup and mpg123 through apt

      Run RetroPie-Setup, Manage Packages and you'll find vgmplay under experimental. Install from source (as there is no binary available).

      Run the following in a shell to install mpg123:

      sudo apt-get install mpg123
      

      Step 3 - Edit Autostart.sh

      Edit autostart.sh (note: this is not meant to be a link)

      sudo nano /opt/retropie/configs/all/autostart.sh
      

      Once in nano, hit enter to create a line break above emulationstation #auto. Go up to the blank line and type/paste the following:

      while pgrep omxplayer >/dev/null; do sleep 1; done
      /opt/retropie/configs/all/generatem3u.sh
      (sleep 10; /opt/retropie/configs/all/playmusic.sh >/dev/null 2>&1) &
      
      

      NOTE 1: vgmplay works with m3u playlists and also does not support randomization so the generatem3u.sh script will first index all your .vgz and .vgm files from /home/pi/vgm/ and then randomize it before starting to play.
      NOTE 2: You can delay how long it takes for vgmplay to begin playing music on boot, which is handy for those with splashscreens. Just replace the 10 in the second line(sleep 10; with any value in seconds you'd like. As shown, it's currently set to a 10 second delay.

      Hit CTRL+O followed by Enter to save the file and CTRL+X to quit nano back to terminal.

      Step 4 - Creating onstart/onend scripts

      Create and edit the runcommand-onstart and onend files.

      sudo nano /opt/retropie/configs/all/runcommand-onstart.sh
      

      Once inside, type/paste the following:

      pkill -STOP vgmplay
      

      Hit CTRL+O followed by Enter to save the file and CTRL+X to quit nano back to terminal.

      sudo nano /opt/retropie/configs/all/runcommand-onend.sh
      

      Once inside, type/paste the following:

      pkill -CONT vgmplay
      

      Hit CTRL+O followed by Enter to save the file and CTRL+X to quit nano back to terminal.

      At the terminal, type/paste:

      sudo chmod a+x /opt/retropie/configs/all/runcommand-onstart.sh  
      

      Followed by:

      sudo chmod a+x /opt/retropie/configs/all/runcommand-onend.sh
      

      Step 5 - Create generatem3u script

      Create and edit the generatem3u.sh file.

      sudo nano /opt/retropie/configs/all/generatem3u.sh
      

      Once inside, type/paste the following:

      #!/bin/sh
      find /home/pi/vgm/ -type f -iname "*.vgm" > /tmp/temp.m3u
      find /home/pi/vgm/ -type f -iname "*.vgz" >> /tmp/temp.m3u
      find /home/pi/mp3/ -type f -iname "*.mp3" >> /tmp/temp.m3u
      cat /tmp/temp.m3u | shuf > /tmp/allfiles.m3u
      

      Hit CTRL+O followed by Enter to save the file and CTRL+X to quit nano back to terminal.

      Change the permissions for the script to allow it to be executed.

      sudo chmod a+x /opt/retropie/configs/all/generatem3u.sh
      

      Step 6 - Create playmusic.sh

      Create and edit the playmusic.sh script:

      #!/bin/bash
      
      while IFS='' read -r music || [ -n "${music}" ]; do
        if [ ${music: -3} = "vgz" ] || [ ${music: -3} = "vgm" ]; then
              /opt/retropie/ports/vgmplay/vgmplay "$music"
        elif [ ${music: -3} = "mp3" ]; then
              /usr/bin/mpg123 "$music"
        else
              echo "Unknown filetype in $music, skipping..."
        fi
      done < /tmp/allfiles.m3u
      

      Hit CTRL+O followed by Enter to save the file and CTRL+X to quit nano back to terminal.

      Change the permissions for the script to allow it to be executed.

      sudo chmod a+x /opt/retropie/configs/all/playmusic.sh
      

      Step 7 - Edit .bashrc

      Edit .bashrc

      sudo nano /home/pi/.bashrc
      

      Once inside, scroll to the very bottom of the file and create a line break under retropie_welcome but above # RETROPIE PROFILE END and type/paste the following:

      [[ $(tty) == "/dev/tty1" ]] && pkill vgmplay
      

      Hit CTRL+O followed by Enter to save the file and CTRL+X to quit nano back to terminal.

      Step 7 - Copy VGM files to /home/pi/vgm/

      Copy your vgm files to /home/pi/vgm/

      And you're finished! Now all that's left is to add your vgms to the /home/pi/vgm folder created by the RetroPie-Setup script via your favorite ftp client or usb. Upon reboot after adding some songs, your music should play automatically and will pause when a game is launched and resuming when you return to emulationstation.

      I've got soundtracks for 71 games, just over 2000 songs total and it's taking up just over 150MB total. Enjoy!

      BuZzB 1 Reply Last reply Reply Quote 6
      • BuZzB
        BuZz administrators @zerojay
        last edited by BuZz

        @zerojay You should use markdown code blocks for the snippets or else some characters are interpreted as styling and information is lost.

        To help us help you - please make sure you read the sticky topics before posting - https://retropie.org.uk/forum/topic/3/read-this-first

        1 Reply Last reply Reply Quote 0
        • Z
          zerojay
          last edited by

          @BuZz Yep, working on formatting now.

          1 Reply Last reply Reply Quote 0
          • MapleStoryM
            MapleStory
            last edited by MapleStory

            @zerojay

            Dude, this is amazing! Something that I've always wished for was the ability to use vgmplay for music, as the source material from consoles and arcade games are usually bite-sized in filesize and lossless in quality. I'll definitely put this to use tonight on my pi.

            1 Reply Last reply Reply Quote 1
            • A
              adjuntor
              last edited by

              I was cracking my head on why it was not working and realised you forgot to add this to step 5:
              sudo chmod a+x /opt/retropie/configs/all/generatem3u.sh

              Z 1 Reply Last reply Reply Quote 1
              • Z
                zerojay @adjuntor
                last edited by

                @adjuntor said in Quick and easy guide for adding -EMULATED- music to Emulationstation on RetroPie:

                I was cracking my head on why it was not working and realised you forgot to add this to step 5:
                sudo chmod a+x /opt/retropie/configs/all/generatem3u.sh

                I made the fix, thank you.

                1 Reply Last reply Reply Quote 0
                • Z
                  zerojay
                  last edited by zerojay

                  I'm planning on taking a second run at this. The main change would be that the playlist creation would allow mp3s and all the vgm file types and if it sees an mp3, it will play through mpg123. If it finds a vgm, it'll play with vgmplay. That way, you can have the best of both worlds with one script.

                  Also, I have an idea of how to do the music fade in/fade out on launch and quit of games so hoping to get that in too.

                  EDIT: Also fixed a minor coding mistake in generatem3u.sh.

                  Eric_McTrainshitE 1 Reply Last reply Reply Quote 1
                  • Eric_McTrainshitE
                    Eric_McTrainshit @zerojay
                    last edited by

                    @zerojay said in Quick and easy guide for adding -EMULATED- music to Emulationstation on RetroPie:

                    'm planning on taking a second run at this. The main change would be that the playlist creation would allow mp3s and all the vgm file types and if it sees an mp3, it will play through mpg123. If it finds a vgm, it'll play with vgmplay. That way, you can have the best of both worlds with one script.

                    That'd be nice for a multitude of reasons and would be highly appreciated.

                    Did you make any progress with that?

                    Eric_McTrainshitE cyperghostC 2 Replies Last reply Reply Quote 0
                    • Eric_McTrainshitE
                      Eric_McTrainshit @Eric_McTrainshit
                      last edited by

                      @zerojay I'm guessing that's a no. Hopefully you've been alright. Your activity has been kind of slow in recent months.

                      1 Reply Last reply Reply Quote 0
                      • B
                        brimby
                        last edited by

                        I'm getting permissions errors when I try to transfer files to the vgm folder. I tried running

                        sudo chown -R pi:pi vgm
                        sudo chown -R 777 vgm
                        

                        and I still got the same denial afterward

                        1 Reply Last reply Reply Quote 0
                        • B
                          brimby
                          last edited by

                          Whoops my issue was that I accidentally did chown instead of chmod for that second line. All good now.

                          1 Reply Last reply Reply Quote 0
                          • cyperghostC
                            cyperghost @Eric_McTrainshit
                            last edited by

                            @Eric_McTrainshit said in Quick and easy guide for adding -EMULATED- music to Emulationstation on RetroPie:

                            @zerojay said in Quick and easy guide for adding -EMULATED- music to Emulationstation on RetroPie:

                            'm planning on taking a second run at this. The main change would be that the playlist creation would allow mp3s and all the vgm file types and if it sees an mp3, it will play through mpg123. If it finds a vgm, it'll play with vgmplay. That way, you can have the best of both worlds with one script.

                            That'd be nice for a multitude of reasons and would be highly appreciated.

                            Did you make any progress with that?

                            Already done here
                            https://retropie.org.uk/forum/topic/19829/bgm-music-mpg123-with-fade-in-out

                            Eric_McTrainshitE 1 Reply Last reply Reply Quote 0
                            • D
                              djDarkX
                              last edited by

                              Sorry for the slight necro, but I just wanted to know if the music playing would be looped or not and if not, is there a way to loop it? I want to make my own background music in SPC format, kind of like SNES Classic, or NSF format, also like NES Classic, where it loops properly.

                              1 Reply Last reply Reply Quote 0
                              • Eric_McTrainshitE
                                Eric_McTrainshit @cyperghost
                                last edited by

                                @cyperghost That would allow me to play VGM and MP3 files?

                                cyperghostC 1 Reply Last reply Reply Quote 0
                                • cyperghostC
                                  cyperghost @Eric_McTrainshit
                                  last edited by

                                  @Eric_McTrainshit No with this script is is possible to realize fade in and fade out of any music. It has nothing to do with the player or file format itself.
                                  I assumed I wrote that comment because someone asked for feature like the python script for BGM has.

                                  1 Reply Last reply Reply Quote 0
                                  • Z
                                    zerojay
                                    last edited by zerojay

                                    Yeah, I've been gone awhile but I'm necroing this thread to say that I'm going back to working on my RetroPie-Extras repo and bringing it up to speed with Buster and RetroPie 4.6 and I'm starting with vgmplay. I seem to remember having made a script that allowed both mp3 and vgm to be played depending on the files found in the playlist so I don't think that will be an issue, but we'll see. I have another idea up my sleeve that might be interesting.

                                    EDIT: Everything in this thread still works with RetroPie 4.6 and with lower requirements on the Pi 4. :)

                                    1 Reply Last reply Reply Quote 0
                                    • Z
                                      zerojay
                                      last edited by

                                      I've switched the repository for vgmplay to a fork that removes the console UI which was causing breakage in something I was trying.

                                      I will be updating the instructions above soon to match the new vgmplay repository as well as adding support for playing mp3 files through mpg123 in the same playlist as the .vgm and .vgz files.

                                      ExarKunIvE 1 Reply Last reply Reply Quote 0
                                      • ExarKunIvE
                                        ExarKunIv @zerojay
                                        last edited by

                                        @zerojay sweet that you are back. cant wait to see what you do on your RetroPie-Extras.

                                        and i think ill give this BGM a try on my Pi4 once i go back to messing with it

                                        RPi3B+ / 200GB/ RetroPie v4.5.14, RPi4 Model B 4gb / 256gb / RetroPie 4.8.2
                                        RPi5 4gb / 512gb / RetroPie 4.8.9 -Basic
                                        Maintainer of RetroPie-Extra .

                                        1 Reply Last reply Reply Quote 0
                                        • Z
                                          zerojay
                                          last edited by

                                          I've updated the original post with the info for getting mp3 and vgm/vgz into the same playlists for those of you who want to use both.

                                          1 Reply Last reply Reply Quote 0
                                          • Z
                                            zerojay
                                            last edited by

                                            Minor fix for generatem3u.sh accidentally erasing the created playlist.

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post

                                            Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.

                                            Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.