AudioStream.cpp & AudioStream.h - BUGFIX

kd5rxt-mark

Well-known member
============
Observed problem:
============

Under high-load conditions, values returned by AudioProcessorUsage() & AudioProcessorUsageMax() suffer from "rollover" (values returned can't go all the way to 100%). For values greater than approximately 60.71, the returned values appear to start over at 0. Also, returns from AudioProcessorUsageMax() indicate values that both increase & decrease, where they should be expected to only increase.

=================
To reproduce the problem:
=================

View attachment TeensyMIDIPolySynth.zip
View attachment MERRYG02.zip

Arduino IDE 1.8.13 + Teensy Loader 1.53-beta1 (same with Arduino 1.8.12 + Teensy Loader 1.52)

Arduino IDE Configuration:
Tools/Board: "Teensy 4.0" or "Teensy 4.1"
Tools/USB Type: "Serial + MIDI"
Tools/CPU Speed: "600MHz"
Tools/Optimize: "Fastest"
Tools/Keyboard Layout: "US English"
Tools/Port: "COMx Serial (Teensy 4.0)"

1) Build, load, & run the included TeensyMIDIPolySynth sketch (NOTE: the ability to reproduce the problem requires a sketch that heavily loads the Teensy/Audio/MIDI capabilities. Lest I get my knuckles rapped for not providing a "simple sketch which reproduces the problem", my humble & respectful reply is that, although it may not be a simple sketch, I am able provide a "sketch which *consistently* reproduces the problem", where reproducing the problem is often much more difficult than fixing the problem !!)

2) Play the included MEERYGO2.MID file on your preferred MIDI player, with the "Teensy16PolySynth vXXX" device selected for output

3) In the serial monitor, note that before the MIDI file starts playing, the values returned by AudioProcessorUsage() & AudioProcessorUsageMax() hover just under 50%

4) while playing the MIDI file, the values returned by AudioProcessorUsage() & AudioProcessorUsageMax() drop to around 4%, where they should be expected to increase to a value greater than 50%

=====================
Proposed modification/resolution:
=====================

In AudioStream.cpp, the following is currently found:

Code:
uint16_t AudioStream::cpu_cycles_total = 0;
uint16_t AudioStream::cpu_cycles_total_max = 0;

Change to the following:

Code:
uint32_t AudioStream::cpu_cycles_total = 0;
uint32_t AudioStream::cpu_cycles_total_max = 0;

Likewise, in AudioStream.h, the following is currently found:

Code:
	uint16_t cpu_cycles;
	uint16_t cpu_cycles_max;
	static uint16_t cpu_cycles_total;
	static uint16_t cpu_cycles_total_max;

Change to the following:

Code:
	uint32_t cpu_cycles;
	uint32_t cpu_cycles_max;
	static uint32_t cpu_cycles_total;
	static uint32_t cpu_cycles_total_max;

=======================
Post-modification observed behavior:
=======================

- before the MIDI file starts playing, the values returned by AudioProcessorUsage() & AudioProcessorUsageMax() hover just under 50%

- while playing the MIDI file, the values returned by AudioProcessorUsage() & AudioProcessorUsageMax() increase to around 65%

Mark J Culross
KD5RXT

P.S. I'm not a github kind of guy, so I do not know how to do a proper PR for this. Any assistance and/or pointers would be appreciated. MJC
 
Thanks Paul !! In the ranges that my TeensyMIDIPolySynth sketch loads the processor (50% - 65%), the change resolved the reported roll-over.

Mark J Culross
KD5RXT
 
Back
Top