Beispiel #1
0
def insert_case(case, commit=False):
    """
    The commit is provided as a hook so that when called from another module,
    this function will have a way to commit the insertions.
    """
    with grant_connection(commit) as dbcon:
        for note in ['signif_note', 'htreb', 'hmid', 'hbass']:
            if case[note] is not None:
                insert_note(case[note], dbcon)
                case[note] = case[note]['id']
            else:
                case[note] = 'null'
        for notes in ['other_notes', 'hother']:
            if case[notes] is not None:
                for note in case[notes]:
                    insert_note(note, dbcon)
                    n = case[notes].index(note)
                    case[notes][n] = note['id']
        for note in ['sig_note_m1', 'sig_note_m2', 'sig_note_p1',
                     'sig_note_p2']:
            if case[note] is not None:
                case[note] = case[note]['id']
            else:
                case[note] = 'null'
        # Include logic for cadence
        query = ("INSERT INTO CASEBASE VALUES ({id}, '{key}', {dos}, "
                 "{cadence}, {signif_note}, '{other_notes}', {sig_note_m1}, "
                 "{sig_note_m2}, {sig_note_p1}, {sig_note_p2}, {htreb}, "
                 "{hmid}, {hbass}, '{hother}')".format(**case.features))
        dbcon.execute(query)
Beispiel #2
0
def next_note_num():
    with grant_connection() as qdb:
        nquery = "SELECT MAX(NID) FROM NOTEBASE"
        note_num = qdb.execute(nquery).fetchone()[0]
        if note_num:
            return int(note_num) + 1
        else:
            return 1
Beispiel #3
0
def next_case_num():
    with grant_connection() as qdb:
        nquery = "SELECT MAX(ID) FROM CASEBASE"
        case_num = qdb.execute(nquery).fetchone()[0]
        if case_num:
            return int(case_num) + 1
        else:
            return 1
Beispiel #4
0
 def get_key(self):
     """
     This is lousy because now this module has to connect to the database
     to get the value of the key. But due to lack of foresight the other
     modules have no room for a quick fix and this has to do for now.
     """
     with grant_connection() as qdb:
         nquery = "SELECT Key FROM METATABLE " + \
                  "WHERE Table_Name = '" + self.name + "'"
         key = qdb.execute(nquery).fetchone()[0]
     return key.lower()
Beispiel #5
0
def build_cases(query):
    with grant_connection() as dbconnect:
        for row in dbconnect.get_case(query):
            case = Case(*row)
            for note in ['signif_note', 'sig_note_m1', 'sig_note_m2',
                         'sig_note_p1', 'sig_note_p2', 'htreb', 'hmid',
                         'hbass']:
                if case[note] is not None:
                    nquery = "SELECT * FROM NOTEBASE WHERE NID = " + \
                             str(case[note])
                    case[note] = Note(*dbconnect.get_note(nquery).fetchone())
            for notes in ['other_notes', 'hother']:
                if case[notes] != 'None':
                    notelist = eval(case[notes])
                    newlist = []
                    for n in notelist:
                        query = "SELECT * FROM NOTEBASE WHERE NID = " + str(n)
                        newlist.append(Note(*dbconnect.get_note(query).
                                            fetchone()))
                    case[notes] = newlist
            yield case
Beispiel #6
0
def build_case(piece):
    """
    bpm is beat per measure
    btdrn is beat duration
    carry will be used if a note's duration is longer than the beat duartion
    these values are not being considered for now
    """
    query = "SELECT * FROM " + piece
    with grant_connection() as qdb:
        nquery = "SELECT Key, Time_Sig, Start FROM METATABLE " +\
                 "WHERE Table_Name = '" + piece + "'"
        metadata = qdb.execute(nquery).fetchone()
        scale = Scale(metadata[0])
        pos_meas = int(metadata[2]) - 1
        #bpm = int(metadata[1].split('/')[0])
        #btdrn = int(metadata[1].split('/')[1])
        btdrn = 4
    carry = []
    case_num = next_case_num()
    note_num = next_note_num()
    for row in get_next_beat(query):
        case = [case_num, 'M', 1.0, 0]
        sig_note_flag = False
        other_notes = []
        hother = []
        if row[1] is not None:
            pos_beat = 1
            for ntgrp in row[1].split(','):
                match = re.search(r'(\w+)(\d)', ntgrp)
                note = match.group(1)
                duration = int(match.group(2))
                # following code is not being used for now
                if btdrn / duration > 1:
                    carry.append((note, 1 / (1 / duration - 1 / btdrn)))
                    duration = btdrn
                # --------------------
                try:
                    degree = scale.diatonic[note]
                except KeyError:
                    print('Warning : Case :' + str(case_num) + ' '
                          + note + ' is not in the diatonic scale')
                    degree = 0
                if not sig_note_flag:
                    case.append(Note(note_num, scale.chromatic[note],
                                degree, duration, pos_beat,
                                pos_meas + 1))
                    sig_note_flag = True
                else:
                    other_notes.append(Note(note_num,
                                            scale.chromatic[note],
                                            degree, duration, 2,
                                            pos_meas + 1))
                note_num += 1
            if len(other_notes) == 0:
                case.append(None)
            else:
                case.append(other_notes)
        else:
            case.extend([None, None])
        if row[2] is not None:
            #pdb.set_trace()
            harmony = {'t': None, 'm': None, 'b': None}
            pos_beat = 1
            next_note = 't'
            # 't' : trebble, 'm' : mid, 'b' : bass
            switch_other = False
            for ntgrp in row[2].split(','):
                match = re.search(r'(\w+)(\d)', ntgrp)
                note = match.group(1)
                duration = int(match.group(2))
                try:
                    degree = scale.diatonic[note]
                except KeyError:
                    print('Warning : Case : ' + str(case_num) + '. '
                          + note + ' is not in the diatonic scale')
                    degree = 0
                if switch_other:
                    hother.append(Note(note_num, scale.chromatic[note],
                                       degree, duration, 2, pos_meas + 1))
                    switch_other = False
                    if next_note == 't':
                        next_note = 'm'
                    else:
                        next_note = 'b'
                else:
                    harmony[next_note] = Note(note_num,
                                              scale.chromatic[note],
                                              degree, duration, pos_beat,
                                              pos_meas + 1)
                    if duration == btdrn and next_note == 't':
                        next_note = 'm'
                    elif duration == btdrn and next_note == 'm':
                        next_note = 'b'
                    else:
                        switch_other = True
                note_num += 1
            case.extend([harmony['t'], harmony['m'], harmony['b']])
            if len(hother) == 0:
                case.append(None)
            else:
                case.append(hother)
        else:
            case.extend([None, None, None, None])
        pos_meas = (pos_meas + 1) % btdrn
        case_num += 1
        yield case
Beispiel #7
0
def get_next_beat(query):
    with grant_connection() as dbconnect:
        for row in dbconnect.get_case(query):
            yield row