Beispiel #1
0
class MusicMakerApp(QWidget):
    TEMPLATEWIDGETFACTORIES = {
        "circle": lambda: PlayWidget("samples/clap.wav", "samples/cymbal.wav",
                                     lambda args: args[0].drawEllipse(*args[1:])),
        "rectangle": lambda: PlayWidget("samples/kick.wav", "samples/rs.wav", lambda args: args[0].drawRect(*args[1:])),
        "caret": lambda: PlayWidget("samples/hh.wav", "samples/ohh.wav", lambda args: DrawHelper.drawTriangle(*args)),
        "zig-zag": lambda: PlayWidget("samples/sd1.wav", "samples/sd2.wav", lambda args: DrawHelper.drawZig(*args)),
        "left_square_bracket": lambda: PlayWidget("samples/cb.wav", "samples/hc.wav",
                                                  lambda args: DrawHelper.drawBracket(*args)),
    }

    def __init__(self):
        super(MusicMakerApp, self).__init__()
        self.setMinimumHeight(500)
        self.setMinimumWidth(800)

        self.markerHelper = IrMarkerEventFilter(self)
        self.installEventFilter(self.markerHelper)

        self.recognizer = Recognizer()
        self.recognizer.addTemplate(template.Template(*template.circle))
        self.recognizer.addTemplate(template.Template(*template.delete))
        self.recognizer.addTemplate(template.Template(*template.rectangle))
        self.recognizer.addTemplate(template.Template(*template.caret))
        self.recognizer.addTemplate(template.Template(*template.zig_zag))
        self.recognizer.addTemplate(template.Template(*template.left_square_bracket))

        self.recognizeThread = RecognizeThread()
        self.recognizeThread.finished.connect(self.recognized)
        self.recognizeThread.start()

        self.head = Playhead(self, self.playheadMoved)

    def setPointerDrawFilter(self, filter):
        self.pointerDrawFilter = filter
        self.pointerDrawFilter.setCompleteCallback(self.onPointerDrawComplete)

    def playheadMoved(self, xpos, stepping):
        cs = self.children()
        lower = xpos - stepping
        p = cs[0].pos().x()
        for c in cs:
            c = c  # type: QWidget
            r = c.geometry()  # type: QRect
            if c.isVisible() and (lower < r.x() < xpos) or (lower < r.right() < xpos):
                if hasattr(c, "play"):
                    c.play()

    def adjustSize(self):
        QWidget.adjustSize(self)
        self.head.adjustSize()

    def recognized(self, context):
        print("recognized")

        recognized = context.res
        if not recognized:
            return

        pointer = context.pointer
        points = context.points

        template = recognized[0]  # type: template.Template
        if (template):
            if (recognized[1] > 0.5):
                print(template.name + " recognized: " + str(recognized[1]))
                command = self.resolveCommand(template.name, points)
                if (command):
                    pointer.undoStack().push(command)
            else:
                # TODO output some status
                pass

    def onPointerDrawComplete(self, pointer, points):
        if (len(points) <= 2):
            return

        points = list(map(lambda p: (p.x(), p.y()), points))
        self.recognizeThread.recognize(RecognizeContext(self.recognizer, points, pointer))

    def paintEvent(self, ev):
        QWidget.paintEvent(self, ev)

        qp = QPainter()
        qp.begin(self)

        qp.setBrush(Qt.black)
        qp.drawRect(self.rect())

        self.drawStepping(qp, self.head.stepping)
        if self.markerHelper.markerMode:
            self.markerHelper.drawMarkers(qp)
        self.pointerDrawFilter.drawPoints(qp)

        qp.end()

    def drawStepping(self, qp, stepping):
        pos = 0
        qp.setBrush(Qt.yellow)
        pen = QPen()
        pen.setColor(Qt.darkGray)
        qp.setPen(pen)
        while pos < self.width():
            pos += stepping
            qp.drawLine(pos, 0, pos, self.height())

    def resolveCommand(self, templateName, points):

        if templateName == "delete":
            x, y = np.mean(points, 0)
            widget = self.childAt(x, y)
            if widget and not widget is self:
                return DeleteCommand(self, widget)

        widgetFactory = MusicMakerApp.TEMPLATEWIDGETFACTORIES.get(templateName, None)

        if (not widgetFactory):
            return None

        widget = widgetFactory()

        self.setupChildWidget(widget, points)
        return AddCommand(self, widget)

    def setupChildWidget(self, widget, points):
        widget.setFixedWidth(100)
        widget.setFixedHeight(100)

        x, y = np.mean(points, 0)
        x = x - widget.width() * 0.5
        y = y - widget.height() * 0.5
        widget.move(x, y)
        widget.setParent(self)
import wx
from recognizer import Recognizer
from template import *
import tuio
tracking = tuio.Tracking()

# Should run the frame as a seperate thread
# http://stackoverflow.com/questions/12786471/invoking-a-wxpython-evt-button-event-programmatically
print "Initializing recognizer"
recognizer = Recognizer()
for template in templates:
	recognizer.addTemplate(template)
print "Initilized recognizer!"


class MyApp(wx.App):
	def OnInit(self):
		self.frame = MyFrame(None, "Sample")  # add two lines here
		self.frame.Centre()
		self.frame.Show(True)
		return True


class MyFrame(wx.Frame):
	def __init__(self, parent, title):
		wx.Frame.__init__(self, parent, title=title)
		wx.StaticText(self, label='Detected shape:', pos=(10, 10))
		wx.StaticText(self, label='Detected score:', pos=(10, 30))
		self.detected_shape = wx.StaticText(self, label='', pos=(95, 10))
		self.detected_score = wx.StaticText(self, label='', pos=(93, 30))
		self.previous_points = []