Пример #1
0
void TitleState::Update() {

  MouseInfo mouse = Mouse();

  if (m_skip_next_mouse_up) {
    mouse.released.left = false;
    m_skip_next_mouse_up = false;
  }

  m_continue_button.Update(mouse);
  m_back_button.Update(mouse);

  MouseInfo output_mouse(mouse);
  output_mouse.x -= m_output_tile->GetX();
  output_mouse.y -= m_output_tile->GetY();
  m_output_tile->Update(output_mouse);

  MouseInfo input_mouse(mouse);
  input_mouse.x -= m_input_tile->GetX();
  input_mouse.y -= m_input_tile->GetY();
  m_input_tile->Update(input_mouse);

  MouseInfo file_mouse(mouse);
  file_mouse.x -= m_file_tile->GetX();
  file_mouse.y -= m_file_tile->GetY();
  m_file_tile->Update(file_mouse);

  // Check to see for clicks on the file box
  if (m_file_tile->Hit()) {
    m_skip_next_mouse_up = true;

    if (m_state.midi_out) {
      m_state.midi_out->Reset();
      m_output_tile->TurnOffPreview();
    }

    Midi *new_midi = 0;

    string filename;
    string file_title;
    FileSelector::RequestMidiFilename(&filename, &file_title);

    if (filename != "") {
      try {
        new_midi = new Midi(Midi::ReadFromFile(filename));
      }
      catch (const MidiError &e) {
        string description = STRING("Problem while loading file: " <<
                                    file_title << "\n") + e.GetErrorDescription();
        Compatible::ShowError(description);
        new_midi = 0;
      }

      if (new_midi) {

        SharedState new_state;
        new_state.midi = new_midi;
        new_state.midi_in = m_state.midi_in;
        new_state.midi_out = m_state.midi_out;
        new_state.song_title = FileSelector::TrimFilename(filename);

        delete m_state.midi;
        m_state = new_state;

        m_file_tile->SetString(m_state.song_title);
      }
    }
  }

  // Check to see if we need to switch to a newly selected output device
  int output_id = m_output_tile->GetDeviceId();
  if (!m_state.midi_out ||
      output_id != static_cast<int>(m_state.midi_out->GetDeviceDescription().id)) {

    if (m_state.midi_out)
      m_state.midi_out->Reset();

    delete m_state.midi_out;
    m_state.midi_out = 0;

    // save to user preferences
    if (output_id >= 0) {

      m_state.midi_out = new MidiCommOut(output_id);
      m_state.midi->Reset(0,0);

      UserSetting::Set(OutputDeviceKey, m_state.midi_out->GetDeviceDescription().name);
    }
    else
      UserSetting::Set(OutputDeviceKey, OutputKeySpecialDisabled);

  }

  if (m_state.midi_out) {
    if (m_output_tile->HitPreviewButton()) {
      m_state.midi_out->Reset();

      if (m_output_tile->IsPreviewOn()) {

        const microseconds_t PreviewLeadIn  = 250000;
        const microseconds_t PreviewLeadOut = 250000;
        m_state.midi->Reset(PreviewLeadIn, PreviewLeadOut);

        PlayDevicePreview(0);
      }
    }

    else
      PlayDevicePreview(static_cast<microseconds_t>(GetDeltaMilliseconds()) * 1000);

  }

  int input_id = m_input_tile->GetDeviceId();
   if (!m_state.midi_in ||
       input_id != static_cast<int>(m_state.midi_in->GetDeviceDescription().id)) {

     if (m_state.midi_in)
       m_state.midi_in->Reset();

     m_last_input_note_name = "";

     delete m_state.midi_in;
     m_state.midi_in = 0;

     if (input_id >= 0) {

       try {
         m_state.midi_in = new MidiCommIn(input_id);
         UserSetting::Set(InputDeviceKey, m_state.midi_in->GetDeviceDescription().name);
       }

       catch (MidiErrorCode) {
         m_state.midi_in = 0;
       }
     }

     else
       UserSetting::Set(InputDeviceKey, InputKeySpecialDisabled);

   }

   if (m_state.midi_in && m_input_tile->IsPreviewOn()) {

     // Read note events to display on screen
     while (m_state.midi_in->KeepReading()) {
       MidiEvent ev = m_state.midi_in->Read();

       // send to output (for a possible audio preview)
       if (m_state.midi_out)
         m_state.midi_out->Write(ev);

       if (ev.Type() == MidiEventType_NoteOff || ev.Type() == MidiEventType_NoteOn) {
         string note = MidiEvent::NoteName(ev.NoteNumber());

         if (ev.Type() == MidiEventType_NoteOn && ev.NoteVelocity() > 0)
           m_last_input_note_name = note;

         else
           if (note == m_last_input_note_name)
             m_last_input_note_name = "";

       }
     }
   }

   else
     m_last_input_note_name = "";

   if (IsKeyPressed(KeyEscape) || m_back_button.hit) {
     delete m_state.midi_out;
     m_state.midi_out = 0;

     delete m_state.midi_in;
     m_state.midi_in = 0;

     delete m_state.midi;
     m_state.midi = 0;

     Compatible::GracefulShutdown();
     return;
   }

   if (IsKeyPressed(KeyEnter) || m_continue_button.hit) {

     if (m_state.midi_out)
       m_state.midi_out->Reset();

     if (m_state.midi_in)
       m_state.midi_in->Reset();

     ChangeState(new TrackSelectionState(m_state));
     return;
   }

   m_tooltip = "";

   if (m_back_button.hovering)
     m_tooltip = "Click to exit Linthesia.";

   if (m_continue_button.hovering)
     m_tooltip = "Click to continue on to the track selection screen.";

   if (m_file_tile->WholeTile().hovering)
     m_tooltip = "Click to choose a different MIDI file.";

   if (m_input_tile->ButtonLeft().hovering)
     m_tooltip = "Cycle through available input devices.";

   if (m_input_tile->ButtonRight().hovering)
     m_tooltip = "Cycle through available input devices.";

   if (m_input_tile->ButtonPreview().hovering) {
     if (m_input_tile->IsPreviewOn())
       m_tooltip = "Turn off test MIDI input for this device.";
     else
       m_tooltip = "Click to test your MIDI input device by playing notes.";
   }

   if (m_output_tile->ButtonLeft().hovering)
     m_tooltip = "Cycle through available output devices.";

   if (m_output_tile->ButtonRight().hovering)
     m_tooltip = "Cycle through available output devices.";

   if (m_output_tile->ButtonPreview().hovering) {
     if (m_output_tile->IsPreviewOn())
       m_tooltip = "Turn off output test for this device.";
      else
        m_tooltip = "Click to test MIDI output on this device.";
   }
}
Пример #2
0
void PlayingState::Listen() {
  if (!m_state.midi_in)
    return;

  while (m_state.midi_in->KeepReading()) {

    microseconds_t cur_time = m_state.midi->GetSongPositionInMicroseconds();
    MidiEvent ev = m_state.midi_in->Read();
    if (m_state.midi_in->ShouldReconnect())
    {
        m_state.midi_in->Reconnect();
        m_state.midi_out->Reconnect();
        continue;
    }


    // Just eat input if we're paused
    if (m_paused)
      continue;

    // We're only interested in NoteOn and NoteOff
    if (ev.Type() != MidiEventType_NoteOn && ev.Type() != MidiEventType_NoteOff)
      continue;

    // Octave Sliding
    ev.ShiftNote(m_note_offset);

    int note_number = ev.NoteNumber();
    string note_name = MidiEvent::NoteName(note_number);

    // On key release we have to look for existing "active" notes and turn them off.
    if (ev.Type() == MidiEventType_NoteOff || ev.NoteVelocity() == 0) {

      // NOTE: This assumes mono-channel input.  If they're piping an entire MIDI file
      //       (or even the *same* MIDI file) through another source, we could get the
      //       same NoteId on different channels -- and this code would start behaving
      //       incorrectly.
      for (ActiveNoteSet::iterator i = m_active_notes.begin(); i != m_active_notes.end(); ++i) {
        if (ev.NoteNumber() != i->note_id)
          continue;

        // Play it on the correct channel to turn the note we started
        // previously, off.
        ev.SetChannel(i->channel);
        if (m_state.midi_out)
          m_state.midi_out->Write(ev);

        m_active_notes.erase(i);
        break;
      }

      // User releases the key
      // If we delete this line, than all pressed keys will be gray until
      // it is unpressed automatically
      m_keyboard->SetKeyActive(note_name, false, Track::FlatGray);
      userPressedKey(note_number, false);
      continue;
    }

    TranslatedNoteSet::iterator closest_match = m_notes.end();
    for (TranslatedNoteSet::iterator i = m_notes.begin(); i != m_notes.end(); ++i) {

      const microseconds_t window_start = i->start - (KeyboardDisplay::NoteWindowLength / 2);
      const microseconds_t window_end = i->start + (KeyboardDisplay::NoteWindowLength / 2);

      // As soon as we start processing notes that couldn't possibly
      // have been played yet, we're done.
      if (window_start > cur_time)
        break;

      if (i->state != UserPlayable)
        continue;

      if (window_end > cur_time && i->note_id == ev.NoteNumber()) {

        if (closest_match == m_notes.end()) {
          closest_match = i;
          continue;
        }

        microseconds_t this_distance = cur_time - i->start;
        if (i->start > cur_time)
          this_distance = i->start - cur_time;

        microseconds_t known_best = cur_time - closest_match->start;
        if (closest_match->start > cur_time)
          known_best = closest_match->start - cur_time;

        if (this_distance < known_best)
          closest_match = i;
      }
    }

    Track::TrackColor note_color = Track::FlatGray;

    if (closest_match != m_notes.end()) {
      note_color = m_state.track_properties[closest_match->track_id].color;

      // "Open" this note so we can catch the close later and turn off
      // the note.
      ActiveNote n;
      n.channel = closest_match->channel;
      n.note_id = closest_match->note_id;
      n.velocity = closest_match->velocity;
      m_active_notes.insert(n);

      // Play it
      ev.SetChannel(n.channel);
      ev.SetVelocity(n.velocity);

      bool silently =
          m_state.track_properties[closest_match->track_id].mode == Track::ModeYouPlaySilently ||
          m_state.track_properties[closest_match->track_id].mode == Track::ModeLearningSilently;
      if (m_state.midi_out && !silently)
        m_state.midi_out->Write(ev);

      // Adjust our statistics
      const static double NoteValue = 100.0;
      m_state.stats.score += NoteValue * CalculateScoreMultiplier() * (m_state.song_speed / 100.0);

      m_state.stats.notes_user_could_have_played++;
      m_state.stats.speed_integral += m_state.song_speed;

      m_state.stats.notes_user_actually_played++;
      m_current_combo++;
      m_state.stats.longest_combo = max(m_current_combo, m_state.stats.longest_combo);

      TranslatedNote replacement = *closest_match;
      replacement.state = UserHit;

      m_notes.erase(closest_match);
      m_notes.insert(replacement);
    }

    else
      m_state.stats.stray_notes++;

    m_state.stats.total_notes_user_pressed++;
    // Display a pressed key by an user
    // Display a colored key, if it is pressed correctly
    // Otherwise display a grey key
    // 
    // If we comment this code, than a missed user pressed key will not shown.
    // But correct presed key will be shown as usual.
    m_keyboard->SetKeyActive(note_name, true, note_color);
    userPressedKey(note_number, true);
  }
}