コード例 #1
0
ファイル: main.py プロジェクト: anndream/virtual-piano
 def init_piano(self):
     keymap_filename = 'pianoeletronico.kmp'
     notes_manager = NotesManager()
     notes_manager.load_file(util.app_file_path(os.path.join('keymaps', keymap_filename)))
     self.midi = midi.Midi()
     self.midi_output = midi.Output(self.midi.get_default_output_id(), 0)
     self.piano = Piano(notes_manager, self.midi_output)
     self.piano.set_instrument(0, 0)
コード例 #2
0
 def _output_device_fired(self):
     if self.midi_input is not None:
         self.midi_output.close()
     self.midi_output = midi.Output(self.output_device[0])
     self.push_current_values()
コード例 #3
0
class Controls(HasTraits):
    if len(inputs) == 1:
        default_input = inputs

    for i in inputs:
        if not "Through Port" in i[1]:
            default_input = i
            break

    default_input = default_input if len(inputs) > 0 else None

    default_output = -1
    for i in outputs:
        if not "Through Port" in i[1]:
            default_output = i
            break
        else:
            through_port_output = i
    default_output = default_output if len(
        outputs) > 1 else through_port_output

    input_device = List(value=default_input,
                        editor=CheckListEditor(values=inputs))
    output_device = List(value=default_output,
                         editor=CheckListEditor(values=outputs))

    max_temp = 2.
    min_temp = 0.5
    max_press = 10.
    min_press = 5e-4
    max_vol = 100000.
    min_vol = 50.
    max_n = 1000
    min_n = 50

    temperature = Range(
        min_temp,
        max_temp,
        1.,
    )
    volume = Float(box_l**3.)
    pressure = Float(1.)
    number_of_particles = Range(
        min_n,
        max_n,
        n_part,
    )
    ensemble = Enum('NVT', 'NPT')

    midi_input = midi.Input(inputs[0][0]) if len(inputs) > 1 else None
    midi_output = midi.Output(outputs[0][0]) if len(outputs) > 1 else None

    MIDI_BASE = 224
    MIDI_NUM_TEMPERATURE = MIDI_BASE + 0
    MIDI_NUM_VOLUME = MIDI_BASE + 1
    MIDI_NUM_PRESSURE = MIDI_BASE + 2
    MIDI_NUM_NUMBEROFPARTICLES = MIDI_BASE + 3

    MIDI_ROTATE = 0

    MIDI_ZOOM = 176

    _ui = Any
    view = View(Group(Item('temperature',
                           editor=RangeEditor(low_name='min_temp',
                                              high_name='max_temp')),
                      Item('volume',
                           editor=RangeEditor(low_name='min_vol',
                                              high_name='max_vol')),
                      Item('pressure',
                           editor=RangeEditor(low_name='min_press',
                                              high_name='max_press')),
                      Item('number_of_particles',
                           editor=RangeEditor(low_name='min_n',
                                              high_name='max_n',
                                              is_float=False)),
                      Item('ensemble', style='custom'),
                      show_labels=True,
                      label='Parameters'),
                Group(Item('input_device'),
                      Item('output_device'),
                      show_labels=True,
                      label='MIDI devices'),
                buttons=[],
                title='Control')

    def __init__(self, **traits):
        super(Controls, self).__init__(**traits)
        self._ui = self.edit_traits()
        self.push_current_values()

    def push_current_values(self):
        """send the current values to the MIDI controller"""
        self._temperature_fired()
        self._volume_fired()
        self._pressure_fired()
        self._number_of_particles_fired()
        self._ensemble_fired()

    def _input_device_fired(self):
        if self.midi_input is not None:
            self.midi_input.close()
        self.midi_input = midi.Input(self.input_device[0])

    def _output_device_fired(self):
        if self.midi_input is not None:
            self.midi_output.close()
        self.midi_output = midi.Output(self.output_device[0])
        self.push_current_values()

    def _temperature_fired(self):
        status = self.MIDI_NUM_TEMPERATURE
        data1 = int((self.temperature - self.min_temp) /
                    (self.max_temp - self.min_temp) * 127)
        data2 = data1
        if self.midi_output is not None:
            self.midi_output.write_short(status, data1, data2)

    def _volume_fired(self):
        status = self.MIDI_NUM_VOLUME
        data1 = limit_range(int((system.box_l[0]**3. - self.min_vol) /
                                (self.max_vol - self.min_vol) * 127),
                            minval=0,
                            maxval=127)
        data2 = data1

        if self.midi_output is not None:
            self.midi_output.write_short(status, data1, data2)

    def _pressure_fired(self):
        status = self.MIDI_NUM_PRESSURE

        if pressure_log_flag:
            data1 = limit_range(int(
                127 * (numpy.log(self.pressure) - numpy.log(self.min_press)) /
                (numpy.log(self.max_press) - numpy.log(self.min_press))),
                                minval=0,
                                maxval=127)
        else:
            data1 = limit_range(int((self.pressure - self.min_press) /
                                    (self.max_press - self.min_press) * 127),
                                minval=0,
                                maxval=127)
        data2 = data1
        if self.midi_output is not None:
            self.midi_output.write_short(status, data1, data2)

    def _number_of_particles_fired(self):
        status = self.MIDI_NUM_NUMBEROFPARTICLES
        data1 = int(self.number_of_particles / self.max_n * 127)
        data2 = data1
        if self.midi_output is not None:
            self.midi_output.write_short(status, data1, data2)

    def _ensemble_fired(self):
        self.midi_output.write_short(144, 0, 127)  # T
        self.midi_output.write_short(144, 1,
                                     127 * (self.ensemble != 'NPT'))  # V
        self.midi_output.write_short(144, 2,
                                     127 * (self.ensemble == 'NPT'))  # P
        self.midi_output.write_short(144, 3, 127)  # N