Login | Register 

Python, simplified logic statements for chops

Still a little green? Ask your beginner's questions here

Python, simplified logic statements for chops

Postby GJG » Tue Apr 30, 2019 8:07 pm

Hi there !
I'm very fresh to this TD and coding experience, so I was hoping that somebody might be able to give me a little bit of guidance when it comes to conjuring more rounded code :)

I have a very basic network of (5 channel) Noise chop on time slice to Chop execute which inputs Select Chop on value change. the idea is very simple: to constantly output a single channel with the largest value into select chop, so I hammered down this simplicity:

n1 = op('noise1')
s1 = op('select1')
maxVal = max(n1[0], n1[1], n1[2], n1[3], n1[4])

if n1[0] == maxVal:
s1.par.channames = 'chan1'
if n1[1] == maxVal:
s1.par.channames = 'chan2'
if n1[2] == maxVal:
s1.par.channames = 'chan3'
if n1[3] == maxVal:
s1.par.channames = 'chan4'
if n1[4] == maxVal:
s1.par.channames = 'chan5'

which as it seems to me is working correctly, although probably not very practical and pretty looking, but then I thought- what if i need to compare and output values of like 100 channels, I'm sure there's a smarter way of going around it than copying and pasting 100 if statements. So after numerous tutorials and peeks at toe examples I couldn't find anything helpful and gotten myself here, so any help would be really appreciated. Thanks
Posts: 6
Joined: Mon Feb 25, 2019 9:38 am

Re: Python, simplified logic statements for chops

Postby matthewwachter » Tue Apr 30, 2019 8:44 pm

Have you seen the Analyze chop? It has a couple different modes that you might find useful:

Index of Highest Peak
Value of Highest Peak

So you could either not time-slice your noise or take your separated channels and use a Shuffle chop to convert it to a single channel with multiple samples before plugging it into the Analyze CHOP.

You could definitely do it in python if you like but using the Analyze chop would be way faster. Regardless, here's how you might do it in python but first I would use at least use a Join or Shuffle chop to get one channel with many samples.

Code: Select all
n1 = op('noise1')['chan1']

max_v = 0
max_i = 0

for i in range(len(n1)):
   if n1[i] > max_v:
      max_v = c[i]
      max_i = i

# print max value and index of max
print(max_v, max_i)

op('select1').par.channames = 'chan' + str(max_i)

Hope this helps! Check out the Analyze CHOP in the OP Snippets scene for usage examples.
Posts: 296
Joined: Fri Jul 08, 2011 3:18 pm
Location: Los Angeles

Re: Python, simplified logic statements for chops

Postby GJG » Wed May 01, 2019 4:46 pm

Hey Matthew,
Big thanks for quick response. Analyze chop is a really handy and fast way of doing it. Since my goal was to have a chop with a single channel outputting a highest value and the channel index(or name) of that value, I had to branch out 2 Analyze chops form Shuffle and set one to Value of Highest Peak and the other to Index of Highest Peak. Then I plugged a rename into Value of Highest Peak and in it's par "renameto" wrote in 'chan' + str(op('analyze2')[0]+1) expression. It kind of worked, but now there's '_0' attached to chan number. is there a way of getting rid of it or getting the same result in a totally different way?
I also tried your code, but it was just throwing couples of zeros into Textport and I just couldn't figure out why. Eventually started reading up on Chop an Channel classes in wiki and came up with this:

Code: Select all
   if op('noise1')[channel.index] == max(op('noise1').chans()):
       op('select1').par.channames = channel.name

Thanks for tips ! :D
Posts: 6
Joined: Mon Feb 25, 2019 9:38 am

Return to Beginners

Who is online

Users browsing this forum: No registered users and 16 guests