PDA

View Full Version : Audio Memory/Output Causing glitches



rexhex
02-29-2016, 02:31 AM
I have noticed a glitch in my code. It cycles through different LED settings some using FFT and some not. The settings that don't use FFT glitch out. I have found that when removing both audio memory in setup and AudioOutputI2S the glitch goes away. When adding either one of those pieces the glitch is back.

I have simplified the problem in this code below. Its just a simple fade function but when running it will glitch out. It also does not need to be receiving any audio signal to perform this visual glitch. If I remove the two Audio pieces it runs just fine.

Any Ideas on how to fix this?


#include <FastLED.h>
#define NUM_LEDS 6
#define DATA_PIN 2
#define BUTTON_PIN 8
#include <avr/power.h>


byte buttonChangeClick = 1;
byte buttonChangeHold = 1;
byte buttonChange3 = 1;
byte prevButtonChangeClick = 1;
byte prevButtonChangeHold = 1;
byte prevButtonChange3 = 1;

unsigned long millisTime;
static unsigned last_time = 0;
CRGB leds[NUM_LEDS];

int ledLevelPin = 0;
int ledLevelVal = 0;
int ledLevel;

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

AudioControlSGTL5000 audioShield;

byte fadeColor = 50;
byte fadeCount = 1;


AudioInputI2S audioInput; // audio shield: mic or line-in
AudioSynthWaveformSine sinewave;
AudioAnalyzeFFT1024 myFFT;
AudioOutputI2S audioOutput;

AudioConnection patchCord1(audioInput, 0, myFFT, 0);

const int myInput = AUDIO_INPUT_LINEIN;

void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);

AudioMemory(12);


audioShield.enable();
audioShield.inputSelect(myInput);
audioShield.volume(0.5);


myFFT.windowFunction(AudioWindowHanning1024);

sinewave.amplitude(0.8);
sinewave.frequency(1034.007);
}

void loop()
{
fadeLoop();
}



void fadeLoop()
{
millisTime = millis();
if (millisTime - last_time > 40 && fadeCount == 1)
{

fadeColor ++;
leds[0] = CRGB (fadeColor,0,255);
leds[2] = CRGB (fadeColor,0,255);
leds[4] = CRGB (fadeColor,0,255);

leds[1] = CRGB (fadeColor,0,0);
leds[3] = CRGB (fadeColor,0,0);
leds[5] = CRGB (fadeColor,0,0);

FastLED.show();
last_time = millisTime;
if (fadeColor >= 254) fadeCount =2;
}

if (millisTime - last_time > 40 && fadeCount == 2)
{

fadeColor --;
leds[0] = CRGB (fadeColor,0,255);
leds[2] = CRGB (fadeColor,0,255);
leds[4] = CRGB (fadeColor,0,255);

leds[1] = CRGB (fadeColor,0,0);
leds[3] = CRGB (fadeColor,0,0);
leds[5] = CRGB (fadeColor,0,0);
FastLED.show();
last_time = millisTime;
if (fadeColor <= 50) fadeCount =1;
}
}

alfa66
03-01-2016, 04:18 AM
Try Increasing the value of AudioMemory. Let me know if it works.

rexhex
03-01-2016, 04:45 AM
Alfa66, Thank you for responding to my post. I incremented the memory by 12 until reaching 72. Each increment appeared to have the same exact glitch.

PaulStoffregen
03-01-2016, 04:55 AM
What does "glitch out" mean?

I get that the LEDs are supposed to slowly increase and then decrease in brightness. But what are you actually seeing? Obviously something different, but I'm not getting much idea of what it actually is.

PaulStoffregen
03-01-2016, 05:01 AM
As a quick test, can you try surrounding those two FastLED.show() with the audio interrupt disable code (www.pjrc.com/teensy/td_libs_AudioProcessorUsage.html). Like this:



AudioNoInterrupts();
FastLED.show();
AudioInterrupts();


Make sure you get both of them.

Does this help, or make things even worse?

rexhex
03-01-2016, 05:05 AM
Glitching out is the best way I can think to describe it. What happens is random pixels will flash randomly in a glitchy fashion. So as its fading smoothly, a few pixels flash around. Then continues fading and FLASH while fading FLASH. Maybe two maybe three at once. Sometimes its just one pixel.

I imagine if it had a sound of its own it would be similar to this. https://www.youtube.com/watch?v=SuLCQanvrC8

rexhex
03-01-2016, 05:08 AM
That does not seem to change the effect I am seeing.

