1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 15:07:45 +00:00

Piano: Add sampler

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.
This commit is contained in:
William McPherson 2020-02-08 17:04:05 +11:00 committed by Andreas Kling
parent 591870c7b4
commit 9997b0dbf5
9 changed files with 317 additions and 35 deletions

View file

@ -67,6 +67,7 @@ enum Wave {
Square,
Saw,
Noise,
RecordedSample,
};
constexpr const char* wave_strings[] = {
@ -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,
@ -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;
@ -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;