# I wrote this for my use, so its only -sorta- practical.
# 
# It's for manually assigning a machine by it's 'real name' 
# automatically upon loading the script, but the automatic
# part i wrote is kinda shitty, so w/o some mods you would
# only want one instance of the machine you want to control
# added to your song.. if there's more than one it will
# always target the newest one added.
#
# the reason i did this is because pybuzz's assignment menu 
# doesn't appear when you click on 'Assign PeerController' 
# on win98, which i have on my (old) Laptop... where i
# do most of my coffee drinking and coding. 
#
# However, there are several things in this .py that anyone
# should want to know. First off, its a good example of how
# SetPeerCtrlTarget() works. Setting assignments manually
# definitly has other practical uses. 
#
# Since I manually edit this script while im testing ideas
# you can change if m == "cheapo amp" to any real name of a
# machine. to find the real name of a machine add it to 
# buzz and look at the first list of names it prints out in
# the console window when you load or reload (update import)
# the script.
# 
# This script when first imported or 'updated' scans for
# all the machines currently loaded in the machines window.
# Once it has that list it goes through them and looks
# for the realname of the machine you're looking for
# as specified by the machine's programmer.
# You could change z[10] to z[11] and instead look for the
# shortname of the machine. check the pybuzz reference txt
# on the index information for these lists (aka arrays)
#
# The last thing i through in here is this part:
#
#		max = p[pindex][4]
#		---
#		doh = max / 128
#		value = value * doh
#		
#		SendPeerCtrlChange(0,1,value);
#
# This makes the resolution and 'smoothness' of the Byte sliders
# on the PyBuzz param window mimic the machine you are controlling.
# I was getting annoyed by going to 800% on params that should
# only goto 100%. W/ the word sliders you should reverse the formula
# with something like this:
#
#		doh = 32768 / max
#		value = value * doh
#		
# 
# Also as a side note, Ive noticed that when you manually assign
# a parameter to pybuzz using SetPeerCtrlTarget on a global param
# (not a track param) the controlled machine's slider moves as
# it would with other PeerMachines. If you assign only from the
# right click menu it doesnt behave that way. Using 
# GetPeerCtrlTarget() you could set it up so that on right click 
# it is also assigned this way.. however.. some machines have weird 
# Track indexes so this won't always work (eg: Jeskola Ninja Delay)
#
# To check how this one works, add a cheapo amp to a bmx, load this
# script, open the cheapo amp and pybuzz buzz param windows and move
# pybuzz's 0-byte param around.
#
# mute
# www.plosive.net
#

import sys

from buzz import *

x = GetMachineNames()
		
for i in range(len(x)) :
	if x[i] == 'Master' :
		a=1
	else:
		da = GetMachine(x[i])
		y = GetMachineInfo (da)
		z = UnpackMachineInfo(y)
		m = z[10] #real machine name
		p = z[7] #list of all parameters, all details
		
		
		print "name : " + m 
		
		if m == "cheapo amp":
	
			pindex = 0

			SetPeerCtrlTarget(0, da, 1, pindex)
			max = p[pindex][4]
			print max			
								
def OnParameter(track,index,value):
	
	global max

	if (index == 1 and track == 0):	
		print "[%02i] %i: %i" % (track,index,value)
		
		doh = max / 128
		value = value * doh
		
		SendPeerCtrlChange(0,1,value);

SetEventTarget("OnParameter",OnParameter)