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

Thread: Audio Adapter Board for Teensy with custom audio file and sound enhancement algorithm

  1. #1
    Junior Member
    Join Date
    Jan 2020

    Audio Adapter Board for Teensy with custom audio file and sound enhancement algorithm

    Hi! I have an application that requires taking a sound file (44 KHz mono, 3-5 minutes nominal), dynamically varying the replay rate (slower and faster), and then amplifying the sound for small speakers. It looks like the 'Audio Adapter' board for Teensy might be just the thing I need.

    Does anyone know if dynamically varying the replay rate is supported by the Teensy Audio Library? Can anyone suggest an inexpensive amplification electronics module (and miniature speakers) at the Audio Adapter's output to produce sound easily audible at 30 feet or so? Any hints, advice and suggestions would be appreciated to get me started.

    Ted Jackson

  2. #2
    Varying playback speed sounds simple but really isn't, and while I've never used the audio library, I would be 99.9% sure this wasn't built in because it's not something a user would be likely to want.
    Consider a vinyl, if you want to slow that down you just turn the motor speed down a notch - easy.

    Now consider your DAC board, firing out 44k conversions per second, and you want to slow this down. You're options are
    - change the clock rate
    - do maths on the samples

    changing clock rates is a bit of a field, but at relatively slows clocks like 44kHz you can get away with a lot more than at higher speeds. Any clock must either be generated by an oscilator, or generated by dividing down another clock (or multiplying, but I dont think teensy has PLL's built in :P). So you could take a 100kHz clock, and get a 50kHz clock by halving it, then 25k by halving it again and so on, but you'll notice this would be non-linear.
    Teensy has hardware timers built-in that can trigger reliably down to microseconds. Ignoring a few other details, lets say you use a 1 us timer, and you use it to toggle your clock every X number of counts to get a varible clock rate. Toggling every 11 uS is a clock rate of 45kHz - close enough right? Your next clock is once every 12uS at 41.6kHz, then 38.4k and so on. These steps wouldn't sound smooth at all. Depending on how good someone's ear is you probably need 10x the resolution. Finally, we're also assuming the DAC is happy with it's clock rate being changed on the fly, and that it expects this clock to be the sample rate, two assumptions big enough to choke on. This method isn't impossible, but unlikely to be practical with this hardware.

    FFT is the better method, and to my understanding the one used en mass. DJ software like serato and Ableton merrily allows the user to change the speed of tracks (even without changing pitch). We're starting to get towards the edge of my knowledge, but you can operate on the track's frequency components rather than it's discrete value at a given time. You can then use these altered frequency components to convert back into a discrete value over time but crucially you can make a slight change that still gets played out at 44kHz. It's probably possible to code this into teensy, particularly a T4 now that that monster is out. But it's not a trivial project!

  3. #3
    Junior Member
    Join Date
    Jan 2020

    Resampling Algorithm

    Thanks for your detailed response, Edward. I already have a simple algorithm that splits up the waveform into small sections and resamples the sections via interpolation according to whatever rescale factor I want at that moment in time, which seems to give good results. Hear attached. You'll notice that I vary the effective play-back rate quite dramatically. I've looked at a couple of other algorithms that use more complex shifting of the STFT, but my method gives more pleasing results. I should be able to implement that fairly simply. So, I don't really need a lot of computational power. But I like the line-out aspect of the chip, so I may try it anyway as a time saver. Suggestions for amp electronics and speakers would still be useful though. Thanks.
    Attached Files Attached Files

  4. #4
    Senior Member DD4WH's Avatar
    Join Date
    Oct 2015
    Central Europe
    Hi Ted,

    from your sound file I understand that you would like to change the playback speed, but you do not care about maintaining the original pitch during playback. Is that right?

    If so, your goal can be easily achieved by just changing the sample rate of the codec/DAC during playback.

    FrankB introduced a way to change the sample rate for Teensy 3.2 / T3.6:

    For T4.0, the code has also been put together by FrankB and allows to set every sample rate the codec can cope with [up to about 234ksps is possible with the SGTL5000 on the Teensy audioshield, although it is only specified up to 96ksps]:

    void setI2SFreq(int freq) {   // thanks FrankB !
      // PLL between 27*24 = 648MHz und 54*24=1296MHz
      int n1 = 4; //SAI prescaler 4 => (n1*n2) = multiple of 4
      int n2 = 1 + (24000000 * 27) / (freq * 256 * n1);
      double C = ((double)freq * 256 * n1 * n2) / 24000000;
      int c0 = C;
      int c2 = 10000;
      int c1 = C * c2 - (c0 * c2);
      set_audioClock(c0, c1, c2, true);
           | CCM_CS1CDR_SAI1_CLK_PRED(n1-1) // &0x07
           | CCM_CS1CDR_SAI1_CLK_PODF(n2-1); // &0x3f 

  5. #5
    Junior Member
    Join Date
    Jan 2020
    Hi DD4WH,

    >from your sound file I understand that you would like to change the playback speed, but you do not care about maintaining the original pitch during playback. Is that right?


    Thanks for the suggestions. I have several approaches to try. In the meantime, any suggestions on amp and speakers? :-)

    Thanks, Ted

Posting Permissions

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