Serenity Operating System
1/*
2 * Copyright (c) 2020-2020, William McPherson <willmcpherson2@gmail.com>
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 "SamplerWidget.h"
28#include "AudioEngine.h"
29#include <LibGUI/BoxLayout.h>
30#include <LibGUI/Button.h>
31#include <LibGUI/FilePicker.h>
32#include <LibGUI/Label.h>
33#include <LibGUI/MessageBox.h>
34#include <LibGUI/Painter.h>
35
36WaveEditor::WaveEditor(AudioEngine& audio_engine)
37 : m_audio_engine(audio_engine)
38{
39}
40
41WaveEditor::~WaveEditor()
42{
43}
44
45int WaveEditor::sample_to_y(double percentage) const
46{
47 double portion_of_half_height = percentage * ((frame_inner_rect().height() - 1) / 2.0);
48 double y = (frame_inner_rect().height() / 2.0) + portion_of_half_height;
49 return y;
50}
51
52void WaveEditor::paint_event(GUI::PaintEvent& event)
53{
54 GUI::Frame::paint_event(event);
55
56 GUI::Painter painter(*this);
57 painter.fill_rect(frame_inner_rect(), Color::Black);
58
59 auto recorded_sample = m_audio_engine.recorded_sample();
60 if (recorded_sample.is_empty())
61 return;
62
63 double width_scale = static_cast<double>(frame_inner_rect().width()) / recorded_sample.size();
64
65 painter.translate(frame_thickness(), frame_thickness());
66
67 int prev_x = 0;
68 int left_prev_y = sample_to_y(recorded_sample[0].left);
69 int right_prev_y = sample_to_y(recorded_sample[0].right);
70 painter.set_pixel({ prev_x, left_prev_y }, left_wave_colors[RecordedSample]);
71 painter.set_pixel({ prev_x, right_prev_y }, right_wave_colors[RecordedSample]);
72
73 for (size_t x = 1; x < recorded_sample.size(); ++x) {
74 int left_y = sample_to_y(recorded_sample[x].left);
75 int right_y = sample_to_y(recorded_sample[x].right);
76
77 Gfx::Point left_point1(prev_x * width_scale, left_prev_y);
78 Gfx::Point left_point2(x * width_scale, left_y);
79 painter.draw_line(left_point1, left_point2, left_wave_colors[RecordedSample]);
80
81 Gfx::Point right_point1(prev_x * width_scale, right_prev_y);
82 Gfx::Point right_point2(x * width_scale, right_y);
83 painter.draw_line(right_point1, right_point2, right_wave_colors[RecordedSample]);
84
85 prev_x = x;
86 left_prev_y = left_y;
87 right_prev_y = right_y;
88 }
89}
90
91SamplerWidget::SamplerWidget(AudioEngine& audio_engine)
92 : m_audio_engine(audio_engine)
93{
94 set_layout<GUI::VerticalBoxLayout>();
95 layout()->set_margins({ 10, 10, 10, 10 });
96 layout()->set_spacing(10);
97 set_fill_with_background_color(true);
98
99 m_open_button_and_recorded_sample_name_container = add<GUI::Widget>();
100 m_open_button_and_recorded_sample_name_container->set_layout<GUI::HorizontalBoxLayout>();
101 m_open_button_and_recorded_sample_name_container->layout()->set_spacing(10);
102 m_open_button_and_recorded_sample_name_container->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
103 m_open_button_and_recorded_sample_name_container->set_preferred_size(0, 24);
104
105 m_open_button = m_open_button_and_recorded_sample_name_container->add<GUI::Button>();
106 m_open_button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
107 m_open_button->set_preferred_size(24, 24);
108 m_open_button->set_focusable(false);
109 m_open_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"));
110 m_open_button->on_click = [this] {
111 Optional<String> open_path = GUI::FilePicker::get_open_filepath();
112 if (!open_path.has_value())
113 return;
114 String error_string = m_audio_engine.set_recorded_sample(open_path.value());
115 if (!error_string.is_empty()) {
116 GUI::MessageBox::show(String::format("Failed to load WAV file: %s", error_string.characters()), "Error", GUI::MessageBox::Type::Error);
117 return;
118 }
119 m_recorded_sample_name->set_text(open_path.value());
120 m_wave_editor->update();
121 };
122
123 m_recorded_sample_name = m_open_button_and_recorded_sample_name_container->add<GUI::Label>("No sample loaded");
124 m_recorded_sample_name->set_text_alignment(Gfx::TextAlignment::CenterLeft);
125
126 m_wave_editor = add<WaveEditor>(m_audio_engine);
127 m_wave_editor->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
128 m_wave_editor->set_preferred_size(0, 100);
129}
130
131SamplerWidget::~SamplerWidget()
132{
133}