Ejemplo n.º 1
0
def move(x1, y1):
	global x0, y0
	
	feedback.children = []
	if state == IDLE:
		for path, point in scene.pick(x1, y1):
			transform = product(*(elem.matrix() for elem in path[:-1]))
			(x_min, y_min), (x_max, y_max) = path[-1].aabbox(transform)
			feedback.children.append(sg.Rectangle(x=x_min, y=y_min,
			                                      width=x_max-x_min, height=y_max-y_min))
			x, y = point
			feedback.children.append(sg.Rectangle(
				x=x-3, y=y-3, width=5, height=5,
				transform = sum((elem.transform + [sg.Translate(elem.x, elem.y)] for elem in path), [])
			))
	
	elif state == DRAGGING:
		scene.transform = [sg.Translate(x1-x0, y1-y0)] + scene.transform
	
	elif state == ROTATING:
		scene.transform = [sg.Rotate((x1-x0)-(y1-y0), *origin)] + scene.transform
	
	elif state == ZOOMING:
		ds = exp(((x1-x0)-(y1-y0))*.01)
		x, y = origin
		scene.transform = [sg.Translate((1-ds)*x, (1-ds)*y), sg.Scale(ds)] + scene.transform
	
	else:
		raise RuntimeError("Unexpected interaction state '%s'" % state)
	
	scene.transform = normalized(scene.transform)
	x0, y0 = x1, y1
	post_redisplay()
Ejemplo n.º 2
0
    def __init__(self, x_pos=0, y_pos=0):
        super().__init__(self.MEM_T, self.MEM_K, self.MEM_THRESHOLD)

        with open(os.path.join(this_dir, "CircleOfFifths.svg")) as f:
            svg = f.read()
        self.model_root, self.model_elements = parse(svg)
        #sys.stdout.write(serialize(self.model_root))
        #sys.stdout.write("elements = %s\n" % (self.model_elements,))
        #json.dump(self.model_root, sys.stdout, cls=JSONDebugEncoder, indent=2, sort_keys=True)
        #json.dump(self.model_elements, sys.stdout, cls=JSONDebugEncoder, indent=2, sort_keys=True)
        #sys.stdout.write("\n") # Python JSON dump misses last newline
        (x_min, y_min), (x_max, y_max) = self.model_root.aabbox()
        self.width = x_max - x_min
        self.height = y_max - y_min
        self.model_root = sg.Use(
            self.model_root,
            transform=[sg.Translate(margin - x_min + x_pos, margin - y_min + y_pos)]
        )

        self.orig_fill_color = {}
        for note in self.NOTES:
            label = 'inner_' + note
            self.orig_fill_color[label] = self.model_elements[label].fill
            label = 'outer_' + note
            self.orig_fill_color[label] = self.model_elements[label].fill
