Chapter 5, Listing 2

Listing 2: Fractal Music

Background-Script

on openStack
	start using "Business Graph"
end openStack

on closeStack
	stop using "Business Graph"
end closeStack

Script of cd Button "Play Music"

on mouseUp
	global instrument, maxIter
	put 31 into maxIter
	if the hilite of me is TRUE then
		set the hilite of me to FALSE
		exit mouseUp
	else
		set the hilite of me to TRUE
		initGraph
		scale 0, -3, maxIter, 12
		drawSideAxes
		if the hilite of cd button "Flute" is TRUE then
			put "Flute" into instrument
		else if the hilite of cd button "Saxophone"¬
		is TRUE then
			put "Saxophone" into instrument
		else if the hilite of cd button "Choir"¬
		is TRUE then
			put "Choir" into instrument
		else
			put "Harpsichord" into instrument
		end if
		get the selectedText of cd button "Color of Music:"
		if it is white then
			whiteMusic
		else if it is pink then
			pinkMusic
		else
			brownMusic
		end if
		closeGraph
		set the hilite of me to FALSE
	end if
end mouseUp

on whiteMusic
	global maxIter
	repeat with i = 0 to maxIter
		if i = 0 then
			put 0 into tune
			put tune into oldTune
		else
			put random(16) - 4 into tune
		end if
		put random(4) into beat
		drawMotion oldTune, tune, i, beat
		playMusic tune, beat
		put tune into oldTune
	end repeat
end whiteMusic

on pinkMusic
	global maxIter
	repeat with i = 0 to maxIter
		if i = 0 then
			put 0 into tune
			put tune into oldTune
			put random(6) into blue
			put random(6) into green
			put random(6) into red
		else
			put i MOD 7 into j
			if j = 0 then
				put random(6) into blue
				put random(6) into green
				put random(6) into red
				put blue + green + red - 6 into tune
			else if j = 1 then
				put random(6) into red
				put blue + green + red - 6 into tune
			else if j = 2 then
				put random(6) into green
				put random(6) into red
				put blue + red + green - 6 into tune
			else if j = 3 then
				put random(6) into red
				put blue + red + green - 6 into tune
			else if j = 4 then
				put random(6) into blue
				put random(6) into green
				put random(6) into red
				put blue + red + green - 6 into tune
			else if j = 5 then
				put random(6) into red
				put blue + red + green - 6 into tune
			else if j = 6 then
				put random(6) into green
				put random(6) into red
				put blue + red + green - 6 into tune
			else
				put random(6) into red
				put blue + red + green - 6 into tune
			end if
		end if
		put random(4) into beat
		drawMotion oldTune, tune, i, beat
		playMusic tune, beat
		put tune into oldTune
	end repeat
end pinkMusic

on brownMusic
	global maxIter
	repeat with i = 0 to maxIter
		if i = 0 then
			put 0 into tune
			put tune into oldTune
		else
			put random(7) - 4 into dist
			add dist to tune
			if (tune > 12) OR (tune < -3) then
				subtract 2*dist from tune
				-- subtract dist from tune
				-- put 4 into tune
			end if
		end if
		put random(4) into beat
		drawMotion oldTune, tune, i, beat
		playMusic tune, beat
		put tune into oldTune
	end repeat
end brownMusic

on playMusic tune, beat
	global instrument
	if the hilite of cd button "Silent" is TRUE then
		exit playMusic
	end if
	put scaleMusic(tune, beat) into tuneString
	play instrument tempo 180 tuneString
end playMusic

on drawMotion oldTune, tune, i, beat
	put convertX(i-1) into oldX
	put convertX(i) into x
	put convertY(oldTune) into oldY
	put convertY(tune) into y
	if i = 0 then
		plotPoint x, y, beat
	else
		connectPoints oldX, oldY, x, y
		plotPoint x, y, beat
	end if
end drawMotion

function scaleMusic tune, beat
	if tune = -3 then
		put "g3" into tuneString
	else if tune = -2 then
		put "a3" into tuneString
	else if tune = -1 then
		put "b3" into tuneString
	else if tune = 0 then
		put "c4" into tuneString
	else if tune = 1 then
		put "d4" into tuneString
	else if tune = 2 then
		put "e4" into tuneString
	else if tune = 3 then
		put "f4" into tuneString
	else if tune = 4 then
		put "g4" into tuneString
	else if tune = 5 then
		put "a4" into tuneString
	else if tune = 6 then
		put "b4" into tuneString
	else if tune = 7 then
		put "c5" into tuneString
	else if tune = 8 then
		put "d5" into tuneString
	else if tune = 9 then
		put "e5" into tuneString
	else if tune = 10 then
		put "f5" into tuneString
	else if tune = 11 then
		put "g5" into tuneString
	else
		put "a5" into tuneString
	end if   -- tune
	if beat = 1 then
		put "w" after tuneString
	else if beat = 2 then
		put "h" after tuneString
	else if beat = 3 then
		put "q" after tuneString
	else
		put "e" after tuneString
	end if    -- beat
	RETURN tuneString
end scaleMusic

on plotPoint x, y, beat
	put 6 - beat into r
	put 2*r into r
	choose oval tool
	drag from x-r, y-r to x+r,  y+r
end plotPoint

on connectPoints x1,y1, x2,y2
	choose line tool
	drag from x1, y1 to x2, y2
end connectPoints

© 1997 by Jörg Kantel
Zurück zum Inhaltsverzeichnis