summaryrefslogtreecommitdiffhomepage
path: root/UI.cpp
blob: 0adb48bda655359401054dba5a2810db8cf7b24f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "UI.h"

#include "cpuload.h"
#include "debug.h"
#include "log.h"

#include <algorithm>
#include <iostream>
#include <string>

#include <fmt/color.h>
#include <fmt/format.h>

const int midi_monitor_max_size = 3;

using namespace std::chrono_literals;

static std::vector<std::string> mode_names{
  "NoteClick", "Clock", "Internal"
};

static std::vector<std::string> output_names{
  "Off", "On"
};

static std::vector<std::string> active_sensing_names{
  "Not Detected", "Detected"
};

IntervalCounter::IntervalCounter()
{
}

int IntervalCounter::get_count_per_second()
{
  // calculate result
  std::chrono::time_point<clock_type> now = clock_type::now();

  uint64_t diff_ms = std::chrono::duration_cast<std::chrono::milliseconds>(now - m_checkpoint_timestamp).count();
  uint64_t count_per_second = (diff_ms == 0 || m_count_checkpoint == 0) ? 0 : ((m_count - m_count_checkpoint) * 1000 / diff_ms);

  // update state
  m_checkpoint_timestamp = now;
  m_count_checkpoint = m_count;

  return count_per_second;
}

void IntervalCounter::count()
{
  m_count++;
}

UI::UI(Config& config):
  m_config(config),
  m_main_loops{},
  m_midi_events{},
  m_active_sensing_timestamp{},
  m_midi_timestamp{},
  m_note_bpm{},
  m_clock_bpm{}
{
}

void UI::draw()
{
  std::vector<int> cpuloads = get_cpu_loads();
  int main_loops_per_second = m_main_loops.get_count_per_second();
  int midi_events_per_second = m_midi_events.get_count_per_second();

  int mode = m_config.get_mode();
  int channel = m_config.get_midi_channel();
  int note = m_config.get_midi_note();
  int bpm = m_config.get_bpm();
  int output = m_config.get_output();

  int active_sensing_detected = (clock_type::now() - m_active_sensing_timestamp) < 2s ? 1 : 0;

  // clear screen
  std::cout << "\x1B[2J\x1B[H";
  //std::cout << std::endl;

  std::cout << fmt::format("- {:3} BPM +", bpm) << std::endl;
  std::cout << "Mode: ";
  for (int i = 0; i < mode_names.size(); ++i) {
    if (i == mode) {
      std::cout << fmt::format(fg(fmt::color::crimson) | fmt::emphasis::bold, " {}", mode_names[i]);
    } else {
      std::cout << fmt::format(" {}", mode_names[i]);
    }
  }
  std::cout << std::endl;
  std::cout << fmt::format("MIDI Note: {}/{}", channel, note);
  std::cout << std::endl;
  std::cout << "Audio Output: " << fmt::format(fg(fmt::color::crimson) | fmt::emphasis::bold, "{}", output_names[output]);
  std::cout << std::endl;
  std::cout << std::endl;
  std::cout << "Status:" << std::endl;

  std::cout << "  CPU:";
  for (auto& i: cpuloads) {
    std::cout << fmt::format(" {:2}%", i);
  }
  int max = *std::max_element(cpuloads.begin(), cpuloads.end());
  std::cout << fmt::format(", max. {:2}%", max) << std::endl;

  std::cout << "  MIDI Notes: ";
  if (m_midi_monitor.empty()) {
    std::cout << "--";
  } else {
    int count{};
    for (const auto& i: m_midi_monitor) {
      if (count == 0) {
        std::cout << fmt::format(fg(fmt::color::crimson) | fmt::emphasis::bold, " {}/{}", i.first, i.second);
      } else {
        std::cout << fmt::format(" {}/{}", i.first, i.second);
      }
      ++count;
    }
    if (count >= midi_monitor_max_size) {
      std::cout << " ...";
    }
  }
  std::cout << std::endl;
  std::cout << fmt::format("  MIDI Timestamp: {}", m_midi_timestamp) << std::endl;
  std::cout << "  MIDI Active Sensing: " << fmt::format(fg(fmt::color::blue) | fmt::emphasis::bold, "{}", active_sensing_names[active_sensing_detected]) << std::endl;
  std::cout << fmt::format("  MIDI Events/s: {}", midi_events_per_second) << std::endl;
  std::cout << fmt::format("  MIDI Clock: {:3} BPM", m_clock_bpm) << std::endl;
  std::cout << fmt::format("  MIDI Click: {:3} BPM", m_note_bpm) << std::endl;
  std::cout << fmt::format("  Internal:   {:3} BPM", bpm) << std::endl;

  std::cout << fmt::format("  Main loops/s: {}", main_loops_per_second) << std::endl;

  std::cout << "Log:" << std::endl;
  std::cout << log_cout.get_log() << std::endl;
}

void UI::count_main_loops()
{
  m_main_loops.count();
}

void UI::count_midi_events()
{
  m_midi_events.count();
}

void UI::slot_active_sensing()
{
  m_active_sensing_timestamp = clock_type::now();
}

void UI::slot_midi_note(int channel, int note, uint64_t timestamp)
{
  m_midi_timestamp = timestamp;

  m_midi_monitor.emplace_front(channel, note);

  while (m_midi_monitor.size() > midi_monitor_max_size) {
    m_midi_monitor.pop_back();
  }
}

void UI::slot_note_bpm(int bpm)
{
  m_note_bpm = bpm;
}

void UI::slot_clock_bpm(int bpm)
{
  m_clock_bpm = bpm;
}