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

    BGM Music mpg123 with fade in/out

    Scheduled Pinned Locked Moved Ideas and Development
    cyperghostbgmbgm playercontrol
    15 Posts 4 Posters 4.0k 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.
    • cyperghostC
      cyperghost
      last edited by cyperghost

      Hello I played around with my ORA XU4 and Raspberry devices and took also a look to the python method with playing background music (the script needs some improvements) but I like the nice fade in/out it offers if it detects some emulators running.
      So this script actually works only for RetroPie on Raspberry and supports mpg123 (or any other music player)

      So I took one and a half hour to write a small script that offers the same for the mpg123 method that was introduced by @synack.
      The How to install and setup mpg123 was wrapped by @MapleStory in the thread Quick and easy guide for adding music to Emulatonstation on RetroPie. Noob friendly!

      GetIt: So here is the script

      You can download it also from my GITHUB account here
      or use command wget https://raw.githubusercontent.com/crcerror/RetroPie-Shares/master/BGM_vol_fade.sh and make it executable.

      Install

      We can now use -stop and -cont parameter to force stoping and resuming music.

      1. type nano /opt/retropie/configs/all/runcommand-onstart.sh
      2. Write just scriptname with exact download location. Maybe $HOME/scripts/BGM_vol_fade.sh -stop
      3. type nano /opt/retropie/configs/all/runcommand-onend.sh
      4. Add line like (sleep 2 && $HOME/scripts/BGM_vol_fade.sh -cont) &
        4.1 This will load the voluming fading into background
        4.2 This will avoid sound corruption caused by CPU overload and gives a better retro experience (Believe me!)
      5. Please report and give some feedback!

      Sidenote: The GitHUB version will likely be more updated than this script here!


      OLD CODE
      Please use GITHUB version instead!

      #!/bin/bash
      
      # A script to fade ALSA volume when starting or ending
      # a Emulator by using amixer and MASTER/PCM Control.
      # kill -19 pauses musicplayer, kill -18 continues musicplayer
      # Place the script in runcommand-onstart.sh to fade-out
      # Place the script in runcommand-onend.sh to fade-in
      # Status of musicplayer is determinated automatically
      #
      # by cyperghost
      # 2018/10/31 - Halloween
      
      # Reason I like the pyscript for BGM but it has it flaws and caveeats
      # so I recommend the mpg123 method brought by synack
      # Read here how to setup https://retropie.org.uk/forum/topic/9133
      
      # Setup Musicplayer and Channel you want to change volume here 
      readonly VOLUMECHANNEL="PCM"
      readonly MUSICPLAYER="mpg123"
      
      # Get ALSA volume value and calculate step
      VOLUMEALSA=$(amixer -M get $VOLUMECHANNEL | grep -o "...%]")
      VOLUMEALSA=${VOLUMEALSA//[^[:alnum:].]/}
      VOLUMESTEP=$(expr $VOLUMEALSA / 10)
      
      # ALSA-Commands
      VOLUMEZERO="amixer -q -M set $VOLUMECHANNEL 0%"
      VOLUMERESET="amixer -q -M set $VOLUMECHANNEL $VOLUMEALSA%"
      
      # Player-Status
      PLAYERPID="$(pidof $MUSICPLAYER)"
      PLAYERSTATUS=$(ps -ostate= -p $PLAYERPID)
      
      if [[ $PLAYERSTATUS == *S* ]]; then
          # Fading down and pausing in ten steps
          for a in {0..9}; do
              amixer -q -M set "$VOLUMECHANNEL" "${VOLUMESTEP}%-"
              sleep 0.2
          done
      
          $VOLUMEZERO
          kill -19 $PLAYERPID
          sleep 0.5
          $VOLUMERESET
      
      elif [[ $PLAYERSTATUS == *T* ]]; then
          # Playing and fading up in ten steps
          $VOLUMEZERO
          sleep 0.5
          kill -18 $PLAYERPID
          for a in {0..9}; do
              amixer -q -M set "$VOLUMECHANNEL" "${VOLUMESTEP}%+"
              sleep 0.2
          done
          $VOLUMERESET
      else
          echo "Musicplayer: $MUSICPLAYER is not running."
      fi
      
      S 1 Reply Last reply Reply Quote 3
      • ExarKunIvE
        ExarKunIv
        last edited by

        will this work for retropie on the Pi3 or is it just for ORA/XU4?

        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 .

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

          @ExarKunIv It's just tested on a Raspberry.
          I think for Odroid there should be a change in the channel-Volume and you need to install the volume mixer device..... I think XU4 uses pulseaudio so the mixer device is annother one.

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

            ok thanks

            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 .

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

              Did a nearly complete rewrite with some nice features. Please update from github!

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

                question for the onend and onstart

                do we put your command before or after what will be in there to start with just to get the mpg123 working?

                UPDATE Never mind i got it to work now.

                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
                • cyperghostC
                  cyperghost
                  last edited by

                  I've updated the script a bit
                  We can now use -stop and -cont parameter to force stoping and resuming music.

                  Updated first topic to

                  1 Reply Last reply Reply Quote 0
                  • S
                    strunx
                    last edited by strunx

                    hi,

                    i tried to use this fade in/out script with the GPi Image from supremeteam (basic-image with mpg123 backgroundmusic, no roms and bios), but it didn't work. Something must be incompatible. I think it cannot be a difficult thing, but i am a noob at all. Can somebody help me please? I tried to change the VOLUMECHANNEL-variable in the fade-script to pcm.monocard, but not getting a fade with that...

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

                      @strunx yes you have to identify the audio channel at first - what name is showen in EmulationStation if you enter its sound menu? It should be next to the sound volume..

                      S 1 Reply Last reply Reply Quote 0
                      • S
                        strunx @cyperghost
                        last edited by strunx

                        @cyperghost thx for fast answering.
                        in emulationstation:
                        AudioCard: DEFAULT
                        Audio Device: PCM

                        my runcommand-onstart.sh:

                        #!/bin/sh
                        home/pi/scripts/BGM_vol_fade.sh -stop

                        my runcommand-onend.sh:

                        #!/bin/sh
                        (sleep 2 && /home/pi/scripts/BGM_vol_fade.sh -cont) &

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

                          @strunx Do you know how to execute commands?

                          Try bash /path/to/your/script/BGM_vol_fade.sh
                          Or if the script is already in same directory you are working with then just bash BGM_vol_fade.sh

                          From your description it should work out of the box. Then write back what the error is.
                          How did you get the script? with wget command?
                          And is the script located to /home/pi/scripts?

                          PS: The Shebang thing (#!...) is not needed!

                          S 1 Reply Last reply Reply Quote 0
                          • S
                            strunx @cyperghost
                            last edited by strunx

                            @cyperghost

                            result of bash BGM_vol_fade.sh:

                            amixer: Unable to find simple control 'pcm',0ยด

                            typing: amixer -> result:

                            Simple mixer control 'PCM',0
                            Capabilities: pvolume pvolume-joined pswitch pswitch-joined
                            Playback channels: Mono
                            Limits: Playback -10239 - 400
                            Mono: Playback -2046 [77%] [-20.46dB] [on]

                            script is located to /home/pi/scripts

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

                              @strunx Well usually it should work .... I've no Pi0 at hand and can't really help now. Maybe reinstalling alsa-utils will help. I think that nobody can support here hence you are using a premade ROM and nobody knows which build this is and what "special" patches were set.

                              I personally think it's not a big deal. Maybe you could try MASTER as volume input.

                              S 1 Reply Last reply Reply Quote 0
                              • S
                                strunx @cyperghost
                                last edited by strunx

                                @cyperghost i will try it out ... thx for helping so fast

                                1 Reply Last reply Reply Quote 0
                                • ExarKunIvE ExarKunIv referenced this topic on
                                • ExarKunIvE ExarKunIv referenced this topic on
                                • ExarKunIvE ExarKunIv referenced this topic on
                                • ExarKunIvE ExarKunIv referenced this topic on
                                • ExarKunIvE ExarKunIv referenced this topic on
                                • ExarKunIvE ExarKunIv referenced this topic on
                                • ExarKunIvE ExarKunIv referenced this topic on
                                • S
                                  sleve_mcdichael @cyperghost
                                  last edited by

                                  @cyperghost I have a working scriptmodule that installs mpg123 and sets up the background music automatically. May I have your blessing to include a modified version of this volume fade script (with credit) in the distribution?

                                  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.