Ejemplo n.º 3
0
    def __init__(self, x_pos=0, y_pos=0):
        with open(os.path.join(this_dir, "HexLayout.svg")) as f:
            svg = f.read()
        self.model_root, self.model_elements = parse(svg)
        (x_min, y_min), (x_max, y_max) = self.model_root.aabbox()
        self.width = x_max - x_min
        self.height = y_max - y_min
        self.model_root = sg.Use(
            self.model_root,
            transform=[sg.Translate(margin - x_min + x_pos, margin - y_min + y_pos)]
        )

        self.press_counter = [0] * 12
        self.accum_time = [0.] * 12
        self.last_timestamp = time.time_ns() / (10 ** 9) # Converted to floating-point seconds

        #self.note_map  = ['cC',  'cG',  'dD',  'dA',  'dE',  'dB', 'aGb', 'bDb', 'bAb', 'bEb', 'bBb', 'cF' ]
        #self.note_map  = ['cGb', 'cDb', 'dAb', 'dEb', 'dBb', 'dF', 'aC',  'bG',  'bD',  'bA',  'bE',  'cB' ]

        #self.note_map   = ['cEb', 'cBb', 'cF',  'dC',  'dG',  'dD', 'dA',  'bE_',  'bB',  'bGb', 'cDb_', 'cAb_']
        #self.note_map   = ['cEb', 'cBb', 'dF_',  'dC',  'dG',  'dD', 'aA',  'bE_',  'bB',  'bGb', 'bDb', 'cAb_']

        #self.note_map  = ['cGb', 'cDb', ['cAb', 'dAb'], 'dEb', ['aBb', 'dBb'], ['aF', 'dF', 'eF'] , ['aC', 'eC'],
        #                 ['aG', 'bG', 'eG'], ['bD', 'eD'],  'bA', ['bE', 'cE'], ['bB_', 'cB'] ]

        #self.note_map  = ['cC',  'cG',  'dD',  'dA',  'dE',  'dB',  'aGb', 'bDb', 'bAb', 'bEb', 'bBb', 'cF' ]
        #self.note_map  = ['cG',  'cD',  'dA',  'dE',  'dB',  'dGb', 'aDb', 'bAb', 'bEb', 'bBb', 'bF',  'cC' ]

        self.note_map  = [\
            [row + 'G'  for row in ['a', 'b', 'c', 'd', 'e']],
            [row + 'D'  for row in ['a', 'b', 'c', 'd', 'e']],
            [row + 'A'  for row in ['a', 'b', 'c', 'd', 'e']],
            [row + 'E'  for row in ['a', 'b', 'c', 'd', 'e']],
            [row + 'B'  for row in ['a', 'b', 'c', 'd', 'e']],
            [row + 'Gb' for row in ['a', 'b', 'c', 'd', 'e']],
            [row + 'Db' for row in ['a', 'b', 'c', 'd', 'e']],
            [row + 'Ab' for row in ['a', 'b', 'c', 'd', 'e']],
            [row + 'Eb' for row in ['a', 'b', 'c', 'd', 'e']],
            [row + 'Bb' for row in ['a', 'b', 'c', 'd', 'e']],
            [row + 'F'  for row in ['a', 'b', 'c', 'd', 'e']],
            [row + 'C'  for row in ['a', 'b', 'c', 'd', 'e']],
        ]
        for note_ids in self.note_map:
            if not isinstance(note_ids, (list, tuple)):
                note_ids = [note_ids]
            new_note_ids = [] 
            for note_id in note_ids:
                new_note_id = note_id + '_'
                if new_note_id in self.model_elements:
                    new_note_ids.append(new_note_id)
            note_ids += new_note_ids

        for note_ids in self.note_map:
            if not isinstance(note_ids, (list, tuple)):
                note_ids = [note_ids]
            for note_id in note_ids:
                self.model_elements[note_id].fill = self.IDLE_COLOR
                self.model_elements[note_id].stroke = self.IDLE_STROKE
                self.model_elements[note_id].stroke_width = 1
Ejemplo n.º 4
0
def koch(n, k0=sg.Line(x2=100)):
    return sg.Group(
        transform=[sg.Scale(10), sg.Translate(10, 40)],
        stroke=sg.Color.black,
        stroke_width=.05 * 3**n,
        stroke_linecap="round",
        children=[_koch(n, k0)],
    )
Ejemplo n.º 5
0
def _koch(n, k0):
    if n <= 1:
        return k0
    kp = _koch(n - 1, k0)
    return sg.Group(children=[
        sg.Use(kp, transform=t) for t in [
            [sg.Scale(1 / 3)],
            [sg.Scale(1 /
                      3), sg.Translate(100),
             sg.Rotate(-60)],
            [
                sg.Scale(1 / 3),
                sg.Translate(200),
                sg.Rotate(-120),
                sg.Scale(1, -1)
            ],
            [sg.Scale(1 / 3), sg.Translate(200)],
        ]
    ])
Ejemplo n.º 6
0
 def __init__(self, num_octaves=10):
     self.octaves = [MusicKeybOctave() for i in range(0, num_octaves)]
     self.elements = []
     self.width = 0
     self.height = 0
     self.keys_pressed = [0] * (12 * num_octaves)
     for octave in self.octaves:
         (x_min, y_min), (x_max, y_max) = octave.root().aabbox()
         element = sg.Use(
             octave.root(),
             transform=[sg.Translate(margin - x_min + self.width, margin - y_min)]
         )
         self.elements.append(element)
         self.width += x_max - x_min - 1
         self.height = y_max - y_min - 1
     self.model_root = sg.Group(self.elements)
