Update synth sine for sample rate

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

@ -24,38 +24,42 @@ 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 (magnitude) { if (enabled) {
block = allocate_f32(); if (magnitude) {
if (block) { block = allocate_f32();
ph = phase_accumulator; if (block) {
inc = phase_increment; block_length = (uint32_t)block->length;
for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) { ph = phase_accumulator;
index = ph >> 24; inc = phase_increment;
val1 = AudioWaveformSine[index]; for (i=0; i < block_length; i++) {
val2 = AudioWaveformSine[index+1]; index = ph >> 24;
scale = (ph >> 8) & 0xFFFF; val1 = AudioWaveformSine[index];
val2 *= scale; val2 = AudioWaveformSine[index+1];
val1 *= 0x10000 - scale; scale = (ph >> 8) & 0xFFFF;
#if defined(KINETISK) val2 *= scale;
block->data[i] = (float) multiply_32x32_rshift32(val1 + val2, magnitude); val1 *= 0x10000 - scale;
#elif defined(KINETISL) #if defined(KINETISK)
block->data[i] = (float) ((((val1 + val2) >> 16) * magnitude) >> 16); block->data[i] = (float) multiply_32x32_rshift32(val1 + val2, magnitude);
#endif #elif defined(KINETISL)
ph += inc; block->data[i] = (float) ((((val1 + val2) >> 16) * magnitude) >> 16);
#endif
ph += inc;
block->data[i] = block->data[i] / 32768.0f; // scale to float block->data[i] = block->data[i] / 32768.0f; // scale to float
} }
phase_accumulator = ph; phase_accumulator = ph;
AudioStream_F32::transmit(block); AudioStream_F32::transmit(block);
AudioStream_F32::release(block); AudioStream_F32::release(block);
return; return;
}
} }
phase_accumulator += phase_increment * block_length;
} }
phase_accumulator += phase_increment * AUDIO_BLOCK_SAMPLES;
} }

@ -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