Skip to content

Commit

Permalink
Piano: Draw stereo waves
Browse files Browse the repository at this point in the history
Draw two waves in different colors.
  • Loading branch information
willmcpherson2 authored and awesomekling committed Feb 10, 2020
1 parent 93903c8 commit 4ad96df
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 16 deletions.
41 changes: 40 additions & 1 deletion Applications/Piano/Music.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ constexpr KeyColor key_pattern[] = {
const Color note_pressed_color(64, 64, 255);
const Color column_playing_color(128, 128, 255);

const Color wave_colors[] = {
const Color left_wave_colors[] = {
// Sine
{
255,
Expand Down Expand Up @@ -151,6 +151,45 @@ const Color wave_colors[] = {
},
};

const Color right_wave_colors[] = {
// Sine
{
255,
223,
0,
},
// Triangle
{
35,
171,
90,
},
// Square
{
139,
128,
255,
},
// Saw
{
240,
100,
220,
},
// Noise
{
197,
223,
225,
},
// RecordedSample
{
227,
105,
39,
},
};

constexpr int notes_per_octave = 12;
constexpr int white_keys_per_octave = 7;
constexpr int black_keys_per_octave = 5;
Expand Down
22 changes: 15 additions & 7 deletions Applications/Piano/SamplerWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,26 @@ void WaveEditor::paint_event(GUI::PaintEvent& event)
painter.translate(frame_thickness(), frame_thickness());

int prev_x = 0;
int prev_y = sample_to_y(recorded_sample[0].left);
painter.set_pixel({ prev_x, prev_y }, wave_colors[RecordedSample]);
int left_prev_y = sample_to_y(recorded_sample[0].left);
int right_prev_y = sample_to_y(recorded_sample[0].right);
painter.set_pixel({ prev_x, left_prev_y }, left_wave_colors[RecordedSample]);
painter.set_pixel({ prev_x, right_prev_y }, right_wave_colors[RecordedSample]);

for (int x = 1; x < recorded_sample.size(); ++x) {
int y = sample_to_y(recorded_sample[x].left);
int left_y = sample_to_y(recorded_sample[x].left);
int right_y = sample_to_y(recorded_sample[x].right);

Gfx::Point point1(prev_x * width_scale, prev_y);
Gfx::Point point2(x * width_scale, y);
painter.draw_line(point1, point2, wave_colors[RecordedSample]);
Gfx::Point left_point1(prev_x * width_scale, left_prev_y);
Gfx::Point left_point2(x * width_scale, left_y);
painter.draw_line(left_point1, left_point2, left_wave_colors[RecordedSample]);

Gfx::Point right_point1(prev_x * width_scale, right_prev_y);
Gfx::Point right_point2(x * width_scale, right_y);
painter.draw_line(right_point1, right_point2, right_wave_colors[RecordedSample]);

prev_x = x;
prev_y = y;
left_prev_y = left_y;
right_prev_y = right_y;
}
}

Expand Down
25 changes: 17 additions & 8 deletions Applications/Piano/WaveWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,32 @@ void WaveWidget::paint_event(GUI::PaintEvent& event)
painter.fill_rect(frame_inner_rect(), Color::Black);
painter.translate(frame_thickness(), frame_thickness());

Color wave_color = wave_colors[m_audio_engine.wave()];
Color left_wave_color = left_wave_colors[m_audio_engine.wave()];
Color right_wave_color = right_wave_colors[m_audio_engine.wave()];
auto buffer = m_audio_engine.buffer();
double width_scale = static_cast<double>(frame_inner_rect().width()) / buffer.size();

int prev_x = 0;
int prev_y = sample_to_y(buffer[0].left);
painter.set_pixel({ prev_x, prev_y }, wave_color);
int prev_y_left = sample_to_y(buffer[0].left);
int prev_y_right = sample_to_y(buffer[0].right);
painter.set_pixel({ prev_x, prev_y_left }, left_wave_color);
painter.set_pixel({ prev_x, prev_y_right }, right_wave_color);

for (size_t x = 1; x < buffer.size(); ++x) {
int y = sample_to_y(buffer[x].left);
int y_left = sample_to_y(buffer[x].left);
int y_right = sample_to_y(buffer[x].right);

Gfx::Point point1(prev_x * width_scale, prev_y);
Gfx::Point point2(x * width_scale, y);
painter.draw_line(point1, point2, wave_color);
Gfx::Point point1_left(prev_x * width_scale, prev_y_left);
Gfx::Point point2_left(x * width_scale, y_left);
painter.draw_line(point1_left, point2_left, left_wave_color);

Gfx::Point point1_right(prev_x * width_scale, prev_y_right);
Gfx::Point point2_right(x * width_scale, y_right);
painter.draw_line(point1_right, point2_right, right_wave_color);

prev_x = x;
prev_y = y;
prev_y_left = y_left;
prev_y_right = y_right;
}

GUI::Frame::paint_event(event);
Expand Down

0 comments on commit 4ad96df

Please sign in to comment.