#!/usr/bin/env python2

from mididings import *
from mididings.extra.osc import *
from liblo import send, Address
from datetime import datetime
from os import environ

monohorn_url = 'osc.unix://localhost/tmp/monohorn.socket'

jack_capture_start = System('jack_capture --format ogg --osc 9898 -fp '+environ['RECORDINGS'])
jack_capture_stop = SendOSC(Address(9898), '/jack_capture/stop')

def mh_start(e):
    for y in range(8):
        send(monohorn_url, '/set', 0, y, 0x1a2000)
    send(monohorn_url, '/render')

def mh_stop(e):
    send(monohorn_url, '/clear')

print('Loopidings')

config(
    client_name = 'loopidings',
    in_ports = [
        ('in', environ['MIDI_DEV']+':0')
    ]
)

run(
    scenes = {
        1: SceneGroup("jack_capture", [
              Scene("stop", Pass(), [jack_capture_stop, Call(mh_stop)]),
              Scene("start", Pass(), [jack_capture_start, Call(mh_start)]),
           ])
    },
    control = Filter(NOTEON) >> KeyFilter(notes=[5]) % SubSceneSwitch(offset=1, wrap=True)
)
