mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:27: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:
parent
591870c7b4
commit
9997b0dbf5
9 changed files with 317 additions and 35 deletions
|
@ -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)
|
||||
|
@ -40,6 +41,7 @@ public:
|
|||
~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; }
|
||||
|
@ -55,6 +57,7 @@ public:
|
|||
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);
|
||||
|
@ -73,6 +76,7 @@ private:
|
|||
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();
|
||||
|
@ -86,6 +90,8 @@ private:
|
|||
|
||||
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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue