Skip to content

Commit

Permalink
Piano: Add sampler
Browse files Browse the repository at this point in the history
This commit adds basic support for importing, viewing and playing WAV
samples at different pitches.

Naming issues:
- We are using the Sample struct from Music.h, but also the Sample
  struct from LibAudio (Audio::Sample). This is a little confusing.

set_recorded_sample() finds the peak sample and then divides all the
samples by that peak to get a guaranteed min/max of -1/1. This is nice
because our other waves are also bound between these values and we can
just do the same stuff. This is why we're using Audio::Sample, because
it uses floats, whereas Music.h's Sample uses i16s. It's a little
annoying that we have to use a mixture of floats and doubles though.

For playback at lower frequencies, we're calculating in-between samples,
rather than just playing samples multiple times. Basically, you get the
current sample and add the difference between the current sample and the
next sample multiplied by the distance from the current sample. This is
like drawing the hypotenuse of a right-angled triangle.
  • Loading branch information
willmcpherson2 authored and awesomekling committed Feb 10, 2020
1 parent 591870c commit 9997b0d
Show file tree
Hide file tree
Showing 9 changed files with 317 additions and 35 deletions.
50 changes: 50 additions & 0 deletions Applications/Piano/AudioEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/

#include "AudioEngine.h"
#include <LibAudio/WavLoader.h>
#include <limits>
#include <math.h>

Expand Down Expand Up @@ -94,6 +95,9 @@ void AudioEngine::fill_buffer(FixedArray<Sample>& buffer)
case Wave::Noise:
val = (volume * m_power[note]) * noise();
break;
case Wave::RecordedSample:
val = (volume * m_power[note]) * recorded_sample(note);
break;
default:
ASSERT_NOT_REACHED();
}
Expand Down Expand Up @@ -143,6 +147,37 @@ void AudioEngine::reset()
m_previous_column = horizontal_notes - 1;
}

String AudioEngine::set_recorded_sample(const StringView& path)
{
Audio::WavLoader wav_loader(path);
if (wav_loader.has_error())
return String(wav_loader.error_string());
auto wav_buffer = wav_loader.get_more_samples(60 * sample_rate * sizeof(Sample)); // 1 minute maximum

if (!m_recorded_sample.is_empty())
m_recorded_sample.clear();
m_recorded_sample.resize(wav_buffer->sample_count());

float peak = 0;
for (int i = 0; i < wav_buffer->sample_count(); ++i) {
float left_abs = fabs(wav_buffer->samples()[i].left);
float right_abs = fabs(wav_buffer->samples()[i].right);
if (left_abs > peak)
peak = left_abs;
if (right_abs > peak)
peak = right_abs;
}

if (peak) {
for (int i = 0; i < wav_buffer->sample_count(); ++i) {
m_recorded_sample[i].left = wav_buffer->samples()[i].left / peak;
m_recorded_sample[i].right = wav_buffer->samples()[i].right / peak;
}
}

return String::empty();
}

// All of the information for these waves is on Wikipedia.

double AudioEngine::sine(size_t note)
Expand Down Expand Up @@ -188,6 +223,21 @@ double AudioEngine::noise() const
return w;
}

double AudioEngine::recorded_sample(size_t note)
{
int t = m_pos[note];
if (t >= m_recorded_sample.size())
return 0;
double w = m_recorded_sample[t].left;
if (t + 1 < m_recorded_sample.size()) {
double t_fraction = m_pos[note] - t;
w += (m_recorded_sample[t + 1].left - m_recorded_sample[t].left) * t_fraction;
}
double recorded_sample_step = note_frequencies[note] / middle_c;
m_pos[note] += recorded_sample_step;
return w;
}

