Skip to content

Instantly share code, notes, and snippets.

@amcgregor
Last active June 22, 2017 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amcgregor/0dc60c9ca3cff66bc87417c37337554d to your computer and use it in GitHub Desktop.
Save amcgregor/0dc60c9ca3cff66bc87417c37337554d to your computer and use it in GitHub Desktop.
An iTunes Announcer which speaks track information, pimps Patreon once an hour, features a silly Easter egg if you use a creepy little girl voice (I use Rosie Infovox iVox) and are up late, updates Ladiocast metadata, and announces to an IRC channel using Textual.
(* Radio caster announcement script.
Based on the "iTunes Announcer" originally written by Aaron Harnly (Nov. 2001) as updated by Doug Adams (Jan. 2012).
*)
property volume_fade_step : 1 -- Rate of change for volume adjustment during announcement.
property idle_time : 7 -- Delay in seconds for iTunes status polling.
property volume_during_announcement : 66 -- Music volume from 0 to 100 during announcement.
property channel : "#webcore" -- IRC channel to notify.
global previous_track
global previous_artist
global previous_album
global previous_hour
-- TODO: Dynamic phrase selection.
-- TODO: Time period greetings.
-- TODO: FYSA news alerts.
-- TODO: Mail hook to trap Patreon subscriptions to announce thanks like Twitch Alerts. Idea care of djdduty.
-- TODO: Handle empty album strings.
-- TODO: Match several pseudo-albums, like "Soundcloud", "Clyp", etc. to better identify source (when not from an album).
-- TODO: IRC announce any URL associated with the track, e.g. the Soundcloud origin link, iTunes Store link, etc.
on run
(* Script entry point.
Launch iTunes if not running and perform initial announcement.
*)
set previous_hour to (time of (current date)) / (60 * 60)
set previous_hour to round previous_hour rounding down
tell application "iTunes"
-- Set these to ensure they are triggered correctly on first check.
set previous_artist to ""
set previous_album to ""
set previous_track to missing value
try
set previous_track to the current track
end try
end tell
my announce()
end run
on idle
(* Periodic event to check if triggering is required. *)
if checkItunesIsActive() then
-- Only check with iTunes if it is currently running
tell application "iTunes"
try
if (previous_track is not equal to the current track) then
my announce()
end if
end try
end tell
end if
return idle_time
end idle
on announce()
(* The meat of generating announcement text and ducking iTunes audio. *)
set thisHour to (time of (current date)) / (60 * 60)
set thisHour to round thisHour rounding down
tell application "iTunes"
if (the player state is not playing) then
return -- If iTunes is not playing, we have nothing to announce.
end if
set current_track to the current track
set track_name to the name of the current_track
set track_artist to the artist of the current_track
set track_album to the album of the current_track
(* Currently unused functionality.
set current_playlist to the playlist of the current_track
if (the current_playlist is the library playlist) then
set playlist_name to "the Main song library"
else if (the current_playlist is audio CD playlist) then
set playlist_name to "the CD " & playlist_name
else if (the current_playlist is radio tuner playlist) then
set playlist_name to "the internet radio station " & playlist_name
else
set playlist_name to "the playlist " & playlist_name
end if
*)
set ladio_announcement to track_artist & " - " & track_name
set irc_announcement to "/notice " & channel & " Now playing: " & ladio_announcement
set say_announcement to "You're listening to " & track_name & ","
-- Only announce the artist if the artist has changed.
if (track_artist is not equal to previous_artist) then
set say_announcement to say_announcement & " by " & track_artist & ","
end if
-- Only announce the album if the album has changed.
if (track_album is not equal to previous_album) then
set say_announcement to say_announcement & " from the album " & track_album & ","
end if
tell application "LadioCast" to set metadata song to ladio_announcement
if (thisHour > previous_hour) then
-- Once an hour, give the longer spiel.
set say_announcement to say_announcement & " on Goth Alice's developer satisfaction stream, the Rabbit Hole. If you would like to support this stream you can donate to Goth Alice's paytreeon by visiting paytreeon dot com forward slash Goth Alice. Thank you, and enjoy!"
if (thisHour is equal to 4) then
set say_announcement to "You're all going to die down here. Just kidding! Pinky swear, I'm not the Red Queen, but if you're hearing this, you're deep into the oh my god what am I doing up this late 'ning. Best of luck on your coding adventure! " & say_announcement -- TODO: Dynamic phrases.
end if
else
-- English it.
set say_announcement to say_announcement & "."
end if
-- Duck (fade down) the currently playing track in iTunes.
set the old_volume to my fade_volume(volume_during_announcement)
tell application "Textual" to activate
tell application "System Events"
delay 0.3
set value of text area 1 of scroll area 1 of window 1 of process "Textual" to irc_announcement
delay 0.3
keystroke return
keystroke return
keystroke return
end tell
-- Speak the announcement. Bonus points for creepy little girl voice.
say "[[volm 0.66]]" & say_announcement
-- Restore the previous volume level.
set the old_volume to my fade_volume(old_volume)
-- Update tracking variables.
set previous_track to current_track
set previous_artist to the track_artist
set previous_album to the track_album
end tell
set previous_hour to thisHour
end announce
on fade_volume(final_volume)
tell application "iTunes"
set old_volume to the sound volume -- remember previous volume
set theVol to old_volume
if (final_volume < theVol) then
-- we need to step down the volume to the destination
repeat while (final_volume < theVol)
set sound volume to theVol
set theVol to theVol - volume_fade_step
delay 0.05
end repeat
else
-- we need to step up the volume to the destination
repeat while (final_volume > theVol)
set sound volume to theVol
set theVol to theVol + volume_fade_step
delay 0.05
end repeat
end if
set sound volume to final_volume -- make it exactly right
end tell
return old_volume -- send back the previous volume
end fade_volume
to checkItunesIsActive()
tell application "System Events" to return (exists (some process whose name is "iTunes"))
end checkItunesIsActive
to checkLadioIsActive()
tell application "System Events" to return (exists (some process whose name is "LadioCast"))
end checkLadioIsActive
to checkTextualIsActive()
tell application "System Events" to return (exists (some process whose name is "Textual"))
end checkTextualIsActive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment