Menu

#45 Add "stop play after X seconds" feature

closed-invalid
nobody
None
5
2021-05-07
2016-09-12
David Gray
No

I'm currently writing an app that plays audio at a set time (to eventually replace the bells at the school I work at). Our existing system turns on a relay for X seconds, then stops. I'd like a similar setup for mpg123 (which I shell out to, because of node.js' lacklustre audio support).

Would this be a possibility? I'm sure I could create a minute-long audio file then use setTimeout to kill the process, but having this all in-built would be a lot more reliable.

Discussion

  • Thomas Orgis

    Thomas Orgis - 2016-09-12

    Are you looping the ringtone? You do need different lengths of ringing?
    I see two options with existing mpg123:

    1. Have a short ringtone file (1 s) that loops smoothly, then use
      mpg123 --loop n ring.mp3 to have a ringtone for n seconds. This
      should work nicely since I added gapless decoding.

    2. Have a long ringtone file and only play the first s frames of it via
      mpg123 -n s ring.mp3. You need to work out the number of frames, but
      for normal MP3, it's 1152 samples per frame. Together with the sampling
      rate, you got your frames per second.

    Are you missing an option besides these two?

     
  • David Gray

    David Gray - 2016-09-12

    I did consider --loop, but in the past teachers have also ask that we play music, or something that isn't a loopable ring, so unless I manually trim the audio file to be X seconds, looping won't work.

    However -n might work. I'd have to work out the sampling rate and number of frames though.

    A third thought just occurred to me: Using -R and issuing a pause / stop command after X seconds, though I think that gets a bit tricky when trying to automate the whole process for reliable playback

     
    • Thomas Orgis

      Thomas Orgis - 2016-09-12

      Am Mon, 12 Sep 2016 08:26:55 +0000
      schrieb "David Gray":

      However -n might work. I'd have to work out the sampling rate and
      number of frames though.

      For a usual 44100 Hz file, one MP3 frame is 0.026 s.

      A third thought just occurred to me: Using -R and issuing a pause /
      stop command

      Nah, you have to do external timing and still have delay depending on
      audio and pipe buffers. It's untidy. Determining the proper length of
      audio to play is the thing.

       
  • Thomas Orgis

    Thomas Orgis - 2021-05-07
    • status: open --> closed-invalid
     

Log in to post a comment.