# Share your snippets

**URL:** <https://forum.toplap.org/t/share-your-snippets/711>\
**Category:** Foxdot\
**Created:** [30 July 2019 17:37 UTC](https://forum.toplap.org/t/share-your-snippets/711 "2019-07-30T17:37:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![CrashServer](https://forum.toplap.org/user_avatar/forum.toplap.org/crashserver/32/310_2.png) [@CrashServer](https://forum.toplap.org/u/CrashServer)\
**Post date:** [30 July 2019 17:37 UTC](https://forum.toplap.org/t/share-your-snippets/711/1 "2019-07-30T17:37:09Z")

</div>

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)
```

---

<div class="post-metadata">

**Author:** ![ryan](https://forum.toplap.org/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://forum.toplap.org/u/ryan)\
**Post date:** [31 July 2019 08:33 UTC](https://forum.toplap.org/t/share-your-snippets/711/2 "2019-07-31T08:33:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CrashServer](https://forum.toplap.org/user_avatar/forum.toplap.org/crashserver/32/310_2.png) [@CrashServer](https://forum.toplap.org/u/CrashServer)\
**Post date:** [1 October 2019 18:40 UTC](https://forum.toplap.org/t/share-your-snippets/711/3 "2019-10-01T18:40:57Z")

</div>

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())
```

---

<div class="post-metadata">

**Author:** ![pedro823](https://forum.toplap.org/letter_avatar_proxy/v4/letter/p/35a633/32.png) [@pedro823](https://forum.toplap.org/u/pedro823)\
**Post date:** [2 October 2019 20:43 UTC](https://forum.toplap.org/t/share-your-snippets/711/4 "2019-10-02T20:43:00Z")

</div>

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))
```

---

<div class="post-metadata">

**Author:** ![CrashServer](https://forum.toplap.org/user_avatar/forum.toplap.org/crashserver/32/310_2.png) [@CrashServer](https://forum.toplap.org/u/CrashServer)\
**Post date:** [27 February 2020 16:50 UTC](https://forum.toplap.org/t/share-your-snippets/711/5 "2020-02-27T16:50:22Z")

</div>

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]
```

---

<div class="post-metadata">

**Author:** ![mathigatti](https://forum.toplap.org/user_avatar/forum.toplap.org/mathigatti/32/529_2.png) [@mathigatti](https://forum.toplap.org/u/mathigatti)\
**Post date:** [2 November 2020 02:47 UTC](https://forum.toplap.org/t/share-your-snippets/711/6 "2020-11-02T02:47:46Z")

</div>

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
```
