def load(index): global scene, image filename = filenames[index] t = "[%3i+1/%i] %s" % (index, len(filenames), filename) glutSetWindowTitle(t.encode()) print(t) try: if filename.endswith('z'): import gzip f = gzip.open(filename) else: f = open(filename) svg, elems = parse(f.read(), logging.WARNING) except: traceback.print_exception(*sys.exc_info()) svg = sg.Group() image = sg.Image("../png/%s.png" % filename.rsplit(".", 1)[0], x=480) scene = sg.Group( children=[svg, image], ) (x_min, y_min), (x_max, y_max) = scene.aabbox() glutReshapeWindow(int(x_max), int(y_max)) glutPostRedisplay() return index
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)], )
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)
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)], ] ])
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") ps.print_stats()
self.keys_pressed[num_key] |= (1<<channel) if self.keys_pressed[num_key]: piano.octaves[num_octave].press(MusicKeybOctave.NOTES[num_key % 12], channel) else: self.keys_pressed[num_key] &= ~(1<<channel) if not self.keys_pressed[num_key]: piano.octaves[num_octave].release(MusicKeybOctave.NOTES[num_key % 12]) def show(self, active=True): self.model_root.active = active piano = MusicKeyboard() fifths = CircleOfFifths(piano.width + margin) hexagonal = HexLayout(0, piano.height + margin) triads_circle = CircleOfTriads(hexagonal.width + margin, piano.height + margin) chords = Chords(hexagonal.width + margin + triads_circle.width + margin, piano.height + margin) scene = sg.Group([piano.root(), fifths.root(), hexagonal.root(), triads_circle.root(), chords.root()]) window_size = int(piano.width + margin + fifths.width + 2 * margin), int(piano.height + margin + hexagonal.height + 2 * margin) feedback = sg.Group(fill=None, stroke=sg.Color.red) #midi_player = RandomSoundPlayer([piano, fifths, hexagonal]) #midi_thread = Thread(target = midi_player.random_play, args = (8, 10, 0.3)) #midi_thread.start() midi_filename = args.midi #midi_filename = 'Bach_Fugue_BWV578.mid' #midi_filename = 'Debussy_Arabesque_No1.mid' if not midi_filename is None: midi_file_player = MidiFileSoundPlayer(os.path.join(this_dir, midi_filename), [piano, fifths, hexagonal, triads_circle, chords]) midi_thread = Thread(target = midi_file_player.play)
] ]) 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)], ) n = 6 scene = sg.Group(children=[koch(n)]) # glut callbacks ############################################################# def keyboard(c, x, y): if c == b'q': sys.exit(0) elif c in b'+-': global n if c == b'+': n += 1 else: n -= 1 scene.children = [koch(n)] elif c == b's':