Piano: Add note names to RollWidget

This commit is contained in:
Peter Elliott 2020-10-10 14:06:36 -06:00 committed by Andreas Kling
parent 01bff0141e
commit 27b990ec19
Notes: sideshowbarker 2024-07-19 01:55:49 +09:00
2 changed files with 24 additions and 1 deletions

View file

@ -208,6 +208,21 @@ constexpr int beats_per_bar = 4;
constexpr int notes_per_beat = 4;
constexpr int roll_length = (sample_rate / (beats_per_minute / 60)) * beats_per_bar;
constexpr const char* note_names[] = {
"C",
"C#",
"D",
"D#",
"E",
"F",
"F#",
"G",
"G#",
"A",
"A#",
"B",
};
// Equal temperament, A = 440Hz
// We calculate note frequencies relative to A4:
// 440.0 * pow(pow(2.0, 1.0 / 12.0), N)

View file

@ -29,6 +29,7 @@
#include "TrackManager.h"
#include <LibGUI/Painter.h>
#include <LibGUI/ScrollBar.h>
#include <LibGfx/Font.h>
#include <math.h>
constexpr int note_height = 20;
@ -122,6 +123,7 @@ void RollWidget::paint_event(GUI::PaintEvent& event)
painter.translate(horizontal_note_offset_remainder, note_offset_remainder);
for (int note = note_count - (note_offset + notes_to_paint); note <= (note_count - 1) - note_offset; ++note) {
int y = ((note_count - 1) - note) * note_height;
for (auto roll_note : m_track_manager.current_track().roll_notes(note)) {
int x = m_roll_width * (static_cast<double>(roll_note.on_sample) / roll_length);
int width = m_roll_width * (static_cast<double>(roll_note.length()) / roll_length);
@ -130,13 +132,19 @@ void RollWidget::paint_event(GUI::PaintEvent& event)
if (width < 2)
width = 2;
int y = ((note_count - 1) - note) * note_height;
int height = note_height;
Gfx::IntRect rect(x, y, width, height);
painter.fill_rect(rect, note_pressed_color);
painter.draw_rect(rect, Color::Black);
}
Gfx::IntRect note_name_rect(3, y, 1, note_height);
const char* note_name = note_names[note % notes_per_octave];
painter.draw_text(note_name_rect, note_name, Gfx::TextAlignment::CenterLeft);
note_name_rect.move_by(Gfx::Font::default_font().width(note_name) + 2, 0);
if (note % notes_per_octave == 0)
painter.draw_text(note_name_rect, String::formatted("{}", note / notes_per_octave + 1), Gfx::TextAlignment::CenterLeft);
}
int x = m_roll_width * (static_cast<double>(m_track_manager.time()) / roll_length);