Changing the delay time

Store Forums Audio Hacker Discussion and Project Ideas Changing the delay time

Tagged: 

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #8533
    CharlesM
    Participant

    Hi — I’m working to build a delay line with the Audio Hacker like the one mentioned a few topics back.
    Unfortunately, this is my first project and I’m quite a noobie.

    I’m confused as to how to control the length of the delay.

    Do I need to change: delayValue and does that change the echoDelay ?

    Any guidance would be helpful.

    Thanks, Charles

    #8539
    Michael
    Keymaster

    Hi Charles,
    I think you are referring to this post: https://nootropicdesign.com/store/forums/topic/audio-delay-line/

    The original poster wanted to use buttons to control the delay amount instead of a pot, so that’s why he introduced the delayValue variable.

    To control the delay amount, change echoDelay and make sure it is a multiple of 3. You may choose to control echoDelay with a pot on A0. The original code is:

    echoDelay = analogRead(0) * 30;

    and that means that echoDelay will have a value from 0-30690.

    #8543
    CharlesM
    Participant

    Thanks very much for your help…

    I’ll give this a try and maybe try buttons later. So far, I’m delaying a radio signal and it sounds fine at the default sampling rate.

    #8625
    Photoflash
    Participant

    Howdy, Michael —

    I’m still having a bit of trouble changing the length of the delay to more than the 2 seconds it produced originally.

    I change echoDelay = 24000 but that has no effect. Is there another delay modification that I’m not finding?

    Any help would be appreciated.

    Thanks, Charles

    #8628
    Michael
    Keymaster

    That should be the only variable you change, replacing the analogRead(0) line. Please post your whole code surrounded by the code tags so it is formatted.

    #8629
    Photoflash
    Participant

    Here’s the code:

    
    #include <AudioHacker.h>
    
    #define DEBUG
    
    unsigned int playbackBuf;
    unsigned int sampleRate;
    unsigned int readBuf[2];
    unsigned int writeBuf;
    boolean evenCycle = true;
    unsigned int timer1Start;
    volatile unsigned int timer1EndEven;
    volatile unsigned int timer1EndOdd;
    unsigned long lastDebugPrint = 0;
    volatile long address = 0;
    unsigned int echoDelay;
    boolean echoWrapped = false;
    
    void setup() {
    #ifdef DEBUG
      Serial.begin(115200);        // connect to the serial port
    #endif
    
      playbackBuf = 2048;
      sampleRate = DEFAULT_RECORDING_SAMPLE_RATE;
      timer1Start = UINT16_MAX - (F_CPU / sampleRate);
    
      AudioHacker.begin();
    
    #ifdef DEBUG
      Serial.print("sample rate = ");
      Serial.print(sampleRate);
      Serial.print(" Hz");
      Serial.println();
    #endif
    }
    
    void loop() {
    
      delay(300);
      // echoDelay is number of memory slots back into the past to read for the echo.
      // must be a factor of 3 because we pack 2 samples into each 3-byte sequence of SRAM.
      echoDelay = 24000; 
    
    #ifdef DEBUG
      if ((millis() - lastDebugPrint) >= 1000) {
        lastDebugPrint = millis();
    
        //each 3 echoDelay2 is 2 samples.  
        unsigned int delayMillis = (float)(((float)((echoDelay * 2) / 3)) / (float)(sampleRate/1000.0));
    
        Serial.print("echo delay = ");
        Serial.print(delayMillis);
        Serial.print(" ms    even cycles remaining = ");
        Serial.print(UINT16_MAX - timer1EndEven);
        Serial.print("   odd cycles remaining = ");
        Serial.print(UINT16_MAX - timer1EndOdd);
        Serial.println();
      }
    }
    #endif
    
    ISR(TIMER1_OVF_vect) {
      TCNT1 = timer1Start;
      unsigned int signal;
      unsigned int echo;
      int mix;
    
      AudioHacker.writeDAC(playbackBuf);
    
      // Read ADC
      signal = AudioHacker.readADC();
    
      if (evenCycle) {
        long echoAddress = address - echoDelay;
        if (echoAddress < 0) {
          echoAddress += MAX_ADDR;
        }
        AudioHacker.readSRAMPacked(0, echoAddress, readBuf);
        if ((!echoWrapped) && (echoAddress > address)) {
          // avoid reading from unwritten memory
          echo = 2048;
          readBuf[1] = 2048;
        } else {
          echo = readBuf[0];
        }
      } else {
        echo = readBuf[1];
      }
      if (echoDelay == 0) {
        echo = 2048;
      }
    
      if (evenCycle) {
        writeBuf = signal;
      } else {
        AudioHacker.writeSRAMPacked(0, address, writeBuf, signal);
        address += 3;
        if (address > MAX_ADDR) {
          address = 0;
          echoWrapped = true;
        }
      }
    
      
      playbackBuf = echo;
    
    #ifdef DEBUG
      if (evenCycle) {
        timer1EndEven = TCNT1;
      } else {
        timer1EndOdd = TCNT1;
      }
    #endif
      evenCycle = !evenCycle;
    }
    #8633
    Michael
    Keymaster

    So, changing echoDelay to different values has no effect at all? How long is the delay when you use a value of 24000? What do you hear? What happens when you change it to 9000? It must be divisible by 3.

    #8635
    Photoflash
    Participant

    Changing the echoDelay value seems to have no audible effect at all. I’ve tried many different values from around 300 to 48000 (all divisible by 3), and I can’t hear, or measure with a stopwatch, any significant change in the delay, which is consistently around 2 sec.

    But when I use a very high value, such as 90000, it produces a lot of feedback-like noise, so there definitely is a maximum limit.

    I’m not using any pots, but just changing the value in the code and uploading to the Audio Hacker to test it out.

    #8636
    Michael
    Keymaster

    Hmm. This is strange. I don’t see anything wrong with this code. Did you ever use this echo example with a potentiometer? Did it ever work?
    I’m not home right now (on a business trip), but will try this code when I get back to my lab.

    #8644
    Photoflash
    Participant

    I have the DJ Shield (unassembled, unfortunately) so I’ll put that together and try using a pot with the echo example. Hopefully, I’ll get to it this weekend.

    #8645
    Michael
    Keymaster

    Photoflash, I have tried your code above and it works fine for me. A small value for echoDelay gives a small delay, and a large value gives a longer delay. I do not know why this would not work for you. Are you sure you are making the code change then uploading the change successfully to the device?

    With the variable echoDelay declared as unsigned int, the max value is 65535. But for longer delays change the declaration of echoDelay to an unsigned long:

    unsigned long echoDelay;

    Then you can use a maximum delay value of 131067 which gives a day of about 4 seconds.

    #8654
    Photoflash
    Participant

    Thanks for the tip… I changed echoDelay to
    unsigned long echoDelay;
    and now have a 4 second delay maximum.

    Do you have any thoughts on how I can reach the 8.6 second maximum, as in the documentation?

    #8657
    Michael
    Keymaster

    The echo example only uses one of the chips for simplicity. That is why the delay is only 4 seconds. If you want it to use both chips, you’d have to modify the code, but the logic becomes more complex to use both chips.

    #8658
    Photoflash
    Participant

    Well, that is disappointing.

    Thanks anyway…

Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.