Exemplo n.º 1
0
 def single_file(filename, x):
     controller.reset()
     controller.filesNew(filename, st)
 
     finger_midi = 0.0
     controller.comment("attack inst %i st %i dyn %i finger_midi %.3f"
             % (inst_type, st, dyn, finger_midi))
 
     begin = vivi_controller.NoteBeginning()
     begin.physical.string_number = st
     begin.physical.bow_force = fbi
     begin.physical.dynamic = prev_dyn
     begin.physical.bow_bridge_distance = dynamics.get_distance(
         inst_type, prev_dyn)
     begin.physical.bow_velocity = dynamics.get_velocity(
         inst_type, prev_dyn)
     end = vivi_controller.NoteEnding()
 
     for i in range(REPS):
         controller.reset(True)
         finger_midi = prev_fmi
         begin.physical.finger_position = utils.midi2pos(finger_midi)
         controller.note(begin, NOTE_LENGTH, end)
         #controller.rest(0.1)
         begin.physical.dynamic = dyn
         begin.physical.bow_bridge_distance = dynamics.get_distance(
             inst_type, dyn)
         begin.physical.bow_velocity = dynamics.get_velocity(
             inst_type, dyn)
         finger_midi = 0.0
         begin.physical.bow_force = x
         begin.physical.finger_position = utils.midi2pos(finger_midi)
         begin.physical.bow_velocity *= -1
         controller.note(begin, NOTE_LENGTH, end)
     controller.filesClose()
Exemplo n.º 2
0
    def single_file(filename, x):
        controller.reset()
        controller.filesNew(filename, st)

        finger_midi = 0.0
        controller.comment("attack inst %i st %i dyn %i finger_midi %.3f" %
                           (inst_type, st, dyn, finger_midi))

        begin = vivi_controller.NoteBeginning()
        begin.physical.string_number = st
        begin.physical.bow_force = fbi
        begin.physical.dynamic = prev_dyn
        begin.physical.bow_bridge_distance = dynamics.get_distance(
            inst_type, prev_dyn)
        begin.physical.bow_velocity = dynamics.get_velocity(
            inst_type, prev_dyn)
        end = vivi_controller.NoteEnding()

        for i in range(REPS):
            controller.reset(True)
            finger_midi = prev_fmi
            begin.physical.finger_position = utils.midi2pos(finger_midi)
            controller.note(begin, NOTE_LENGTH, end)
            #controller.rest(0.1)
            begin.physical.dynamic = dyn
            begin.physical.bow_bridge_distance = dynamics.get_distance(
                inst_type, dyn)
            begin.physical.bow_velocity = dynamics.get_velocity(inst_type, dyn)
            finger_midi = 0.0
            begin.physical.bow_force = x
            begin.physical.finger_position = utils.midi2pos(finger_midi)
            begin.physical.bow_velocity *= -1
            controller.note(begin, NOTE_LENGTH, end)
        controller.filesClose()
Exemplo n.º 3
0
        def single_file(bow_force, count, K):
            finger_midi = basic_training.FINGER_MIDIS[self.fmi]
            # FIXME: oh god ick
            ap = vivi_types.AudioParams( self.st,
                finger_midi,
                dynamics.get_distance(self.inst_type,
                    self.dyn),
                bow_force,
                dynamics.get_velocity(self.inst_type,
                    self.dyn))
            attack_filename = self.files.make_attack_filename(
                self.taskname+'-%.2f' % K,
                ap, count)
            #print attack_filename

            self.controller.reset()
            self.controller.filesNew(attack_filename, self.st)

            self.controller.comment("attack inst %i st %i dyn %i finger_midi %.3f"
                    % (self.inst_type, self.st, self.dyn, finger_midi))

            begin = vivi_controller.NoteBeginning()
            begin.physical.string_number = self.st
            begin.physical.dynamic = self.dyn
            begin.physical.finger_position = utils.midi2pos(finger_midi)
            begin.physical.bow_force = bow_force
            begin.physical.bow_bridge_distance = dynamics.get_distance(
                self.inst_type, self.dyn)
            begin.physical.bow_velocity = dynamics.get_velocity(
                self.inst_type, self.dyn)
            orig_velocity = begin.physical.bow_velocity
            end = vivi_controller.NoteEnding()
            #if finger_midi != 0:
            #    self.process_step.emit()
            #    return
            #print "------"

            for i, bow_direction in enumerate([1, -1]*RAPS):
                self.controller.reset(True)
                #print i, bow_direction
                begin.physical.bow_velocity = orig_velocity * bow_direction
                self.controller.note(begin, ATTACK_LENGTH, end)
                #if i % 4 < 2:
                #    self.controller.note(begin, ATTACK_LENGTH, end)
                #else:
                #    self.controller.note(begin, SHORT_ATTACK_LENGTH, end)
                #if i % 4 == 3:
                #if i % 2 == 1:
                #if True:
                    #print "reset"
                #    self.controller.reset(True)
            self.controller.filesClose()

            self.kept_files.append(attack_filename)
            self.process_step.emit()
Exemplo n.º 4
0
        def make_file_force(bow_force, fmi, count):
            self.controller.set_stable_K(self.st, self.dyn, fmi, K)
            self.controller.set_stable_K_main(self.st, self.dyn, fmi, K_main)
            finger_midi = basic_training.FINGER_MIDIS[fmi]
            # FIXME: oh god ick
            ap = vivi_types.AudioParams( self.st,
                    finger_midi,
                    dynamics.get_distance(self.inst_type,
                        self.dyn),
                    bow_force,
                    dynamics.get_velocity(self.inst_type,
                        self.dyn))
            attack_filename = self.files.make_attack_filename(
                    self.taskname,
                    ap, count)
            #print attack_filename

            self.controller.reset()
            self.controller.filesNew(attack_filename, self.st)

            self.controller.comment("verify inst %i st %i dyn %i finger_midi %.3f"
                        % (self.inst_type, self.st, self.dyn, finger_midi))

            begin = vivi_controller.NoteBeginning()
            begin.physical.string_number = self.st
            begin.physical.dynamic = self.dyn
            begin.physical.finger_position = utils.midi2pos(finger_midi)
            begin.physical.bow_force = bow_force
            begin.physical.bow_bridge_distance = dynamics.get_distance(
                    self.inst_type, self.dyn)
            begin.physical.bow_velocity = dynamics.get_velocity(
                    self.inst_type, self.dyn)

            end = vivi_controller.NoteEnding()
            #end.keep_bow_velocity = True

            for bow_direction in [1, -1, 1, -1]:
                self.controller.reset(True)
                begin.physical.bow_velocity *= bow_direction
                #begin.physical.bow_force = bow_force
                #begin.keep_ears = False
                #begin.keep_bow_force = False
                #self.controller.note(begin, ATTACK_LENGTH, end)

                #begin.physical.bow_force = bow_force*0.8
                #begin.keep_ears = True
                #begin.keep_bow_force = True
                #self.controller.note(begin, STABLE_LENGTH-ATTACK_LENGTH, end)
                self.controller.note(begin, STABLE_LENGTH, end)
            self.controller.filesClose()
            #self.process_step.emit()
            return attack_filename
Exemplo n.º 5
0
        def make_file_force(bow_force, fmi, count):
            self.controller.set_stable_K(self.st, self.dyn, fmi, K)
            self.controller.set_stable_K_main(self.st, self.dyn, fmi, K_main)
            finger_midi = basic_training.FINGER_MIDIS[fmi]
            # FIXME: oh god ick
            ap = vivi_types.AudioParams(
                self.st, finger_midi,
                dynamics.get_distance(self.inst_type, self.dyn), bow_force,
                dynamics.get_velocity(self.inst_type, self.dyn))
            attack_filename = self.files.make_attack_filename(
                self.taskname, ap, count)
            #print attack_filename

            self.controller.reset()
            self.controller.filesNew(attack_filename, self.st)

            self.controller.comment(
                "verify inst %i st %i dyn %i finger_midi %.3f" %
                (self.inst_type, self.st, self.dyn, finger_midi))

            begin = vivi_controller.NoteBeginning()
            begin.physical.string_number = self.st
            begin.physical.dynamic = self.dyn
            begin.physical.finger_position = utils.midi2pos(finger_midi)
            begin.physical.bow_force = bow_force
            begin.physical.bow_bridge_distance = dynamics.get_distance(
                self.inst_type, self.dyn)
            begin.physical.bow_velocity = dynamics.get_velocity(
                self.inst_type, self.dyn)

            end = vivi_controller.NoteEnding()
            #end.keep_bow_velocity = True

            for bow_direction in [1, -1, 1, -1]:
                self.controller.reset(True)
                begin.physical.bow_velocity *= bow_direction
                #begin.physical.bow_force = bow_force
                #begin.keep_ears = False
                #begin.keep_bow_force = False
                #self.controller.note(begin, ATTACK_LENGTH, end)

                #begin.physical.bow_force = bow_force*0.8
                #begin.keep_ears = True
                #begin.keep_bow_force = True
                #self.controller.note(begin, STABLE_LENGTH-ATTACK_LENGTH, end)
                self.controller.note(begin, STABLE_LENGTH, end)
            self.controller.filesClose()
            #self.process_step.emit()
            return attack_filename
Exemplo n.º 6
0
def _get_matching_fingers(inst_type, dyn, coll, files):
    """ finds all items in coll that match the dynamic.  Splits
        matching items into lists matching FINGER_MIDIS pitches."""
    ### get all items on the right level
    def is_level_match(pair, level_bbd, level_bv):
        """ does a collection pair match the level parameters? """
        params = files.get_audio_params(pair[0])
        return (utils.almost_equals(params.bow_bridge_distance, level_bbd) and
            utils.almost_equals(params.bow_velocity, level_bv))
    # "level" parameters
    bbd = dynamics.get_distance(inst_type, dyn)
    bv  = dynamics.get_velocity(inst_type, dyn)
    match_level = filter(lambda(pair): is_level_match(pair, bbd, bv),
                        coll.coll)

    ### split forces+cats into fingers
    def is_finger_match(pair, finger_midi, files):
        """ does a collection pair match the level parameters? """
        params = files.get_audio_params(pair[0])
        return utils.almost_equals(params.finger_midi, finger_midi)
    # actual splitting
    forces = [[], [], []]
    cats = [[], [], []]
    unknowns = [[], [], []]
    for pair in match_level:
        params = files.get_audio_params(pair[0])
        for i, fm in enumerate(FINGER_MIDIS):
            if is_finger_match(pair, fm, files):
                cat = pair[1]
                if coll.is_cat_valid(cat):
                    cats[i].append(cat)
                    forces[i].append(params.bow_force)
                else:
                    unknowns[i].append(params.bow_force)
    return forces, cats, unknowns
Exemplo n.º 7
0
 def get_extreme_forces(self):
     finger_forces = []
     for fmi, fm in enumerate(basic_training.FINGER_MIDIS):
         cands = numpy.array(map(
             lambda(x): self.files.get_audio_params(x[0]).bow_force,
             (
               self.coll.get_items_cat_finger(  0, fm)
             )))
         median = scipy.median(cands)
         cands = numpy.array(map(
             lambda(x): self.files.get_audio_params(x[0]).bow_force,
             (
               self.coll.get_items_cat_finger( -2, fm)
             + self.coll.get_items_cat_finger( -1, fm)
             + self.coll.get_items_cat_finger(  0, fm)
             + self.coll.get_items_cat_finger(  1, fm)
             + self.coll.get_items_cat_finger(  2, fm)
             )))
         #print cands.min()
         #print median
         #print cands.max()
         forces = [cands.min(), median, cands.max()]
         finger_forces.append( forces )
         continue
         # middle
         middle_forces = self.get_forces_finger(0,fm)
         middle_force = scipy.mean(middle_forces)
         # low
         low_forces = self.coll.get_items_cat_finger_bp(
             -vivi_defines.CATEGORIES_EXTREME, fm,
             dynamics.get_distance(self.inst_type,
                 self.dyn))
         low_forces = map(
             lambda(x): self.files.get_audio_params(x[0]).bow_force,
             low_forces)
         really_low = min(low_forces)
         #forces = self.get_all_cat_forces(
         #    -vivi_defines.CATEGORIES_EXTREME,-1,0)
         #low_force = scipy.stats.gmean( forces + [min(middle_forces)])
         #print low_forces
         low_force = scipy.mean( low_forces + [min(middle_forces)])
         # high
         forces = self.get_forces(vivi_defines.CATEGORIES_EXTREME)
         really_high = max(forces)
         forces = self.get_all_cat_forces(
             1, vivi_defines.CATEGORIES_EXTREME,0)
         high_force = scipy.mean( forces + [min(middle_forces)])
         # all
         mid_low = scipy.mean( [low_force, middle_force] )
         mid_high = scipy.mean( [high_force, middle_force] )
         forces = [really_low, low_force, mid_low,
             middle_force, mid_high, high_force, really_high] 
         #print low_force, middle_force, high_force
         finger_forces.append( forces )
     #print finger_forces
     return finger_forces
Exemplo n.º 8
0
 def keypress_extra(self, c):
     if c == 'b':
         self.dyn += 1
         if self.dyn >= 4:
             self.dyn = 0
         self.params.bow_position = dynamics.get_distance(
             self.instrument_type, self.dyn)
         self.params.velocity = dynamics.get_velocity(
             self.instrument_type, self.dyn)
         self.stdscr.addstr(23, 20, str("dyn: %i  " % self.dyn))
Exemplo n.º 9
0
 def keypress_extra(self, c):
     if c == 'b':
         self.dyn += 1
         if self.dyn >= 4:
             self.dyn = 0
         self.params.bow_position = dynamics.get_distance(
             self.instrument_type, self.dyn)
         self.params.velocity = dynamics.get_velocity(
             self.instrument_type, self.dyn)
         self.stdscr.addstr(23, 20, str("dyn: %i  " % self.dyn))
Exemplo n.º 10
0
    def set_dynamic(self, physical, dynamic):
        physical.dynamic = dynamic
        physical.bow_bridge_distance = dynamics.get_distance(
            self.inst_type, dynamic)
        physical.bow_force = self.get_simple_force(physical.string_number,
                                                   physical.finger_position,
                                                   dynamic)
        physical.bow_velocity = dynamics.get_velocity(self.inst_type, dynamic)

        if physical.bow_force > 0:
            physical.bow_force = utils.norm_bounded(
                physical.bow_force, FORCE_SKILL_BASE * shared.skill)
Exemplo n.º 11
0
def main(stdscr):
    artifastring_interactive.GAUSSIAN_FORCE = 0.01
    artifastring_interactive.GAUSSIAN_VELOCITY = 0.01

    try:
        instrument_type = int(sys.argv[1])
    except:
        instrument_type = 0
    try:
        instrument_number = int(sys.argv[2])
    except:
        instrument_number = 0

    try:
        st = int(sys.argv[3])
    except:
        st = 0
    try:
        dyn = int(sys.argv[4])
    except:
        dyn = 0
    try:
        finger = int(sys.argv[5])
    except:
        finger = 0
    try:
        text_message = " ".join(sys.argv[6:])
    except:
        text_message = ""


    artifastring_init = artifastring_process.ArtifastringInit(
        instrument_type, instrument_number)

    vln = ViviTrainer(artifastring_init, stdscr, "train-data")
    vln.turn_off_current_string()
    vln.params.violin_string = st
    vln.params.finger_position = midi_pos.midi2pos(finger)
    vln.params.bow_position = dynamics.get_distance(instrument_type,dyn)
    vln.params.velocity = dynamics.get_velocity(instrument_type,dyn)
    vln.commands_pipe_master.send( (COMMANDS.BOW, vln.params) )

    vln.dyn = dyn

    if stdscr is not None:
        vln.set_message(text_message)

    time.sleep(0.5)
    vln.main_loop()
    curses.endwin()
    if ACTIONS2CSV:
        vln.actions2csv()
Exemplo n.º 12
0
    def _make_files(self):
        self._setup_controller()
        #print self.test_range
        #print self.keep_bow_velocity

        for damp in self.test_range:
            self.controller.set_dampen(self.st, self.dyn, damp)
            for count in range(1, self.REPS+1):

                begin = vivi_controller.NoteBeginning()
                begin.physical.string_number = self.st
                begin.physical.dynamic = self.dyn
                begin.physical.finger_position = 0.0
                begin.physical.bow_force = self.initial_force

                begin.physical.bow_bridge_distance = dynamics.get_distance(
                    self.inst_type, self.dyn)
                begin.physical.bow_velocity = dynamics.get_velocity(
                    self.inst_type, self.dyn)

                filename = self.files.make_dampen_filename(
                    self.taskname, 
                    vivi_types.AudioParams(self.st,
                        midi_pos.pos2midi(begin.physical.finger_position),
                        dynamics.get_distance(
                            self.inst_type, self.dyn),
                        begin.physical.bow_force,
                        dynamics.get_velocity(
                            self.inst_type, self.dyn)),
                    damp, count)
                end = vivi_controller.NoteEnding()
                end.lighten_bow_force = True
                end.keep_bow_velocity = self.keep_bow_velocity
                self.controller.filesNew(filename)
                self.controller.note(begin, DAMPEN_NOTE_SECONDS, end)
                self.controller.rest(DAMPEN_WAIT_SECONDS)
                self.controller.filesClose()
                self.process_step.emit()
        return
Exemplo n.º 13
0
def main(stdscr):
    artifastring_interactive.GAUSSIAN_FORCE = 0.01
    artifastring_interactive.GAUSSIAN_VELOCITY = 0.01

    try:
        instrument_type = int(sys.argv[1])
    except:
        instrument_type = 0
    try:
        instrument_number = int(sys.argv[2])
    except:
        instrument_number = 0

    try:
        st = int(sys.argv[3])
    except:
        st = 0
    try:
        dyn = int(sys.argv[4])
    except:
        dyn = 0
    try:
        finger = int(sys.argv[5])
    except:
        finger = 0
    try:
        text_message = " ".join(sys.argv[6:])
    except:
        text_message = ""

    artifastring_init = artifastring_process.ArtifastringInit(
        instrument_type, instrument_number)

    vln = ViviTrainer(artifastring_init, stdscr, "train-data")
    vln.turn_off_current_string()
    vln.params.violin_string = st
    vln.params.finger_position = midi_pos.midi2pos(finger)
    vln.params.bow_position = dynamics.get_distance(instrument_type, dyn)
    vln.params.velocity = dynamics.get_velocity(instrument_type, dyn)
    vln.commands_pipe_master.send((COMMANDS.BOW, vln.params))

    vln.dyn = dyn

    if stdscr is not None:
        vln.set_message(text_message)

    time.sleep(0.5)
    vln.main_loop()
    curses.endwin()
    if ACTIONS2CSV:
        vln.actions2csv()
Exemplo n.º 14
0
def main(stdscr):
    try:
        instrument_type = int(sys.argv[1])
    except:
        instrument_type = 0
    try:
        instrument_number = int(sys.argv[2])
    except:
        instrument_number = 0
    try:
        st = int(sys.argv[3])
    except:
        st = 0
    try:
        dyn = int(sys.argv[4])
    except:
        dyn = 0
    try:
        finger = int(sys.argv[5])
    except:
        finger = 0
    try:
        text_message = " ".join(sys.argv[6:])
    except:
        text_message = ""

    p = pyaudio.PyAudio()
    audio_stream = p.open(
        rate = 44100,
        channels = 1,
        format = pyaudio.paInt16,
        output = True,
        frames_per_buffer=HOPSIZE,
    )

    vln = InteractiveViolin(instrument_type, instrument_number, stdscr, audio_stream)
    vln.turn_off_current_string()
    vln.params.violin_string = st
    vln.params.finger_position = midi_pos.midi2pos(finger)
    vln.params.bow_position = dynamics.get_distance(dyn)
    vln.params.velocity = dynamics.get_velocity(dyn)
    vln.params_queue.put(vln.params)

    vln.dyn = dyn

    if stdscr is not None:
        vln.set_message(text_message)
    vln.main_loop()

    audio_stream.close()
    p.terminate()
Exemplo n.º 15
0
    def _make_files(self):
        self._setup_controller()
        #print self.test_range
        #print self.keep_bow_velocity

        for damp in self.test_range:
            self.controller.set_dampen(self.st, self.dyn, damp)
            for count in range(1, self.REPS + 1):

                begin = vivi_controller.NoteBeginning()
                begin.physical.string_number = self.st
                begin.physical.dynamic = self.dyn
                begin.physical.finger_position = 0.0
                begin.physical.bow_force = self.initial_force

                begin.physical.bow_bridge_distance = dynamics.get_distance(
                    self.inst_type, self.dyn)
                begin.physical.bow_velocity = dynamics.get_velocity(
                    self.inst_type, self.dyn)

                filename = self.files.make_dampen_filename(
                    self.taskname,
                    vivi_types.AudioParams(
                        self.st,
                        midi_pos.pos2midi(begin.physical.finger_position),
                        dynamics.get_distance(self.inst_type, self.dyn),
                        begin.physical.bow_force,
                        dynamics.get_velocity(self.inst_type, self.dyn)), damp,
                    count)
                end = vivi_controller.NoteEnding()
                end.lighten_bow_force = True
                end.keep_bow_velocity = self.keep_bow_velocity
                self.controller.filesNew(filename)
                self.controller.note(begin, DAMPEN_NOTE_SECONDS, end)
                self.controller.rest(DAMPEN_WAIT_SECONDS)
                self.controller.filesClose()
                self.process_step.emit()
        return
Exemplo n.º 16
0
    def set_dynamic(self, physical, dynamic):
        physical.dynamic = dynamic
        physical.bow_bridge_distance = dynamics.get_distance(
            self.inst_type, dynamic)
        physical.bow_force = self.get_simple_force(
            physical.string_number,
            physical.finger_position,
            dynamic)
        physical.bow_velocity = dynamics.get_velocity(
            self.inst_type, dynamic)

        if physical.bow_force > 0:
            physical.bow_force = utils.norm_bounded(
                physical.bow_force,
                FORCE_SKILL_BASE*shared.skill)
Exemplo n.º 17
0
def _get_matching_fingers(inst_type, dyn, coll, files):
    """ finds all items in coll that match the dynamic.  Splits
        matching items into lists matching FINGER_MIDIS pitches."""

    ### get all items on the right level
    def is_level_match(pair, level_bbd, level_bv):
        """ does a collection pair match the level parameters? """
        params = files.get_audio_params(pair[0])
        return (utils.almost_equals(params.bow_bridge_distance, level_bbd)
                and utils.almost_equals(params.bow_velocity, level_bv))

    # "level" parameters
    bbd = dynamics.get_distance(inst_type, dyn)
    bv = dynamics.get_velocity(inst_type, dyn)
    match_level = filter(lambda (pair): is_level_match(pair, bbd, bv),
                         coll.coll)

    ### split forces+cats into fingers
    def is_finger_match(pair, finger_midi, files):
        """ does a collection pair match the level parameters? """
        params = files.get_audio_params(pair[0])
        return utils.almost_equals(params.finger_midi, finger_midi)

    # actual splitting
    forces = [[], [], []]
    cats = [[], [], []]
    unknowns = [[], [], []]
    for pair in match_level:
        params = files.get_audio_params(pair[0])
        for i, fm in enumerate(FINGER_MIDIS):
            if is_finger_match(pair, fm, files):
                cat = pair[1]
                if coll.is_cat_valid(cat):
                    cats[i].append(cat)
                    forces[i].append(params.bow_force)
                else:
                    unknowns[i].append(params.bow_force)
    return forces, cats, unknowns
Exemplo n.º 18
0
    def _make_files(self):
        self._setup_controller()

        #Ks = numpy.linspace(1.0, 2.5, STEPS_X)
        # skip K=1.0
        #Ks = numpy.exp(numpy.linspace(
        #    numpy.log(self.LOW_INIT),
        #    numpy.log(self.HIGH_INIT),
        #    STEPS_X+1))[1:]
        Ks = numpy.exp(numpy.linspace(
            numpy.log(self.LOW_INIT),
            numpy.log(self.HIGH_INIT),
            STEPS_Y))

        #f = self.force_init
        #forces = [ 0.25*f, 0.5*f, f, 2*f, 4*f ]
        #forces = [ 0.5*f, f, 2*f ]
        #forces = self._make_forces(self.force_init, num=9)
        #forces = [forces[3], forces[4], forces[5]]
        #forces = numpy.linspace(forces[4], forces[5], num=3)
        #self.forces_initial = forces
        #print forces
        self.forces_init_used = []

        #emits = 0
        for fmi, finger_midi in enumerate(basic_training.FINGER_MIDIS):
            #forces = self._make_forces(self.forces_initial[fmi], 9)
            #forces = numpy.linspace(forces[4], forces[6], num=3,
            #    endpoint=True)
            #forces = [1.2, 1.35, 1.5]
            forces = numpy.linspace(
                self.forces_initial[fmi][1],
                self.forces_initial[fmi][1] + self.forces_initial[fmi][0],
                num=3, endpoint=True)
            #print forces
            self.forces_init_used.extend(forces)
            for K in Ks:
                for bow_force in forces:
                    for count in range(self.REPS):
                        audio_params = vivi_types.AudioParams(
                                    self.st, finger_midi,
                                    dynamics.get_distance(self.inst_type,
                                        self.dyn),
                                    bow_force,
                                    dynamics.get_velocity(self.inst_type,
                                        self.dyn))
                        stable_filename = self.files.make_stable_filename(
                                audio_params,
                                K, count+1)
                        self.controller.set_stable_K(self.st, self.dyn, K)
                        self.controller.filesNew(stable_filename)
                        self.controller.comment(
                                "stable inst %i st %i dyn %i finger_midi %.3f"
                                % (self.inst_type, self.st, self.dyn, finger_midi))
                        begin = vivi_controller.NoteBeginning()
                        vivi_types.audio_params_to_physical(
                                audio_params, self.dyn, begin.physical)
                        end = vivi_controller.NoteEnding()
                        #end.keep_bow_velocity = True
                        for bow_direction in [1, -1]:
                           begin.physical.bow_velocity *= bow_direction
                           self.controller.note(begin, STABLE_LENGTH, end)
                        self.controller.filesClose()
                    #emits += 1
                    #print "emit: %i / %i" % (emits, self.steps_full())
                    self.process_step.emit()
Exemplo n.º 19
0
    def load_note(self, note_text, full=False):
        if not self.lines:
            return False
        self.reset_note()
        self.note_text = note_text

        i = 0
        skip_start = 0
        while not self.lines[i].find(self.note_text) >= 0:
            if self.lines[i] == 's':
                skip_start = float(self.lines[i].split()[1])
            if self.lines[i] == 'k':
                skip_end = float(self.lines[i].split()[1])
                self.note_skip += skip_end - skip_start
            i += 1
            if i >= len(self.lines):
                #print "Item not found"
                return False

        self.note_prelim_line = self.lines[i]
        note_prelim_info = self.note_prelim_line.split()
        self.note_inst = int(note_prelim_info[3])
        self.note_st = int(note_prelim_info[5])
        self.note_dyn = float(note_prelim_info[7])
        self.note_finger = round(float(note_prelim_info[9]))
        self.note_pos = dynamics.get_distance(self.note_inst, self.note_dyn)
        self.note_vel = dynamics.get_velocity(self.note_inst, self.note_dyn)
        # find beginning of real actions
        while self.lines[i].startswith('#'):
            i += 1
        i += 1

        while (not self.lines[i].startswith('#')) or (full):
            if not self.lines[i].startswith('#'):
                self.note_lines.append(self.lines[i])
            if self.lines[i][0] == 's':
                skip_start = float(self.lines[i].split()[1])
            if self.lines[i][0] == 'k':
                skip_end = float(self.lines[i].split()[1])
                self.note_skip += skip_end - skip_start
            i += 1
            if i >= len(self.lines):
                break

        self.note_start = float(self.note_lines[0].split()[1])
        self.note_length = (float(self.note_lines[-1].split()[1]) -
                            self.note_start) - self.note_skip
        note_end = self.note_start + self.note_length

        cat_note_begin = None
        cat_note_end = len(self.cat_new_notes)
        for i, line in enumerate(self.cat_new_notes):
            if line is None:
                continue
            else:
                if line.find(note_text) >= 0:
                    cat_note_begin = i
                elif cat_note_begin is not None:
                    cat_note_end = i
                    break
        #print "from notes:", cat_note_begin, cat_note_end
        #print "from orig :", self.note_start, note_end

        for line in self.cat_lines:
            if line[0] != '#':
                splitline = line.split()
                seconds = float(splitline[1])
                if seconds >= self.note_start and seconds <= note_end:
                    self.note_cat_lines.append(line)
            #if line.startswith("#\tnote"):
            #    self.note_cat_lines.append(line)
        #print len(self.note_cat_lines)

        self.note_forces = self.get_note_forces()
        self.note_cats = self.get_note_cats()
        self.note_cats_means = self.get_note_cats_means(self.note_cats)
        #print len(self.note_forces), len(self.note_cats)

        # no clue why I wrote this, so I'm scared to remove it.  :(
        #if len(self.note_cats) < len(self.note_forces):
        #    self.note_cats.append((0, vivi_defines.CATEGORY_NULL))

        #print len(self.note_cats), len(self.note_forces)
        #print '----'
        #print self.note_cats[0], self.note_cats[-1]
        #print '----'
        #print self.note_forces[1], self.note_forces[-1]
        return True
Exemplo n.º 20
0
    def keypress(self, c):
        if c == 'q':
            return False
        if c == 's':
            self.turn_off_current_string()
            # new
            self.params.violin_string += 1
            if self.params.violin_string > 3:
                self.params.violin_string = 0
        if c == 'a':
            self.turn_off_current_string()
            # new
            self.params.violin_string -= 1
            if self.params.violin_string < 0:
                self.params.violin_string = 3
        if c == 'f':
            if self.params.finger_position == 0.0:
                self.params.finger_position = 0.02
            else:
                self.params.finger_position *= 1.1
        if c == 'd':
            if self.params.finger_position < 0.02:
                self.params.finger_position = 0.00
            else:
                self.params.finger_position /= 1.1
        if c == 't':
            self.params.bow_position *= 1.01
        if c == 'g':
            self.params.bow_position /= 1.01
        if c == 'y':
            self.params.force *= 1.1
        if c == 'h':
            self.params.force /= 1.1
        if c == 'u':
            self.params.velocity *= 1.1
        if c == 'j':
            self.params.velocity /= 1.1

        skip_violin_print = False
        if c == 'z' or c == 'x' or c == 'c' or c == 'v':
            if c == 'z':
                alter = 1.0/1.1
            if c == 'x':
                alter = 1.0/1.01
            if c == 'c':
                alter = 1.01
            if c == 'v':
                alter = 1.1
            self.change_tension(alter)
            skip_violin_print = True
        if c >= '1' and c <= str(vivi_defines.CATEGORIES_NUMBER):
            skip_violin_print = True
            self.snapshot(int(c) - vivi_defines.CATEGORIES_CENTER_OFFSET)
            self.stdscr.addstr(self.row, 0, str("file written"))

        if c == 'l':
            if self.params.force > 0:
                self.store_force = self.params.force
                self.params.force = 0
            else:
                self.params.force = self.store_force

        if c == 'm':
            midi = midi_pos.pos2midi(self.params.finger_position)
            midi = round(midi)
            self.params.finger_position = midi_pos.midi2pos(midi)
            self.stdscr.addstr(23, 20, str("midi: %i" % midi))
        if c == 'b':
            self.dyn += 1
            if self.dyn >= 4:
                self.dyn = 0
            self.params.bow_position = dynamics.get_distance(self.dyn)
            self.params.velocity = dynamics.get_velocity(self.dyn)
            self.stdscr.addstr(23, 20, str("dyn: %i  " % self.dyn))
        if c == 'n':
            midi = midi_pos.pos2midi(self.params.finger_position)
            midi = round(midi) + 1
            if midi > 7:
                midi = 0
            self.params.finger_position = midi_pos.midi2pos(midi)
            self.stdscr.addstr(23, 20, str("midi: %i  " % midi))

        self.params_queue.put(self.params)
        if not skip_violin_print:
            self.stdscr.addstr(self.row, 0, str(
                "%i\t%.3f\t%.3f\t%.3f\t%.3f" % (
                self.params.violin_string, self.params.finger_position,
                self.params.bow_position, self.params.force, self.params.velocity)))
            # next line
            self.row += 1
            if self.row > 20:
                self.row = 5
            self.stdscr.addstr(self.row, 0, str(" "*40))
            self.stdscr.move(self.row, 0)
        return True
Exemplo n.º 21
0
    def load_note(self, note_text, full=False):
        if not self.lines:
            return False
        self.reset_note()
        self.note_text = note_text


        i = 0
        skip_start = 0
        while not self.lines[i].find(self.note_text) >= 0:
            if self.lines[i] == 's':
                skip_start = float(self.lines[i].split()[1])
            if self.lines[i] == 'k':
                skip_end = float(self.lines[i].split()[1])
                self.note_skip += skip_end - skip_start
            i += 1
            if i >= len(self.lines):
                #print "Item not found"
                return False

        self.note_prelim_line = self.lines[i]
        note_prelim_info = self.note_prelim_line.split()
        self.note_inst = int(note_prelim_info[3])
        self.note_st = int(note_prelim_info[5])
        self.note_dyn = float(note_prelim_info[7])
        self.note_finger = round(float(note_prelim_info[9]))
        self.note_pos = dynamics.get_distance(self.note_inst, self.note_dyn)
        self.note_vel = dynamics.get_velocity(self.note_inst, self.note_dyn)
        # find beginning of real actions
        while self.lines[i].startswith('#'):
            i += 1
        i += 1

        while (not self.lines[i].startswith('#')) or (full):
            if not self.lines[i].startswith('#'):
                self.note_lines.append( self.lines[i] )
            if self.lines[i][0] == 's':
                skip_start = float(self.lines[i].split()[1])
            if self.lines[i][0] == 'k':
                skip_end = float(self.lines[i].split()[1])
                self.note_skip += skip_end - skip_start
            i += 1
            if i >= len(self.lines):
                break

        self.note_start = float(self.note_lines[0].split()[1])
        self.note_length = (float(self.note_lines[-1].split()[1])
            - self.note_start) - self.note_skip
        note_end = self.note_start + self.note_length

        
        cat_note_begin = None
        cat_note_end = len(self.cat_new_notes)
        for i, line in enumerate(self.cat_new_notes):
            if line is None:
                continue
            else:
                if line.find(note_text) >= 0:
                    cat_note_begin = i
                elif cat_note_begin is not None:
                    cat_note_end = i
                    break
        #print "from notes:", cat_note_begin, cat_note_end
        #print "from orig :", self.note_start, note_end

        for line in self.cat_lines:
            if line[0] != '#':
                splitline = line.split()
                seconds = float(splitline[1])
                if seconds >= self.note_start and seconds <= note_end:
                    self.note_cat_lines.append(line)
            #if line.startswith("#\tnote"):
            #    self.note_cat_lines.append(line)
        #print len(self.note_cat_lines)

        self.note_forces = self.get_note_forces()
        self.note_cats = self.get_note_cats()
        self.note_cats_means = self.get_note_cats_means(self.note_cats)
        #print len(self.note_forces), len(self.note_cats)

        # no clue why I wrote this, so I'm scared to remove it.  :(
        #if len(self.note_cats) < len(self.note_forces):
        #    self.note_cats.append((0, vivi_defines.CATEGORY_NULL))

        #print len(self.note_cats), len(self.note_forces)
        #print '----'
        #print self.note_cats[0], self.note_cats[-1]
        #print '----'
        #print self.note_forces[1], self.note_forces[-1]
        return True
Exemplo n.º 22
0
 def _remove_previous_files(self):
     """ remove files from previous computation of task """
     self.files.delete_files(
         self.files.get_task_files(self.taskname, self.st,
             dynamics.get_distance(self.inst_type, self.dyn),
             dynamics.get_velocity(self.inst_type, self.dyn)))
Exemplo n.º 23
0
 def _get_files(self):
     return self.files.get_task_files(self.taskname, self.st,
         dynamics.get_distance(self.inst_type, self.dyn),
         dynamics.get_velocity(self.inst_type, self.dyn))
Exemplo n.º 24
0
#!/usr/bin/env python

import sys
sys.path.append('build/swig/')

# this breaks it?!
# it's ok if it's: yes, yes, commented
import vivi_controller
import dynamics
#import vivi_controller

vc = vivi_controller.ViviController()
foo = vc.getEars(0,0)
foo.set_training("train/0_0.mf", "bar.arff")
foo.tick_file()
#foo.processFile()

print dynamics.get_distance(0)

Exemplo n.º 25
0
    def _make_files(self):
        self._setup_controller()
        #K = 0.01
        K = 0.0
        K_main = 0.05

        def make_file_force(bow_force, fmi, count):
            self.controller.set_stable_K(self.st, self.dyn, fmi, K)
            self.controller.set_stable_K_main(self.st, self.dyn, fmi, K_main)
            finger_midi = basic_training.FINGER_MIDIS[fmi]
            # FIXME: oh god ick
            ap = vivi_types.AudioParams(
                self.st, finger_midi,
                dynamics.get_distance(self.inst_type, self.dyn), bow_force,
                dynamics.get_velocity(self.inst_type, self.dyn))
            attack_filename = self.files.make_attack_filename(
                self.taskname, ap, count)
            #print attack_filename

            self.controller.reset()
            self.controller.filesNew(attack_filename, self.st)

            self.controller.comment(
                "verify inst %i st %i dyn %i finger_midi %.3f" %
                (self.inst_type, self.st, self.dyn, finger_midi))

            begin = vivi_controller.NoteBeginning()
            begin.physical.string_number = self.st
            begin.physical.dynamic = self.dyn
            begin.physical.finger_position = utils.midi2pos(finger_midi)
            begin.physical.bow_force = bow_force
            begin.physical.bow_bridge_distance = dynamics.get_distance(
                self.inst_type, self.dyn)
            begin.physical.bow_velocity = dynamics.get_velocity(
                self.inst_type, self.dyn)

            end = vivi_controller.NoteEnding()
            #end.keep_bow_velocity = True

            for bow_direction in [1, -1, 1, -1]:
                self.controller.reset(True)
                begin.physical.bow_velocity *= bow_direction
                #begin.physical.bow_force = bow_force
                #begin.keep_ears = False
                #begin.keep_bow_force = False
                #self.controller.note(begin, ATTACK_LENGTH, end)

                #begin.physical.bow_force = bow_force*0.8
                #begin.keep_ears = True
                #begin.keep_bow_force = True
                #self.controller.note(begin, STABLE_LENGTH-ATTACK_LENGTH, end)
                self.controller.note(begin, STABLE_LENGTH, end)
            self.controller.filesClose()
            #self.process_step.emit()
            return attack_filename

        def split_cats(cats):
            #notes = [[],[]]
            notes = [[], [], [], []]
            notes_i = -1
            prev = None
            for i, cat in enumerate(cats):
                if cat is None:
                    continue
                if cat == vivi_defines.CATEGORY_WAIT:
                    continue
                if cat == vivi_defines.CATEGORY_NULL:
                    prev = None
                    continue
                if prev is None:
                    notes_i += 1
                prev = cat
                notes[notes_i].append(cat)
            return notes

        def get_cats(basename):
            nac = note_actions_cats.NoteActionsCats()
            nac.load_file(basename, self.files)
            #to_find = "finger_midi %i" % basic_training.FINGER_MIDIS[self.fm]
            to_find = "finger_midi"
            nac.load_note(to_find, full=True)
            cats = [b for a, b in nac.get_note_cats()]
            #att = self.portion_attack(cats_means)
            #att = cats_means # consider entire note
            #cost = self.get_cost(att)
            notes = split_cats(cats)
            return notes

        def process_cats(cats):
            #print len(cats)
            mean = numpy.mean(cats)
            return mean

        #    #std = numpy.std(cats)
        #    #exit(1)
        #    #if std > 0.5:
        #    return 0
        #    if mean < -0.5:
        #        direction = -1
        #    elif mean > 0.5:
        #        direction = 1
        #    else:
        #        direction = 0
        #    return direction

        def handle_finger(fmi):
            files = []
            init = self.force_init[fmi]
            #low = scipy.stats.gmean( [init[0], init[1]])
            #high = scipy.mean( [init[1], init[2]])
            low = init[0]
            #if low < 0.01:
            #    low = 0.01
            high = init[2]

            # sanity check
            lows = []
            highs = []
            mids = []

            low_files = []
            high_files = []
            lows.append(low)
            highs.append(high)
            for count in range(1, self.REPS + 1):
                low_filename = make_file_force(low, fmi, count)
                low_cats = []
                low_cats.extend(sum(get_cats(low_filename), []))
                low_direction = process_cats(low_cats)
                low_files.append((low, low_filename, low_direction))
                high_filename = make_file_force(high, fmi, count)
                high_cats = []
                high_cats.extend(sum(get_cats(high_filename), []))
                high_direction = process_cats(high_cats)
                high_files.append((high, high_filename, high_direction))
                #print low_direction, high_direction
                if low_direction > LOW_MEAN_ACCEPTED or high_direction < HIGH_MEAN_ACCEPTED:
                    #print "PANIC: fail to begin attack search"
                    self.verify_good = False
                self.process_step.emit()
                self.process_step.emit()

            def process(force, fmi, lows, mids, highs, low_files, high_files):
                #print "%.3f\t%.3f\t%.3f" % (
                #    low, force, high)
                these_files = []
                directions = []
                for count in range(1, self.REPS + 1):
                    mid_filename = make_file_force(force, fmi, count)
                    mid_cats = get_cats(mid_filename)
                    mid_direction1 = process_cats(mid_cats[0])
                    mid_direction2 = process_cats(mid_cats[1])
                    mid_direction = numpy.mean(
                        (mid_direction1, mid_direction2))
                    #if mid_direction1 != mid_direction2:
                    #    mid_direction1 = 0
                    these_files.append((force, mid_filename, mid_direction))
                    directions.append(mid_direction1)
                if numpy.mean(directions) < LOW_MEAN_ACCEPTED:
                    lows.append(force)
                    #low_files = these_files
                    low_files.extend(these_files)
                    #print "low"
                elif numpy.mean(directions) > HIGH_MEAN_ACCEPTED:
                    highs.append(force)
                    #high_files = these_files
                    high_files.extend(these_files)
                    #print "high"
                else:
                    mids.append(force)
                    files.extend(these_files)
                    #print "mid"
                for count in range(self.REPS):
                    self.process_step.emit()
                return low_files, high_files

            while len(mids) == 0:
                if (len(lows) + len(highs) + len(mids)) >= STEPS:
                    break
                #force = numpy.mean( (max(lows), min(highs)) )
                force = scipy.stats.gmean((max(lows), min(highs)))
                #print "init", fmi, max(lows), force, min(highs)
                low_files, high_files = process(force, fmi, lows, mids, highs,
                                                low_files, high_files)
            while True:
                if (len(lows) + len(highs) + len(mids)) >= STEPS:
                    break
                #print max(lows), min(mids), max(mids), min(highs)
                lowdistrel = min(mids) / max(lows)
                highdistrel = min(highs) / max(mids)
                #print lowdistrel, highdistrel
                if lowdistrel > highdistrel:
                    force = scipy.stats.gmean((max(lows), min(mids)))
                    force *= random.uniform(0.95, 1.05)
                else:
                    force = scipy.stats.gmean((max(mids), min(highs)))
                    force *= random.uniform(0.95, 1.05)
                #print "main", max(lows), min(mids), max(mids), min(highs)
                #print "new force:\t", force
                # do force
                low_files, high_files = process(force, fmi, lows, mids, highs,
                                                low_files, high_files)
            files.extend(low_files)
            files.extend(high_files)
            files.sort()
            if len(mids) == 1:
                mids.append(mids[0] * 1.05)
            elif len(mids) == 0:
                mids.append(max(lows))
                mids.append(min(highs))
                self.verify_good = False
            #self.mids[fmi] = [ min(mids), max(mids) ]
            self.mids[fmi] = [max(lows), min(highs)]
            #mids.sort()
            #print mids

            return files

        self.kept_files = [[], [], []]
        self.mids = [[], [], []]
        for fmi in range(3):
            #print '-------------- ', fmi
            self.kept_files[fmi] = handle_finger(fmi)
            #print self.mids[fmi]
        #handle_finger(1, self.kept_files[1])
        #handle_finger(2, self.kept_files[2])

        #print self.kept_files
        #print self.mids

        return

        #print self.forces
        #import scipy.special
        #agm = scipy.special.agm(min(self.forces), max(self.forces))
        #agm /= 4.0
        #mid = (min(self.forces) + max(self.forces)) / 2
        #mid /= 8.0
        #lower = list(numpy.linspace(min(self.forces), mid, STEPS/2+1))
        ###upper = list(numpy.linspace(agm, max(self.forces), STEPS/2+2))[:-1]
        #upper = list(numpy.exp(
        #    numpy.linspace(
        #        numpy.log(mid),
        #        numpy.log(max(self.forces)),
        #        STEPS/2+1)))
        #self.test_range = list(set(lower + upper))

        #self.test_range = list(numpy.linspace(min(self.forces),
        #    max(self.forces), STEPS))
        #self.test_range = list(numpy.exp(
        #numpy.linspace(
        #    numpy.log(min(self.forces)),
        #    numpy.log(max(self.forces)),
        #    STEPS)))
        #print "TEST:", self.test_range
        #print "fmi:", self.finger_midi_index
        #print self.forces
        #self.test_range = self._make_forces(self.forces, 7)
        self.test_range = []
        #forces = self.test_range
        #self.test_range = numpy.array([forces[0], forces[1], forces[2],
        #    forces[4],
        #    forces[6], forces[7], forces[8]])
        #forces = self.test_range

        K = 1.0
        self.controller.set_stable_K(self.st, self.dyn, K)
        for fmi, finger_midi in enumerate(FINGERS):
            test_forces = self._make_forces(self.forces[fmi], 9)
            test_forces = test_forces[3:6]
            self.test_range.extend(list(test_forces))
            #print self.test_range
            for count in range(self.REPS):
                # ick
                #fmi = self.finger_midi_index
                #finger_midi = self.finger_midi
                #finger_midi = basic_training.FINGER_MIDIS[fmi]
                #test_forces = numpy.append(numpy.linspace(
                #      self.forces[fmi][0],
                #      self.forces[fmi][1],
                #      num=3, endpoint=False),
                #    numpy.linspace(
                #      self.forces[fmi][1],
                #      self.forces[fmi][2],
                #      num=4, endpoint=True))
                #for a in test_forces:
                #    print "%.3f " % a,
                #print
                #test_forces = self.test_range
                #print test_forces
                #for fmi, finger_midi in enumerate(basic_training.FINGER_MIDIS):
                for bow_force in test_forces:
                    audio_params = vivi_types.AudioParams(
                        self.st, finger_midi,
                        dynamics.get_distance(self.inst_type, self.dyn),
                        bow_force,
                        dynamics.get_velocity(self.inst_type, self.dyn))
                    verify_filename = self.files.make_verify_filename(
                        self.taskname, audio_params, count)
                    #print verify_filename
                    self.controller.filesNew(verify_filename)
                    self.controller.comment(
                        "verify int %i st %i dyn %i finger_midi %.3f" %
                        (self.inst_type, self.st, self.dyn, finger_midi))
                    begin = vivi_controller.NoteBeginning()
                    vivi_types.audio_params_to_physical(
                        audio_params, self.dyn, begin.physical)
                    end = vivi_controller.NoteEnding()
                    end.keep_bow_velocity = True
                    #for bow_direction in [1, -1]:
                    #    begin.physical.bow_velocity *= bow_direction
                    #    self.controller.note(begin, STABLE_LENGTH, end)

                    #print "Starting note, force:", bow_force
                    self.controller.note(begin, STABLE_LENGTH, end)
                    self.controller.filesClose()
                self.process_step.emit()
Exemplo n.º 26
0
    def _make_files(self):
        self._setup_controller()
        #K = 0.01
        K = 0.0
        K_main = 0.05

        def make_file_force(bow_force, fmi, count):
            self.controller.set_stable_K(self.st, self.dyn, fmi, K)
            self.controller.set_stable_K_main(self.st, self.dyn, fmi, K_main)
            finger_midi = basic_training.FINGER_MIDIS[fmi]
            # FIXME: oh god ick
            ap = vivi_types.AudioParams( self.st,
                    finger_midi,
                    dynamics.get_distance(self.inst_type,
                        self.dyn),
                    bow_force,
                    dynamics.get_velocity(self.inst_type,
                        self.dyn))
            attack_filename = self.files.make_attack_filename(
                    self.taskname,
                    ap, count)
            #print attack_filename

            self.controller.reset()
            self.controller.filesNew(attack_filename, self.st)

            self.controller.comment("verify inst %i st %i dyn %i finger_midi %.3f"
                        % (self.inst_type, self.st, self.dyn, finger_midi))

            begin = vivi_controller.NoteBeginning()
            begin.physical.string_number = self.st
            begin.physical.dynamic = self.dyn
            begin.physical.finger_position = utils.midi2pos(finger_midi)
            begin.physical.bow_force = bow_force
            begin.physical.bow_bridge_distance = dynamics.get_distance(
                    self.inst_type, self.dyn)
            begin.physical.bow_velocity = dynamics.get_velocity(
                    self.inst_type, self.dyn)

            end = vivi_controller.NoteEnding()
            #end.keep_bow_velocity = True

            for bow_direction in [1, -1, 1, -1]:
                self.controller.reset(True)
                begin.physical.bow_velocity *= bow_direction
                #begin.physical.bow_force = bow_force
                #begin.keep_ears = False
                #begin.keep_bow_force = False
                #self.controller.note(begin, ATTACK_LENGTH, end)

                #begin.physical.bow_force = bow_force*0.8
                #begin.keep_ears = True
                #begin.keep_bow_force = True
                #self.controller.note(begin, STABLE_LENGTH-ATTACK_LENGTH, end)
                self.controller.note(begin, STABLE_LENGTH, end)
            self.controller.filesClose()
            #self.process_step.emit()
            return attack_filename

        def split_cats(cats):
            #notes = [[],[]]
            notes = [[],[],[],[]]
            notes_i = -1
            prev = None
            for i, cat in enumerate(cats):
                if cat is None:
                    continue
                if cat == vivi_defines.CATEGORY_WAIT:
                    continue
                if cat == vivi_defines.CATEGORY_NULL:
                    prev = None
                    continue
                if prev is None:
                    notes_i += 1
                prev = cat
                notes[notes_i].append(cat)
            return notes


        def get_cats(basename):
            nac = note_actions_cats.NoteActionsCats()
            nac.load_file(basename, self.files)
            #to_find = "finger_midi %i" % basic_training.FINGER_MIDIS[self.fm]
            to_find = "finger_midi"
            nac.load_note(to_find, full=True)
            cats = [ b for a,b in nac.get_note_cats() ]
            #att = self.portion_attack(cats_means)
            #att = cats_means # consider entire note
            #cost = self.get_cost(att)
            notes = split_cats(cats)
            return notes

        def process_cats(cats):
            #print len(cats)
            mean = numpy.mean(cats)
            return mean
        #    #std = numpy.std(cats)
        #    #exit(1)
        #    #if std > 0.5:
            #    return 0
        #    if mean < -0.5:
        #        direction = -1
        #    elif mean > 0.5:
        #        direction = 1
        #    else:
        #        direction = 0
        #    return direction

        def handle_finger(fmi):
            files = []
            init = self.force_init[fmi]
            #low = scipy.stats.gmean( [init[0], init[1]])
            #high = scipy.mean( [init[1], init[2]])
            low = init[0]
            #if low < 0.01:  
            #    low = 0.01
            high = init[2]
    
            # sanity check
            lows = []
            highs = []
            mids = []

    
            low_files = []
            high_files = []
            lows.append(low)
            highs.append(high)
            for count in range(1,self.REPS+1):
                low_filename = make_file_force(low, fmi, count)
                low_cats = []
                low_cats.extend( sum( get_cats(low_filename), []) )
                low_direction = process_cats(low_cats)
                low_files.append((low, low_filename, low_direction))
                high_filename = make_file_force(high, fmi, count)
                high_cats = []
                high_cats.extend( sum( get_cats(high_filename), []) )
                high_direction = process_cats(high_cats)
                high_files.append((high, high_filename, high_direction))
                #print low_direction, high_direction
                if low_direction > LOW_MEAN_ACCEPTED or high_direction < HIGH_MEAN_ACCEPTED:
                    #print "PANIC: fail to begin attack search"
                    self.verify_good = False
                self.process_step.emit()
                self.process_step.emit()

            def process(force, fmi, lows, mids, highs, low_files,
                    high_files):
                #print "%.3f\t%.3f\t%.3f" % (
                #    low, force, high)
                these_files = []
                directions = []
                for count in range(1,self.REPS+1):
                    mid_filename = make_file_force(force, fmi, count)
                    mid_cats = get_cats(mid_filename)
                    mid_direction1 = process_cats(mid_cats[0])
                    mid_direction2 = process_cats(mid_cats[1])
                    mid_direction = numpy.mean((mid_direction1, mid_direction2))
                    #if mid_direction1 != mid_direction2:
                    #    mid_direction1 = 0
                    these_files.append( (force, mid_filename, mid_direction))
                    directions.append(mid_direction1)
                if numpy.mean(directions) < LOW_MEAN_ACCEPTED:
                    lows.append(force)
                    #low_files = these_files
                    low_files.extend(these_files)
                    #print "low"
                elif numpy.mean(directions) > HIGH_MEAN_ACCEPTED:
                    highs.append(force)
                    #high_files = these_files
                    high_files.extend(these_files)
                    #print "high"
                else:
                    mids.append(force)
                    files.extend(these_files)
                    #print "mid"
                for count in range(self.REPS):
                    self.process_step.emit()
                return low_files, high_files

            while len(mids) == 0:
                if (len(lows) + len(highs) + len(mids)) >= STEPS: 
                    break
                #force = numpy.mean( (max(lows), min(highs)) )
                force = scipy.stats.gmean( (max(lows), min(highs)) )
                #print "init", fmi, max(lows), force, min(highs)
                low_files, high_files = process(force, fmi, lows,
                    mids, highs, low_files, high_files)
            while True:
                if (len(lows) + len(highs) + len(mids)) >= STEPS: 
                    break
                #print max(lows), min(mids), max(mids), min(highs)
                lowdistrel = min(mids) / max(lows)
                highdistrel = min(highs) / max(mids)
                #print lowdistrel, highdistrel
                if lowdistrel > highdistrel:
                    force = scipy.stats.gmean( (max(lows), min(mids)) )
                    force *= random.uniform(0.95, 1.05)
                else:
                    force = scipy.stats.gmean( (max(mids), min(highs)) )
                    force *= random.uniform(0.95, 1.05)
                #print "main", max(lows), min(mids), max(mids), min(highs)
                #print "new force:\t", force
                # do force
                low_files, high_files = process(force, fmi, lows,
                    mids, highs, low_files, high_files)
            files.extend(low_files)
            files.extend(high_files)
            files.sort()
            if len(mids) == 1:
                mids.append( mids[0]*1.05 )
            elif len(mids) == 0:
                mids.append( max(lows) )
                mids.append( min(highs) )
                self.verify_good = False
            #self.mids[fmi] = [ min(mids), max(mids) ]
            self.mids[fmi] = [ max(lows), min(highs) ]
            #mids.sort()
            #print mids

            return files
   
        self.kept_files = [[], [], []]
        self.mids = [[], [], []]
        for fmi in range(3):
            #print '-------------- ', fmi
            self.kept_files[fmi] = handle_finger(fmi)
            #print self.mids[fmi]
        #handle_finger(1, self.kept_files[1])
        #handle_finger(2, self.kept_files[2])

        #print self.kept_files
        #print self.mids

        return


        #print self.forces
        #import scipy.special
        #agm = scipy.special.agm(min(self.forces), max(self.forces))
        #agm /= 4.0
        #mid = (min(self.forces) + max(self.forces)) / 2
        #mid /= 8.0
        #lower = list(numpy.linspace(min(self.forces), mid, STEPS/2+1))
        ###upper = list(numpy.linspace(agm, max(self.forces), STEPS/2+2))[:-1]
        #upper = list(numpy.exp(
        #    numpy.linspace(
        #        numpy.log(mid),
        #        numpy.log(max(self.forces)),
        #        STEPS/2+1)))
        #self.test_range = list(set(lower + upper))

        #self.test_range = list(numpy.linspace(min(self.forces),
        #    max(self.forces), STEPS))
        #self.test_range = list(numpy.exp(
            #numpy.linspace(
            #    numpy.log(min(self.forces)),
            #    numpy.log(max(self.forces)),
            #    STEPS)))
        #print "TEST:", self.test_range
        #print "fmi:", self.finger_midi_index
        #print self.forces
        #self.test_range = self._make_forces(self.forces, 7)
        self.test_range = []
        #forces = self.test_range
        #self.test_range = numpy.array([forces[0], forces[1], forces[2],
        #    forces[4],
        #    forces[6], forces[7], forces[8]])
        #forces = self.test_range

        K = 1.0
        self.controller.set_stable_K(self.st, self.dyn, K)
        for fmi, finger_midi in enumerate(FINGERS):
            test_forces = self._make_forces(self.forces[fmi], 9)
            test_forces = test_forces[3:6]
            self.test_range.extend( list(test_forces))
            #print self.test_range
            for count in range(self.REPS):
                # ick
                #fmi = self.finger_midi_index
                #finger_midi = self.finger_midi
                #finger_midi = basic_training.FINGER_MIDIS[fmi]
                #test_forces = numpy.append(numpy.linspace(
                #      self.forces[fmi][0],
                #      self.forces[fmi][1],
                #      num=3, endpoint=False),
                #    numpy.linspace(
                #      self.forces[fmi][1],
                #      self.forces[fmi][2],
                #      num=4, endpoint=True))
                #for a in test_forces:
                #    print "%.3f " % a,
                #print
                #test_forces = self.test_range
                #print test_forces
                #for fmi, finger_midi in enumerate(basic_training.FINGER_MIDIS):
                for bow_force in test_forces:
                    audio_params = vivi_types.AudioParams(
                                self.st, finger_midi,
                                dynamics.get_distance(self.inst_type,self.dyn),
                                bow_force,
                                dynamics.get_velocity(self.inst_type,self.dyn))
                    verify_filename = self.files.make_verify_filename(
                            self.taskname,
                            audio_params,
                            count)
                    #print verify_filename
                    self.controller.filesNew(verify_filename)
                    self.controller.comment(
                            "verify int %i st %i dyn %i finger_midi %.3f"
                            % (self.inst_type, self.st, self.dyn, finger_midi))
                    begin = vivi_controller.NoteBeginning()
                    vivi_types.audio_params_to_physical(
                            audio_params, self.dyn, begin.physical)
                    end = vivi_controller.NoteEnding()
                    end.keep_bow_velocity = True
                        #for bow_direction in [1, -1]:
                        #    begin.physical.bow_velocity *= bow_direction
                        #    self.controller.note(begin, STABLE_LENGTH, end)

                        #print "Starting note, force:", bow_force
                    self.controller.note(begin, STABLE_LENGTH, end)
                    self.controller.filesClose()
                self.process_step.emit()