Skip to content

Commit fe5c8ab

Browse files
committed
starting new plugin system + stubs
1 parent 9ce2e54 commit fe5c8ab

File tree

5 files changed

+113
-26
lines changed

5 files changed

+113
-26
lines changed

textbeat/instrument.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
1-
1+
#!/usr/bin/env python
22

33
class Instrument(object):
4-
5-
pass
4+
def __init__(self,name):
5+
self.name = name
6+
def inited(self):
7+
return False
8+
def supported(self):
9+
return False
10+
def support(self):
11+
return []
12+
def stop(self):
13+
pass
14+
15+
PLUGINS = []
16+
# plugins call this method
17+
def export(s):
18+
global PLUGINS
19+
if s not in PLUGINS:
20+
PLUGINS.append(s())
21+
def plugins():
22+
return PLUGINS
23+

textbeat/plugins/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/python
2+
import os
3+
plugins = os.listdir(os.path.dirname(__file__))
4+
plugins = list(filter(lambda x: x not in ['__pycache__','__init__'], map(lambda x: x.split('.')[0], plugins)))
5+
__all__ = plugins

textbeat/plugins/sonicpi.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
import textbeat.instrument as instrument
3+
from textbeat.instrument import Instrument
4+
5+
ERROR = False
6+
try:
7+
import psonic
8+
except ImportError:
9+
ERROR = True
10+
11+
class SonicPi(Instrument):
12+
NAME = 'sonicpi'
13+
def __init__(self):
14+
Instrument.__init__(self, SonicPi.NAME)
15+
self.enabled = False
16+
def init(self):
17+
self.enabled = True
18+
def inited(self):
19+
return self.enabled
20+
def supported(self):
21+
return not ERROR
22+
def support(self):
23+
return ['sonicpi']
24+
def stop(self):
25+
pass
26+
27+
instrument.export(SonicPi)
28+

textbeat/plugins/supercollider.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
import textbeat.instrument as instrument
3+
from textbeat.instrument import Instrument
4+
from shutilwhich import which
5+
6+
ERROR = False
7+
if which('scsynth'):
8+
try:
9+
import pythonosc
10+
except:
11+
ERROR = True
12+
else:
13+
ERROR = True
14+
15+
class SuperCollider(Instrument):
16+
NAME = 'supercollider'
17+
def __init__(self):
18+
Instrument.__init__(self, SuperCollider.NAME)
19+
self.enabled = False
20+
def init(self):
21+
self.enabled = True
22+
def inited(self):
23+
return self.enabled
24+
def supported(self):
25+
return not ERROR
26+
def support(self):
27+
return ['supercollider']
28+
def stop(self):
29+
pass
30+
31+
instrument.export(SuperCollider)
32+

textbeat/support.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from .defs import *
22
from shutilwhich import which
33
import tempfile, shutil
4+
from . import instrument
45
# from xml.dom import minidom
56
ARGS = get_args()
67
SUPPORT = set(['midi'])
@@ -10,34 +11,31 @@
1011
AUTO = False
1112
auto_inited = False
1213

13-
# TODO: eventually: scan and load plugins
14-
15-
class PluginType:
16-
NONE = 0
17-
AUTO = 1
18-
SOUNDFONTS = 2
19-
20-
class Plugin:
21-
def __init__(self, name, typ):
22-
self.name = name
23-
self.type = typ
24-
def register(self):
25-
pass
26-
2714
SUPPORT_PLUGINS = {}
2815

16+
# load new-style plugins from plugins dir
17+
from textbeat.plugins import *
18+
# get plugins from instrument modules's export list
19+
plugs = instrument.plugins()
20+
for plug in plugs:
21+
# plug.init()
22+
ps = plug.support()
23+
SUPPORT_ALL = SUPPORT_ALL.union(ps)
24+
if not plug.supported():
25+
continue
26+
for s in ps:
27+
SUPPORT.add(s)
28+
SUPPORT_PLUGINS[s] = plug
29+
if 'auto' in s:
30+
AUTO = True
31+
auto_inited = True
32+
33+
SUPPORT_ALL.add('carla')
2934
if which('carla'):
3035
SUPPORT.add('carla')
3136
SUPPORT.add('auto') # auto generate
3237
AUTO = True
3338
auto_inited = True
34-
35-
# if which('scsynth'):
36-
# try:
37-
# import oscpy
38-
# SUPPORT.add('supercollider')
39-
# except:
40-
# pass
4139

4240
# try:
4341
# import psonic
@@ -46,18 +44,20 @@ def register(self):
4644
# pass
4745

4846
try:
47+
SUPPORT_ALL.add('fluidsynth')
4948
if which('fluidsynth'):
5049
import fluidsynth # https://github.com/flipcoder/pyfluidsynth
5150
SUPPORT.add('fluidsynth')
5251
SUPPORT.add('soundfonts')
5352
SOUNDFONTS = True
54-
except AttributeError:
55-
error("pyFluidSynth AttributeError detected. Use this pyFluidSynth version: https://github.com/flipcoder/pyfluidsynth")
53+
# except AttributeError:
54+
# error("pyFluidSynth AttributeError detected. Use this pyFluidSynth version: https://github.com/flipcoder/pyfluidsynth")
5655
except ImportError:
5756
pass
5857

5958
csound = None
6059
# if which('csound'):
60+
SUPPORT_ALL.add('csound')
6161
try:
6262
import csnd6
6363
SUPPORT.add('csound')
@@ -219,6 +219,10 @@ def support_stop():
219219
if BGPROC:
220220
BGPIPE.send((BGCMD.QUIT,))
221221
BGPROC.join()
222+
global plugs
223+
for plug in plugs:
224+
if plug.inited():
225+
plug.stop()
222226
# if gen_inited and carla_proj:
223227
# try:
224228
# os.remove(carla_proj[1])

0 commit comments

Comments
 (0)