Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353
Rep:
Is this feasible
Hello all,
I am considering an attempt to make some kind of bash script which can fetch a random entry from a playlist, invoke xine to play the entry from the play list and set a volume level accordingly.
To do this, I would need to make a list of entries which would have a parameter, separated by a comma at the begining of each row, which represents a playback volume. The list would need a row count at the beginning of the file too, perhaps.
That of course is the easy bit.
I have thought this through a little bit and I think I would need a script to perform the following tasks
- Script to read first row of list which will set a variable representing the row count.
- Script to choose a random number between 1 and the "row count" found in the list.
- Script to use the random number to find a row number in the list and fetch the data from that row
- Script then sets a variable representing the volume
- Script sets a variable representing the path to a file to be played
- Script invokes xine by way of xine -f -g -S --session 0,volume=<$VOL>,mrl=<$PATH>
Not being all that familiar with BASH scripts, I was wondering if this idea was a little too ambitious or not. Can this actually be achieved with a BASH script I guess is what I am wondering?
That's totally doable. You only need one script, not the 6 you list. Random numbers, or at least random enough for your purposes, are trivial.
Code:
$ let foo=$RANDOM
if you want a number between 0 and 99
Code:
$ let foo=$RANDOM%100
Look at the wc command for getting a line count of a file. cut, head and tail will probably all be useful. There are lots of useful little commands like those that can all be stuck together to achieve all kinds of things.
As arizonagroovejet mentioned earlier, there's no point have a row count, just use wc to count the number of lines.
Do you need a seperate volume level for each file? Won't one do? If the files have different volumes, you could normalize them.
If you do need seperate volumes, you could read the file into two arrays, one for filenames one for volumes, then pick one to play.
E.g.
Code:
#/bin/sh
volumes=()
files=()
# Assume whitespace seperator. Uncomment if fields are comma seperated.
# In that case, you might want to save and restore IFS.
#IFS=,
while read filename volume; do
files=("${files[@]}" "$filename")
volumes=(${volumes[*]} $volume)
done < "$1"
len=${#files[*]}
while true; do # use whatever condition you want
n=$(( RANDOM % len ))
xine -f -g -S --session 0,volume=${volumes[$n]},mrl=${files[$n]}
done
Of course, that will just loop indefinitely until you break it.
If you want a limited number of plays, how do want to do it? Just play N files or do you want to play them all? I.e. if you play files at random, it's obviously unlikely you'd play them all without repeating any. If you want to make sure the're all played, you'll have to keep track of what's been played. You could remove a file from the list once it's played, then when the list is empty, it's finished. E.g.
Code:
while [[ -n "$files" ]]; do
n=$(( RANDOM % len ))
xine -f -g -S --session 0,volume=${volumes[$n]},mrl="${files[$n]}"
unset files[$n]
unset volumes[$n]
done
Last edited by smoked kipper; 08-02-2008 at 12:23 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.