giuliomoro / Bela-Q

An example repo to use the Cycfi's Q library with Bela
2 stars 0 forks source link

Make parameters #1

Open resynth opened 4 years ago

resynth commented 4 years ago

I found the make parameters, specified in read me, allowed Bela to compile the example delay script but will not compile projects that use pitchdetector.

CPPFLAGS=-I /root/Q/q_lib/include -I /root/Q/infra/include -std=c++1z; worked on my Bela for compiling pitchdetector.

giuliomoro commented 4 years ago

@resynth thanks for this, can you post a code example?

resynth commented 4 years ago

// Simple example of Q library pitch_detector

// Requires Q library in root and // CPPFLAGS=-I /root/Q/q_lib/include -I /root/Q/infra/include -std=c++1z // in Make Parameters

// Detects pitch from audio input, outputs sine at tracked pitch

include

include <libraries/math_neon/math_neon.h>

include <q/pitch/pitch_detector.hpp>

include <q/support/literals.hpp>

namespace q = cycfi::q; using namespace q::literals;

define SAMPLERATE 44100

const int lowestTrackableFrequency = 80;
const int highestTrackableFrequency = 2000;

q::pitch_detector pd(lowestTrackableFrequency, highestTrackableFrequency, SAMPLERATE, -45_dB);

float in = 0;

// RMS stuff for envelope follow
const float rms_C = 0.005; float squareSum = 0; float rmsValue = 0;

//osc stuff float gPhase; float gInverseSampleRate;

bool setup(BelaContext context, void userData) { gInverseSampleRate = 1.0 / context->audioSampleRate; gPhase = 0.0;

return true; }

// for printing trackedFrequency float gInterval = 1; float gSecondsElapsed = 0; int gCount = 0;

float osc = 0;

void render(BelaContext context, void userData) { for (unsigned int n = 0; n < context->audioFrames; n++) { in = audioRead(context, n, 0);

// Pitch Tracking 
bool pdIsReady = pd(in);
static float trackedFrequency = 0.f;
if (pdIsReady)
{
  trackedFrequency = pd.get_frequency();
}

// Print tracked freq every second for testing purposes
gCount++;
if(gCount % (int)(context->audioSampleRate*gInterval) == 0) {
    gSecondsElapsed += gInterval;
    rt_printf("Pitch: %f\n",trackedFrequency);
}

// RMS envelope follow calculations                                                                                                                     
squareSum = (1.0f - rms_C) * squareSum + rms_C * in * in;
rmsValue = sqrt(squareSum);

// oscillator follows tracked frequency
osc = sinf_neon(gPhase) * rmsValue;
gPhase += 2.0f * (float)M_PI * trackedFrequency * gInverseSampleRate;
if(gPhase > M_PI)
    gPhase -= 2.0f * (float)M_PI;

for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) {
    audioWrite(context, n, channel, osc);
}

} } void cleanup(BelaContext context, void userData) {}

resynth commented 4 years ago

not sure I did that quite right! New to github but hope that's workable?

giuliomoro commented 4 years ago

thanks, you'd need triple backticks (```) to enclose your code to make it look readable!

resynth commented 4 years ago
#include <Bela.h>
#include <libraries/math_neon/math_neon.h>

#include <q/pitch/pitch_detector.hpp>   
#include <q/support/literals.hpp>
namespace q = cycfi::q;
using namespace q::literals;

#define SAMPLERATE 44100
const int lowestTrackableFrequency = 80;  
const int highestTrackableFrequency = 2000;

q::pitch_detector pd(lowestTrackableFrequency, highestTrackableFrequency, SAMPLERATE, -45_dB);

float in = 0;

// RMS stuff for envelope follow                                                                                                                              
const float rms_C = 0.005;
float squareSum = 0;
float rmsValue = 0;

//osc stuff
float gPhase;
float gInverseSampleRate;

bool setup(BelaContext *context, void *userData)
{
  gInverseSampleRate = 1.0 / context->audioSampleRate;
  gPhase = 0.0;

  return true;
}

// for printing trackedFrequency
float gInterval = 1;
float gSecondsElapsed = 0;
int gCount = 0;

float osc = 0;

void render(BelaContext *context, void *userData)
{
  for (unsigned int n = 0; n < context->audioFrames; n++)
  {
    in = audioRead(context, n, 0);

    // Pitch Tracking 
    bool pdIsReady = pd(in);
    static float trackedFrequency = 0.f;
    if (pdIsReady)
    {
      trackedFrequency = pd.get_frequency();
    }

    // Print tracked freq every second for testing purposes
    gCount++;
    if(gCount % (int)(context->audioSampleRate*gInterval) == 0) {
        gSecondsElapsed += gInterval;
        rt_printf("Pitch: %f\n",trackedFrequency);
    }

    // RMS envelope follow calculations                                                                                                                     
    squareSum = (1.0f - rms_C) * squareSum + rms_C * in * in;
    rmsValue = sqrt(squareSum);

    // oscillator follows tracked frequency
    osc = sinf_neon(gPhase) * rmsValue;
    gPhase += 2.0f * (float)M_PI * trackedFrequency * gInverseSampleRate;
    if(gPhase > M_PI)
        gPhase -= 2.0f * (float)M_PI;

    for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) {
        audioWrite(context, n, channel, osc);
    }
  }
}

void cleanup(BelaContext *context, void *userData) {}
giuliomoro commented 4 years ago

brilliant