I have a class I wrote myself that uses the native ARM FFT routines to sample audio inputs to an analog pin. It samples the pin, does the FFT, and then saves the FFT results in an array. I store about 100 FFT results (ie. the array is int[100[256]) to average out the values over time.
It works OK but is quite messy because I don't really understand the audio/fft stuff fully - it's mostly just copied from bits and pieces of code on this forum.
I'd like to replace that bit with the FFT abilities of the TeensyAudio library. I have test code working based on the example:
However I'd like to use the library in my own audio class. I'm new to C/C++, especially classes and am not sure how to to this.
I tried:
But won't compile, with errors:
Presumably because I'm trying to instantiate the object in the header, without passing the required params. However If I put the initial params in the header file e.g.:
It throws:
So how can I define and use the Teensy Audio class inside my own TronAudio class?
It works OK but is quite messy because I don't really understand the audio/fft stuff fully - it's mostly just copied from bits and pieces of code on this forum.
I'd like to replace that bit with the FFT abilities of the TeensyAudio library. I have test code working based on the example:
Code:
#include <Audio.h>
#include <Wire.h>
#include <SD.h>
AudioInputAnalog analogPinInput(16); // audio shield: mic or line-in
AudioAnalyzeFFT256 myFFT(20);
AudioConnection c2(analogPinInput, 0, myFFT, 0);
void setup() {
// Audio connections require memory to work. For more
// detailed information, see the MemoryAndCpuUsage example
AudioMemory(12);
}
void loop() {
if (myFFT.available()) {
// each time new FFT data is available
// print it all to the Arduino Serial Monitor
Serial.print("FFT: ");
for (int i=0; i<128; i++) {
Serial.print(myFFT.output[i]);
Serial.print(",");
}
Serial.println();
}
}
However I'd like to use the library in my own audio class. I'm new to C/C++, especially classes and am not sure how to to this.
I tried:
Code:
#ifndef _AUDIO_h
#define _AUDIO_h
#include "Arduino.h"
#include <Audio/Audio.h>
#define FFT_SIZE 64
class TronAudio
{
private:
AudioAnalyzeFFT256 myFFT;
AudioInputAnalog analogPinInput;
public:
TronAudio();
void getSpectrum(int spectrumOut[]);
};
#endif
Code:
#include <Audio/Audio.h>
#include <Wire.h>
#include <SD.h>
#include "TronAudio.h";
TronAudio::TronAudio() {
analogPinInput(16); // audio shield: mic or line-in
myFFT(20);
AudioConnection c2(analogPinInput, 0, myFFT, 0);
}
void TronAudio::getSpectrum(int spectrumOut[]) {
if (myFFT.available()) {
memcpy(spectrumOut, myFFT.output, 128 * sizeof(int));
}
}
Code:
Compiling 'Tron' for 'Teensy 3.0'
TronAudio.cpp:In constructor 'TronAudio::TronAudio()'
TronAudio.cpp:6:22: error: no matching function for call to 'AudioInputAnalog::AudioInputAnalog()'
TronAudio.cpp:candidates are
Audio.h:In file included from
TronAudio.cpp:from
input_adc.h:AudioInputAnalog(unsigned int)
input_adc.h:candidate expects 1 argument, 0 provided
input_adc.h:AudioInputAnalog(const AudioInputAnalog&)
input_adc.h:candidate expects 1 argument, 0 provided
input_adc.h:AudioInputAnalog(AudioInputAnalog&&)
input_adc.h:candidate expects 1 argument, 0 provided
TronAudio.cpp:7:19: error: no match for call to '(AudioInputAnalog) (int)'
TronAudio.cpp:8:10: error: no match for call to '(AudioAnalyzeFFT256) (int)'
Error compiling
Compiling 'test' for 'Teensy 3.0'
Binary sketch size: 14,136 bytes (used 11% of a 131,072 byte maximum) (0.57 secs)
Presumably because I'm trying to instantiate the object in the header, without passing the required params. However If I put the initial params in the header file e.g.:
Code:
...
private:
AudioAnalyzeFFT256 myFFT(16);
AudioInputAnalog analogPinInput(20);
It throws:
Code:
Compiling 'Tron' for 'Teensy 3.0'
TronAudio.cpp:In file included from
TronAudio.h:12:28: error: expected identifier before numeric constant
TronAudio.h:12:28: error: expected ',' or '...' before numeric constant
TronAudio.h:13:37: error: expected identifier before numeric constant
TronAudio.h:13:37: error: expected ',' or '...' before numeric constant
TronAudio.cpp:In constructor 'TronAudio::TronAudio()'
TronAudio.cpp:9:48: error: no matching function for call to 'AudioConnection::AudioConnection(<unresolved overloaded function type>, int, <unresolved overloaded function type>, int)'
TronAudio.cpp:candidates are
analyze_fft256.h:In file included from
Audio.h:from
TronAudio.cpp:from
AudioStream.h:AudioConnection(AudioStream&, unsigned char, AudioStream&, unsigned char)
AudioStream.h:no known conversion for argument 1 from '<unresolved overloaded function type>' to 'AudioStream&'
AudioStream.h:AudioConnection(AudioStream&, AudioStream&)
AudioStream.h:candidate expects 2 arguments, 4 provided
AudioStream.h:AudioConnection(const AudioConnection&)
AudioStream.h:candidate expects 1 argument, 4 provided
AudioStream.h:AudioConnection(AudioConnection&&)
AudioStream.h:candidate expects 1 argument, 4 provided
TronAudio.cpp:In member function 'void TronAudio::getSpectrum(int*)'
TronAudio.cpp:15:11: error: '((TronAudio*)this)->TronAudio::myFFT' does not have class type
TronAudio.cpp:16:28: error: '((TronAudio*)this)->TronAudio::myFFT' does not have class type
Error compiling
Compiling 'test' for 'Teensy 3.0'
Binary sketch size: 14,136 bytes (used 11% of a 131,072 byte maximum) (0.56 secs)
So how can I define and use the Teensy Audio class inside my own TronAudio class?