Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/common/Util/InstrumentDB.py
blob: fd276ae9127696d3c998a9e7e4c7d9681fe6edd4 (plain)
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
#!/bin/env python
import os

##############
## SOUNDS
##############

class Instrument:
    def __init__(self, id):
        self.instrumentId = id

    # build an Instrument instance from argument list
    def loadFromArgs( self, name, csoundInstrumentId, register, loopStart,
            loopEnd, crossDur, ampScale, kit, wav, img, category ):
        self.name = name
        self.csoundInstrumentId = csoundInstrumentId
        self.instrumentRegister = register
        self.loopStart = loopStart
        self.loopEnd = loopEnd
        self.crossDur = crossDur
        self.ampScale = ampScale
        self.kit = kit
        self.wav = wav
        self.img = img
        self.category = category

    # build an Instrument instance by parsing a file
    def loadFromPath(self, path ):
        f = file(path, 'r')
        magic = f.readline()[:-1]
        if (magic != 'TamTam idf v1'):
            raise 'given file has wrong header'
        self.name = f.readline()
        self.csoundInstrumentId = f.readline()
        self.register = f.readline()
        self.loopStart = float( f.readline())
        self.loopEnd = float( f.readline())
        self.crossDur = float( f.readline())
        self.ampScale = float( f.readline())
        self.kit = None
        self.wav = f.readline()
        self.img = f.readline()
        self.category = f.readline().split()
        f.close()

class InstrumentDB:

    # initialize an empty InstrumentDB instance
    def __init__(self):
        self.labelSet = {'All':set([])}  # <key>  -> all instruments labelled by <key>
        self.inst = []      # all instruments
        self.instNamed = {} # <name> -> instrument with that name
        self.instId = {}    # <instrumentId> -> instrument

    # TEMP? add instrument from args
    def addInstrumentFromArgs( self, name, csoundInstrumentId, register, loopStart,
            loopEnd, crossDur, ampScale, kit, wav, img, category ):
        i = Instrument(len(self.inst))
        self.inst += [ i ]
        i.loadFromArgs( name, csoundInstrumentId, register, loopStart, loopEnd, crossDur, ampScale, kit, wav, img, category )
        self.instNamed[ i.name ] = i
        self.instId[i.instrumentId] = i

        self.labelSet['All'].add(i)
        if not self.labelSet.has_key(category):
            self.labelSet[category] = set([])
        self.labelSet[category].add( i )

    # add an instrument to the DB by reading from an instrument definition file
    def addInstrument( self, path ):
        i = Instrument(len(self.inst))
        self.inst += [ i ]
        i.loadFromPath( path )
        self.instNamed[ i.name ] = i
        self.instId[self.instNamed[i].instrumentId] = i
        #print 'labelSet... ', self.labelSet
        self.labelSet['All'].add(i)
        if not self.labelSet.has_key(category):
            self.labelSet[category] = set([])
        self.labelSet[category].add( i )

    # try to load each file in a given folder as an instrument def. file
    def scanInstrumentDir( self, path ):
        dirlist = os.listdir( path )
        for fpath in dirlist:
            try :
                self.addInstrument( path + fpath )
            except :
                print 'ERROR: scanning instrument path %s: file %s invalid' % (path, fpath)

    def getLabels( self ):
        return self.labelSet.keys()

    def getSet( self, label ):
        return self.labelSet[label]

    def getInstrument( self, id ):
        return self.inst[id]

    def debug_summarize(self):
        for i in self.inst:
            print i.id, i.name

        for l in self.labelSet:
            print l, [ i.name for i in self.labelSet[l]]


db_instance = None
def getRef():
    global db_instance
    if (None == db_instance):
        db_instance = InstrumentDB()
    return db_instance


if __name__ == "__main__":
    i1 = getRef()
    i2 = getRef()

    print i1, i2

    import sys
    i1.scanInstrumentDir( sys.argv[1] )

    i1.debug_summarize()