Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@glutanimate
Last active October 20, 2020 13:02
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save glutanimate/413c7e0a61b826d687a1 to your computer and use it in GitHub Desktop.
Save glutanimate/413c7e0a61b826d687a1 to your computer and use it in GitHub Desktop.
MoodyRain
#!/bin/bash
# MoodyRain by Glutanimate
# License: GNU GPLv3
# Dependencies: yad, vlc
#
# All streams linked to in this script are the property of their respective owners.
#
# Using a different audio player:
#
# Replace the cvlc command-line with one of the following
# - mpv --loop=inf --vo null --volume="$Volume" --gapless-audio=yes "$i" #> /dev/null 2>&1 &
# - avplay -loop 0 -nodisp -autoexit "$i" > /dev/null 2>&1 &
#
# Using local files:
#
# place files with the same name as the streams in 'ambient_sounds' directory next to script
# (e.g. ./ambient_sounds/RainyMood.ogg)
# Variables
Executable="$(readlink -f "$0")"
ProgDir="${Executable%/*}"
SoundDir="$ProgDir/ambient_sounds"
StreamUrl=("http://174.36.223.28/audio1110/RainyMood.ogg" \
"http://st2.asoftmurmur.com/beta/main-thunder.mp4" \
"http://st3.asoftmurmur.com/beta/main-sbowl.mp4" \
"http://st1.asoftmurmur.com/beta/main-birds.mp4" \
"http://st3.asoftmurmur.com/beta/main-wind.mp4" \
"http://st1.asoftmurmur.com/beta/main-whitenoise.mp4" \
"http://st3.asoftmurmur.com/beta/main-waves.mp4" \
"http://st1.asoftmurmur.com/beta/main-rain.mp4" \
"http://st2.asoftmurmur.com/beta/main-people.mp4" \
"http://st3.asoftmurmur.com/beta/main-fire.mp4" \
"http://st2.asoftmurmur.com/beta/main-crickets.mp4" \
"http://augustambience.com/night.mp3" \
"http://www.soundrown.com/Audio/Final%20Normalized/mp33/FireFinal15.mp3" \
"http://coffitivity.com/sounds/audio/mp3/morningMurmur_mp3.mp3" \
"http://coffitivity.com/sounds/audio/mp3/lunchtimeLounge_mp3.mp3" \
"http://coffitivity.com/sounds/audio/mp3/universityUndertones_mp3.mp3")
StreamName=("rainymood.com - Rainy Mood" \
"A Soft Murmur - Thunder" \
"A Soft Murmur - Singing Bowl" \
"A Soft Murmur - Birds" \
"A Soft Murmur - Wind (short)" \
"A Soft Murmur - White Noise (short)" \
"A Soft Murmur - Waves (short)" \
"A Soft Murmur - Rain (short)" \
"A Soft Murmur - People (short)" \
"A Soft Murmur - Fire (short)" \
"A Soft Murmur - Crickets (short)" \
"August ambience - August Night" \
"Soundrown - Fire" \
"Coffitivity - Morning Murmur" \
"Coffitivity - Lunchtime Lounge"\
"Coffitivity - University Undertones")
Volume="100"
# Gui
YadTitle="MoodyRain"
TrayIcon="rhythmbox-panel"
GuiTxtDescr="Please select one or multiple ambient sounds. Internet connection required!"
GuiTxtPlay="Play selected streams"
GuiTxtCancel="Cancel and quit"
WmIcon="gnome-audio"
ThumbIcon="gnome-audio"
# Functions
gui_streamsel () {
YadChoices="$(\
for i in "${!StreamUrl[@]}"; do
echo "FALSE"
echo "${StreamName[$i]}"
echo "${StreamUrl[$i]}"
done | \
yad --list --checklist \
--title="$YadTitle" \
--class="$WmClass" \
--text="$GuiTxtDescr" \
--window-icon="$WmIcon" \
--image="$ThumbIcon" \
--print-all --multiple \
--width=800 --height=400 --center \
--button="_Play!gtk-media-play-ltr!$GuiTxtPlay":2 \
--button="_Cancel!gtk-close!$GuiTxtCancel":1 \
--column="Choose" \
--column="Stream":TEXT --column="Stream URL":TEXT \
)"
YadChoicesRet="$?"
Playlist=($(echo "$YadChoices" | grep "TRUE" | cut -d '|' -f 3))
Sounds=($(echo "$YadChoices" | grep "TRUE" | cut -d '|' -f 2 | awk -F '\n' '{print $1", "}'))
if [[ "$YadChoicesRet" != "2" || -z "$Playlist" ]]; then
echo "Aborted."
exit 1
fi
}
gui_systray () {
yad --notification \
--text="MoodyRain" \
--image="$TrayIcon" \
--command="notify-send -i $ThumbIcon 'MoodyRain' 'Ambient sounds: ${Sounds[*]}'" \
--menu="Stream selection!bash -c gui_systray_onclick!$ThumbIcon|Exit!kill -s TERM $TopPid!exit"&
YadPid="$!"
}
gui_systray_onclick(){
kill -s TERM "$TopPid"
"$TopScript" > /dev/null 2>&1 &
}
player(){
for i in "${Playlist[@]}"; do
# prefer local data
if [[ -f "$SoundDir/${i##*/}" ]]; then
PlaylistItem="$SoundDir/${i##*/}"
echo "Playing local file $PlaylistItem"
else
PlaylistItem="$i"
echo "Playing remote stream $PlaylistItem"
fi
cvlc --repeat --volume=$((256*Volume/100)) --play-and-exit "$PlaylistItem" > /dev/null 2>&1 &
PlayerPids+=("$!")
done
wait "${PlayerPids[0]}"
}
cleanup(){
for i in "${PlayerPids[@]}"; do
kill -s TERM "$i"
done
kill -s TERM "$YadPid"
kill -s TERM "$TopPid"
}
# Preamble
export -f gui_systray_onclick
export TopScript="$0"
export TopPid=$$
PlayerPids=()
trap "cleanup; exit" EXIT
# Main
gui_streamsel
gui_systray
player
@orschiro
Copy link

I understand the point of potential copyright violations. I assume that also means that I cannot create an Arch Linux package for it, correct?

Two small things:

  • I suggest to change TrayIcon="media-player-banshee-panel" to TrayIcon="gnome-audio as well. Why having another dependency for Banshee just for the icon?
  • The icon shown is too large. I had that issue with some other icons in the past as well. I will have to see why this is happening (using stalonetray). Screenshot

@glutanimate
Copy link
Author

@orschiro

I understand the point of potential copyright violations. I assume that also means that I cannot create an Arch Linux package for it, correct?

Yes. In order to get this script in a repo we would have to replace all the copyrighted streams with our own.

If you look through the credits of Noisli, A Soft Murmur, etc. you will find that almost all of their sound files are based on CC-BY licensed samples from freesound.org:

Now, of course we could just download some of these, convert them to .ogg/.mp3, and put them up in a GitHub repo together with the script. However, this wouldn't be a good idea for a couple of reasons:

  1. Differences in audio quality and volume mean that a lot of these tracks won't sound good together
  2. Most of these samples are very short and end abruptly. If we put them on repeat it would become obvious that we are using the same sample over and over again.

That's why we would first have to edit these tracks, maybe splice some of them together and add fade-ins/-outs before using them.

To be frank, I don't have any idea how to do this and I simply don't have the time to learn it. You don't have any experience with audio mixing/editing by any chance, do you? I would be happy to continue with the coding side of things if you took care of the samples.


I suggest to change TrayIcon="media-player-banshee-panel" to TrayIcon="gnome-audio as well. Why having another dependency for Banshee just for the icon?

I went with media-player-banshee-panel initially because Faenza and other icon themes provide a monochrome panel icon for it. I would gladly use gnome-audio instead, but it just looks out of place among all the other monochrome icons. For now I've settled with rhythmbox-panel because it ships with Ubuntu by default.

The icon shown is too large. I had that issue with some other icons in the past as well. I will have to see why this is happening (using stalonetray).

No idea what could be causing that. What panel are using? Everything looks fine for me on XFCE and Unity:


BTW, I've uploaded a new revision of the script. Changes (aside from the systray icon and documentation):

  • moodyrain will now look for local sound files before playing a remote stream
    • just place your files in an ambient_sounds directory next to the script and name them after the streams (e.g. ./ambient_sounds/RainyMood.ogg)