PaulStoffregen
03-01-2016, 05:15 AM
Maybe try this?



noInterrupts();
FastLED.show();
interrupts();

rexhex
03-01-2016, 05:24 AM
That also did not fix it.

rexhex
03-01-2016, 06:40 AM
The only thing that does seem to fix it is commenting out the audio memory in the setup and the AudioOutputI2s. Adding either one of these elements brings back the "glitch".

PaulStoffregen
03-01-2016, 08:54 AM
That also did not fix it.

That's very surprising to me. Usually when we see Neopixel and other libs conflict, it's due to interrupt usage conflicts.


The only thing that does seem to fix it is commenting out the audio memory in the setup and the AudioOutputI2s. Adding either one of these elements brings back the "glitch".

Well, why don't you just leave off AudioOutputI2S? You haven't connected any audio stream to it.

Obviously nothing in the audio library runs if you don't allocate any memory for it to use.

PaulStoffregen
03-01-2016, 10:29 AM
Ok, I ran it here, and I managed to reproduce the problem.

6504

It is indeed an interrupt conflict. But FastLED ignores those ways I suggested. The solution is defining FASTLED_ALLOW_INTERRUPTS to 0.

This works:



#define FASTLED_ALLOW_INTERRUPTS 0
#include <FastLED.h>
#define NUM_LEDS 6
#define DATA_PIN 2
#define BUTTON_PIN 8
#include <avr/power.h>

byte buttonChangeClick = 1;
byte buttonChangeHold = 1;
byte buttonChange3 = 1;
byte prevButtonChangeClick = 1;
byte prevButtonChangeHold = 1;
byte prevButtonChange3 = 1;

unsigned long millisTime;
static unsigned last_time = 0;
CRGB leds[NUM_LEDS];

int ledLevelPin = 0;
int ledLevelVal = 0;
int ledLevel;

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

AudioControlSGTL5000 audioShield;

byte fadeColor = 50;
byte fadeCount = 1;

AudioInputI2S audioInput; // audio shield: mic or line-in
AudioSynthWaveformSine sinewave;
AudioAnalyzeFFT1024 myFFT;
AudioOutputI2S audioOutput;

AudioConnection patchCord1(audioInput, 0, myFFT, 0);

const int myInput = AUDIO_INPUT_LINEIN;

void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
AudioMemory(12);

audioShield.enable();
audioShield.inputSelect(myInput);
audioShield.volume(0.5);

myFFT.windowFunction(AudioWindowHanning1024);

sinewave.amplitude(0.8);
sinewave.frequency(1034.007);
}

void fadeLoop();

void loop()
{
fadeLoop();
}



void fadeLoop()
{
millisTime = millis();
if (millisTime - last_time > 40 && fadeCount == 1)
{

fadeColor ++;
leds[0] = CRGB (fadeColor, 0, 255);
leds[2] = CRGB (fadeColor, 0, 255);
leds[4] = CRGB (fadeColor, 0, 255);
leds[1] = CRGB (fadeColor, 0, 0);
leds[3] = CRGB (fadeColor, 0, 0);
leds[5] = CRGB (fadeColor, 0, 0);
FastLED.show();
last_time = millisTime;
if (fadeColor >= 254) fadeCount = 2;
}

if (millisTime - last_time > 40 && fadeCount == 2)
{
fadeColor --;
leds[0] = CRGB (fadeColor, 0, 255);
leds[2] = CRGB (fadeColor, 0, 255);
leds[4] = CRGB (fadeColor, 0, 255);
leds[1] = CRGB (fadeColor, 0, 0);
leds[3] = CRGB (fadeColor, 0, 0);
leds[5] = CRGB (fadeColor, 0, 0);
FastLED.show();
last_time = millisTime;
if (fadeColor <= 50) fadeCount = 1;
}
}

PaulStoffregen
03-01-2016, 10:31 AM
However, if you use a large number of Neopixel LEDs, FastLED will disable interrupts for too long, causing the audio to stutter. For a long LED strip, you'll need either the OctoWS2811 driver, or a 4-wire LED type like APA102.

rexhex
03-01-2016, 02:29 PM
I am planning on using the AudioOutput later on so I wanted to keep it in the sketch. Plus the issue still occurred with just the audio memory in the setup.

So it was an interrupt issue after all... What is it that made you think it was an interrupt issue right away? Im asking because I would like to be able to identify interrupt issues as well.

Thank you for the support Mr. Stoffregen! I have it working over here on my end as well.