Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "SampleWidget.h"
28#include <LibAudio/Buffer.h>
29#include <LibGUI/Painter.h>
30#include <LibM/math.h>
31
32SampleWidget::SampleWidget()
33{
34}
35
36SampleWidget::~SampleWidget()
37{
38}
39
40void SampleWidget::paint_event(GUI::PaintEvent& event)
41{
42 GUI::Frame::paint_event(event);
43 GUI::Painter painter(*this);
44
45 painter.add_clip_rect(event.rect());
46 painter.fill_rect(frame_inner_rect(), Color::Black);
47
48 float sample_max = 0;
49 int count = 0;
50 int x_offset = frame_inner_rect().x();
51 int x = x_offset;
52 int y_offset = frame_inner_rect().center().y();
53
54 if (m_buffer) {
55 int samples_per_pixel = m_buffer->sample_count() / frame_inner_rect().width();
56 for (int sample_index = 0; sample_index < m_buffer->sample_count() && (x - x_offset) < frame_inner_rect().width(); ++sample_index) {
57 float sample = fabsf((float)m_buffer->samples()[sample_index].left);
58
59 sample_max = max(sample, sample_max);
60 ++count;
61
62 if (count >= samples_per_pixel) {
63 Gfx::Point min_point = { x, y_offset + static_cast<int>(-sample_max * frame_inner_rect().height() / 2) };
64 Gfx::Point max_point = { x++, y_offset + static_cast<int>(sample_max * frame_inner_rect().height() / 2) };
65 painter.draw_line(min_point, max_point, Color::Green);
66
67 count = 0;
68 sample_max = 0;
69 }
70 }
71 } else {
72 painter.draw_line({ x, y_offset }, { frame_inner_rect().width(), y_offset }, Color::Green);
73 }
74}
75
76void SampleWidget::set_buffer(Audio::Buffer* buffer)
77{
78 if (m_buffer == buffer)
79 return;
80 m_buffer = buffer;
81 update();
82}