The officially official Devuan Forum!

You are not logged in.

#1 2021-06-20 16:22:31

zapper
Member
Registered: 2017-05-29
Posts: 853  

[SOLVED] how to randomly start audio file, via script,

With sox,

This is gonna sound weird, insane and odd,

But I wanted when I login, for a script to start that would randomly choose from one folder,

Say its folder 1, and say it has 4 folders, randomly choose one of those folders, and play an audio from one of those.

And also, if it has folders within the four folders, it will randomly choose one of them.

This sounds crazy, and why would I want it right?

I have an idea how to do a tiny bit of it, but not much.

I have found scripts ike this for thinkpads aka:

#!/bin/bash

declare -i ID
ID=`xinput list | grep -Eo 'TouchPad\s*id\=[0-9]{1,2}' | grep -Eo '[0-9]{1,2}'`
declare -i STATE
STATE=`xinput list-props $ID|grep 'Device Enabled'|awk '{print $4}'`
if [ $STATE -eq 1 ]
then
    xinput disable $ID
    echo "Touchpad disabled."
else
    xinput enable $ID
    echo "Touchpad enabled."
fi

the above script is for disabling the touchpad... smile

Without system dumb.

Anyways give me a holler if you know how to do the audio random playing thing. Aka, the random audio play script I want.

I am not a programmer, so I cannot even figure out how this can be done... ;P

Last edited by zapper (2021-06-20 16:23:36)


Freedom is never more than one generation away from extinction. Feelings are not facts
If you wish to be humbled, try to exalt yourself long term  If you wish to be exalted, try to humble yourself long term
Favourite operating systems: Hyperbola Devuan OpenBSD
Peace Be With us All!

Offline

#2 2021-06-20 23:11:48

ralph.ronnquist
Administrator
From: Battery Point, Tasmania, AUS
Registered: 2016-11-30
Posts: 1,117  

Re: [SOLVED] how to randomly start audio file, via script,

The script could be something simple, like

#!/bin/bash
AUDIO=( $HOME/folder*/*.{wav,mp3,ogg} )
LENGTH=${#AUDIO[@]}
PICK=$(( RANDOM % $LENGTH ))
play ${AUDIO[${PICK}]}

Then it depends on which display management you are using; with xfce4 you would go to "Settings":"Session and Startup" and add a new entry for "Application Autorstart" and there point at your executable script. That would set it up so that that script gets executed upon login.

If you want it tied to console login you might add it to your ".bashlogin", or similar depending on which shell you are using.

Offline

#3 2021-06-21 00:36:50

zapper
Member
Registered: 2017-05-29
Posts: 853  

Re: [SOLVED] how to randomly start audio file, via script,

ralph.ronnquist wrote:

The script could be something simple, like

#!/bin/bash
AUDIO=( $HOME/folder*/*.{wav,mp3,ogg} )
LENGTH=${#AUDIO[@]}
PICK=$(( RANDOM % $LENGTH ))
play ${AUDIO[${PICK}]}

Then it depends on which display management you are using; with xfce4 you would go to "Settings":"Session and Startup" and add a new entry for "Application Autorstart" and there point at your executable script. That would set it up so that that script gets executed upon login.

If you want it tied to console login you might add it to your ".bashlogin", or similar depending on which shell you are using.

Hmm, it doesn't seem to pick a random audio to play, always plays the same one when I tried it.

I was going to use jwm's method to autostart it to be honest.

I am curious, though beyond that, how would I make it only pick one of those audios to play before script stopping.

And also, if there is a way to add code behind before the audio part,  say there is one master folder and it has 5 different folders to choose from... I think you know where I am going with this right?

That and figuring out why it doesn't randomly select a different one each time I trigger it, is what I need to figure out.


Freedom is never more than one generation away from extinction. Feelings are not facts
If you wish to be humbled, try to exalt yourself long term  If you wish to be exalted, try to humble yourself long term
Favourite operating systems: Hyperbola Devuan OpenBSD
Peace Be With us All!

Offline

#4 2021-06-21 04:17:36

ralph.ronnquist
Administrator
From: Battery Point, Tasmania, AUS
Registered: 2016-11-30
Posts: 1,117  

Re: [SOLVED] how to randomly start audio file, via script,

Expanded example with some more audio paths, and also that I added "$" for "$RANDOM" just for symmetry (although it shouldn't be needed) and removed the braces for $PICK (also not needed).

#!/bin/bash
AUDIO=( 
    $HOME/folder*/*.{wav,mp3,ogg}
    $HOME/with/some/path/*.mp3
    /usr/share/sounds/alsa/*.wav
)
LENGTH=${#AUDIO[@]}
PICK=$(( $RANDOM % $LENGTH ))
play ${AUDIO[$PICK]}

In detail,

  • the AUDIO assignment is the collection of possible audio files, which in this example includes any files with extensions wav, mp3 or ogg in any home folder whose name starts with "folder", plus any mp3 file in the folder path "with/some/path" from the home folder, plus any wav file in (system) folder "/usr/share/sounds/alsa/";

  • the LENGTH assignment is the number of pathnames in the AUDIO list;

  • the PICK assignment is an integer between 0 and LENGTH-1 picked from a uniform distribution between 0 and 32767, modulo the length (which makes it favour lower numbers); and

  • the play command is of the PICK:th pathname of AUDIO list

I'm sure you can edit the audio paths to suit your needs.

But, there's something funny going on if it doesn't pick at random; perhaps a misspelling? But if you used Copy-and-Paste then that wouldn't be the case. The other option is that the audio selection doesn't find more than one file.. You might check that by inserting

echo ${AUDIO[@]}

immediately after the assignment, and review that output....

Offline

#5 2021-06-21 11:47:34

zapper
Member
Registered: 2017-05-29
Posts: 853  

Re: [SOLVED] how to randomly start audio file, via script,

Fascinating it is, your script does work with the modifications now, but... theres one small, catch...

This is what it looks like now:

#!/bin/bash
AUDIO=(
  $HOME/Zeal/Music*/*.{opus}
  $HOME/Zeal/Music*.opus
  $HOME/Zanra/Music*/*.{opus}
  $HOME/Zanra/Music*.opus
  $HOME/Kai/Music*/*.{opus}
  $HOME/Kai/Music*.opus
  $HOME/Drasu/Music*/*.{opus}
  $HOME/Drasu/Music*.opus
   /usr/share/sounds/alsa*.wav
)
LENGTH=${#AUDIO[@]}
PICK=$(( RANDOM % $LENGTH ))
play ${AUDIO[$PICK]}
echo ${AUDIO[@]}

And for some reason, it fails sometimes... to trigger, like 1/3 of the time it works for me. I wonder what I am doing wrong...

Gives me errors, despite my folders being there and such...

play WARN alsa: can't encode 0-bit Unknown or not applicable
play FAIL formats: can't open input file

This happens like 1/3 of the time. weird it is.


Freedom is never more than one generation away from extinction. Feelings are not facts
If you wish to be humbled, try to exalt yourself long term  If you wish to be exalted, try to humble yourself long term
Favourite operating systems: Hyperbola Devuan OpenBSD
Peace Be With us All!

Offline

#6 2021-06-21 12:26:26

dice
Member
Registered: 2020-11-22
Posts: 559  
Website

Re: [SOLVED] how to randomly start audio file, via script,

found this one liner that works ok with mpg123, picks  a random mp3 from Music dir.

find ~/Music -type f -name '*.mp3' | shuf -n 1 | xargs -d "\n" mpg123

you could use any music player,  not sure how to add other/more file formats though.

Cant figure out how to get this to open in own terminal to see the output.

Offline

#7 2021-06-21 12:55:42

ralph.ronnquist
Administrator
From: Battery Point, Tasmania, AUS
Registered: 2016-11-30
Posts: 1,117  

Re: [SOLVED] how to randomly start audio file, via script,

zapper wrote:

  $HOME/Zeal/Music*/*.{opus}

I think bash doesn't handle braces around singleton option well; it only works well when there are options. And it also has to find some * match for every option otherwise it results in the glob string itself. Though, perhaps inserting a prior

shopt -s nullglob

changes that.

Thus, in effect, all those selections ending {opus} end up as "bad pathnames" when picked for the later play command.

In other words, remove the braces for all {opus} and insert that shopt command, and then it hopefully will work consistently.

Offline

#8 2021-06-21 17:05:15

zapper
Member
Registered: 2017-05-29
Posts: 853  

Re: [SOLVED] how to randomly start audio file, via script,

ralph.ronnquist wrote:
zapper wrote:

  $HOME/Zeal/Music*/*.{opus}

I think bash doesn't handle braces around singleton option well; it only works well when there are options. And it also has to find some * match for every option otherwise it results in the glob string itself. Though, perhaps inserting a prior

shopt -s nullglob

changes that.

Thus, in effect, all those selections ending {opus} end up as "bad pathnames" when picked for the later play command.

In other words, remove the braces for all {opus} and insert that shopt command, and then it hopefully will work consistently.

Hmm.... I didn't really understand how to do that... that being said, Dice's command context was smaller and actually more usable.

But it does have the same issue oddly enough.

I assume its the same problem in the sense of how often it fails. I will test more though and say otherwise if works more.

EDIT: Not even close, Dice's script seems to work more than 90% of the time.

Thank you Dice, you seem to know your software well.

Also here is what I did:

#!/bin/bash
find ~/Folder/ -type f -name '*.opus' | shuf -n 1 | xargs -d "\n" play

Last edited by zapper (2021-06-21 17:20:10)


Freedom is never more than one generation away from extinction. Feelings are not facts
If you wish to be humbled, try to exalt yourself long term  If you wish to be exalted, try to humble yourself long term
Favourite operating systems: Hyperbola Devuan OpenBSD
Peace Be With us All!

Offline

Board footer