Browse Source

Add phase() to waveform objects

dds
PaulStoffregen 10 years ago
parent
commit
11cc1b2bd8
4 changed files with 47 additions and 31 deletions
  1. +1
    -0
      keywords.txt
  2. +7
    -27
      synth_sine.cpp
  3. +31
    -4
      synth_sine.h
  4. +8
    -0
      synth_waveform.h

+ 1
- 0
keywords.txt View File

play KEYWORD2 play KEYWORD2
updateCoefs KEYWORD2 updateCoefs KEYWORD2
frequency KEYWORD2 frequency KEYWORD2
phase KEYWORD2
amplitude KEYWORD2 amplitude KEYWORD2
offset KEYWORD2 offset KEYWORD2
resonance KEYWORD2 resonance KEYWORD2

+ 7
- 27
synth_sine.cpp View File

} }




void AudioSynthWaveformSine::frequency(float f)
{
if (f > AUDIO_SAMPLE_RATE_EXACT / 2 || f < 0.0) return;
phase_increment = (f / AUDIO_SAMPLE_RATE_EXACT) * 4294967296.0f;
}

void AudioSynthWaveformSine::update(void) void AudioSynthWaveformSine::update(void)
{ {
audio_block_t *block; audio_block_t *block;


block = allocate(); block = allocate();
if (block) { if (block) {
ph = phase;
ph = phase_accumulator;
inc = phase_increment; inc = phase_increment;
for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) { for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
index = ph >> 24; index = ph >> 24;
block->data[i] = multiply_32x32_rshift32(val1 + val2, magnitude); block->data[i] = multiply_32x32_rshift32(val1 + val2, magnitude);
ph += inc; ph += inc;
} }
phase = ph;
phase_accumulator = ph;
transmit(block); transmit(block);
release(block); release(block);
return; return;
} }
phase += phase_increment * AUDIO_BLOCK_SAMPLES;
phase_accumulator += phase_increment * AUDIO_BLOCK_SAMPLES;
} }






void AudioSynthWaveformSineModulated::frequency(float f)
{
// maximum unmodulated carrier frequency is 11025 Hz
// input = +1.0 doubles carrier
// input = -1.0 DC output
if (f >= AUDIO_SAMPLE_RATE_EXACT / 4 || f < 0.0) return;
phase_increment = (f / AUDIO_SAMPLE_RATE_EXACT) * 4294967296.0f;
}

void AudioSynthWaveformSineModulated::update(void) void AudioSynthWaveformSineModulated::update(void)
{ {
audio_block_t *block, *modinput; audio_block_t *block, *modinput;
int16_t mod; int16_t mod;


modinput = receiveReadOnly(); modinput = receiveReadOnly();
ph = phase;
ph = phase_accumulator;
inc = phase_increment; inc = phase_increment;
block = allocate(); block = allocate();
if (!block) { if (!block) {
} else { } else {
ph += phase_increment * AUDIO_BLOCK_SAMPLES; ph += phase_increment * AUDIO_BLOCK_SAMPLES;
} }
phase = ph;
phase_accumulator = ph;
return; return;
} }
if (modinput) { if (modinput) {
} }
release(modinput); release(modinput);
} else { } else {
ph = phase;
ph = phase_accumulator;
inc = phase_increment; inc = phase_increment;
for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) { for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
index = ph >> 24; index = ph >> 24;
ph += inc; ph += inc;
} }
} }
phase = ph;
phase_accumulator = ph;
transmit(block); transmit(block);
release(block); release(block);
} }








+ 31
- 4
synth_sine.h View File

{ {
public: public:
AudioSynthWaveformSine() : AudioStream(0, NULL), magnitude(16384) {} AudioSynthWaveformSine() : AudioStream(0, NULL), magnitude(16384) {}
void frequency(float freq);
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);
}
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;
}
phase_accumulator = angle * (4294967296.0 / 360.0);
}
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.0) n = 1.0;
} }
virtual void update(void); virtual void update(void);
private: private:
uint32_t phase;
uint32_t phase_accumulator;
uint32_t phase_increment; uint32_t phase_increment;
int32_t magnitude; int32_t magnitude;
}; };
{ {
public: public:
AudioSynthWaveformSineModulated() : AudioStream(1, inputQueueArray), magnitude(16384) {} AudioSynthWaveformSineModulated() : AudioStream(1, inputQueueArray), magnitude(16384) {}
void frequency(float freq);
// maximum unmodulated carrier frequency is 11025 Hz
// input = +1.0 doubles carrier
// input = -1.0 DC output
void frequency(float freq) {
if (freq < 0.0) freq = 0.0;
else if (freq > AUDIO_SAMPLE_RATE_EXACT/4) freq = AUDIO_SAMPLE_RATE_EXACT/4;
phase_increment = freq * (4294967296.0 / AUDIO_SAMPLE_RATE_EXACT);
}
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;
}
phase_accumulator = angle * (4294967296.0 / 360.0);
}
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.0) n = 1.0;
} }
virtual void update(void); virtual void update(void);
private: private:
uint32_t phase;
uint32_t phase_accumulator;
uint32_t phase_increment; uint32_t phase_increment;
audio_block_t *inputQueueArray[1]; audio_block_t *inputQueueArray[1];
int32_t magnitude; int32_t magnitude;

+ 8
- 0
synth_waveform.h View File

else if (t_freq > AUDIO_SAMPLE_RATE_EXACT / 2) t_freq = AUDIO_SAMPLE_RATE_EXACT / 2; else if (t_freq > AUDIO_SAMPLE_RATE_EXACT / 2) t_freq = AUDIO_SAMPLE_RATE_EXACT / 2;
tone_incr = (t_freq * (0x80000000LL/AUDIO_SAMPLE_RATE_EXACT)) + 0.5; tone_incr = (t_freq * (0x80000000LL/AUDIO_SAMPLE_RATE_EXACT)) + 0.5;
} }
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;
}
tone_phase = angle * (2147483648.0 / 360.0);
}
void amplitude(float n) { // 0 to 1.0 void amplitude(float n) { // 0 to 1.0
if (n < 0) n = 0; if (n < 0) n = 0;
else if (n > 1.0) n = 1.0; else if (n > 1.0) n = 1.0;

Loading…
Cancel
Save