Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2019-2020, William McPherson <willmcpherson2@gmail.com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "WaveWidget.h"
29#include "AudioEngine.h"
30#include <LibGUI/Painter.h>
31#include <limits>
32
33WaveWidget::WaveWidget(AudioEngine& audio_engine)
34 : m_audio_engine(audio_engine)
35{
36}
37
38WaveWidget::~WaveWidget()
39{
40}
41
42int WaveWidget::sample_to_y(int sample) const
43{
44 constexpr int nice_scale_factor = 4;
45 sample *= nice_scale_factor;
46 constexpr double sample_max = std::numeric_limits<i16>::max();
47 double percentage = sample / sample_max;
48 double portion_of_half_height = percentage * ((frame_inner_rect().height() - 1) / 2.0);
49 double y = (frame_inner_rect().height() / 2.0) + portion_of_half_height;
50 return y;
51}
52
53void WaveWidget::paint_event(GUI::PaintEvent& event)
54{
55 GUI::Painter painter(*this);
56 painter.fill_rect(frame_inner_rect(), Color::Black);
57 painter.translate(frame_thickness(), frame_thickness());
58
59 Color left_wave_color = left_wave_colors[m_audio_engine.wave()];
60 Color right_wave_color = right_wave_colors[m_audio_engine.wave()];
61 auto buffer = m_audio_engine.buffer();
62 double width_scale = static_cast<double>(frame_inner_rect().width()) / buffer.size();
63
64 int prev_x = 0;
65 int prev_y_left = sample_to_y(buffer[0].left);
66 int prev_y_right = sample_to_y(buffer[0].right);
67 painter.set_pixel({ prev_x, prev_y_left }, left_wave_color);
68 painter.set_pixel({ prev_x, prev_y_right }, right_wave_color);
69
70 for (size_t x = 1; x < buffer.size(); ++x) {
71 int y_left = sample_to_y(buffer[x].left);
72 int y_right = sample_to_y(buffer[x].right);
73
74 Gfx::Point point1_left(prev_x * width_scale, prev_y_left);
75 Gfx::Point point2_left(x * width_scale, y_left);
76 painter.draw_line(point1_left, point2_left, left_wave_color);
77
78 Gfx::Point point1_right(prev_x * width_scale, prev_y_right);
79 Gfx::Point point2_right(x * width_scale, y_right);
80 painter.draw_line(point1_right, point2_right, right_wave_color);
81
82 prev_x = x;
83 prev_y_left = y_left;
84 prev_y_right = y_right;
85 }
86
87 GUI::Frame::paint_event(event);
88}