RetroPie forum home
    • Recent
    • Tags
    • Popular
    • Home
    • Docs
    • Register
    • Login
    Please do not post a support request without first reading and following the advice in https://retropie.org.uk/forum/topic/3/read-this-first

    BGM, vcgencmd force_audio, and mpg123 &

    Scheduled Pinned Locked Moved Help and Support
    audiochannelbgmmpg123
    3 Posts 1 Posters 516 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.
    • S
      sleve_mcdichael
      last edited by sleve_mcdichael

      I am trying to set up background music on my RPi4 using mpg123.

      It's working great, there's only one hang-up left. When it first starts up, it cuts off the first second of the first track. It happens even just running mpg123 from the console. I've tracked it down to how the audio channel isn't opened until it detects an audio stream. At that point it opens the channel and starts streaming but not before the first moments of the stream are lost.

      I can solve this by forcing the channel open with vcgencmd force_audio hdmi 1 before streaming the audio. This opens the channel early, allowing the track to play from the first instant.

      It also introduces a low "hum" in my speakers that is not present when the channel is closed. (It is, and has been even before I added music, always present when EmulationStation is open, for example, because it does use the audio channel, but when I quit to console there is -- or was -- no sound at all. Now with the channel fixed open, I get the hum in the console after I exit.)

      If I then do vcgencmd force_audio hdmi 0 it will allow the channel to close but will not close it immediately; the "hum" persists until another audio source plays and stops, and then the audio channel is closed and my speakers go silent.

      So I guess I need to force open the channel, start the audio, and then "unlock" the channel again at some point prior to killing the audio player so that it can close when the player does.

      Testing this by starting ES and letting the bgm start up natively quickly became tedious as I waited for ES to load each time. I thought I would test it from command line by just starting mpg123 with an & like it's done in the scripts:

      mpg123 path/to/my_song.mp3 &
      

      ...but when I do that, the player starts "stopped." No audio plays, and it won't respond to pkill mpg123, I've had to kill it specifically with the -KILL flag with pkill -KILL mpg123 or logout twice, because the first time just tells me "there are stopped jobs" and doesn't log me out.

      So I guess my question is, ultimately, "where should I put the vcgencmd force_audio commands so the channel opens before playback starts and closes when it stops?" but I could probably make some progress on that myself, if I understood why/how to avoid the player being "stopped" when I run it from command line in the first place.

      ...or, is there another/better way to accomplish this than using vcgencmd force_audio at all?

      1 Reply Last reply Reply Quote 1
      • S
        sleve_mcdichael
        last edited by

        Update: I had some more time to test this today, but I didn't make much progress.

        If I just open the channel and then close it immediately, it does go silent:

        vcgencmd force_audio hdmi 1
        # channel opens, hum begins
        
        vcgencmd force_audio hdmi 0
        # channel closes, hum ceases
        

        But if I start the player while it's forced open, then it starts up from the first instant like I want, but doesn't close properly after:

        vcgencmd force_audio hdmi 1
        # channel opens, hum begins
        
        mpg123 path/to/my_song.mp3
        # song plays from the first instant
        # I press Q to quit the player
        # hum persists (expected)
        
        vcgencmd force_audio hdmi 0
        # hum persists (*NOT* expected or desired)
        
        mpg123 path/to/my_song.mp3
        # channel closes (hum ceases)
        # audio stream begins (no output)
        # audio stream detected, channel opens (hum begins)
        # song plays from ~1 second in
        # I press Q to quit player
        # channel closes (hum ceases)
        

        As shown, when another source does play, the channel closes momentarily before reopening (late) for the new stream.

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

          Still not sure why the things I was trying from command-line didn't work, but this seems to be getting the job done pretty reliably:

          autostart.sh:

          (vcgencmd force_audio hdmi 1 >/dev/null 2>&1; sleep 8; mpg123 -Z /home/pi/RetroPie/bgm/*.mp3 >/dev/null 2>&1) & #bgm
          emulationstation #auto
          

          .bashrc (excerpt):

          [[ $(tty) == "/dev/tty1" ]] && (vcgencmd force_audio hdmi 0 >/dev/null 2>&1; pkill mpg123) #bgm
          
          # RETROPIE PROFILE START
          

          ...this seems to work as intended, even though I couldn't get it to work on command-line for whatever reason. With this setup the audio channel opens in time for the music to begin, and closes again when I quit to console.

          In autostart.sh, I put vcgencmd before the sleep command because when I did the other way, sometimes it would open up in time for the music but other times it wouldn't. This way it opens up and "hums" for a few seconds before the music starts, but it always starts from the first instant, so an acceptable compromise.

          In .bashrc, the original guide said to put the command inside the RETROPIE PROFILE section but that whole section gets rewritten if you update bashwelcometweak; putting it outside the section preserves it even when the welcome tweak is updated.

          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.