浏览代码

Add phase() to waveform objects

dds
PaulStoffregen 10 年前
父节点
当前提交
11cc1b2bd8
共有 4 个文件被更改,包括 47 次插入31 次删除
  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 查看文件

@@ -43,6 +43,7 @@ stop KEYWORD2
play KEYWORD2
updateCoefs KEYWORD2
frequency KEYWORD2
phase KEYWORD2
amplitude KEYWORD2
offset KEYWORD2
resonance KEYWORD2

+ 7
- 27
synth_sine.cpp 查看文件

@@ -33,12 +33,6 @@ extern const int16_t AudioWaveformSine[257];
}


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)
{
audio_block_t *block;
@@ -47,7 +41,7 @@ void AudioSynthWaveformSine::update(void)

block = allocate();
if (block) {
ph = phase;
ph = phase_accumulator;
inc = phase_increment;
for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
index = ph >> 24;
@@ -60,26 +54,15 @@ void AudioSynthWaveformSine::update(void)
block->data[i] = multiply_32x32_rshift32(val1 + val2, magnitude);
ph += inc;
}
phase = ph;
phase_accumulator = ph;
transmit(block);
release(block);
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)
{
audio_block_t *block, *modinput;
@@ -88,7 +71,7 @@ void AudioSynthWaveformSineModulated::update(void)
int16_t mod;

modinput = receiveReadOnly();
ph = phase;
ph = phase_accumulator;
inc = phase_increment;
block = allocate();
if (!block) {
@@ -103,7 +86,7 @@ void AudioSynthWaveformSineModulated::update(void)
} else {
ph += phase_increment * AUDIO_BLOCK_SAMPLES;
}
phase = ph;
phase_accumulator = ph;
return;
}
if (modinput) {
@@ -124,7 +107,7 @@ void AudioSynthWaveformSineModulated::update(void)
}
release(modinput);
} else {
ph = phase;
ph = phase_accumulator;
inc = phase_increment;
for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
index = ph >> 24;
@@ -137,12 +120,9 @@ void AudioSynthWaveformSineModulated::update(void)
ph += inc;
}
}
phase = ph;
phase_accumulator = ph;
transmit(block);
release(block);
}






+ 31
- 4
synth_sine.h 查看文件

@@ -38,7 +38,19 @@ class AudioSynthWaveformSine : public AudioStream
{
public:
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) {
if (n < 0) n = 0;
else if (n > 1.0) n = 1.0;
@@ -46,7 +58,7 @@ public:
}
virtual void update(void);
private:
uint32_t phase;
uint32_t phase_accumulator;
uint32_t phase_increment;
int32_t magnitude;
};
@@ -55,7 +67,22 @@ class AudioSynthWaveformSineModulated : public AudioStream
{
public:
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) {
if (n < 0) n = 0;
else if (n > 1.0) n = 1.0;
@@ -63,7 +90,7 @@ public:
}
virtual void update(void);
private:
uint32_t phase;
uint32_t phase_accumulator;
uint32_t phase_increment;
audio_block_t *inputQueueArray[1];
int32_t magnitude;

+ 8
- 0
synth_waveform.h 查看文件

@@ -67,6 +67,14 @@ public:
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;
}
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
if (n < 0) n = 0;
else if (n > 1.0) n = 1.0;

正在加载...
取消
保存