Code/new AUTOMAGIC iTunes Crossfader

From kJams Wiki
Revision as of 18:44, 21 July 2010 by Deusexmachina (talk | contribs)
Jump to navigation Jump to search

Here is the autoCrossFade script. Just past it into an empty AppleScript document, save and go:


-- Please have iTunes and kJams running before launching this script and
-- have a playlist selected in iTunes 
-- The simple logic of the script assumes that if iTunes is playing and this script is run, 
--  you want to cross fade over to kJams.. Alternately, if iTunes is not playing a track, 
--  it assumes you want to cross fade over to iTunes. 
-- when you are ready to cross fade to kJams, make sure your next song is 
-- highlighted/selected.  It will NOT automatically go to the next track in 
-- your kJams playlist.
-- Unlike the original version of this script, you do not need to set your max volume levels. Instead, it will poll iTunes and kJams for their current volumes, and then cross fade using those values. Even if these volumes are very different, it will fade them such that they reach their appropriate values at the same time. No more need for these values to be identical!
-- Although I have tested this code, I make no guarantees with this code, 
-- any disasters that occur are not my fault.  Use at your own risk!! 

set kSpeakers to 0
--sets how long in seconds between each step in the fade 
set fadeDelay to 0.1
set kScriptCommand_PLAY_PAUSE to 1
set kScriptCommand_STOP to 2
--Get current audio volume from kJams
tell application "kJams Pro" to set kJamsMaxVolume to get volume kSpeakers
-- Number of steps in fade
set fadeSteps to 20
--set all volumes to cross fade starting values 
--set iTune volume maximum.
tell application "iTunes" to set iTunesMaxVolume to the sound volume
--sets how much in percentage the volume is decreased per step
set kJFadeStep to kJamsMaxVolume / fadeSteps
set iTFadeStep to iTunesMaxVolume / fadeSteps
--this is the minimum volume setting 
set MinVolume to 0

-- Playstate will be either 1 for iTunes or 0 for kJams
set playState to 0
set prevPlayState to 0

set kPlayModeType_STOPPED to 0
set kPlayModeType_PLAYING to 1
set kPlayModeType_PAUSED to 2
repeat
   tell application "kJams Pro"
      set playMode to get mode
   end tell
   tell application "iTunes"
      if (playMode = kPlayModeType_PLAYING) then
         set playState to 0
         -- If prevPlayState is not the same as the current state
         if (prevPlayState = 1) then
            set kJamsVolume to MinVolume
            tell application "kJams Pro" to set volume kSpeakers level MinVolume
            repeat until (iTunesVolume ≤ 0 and kJamsVolume ≥ kJamsMaxVolume)
               set iTunesVolume to (iTunesVolume - iTFadeStep)
               set kJamsVolume to (kJamsVolume + kJFadeStep)
               set the sound volume to iTunesVolume
               tell application "kJams Pro" to set volume kSpeakers level kJamsVolume
               delay fadeDelay
            end repeat
            next track
            pause
         end if
         -- I repeat these lines in case you decide to change the volume levels while you are working. Otherwise, they will always reset to the original values.
         --Get current audio volume from kJams
         --tell application "kJams Pro" to set kJamsMaxVolume to get volume kSpeakers
         --set kJFadeStep to kJamsMaxVolume / fadeSteps
      else
         set playState to 1
         if (prevPlayState = 0) then
            -- I repeat these lines in case you decide to change the volume levels while you are working. Otherwise, they will always reset to the original values.
            --Get current audio volume from kJams
            tell application "kJams Pro" to set kJamsMaxVolume to get volume kSpeakers
            set kJFadeStep to kJamsMaxVolume / fadeSteps
            tell application "kJams Pro" to set volume kSpeakers level MinVolume
            
            --set all volumes to cross fade starting values 
            set iTunesVolume to MinVolume
            set the sound volume to MinVolume
            play
            repeat until iTunesVolume ≥ iTunesMaxVolume
               set iTunesVolume to (iTunesVolume + iTFadeStep)
               set the sound volume to iTunesVolume
               --tell application "kJams Pro" to set volume kSpeakers level MinVolume
               delay fadeDelay
            end repeat
         end if
         -- I repeat these lines in case you decide to change the volume levels while you are working. Otherwise, they will always reset to the original values.
         --Get current audio volume from iTunes
         set iTunesMaxVolume to the sound volume
         set iTFadeStep to iTunesMaxVolume / fadeSteps
         
      end if
      set prevPlayState to playState
      delay 0.5
   end tell
end repeat