{

// Set these variables.

var subdivision = 5 ; // Must be a whole number.

var tempoDirection = "up" ; // Must be either "up" or "down". Doesn't do anything yet.

var bars = 60 ; // Must be a positive whole number.

var duration = 240 ; // Must be positive.

var choruses = 1 ; // Should be a whole number from 0 to 3.

var fade = 1 ; // Should be a whole number from 1 to 3.

var volume = 0.03 ; // Must be from 0 to 1.

var probability = 1 ; // Must be from 0 to 1. Applicable to percussion sounds only. Doesn't do anything yet.

// Make sure these values are correct.

var sampleRate = 96000 ;

var controlPeriod = 64 ;

var screenWidth = 1920 ;

var screenHeight = 1080 ;

var screenWidthOffset = 0 ;

var screenHeightOffset = - 1 ;

// Check the offsets by uncommenting the two ‘Poll.kr’ lines one-at-a-time and running the resulting block.

// The values should be from 1 to the number of pixels, with all the numbers being close to a whole number.

// Otherwise the synth may be out of tune by a pixel.

// The bottom left corner of the screen should be the one with values of roughly 0.

// If this is a different corner, enter a negative width/height.

//Poll.kr(Impulse.kr(100),MouseX.kr(screenWidthOffset + 1, screenWidthOffset + screenWidth + 1)) }.play

//Poll.kr(Impulse.kr(100),MouseY.kr(screenHeightOffset + 1, screenHeightOffset + screenHeight + 1)) }.play

// Don't mess with these ones.

var controlRate = sampleRate / controlPeriod ;

var mouseXProportion = MouseX . kr ( screenWidthOffset , screenWidthOffset + screenWidth ) / ( screenWidth - 1 ) ;

var mouseYProportion = MouseY . kr ( screenHeightOffset , screenHeightOffset + screenHeight ) / ( screenHeight - 1 ) ;

var controlSamples = duration * controlRate ;

var chorusProportion = Phasor . kr ( 0 , 1 / controlSamples , 0 - ( 0.1 / controlSamples ) , 1 - ( 0.1 / controlSamples ) ) ;

var time = duration * chorusProportion ;

var bar = bars * ( ( subdivision ** chorusProportion ) - 1 ) / ( subdivision - 1 ) ;

var subdivision0Count = bar * ( subdivision ** ( 0 + 1 ) ) ;

var subdivision1Count = bar * ( subdivision ** ( 1 + 1 ) ) ;

var subdivision2Count = bar * ( subdivision ** ( 2 + 1 ) ) ;

var subdivision3Count = bar * ( subdivision ** ( 3 + 1 ) ) ;

var subdivision4Count = bar * ( subdivision ** ( 4 + 1 ) ) ;

var subdivision5Count = bar * ( subdivision ** ( 5 + 1 ) ) ;

var barRounded = round ( bar , 1 ) ;

var subdivision0Rounded = round ( subdivision0Count , 1 ) ;

var subdivision1Rounded = round ( subdivision1Count , 1 ) ;

var subdivision2Rounded = round ( subdivision2Count , 1 ) ;

var subdivision3Rounded = round ( subdivision3Count , 1 ) ;

var subdivision4Rounded = round ( subdivision4Count , 1 ) ;

var subdivision5Rounded = round ( subdivision5Count , 1 ) ;

var subdivision0Tick = subdivision0Count - subdivision0Rounded ;

var subdivision1Tick = subdivision1Count - subdivision1Rounded ;

var subdivision2Tick = subdivision2Count - subdivision2Rounded ;

var subdivision3Tick = subdivision3Count - subdivision3Rounded ;

var subdivision4Tick = subdivision4Count - subdivision4Rounded ;

var subdivision5Tick = subdivision5Count - subdivision5Rounded ;

var barTrigger = bar - barRounded ;

var subdivision0Trigger = Select . kr ( ( ( - 0.25 / subdivision ) <= barTrigger ) * ( barTrigger < ( 0.75 / subdivision ) ) , [ subdivision0Tick , - 1 ] ) ;

var subdivision1Trigger = Select . kr ( ( ( - 0.25 / subdivision ) <= subdivision0Tick ) * ( subdivision0Tick < ( 0.75 / subdivision ) ) , [ subdivision1Tick , - 1 ] ) ;

var subdivision2Trigger = Select . kr ( ( ( - 0.25 / subdivision ) <= subdivision1Tick ) * ( subdivision1Tick < ( 0.75 / subdivision ) ) , [ subdivision2Tick , - 1 ] ) ;

var subdivision3Trigger = Select . kr ( ( ( - 0.25 / subdivision ) <= subdivision2Tick ) * ( subdivision2Tick < ( 0.75 / subdivision ) ) , [ subdivision3Tick , - 1 ] ) ;

var subdivision4Trigger = Select . kr ( ( ( - 0.25 / subdivision ) <= subdivision3Tick ) * ( subdivision3Tick < ( 0.75 / subdivision ) ) , [ subdivision4Tick , - 1 ] ) ;

var subdivision5Trigger = Select . kr ( ( ( - 0.25 / subdivision ) <= subdivision4Tick ) * ( subdivision4Tick < ( 0.75 / subdivision ) ) , [ subdivision5Tick , - 1 ] ) ;

var subdivision0Density = if ( 0 < choruses ,

1 ,

if ( 0 < ( choruses + fade ) ,

( ( ( subdivision ** ( ( time / duration ) + 0 - choruses - fade ) ) - 1 ) * ( subdivision ** fade ) )

/ ( 1 - ( subdivision ** fade ) ) ,

0 ) ) ;

var subdivision1Density = if ( 1 < choruses ,

1 ,

if ( 1 < ( choruses + fade ) ,

( ( ( subdivision ** ( ( time / duration ) + 1 - choruses - fade ) ) - 1 ) * ( subdivision ** fade ) )

/ ( 1 - ( subdivision ** fade ) ) ,

0 ) ) ;

var subdivision2Density = if ( 2 < choruses ,

1 ,

if ( 2 < ( choruses + fade ) ,

( ( ( subdivision ** ( ( time / duration ) + 2 - choruses - fade ) ) - 1 ) * ( subdivision ** fade ) )

/ ( 1 - ( subdivision ** fade ) ) ,

0 ) ) ;

var subdivision3Density = if ( 3 < choruses ,

1 ,

if ( 3 < ( choruses + fade ) ,

( ( ( subdivision ** ( ( time / duration ) + 3 - choruses - fade ) ) - 1 ) * ( subdivision ** fade ) )

/ ( 1 - ( subdivision ** fade ) ) ,

0 ) ) ;

var subdivision4Density = if ( 4 < choruses ,

1 ,

if ( 4 < ( choruses + fade ) ,

( ( ( subdivision ** ( ( time / duration ) + 4 - choruses - fade ) ) - 1 ) * ( subdivision ** fade ) )

/ ( 1 - ( subdivision ** fade ) ) ,

0 ) ) ;

var subdivision5Density = if ( 5 < choruses ,

1 ,

if ( 5 < ( choruses + fade ) ,

( ( ( subdivision ** ( ( time / duration ) + 5 - choruses - fade ) ) - 1 ) * ( subdivision ** fade ) )

/ ( 1 - ( subdivision ** fade ) ) ,

0 ) ) ;

var subdivision0PerNote = subdivision0Density / ( ( subdivision ) ** ( chorusProportion + 0 ) ) ;

var subdivision1PerNote = subdivision1Density / ( ( subdivision ) ** ( chorusProportion + 1 ) ) ;

var subdivision2PerNote = subdivision2Density / ( ( subdivision ) ** ( chorusProportion + 2 ) ) ;

var subdivision3PerNote = subdivision3Density / ( ( subdivision ) ** ( chorusProportion + 3 ) ) ;

var subdivision4PerNote = subdivision4Density / ( ( subdivision ) ** ( chorusProportion + 4 ) ) ;

var subdivision5PerNote = subdivision5Density / ( ( subdivision ) ** ( chorusProportion + 5 ) ) ;

var barEvent = EnvGen . kr ( Env ( [ - 0.0001 , 1 , 0 ] , [ 0 , 0 ] ) , barTrigger ) ;

var subdivision0Event = EnvGen . kr ( Env ( [ - 0.0001 , 1 , 0 ] , [ 0 , 0 ] ) , subdivision0Trigger ) * subdivision0PerNote ;

var subdivision1Event = EnvGen . kr ( Env ( [ - 0.0001 , 1 , 0 ] , [ 0 , 0 ] ) , subdivision1Trigger ) * subdivision1PerNote ;

var subdivision2Event = EnvGen . kr ( Env ( [ - 0.0001 , 1 , 0 ] , [ 0 , 0 ] ) , subdivision2Trigger ) * subdivision2PerNote ;

var subdivision3Event = EnvGen . kr ( Env ( [ - 0.0001 , 1 , 0 ] , [ 0 , 0 ] ) , subdivision3Trigger ) * subdivision3PerNote ;

var subdivision4Event = EnvGen . kr ( Env ( [ - 0.0001 , 1 , 0 ] , [ 0 , 0 ] ) , subdivision4Trigger ) * subdivision4PerNote ;

var subdivision5Event = EnvGen . kr ( Env ( [ - 0.0001 , 1 , 0 ] , [ 0 , 0 ] ) , subdivision5Trigger ) * subdivision5PerNote ;

var allEvents = barEvent + subdivision0Event + subdivision1Event + subdivision2Event + subdivision3Event + subdivision4Event + subdivision5Event ;

var allTriggers = Select . kr ( allEvents > 0 , [ - 0.5 , allEvents ] ) ;

var metronome = WhiteNoise . ar ( EnvGen . kr ( Env ( [ 0 , allTriggers , 0 ] , [ 0.01 , 0.04 ] , \sin ) , allTriggers ) * volume ) ;

var output = metronome ;

//Poll.kr(Impulse.kr(100), output)

[ output , output ]