Browse Source

Add AudioSynthWaveform (any waveform), remove AudioSineWave (sine only)

dds
PaulStoffregen 11 years ago
parent
commit
9a455c1dc0
4 changed files with 26 additions and 84 deletions
  1. +7
    -53
      Audio.cpp
  2. +10
    -22
      Audio.h
  3. +7
    -7
      examples/PassThroughPlusTone/PassThroughPlusTone.ino
  4. +2
    -2
      examples/PlayMidiTones/MidiTones.ino

+ 7
- 53
Audio.cpp View File







/******************************************************************/







static const int16_t sine_table[] = {
0, 804, 1608, 2410, 3212, 4011, 4808, 5602, 6393, 7179,
7962, 8739, 9512, 10278, 11039, 11793, 12539, 13279, 14010, 14732,
15446, 16151, 16846, 17530, 18204, 18868, 19519, 20159, 20787, 21403,
22005, 22594, 23170, 23731, 24279, 24811, 25329, 25832, 26319, 26790,
27245, 27683, 28105, 28510, 28898, 29268, 29621, 29956, 30273, 30571,
30852, 31113, 31356, 31580, 31785, 31971, 32137, 32285, 32412, 32521,
32609, 32678, 32728, 32757, 32767, 32757, 32728, 32678, 32609, 32521,
32412, 32285, 32137, 31971, 31785, 31580, 31356, 31113, 30852, 30571,
30273, 29956, 29621, 29268, 28898, 28510, 28105, 27683, 27245, 26790,
26319, 25832, 25329, 24811, 24279, 23731, 23170, 22594, 22005, 21403,
20787, 20159, 19519, 18868, 18204, 17530, 16846, 16151, 15446, 14732,
14010, 13279, 12539, 11793, 11039, 10278, 9512, 8739, 7962, 7179,
6393, 5602, 4808, 4011, 3212, 2410, 1608, 804, 0, -804,
-1608, -2410, -3212, -4011, -4808, -5602, -6393, -7179, -7962, -8739,
-9512,-10278,-11039,-11793,-12539,-13279,-14010,-14732,-15446,-16151,
-16846,-17530,-18204,-18868,-19519,-20159,-20787,-21403,-22005,-22594,
-23170,-23731,-24279,-24811,-25329,-25832,-26319,-26790,-27245,-27683,
-28105,-28510,-28898,-29268,-29621,-29956,-30273,-30571,-30852,-31113,
-31356,-31580,-31785,-31971,-32137,-32285,-32412,-32521,-32609,-32678,
-32728,-32757,-32767,-32757,-32728,-32678,-32609,-32521,-32412,-32285,
-32137,-31971,-31785,-31580,-31356,-31113,-30852,-30571,-30273,-29956,
-29621,-29268,-28898,-28510,-28105,-27683,-27245,-26790,-26319,-25832,
-25329,-24811,-24279,-23731,-23170,-22594,-22005,-21403,-20787,-20159,
-19519,-18868,-18204,-17530,-16846,-16151,-15446,-14732,-14010,-13279,
-12539,-11793,-11039,-10278, -9512, -8739, -7962, -7179, -6393, -5602,
-4808, -4011, -3212, -2410, -1608, -804, 0
};


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

void AudioSineWave::update(void)
void AudioSynthWaveform::update(void)
{ {
audio_block_t *block; audio_block_t *block;
uint32_t i, ph, inc, index, scale; uint32_t i, ph, inc, index, scale;
int32_t val1, val2, val3; int32_t val1, val2, val3;


//Serial.println("AudioSineWave::update");

//Serial.println("AudioSynthWaveform::update");
if (magnitude > 0 && (block = allocate()) != NULL) { if (magnitude > 0 && (block = allocate()) != NULL) {
ph = phase; ph = phase;
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;
val1 = sine_table[index];
val2 = sine_table[index+1];
val1 = wavetable[index];
val2 = wavetable[index+1];
scale = (ph >> 8) & 0xFFFF; scale = (ph >> 8) & 0xFFFF;
val2 *= scale; val2 *= scale;
val1 *= 0xFFFF - scale; val1 *= 0xFFFF - scale;














#if 0
void AudioSineWaveMod::frequency(float f) void AudioSineWaveMod::frequency(float f)
{ {
if (f > AUDIO_SAMPLE_RATE_EXACT / 2 || f < 0.0) return; if (f > AUDIO_SAMPLE_RATE_EXACT / 2 || f < 0.0) return;
transmit(block); transmit(block);
release(block); release(block);
} }
#endif







+ 10
- 22
Audio.h View File







class AudioSineWave : public AudioStream
class AudioSynthWaveform : public AudioStream
{ {
public: public:
AudioSineWave() : AudioStream(0, NULL) { magnitude = 0; }
void frequency(float freq);
//void amplitude(double n) { amplitude((float)n); }
AudioSynthWaveform(const int16_t *waveform)
: AudioStream(0, NULL), wavetable(waveform), magnitude(0), phase(0) { }
void frequency(float freq) {
if (freq > AUDIO_SAMPLE_RATE_EXACT / 2 || freq < 0.0) return;
phase_increment = (freq / AUDIO_SAMPLE_RATE_EXACT) * 4294967296.0f;
}
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;
magnitude = n * 32767.0; magnitude = n * 32767.0;
Serial.print("magnitude(f) = ");
Serial.println(magnitude);
}
/*
void amplitude(int n) { // 0 to 32767
if (n < 0) n = 0;
else if (n > 32767) n = 32767;
magnitude = n;
Serial.print("magnitude(i) = ");
Serial.println(magnitude);
}
void amplitude(unsigned int n) { // 0 to 32767
if (n > 32767) n = 32767;
magnitude = n;
Serial.print("magnitude(u) = ");
Serial.println(magnitude);
} }
*/
virtual void update(void); virtual void update(void);
private: private:
const int16_t *wavetable;
uint16_t magnitude; uint16_t magnitude;
uint32_t phase; uint32_t phase;
uint32_t phase_increment; uint32_t phase_increment;






#if 0
class AudioSineWaveMod : public AudioStream class AudioSineWaveMod : public AudioStream
{ {
public: public:
uint32_t modulation_factor; uint32_t modulation_factor;
audio_block_t *inputQueueArray[1]; audio_block_t *inputQueueArray[1];
}; };
#endif







+ 7
- 7
examples/PassThroughPlusTone/PassThroughPlusTone.ino View File

// order data flows, inputs/sources -> processing -> outputs // order data flows, inputs/sources -> processing -> outputs
// //
//AudioInputAnalog analogPinInput(16); // analog A2 (pin 16) //AudioInputAnalog analogPinInput(16); // analog A2 (pin 16)
AudioInputI2S audioInput; // audio shield: mic or line-in
AudioSineWave toneLow;
AudioSineWave toneHigh;
AudioMixer4 mixerLeft;
AudioMixer4 mixerRight;
AudioOutputI2S audioOutput; // audio shield: headphones & line-out
AudioOutputPWM pwmOutput; // audio output with PWM on pins 3 & 4
AudioInputI2S audioInput; // audio shield: mic or line-in
AudioSynthWaveform toneLow(AudioWaveformSine);
AudioSynthWaveform toneHigh(AudioWaveformTriangle);
AudioMixer4 mixerLeft;
AudioMixer4 mixerRight;
AudioOutputI2S audioOutput; // audio shield: headphones & line-out
AudioOutputPWM pwmOutput; // audio output with PWM on pins 3 & 4


// Create Audio connections between the components // Create Audio connections between the components
// //

+ 2
- 2
examples/PlayMidiTones/MidiTones.ino View File



//AudioInputI2S adc; //AudioInputI2S adc;
//AudioInputAnalog ana(16); //AudioInputAnalog ana(16);
AudioSineWave mysine;
AudioSineWave sine2;
AudioSynthWaveform mysine(AudioWaveformSine);
AudioSynthWaveform sine2(AudioWaveformSine);
//AudioOutputPWM myout; //AudioOutputPWM myout;
//AudioPlaySDcardWAV wav; //AudioPlaySDcardWAV wav;
//AudioPlaySDcardRAW wav; //AudioPlaySDcardRAW wav;

Loading…
Cancel
Save