Code/new AUTOMAGIC iTunes Crossfader

From kJams Wiki
Jump to navigation Jump to search

After having to poke through the code again to check it out, I was inspired to add a change that has been bugging me for a while. Namely, if you are currently playing in kJams, with the script running, iTune volume should be 0, and stopping kJams should cross fade to the previous iTunes volume. But what if you want to change that volume for whatever reason? Previously, if you changed the iTunes volume while kJams was playing, the script had no way of knowing, and would just cross fade to the old value. While this is the desired outcome 99.9% of the time, I suspect, that last .1% always bugged me. Previously you just had to wait until the crossfade was done, and then change the final volume manually. Now, introducing the improved autoCrossfade script! With the new script, if you adjust volume from 0 in iTune while kJams is playing, the script senses that, and next time you pause kJams, you will automatically crossfade to the NEW value. And vice versa, with one caveat. Since you changed the value from 0 in kJams, and initiate play in kJams, when you crossfade, the script has no way of stopping kJams from immediately playing, so the first bit comes out not at 0, as it should, but at the new volume you have set. The script immediately senses this and readjusts the volume to 0 to start the cross fade, but you still get an initial burst of play in kJams at the new volume level you have set. As this is an very low percentage case of a very low percentage case, the likelihood is that no one will ever run into this issue. Besides which, there is nothing that can be done about it anyway!

Here is the code. You can open up to old version, and just paste this in:


-- 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

--established kJams command codes
set kScriptCommand_PLAY_PAUSE to 1
set kScriptCommand_STOP to 2

--set all volumes to cross fade starting values 
--Get current audio volume from kJams
tell application "kJams Pro" to set kJamsMaxVolume to get volume kSpeakers
--get iTune volume maximum.
tell application "iTunes" to 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

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

--sets kJams state variable codes
set kPlayModeType_STOPPED to 0
set kPlayModeType_PLAYING to 1
set kPlayModeType_PAUSED to 2

repeat
	--is kJams playing?
	tell application "kJams Pro"
		set playMode to get mode
	end tell
	tell application "iTunes"
		-- Check to see if kJams is playing
		if (playMode = kPlayModeType_PLAYING) then
			-- if so, set current playstate to playing
			set playState to 0
			-- If prevPlayState is not the same as the current state
			--i.e. if kJams is has just started playing, fade in kJams and fade out iTunes
			if (prevPlayState = 1) then
				tell application "kJams Pro" to set currentVolume to get volume kSpeakers
				if currentVolume ≠ MinVolume then set kJamsMaxVolume to currentVolume
				--sets how much in percentage the volume is decreased per step
				set kJFadeStep to kJamsMaxVolume / fadeSteps
				set kJamsVolume to MinVolume
				-- get current starting volume from iTunes
				set iTunesVolume to the sound volume
				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
				-- if you want iTunes to pick up where it left off, remove the following line
				next track
				pause
			end if
		else
			-- if kJams is not playing, set current playstate to "not playing"
			set playState to 1
			--if kJams has just now been paused, cross fade in iTunes
			if (prevPlayState = 0) then
				set currentVolume to the sound volume
				if currentVolume ≠ MinVolume then set iTunesMaxVolume to currentVolume
				set iTFadeStep to iTunesMaxVolume / fadeSteps
				--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
			--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