I wrote what you said (it's not possible to attach again a file so here below 
it is the code) and i receive this error:
"oscc_cr_sr_autonomous1-monoed_no_vector4.cpp:144:44: error: cannot convert 
'float' to 'float*' in assignment  buffer = feedforward(buffer + i, i,a0,a1,1);"
the 144:44 is the place of the number of samples of the delay, here is simply 
1. Don't understand what's wrong."

It is the place of the number 1,  the last place:
buffer = feedforward(buffer + i ,i ,a0 ,a1 ,1 );

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sndfile.h>

//This is me on windows:
//g++ -o a oscc_cr_sr_autonomous1-monoed_no_vector4.cpp   libsndfile-1.dll
//a oscc_cr_sr_autonomous1-monoed_no_vector2.wav  0.5 300 10


//g++ oscc_cr_sr_autonomous1.cpp -lsndfile
//# oscillator sndfile.wav amp freq(hz) dur(s)
//./a.out ross_oscc_1.wav 0.5 300 10

///////////////////////////LIBSNDFILE//////////////////////////////
///functions to open, write, and close a sound file.
SNDFILE *soundout_open(char* name, int chans, float sr){
  // initialise the SF_INFO structure
  SF_INFO info;
  info.samplerate = (int) sr;
  info.channels = chans;
  info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
  // open the file
  return sf_open(name, SFM_WRITE, &info);
}

//vector where are written the values
int soundout(SNDFILE *psf_out, float *buffer, int vecsize){
      return (int) sf_writef_float(psf_out, buffer, vecsize);
}

void soundout_close(SNDFILE *psf_out){
  sf_close(psf_out);
}


/////////////////////OSCILLATOR////////////////////////////////////////////////

float oscc(float *output, float amp, float freq, float *table, float *index, 
/*float phase,*/ int tablength,float sr)
{
  float incr = freq*tablength/sr, frac, fracsq, fracc; // increment
  float pos,a,b,c,d, tmp;

    // cubic interpolation
    frac = *index - (int)*index;
    a = (int) *index > 0 ? table[(int)(*index)-1] : table[tablength-1];
    b = table[(int)(*index)];
    c = table[(int)(*index)+1];
    d = table[(int)(*index)+2];


    fracsq = frac*frac;
    fracc = frac*fracsq;

      //int i;
    *output = amp*(fracc*(d-a)/8 + fracsq*(c-a)/4 + frac*(b-a) + a); //output[i]
    *index += incr;
    while(*index >= tablength) *index -= (tablength);
    while(*index < 0) *index += tablength;
      
      return *output;
}

float feedforward(const float * in, int index, double a0, double a1, int delay){
  if (index >= delay)
    return a0 * in [index] + a1 * in [index - delay];
  else
    return a0 * in [index];
}

////////////////////////////////TABLE /////////////////////////////////////
const double pi = 4*atan(1.);
float* sinus_table(int length, float phase){

  float *table = new float[length+2];
  phase *= (float)pi*2;
  for(int n=0; n < length+2; n++)
    table[n] = (float) cos(phase+n*2*pi/length);

  return table;
}
float *buzz_table(){
      
      #define BUFFER_SIZE 44100
    float *wtab = new float[BUFFER_SIZE];

    // initialize buffer to zero, since we'll be summing into it
    for (int i=0; i < BUFFER_SIZE; ++i)
        wtab[i] = 0.0f;


    const int num_harmonics = 50; // compute this as needed, highest harmonic
    // must not have period smaller than 2 samples in the buffer.

    // normalize the output amplitude to 1.0...
    double amp_scale = 1.0 / num_harmonics;

    // outer loop: iterate through harmonics from 1 ... num_harmonics
    for (int j=1; j <= num_harmonics; ++j) {
        // inner loop: compute the jth harmonic, sum into buffer
        for (int i=0; i < BUFFER_SIZE; ++i)
            wtab[i] += cos (i * j * (2.0 * pi / (double)BUFFER_SIZE)) * 
amp_scale;
    }
            
                  return wtab;
            
}

int main(int argc, char** argv){
// NOTE: all of these could be variables instead of #defines
#define CHANS 1
#define SR 44100.0
#define WAVE_SIZE 44100

  SNDFILE *psf;
  float *buffer;
  int bufs;

  float amp, freq, *wave, cs, ndx=0,*wtab;

  if(argc != 5){
    printf("usage: oscillator sndfile.wav amp freq(hz) dur(s)\n");
    return 1;
  }
  amp   = (float) atof(argv[2]);
  freq  = (float) atof(argv[3]);
  bufs  = (int) (atof(argv[4])*SR);

  // allocate buffer & table memory
  buffer = new float[bufs];
  wave =   buzz_table();//buzz_table();//sinus_table(WAVE_SIZE, 0.0);

  // now we open the file
  if(!(psf = soundout_open(argv[1], CHANS, SR))){
    printf("error opening output file\n");
    exit(-1);
  }

  double a0 = 0.5, a1 = 0.5;
  for(int i=0; i < bufs; i++){

    oscc(buffer, amp, freq, wave, &ndx, WAVE_SIZE, SR);
      //feedforward(const float * in, int index, double a0, double a1, int 
delay){
      buffer = feedforward(buffer + i, i,a0,a1,1);
    soundout(psf, buffer, 1);
  }

  // close file & free memory
  soundout_close(psf);
  delete[] buffer;
  delete[] wave;

  return 0;
}
________________________________
From: music-dsp <[email protected]> on behalf of Stefano D'Angelo 
<[email protected]>
Sent: Tuesday, November 12, 2024 6:29 AM
To: [email protected] <[email protected]>
Subject: Re: [MUSIC-DSP] feedforward filter


Il 12/11/24 07:23, Stefano D'Angelo ha scritto:

Il 12/11/24 05:28, Pablo Frank ha scritto:
Hi all, in the .cpp file attached there is an oscillator that works ok, and a 
feedforward filter (line 64) that i don't  know how to insert between the 
oscillator (line 142) and the soundout function (line 143) to get it working. 
I'm sure it's a simple programming matter that most of you can solve it 
immediatelly. Thanks in advance for any help,

Probably something like:


feedforward(&buffer, i, a0, a1, delay);


where a0 and a1 are the filter coefficients and delay is the distance between 
current input sample and delayed input tap in number of samples.


Sorry, bad copy/paste


buffer = feedforward(buffer + i, i, a0, a1, delay);


However, this will never work and actually crash as buffer is 1 sample wide. 
You need to either allocate it as


buffer = new float[bufs];


or you need to implement and operate on a circular buffer...


Stefano

Reply via email to