Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: Accessing Audio Objects from an Array

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Posts
    3

    Accessing Audio Objects from an Array

    Hello Folks!
    I'm very happy with the Teensy Boards and am currently building a polyphonic synthesizer based on the Teensy 4.0 and the Audio Shield Rev C.
    Therefore, I'd like to store some AudioSynthWaveform objects into an Array, so I can easily access them, but apparently this is impossible.

    Following Code doesn't work:

    Code:
    #include <Audio.h>
    
    // GUItool: begin automatically generated code
    AudioSynthWaveform       waveform1;      //xy=470.183349609375,212.99998474121094
    AudioOutputI2S           i2s1;           //xy=680.183349609375,211.99998474121094
    AudioConnection          patchCord1(waveform1, 0, i2s1, 0);
    AudioConnection          patchCord2(waveform1, 0, i2s1, 1);
    AudioControlSGTL5000     sgtl5000_1;     //xy=470.183349609375,443.00001525878906
    // GUItool: end automatically generated code
    
    AudioSynthWaveform waveforms [1] = {waveform1};
    void setup() {
      Serial.begin(9600);
      AudioMemory(10);
    
    
      sgtl5000_1.enable();
      sgtl5000_1.volume(0.01);
    
      //   This here produces Sound
      //   waveform1.begin(WAVEFORM_TRIANGLE);
      //   waveform1.frequency(440);
      //   waveform1.amplitude(0.01);
    
      // This here produces no Sound
      waveforms[0].begin(WAVEFORM_TRIANGLE);
      waveforms[0].frequency(440);
      waveforms[0].amplitude(0.01);
    }
    
    void loop() {
    }
    Is it somehow possibly to reference these Audio Objects in an Array? (maybe with Pointers, but this doesn't work either as far as i've tried)
    If it is needed, I could maybe write a custom class containing the Audio Objects which I can then reference in my code, but I'd like to stay simple there.

    Thanks in advance!

  2. #2
    Senior Member
    Join Date
    Nov 2017
    Location
    Belgium
    Posts
    165
    This is one way to do it:
    Code:
    //example for phase modulation
    
    #include <Audio.h>
    
    // GUItool: begin automatically generated code
    // AudioSynthWaveform       waveform[0];      //xy=277,194
    // AudioSynthWaveform       waveform[1]; //xy=283,235
    // AudioSynthWaveform       waveform[2]; //xy=288,276
    // AudioSynthWaveform       waveform[3]; //xy=293,317
    // AudioSynthWaveformSineModulated sine_fm[0];       //xy=437,199
    // AudioSynthWaveformSineModulated sine_fm[1]; //xy=443,240
    // AudioSynthWaveformSineModulated sine_fm[2]; //xy=448,281
    // AudioSynthWaveformSineModulated sine_fm[3]; //xy=453,322
    AudioSynthWaveform*  waveform = new AudioSynthWaveform[4];  //not automatically generated code
    AudioSynthWaveformSineModulated*  sine_fm = new AudioSynthWaveformSineModulated[4];  //not automatically generated code
    AudioMixer4              mixer1;         //xy=630,252
    AudioOutputI2S           i2s1;           //xy=776,259
    AudioConnection          patchCord1(waveform[0], sine_fm[0]);
    AudioConnection          patchCord2(waveform[1], sine_fm[1]);
    AudioConnection          patchCord3(waveform[2], sine_fm[2]);
    AudioConnection          patchCord4(waveform[3], sine_fm[3]);
    AudioConnection          patchCord5(sine_fm[0], 0, mixer1, 0);
    AudioConnection          patchCord6(sine_fm[1], 0, mixer1, 1);
    AudioConnection          patchCord7(sine_fm[2], 0, mixer1, 2);
    AudioConnection          patchCord8(sine_fm[3], 0, mixer1, 3);
    AudioConnection          patchCord9(mixer1, 0, i2s1, 0);
    AudioConnection          patchCord10(mixer1, 0, i2s1, 1);
    AudioControlSGTL5000     sgtl5000_1;     //xy=630,319
    // GUItool: end automatically generated code
    
    void setup() {
      Serial.begin(250000);
      AudioMemory(20);
    
      sgtl5000_1.enable();
      sgtl5000_1.volume(0.5);
    
      for(int i = 0; i < 4; i++) {
        mixer1.gain(i, 0.25);
      }
    }
    
    void loop() {
      Serial.println("sirens in the city");
      phaseSweep(WAVEFORM_SINE, .1, 3, 1000, 0, 90, 0.05, 0.05); //sirens in the city
      Serial.println("red alert");
      phaseSweep(WAVEFORM_SAWTOOTH, 0.9, .75, 500, 0, 90, 0.05, 0.5); //red alert
      Serial.println("helicopter flyby");
      phaseSweep(WAVEFORM_SAWTOOTH_REVERSE, 0.9, 20, 200, 5, 50, 0.05, 0.05); //helicopter flyby
      Serial.println("etheral winds");
      phaseSweep(WAVEFORM_SINE, 1.0, 0.01, 300, 0, 90, 0.01, 0.05);  //etheral winds
    }
    
    void phaseSweep(short t_type, float ampl_lfo, float freq_lfo, float freq_vco, float from_phase, float to_phase, float step_up, float step_down){
      //initialise lfo's & vco's
      for(int i = 0; i < 4; i++) {
        waveform[i].amplitude(ampl_lfo);
        waveform[i].frequency(freq_lfo);
        waveform[i].begin(t_type);
        waveform[i].phase(from_phase);
        sine_fm[i].amplitude(1);
        sine_fm[i].frequency(freq_vco);
      }
      waveform[0].phase(0); //phase of lfo0 always 0
      delay(2000);
      //sweep up phase of lfo's
      Serial.println("phase up");
      for(float p = from_phase; p <= to_phase; p+=step_up){
        AudioNoInterrupts();
        for (int i = 1; i < 4; i++) {
          waveform[i].phase(p * i);
        }
        AudioInterrupts();
        delay(10);
        Serial.print("phase up ");
        Serial.println(p);
      }
      Serial.println("phase steady");
      delay(2000);
      //sweep down phase of lfo's
      Serial.println("phase down");
      for(float p = to_phase; p >= from_phase; p-=step_down){
        AudioNoInterrupts();
        for (int i = 1; i < 4; i++) {
          waveform[i].phase(p * i);
        }
        AudioInterrupts();
        delay(10);
        Serial.print("phase down ");
        Serial.println(p);
      }
      Serial.println("phase steady");
      delay(2000);
    }

  3. #3
    Junior Member
    Join Date
    Oct 2019
    Posts
    3
    Many thanks!
    Does this also work with 2D -Arrays?
    Calling
    Code:
    AudioSynthWaveformModulated* oscillators = new AudioSynthWaveformModulated[16][3];
    doesn't compile

  4. #4
    Junior Member
    Join Date
    Oct 2019
    Posts
    3
    I should've spent some time with looking up the problem myself before posting, sorry.
    I'm finally going to settle on this form:
    Code:
    int* ary = new int[sizeX*sizeY];
    
    // ary[i][j] is then rewritten as
    ary[i*sizeY+j]

  5. #5
    Senior Member
    Join Date
    Nov 2017
    Location
    Belgium
    Posts
    165
    No problem, good you found the solution yourself.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •