Update synth sine for sample rate

feature_setBlockSize
Chip Audette 8 years ago
parent baa0a36268
commit f9c8926efb
  1. 8
      synth_sine_f32.cpp
  2. 37
      synth_sine_f32.h

@ -24,13 +24,16 @@ void AudioSynthWaveformSine_F32::update(void)
audio_block_f32_t *block; audio_block_f32_t *block;
uint32_t i, ph, inc, index, scale; uint32_t i, ph, inc, index, scale;
int32_t val1, val2; int32_t val1, val2;
static uint32_t block_length = 0;
if (enabled) {
if (magnitude) { if (magnitude) {
block = allocate_f32(); block = allocate_f32();
if (block) { if (block) {
block_length = (uint32_t)block->length;
ph = phase_accumulator; ph = phase_accumulator;
inc = phase_increment; inc = phase_increment;
for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) { for (i=0; i < block_length; i++) {
index = ph >> 24; index = ph >> 24;
val1 = AudioWaveformSine[index]; val1 = AudioWaveformSine[index];
val2 = AudioWaveformSine[index+1]; val2 = AudioWaveformSine[index+1];
@ -55,7 +58,8 @@ void AudioSynthWaveformSine_F32::update(void)
return; return;
} }
} }
phase_accumulator += phase_increment * AUDIO_BLOCK_SAMPLES; phase_accumulator += phase_increment * block_length;
}
} }

@ -21,31 +21,44 @@
class AudioSynthWaveformSine_F32 : public AudioStream_F32 class AudioSynthWaveformSine_F32 : public AudioStream_F32
{ {
//GUI: inputs:0, outputs:1 //this line used for automatic generation of GUI node //GUI: inputs:0, outputs:1 //this line used for automatic generation of GUI node
//GUI: shortName:sine //this line used for automatic generation of GUI node
public: public:
AudioSynthWaveformSine_F32() : AudioStream_F32(0, NULL), magnitude(16384) { } AudioSynthWaveformSine_F32() : AudioStream_F32(0, NULL), magnitude(16384) { }
AudioSynthWaveformSine_F32(const AudioSettings_F32 &settings) : AudioStream_F32(0, NULL), magnitude(16384) {
setSampleRate_Hz(settings.sample_rate_Hz);
}
void frequency(float freq) { void frequency(float freq) {
if (freq < 0.0) freq = 0.0; if (freq < 0.0) freq = 0.0;
else if (freq > AUDIO_SAMPLE_RATE_EXACT/2) freq = AUDIO_SAMPLE_RATE_EXACT/2; else if (freq > sample_rate_Hz/2.f) freq = sample_rate_Hz/2.f;
phase_increment = freq * (4294967296.0 / AUDIO_SAMPLE_RATE_EXACT); phase_increment = freq * (4294967296.0 / sample_rate_Hz);
} }
void phase(float angle) { void phase(float angle) {
if (angle < 0.0) angle = 0.0; if (angle < 0.0f) angle = 0.0f;
else if (angle > 360.0) { else if (angle > 360.0f) {
angle = angle - 360.0; angle = angle - 360.0f;
if (angle >= 360.0) return; if (angle >= 360.0f) return;
} }
phase_accumulator = angle * (4294967296.0 / 360.0); phase_accumulator = angle * (4294967296.0f / 360.0f);
} }
void amplitude(float n) { void amplitude(float n) {
if (n < 0) n = 0; if (n < 0) n = 0;
else if (n > 1.0) n = 1.0; else if (n > 1.0f) n = 1.0f;
magnitude = n * 65536.0; magnitude = n * 65536.0f;
}
void setSampleRate_Hz(const float &fs_Hz) {
phase_increment *= sample_rate_Hz / fs_Hz; //change the phase increment for the new frequency
sample_rate_Hz = fs_Hz;
} }
void begin(void) { enabled = true; }
void end(void) { enabled = false; }
virtual void update(void); virtual void update(void);
private: private:
uint32_t phase_accumulator; uint32_t phase_accumulator = 0;
uint32_t phase_increment; uint32_t phase_increment = 0;
int32_t magnitude; int32_t magnitude = 0;
float sample_rate_Hz = AUDIO_SAMPLE_RATE;
volatile uint8_t enabled = 1;
}; };

Loading…
Cancel
Save