mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:27:44 +00:00
Piano: Draw stereo waves
Draw two waves in different colors.
This commit is contained in:
parent
93903c8064
commit
4ad96df0d4
3 changed files with 72 additions and 16 deletions
|
@ -112,7 +112,7 @@ constexpr KeyColor key_pattern[] = {
|
||||||
const Color note_pressed_color(64, 64, 255);
|
const Color note_pressed_color(64, 64, 255);
|
||||||
const Color column_playing_color(128, 128, 255);
|
const Color column_playing_color(128, 128, 255);
|
||||||
|
|
||||||
const Color wave_colors[] = {
|
const Color left_wave_colors[] = {
|
||||||
// Sine
|
// Sine
|
||||||
{
|
{
|
||||||
255,
|
255,
|
||||||
|
@ -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 notes_per_octave = 12;
|
||||||
constexpr int white_keys_per_octave = 7;
|
constexpr int white_keys_per_octave = 7;
|
||||||
constexpr int black_keys_per_octave = 5;
|
constexpr int black_keys_per_octave = 5;
|
||||||
|
|
|
@ -69,18 +69,26 @@ void WaveEditor::paint_event(GUI::PaintEvent& event)
|
||||||
painter.translate(frame_thickness(), frame_thickness());
|
painter.translate(frame_thickness(), frame_thickness());
|
||||||
|
|
||||||
int prev_x = 0;
|
int prev_x = 0;
|
||||||
int prev_y = sample_to_y(recorded_sample[0].left);
|
int left_prev_y = sample_to_y(recorded_sample[0].left);
|
||||||
painter.set_pixel({ prev_x, prev_y }, wave_colors[RecordedSample]);
|
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) {
|
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 left_point1(prev_x * width_scale, left_prev_y);
|
||||||
Gfx::Point point2(x * width_scale, y);
|
Gfx::Point left_point2(x * width_scale, left_y);
|
||||||
painter.draw_line(point1, point2, wave_colors[RecordedSample]);
|
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_x = x;
|
||||||
prev_y = y;
|
left_prev_y = left_y;
|
||||||
|
right_prev_y = right_y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -60,23 +60,32 @@ void WaveWidget::paint_event(GUI::PaintEvent& event)
|
||||||
painter.fill_rect(frame_inner_rect(), Color::Black);
|
painter.fill_rect(frame_inner_rect(), Color::Black);
|
||||||
painter.translate(frame_thickness(), frame_thickness());
|
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();
|
auto buffer = m_audio_engine.buffer();
|
||||||
double width_scale = static_cast<double>(frame_inner_rect().width()) / buffer.size();
|
double width_scale = static_cast<double>(frame_inner_rect().width()) / buffer.size();
|
||||||
|
|
||||||
int prev_x = 0;
|
int prev_x = 0;
|
||||||
int prev_y = sample_to_y(buffer[0].left);
|
int prev_y_left = sample_to_y(buffer[0].left);
|
||||||
painter.set_pixel({ prev_x, prev_y }, wave_color);
|
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) {
|
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 point1_left(prev_x * width_scale, prev_y_left);
|
||||||
Gfx::Point point2(x * width_scale, y);
|
Gfx::Point point2_left(x * width_scale, y_left);
|
||||||
painter.draw_line(point1, point2, wave_color);
|
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_x = x;
|
||||||
prev_y = y;
|
prev_y_left = y_left;
|
||||||
|
prev_y_right = y_right;
|
||||||
}
|
}
|
||||||
|
|
||||||
GUI::Frame::paint_event(event);
|
GUI::Frame::paint_event(event);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue