1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
import logging
from renardo_lib.Code import *
from renardo_gatherer.samples_download import SPackManager
FoxDotCode.namespace = globals()
spack_manager = SPackManager()
from renardo_lib.TempoClock import *
from renardo_lib.Buffers import *
from renardo_lib.Players import *
from renardo_lib.Patterns import *
from renardo_lib.Effects import *
from renardo_lib.TimeVar import *
from renardo_lib.Constants import *
from renardo_lib.Midi import *
from renardo_lib.Settings import *
from renardo_lib.SCLang._SynthDefs import *
from renardo_lib.ServerManager import *
from renardo_lib.SCLang import SynthDefs, Env, SynthDef, CompiledSynthDef
from renardo_lib.Root import Root
from renardo_lib.Scale import Scale, Tuning
@PatternMethod
def __getitem__(self, key):
""" Overrides the Pattern.__getitem__ to allow indexing
by TimeVar and PlayerKey instances. """
if isinstance(key, PlayerKey):
# Create a player key whose calculation is get_item
return key.index(self)
elif isinstance(key, TimeVar):
# Create a TimeVar of a PGroup that can then be indexed by the key
item = TimeVar(tuple(self.data))
item.dependency = key
item.evaluate = fetch(Get)
return item
else:
return self.getitem(key)
def player_method(f):
""" Decorator for assigning functions as Player methods.
>>> @player_method
... def test(self):
... print(self.degree)
>>> p1.test()
"""
setattr(Player, f.__name__, f)
return getattr(Player, f.__name__)
PlayerMethod = player_method # Temporary alias
def _futureBarDecorator(n, multiplier=1):
if callable(n):
def switch(*args, **kwargs):
Clock.now_flag = True
output = n()
Clock.now_flag = False
return output
Clock.schedule(switch, Clock.next_bar())
return switch
def wrapper(f):
Clock.schedule(f, Clock.next_bar() + (n * multiplier))
return f
return wrapper
def next_bar(n=0):
''' Schedule functions when you define them with @nextBar
Functions will run n beats into the next bar.
>>> nextBar(v1.solo)
or
>>> @nextBar
... def dostuff():
... v1.solo()
'''
return _futureBarDecorator(n)
nextBar = next_bar # temporary alias
def futureBar(n=0):
''' Schedule functions when you define them with @futureBar
Functions will run n bars in the future (0 is the next bar)
>>> futureBar(v1.solo)
or
>>> @futureBar(4)
... def dostuff():
... v1.solo()
'''
return _futureBarDecorator(n, Clock.bar_length())
def update_foxdot_clock(clock):
""" Tells the TimeVar, Player, and MidiIn classes to use
a new instance of TempoClock. """
assert isinstance(clock, TempoClock)
for item in (TimeVar, Player, MidiIn):
item.set_clock(clock)
clock.add_method(_convert_json_bpm)
return
def update_foxdot_server(serv):
""" Tells the `Effect` and`TempoClock`classes to send OSC messages to
a new ServerManager instance.
"""
assert isinstance(serv, ServerManager)
TempoClock.set_server(serv)
SynthDefs.set_server(serv)
return
def instantiate_player_objects():
""" Instantiates all two-character variable Player Objects """
alphabet = list('abcdefghijklmnopqrstuvwxyz')
numbers = list('0123456789')
for char1 in alphabet:
group = []
for char2 in alphabet + numbers:
arg = char1 + char2
FoxDotCode.namespace[arg] = EmptyPlayer(arg)
group.append(arg)
FoxDotCode.namespace[char1 + "_all"] = Group(*[FoxDotCode.namespace[char1+str(n)] for n in range(10)])
return
def _reload_synths():
""" Resends all the synth / sample info to SuperCollider. Useful for times
when starting FoxDot before running `FoxDot.start` in SuperCollider. """
from renardo_lib import SCLang
from renardo_lib import Effects
reload(SCLang._SynthDefs)
reload(Effects)
Samples._reset_buffers()
return
def foxdot_reload():
Server.reset()
SynthDefs.reload()
FxList.reload()
Samples.reset()
return
def _convert_json_bpm(clock, data):
""" Returns a TimeVar object that has been sent across a network using JSON """
if isinstance(data, list):
cls = data[0]
val = data[1]
dur = data[2]
return FoxDotCode.namespace[cls](val, dur)
else:
return data
def Master():
""" Returns a `Group` containing all the players currently active in the Clock """
return Group(*Clock.playing)
def Ramp(t=32, ramp_time=4):
""" Returns a `linvar` that goes from 0 to 1 over the course of the last
`ramp_time` bars of every `t` length cycle. """
return linvar([0,0,1,0],[t-ramp_time, ramp_time, 0, 0])
def allow_connections(valid = True, *args, **kwargs):
""" Starts a new instance of ServerManager.TempoServer and connects it with the clock. Default port is 57999 """
if valid:
Clock.start_tempo_server(TempoServer, **kwargs)
print("Listening for connections on {}".format(Clock.tempo_server))
else:
Clock.kill_tempo_server()
print("Closed connections")
return
def Go():
""" Function to be called at the end of Python files with FoxDot code in to keep
the TempoClock thread alive. """
try:
import time
while 1:
time.sleep(100)
except KeyboardInterrupt:
return
# Util class
class _util:
def __repr__(self):
return "Renardo ver. 0.9.12"
def reload(self):
Server.reset()
SynthDefs.reload()
FxList.reload()
Samples.reset()
return
def reassign_clock(self):
FoxDotCode.namespace['Clock'] = _Clock
return
FoxDot = _util()
# Create a clock and define functions
_ = None
logging.basicConfig(level=logging.ERROR)
when.set_namespace(FoxDotCode) # experimental
_Clock = Clock = TempoClock()
update_foxdot_server(Server)
update_foxdot_clock(Clock)
instantiate_player_objects()
# Create a "now" time variable
now = var([0]).transform(lambda a: Clock.now())
nextbar = var([0]).transform(lambda a: Clock.next_bar())
Attributes = Player.get_attributes()
PatternMethods = Pattern.get_methods()
PatternTypes = functions(Patterns.Sequences)
# Start
Clock.start()
|