Ejemplo n.º 7
0
    def __init__(self, x_pos=0, y_pos=0):
        super().__init__(self.MEM_T, self.MEM_K, self.MEM_THRESHOLD)

        for chord_info in self.CHORDS_INFO:
            if not chord_info[0]:
                chord_info[0] = [0] * 12
                for i in range(0, 12):
                    chord_mask = 0
                    for num_note in chord_info[1]:
                        chord_mask |= 1 << (i + num_note) % 12
                    chord_info[0][i] = chord_mask
            if chord_info[2] is None:
                chord_info[2] = self.COLOR_GRAY
        #json.dump(self.CHORDS_INFO, sys.stdout, cls=JSONDebugEncoder, indent=2, sort_keys=True)

        with open(os.path.join(this_dir, "ChordMatrix.svg")) as f:
            svg = f.read()
        self.model_root, self.model_elements = parse(svg)
        #sys.stdout.write(serialize(self.model_root))
        #sys.stdout.write("elements = %s\n" % (self.model_elements,))
        #json.dump(self.model_root, sys.stdout, cls=JSONDebugEncoder, indent=2, sort_keys=True)
        #json.dump(self.model_elements, sys.stdout, cls=JSONDebugEncoder, indent=2, sort_keys=True)
        #sys.stdout.write("\n") # Python JSON dump misses last newline
        (x_min, y_min), (x_max, y_max) = self.model_root.aabbox()
        self.width = x_max - x_min
        self.height = y_max - y_min
        self.model_root = sg.Use(
            self.model_root,
            transform=[sg.Translate(margin - x_min + x_pos, margin - y_min + y_pos)]
        )

        self.orig_fill_color = {}
        for note_id in self.CHROMATIC_NOTES:
            key_label = '{}'.format(note_id)
            self.orig_fill_color[key_label] = self.model_elements[key_label].fill
        for row in range(1,12):
            row_label = 'row{:02d}'.format(row)
            self.model_elements[row_label].active = False
            for note_id in self.CHROMATIC_NOTES:
                label = '{}{:02d}'.format(note_id, row)
                self.model_elements[label].active = False
Ejemplo n.º 8
0
    def __init__(self, x_pos=0, y_pos=0):
        super().__init__(self.MEM_T, self.MEM_K, self.MEM_THRESHOLD)

        with open(os.path.join(this_dir, "CircleOfTriads.svg")) as f:
            svg = f.read()
        self.model_root, self.model_elements = parse(svg)
        (x_min, y_min), (x_max, y_max) = self.model_root.aabbox()
        self.width = x_max - x_min
        self.height = y_max - y_min
        self.model_root = sg.Use(
            self.model_root,
            transform=[sg.Translate(margin - x_min + x_pos, margin - y_min + y_pos)]
        )

        for note in self.NOTES:
            self.model_elements[note].stroke = self.COLOR_GRAY
            self.model_elements[note].stroke_width = 1
            self.model_elements[note].active = False
            for circle in self.CIRCLES:
                element_id = '{}{}'.format(circle, note)
                self.model_elements[element_id].fill = self.COLOR_WHITE
                self.model_elements[element_id].stroke = self.COLOR_GRAY
                self.model_elements[element_id].active = False
Ejemplo n.º 9
0
    import OpenGL
    OpenGL.ERROR_CHECKING = False
    OpenGL.ERROR_LOGGING = False
    OpenGL.ERROR_ON_COPY = True
    OpenGL.STORE_POINTERS = False

with open(os.path.join(this_dir, "rgb_lights.svg")) as f:
    svg = f.read()
    svg, elements = parse(svg)
    #sys.stdout.write(serialize(svg))
    #sys.stdout.write("elements = %s\n" % (elements,))

(x_min, y_min), (x_max, y_max) = svg.aabbox()
window_size = int(x_max - x_min + 2 * margin), int(y_max - y_min + 2 * margin)

scene = sg.Use(svg, transform=[sg.Translate(margin - x_min, margin - y_min)])
feedback = sg.Group(fill=None, stroke=sg.Color.red)

elements['red'].active = False
elements['green'].active = True
elements['blue'].active = False


def profiling(f):
    """a profiling decorator"""
    import cProfile, pstats, atexit
    pr = cProfile.Profile()

    @atexit.register
    def report():
        ps = pstats.Stats(pr).sort_stats("tottime")