Code/new AUTOMAGIC iTunes Crossfader: Difference between revisions

From kJams Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
<nowiki>-- Please have iTunes and kJams running before launching this script and
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:
 
 
<pre>-- Please have iTunes and kJams running before launching this script and  
-- have a playlist selected in iTunes  
-- have a playlist selected in iTunes  
-- The simple logic of the script assumes that if iTunes is playing and this script is run,  
-- The simple logic of the script assumes that if iTunes is playing and this script is run,  
Line 14: Line 22:
--sets how long in seconds between each step in the fade  
--sets how long in seconds between each step in the fade  
set fadeDelay to 0.1
set fadeDelay to 0.1
--established kJams command codes
set kScriptCommand_PLAY_PAUSE to 1
set kScriptCommand_PLAY_PAUSE to 1
set kScriptCommand_STOP to 2
set kScriptCommand_STOP to 2
--set all volumes to cross fade starting values
--Get current audio volume from kJams
--Get current audio volume from kJams
tell application "kJams Pro" to set kJamsMaxVolume to get volume kSpeakers
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
-- Number of steps in fade
set fadeSteps to 20
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
--sets how much in percentage the volume is decreased per step
set kJFadeStep to kJamsMaxVolume / fadeSteps
-- set kJFadeStep to kJamsMaxVolume / fadeSteps
set iTFadeStep to iTunesMaxVolume / fadeSteps
--set iTFadeStep to iTunesMaxVolume / fadeSteps
--this is the minimum volume setting  
--this is the minimum volume setting  
set MinVolume to 0
set MinVolume to 0
Line 33: Line 44:
set prevPlayState to 0
set prevPlayState to 0


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

Latest revision as of 17:12, 29 September 2010

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