diff options
author | Peter Elliott <pelliott@ualberta.ca> | 2020-10-10 14:06:36 -0600 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-10-12 19:41:53 +0200 |
commit | 27b990ec19f490e078e6c19ca803fd80cb13d185 (patch) | |
tree | bc8b884f1b487ab7ceb80b8fddfc90324118c7d0 /Applications | |
parent | 01bff0141e5410673b2dc9aa20399b178fa6cece (diff) | |
download | serenity-27b990ec19f490e078e6c19ca803fd80cb13d185.zip |
Piano: Add note names to RollWidget
Diffstat (limited to 'Applications')
-rw-r--r-- | Applications/Piano/Music.h | 15 | ||||
-rw-r--r-- | Applications/Piano/RollWidget.cpp | 10 |
2 files changed, 24 insertions, 1 deletions
diff --git a/Applications/Piano/Music.h b/Applications/Piano/Music.h index c2736c067e..3a150944ec 100644 --- a/Applications/Piano/Music.h +++ b/Applications/Piano/Music.h @@ -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) diff --git a/Applications/Piano/RollWidget.cpp b/Applications/Piano/RollWidget.cpp index 255c5d0cbe..d8d0aa670e 100644 --- a/Applications/Piano/RollWidget.cpp +++ b/Applications/Piano/RollWidget.cpp @@ -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); |