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

Thread: Vehicle Cabin Audio - Audio update painfully slow!

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

    Vehicle Cabin Audio - Audio update painfully slow!

    Hello,

    I am trying to create in vehicle audio. Its quite basic right now, the GPS gets vehicle speed, the sound changes (frequency shift) based on the speed. Sound is played through vehicle speakers with the help of AUX cable (from Audio Shield).

    The code works -- GPS updates and the sound changes based on frequency but it is too slow. I am not sure whether this is a drawback of GPS or if my code is inefficient and can be improved (I'm more inclined towards this as I am a newbie to Teensy and Arduino).

    Hardware Used:

    1. Teensy 3.6
    2. Teensy Audio Shield (Rev. B)
    3. Adafruit Ultimate GPS version 3 (Amazon Link)


    Click image for larger version. 

Name:	Project Hardware.jpg 
Views:	2 
Size:	42.9 KB 
ID:	17853

    I am confident hardware works as I have tested the GPS code (using a shortened version of Adafruit GPS code) and AudioShield codes separately and they perform well.

    Operating System and Software:


    Operating System: Windows 10
    Software: Arduino 1.8.7

    CODE:

    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    #include <Adafruit_GPS.h>
    
    #define mySerial Serial1
    
    Adafruit_GPS GPS(&mySerial);
    
    
    // Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
    // Set to 'true' if you want to debug and listen to the raw GPS sentences.
    #define GPSECHO  false
    
    // this keeps track of whether we're using the interrupt
    // off by default!
    boolean usingInterrupt = false;
    void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
    
    
    // Number of samples in each delay line. Change length to 0 to remove effect of flange
    #define FLANGE_DELAY_LENGTH (6*AUDIO_BLOCK_SAMPLES)
    // Allocate the delay line
    short delayBuffer[FLANGE_DELAY_LENGTH];
    
    //int offset1 = FLANGE_DELAY_LENGTH / 6;
    int offset1 = 0;
    //int depth = FLANGE_DELAY_LENGTH / 6;
    int depth = 0;
    //Change delayRate to 0 to remove effect of flange
    double delayRate = 0.0;
    
    //Define Bench variables
    
    int BENCH = 0; //1 for bench mode, 0 for vehicle mode
    
    float freq1 = 291.3;
    float freq2 = 7;
    float freq3 = freq1 * 3;
    float freq4 = 11;
    float freq5 = 647;
    float freq6 = 13;
    float freq7 = freq5 * 1.2;
    float freq8 = 17;
    
    float freq9 = 201.7;
    float freq10 = 2;
    float freq11 = freq9 * 3.125;
    float freq12 = 3;
    float freq13 = 897.1;
    float freq14 = 5;
    float freq15 = freq13 * 1.2;
    float freq16 = 7;
    
    float MySpeed = 0.0;
    float multiplier = 1.0;
    
    // GUItool: begin automatically generated code
    AudioSynthWaveformSine   sine7;          //xy=160.20001220703125,371.20001220703125
    AudioSynthWaveformSine   sine4;          //xy=162.20001220703125,218.1999969482422
    AudioSynthWaveformSine   sine5;          //xy=163.20001220703125,267.20001220703125
    AudioSynthWaveformSine   sine1;          //xy=164.1999969482422,66.19999694824219
    AudioSynthWaveformSine   sine6;          //xy=163.20001220703125,320.20001220703125
    AudioSynthWaveformSine   sine2;          //xy=164.20001220703125,119.19999694824219
    AudioSynthWaveformSine   sine3;          //xy=164.20001220703125,168.1999969482422
    AudioSynthWaveformSine   sine8;          //xy=164.20001220703125,422.20001220703125
    AudioSynthWaveformSine   sine10;         //xy=164.20001220703125,535.2000122070312
    AudioSynthWaveformSine   sine11;         //xy=164.20001220703125,590.2000122070312
    AudioSynthWaveformSine   sine9;          //xy=165.20001220703125,486.20001220703125
    AudioSynthWaveformSine   sine12;         //xy=165.20001220703125,637.2000122070312
    AudioSynthWaveformSine   sine13;         //xy=166.20001220703125,692.2000122070312
    AudioSynthWaveformSine   sine14;         //xy=167.20001220703125,740.2000122070312
    AudioSynthWaveformSine   sine15;         //xy=168.20001220703125,795.2000122070312
    AudioSynthWaveformSine   sine16;         //xy=171.20001220703125,844.2000122070312
    AudioEffectMultiply      multiply1;      //xy=315.20001220703125,92.19999694824219
    AudioEffectMultiply      multiply6;      //xy=317.20001220703125,609.2000122070312
    AudioEffectMultiply      multiply7;      //xy=317.20001220703125,716.2000122070312
    AudioEffectMultiply      multiply2;      //xy=321.20001220703125,200.1999969482422
    AudioEffectMultiply      multiply3;      //xy=321.20001220703125,292.20001220703125
    AudioEffectMultiply      multiply5;      //xy=321.20001220703125,507.20001220703125
    AudioEffectMultiply      multiply4;      //xy=324.20001220703125,396.20001220703125
    AudioEffectMultiply      multiply8;      //xy=323.1999969482422,808.1999969482422
    AudioMixer4              mixer2;         //xy=509.2000427246094,652.2000122070312
    AudioMixer4              mixer1;         //xy=524.2000122070312,239.1999969482422
    AudioMixer4              mixer3;         //xy=640.2000122070312,455.20001220703125
    AudioEffectFlange        flange1;        //xy=767.2000122070312,232.1999969482422
    AudioEffectFreeverb      freeverb1;      //xy=778.2000122070312,315.20001220703125
    AudioOutputI2S           i2s1;           //xy=925.199951171875,131.1999969482422
    AudioMixer4              mixer4;         //xy=946.1999969482422,277.1999969482422
    AudioConnection          patchCord1(sine7, 0, multiply4, 0);
    AudioConnection          patchCord2(sine4, 0, multiply2, 1);
    AudioConnection          patchCord3(sine5, 0, multiply3, 0);
    AudioConnection          patchCord4(sine1, 0, multiply1, 0);
    AudioConnection          patchCord5(sine6, 0, multiply3, 1);
    AudioConnection          patchCord6(sine2, 0, multiply1, 1);
    AudioConnection          patchCord7(sine3, 0, multiply2, 0);
    AudioConnection          patchCord8(sine8, 0, multiply4, 1);
    AudioConnection          patchCord9(sine10, 0, multiply5, 1);
    AudioConnection          patchCord10(sine11, 0, multiply6, 0);
    AudioConnection          patchCord11(sine9, 0, multiply5, 0);
    AudioConnection          patchCord12(sine12, 0, multiply6, 1);
    AudioConnection          patchCord13(sine13, 0, multiply7, 0);
    AudioConnection          patchCord14(sine14, 0, multiply7, 1);
    AudioConnection          patchCord15(sine15, 0, multiply8, 0);
    AudioConnection          patchCord16(sine16, 0, multiply8, 1);
    AudioConnection          patchCord17(multiply1, 0, mixer1, 0);
    AudioConnection          patchCord18(multiply6, 0, mixer2, 1);
    AudioConnection          patchCord19(multiply7, 0, mixer2, 2);
    AudioConnection          patchCord20(multiply2, 0, mixer1, 1);
    AudioConnection          patchCord21(multiply3, 0, mixer1, 2);
    AudioConnection          patchCord22(multiply5, 0, mixer2, 0);
    AudioConnection          patchCord23(multiply4, 0, mixer1, 3);
    AudioConnection          patchCord24(multiply8, 0, mixer2, 3);
    AudioConnection          patchCord25(mixer2, 0, mixer3, 1);
    AudioConnection          patchCord26(mixer1, 0, mixer3, 0);
    AudioConnection          patchCord27(mixer3, flange1);
    AudioConnection          patchCord28(mixer3, freeverb1);
    AudioConnection          patchCord29(flange1, 0, mixer4, 0);
    AudioConnection          patchCord30(freeverb1, 0, mixer4, 1);
    AudioConnection          patchCord31(mixer4, 0, i2s1, 0);
    AudioConnection          patchCord32(mixer4, 0, i2s1, 1);
    AudioControlSGTL5000     sgtl5000_1;     //xy=475.2000427246094,20
    // GUItool: end automatically generated code
    
    void setup() {
      // put your setup code here, to run once:
    
      // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
      // also spit it out
      Serial.begin(115200);
    
      // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
      GPS.begin(9600);
      mySerial.begin(9600);
      if (BENCH == 0) {
        MySpeed = GPS.speed * 1.852;//actual speed is in knots. Convert to kph
      }
    
      // Set the update rate
      GPS.sendCommand(PMTK_SET_NMEA_UPDATE_10HZ);   // 1 Hz update rate
      // For the parsing code to work nicely and have time to sort thru the data, and
      // print it out we don't suggest using anything higher than 1 Hz
    
      AudioMemory(10);
      sgtl5000_1.enable();
      sgtl5000_1.volume(0.75);
    
      //Set to zero to negate effect
      freeverb1.roomsize(0.3);
      freeverb1.damping(1);
    
      flange1.begin(delayBuffer, FLANGE_DELAY_LENGTH, offset1, depth, delayRate);
      //flange1.voices(offset1,depth,delayRate);
    
    }
    
    uint32_t timer = millis();
    void loop() {
      // put your main code here, to run repeatedly:
    
    
      //Serial.print("Test Mode: ");
      //Serial.println(BENCH);
      sine1.amplitude(0.5);
      sine2.amplitude(0.1);
      sine3.amplitude(0.5);
      sine4.amplitude(0.1);
      sine5.amplitude(0.4);
      sine6.amplitude(0.05);
      sine7.amplitude(0.4);
      sine8.amplitude(0.05);
      sine9.amplitude(0.5);
      sine10.amplitude(0.1);
      sine11.amplitude(0.5);
      sine12.amplitude(0.1);
      sine13.amplitude(0.4);
      sine14.amplitude(0.05);
      sine15.amplitude(0.4);
      sine16.amplitude(0.05);
      //AudioNoInterrupts();
    
    
      while (MySpeed <= 45)
      {
        //Serial.println("Speed < 45");
        sine1.frequency(freq2);
        sine2.frequency(freq1 * multiplier);
    
        sine3.frequency(freq4);
        sine4.frequency(freq3 * multiplier);
    
        sine5.frequency(freq6);
        sine6.frequency(freq5 * multiplier);
    
        sine7.frequency(freq8);
        sine8.frequency(freq7 * multiplier);
    
        sine1.frequency(freq10);
        sine2.frequency(freq9 * multiplier);
    
        sine3.frequency(freq12);
        sine4.frequency(freq11 * multiplier);
    
        sine5.frequency(freq14);
        sine6.frequency(freq13 * multiplier);
    
        sine7.frequency(freq16);
        sine8.frequency(freq15 * multiplier);
    
        if (BENCH == 1) {
          if (MySpeed == 0)
          {
            delay(2000); //hold for 3s to show stationary noise
            Serial.println("Inside if statement. Multiplier is 1. Delay 3s");
          }
    
          if (MySpeed == 45)
          {
            Serial.println("Speed is 45kph. Resetting variables...");
            delay(2000); //hold for 3s to show stationary noise
            MySpeed = -1;
            multiplier = 1;
            freq1 = 291.3;
            freq2 = 7;
            freq3 = freq1 * 3;
            freq4 = 11;
            freq5 = 647;
            freq6 = 13;
            freq7 = freq5 * 1.2;
            freq8 = 17;
    
            freq9 = 201.7;
            freq10 = 2;
            freq11 = freq9 * 3.125;
            freq12 = 3;
            freq13 = 897.1;
            freq14 = 5;
            freq15 = freq13 * 1.2;
            freq16 = 7;
    
          }
          MySpeed += 1;
          multiplier += 0.0125;
          Serial.print("Multiplier: ");
          Serial.println(multiplier);
          delay(300);
          Serial.print("Speed (kph): ");
          Serial.println(MySpeed);
        }
        else {
          //Serial.println("Vehicle Mode. Else Condition");
    
          if (! usingInterrupt) {
            // read data from the GPS in the 'main loop'
            //Serial.println("Read GPS data");
            char c = GPS.read();
            // if you want to debug, this is a good time to do it!
            if (GPSECHO)
              if (c) {
                //Serial.print(c);
              }
          }
    
          // if a sentence is received, we can check the checksum, parse it...
          if (GPS.newNMEAreceived()) {
            if (!GPS.parse(GPS.lastNMEA()))   // this also sets the newNMEAreceived() flag to false
              return;  // we can fail to parse a sentence in which case we should just wait for another
          }
    
          // if millis() or timer wraps around, we'll just reset it
          if (timer > millis())  timer = millis();
    
          // approximately every 2 seconds or so, print out the current stats
          if (millis() - timer > 2000) {
            timer = millis(); // reset the timer
            if (GPS.fix) {
              Serial.print("Speed (kph): "); Serial.println(GPS.speed * 1.852); //actual speed is in knots
            }
          }
    
    
          MySpeed = GPS.speed * 1.852;
          multiplier = pow(1.0125, MySpeed) ;
          //Serial.print("Speed (kph): ");
          //Serial.println(MySpeed);
          //Serial.println(multiplier);
          if (MySpeed == 45) {
            mixer4.gain(0, 0);
            mixer4.gain(1, 0);
            mixer4.gain(2, 0);
            mixer4.gain(3, 0);
          }
        }
      }
    }
    I did a quick test in my car. The GPS code from Adafruit works fine. The issue is when the GPS and Audio works together. I decided to go with GPS instead of CAN because I can play it in any vehicle. Also, getting a dbc file is not easy. Moreover, CAN communication also happens at 10Hz for a generic dbc file as far as I know, so I did not think that CAN would be necessarily better.

    Hopefully this helps you guys in understanding the issue quickly and help me with some suggestions.

    Anxiously waiting,
    Mayo
    Last edited by mayo; 10-09-2019 at 04:30 PM.

  2. #2
    I assume that you mean that pitch tone speeds up slow compared with the car speed.
    The first question will be what is your top speed reading from the GPS.
    Second will be if GPS really update your speed at 10HZ. I really doubt that. Maybe 10HZ is maximum speed of the reading from the GPS through serial , not the actual real time speed GPS update.
    If you know what are the top speed (bottom will be zero or negative and you can use abs(speed)) it's easy use the map function to scale the pitch relative to the velocity.

  3. #3
    Junior Member
    Join Date
    Oct 2019
    Posts
    6
    Quote Originally Posted by Stendall View Post
    I assume that you mean that pitch tone speeds up slow compared with the car speed.
    Yes that is correct. The issue I think is with the GPS not updating quick enough.

    Quote Originally Posted by Stendall View Post
    The first question will be what is your top speed reading from the GPS..
    I plan to stop the sound at 45kph since the tire and road noise will take over at this speed.

    Quote Originally Posted by Stendall View Post
    Second will be if GPS really update your speed at 10HZ. I really doubt that. Maybe 10HZ is maximum speed of the reading from the GPS through serial , not the actual real time speed GPS update.
    The GPS supports 10Hz updates but yes, it is not updating quickly. One important point is the GPS was quick enough when I tested the GPS code by itself (not with audio). The issue is when I combine GPS with Audio.

    Quote Originally Posted by Stendall View Post
    If you know what are the top speed (bottom will be zero or negative and you can use abs(speed)) it's easy use the map function to scale the pitch relative to the velocity.
    I believe I have already done this. Code snippet below:

    Code:
    multiplier = pow(1.0125, MySpeed)
    The multiplier updates the frequency.

  4. #4
    I believe I have already done this. Code snippet below:

    Code:
    multiplier = pow(1.0125, MySpeed)
    The multiplier updates the frequency.
    That depends on what is the pitch you want to be the minimum ( for 0kph I guess) and that the maximum (for 45kph)
    Instead using pow that in the event of readings outside scale do not limit function return values, you could use the map function.

    Let's say that you want the pitch for the slowest speed (0kph) to be the half of nominal (multiplier=1.0) pitch and the pitch for the top speed to be 50% higher than nominal.
    Then you can use this to calculate the pitch multiplier:
    Code:
    pitch_multiplier=map( speed_kph ,0, 45,  0.5 , 1.5 )
    Where the parameters of the map function are:
    Speed in Kph
    Lowest speed in Kph
    Top speed in Kph
    Desired pitch multiplier for lowest speed (1.0=Nominal pitch multiplier)
    Desired pith multiplier for top speed.

    The pitch multiplier returned scales evenly from 0.5 to 1.5 for input speed values from 0 to 45pkh. But if the input values of speed are lower o higher than 0 or 45 the function clamps the input (and thus output) to the limits.
    Now you can use the pitch multiplier to multiply the frequency of the sine generators..

    Maybe I'm telling you something that you already know and not focusing in the real problem that you have. Please accept my excuses if that is the case.
    Last edited by Stendall; 10-09-2019 at 07:28 PM.

  5. #5
    Junior Member
    Join Date
    Oct 2019
    Posts
    6
    Maybe I'm telling you something that you already know and not focusing in the real problem that you have. Please accept my excuses if that is the case.
    No, that's fine, I learnt something new.

    Although that was not the main issue. I used NeoGPS library for quicker updates. Seems to be better than before. Now the new issue I am having is a stair-step sound as the frequency changes with speed.

    For example, if I am driving at 5kph and then give a high accelerator input, the sound directly jumps to that frequency rather than traversing quickly through all the frequencies before it.
    I tried AudioInterrupts but it doesn't fix the issue.

    Any thoughts?

  6. #6
    Quote Originally Posted by mayo View Post
    No, that's fine, I learnt something new.

    For example, if I am driving at 5kph and then give a high accelerator input, the sound directly jumps to that frequency rather than traversing quickly through all the frequencies before it.
    I tried AudioInterrupts but it doesn't fix the issue.

    Any thoughts?
    Yep. Use 2 variables to store velocity.
    One will have the real_velocity reported by the GPS, the other one will have the virtual_velocity used to calc the pitch.
    Use a PID or similar algorithm to approach the virtual_velocity to real_velocity.
    There are a ton of simple ways to do that more or less refined.

    Fast and simple way:
    You can use
    increment=(real_velocity-virtual_velocity)/10
    and use that value like:
    virtual_velocity+=increment

    Hope it helps.

  7. #7
    Junior Member
    Join Date
    Oct 2019
    Posts
    6
    Okay, so I tried that, but it did not work. The speed difference is 0 even after trying at different acceleration rates (light, medium, heavy). I now think the issue is with the audio update.

    Probably I am using
    Code:
    AudioNoInterrupts()
    and
    Code:
    AudioInterrupts()
    incorrectly?

    Or maybe going the GPS route was wrong and I should use CAN?

    I also have audio files for bench mode vs. vehicle mode so that you can clearly hear the difference. However, for some reason I am unable to upload WAV files.

    Code Below:

    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    #include <Adafruit_GPS.h>
    #include "NMEAGPS.h"
    #define PMTK_SET_NMEA_UPDATE_10HZ "$PMTK220,100*2F"
    
    HardwareSerial & gps_port = Serial1; // an alias, if you have a Mega
    
    NMEAGPS gps;
    gps_fix fix;
    
    
    
    
    // Number of samples in each delay line. Change length to 0 to remove effect of flange
    #define FLANGE_DELAY_LENGTH (6*AUDIO_BLOCK_SAMPLES)
    // Allocate the delay line
    short delayBuffer[FLANGE_DELAY_LENGTH];
    
    int offset1 = FLANGE_DELAY_LENGTH / 6;
    //int offset1 = 0;
    int depth = FLANGE_DELAY_LENGTH / 6;
    //int depth = 0;
    //Change delayRate to 0 to remove effect of flange
    double delayRate = 0.5;
    //double delayRate = 0.0;
    
    //Define Bench Test variables
    
    int BENCH = 1; //1 for bench mode, 0 for vehicle mode
    int up_down = 2; //0 - up, 1 - down, 2 - up & down
    float pi = 3.14159265359; //  PI!!!
    
    //float freq1 = 291.3;
    float freq1 = 200;
    float freq2 = 41;
    float freq3 = freq1 * 1.45;
    float freq4 = 43;
    //float freq5 = 600;
    float freq5 = 250;
    float freq6 = 47;
    //float freq7 = freq5 * 1.5;
    float freq7 = freq5 * 3.6;
    float freq8 = 51;
    
    float freq9 = 210.7;
    float freq10 = 41;
    float freq11 = freq9 * 1.45;
    float freq12 = 43;
    //float freq13 = 632.1;
    float freq13 = 311;
    float freq14 = 47;
    //float freq15 = freq13 * 1.41;
    float freq15 = freq13 * 2.87;
    float freq16 = 51;
    
    float MySpeed = 0.0;
    float tempSpeed = 0.0;
    float speed_map [] = { 0, 1,
                           5, 1.040645,
                           10, 1.0829,
                           15, 1.126959,
                           20, 1.172764,
                           25, 1.220431,
                           30, 1.270036,
                           35, 1.321657,
                           40, 1.375376,
                           45, 1.431278
                         };
    float multiplier = 1.0;
    
    // GUItool: begin automatically generated code
    AudioSynthWaveformSine   sine7;          //xy=160.20001220703125,371.20001220703125
    AudioSynthWaveformSine   sine4;          //xy=162.20001220703125,218.1999969482422
    AudioSynthWaveformSine   sine5;          //xy=163.20001220703125,267.20001220703125
    AudioSynthWaveformSine   sine1;          //xy=164.1999969482422,66.19999694824219
    AudioSynthWaveformSine   sine6;          //xy=163.20001220703125,320.20001220703125
    AudioSynthWaveformSine   sine2;          //xy=164.20001220703125,119.19999694824219
    AudioSynthWaveformSine   sine3;          //xy=164.20001220703125,168.1999969482422
    AudioSynthWaveformSine   sine8;          //xy=164.20001220703125,422.20001220703125
    AudioSynthWaveformSine   sine10;         //xy=164.20001220703125,535.2000122070312
    AudioSynthWaveformSine   sine11;         //xy=164.20001220703125,590.2000122070312
    AudioSynthWaveformSine   sine9;          //xy=165.20001220703125,486.20001220703125
    AudioSynthWaveformSine   sine12;         //xy=165.20001220703125,637.2000122070312
    AudioSynthWaveformSine   sine13;         //xy=166.20001220703125,692.2000122070312
    AudioSynthWaveformSine   sine14;         //xy=167.20001220703125,740.2000122070312
    AudioSynthWaveformSine   sine15;         //xy=168.20001220703125,795.2000122070312
    AudioSynthWaveformSine   sine16;         //xy=171.20001220703125,844.2000122070312
    AudioEffectMultiply      multiply1;      //xy=315.20001220703125,92.19999694824219
    AudioEffectMultiply      multiply6;      //xy=317.20001220703125,609.2000122070312
    AudioEffectMultiply      multiply7;      //xy=317.20001220703125,716.2000122070312
    AudioEffectMultiply      multiply2;      //xy=321.20001220703125,200.1999969482422
    AudioEffectMultiply      multiply3;      //xy=321.20001220703125,292.20001220703125
    AudioEffectMultiply      multiply5;      //xy=321.20001220703125,507.20001220703125
    AudioEffectMultiply      multiply4;      //xy=324.20001220703125,396.20001220703125
    AudioEffectMultiply      multiply8;      //xy=323.1999969482422,808.1999969482422
    AudioMixer4              mixer2;         //xy=509.2000427246094,652.2000122070312
    AudioMixer4              mixer1;         //xy=524.2000122070312,239.1999969482422
    AudioMixer4              mixer3;         //xy=640.2000122070312,455.20001220703125
    AudioEffectFlange        flange1;        //xy=767.2000122070312,232.1999969482422
    AudioEffectFreeverb      freeverb1;      //xy=778.2000122070312,315.20001220703125
    AudioOutputI2S           i2s1;           //xy=925.199951171875,131.1999969482422
    AudioMixer4              mixer4;         //xy=946.1999969482422,277.1999969482422
    AudioConnection          patchCord1(sine7, 0, multiply4, 0);
    AudioConnection          patchCord2(sine4, 0, multiply2, 1);
    AudioConnection          patchCord3(sine5, 0, multiply3, 0);
    AudioConnection          patchCord4(sine1, 0, multiply1, 0);
    AudioConnection          patchCord5(sine6, 0, multiply3, 1);
    AudioConnection          patchCord6(sine2, 0, multiply1, 1);
    AudioConnection          patchCord7(sine3, 0, multiply2, 0);
    AudioConnection          patchCord8(sine8, 0, multiply4, 1);
    AudioConnection          patchCord9(sine10, 0, multiply5, 1);
    AudioConnection          patchCord10(sine11, 0, multiply6, 0);
    AudioConnection          patchCord11(sine9, 0, multiply5, 0);
    AudioConnection          patchCord12(sine12, 0, multiply6, 1);
    AudioConnection          patchCord13(sine13, 0, multiply7, 0);
    AudioConnection          patchCord14(sine14, 0, multiply7, 1);
    AudioConnection          patchCord15(sine15, 0, multiply8, 0);
    AudioConnection          patchCord16(sine16, 0, multiply8, 1);
    AudioConnection          patchCord17(multiply1, 0, mixer1, 0);
    AudioConnection          patchCord18(multiply6, 0, mixer2, 1);
    AudioConnection          patchCord19(multiply7, 0, mixer2, 2);
    AudioConnection          patchCord20(multiply2, 0, mixer1, 1);
    AudioConnection          patchCord21(multiply3, 0, mixer1, 2);
    AudioConnection          patchCord22(multiply5, 0, mixer2, 0);
    AudioConnection          patchCord23(multiply4, 0, mixer1, 3);
    AudioConnection          patchCord24(multiply8, 0, mixer2, 3);
    AudioConnection          patchCord25(mixer2, 0, mixer3, 1);
    AudioConnection          patchCord26(mixer1, 0, mixer3, 0);
    AudioConnection          patchCord27(mixer3, flange1);
    AudioConnection          patchCord28(mixer3, freeverb1);
    AudioConnection          patchCord29(flange1, 0, mixer4, 0);
    AudioConnection          patchCord30(freeverb1, 0, mixer4, 1);
    AudioConnection          patchCord31(mixer4, 0, i2s1, 0);
    AudioConnection          patchCord32(mixer4, 0, i2s1, 1);
    AudioControlSGTL5000     sgtl5000_1;     //xy=475.2000427246094,20
    // GUItool: end automatically generated code
    
    
    void setup() {
      // put your setup code here, to run once:
    
      Serial.begin( 115200 );
      gps_port.begin( 9600 );
      Serial1.println(PMTK_SET_NMEA_UPDATE_10HZ);
    
      AudioMemory(20);
      sgtl5000_1.enable();
      sgtl5000_1.volume(0.75);
    
      //Set to zero to negate effect
      freeverb1.roomsize(0.3);
      freeverb1.damping(1);
    
      flange1.begin(delayBuffer, FLANGE_DELAY_LENGTH, offset1, depth, delayRate);
      //flange1.voices(offset1,depth,delayRate);
    
    }
    
    
    void loop() {
      // put your main code here, to run repeatedly:
    
    
      //Serial.print("Test Mode: ");
      //Serial.println(BENCH);
      sine1.amplitude(0.85);
      sine2.amplitude(0.1);
      sine3.amplitude(0.5);
      sine4.amplitude(0.1);
      sine5.amplitude(0.1);
      sine6.amplitude(0.05);
      sine7.amplitude(0.25);
      sine8.amplitude(0.05);
      sine9.amplitude(0.85);
      sine10.amplitude(0.1);
      sine11.amplitude(0.5);
      sine12.amplitude(0.1);
      sine13.amplitude(0.1);
      sine14.amplitude(0.05);
      sine15.amplitude(0.25);
      sine16.amplitude(0.05);
    
    
      //AudioNoInterrupts();
    
      if (BENCH == 1)
      {
        Serial.println("Bench Testing...");
        Serial.print("up_down value: ");
        Serial.println(up_down);
        updateSound(MySpeed, up_down);
      }
    
      else
      {
        //Serial.println("Vehicle Mode. Else Condition");
    
        AudioNoInterrupts();
        if (gps.available( gps_port ))
        {
          fix = gps.read();
          MySpeed = fix.speed_kph();
          tempSpeed = MySpeed;
        }
    
        up_down = -1;
    
        AudioInterrupts();
        updateSound(MySpeed, up_down);
    
    
        //multiplier = pow(1.012, MySpeed);
        multiplier = map(MySpeed, 0, 45, 1, 1.374941);
    
        //Serial.print("Speed (kph): ");
        //Serial.println(MySpeed);
        //Serial.println(multiplier);
        
      }
    
    }
    
    void updateSound(float MySpeed, int up_down)
    {
    
      int temp = 0;
      //Serial.print("Value of up_down: ");
      //Serial.println(up_down);
    
      if (up_down == 2)
      {
        Serial.println("up_down was 2. Now resetting to 1. Temp is 1");
        up_down = 0;
        temp = 1;
      }
    
    
      if (up_down == 0)
      {
        Serial.println("up_down is 0");
        MySpeed = 0;
        multiplier = 1.0;
    
        while (MySpeed <= 45)
        {
          frequencyUpdate(multiplier);
    
          if (MySpeed == 45 || MySpeed == 0)
          {
            delay(2000); //hold for 3s to show stationary noise
            Serial.println("Inside if statement. Multiplier is 1. Delay 3s");
          }
    
    
          delay(30);
          Serial.print("Speed (kph): ");
          Serial.print(MySpeed);
          Serial.print("  Multiplier: ");
          Serial.println(multiplier);
          MySpeed += 0.1;
          //MySpeed += 2;
          multiplier = map(MySpeed, 0, 45, 1, 1.374941);
          //multiplier += 0.001;
        }
    
        if (temp == 1)
        {
          Serial.println("Temp is 1. up_down is 1");
          up_down = 1;
        }
    
      }
    
      if (up_down == 1)
      {
        Serial.println("up_down is 1");
        MySpeed = 45;
        multiplier = 1.45;
        while (MySpeed >= 0)
        {
          frequencyUpdate(multiplier);
          if (MySpeed == 32 || MySpeed == 0)
          {
            //Serial.println("Speed is 32kph. Resetting variables...");
            delay(2000); //hold for 3s to show stationary noise
          }
    
          delay(30);
          Serial.print("Speed (kph): ");
          Serial.print(MySpeed);
          Serial.print("  Multiplier: ");
          Serial.println(multiplier);
          MySpeed -= 0.1;
          //MySpeed -= 2;
          multiplier = map(MySpeed, 0, 45, 1, 1.374941);
          //multiplier -= 0.001;
        }
        if (temp == 1)
        {
          up_down = 0;
        }
      }
    
      if (up_down == -1)
      {
        //AudioNoInterrupts();
        //multiplier = pow(1.012, MySpeed);
        //multiplier = map(MySpeed, 0, 45, 1, 1.374941);
        MySpeed = fix.speed_kph();
        //Serial.print("tempSpeed: ");
        //Serial.print(tempSpeed);
        //Serial.print(" MySpeed: ");
        Serial.println(MySpeed-tempSpeed);
    
        if (tempSpeed < MySpeed)
        {
          while (tempSpeed < MySpeed)
          {
            multiplier = map(tempSpeed, 0, 45, 1, 1.374941);
            frequencyUpdate(multiplier);
            tempSpeed += 0.1;
          }
        }
    
        if (tempSpeed > MySpeed)
        {
          while (tempSpeed > MySpeed)
          {
            multiplier = map(tempSpeed, 0, 45, 1, 1.374941);
            frequencyUpdate(multiplier);
            tempSpeed -= 0.1;
          }
        }
    
        if (tempSpeed == MySpeed)
        {
          for (int ii = 0; ii < 20; ii = ii + 2)
          {
            if (ii > 0)
            {
              if (MySpeed > speed_map[ii])
              {
                multiplier = ((speed_map[ii + 3] - speed_map[ii - 1]) / (speed_map[ii + 2] - speed_map[ii])) * (MySpeed - speed_map[ii]) + speed_map[ii + 1];
              }
            }
            if (ii == 0 && MySpeed == speed_map[ii])
            {
              multiplier = speed_map[ii + 1];
            }
          }
        }
    
    
        //AudioInterrupts();
    
        frequencyUpdate(multiplier);
      }
    }
    
    void frequencyUpdate (float multiplier)
    {
    
      sine1.frequency(freq2);
      sine2.frequency(freq1 * multiplier);
    
      sine3.frequency(freq4);
      sine4.frequency(freq3 * multiplier);
    
      sine5.frequency(freq6);
      sine6.frequency(freq5 * multiplier);
    
      sine7.frequency(freq8);
      sine8.frequency(freq7 * multiplier);
    
      sine1.frequency(freq10);
      sine2.frequency(freq9 * multiplier);
    
      sine3.frequency(freq12);
      sine4.frequency(freq11 * multiplier);
    
      sine5.frequency(freq14);
      sine6.frequency(freq13 * multiplier);
    
      sine7.frequency(freq16);
      sine8.frequency(freq15 * multiplier);
    }

  8. #8
    Junior Member
    Join Date
    Oct 2019
    Posts
    6
    Just found this thread: https://forum.pjrc.com/threads/26686...t-based-timing

    It talks about placement of code to talk to the hardware. Do you think this maybe applicable to me? I am going to move my code around to see if that works. Will post an update soon.

  9. #9
    Junior Member
    Join Date
    Oct 2019
    Posts
    6
    Update -- That did not help

  10. #10
    Senior Member
    Join Date
    Jul 2014
    Posts
    2,343
    Quote Originally Posted by mayo View Post

    Probably I am using
    Code:
    AudioNoInterrupts()
    and
    Code:
    AudioInterrupts()
    incorrectly?
    what happens if you use "AudioNoInterrupts" ONLY in the beginning and AudioInterrupts at the end of frequencyUpdate ?
    IMO that is the only place where it matters (you stop and restart audio processing only while you change frequency

  11. #11
    I see why the two speed variables don't do the trick for you:
    You do all in one loop:

    Code:
        if (tempSpeed < MySpeed)                                                               
           {                                                                                      
             while (tempSpeed < MySpeed)                                                          
             {                                                                                    
               multiplier = map(tempSpeed, 0, 45, 1, 1.374941);                                   
               frequencyUpdate(multiplier);                                                       
               tempSpeed += 0.1;                                                                  
             }                                                                                    
           }
    Code:
    void frequencyUpdate (float multiplier)
    {
    
      sine1.frequency(freq2);
      sine2.frequency(freq1 * multiplier);
    
      sine3.frequency(freq4);
      sine4.frequency(freq3 * multiplier);
    
      sine5.frequency(freq6);
      sine6.frequency(freq5 * multiplier);
    
      sine7.frequency(freq8);
      sine8.frequency(freq7 * multiplier);
    
      sine1.frequency(freq10);
      sine2.frequency(freq9 * multiplier);
    
      sine3.frequency(freq12);
      sine4.frequency(freq11 * multiplier);
    
      sine5.frequency(freq14);
      sine6.frequency(freq13 * multiplier);
    
      sine7.frequency(freq16);
      sine8.frequency(freq15 * multiplier);
    }
    ¿What it's the point in execute all math at once in one closed loop that will take not time at all?

    You are doing this:

    Code:
    Pitch
    |
    |             ..........
    |             :
    |             :
    |.............:
    |________________________Time
    Instead of this:
    Code:
    Pitch
    |               ..........
    |           ...:
    |       ...:
    |   ...:
    |..:
    |_________________________Time
    Approximate the virtual_speed to the real_speed at constant rate, lets say 10-20 times every second. Not at once in one loop that will take 1 millisecond.
    https://www.pjrc.com/teensy/td_timin...pedMillis.html

Posting Permissions

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