static inline double calculate_step(double distance, int milliseconds)
{
if (milliseconds == 0)
Expand Down
6 changes: 6 additions & 0 deletions Applications/Piano/AudioEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <AK/FixedArray.h>
#include <AK/Noncopyable.h>
#include <AK/Queue.h>
#include <LibAudio/Buffer.h>

class AudioEngine {
AK_MAKE_NONCOPYABLE(AudioEngine)
Expand All @@ -40,6 +41,7 @@ class AudioEngine {
~AudioEngine();

const FixedArray<Sample>& buffer() const { return *m_front_buffer_ptr; }
const Vector<Audio::Sample>& recorded_sample() const { return m_recorded_sample; }
void reset();
Switch roll_note(int y, int x) const { return m_roll_notes[y][x]; }
int current_column() const { return m_current_column; }
Expand All @@ -55,6 +57,7 @@ class AudioEngine {
int tick() const { return m_tick; }

void fill_buffer(FixedArray<Sample>& buffer);
String set_recorded_sample(const StringView& path);
void set_note(int note, Switch);
void set_note_current_octave(int note, Switch);
void set_roll_note(int y, int x, Switch);
Expand All @@ -73,6 +76,7 @@ class AudioEngine {
double square(size_t note);
double triangle(size_t note);
double noise() const;
double recorded_sample(size_t note);

void update_roll();
void set_notes_from_roll();
Expand All @@ -86,6 +90,8 @@ class AudioEngine {

Queue<NonnullOwnPtr<FixedArray<Sample>>> m_delay_buffers;

Vector<Audio::Sample> m_recorded_sample;

u8 m_note_on[note_count] { 0 };
double m_power[note_count] { 0 };
double m_pos[note_count]; // Initialized lazily.
Expand Down
10 changes: 9 additions & 1 deletion Applications/Piano/MainWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
#include "KeysWidget.h"
#include "KnobsWidget.h"
#include "RollWidget.h"
#include "SamplerWidget.h"
#include "WaveWidget.h"
#include <LibGUI/BoxLayout.h>
#include <LibGUI/TabWidget.h>

MainWidget::MainWidget(AudioEngine& audio_engine)
: m_audio_engine(audio_engine)
Expand All @@ -45,10 +47,16 @@ MainWidget::MainWidget(AudioEngine& audio_engine)
m_wave_widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
m_wave_widget->set_preferred_size(0, 100);

m_roll_widget = RollWidget::construct(this, audio_engine);
m_roll_widget = RollWidget::construct(nullptr, audio_engine);
m_roll_widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
m_roll_widget->set_preferred_size(0, 300);

m_sampler_widget = SamplerWidget::construct(nullptr, audio_engine);

m_tab_widget = GUI::TabWidget::construct(this);
m_tab_widget->add_widget("Piano Roll", m_roll_widget);
m_tab_widget->add_widget("Sampler", m_sampler_widget);

m_keys_and_knobs_container = GUI::Widget::construct(this);
m_keys_and_knobs_container->set_layout(make<GUI::HorizontalBoxLayout>());
m_keys_and_knobs_container->layout()->set_spacing(2);
Expand Down
7 changes: 7 additions & 0 deletions Applications/Piano/MainWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@
class AudioEngine;
class WaveWidget;
class RollWidget;
class SamplerWidget;
class KeysWidget;
class KnobsWidget;

namespace GUI {
class TabWidget;
}

class MainWidget final : public GUI::Widget {
C_OBJECT(MainWidget)
public:
Expand All @@ -57,6 +62,8 @@ class MainWidget final : public GUI::Widget {

RefPtr<WaveWidget> m_wave_widget;
RefPtr<RollWidget> m_roll_widget;
RefPtr<SamplerWidget> m_sampler_widget;
RefPtr<GUI::TabWidget> m_tab_widget;
RefPtr<GUI::Widget> m_keys_and_knobs_container;
RefPtr<KeysWidget> m_keys_widget;
RefPtr<KnobsWidget> m_knobs_widget;
Expand Down
1 change: 1 addition & 0 deletions Applications/Piano/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ OBJS = \
MainWidget.o \
WaveWidget.o \
RollWidget.o \
SamplerWidget.o \
KeysWidget.o \
KnobsWidget.o \
main.o
Expand Down
45 changes: 44 additions & 1 deletion Applications/Piano/Music.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ enum Wave {
Square,
Saw,
Noise,
RecordedSample,
};

constexpr const char* wave_strings[] = {
Expand All @@ -75,10 +76,11 @@ constexpr const char* wave_strings[] = {
"Square",
"Saw",
"Noise",
"Sample",
};

constexpr int first_wave = Sine;
constexpr int last_wave = Noise;
constexpr int last_wave = RecordedSample;

enum Envelope {
Done,
Expand Down Expand Up @@ -110,6 +112,45 @@ constexpr KeyColor key_pattern[] = {
const Color note_pressed_color(64, 64, 255);
const Color column_playing_color(128, 128, 255);

const Color wave_colors[] = {
// Sine
{
255,
192,
0,
},
// Triangle
{
35,
171,
35,
},
// Square
{
128,
160,
255,
},
// Saw
{
240,
100,
128,
},
// Noise
{
197,
214,
225,
},
// RecordedSample
{
227,
39,
39,
},
};

constexpr int notes_per_octave = 12;
constexpr int white_keys_per_octave = 7;
constexpr int black_keys_per_octave = 5;
Expand Down Expand Up @@ -217,6 +258,8 @@ constexpr double note_frequencies[] = {
};
constexpr int note_count = sizeof(note_frequencies) / sizeof(double);

constexpr double middle_c = note_frequencies[36];

}

using namespace Music;
133 changes: 133 additions & 0 deletions Applications/Piano/SamplerWidget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright (c) 2020-2020, William McPherson <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "SamplerWidget.h"
#include "AudioEngine.h"
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
#include <LibGUI/FilePicker.h>
#include <LibGUI/Label.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Painter.h>

WaveEditor::WaveEditor(GUI::Widget* parent, AudioEngine& audio_engine)
: GUI::Frame(parent)
, m_audio_engine(audio_engine)
{
set_frame_thickness(2);
set_frame_shadow(Gfx::FrameShadow::Sunken);
set_frame_shape(Gfx::FrameShape::Container);
}

WaveEditor::~WaveEditor()
{
}

int WaveEditor::sample_to_y(float percentage) const
{
double portion_of_half_height = percentage * ((frame_inner_rect().height() - 1) / 2.0);
double y = (frame_inner_rect().height() / 2.0) + portion_of_half_height;
return y;
}

void WaveEditor::paint_event(GUI::PaintEvent& event)
{
GUI::Frame::paint_event(event);

GUI::Painter painter(*this);
painter.fill_rect(frame_inner_rect(), Color::Black);

auto recorded_sample = m_audio_engine.recorded_sample();
if (recorded_sample.is_empty())
return;

double width_scale = static_cast<double>(frame_inner_rect().width()) / recorded_sample.size();

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]);

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

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

prev_x = x;
prev_y = y;
}
}

SamplerWidget::SamplerWidget(GUI::Widget* parent, AudioEngine& audio_engine)
: GUI::Frame(parent)
, m_audio_engine(audio_engine)
{
set_frame_thickness(2);
set_frame_shadow(Gfx::FrameShadow::Sunken);
set_frame_shape(Gfx::FrameShape::Container);
set_layout(make<GUI::VerticalBoxLayout>());
layout()->set_margins({ 10, 10, 10, 10 });
layout()->set_spacing(10);
set_fill_with_background_color(true);

m_open_button_and_recorded_sample_name_container = GUI::Widget::construct(this);
m_open_button_and_recorded_sample_name_container->set_layout(make<GUI::HorizontalBoxLayout>());
m_open_button_and_recorded_sample_name_container->layout()->set_spacing(10);
m_open_button_and_recorded_sample_name_container->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
m_open_button_and_recorded_sample_name_container->set_preferred_size(0, 24);

m_open_button = GUI::Button::construct(m_open_button_and_recorded_sample_name_container);
m_open_button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
m_open_button->set_preferred_size(24, 24);
m_open_button->set_focusable(false);
m_open_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"));
m_open_button->on_click = [this](const auto&) {
Optional<String> open_path = GUI::FilePicker::get_open_filepath();
if (!open_path.has_value())
return;
String error_string = m_audio_engine.set_recorded_sample(open_path.value());
if (!error_string.is_empty()) {
GUI::MessageBox::show(String::format("Failed to load WAV file: %s", error_string.characters()), "Error", GUI::MessageBox::Type::Error);
return;
}
m_recorded_sample_name->set_text(open_path.value());
m_wave_editor->update();
};

m_recorded_sample_name = GUI::Label::construct("No sample loaded", m_open_button_and_recorded_sample_name_container);
m_recorded_sample_name->set_text_alignment(Gfx::TextAlignment::CenterLeft);

m_wave_editor = WaveEditor::construct(this, m_audio_engine);
m_wave_editor->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
m_wave_editor->set_preferred_size(0, 100);
}

SamplerWidget::~SamplerWidget()
{
}
Loading

0 comments on commit 9997b0d

Please sign in to comment.