Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.95 KB | None | 0 0
  1. /*
  2.  Alien-Wah by Nasca Octavian Paul from Tg. Mures, Romania
  3.  e-mail:  <paulnasca@email.ro> or <paulnasca@yahoo.com>.
  4. */
  5.  
  6. /*
  7.  The algorithm was found by me by mistake(I was looking for something else);
  8.  I called this effect "Alien Wah" because sounds a bit like wahwah, but more strange.
  9.  The ideea of this effect is very simple: It is a feedback delay who uses complex numbers.
  10.  If x[] represents the input and y[] is the output, so a simple feedback delay looks like this:
  11.  y[n]=y[n-delay]*fb+x[n]*(1-fb)
  12.  
  13.  'fb' is a real number between 0 and 1.
  14.  If you change the fb with a complex number who has the MODULUS smaller than 1, it will look like this.
  15.  
  16.  fb=R*(cos(alpha)+i*sin(alpha));  i^2=-1; R<1;
  17.  y[n]=y[n-delay]*R*(cos(alpha)+i*sin(alpha))+x[n]*(1-R);
  18.  
  19.  alpha is the phase of the number and is controlled by the LFO(Low Frequency Oscillator).
  20.  If the 'delay' parameter is low, the effect sounds more like wah-wah,
  21.  but if it is big, the effect will sound very interesting.
  22.  The input x[n] has the real part of the samples from the wavefile and the imaginary part is zero.
  23.  The output of this effect is the real part of y[n].
  24.  
  25.  Here it is a simple and unoptimised implementation of the effect. All parameters should be changed at compile time.
  26.  It was tested only with Borland C++ 3.1.
  27.  
  28.  Please send me your opinions about this effect.
  29.  Hope you like it (especially if you are play to guitar).
  30.  Paul.
  31. */
  32.  
  33. /*
  34. Alien Wah Parameters
  35.  
  36.  freq       - "Alien Wah" LFO frequency
  37.  startphase - "Alien Wah" LFO startphase (radians), needed for stereo
  38.  fb         - "Alien Wah" FeedBack (0.0 - low feedback, 1.0 = 100% high feedback)
  39.  delay      -  delay in samples at 44100 KHz (recomanded from 5 to 50...)
  40. */
  41.  
  42. #include <complex.h>
  43. #include <fcntl.h>
  44. #include <sys\stat.h>
  45. #include <io.h>
  46. #include <stdio.h>
  47. #include <math.h>
  48.  
  49. /*
  50.  .raw files are raw files (without header), signed 16 bit,mono
  51. */
  52. #define infile "a.raw" //input file
  53. #define outfile "b.raw" //input file
  54. #define samplerate 44100
  55.  
  56. #define bufsize 1024
  57. int buf1[bufsize];//input buffer
  58. int buf2[bufsize];//output buffer
  59.  
  60.  
  61. #define lfoskipsamples 25 // How many samples are processed before compute the lfo value again
  62.  
  63. struct params
  64. {
  65.    float freq,startphase,fb;
  66.    int delay;
  67. } awparams;
  68. //alien wah internal parameters
  69.  
  70. struct alienwahinternals
  71. {
  72.  complex *delaybuf;
  73.  float lfoskip;
  74.  long int t;
  75.  complex c;
  76.  int k;
  77. } awint;
  78.  
  79. //effect initialisation
  80. void init(float freq,float startphase,float fb,int delay){
  81.   awparams.freq=freq;
  82.   awparams.startphase=startphase;
  83.   awparams.fb=fb/4+0.74;
  84.   awparams.delay=(int)(delay/44100.0*samplerate);
  85.   if (delay<1) delay=1;
  86.   awint.delaybuf=new complex[awparams.delay];
  87.   int i;
  88.   for (i=0;i<delay;i++) awint.delaybuf[i]=complex(0,0);
  89.   awint.lfoskip=freq*2*3.141592653589/samplerate;
  90.   awint.t=0;
  91. }
  92.  
  93. //process buffer
  94. void process()
  95. {
  96.  int i;
  97.  float lfo,out;
  98.  complex outc;
  99.  for(i=0;i<bufsize;i++)
  100.  {
  101.    if (awint.t++%lfoskipsamples==0)
  102.    {
  103.       lfo=(1+cos(awint.t*awint.lfoskip+awparams.startphase));
  104.       awint.c=complex(cos(lfo)*awparams.fb,sin(lfo)*awparams.fb);
  105.    };
  106.    outc=awint.c*awint.delaybuf[awint.k]+(1-awparams.fb)*buf1[i];
  107.    awint.delaybuf[awint.k]=outc;
  108.    if ((++awint.k)>=awparams.delay)
  109.       awint.k=0;
  110.    out=real(outc)*3;  //take real part of outc
  111.    if (out<-32768) out=-32768;
  112.    else if (out>32767) out=32767; //Prevents clipping
  113.    buf2[i]=out;
  114.  };
  115. }
  116.  
  117. int main()
  118. {
  119.   char f1,f2;
  120.   int readed;
  121.   long int filereaded=0;
  122.   printf("\n");
  123.   f1=open(infile,O_RDONLY|O_BINARY);
  124.   remove(outfile);
  125.   f2=open(outfile,O_BINARY|O_CREAT,S_IWRITE);
  126.   long int i;
  127.  
  128.   init(0.6,0,0.5,20);  //effects parameters
  129.  
  130.   do
  131.   {
  132.     readed=read(f1,buf1,bufsize*2);
  133.  
  134.     process();
  135.  
  136.     write(f2,buf2,readed);
  137.     printf("%ld  bytes \r",filereaded);
  138.     filereaded+=readed;
  139.   }while (readed==bufsize*2);
  140.  
  141.   delete(awint.delaybuf);
  142.   close(f1);
  143.   close(f2);
  144.   printf("\n\n");
  145.  
  146.   return(0);
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement