-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify sound system to use single AudioStream
- Loading branch information
Showing
4 changed files
with
152 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,59 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <raylib.h> | ||
#include <array> | ||
#include <vector> | ||
|
||
constexpr int kMaxSamplesPerUpdate = 4096; | ||
constexpr int kMaxSamplesPerUpdate = 1024; | ||
constexpr int kAudioSampleRate = 44100; | ||
constexpr int kAudioSampleSize = 16; | ||
constexpr int kAudioNumChannels = 1; | ||
|
||
class SoundSource { | ||
public: | ||
SoundSource(); | ||
virtual ~SoundSource(); | ||
virtual ~SoundSource() = default; | ||
|
||
virtual void render() = 0; | ||
virtual void gen_sound_data(bool play_sound, short buffer[], size_t buffer_size) = 0; | ||
virtual void update(bool play_sound, double time) = 0; | ||
|
||
void update(bool play_sound); | ||
auto get_samples() const { | ||
return samples.data(); | ||
} | ||
|
||
private: | ||
AudioStream stream; | ||
std::array<short, kMaxSamplesPerUpdate> buffer; | ||
protected: | ||
std::array<float, kMaxSamplesPerUpdate> samples; | ||
}; | ||
|
||
class WaveGeneratorSource : public SoundSource { | ||
class WaveGeneratorSource final : public SoundSource { | ||
public: | ||
virtual ~WaveGeneratorSource() = default; | ||
|
||
virtual void render() override; | ||
virtual void gen_sound_data(bool play_sound, short buffer[], size_t buffer_size) override; | ||
virtual void update(bool play_sound, double time) override; | ||
|
||
private: | ||
std::array<float, 1000> samples; | ||
bool force_play = false; | ||
float frequency = 440.0f; | ||
float volume = 100.0f; | ||
float wave_idx = 0.0f; | ||
float offset = 0.0f; | ||
float volume = 50.0f; | ||
int wave_type = 0; | ||
}; | ||
|
||
class SoundManager final { | ||
public: | ||
void initialize(); | ||
void render(); | ||
void update(bool play_sound); | ||
void cleanup(); | ||
|
||
void add_source(std::unique_ptr<SoundSource> source); | ||
void remove_source_at(size_t index); | ||
|
||
private: | ||
double time = 0; | ||
AudioStream stream; | ||
std::array<float, kMaxSamplesPerUpdate> samples; | ||
std::array<short, kMaxSamplesPerUpdate> buffer; | ||
std::vector<std::unique_ptr<SoundSource>> sources; | ||
}; |