Implement Sound::FromNoise()
This commit is contained in:
@@ -78,6 +78,9 @@ namespace ReMixer {
|
||||
/// @see https://en.wikipedia.org/wiki/Square_wave
|
||||
static Sound FromSquareWave(float length, float wavelength, float amplitude, float phase);
|
||||
|
||||
|
||||
static Sound FromNoise(float length, float amplitude = 1.f);
|
||||
|
||||
/// Fills this sound instance with data from a file encoded with OGG vorbis, usually with an .ogg extension.
|
||||
/// @param file_name The full path relative to the executable in which the file is located.
|
||||
/// @see https://en.wikipedia.org/wiki/Vorbis
|
||||
@@ -105,6 +108,8 @@ namespace ReMixer {
|
||||
void LoadTriangleWave(float length, float wavelength, float amplitude, float phase);
|
||||
void LoadSquareWave(float length, float wavelength, float amplitude, float phase);
|
||||
|
||||
void LoadNoise(float length, float amplitude = 1.f);
|
||||
|
||||
/// Returns the sample count of the sound.
|
||||
uint SampleCount() const;;
|
||||
|
||||
|
2
main.cpp
2
main.cpp
@@ -45,11 +45,13 @@
|
||||
Sound sawtooth_sound = Sound::FromSawtoothWave(5.f, 0.025f, 2000, 0);
|
||||
Sound triangle_sound = Sound::FromTriangleWave(5.f, 0.025f, 2000, 0);
|
||||
Sound square_sound = Sound::FromSquareWave(5.f, 0.025f, 2000, 0);
|
||||
Sound noise_sound = Sound::FromNoise(5.f, 0.01);
|
||||
|
||||
server.Play(test_stream, sine_sound);
|
||||
server.Play(test_stream, sawtooth_sound);
|
||||
server.Play(test_stream, triangle_sound);
|
||||
server.Play(test_stream, square_sound);
|
||||
server.Play(test_stream, noise_sound);
|
||||
server.Play(test_stream, test_sound);
|
||||
|
||||
while (true) {
|
||||
|
@@ -3,8 +3,7 @@
|
||||
void ReMixer::PulseAudioSubsystem::Play(ReMixer::PulseStream &stream, const ReMixer::Sound &sound) {
|
||||
|
||||
// Check parameters and compatibility between stream and sound.
|
||||
if (stream.SampleRate() != sound.SampleRate())
|
||||
{
|
||||
if (stream.SampleRate() != sound.SampleRate()) {
|
||||
std::cerr << "There is an incongruency in sample rate between the stream and the sound file. "
|
||||
<< std::format("Stream: {}hz, meanwhile sound: {}hz", stream.SampleRate(), sound.SampleRate())
|
||||
<< std::endl;
|
||||
|
@@ -265,7 +265,6 @@ void ReMixer::Sound::LoadSquareWave(float length, float wavelength, float amplit
|
||||
|
||||
audio_data.push_back(low_half);
|
||||
audio_data.push_back(high_half);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -293,6 +292,35 @@ ReMixer::Sound ReMixer::Sound::FromSquareWave(float length, float wavelength, fl
|
||||
return sound;
|
||||
}
|
||||
|
||||
void ReMixer::Sound::LoadNoise(float length, float amplitude) {
|
||||
stream_mode = StreamMode::MONO;
|
||||
sample_rate = 44.1_kHz;
|
||||
|
||||
uint samples = length * SampleRate();
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
for (int i = 0; i < samples; i++) {
|
||||
float time = (float) i / (float) sample_rate;
|
||||
|
||||
float wave = (rand() % 65535) - 32000;
|
||||
|
||||
short sample = (short) (wave*amplitude);
|
||||
char low_half = sample;
|
||||
char high_half = (sample >> 8);
|
||||
|
||||
audio_data.push_back(low_half);
|
||||
audio_data.push_back(high_half);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
ReMixer::Sound ReMixer::Sound::FromNoise(float length, float amplitude) {
|
||||
Sound sound;
|
||||
sound.LoadNoise(length, amplitude);
|
||||
return sound;
|
||||
}
|
||||
|
||||
int mix(int a, int b) {
|
||||
return std::clamp(a + b, -128, 128);
|
||||
}
|
||||
|
Reference in New Issue
Block a user