Share your snippets

What I find really cool with Foxdot is that you can write small functions very easily and without being a genius of programming.
I propose to share these little pieces of code that can be used by everyone.

FIND SCALE
The other day, I was looking for a melodic line that sounds good with a song. I ended up with a note list in a chromatic scale and wondered which scale contains them. Easy with python:

def find_scale(notes):
    """print all scales which contain the notes. notes is a list"""
    for name, scale in Scale.library().items():
        try:		
            result =  all(elem in scale for elem in notes)
        except:
            pass
        if result:
            print(name)

find_scale([0,1,3,5,10])
chromatic
phrygian
locrian
susb9

BINARY CONVERTER
I saw this on the latest version of tidal, easy with python :

def binary(number):
   """ return a list converted to binary from a number """
    binlist = [int(i) for i in str(bin(number)[2:])]
    return binlist

hh >> play("-", amplify=binary(122589), dur=1/4)
11101111011011101

RANDOM CHANGE SYNTH

synthlist = [i for i in SynthDefs][4:]

sy >> blip(PRand(8), dur=1/4).changeSynth(synthlist)

Very nice! The finding a scale function is pretty cool. I actually forgot about the changeSynth method!

Another one, totally useful and essential : the Time Pattern Generator:

def PTime():
### Generate a pattern from the local machine time
return [int(t) for t in str(Clock.get_time_at_beat(int(Clock.now()))) if t is not '.']

ti >> dbass(PTime(), dur=P[4]/PTime())

Extremely useful for progression in your music:

def grow(to, how_long=32, fromm=0):
    return linvar([fromm, to], [how_long, inf], start=now)

Use it like so:

d1 >> play('x ', amp=grow(1.2))