| 1 | # Copyright (C) 2006 by Patrick Stinson |
|---|
| 2 | # patrickkidd@gmail.com |
|---|
| 3 | # |
|---|
| 4 | # This program is free software; you can redistribute it and/or modify |
|---|
| 5 | # it under the terms of the GNU General Public License as published by |
|---|
| 6 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 7 | # (at your option) any later version. |
|---|
| 8 | # |
|---|
| 9 | # This program is distributed in the hope that it will be useful, |
|---|
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | # GNU General Public License for more details. |
|---|
| 13 | # |
|---|
| 14 | # You should have received a copy of the GNU General Public License |
|---|
| 15 | # along with this program; if not, write to the |
|---|
| 16 | # Free Software Foundation, Inc., |
|---|
| 17 | # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 18 | # |
|---|
| 19 | """ |
|---|
| 20 | Brain folding fuel. |
|---|
| 21 | |
|---|
| 22 | Live timeline editor |
|---|
| 23 | - pattern |
|---|
| 24 | \_ notes |
|---|
| 25 | \_ on, off events |
|---|
| 26 | |
|---|
| 27 | pattern/notes |
|---|
| 28 | |
|---|
| 29 | EVENT POOL |
|---|
| 30 | current time |
|---|
| 31 | end time |
|---|
| 32 | expiration |
|---|
| 33 | music time - repr? |
|---|
| 34 | control window |
|---|
| 35 | play window |
|---|
| 36 | |
|---|
| 37 | NOTE POOL |
|---|
| 38 | id pool |
|---|
| 39 | """ |
|---|
| 40 | |
|---|
| 41 | import container |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | class Synth(container.DictProxy): |
|---|
| 45 | """ a dict of values, set once per note. """ |
|---|
| 46 | |
|---|
| 47 | name = None |
|---|
| 48 | node = None |
|---|
| 49 | |
|---|
| 50 | def __init__(self): |
|---|
| 51 | container.DictProxy.__init__(self) |
|---|
| 52 | |
|---|
| 53 | def __repr__(self): |
|---|
| 54 | s = container.DictProxy.__repr__(self) |
|---|
| 55 | return "<%s name=\'%s\', node=\'%s\', %s>" % (self.__class__.__name__, |
|---|
| 56 | self.name, self.node, |
|---|
| 57 | s) |
|---|
| 58 | def __str__(self): |
|---|
| 59 | return self.__repr__() |
|---|
| 60 | |
|---|
| 61 | def pitch(self, pitch): |
|---|
| 62 | """ 0.0 < pitch < n |
|---|
| 63 | nominal: 1.0 |
|---|
| 64 | """ |
|---|
| 65 | pass |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | class Effect(Synth): |
|---|
| 69 | pass |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | class Player: |
|---|
| 73 | """ manage synth and buffer ids for playing synths. """ |
|---|
| 74 | def __init__(self, server): |
|---|
| 75 | self.server = server |
|---|
| 76 | |
|---|
| 77 | def route(self, synth, sid): |
|---|
| 78 | if synth.node is None: |
|---|
| 79 | action = 1 |
|---|
| 80 | node = 0 |
|---|
| 81 | else: |
|---|
| 82 | action = 2 |
|---|
| 83 | node = synth.node |
|---|
| 84 | return ['/s_new', synth.name, sid, action, node] |
|---|
| 85 | |
|---|
| 86 | def play(self, synth, start, stop): |
|---|
| 87 | self.server.synthpool.check() |
|---|
| 88 | sid = self.server.synthpool.get(stop + 5) |
|---|
| 89 | msg = self.route(synth, sid) |
|---|
| 90 | for key, value in synth.items(): |
|---|
| 91 | msg.extend([key, value]) |
|---|
| 92 | self.server.sendBundleAbs(start, [msg]) |
|---|
| 93 | self.server.sendBundleAbs(stop, [['/n_free', sid]]) |
|---|
| 94 | |
|---|
| 95 | def play_rt(self, synth): |
|---|
| 96 | """ """ |
|---|
| 97 | self.server.synthpool.check() |
|---|
| 98 | sid = self.server.synthpool.get() |
|---|
| 99 | msg = self.route(synth, sid) |
|---|
| 100 | for key, value in synth.items(): |
|---|
| 101 | msg.extend([key, value]) |
|---|
| 102 | self.server.sendBundleAbs(-1, [msg]) |
|---|
| 103 | return sid |
|---|
| 104 | |
|---|
| 105 | def stop_rt(self, sid): |
|---|
| 106 | """ """ |
|---|
| 107 | self.server.sendBundleAbs(-1, [['/n_free', sid]]) |
|---|
| 108 | self.server.synthpool.recycle(sid) |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | def main(): |
|---|
| 112 | import os |
|---|
| 113 | import time |
|---|
| 114 | import tempoclock |
|---|
| 115 | import loader |
|---|
| 116 | import server |
|---|
| 117 | |
|---|
| 118 | ctl = server.connect(spew=True) |
|---|
| 119 | ctl.sendMsg('/dumpOSC', 1) |
|---|
| 120 | |
|---|
| 121 | SYNTHDEF_PATH = os.path.join(os.path.expanduser('~'), |
|---|
| 122 | '.pksampler', 'synthdefs') |
|---|
| 123 | SYNTHDEFS = ('JASStereoSamplePlayer.scsyndef', |
|---|
| 124 | 'JASSine.scsyndef', |
|---|
| 125 | ) |
|---|
| 126 | for fname in SYNTHDEFS: |
|---|
| 127 | ctl.sendMsg('/d_load', os.path.join(SYNTHDEF_PATH, fname)) |
|---|
| 128 | |
|---|
| 129 | player = Player(ctl) |
|---|
| 130 | ldr = loader.Loader(ctl) |
|---|
| 131 | bid = ldr.load('/Users/patrick/.pksampler/clicks/click_1.wav') |
|---|
| 132 | |
|---|
| 133 | clock = tempoclock.TempoClock(140.0) |
|---|
| 134 | |
|---|
| 135 | beats = [clock.spb() * i for i in range(100)] |
|---|
| 136 | now = time.time() + 1 |
|---|
| 137 | freqs = [440, 550, 220, 200, 460] |
|---|
| 138 | synth = Synth() |
|---|
| 139 | synth.name = 'JASSine' |
|---|
| 140 | for seconds in beats: |
|---|
| 141 | abs = now + seconds |
|---|
| 142 | freqs = freqs[1:] + freqs[:1] |
|---|
| 143 | synth['freq'] = freqs[0] |
|---|
| 144 | player.play(synth, abs, abs + 1) |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | if __name__ == '__main__': |
|---|
| 148 | main() |
|---|