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

@ -21,31 +21,44 @@
class AudioSynthWaveformSine_F32 : public AudioStream_F32
{
//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:
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) {
if (freq < 0.0) freq = 0.0;
else if (freq > AUDIO_SAMPLE_RATE_EXACT/2) freq = AUDIO_SAMPLE_RATE_EXACT/2;
phase_increment = freq * (4294967296.0 / AUDIO_SAMPLE_RATE_EXACT);
else if (freq > sample_rate_Hz/2.f) freq = sample_rate_Hz/2.f;
phase_increment = freq * (4294967296.0 / sample_rate_Hz);
}
void phase(float angle) {
if (angle < 0.0) angle = 0.0;
else if (angle > 360.0) {
angle = angle - 360.0;
if (angle >= 360.0) return;
if (angle < 0.0f) angle = 0.0f;
else if (angle > 360.0f) {
angle = angle - 360.0f;
if (angle >= 360.0f) return;
}
phase_accumulator = angle * (4294967296.0 / 360.0);
phase_accumulator = angle * (4294967296.0f / 360.0f);
}
void amplitude(float n) {
if (n < 0) n = 0;
else if (n > 1.0) n = 1.0;
magnitude = n * 65536.0;
else if (n > 1.0f) n = 1.0f;
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);
private:
uint32_t phase_accumulator;
uint32_t phase_increment;
int32_t magnitude;
uint32_t phase_accumulator = 0;
uint32_t phase_increment = 0;
int32_t magnitude = 0;
float sample_rate_Hz = AUDIO_SAMPLE_RATE;
volatile uint8_t enabled = 1;
};

Loading…
Cancel
Save