I’m doing an experiment, trying to get out the frequency response from a signal with the help of filter resonance.
I’ve encountered a weird error (it’s late afternoon so my brain is kind of dull at this moment)
I’ve hacked this together, so the code is all over the place, so please bare with me.
However, the error I have is that for some reason, running my registerHighestFreq-method once in my opinion and knowledge should be enough to get the result I want. For some reason, that is not the case, I have to supply it with a repeated call to the method and supplying either the same value again or just the other channel, makes the function start working.
I have no clue why this is, at this time of day, I’m left clueluess as to what is going on.
I recon I am initilized something totaly wrong, but why whould this start working just by typing the same function again?
By using the D-key to increase the filter-cuttoff, and A-key to decrease, you can sweep through the spectrum, and the code will register at which frequnecy resonance with the signal and the filter occurs.
If someone with a sharp mind is able to find what the error is, i’ve hacked together I would much appriciate it
I have the following code:
testApp.cpp
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup()
{
ofEnableAlphaBlending();
ofSetupScreen();
ofBackground(0, 0, 0);
ofSetVerticalSync(true);
this->fc = 1.0;
this->q = 1.0;
highestFreq = 0.0;
highestAmp = 0.0;
freqSweep = 0;
/* This is stuff you always need.*/
sampleRate = 44100; /* Sampling Rate */
initialBufferSize = 512; /* Buffer Size. you have to fill this buffer with sound*/
demoBuffer = new float[initialBufferSize];
/* Now you can put anything you would normally put in maximilian's 'setup' method in here. */
for (int i = 0; i < initialBufferSize; i++)
{
demoBuffer[i] = 0;
}
ofSoundStreamSetup(2,0,this, sampleRate, initialBufferSize, 4);/* Call this last ! */
}
//--------------------------------------------------------------
void testApp::update()
{
//registerHighestFreq();
//for(int i=0; i < initialBufferSize; i++)
// {
// registerHighestFreq(demoBuffer[i]);
// }
}
//--------------------------------------------------------------
void testApp::draw()
{
ofSetColor(0, 255, 30);
ofNoFill();
ofPushStyle();
ofPushMatrix();
ofTranslate(32, 150, 0);
//ofSetColor(245, 58, 135);
ofSetLineWidth(3);
float increment = 10;
ofBeginShape();
y = ofGetHeight()/2; // draw inside entire window, whatever size you want for demo
x = 0;
w = ofGetWidth();
h = y;
float width = (float)(ofGetWidth()) / initialBufferSize;
// draw a line connecting the values in the buffer across the screen
for (int i = 0;i < initialBufferSize - 1; i++)
{
ofLine(x + i*width, y - h/2 - demoBuffer[i]*h,x + (i+1)*width, y - h/2 - demoBuffer[i+1]*h);
hroutine = y - h/2 - demoBuffer[i]*h;
}
ofEndShape(false);
ofPopMatrix();
ofPopStyle();
}
double testApp::myFilter(double input, double fc)
{
if(fc > 0.49)
{
fc = 0.49;
}
double xn = input;
xn_1 = z1left;
double yn = fc*xn + (fc-1.0)*xn_1;
z1left = xn;
return yn;
}
void testApp::registerHighestFreq(double input)
{
input = myFilter3.lores(input, fc, 2.0);
hroutine = (100 - 100/2 - input*100);
if(hroutine > maxH)
{
maxH = hroutine;
highestFreq = fc;
}
}
//--------------------------------------------------------------
void testApp::audioRequested (float * output, int bufferSize, int nChannels)
{
for (int i = 0; i < bufferSize; i++)
{
/* Stick your maximilian 'play()' code in here ! Declare your objects in testApp.h.
For information on how maximilian works, take a look at the example code at
[http://www.maximilian.strangeloop.co.uk](http://www.maximilian.strangeloop.co.uk)
under 'Tutorials'.
*/
//wave=sine1.sinewave(1500);
wave=sine1.sinebuf(abs(mouseX));/* mouse controls sinewave pitch. we get abs value to stop it dropping
// delow zero and killing the soundcard*/
//sample=tri1.triangle(4440);
//mySine.sinewave(myOtherSine.sinewave(myLastSine.sinewave(0.1)*30)*440);//awesome bassline
mymix.stereo(wave, outputs, 0.5);
//outputs[0] = myFilter2.lores(outputs[0], fc, q); //myFilter(outputs[0], fc);//
//outputs[1] = myFilter2.lores(outputs[1], fc, q);
registerHighestFreq(outputs[1]);
registerHighestFreq(outputs[1]);
demoBuffer[i] = outputs[0];//myFilter2.lores(outputs[0], fc, q);
demoBuffer[i] = outputs[1];//myFilter2.lores(outputs[1], fc, q);
//cout << outputs[0] << std::endl;
output[i*nChannels ] = outputs[0]; /* You may end up with lots of outputs. add them here */
output[i*nChannels + 1] = outputs[1];
}
//registerHighestFreq(outputs[0]);
}
//--------------------------------------------------------------
void testApp::audioReceived (float * input, int bufferSize, int nChannels){
/* You can just grab this input and stick it in a double, then use it above to create output*/
for (int i = 0; i < bufferSize; i++)
{
/* you can also grab the data out of the arrays*/
}
}
//--------------------------------------------------------------
void testApp::keyPressed(int key)
{
switch(key)
{
case 'q':
case 'Q':
{
ofExit();
break;
}
case 'a':
case 'A':
{
fc-=10.00;
if(fc<=0.0)
{
fc=0.0;
}
cout << fc << endl;
break;
}
case 'd':
case 'D':
{
fc+=10.00;
if(fc>=22050.0)
{
fc=22050.0;
}
cout << fc << endl;
break;
}
case 'z':
case 'Z':
{
this->q -= 0.1;
cout << q << endl;
break;
}
case 'c':
case 'C':
{
this->q += 0.1;
cout << q << endl;
break;
}
case 'p':
case 'P':
{
cout << "Wave Frequency at: " << mouseX << endl;
cout << "Higest Amplitude at: " << highestFreq << " hz" << endl;
break;
}
case 'o':
case 'O':
{
maxH=0;
highestFreq=0;
//cout << mouseX << endl;
break;
}
}
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y )
{
//std::cout << outputs[0] << std::endl;
//std::cout << mouseX << std::endl;
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){
}
testApp.h:
#pragma once
#include "ofMain.h"
#include "ofMain.h"
#include "ofxMaxim.h"
#include <math.h>
class testApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
void audioRequested (float * input, int bufferSize, int nChannels); /* output method */
void audioReceived (float * input, int bufferSize, int nChannels); /* input method */
double myFilter(double input, double fc);
void registerHighestFreq(double input);
int initialBufferSize; /* buffer size */
int sampleRate;
/* stick you maximilian stuff below */
double wave,sample,outputs[2];
ofxMaxiMix mymix;
ofxMaxiOsc sine1;
ofxMaxiOsc tri1;
ofxMaxiOsc mySine,myOtherSine,myLastSine;
maxiFilter myFilter2,myFilter3;
double fc, q;
double freqSweep;
float *demoBuffer;
double xn_1;
double z1left;
double highestAmp,highestFreq;
int y;
int x;
int w;
int h;
int hroutine;
int maxH;
};