From 3e9a586f404b70cda362cf888653de9c940fd849 Mon Sep 17 00:00:00 2001 From: Oli Date: Fri, 19 Oct 2007 08:08:02 +0000 Subject: switch to instrumentDB --- (limited to 'TamTamJam.activity') diff --git a/TamTamJam.activity/Jam/GenRythm.py b/TamTamJam.activity/Jam/GenRythm.py index 330e174..b9e6d48 100644 --- a/TamTamJam.activity/Jam/GenRythm.py +++ b/TamTamJam.activity/Jam/GenRythm.py @@ -1,10 +1,14 @@ import random import common.Config as Config +import common.Util.InstrumentDB as InstrumentDB from common.Generation.GenerationConstants import GenerationConstants from common.Generation.Utils import * class GenRythm: + def __init__(self): + self.instrumentDB = InstrumentDB.getRef() + def drumRythmSequence(self, instrumentName, nbeats, density, regularity ): rythmSequence = [] binSelection = [] @@ -14,28 +18,28 @@ class GenRythm: countDown = 0 onsetTime = None - if Config.INSTRUMENTS[instrumentName].instrumentRegister == Config.PUNCH: + if self.instrumentDB.instNamed[instrumentName].instrumentRegister == Config.PUNCH: registerDensity = 0.5 downBeatRecurence = 4 downBeats = [x for x in GenerationConstants.DRUM_PUNCH_ACCENTS[ nbeats ]] for downBeat in downBeats: upBeats.append( downBeat + Config.TICKS_PER_BEAT / 2 ) - if Config.INSTRUMENTS[instrumentName].instrumentRegister == Config.LOW: + if self.instrumentDB.instNamed[instrumentName].instrumentRegister == Config.LOW: registerDensity =1 downBeatRecurence = 4 downBeats = [x for x in GenerationConstants.DRUM_LOW_ACCENTS[ nbeats ]] for downBeat in downBeats: upBeats.append( downBeat + Config.TICKS_PER_BEAT / 2 ) - if Config.INSTRUMENTS[instrumentName].instrumentRegister == Config.MID: + if self.instrumentDB.instNamed[instrumentName].instrumentRegister == Config.MID: registerDensity = .75 downBeatRecurence = 1 downBeats = [x for x in GenerationConstants.DRUM_MID_ACCENTS[ nbeats ]] for downBeat in downBeats: upBeats.append( downBeat + Config.TICKS_PER_BEAT / 4 ) - if Config.INSTRUMENTS[instrumentName].instrumentRegister == Config.HIGH: + if self.instrumentDB.instNamed[instrumentName].instrumentRegister == Config.HIGH: registerDensity = 1.5 downBeatRecurence = 1 downBeats = [x for x in GenerationConstants.DRUM_HIGH_ACCENTS[ nbeats ]] @@ -48,10 +52,10 @@ class GenRythm: list = range( int( realDensity * len( downBeats ) ) ) for i in list: - if random.random() < ( regularity * downBeatRecurence ) and binSelection.count( 1 ) < len( downBeats ): - binSelection.append( 1 ) + if random.random() < ( regularity * downBeatRecurence ) and binSelection.count( 1 ) < len( downBeats ): + binSelection.append( 1 ) else: - if binSelection.count( 0 ) < len( downBeats ): + if binSelection.count( 0 ) < len( downBeats ): binSelection.append( 0 ) else: binSelection.append( 1 ) diff --git a/TamTamJam.activity/Jam/JamMain.py b/TamTamJam.activity/Jam/JamMain.py index f795008..dbd7f8b 100644 --- a/TamTamJam.activity/Jam/JamMain.py +++ b/TamTamJam.activity/Jam/JamMain.py @@ -6,6 +6,7 @@ import pango import os, sys, shutil +import common.Util.Instruments import common.Config as Config from gettext import gettext as _ import sugar.graphics.style as style @@ -173,7 +174,7 @@ class JamMain(gtk.EventBox): self.instrumentImage = {} self.instrumentImageActive = {} for inst in self.instrumentDB.getSet( "All" ): - self.prepareInstrumentImage( inst.id, inst.img ) + self.prepareInstrumentImage( inst.instrumentId, inst.img ) #-- Loop Images --------------------------------------- self.loopImage = {} # get filled in through updateLoopImage @@ -260,18 +261,18 @@ class JamMain(gtk.EventBox): self.keyMap = {} # default instrument - self._updateInstrument( Config.INSTRUMENTS["kalimba"].instrumentId, 0.5 ) + self._updateInstrument( self.instrumentDB.instNamed["kalimba"].instrumentId, 0.5 ) self.instrumentStack = [] # metronome page = NoteDB.Page( 1, local = False ) - self.metronomePage = self.noteDB.addPage( -1, page ) + self.metronomePage = self.noteDB.addPage( -1, page ) self.metronome = False #-- Drums --------------------------------------------- self.drumLoopId = None # use dummy values for now - self.drumFillin = Fillin( 2, 100, Config.INSTRUMENTS["drum1kit"].instrumentId, 0, 1 ) + self.drumFillin = Fillin( 2, 100, self.instrumentDB.instNamed["drum1kit"].instrumentId, 0, 1 ) #-- Desktops ------------------------------------------ self.curDesktop = None @@ -362,7 +363,7 @@ class JamMain(gtk.EventBox): if Config.KEY_MAP_PIANO.has_key( key ): pitch = Config.KEY_MAP_PIANO[key] - inst = Config.INSTRUMENTSID[self.instrument["id"]] + inst = self.instrumentDB.instId[self.instrument["id"]] if inst.kit: # drum kit if pitch in GenerationConstants.DRUMPITCH: @@ -426,7 +427,7 @@ class JamMain(gtk.EventBox): def _stopNote( self, key ): csnote = self.key_dict[key] - if Config.INSTRUMENTSID[ csnote.instrumentId ].csoundInstrumentId == Config.INST_TIED: + if self.instrumentDB.instId[ csnote.instrumentId ].csoundInstrumentId == Config.INST_TIED: csnote.duration = .5 csnote.decay = 0.7 csnote.tied = False @@ -476,7 +477,7 @@ class JamMain(gtk.EventBox): self.csnd.loopSetNumTicks( ticks, loopId ) self.drumFillin.setLoopId( loopId ) - self.drumFillin.setProperties( self.tempo, Config.INSTRUMENTSID[id].name, volume, beats, reverb ) + self.drumFillin.setProperties( self.tempo, self.instrumentDB.instId[id].name, volume, beats, reverb ) self.drumFillin.unavailable( noteOnsets, notePitchs ) self.drumFillin.play() @@ -527,7 +528,7 @@ class JamMain(gtk.EventBox): # TODO update track volume - inst = Config.INSTRUMENTSID[id] + inst = self.instrumentDB.instId[id] offset = 0 for page in tune: @@ -548,11 +549,11 @@ class JamMain(gtk.EventBox): self.csnd.loopSetNumTicks( offset, loopId ) - while startTick > offset: + while startTick > offset: startTick -= offset - + # sync to heartbeat - if sync: + if sync: beatTick = startTick % Config.TICKS_PER_BEAT syncTick = self.csnd.loopGetTick( self.heartbeatLoop ) % Config.TICKS_PER_BEAT if beatTick > syncTick: @@ -590,7 +591,7 @@ class JamMain(gtk.EventBox): 0.5, # pan 100, # duration 0, # track - Config.INSTRUMENTS["drum1hatpedal"].instrumentId, + self.instrumentDB.instNamed["drum1hatpedal"].instrumentId, reverbSend = 0.5, tied = True, mode = 'mini' ) @@ -600,12 +601,12 @@ class JamMain(gtk.EventBox): for b in range( self.noteDB.getPage( page ).beats ): cs = baseCS.clone() - cs.instrumentId = Config.INSTRUMENTS["drum1hatshoulder"].instrumentId + cs.instrumentId = self.instrumentDB.instNamed["drum1hatshoulder"].instrumentId cs.amplitude = 0.5 cs.onset += offset stream.append( cs ) - + onset = period while onset < Config.TICKS_PER_BEAT: cs = baseCS.clone() @@ -616,7 +617,7 @@ class JamMain(gtk.EventBox): offset += Config.TICKS_PER_BEAT self.noteDB.addNotes( [ page, 1, len(stream) ] + stream + [ -1 ] ) - + def removeMetronome( self, page ): self.noteDB.deleteNotesByTrack( [ page ], [ 1 ] ) @@ -634,14 +635,14 @@ class JamMain(gtk.EventBox): self.paused = True for loop in loops: self.csnd.loopPause( loop ) - - def setStopped( self ): + + def setStopped( self ): for drum in list(self.desktop.drums): self.desktop.deactivateDrum(drum) - + for loop in list(self.desktop.loops): # we copy the list using the list() method self.desktop.deactivateLoop(loop) - + #========================================================== @@ -654,7 +655,7 @@ class JamMain(gtk.EventBox): rval += l return rval - notes = flatten( generator( Config.INSTRUMENTSID[instrumentId].name, beats, 0.8, regularity, reverb) ) + notes = flatten( generator( self.instrumentDB.instId[instrumentId].name, beats, 0.8, regularity, reverb) ) if pageId == -1: page = Page( beats ) @@ -669,7 +670,7 @@ class JamMain(gtk.EventBox): def _generateTrack( self, instrumentId, page, track, parameters, algorithm ): dict = { track: { page: self.noteDB.getCSNotesByTrack( page, track ) } } - instruments = { page: [ Config.INSTRUMENTSID[instrumentId].name for i in range(Config.NUMBER_OF_TRACKS) ] } + instruments = { page: [ self.instrumentDB.instId[instrumentId].name for i in range(Config.NUMBER_OF_TRACKS) ] } beatsOfPages = { page: self.noteDB.getPage(page).beats } algorithm( parameters, @@ -1103,7 +1104,7 @@ class JamMain(gtk.EventBox): # Sync def setSyncBeats( self, beats ): - self.jamToolbar.setSyncBeats( beats ) + self.jamToolbar.setSyncBeats( beats ) def _setSyncBeats( self, beats ): if beats == self.syncBeats: @@ -1114,7 +1115,7 @@ class JamMain(gtk.EventBox): ticks = beats * Config.TICKS_PER_BEAT curTick = self.csnd.loopGetTick( self.heartbeatLoop ) - + self.csnd.loopSetNumTicks( ticks, self.heartbeatLoop ) while curTick > ticks: curTick -= ticks diff --git a/TamTamJam.activity/Jam/Picker.py b/TamTamJam.activity/Jam/Picker.py index 5609e56..731e85b 100644 --- a/TamTamJam.activity/Jam/Picker.py +++ b/TamTamJam.activity/Jam/Picker.py @@ -24,7 +24,7 @@ class Picker( gtk.HBox ): gtk.HBox.__init__( self ) self.owner = owner - + # take drawing setup from owner self.gc = owner.gc self.colors = owner.colors @@ -96,7 +96,7 @@ class Picker( gtk.HBox ): if self._testAgainstFilter( block ): self.pickerBox.pack_start( block, False, False, 3 ) - + block.show_all() return block @@ -159,7 +159,7 @@ class Picker( gtk.HBox ): class Instrument( Picker ): - + def __init__( self, owner, filter = ( "All" ) ): Picker.__init__( self, owner, filter ) @@ -168,12 +168,12 @@ class Instrument( Picker ): self.instrumentDB = InstrumentDB.getRef() for inst in self.instrumentDB.getSet( "All" ): - self.addBlock( inst.id ) + self.addBlock( inst.instrumentId ) def addBlock( self, id ): # match data structure of Block.Instrument - data = { "name": _(Config.INSTRUMENTSID[id].name), - "id": id } + data = { "name": _(self.instrumentDB.instId[id].name), + "id": id } win = gtk.gdk.get_default_root_window() width = Block.Instrument.WIDTH @@ -181,13 +181,13 @@ class Instrument( Picker ): pixmap = gtk.gdk.Pixmap( win, width, height ) self.gc.set_clip_rectangle( gtk.gdk.Rectangle( 0, 0, width, height ) ) - + # draw bg self.gc.foreground = self.colors["Picker_Bg"] pixmap.draw_rectangle( self.gc, True, 0, 0, width, height ) self.gc.set_clip_mask( self.blockMask ) - + # draw border self.gc.foreground = self.colors["Border_Inactive"] self.gc.set_clip_origin( -Block.Instrument.MASK_START, 0 ) @@ -196,7 +196,7 @@ class Instrument( Picker ): # draw block inst = self.owner.getInstrumentImage( data["id"] ) self.gc.set_clip_origin( -Block.Instrument.MASK_START, -height ) - pixmap.draw_drawable( self.gc, inst, 0, 0, 0, 0, width, height ) + pixmap.draw_drawable( self.gc, inst, 0, 0, 0, 0, width, height ) image = gtk.Image() image.set_from_pixmap( pixmap, None ) @@ -211,12 +211,11 @@ class Instrument( Picker ): if "All" in self.filter: return True - for label in self.instrumentDB.getInstrument( block.data["id"] ).labels: - if label in self.filter: - return True + if self.instrumentDB.getInstrument( block.data["id"] ).category in self.filter: + return True return False - + def on_button_press( self, widget, event ): walloc = widget.get_allocation() salloc = self.scrolledWindow.get_allocation() @@ -227,7 +226,7 @@ class Instrument( Picker ): class Drum( Picker ): - + def __init__( self, owner, filter = None ): Picker.__init__( self, owner, filter ) @@ -236,12 +235,12 @@ class Drum( Picker ): self.instrumentDB = InstrumentDB.getRef() for inst in self.instrumentDB.getSet( "percussions" ): - if Config.INSTRUMENTS[inst.name].kit: - self.addBlock( inst.id ) + if self.instrumentDB.instNamed[inst.name].kit: + self.addBlock( inst.instrumentId ) def addBlock( self, id ): # match data structure of Block.Drum - data = { "name": _(Config.INSTRUMENTSID[id].name), + data = { "name": _(self.instrumentDB.instId[id].name), "id": id } win = gtk.gdk.get_default_root_window() @@ -250,13 +249,13 @@ class Drum( Picker ): pixmap = gtk.gdk.Pixmap( win, width, height ) self.gc.set_clip_rectangle( gtk.gdk.Rectangle( 0, 0, width, height ) ) - + # draw bg self.gc.foreground = self.colors["Picker_Bg"] pixmap.draw_rectangle( self.gc, True, 0, 0, width, height ) self.gc.set_clip_mask( self.blockMask ) - + # draw border self.gc.foreground = self.colors["Border_Inactive"] self.gc.set_clip_origin( -Block.Drum.MASK_START, 0 ) @@ -265,7 +264,7 @@ class Drum( Picker ): # draw block inst = self.owner.getInstrumentImage( data["id"] ) self.gc.set_clip_origin( -Block.Drum.MASK_START, -height ) - pixmap.draw_drawable( self.gc, inst, 0, 0, 0, 0, width, height ) + pixmap.draw_drawable( self.gc, inst, 0, 0, 0, 0, width, height ) image = gtk.Image() image.set_from_pixmap( pixmap, None ) @@ -274,8 +273,8 @@ class Drum( Picker ): block.modify_bg( gtk.STATE_NORMAL, self.colors["Picker_Bg"] ) block.add( image ) - Picker.addBlock( self, data, data["name"], block ) - + Picker.addBlock( self, data, data["name"], block ) + def on_button_press( self, widget, event ): walloc = widget.get_allocation() salloc = self.scrolledWindow.get_allocation() @@ -284,16 +283,16 @@ class Drum( Picker ): class Loop( Picker ): - + def __init__( self, owner, filter = None ): Picker.__init__( self, owner, filter ) self.type = Loop self.presetLoops = self._scanDirectory( Config.FILES_DIR+"/Loops" ) - + def _loadFile( self, fullpath, filename ): - if filename[-4:] != ".ttl": + if filename[-4:] != ".ttl": if Config.DEBUG >= 3: print "WARNING: incorrect extension on loop file: " + filename return -1 try: @@ -310,15 +309,15 @@ class Loop( Picker ): if len(newPages) != 1: print "ERROR: bad loop file, contains more than one page (or none)" return -1 - + id = newPages.pop() # new pageId self.owner.noteDB.getPage( id ).setLocal( False ) # flag as a global page - + self.addBlock( id, filename[:-4] ) return id - + except OSError,e: print 'ERROR: failed to open file %s for reading\n' % ofilename return -1 @@ -334,11 +333,11 @@ class Loop( Picker ): def addBlock( self, id, name ): # match data structure of Block.Loop data = { "name": _(name), - "id": id } + "id": id } self.owner.updateLoopImage( data["id"] ) loop = self.owner.getLoopImage( data["id"] ) - + page = self.owner.noteDB.getPage( id ) win = gtk.gdk.get_default_root_window() @@ -347,14 +346,14 @@ class Loop( Picker ): pixmap = gtk.gdk.Pixmap( win, width, height ) self.gc.set_clip_rectangle( gtk.gdk.Rectangle( 0, 0, width, height ) ) - + # draw bg self.gc.foreground = self.colors["Picker_Bg"] pixmap.draw_rectangle( self.gc, True, 0, 0, width, height ) self.gc.set_clip_mask( self.blockMask ) self.gc.foreground = self.owner.colors["Border_Inactive"] - + #-- draw head ----------------------------------------- # draw border @@ -363,10 +362,10 @@ class Loop( Picker ): # draw block self.gc.set_clip_origin( -Block.Loop.MASK_START, -height ) - pixmap.draw_drawable( self.gc, loop, 0, 0, 0, 0, Block.Loop.HEAD, height ) + pixmap.draw_drawable( self.gc, loop, 0, 0, 0, 0, Block.Loop.HEAD, height ) #-- draw beats ---------------------------------------- - + beats = page.beats - 1 # last beat is drawn with the tail curx = Block.Loop.HEAD while beats > 3: @@ -376,13 +375,13 @@ class Loop( Picker ): # draw block self.gc.set_clip_origin( curx-Block.Loop.MASK_BEAT, -height ) - pixmap.draw_drawable( self.gc, loop, curx, 0, curx, 0, Block.Loop.BEAT_MUL3, height ) + pixmap.draw_drawable( self.gc, loop, curx, 0, curx, 0, Block.Loop.BEAT_MUL3, height ) curx += Block.Loop.BEAT_MUL3 beats -= 3 if beats: - w = Block.Loop.BEAT*beats + w = Block.Loop.BEAT*beats # draw border self.gc.set_clip_origin( curx-Block.Loop.MASK_BEAT, 0 ) @@ -390,9 +389,9 @@ class Loop( Picker ): # draw block self.gc.set_clip_origin( curx-Block.Loop.MASK_BEAT, -height ) - pixmap.draw_drawable( self.gc, loop, curx, 0, curx, 0, w, height ) + pixmap.draw_drawable( self.gc, loop, curx, 0, curx, 0, w, height ) - curx += w + curx += w #-- draw tail ----------------------------------------- @@ -402,7 +401,7 @@ class Loop( Picker ): # draw block self.gc.set_clip_origin( curx-Block.Loop.MASK_TAIL, -height ) - pixmap.draw_drawable( self.gc, loop, curx, 0, curx, 0, Block.Loop.TAIL, height ) + pixmap.draw_drawable( self.gc, loop, curx, 0, curx, 0, Block.Loop.TAIL, height ) image = gtk.Image() image.set_from_pixmap( pixmap, None ) @@ -411,8 +410,8 @@ class Loop( Picker ): block.modify_bg( gtk.STATE_NORMAL, self.colors["Picker_Bg"] ) block.add( image ) - Picker.addBlock( self, data, data["name"], block ) - + Picker.addBlock( self, data, data["name"], block ) + def on_button_press( self, widget, event ): walloc = widget.get_allocation() salloc = self.scrolledWindow.get_allocation() @@ -422,10 +421,8 @@ class Loop( Picker ): for key in widget.data.keys(): data[key] = widget.data[key] - newid = self.owner.noteDB.duplicatePages( [ data["id"] ] )[data["id"]] + newid = self.owner.noteDB.duplicatePages( [ data["id"] ] )[data["id"]] self.owner.updateLoopImage( newid ) data["id"] = newid block = self.desktop.addBlock( Block.Loop, data, loc, True ) - - diff --git a/TamTamJam.activity/Jam/RythmGenerator.py b/TamTamJam.activity/Jam/RythmGenerator.py index 537f598..d1ea46e 100644 --- a/TamTamJam.activity/Jam/RythmGenerator.py +++ b/TamTamJam.activity/Jam/RythmGenerator.py @@ -1,10 +1,13 @@ import random +import common.Util.InstrumentDB as InstrumentDB import common.Config as Config from common.Util.CSoundNote import CSoundNote from common.Generation.GenerationConstants import GenerationConstants from GenRythm import GenRythm +instrumentDB = InstrumentDB.getRef() + def generator( instrument, nbeats, density, regularity, reverbSend ): makeRythm = GenRythm() @@ -18,15 +21,15 @@ def generator( instrument, nbeats, density, regularity, reverbSend ): filterCutoff = 1000 tied = False mode = 'mini' - + def makePitchSequence(length, drumPitch): pitchSequence = [] append = pitchSequence.append list = range(length) max = len(drumPitch) - 1 for i in list: - append(drumPitch[ random.randint( 0, max ) ] ) - return pitchSequence + append(drumPitch[ random.randint( 0, max ) ] ) + return pitchSequence def makeGainSequence( onsetList ): gainSequence = [] @@ -36,16 +39,16 @@ def generator( instrument, nbeats, density, regularity, reverbSend ): gain = random.uniform(GenerationConstants.GAIN_MID_MAX_BOUNDARY, GenerationConstants.GAIN_MAX_BOUNDARY) elif ( onset % Config.TICKS_PER_BEAT) == 0: gain = random.uniform(GenerationConstants.GAIN_MID_MIN_BOUNDARY, GenerationConstants.GAIN_MID_MAX_BOUNDARY) - else: + else: gain = random.uniform(GenerationConstants.GAIN_MIN_BOUNDARY, GenerationConstants.GAIN_MID_MIN_BOUNDARY) append(gain) - return gainSequence - + return gainSequence + def pageGenerate( regularity, drumPitch ): barLength = Config.TICKS_PER_BEAT * nbeats #print 'pageGenerate drumPitch[0] ', drumPitch[0] - currentInstrument = Config.INSTRUMENTS[ instrument ].kit[ drumPitch[0] ].name + currentInstrument = instrumentDB.instNamed[ instrument ].kit[ drumPitch[0] ] rythmSequence = makeRythm.drumRythmSequence(currentInstrument, nbeats, density, regularity) pitchSequence = makePitchSequence(len(rythmSequence), drumPitch ) @@ -54,16 +57,16 @@ def generator( instrument, nbeats, density, regularity, reverbSend ): trackNotes = [] list = range(len(rythmSequence)) for i in list: - trackNotes.append( CSoundNote( rythmSequence[i], pitchSequence[i], gainSequence[i], - pan, noteDuration, trackId, - Config.INSTRUMENTS[instrument].instrumentId, attack, + trackNotes.append( CSoundNote( rythmSequence[i], pitchSequence[i], gainSequence[i], + pan, noteDuration, trackId, + instrumentDB.instNamed[instrument].instrumentId, attack, decay, reverbSend, filterType, filterCutoff, tied, mode)) return trackNotes -################################################################################## - # begin generate() +################################################################################## + # begin generate() if regularity > 0.75: - streamOfPitch = GenerationConstants.DRUM_COMPLEXITY1 + streamOfPitch = GenerationConstants.DRUM_COMPLEXITY1 elif regularity > 0.5: streamOfPitch = GenerationConstants.DRUM_COMPLEXITY2 elif regularity > 0.25: -- cgit v0.9.1