Code/new iTunes Crossfader

From kJams Wiki
Jump to navigation Jump to search

Here's the older version, you may want to read that for some context

Mavericks caution: save this script in Automator or ASE as an application. That way you can "Get Info" on it, and then click "Prevent App Nap".

Here is my new version of the cross fader script. The old version of the original crossfader forced you to use identical max volumes for both iTunes and kJams, since if you didn't, its logic would have one faded to zero while the other one was still in the process of fading up, and vice versa. Now you can have any values you want. Also, to change the volume settings you had to use an editor to edit the script code. This new version polls both applications and uses their current volume settings for the cross fade. It then resets them after the fade. No need for both volumes to be identical, since the script now fades each at different speeds, such that both reach their endpoints at the same time, regardless of initial settings! (P.S. send me a P.M. if you download it and let me know if you find it helpful.)

Hope someone finds it useful. Enjoy!

Here is a "step by step" for getting it installed.

-- 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!! 
tell application "iTunes"
	
	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
	--set iTune volume maximum.
	set iTunesMaxVolume to the sound volume
	-- Number of steps in fade
	set fadeSteps to 20
	--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
	
	if (player state is playing) then
		--set all volumes to cross fade starting values 
		set kJamsVolume to MinVolume
		set iTunesVolume to iTunesMaxVolume
		tell application "kJams Pro"
			set volume kSpeakers level MinVolume
			docommand kScriptCommand_PLAY_PAUSE
		end tell
		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
		
		pause
		set the sound volume to iTunesMaxVolume
		
	else
		--set all volumes to cross fade starting values 
		set iTunesVolume to MinVolume
		set kJamsVolume to kJamsMaxVolume
		tell application "kJams Pro" to set volume kSpeakers level kJamsMaxVolume
		set the sound volume to MinVolume
		play
		repeat until (kJamsVolume ≤ 0 and iTunesVolume ≥ iTunesMaxVolume)
			set iTunesVolume to (iTunesVolume + iTFadeStep)
			set kJamsVolume to (kJamsVolume - kJFadeStep)
			tell application "iTunes" to set the sound volume to iTunesVolume
			tell application "kJams Pro" to set volume kSpeakers level kJamsVolume
			delay fadeDelay
		end repeat
		tell application "kJams Pro"
			docommand kScriptCommand_STOP
			set volume kSpeakers level kJamsMaxVolume
		end tell
	end if
end tell


Special thanks and Kudos go out to oss, whose original work served as the foundation and backbone of this effort!