Skip to content

Commit

Permalink
Piano: Add note names to RollWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
petelliott authored and awesomekling committed Oct 12, 2020
1 parent 01bff01 commit 27b990e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Applications/Piano/Music.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion Applications/Piano/RollWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 27b990e

Please sign in to comment.