@Muges
Copy link

Muges commented Dec 21, 2014

Hi,

I am working on a similar project, and I figured you may be interested in the sound files I use (in the sounds folder). They loop seamlessly, the only problem you may have is that the volumes are different from one track to another (especially the Stream track which is very loud). This is not a problem for me, since I let the user adjust the volume of each track, you could do that, or just set a volume for each track in the code.

For now there are only 5 tracks, I will probably add some in the next few days.

Just FYI, creating seamless loops is really easy with natural sounds. I use audacity, it took me maybe half an hour to do it the first time, and now that I figured it out it only takes a few minutes. These two tutorials (1, 2) were helpful.

@glutanimate, thanks for the list of sound files, it helped me a lot.

@glutanimate
Copy link
Author

@Muges:

Very interesting project! I love how you can adjust the volume for each track. The tracks loop perfectly, too, so fantastic job on that!

Would you be fine with me using your sounds for moodyrain?

I will definitely check out the audacity tutorials you linked to. I don't have much time on my hands right now, but when I eventually do get down to compiling ambient loops of my own I will make sure to let you know.

Glad I could help with the samples!

@Muges
Copy link

Muges commented Dec 23, 2014

Would you be fine with me using your sounds for moodyrain?

Off course! Just put links to the original files somewhere.

@glutanimate
Copy link
Author

@Muges:

Awesome, thanks a lot! I just released an overhauled version of MoodyRain with your samples included. I would be glad if you and @orschiro gave it a try.

One small thing about the sound files:

I noticed that they all have even-numbered durations (in seconds). I think it could be beneficial to use tracks with more differing durations.

See, if we only use even-numbered tracks with similar lengths we have far more overlapping seams...

track 1 |-----|-----|-----|-----| period: 120 secs
track 2 |--|--|--|--|--|--|--|--| period: 60 secs
track 3 |-----------|-----------| period: 240 secs

...than if we use sound files with more variance in their periods:

track 1 |----|----|----|----|----| period: 89 secs
track 2 |---|---|---|---|---|---|- period: 72 secs
track 3 |------|------|------|---- period: 131 secs

This could reduce repetitiveness and create a more natural-sounding soundscape.

Just a thought, though. I haven't had the chance to play around with audacity, yet. I might very well be talking nonsense here.

@Muges
Copy link

Muges commented Dec 23, 2014

Awesome, thanks a lot! I just released an overhauled version of MoodyRain with your samples included. I would be glad if you and @orschiro gave it a try.

Just one problem: file -Lib "$File" (on line 36) returns audio/ogg (instead of application/ogg) on my system (archlinux). You should probably check if it's equal to */ogg *, or find a better way. Appart from that it works fine.

I noticed that they all have even-numbered durations (in seconds). I think it could be beneficial to use tracks with more differing durations.

At first I thought too that it would be important to reduce repetitiveness, but for A soft murmur for examples, all the audio files are two minutes long if I remember correctly, and you can listen to them for hours without realising that it is always the same two-minutes segment.

I've been listening to the tracks I edited a lot, and at least once for a whole hour without pause, and I don't hear any repetition, so it should be good.

Although you are probably right, I should be careful when creating tracks that do not always sound the same (such as the Thunderstorm), if all of them have the same length, some pattern may appear.

@glutanimate
Copy link
Author

Just one problem: file -Lib "$File" (on line 36) returns audio/ogg (instead of application/ogg) on my system (archlinux).

Huh, I must say I am quite surprised that file behaves differently on Arch. Thanks for the pointer! It should be fixed now

I've been listening to the tracks I edited a lot, and at least once for a whole hour without pause, and I don't hear any repetition, so it should be good.

Although you are probably right, I should be careful when creating tracks that do not always sound the same (such as the Thunderstorm), if all of them have the same length, some pattern may appear.

Yeah, you did a fantastic job with the edits. So far I've only been able to discern a repetitive pattern in the thunderstorm sample.

@Muges
Copy link

Muges commented Dec 25, 2014

Yeah, you did a fantastic job with the edits. So far I've only been able to discern a repetitive pattern in the thunderstorm sample.

I'm not really happy with that one, I'll try to find a long and nice rolling thunder sound when I have the time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment