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)
1 Like

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))
1 Like

essential gadget, the PMorse("text"), generate a pattern from morse code with default values point = 1/4, hyphen=3/4. Very cool for drum patterns.

d1 >> play("x-o-", dur= PMorse("foxdot"))

Or you can change the default values :
b1 >> dbass(PMorse("foxdot", 0,1), dur=PMorse("foxdot", 1/2,1))

Have fun !

def PMorse(text, point=1/4, hyphen=3/4):
                 MORSE_DICT = { 'A':'.-', 'B':'-...', 
				'C':'-.-.', 'D':'-..', 'E':'.', 
				'F':'..-.', 'G':'--.', 'H':'....', 
				'I':'..', 'J':'.---', 'K':'-.-', 
				'L':'.-..', 'M':'--', 'N':'-.', 
				'O':'---', 'P':'.--.', 'Q':'--.-', 
				'R':'.-.', 'S':'...', 'T':'-', 
				'U':'..-', 'V':'...-', 'W':'.--', 
				'X':'-..-', 'Y':'-.--', 'Z':'--..', 
				'1':'.----', '2':'..---', '3':'...--', 
				'4':'....-', '5':'.....', '6':'-....', 
				'7':'--...', '8':'---..', '9':'----.', 
				'0':'-----', ', ':'--..--', '.':'.-.-.-', 
				'?':'..--..', '/':'-..-.', '-':'-....-', 
				'(':'-.--.', ')':'-.--.-'} 
morse = []
for l in text.split(" "):
	for w in l:
		for i in MORSE_DICT[w.upper()]:
			if i == ".":
				morse.append(point)
			elif i == "-":
				morse.append(hyphen)
return P[morse]
1 Like

Excellent thread! I use this function a lot, it changes the dur of a group of instruments.

def changeDur(rate,group=None):
    if group is None:
        group = Master()
    for i in range(len(group)):
        if (not group.players[i].synthdef is None) and (group.players[i].name in list(map(lambda x:x.name,Clock.playing))):
            try:
                print("modifico a",group.players[i])
                group.players[i].dur = float(group.players[i].dur)*rate
            except:
                print("ERROR")
                print(group.players[i].dur)
                print(help(group.players[i].dur))

p1 >> pluck()
p2 >> play("X-Xo")

changeDur(2) # makes everything twice slower
1 Like