update synth_waveform with sample rate

feature_setBlockSize
Chip Audette 8 years ago
parent 1f629ceaaf
commit 9c2dc661f2
  1. 4
      examples/OscillatorWithPitchmod_Float/OscillatorWithPitchmod_Float.ino
  2. 35
      synth_waveform_F32.h

@ -19,10 +19,10 @@
#include <OpenAudio_ArduinoLibrary.h> //for AudioConvert_I16toF32, AudioConvert_F32toI16, and AudioEffectGain_F32 #include <OpenAudio_ArduinoLibrary.h> //for AudioConvert_I16toF32, AudioConvert_F32toI16, and AudioEffectGain_F32
#define DO_USB 1 //set to 1 to enable USB audio. Be sure to go under the "Tools" menu and do "USB Type" -> "Audio" #define DO_USB 0 //set to 1 to enable USB audio. Be sure to go under the "Tools" menu and do "USB Type" -> "Audio"
//create audio library objects for handling the audio //create audio library objects for handling the audio
AudioControlSGTL5000_Extended sgtl5000; //controller for the Teensy Audio Board AudioControlSGTL5000 sgtl5000; //controller for the Teensy Audio Board
AudioOutputI2S i2s_out; //Digital audio *to* the Teensy Audio Board DAC. Expects Int16. Stereo AudioOutputI2S i2s_out; //Digital audio *to* the Teensy Audio Board DAC. Expects Int16. Stereo
AudioConvert_F32toI16 float2Int; //Converts Float to Int16. See class in AudioStream_F32.h AudioConvert_F32toI16 float2Int; //Converts Float to Int16. See class in AudioStream_F32.h

@ -25,9 +25,29 @@ class AudioSynthWaveform_F32 : public AudioStream_F32
OSCILLATOR_MODE_TRIANGLE OSCILLATOR_MODE_TRIANGLE
}; };
AudioSynthWaveform_F32(void) : AudioStream_F32(1, inputQueueArray_f32), AudioSynthWaveform_F32(const AudioSettings_F32 &settings) : AudioStream_F32(1, inputQueueArray_f32),
_PI(2*acos(0.0f)),
twoPI(2 * _PI),
sample_rate_Hz(AUDIO_SAMPLE_RATE_EXACT),
_OscillatorMode(OSCILLATOR_MODE_SINE),
_Frequency(440.0f),
_Phase(0.0f),
_PhaseIncrement(0.0f),
_PitchModAmt(0.0f),
_PortamentoIncrement(0.0f),
_PortamentoSamples(0),
_CurrentPortamentoSample(0),
_NotesPlaying(0)
{
setSampleRate(settings.sample_rate_Hz);
}
AudioSynthWaveform_F32(void) : AudioStream_F32(1, inputQueueArray_f32), //uses default AUDIO_SAMPLE_RATE from AudioStream.h
_PI(2*acos(0.0f)), _PI(2*acos(0.0f)),
twoPI(2 * _PI), twoPI(2 * _PI),
sample_rate_Hz(AUDIO_SAMPLE_RATE_EXACT),
_OscillatorMode(OSCILLATOR_MODE_SINE), _OscillatorMode(OSCILLATOR_MODE_SINE),
_Frequency(440.0f), _Frequency(440.0f),
_Phase(0.0f), _Phase(0.0f),
@ -39,7 +59,7 @@ class AudioSynthWaveform_F32 : public AudioStream_F32
_NotesPlaying(0) {}; _NotesPlaying(0) {};
void frequency(float32_t freq) { void frequency(float32_t freq) {
float32_t nyquist = AUDIO_SAMPLE_RATE_EXACT/2; float32_t nyquist = sample_rate_Hz/2.f;
if (freq < 0.0) freq = 0.0; if (freq < 0.0) freq = 0.0;
else if (freq > nyquist) freq = nyquist; else if (freq > nyquist) freq = nyquist;
@ -51,7 +71,7 @@ class AudioSynthWaveform_F32 : public AudioStream_F32
_Frequency = freq; _Frequency = freq;
} }
_PhaseIncrement = _Frequency * twoPI / AUDIO_SAMPLE_RATE_EXACT; _PhaseIncrement = _Frequency * twoPI / sample_rate_Hz;
} }
void amplitude(float32_t n) { void amplitude(float32_t n) {
@ -80,7 +100,7 @@ class AudioSynthWaveform_F32 : public AudioStream_F32
void portamentoTime(float32_t slidetime) { void portamentoTime(float32_t slidetime) {
_PortamentoTime = slidetime; _PortamentoTime = slidetime;
_PortamentoSamples = floorf(slidetime * AUDIO_SAMPLE_RATE_ROUNDED); _PortamentoSamples = floorf(slidetime * sample_rate_Hz);
} }
@ -95,10 +115,17 @@ class AudioSynthWaveform_F32 : public AudioStream_F32
} }
void update(void); void update(void);
void setSampleRate(const float32_t fs_Hz)
{
_PhaseIncrement = _PhaseIncrement*sample_rate_Hz / fs_Hz;
_PortamentoSamples = floorf( ((float)_PortamentoSamples) * fs_Hz / sample_rate_Hz );
sample_rate_Hz = fs_Hz;
}
private: private:
inline float32_t applyMod(uint32_t sample, audio_block_f32_t *lfo); inline float32_t applyMod(uint32_t sample, audio_block_f32_t *lfo);
const float32_t _PI; const float32_t _PI;
float32_t twoPI; float32_t twoPI;
float32_t sample_rate_Hz;
OscillatorMode _OscillatorMode; OscillatorMode _OscillatorMode;
float32_t _Frequency; float32_t _Frequency;

Loading…
Cancel
Save