How to change AUDIO_BLOCK_SAMPLES?

Tibbar

Member
Hi,

I've seen some posts that changing AUDIO_BLOCK_SAMPLES in AudioStream is possible, but I'm not exactly sure on how to do it. It involves deleting and cloning cores in Teensyduino?

I'm a bit of a newbie and any help would be much appreciated! :D

(Also, I'm using a Mac with Arduino IDE 1.8.7 and Teensyduino 1.45)
 
Hi,

I've seen some posts that changing AUDIO_BLOCK_SAMPLES in AudioStream is possible, but I'm not exactly sure on how to do it. It involves deleting and cloning cores in Teensyduino?

I'm a bit of a newbie and any help would be much appreciated! :D

(Also, I'm using a Mac with Arduino IDE 1.8.7 and Teensyduino 1.45)

you simply change the number in AudioStream, and force a complete recompile by changing any tools-menu item (e.g changing CPU speed ot Optimize option)
 
Yeah but where exactly is AudioStream.h? I can't find it in the libs and have read that its in the core, but can find that either.
 
I found it here:

Code:
...\hardware\teensy\avr\cores\teensy3\AudioStream.h:
   38  #endif
   39  
   40: // AUDIO_BLOCK_SAMPLES determines how many samples the audio library processes
   41  // per update.  It may be reduced to achieve lower latency response to events,
   42  // at the expense of higher interrupt and DMA setup overhead.
   ..
   50  #ifndef AUDIO_BLOCK_SAMPLES
   51  #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
   52: #define AUDIO_BLOCK_SAMPLES  128
   53  #elif defined(__MKL26Z64__)
   54: #define AUDIO_BLOCK_SAMPLES  64
   55  #endif
   56  #endif
 
Man... Open the applications folder on your Mac, right click on the Arduino app icon, select "show package contents". Then navigate through Contents -> Java. From there, follow the path which defragster indicated.
 
Ok, the Contents -> Java part. In Contents there should be a folder called Java?

Sorry if I'm asking dumb questions. I am a total noob though.
 
Location of AudioStream.h

Ok, the Contents -> Java part. In Contents there should be a folder called Java?

Once you enter the app folder, the path should be

/Contents/Java/hardware/teensy/avr/cores/teensy3
or
/Contents/Java/hardware/teensy/avr/cores/teensy4
 
I think you can just #define AUDIO_BLOCK_SAMPLES _before_ #include'ing "Audio.h"

ie
Code:
#define AUDIO_BLOCK_SAMPLES 64
#include <Audio.h>

I think this is all you need to override it. If you look in AudioStream.h you'll see
Code:
#ifndef AUDIO_BLOCK_SAMPLES
#define AUDIO_BLOCK_SAMPLES  128
#endif

#ifndef AUDIO_SAMPLE_RATE_EXACT
#define AUDIO_SAMPLE_RATE_EXACT 44100.0f
#endif

So these two parameters are designed to be overridden (caveat - I've never actually tested this personally, and this is the T4
version I'm looking at)

Be aware it should be a power of 2 and some Audio lib classes don't work with different values from 128.
 
Perhaps then it should be more like:

Code:
#ifdef AUDIO_BLOCK_SAMPLES
#error You should override AUDIO_BLOCK_SAMPLES in the file AudioStream.h
#endif
// Change here for different block size, note some functionality in the Audio library will be broken by this
#define AUDIO_BLOCK_SAMPLES  128
 
Perhaps then it should be more like:

I just added a reminder in the comments on my program. However, being a global IDE change, you inspired me to also add a #warning message to pop up text in compile time.

Code:
#ifndef AUDIO_BLOCK_SAMPLES
#define AUDIO_BLOCK_SAMPLES  16
#warning AUDIO_BLOCK_SAMPLES modified by hand from 128 to 16 in .../hardware/teensy/avr/cores/teensy4/AudioStream.h.
#endif

#ifndef AUDIO_SAMPLE_RATE_EXACT
#define AUDIO_SAMPLE_RATE_EXACT 16000.0f
#warning AUDIO_SAMPLE_RATE_EXACT modified by hand from 44100.0f to 16000.0f in .../hardware/teensy/avr/cores/teensy4/AudioStream.h.
#endif

Screen Shot 2022-02-14 at 20.45.16.png

That will help when re-compiling the code in the future.
 

Attachments

  • Screen Shot 2022-02-14 at 20.28.38.png
    Screen Shot 2022-02-14 at 20.28.38.png
    46.2 KB · Views: 45
Back
Top