[Stk] Problem with the Stk reverbs

edpa06 at imi.aau.dk edpa06 at imi.aau.dk
Wed Mar 3 03:17:55 PST 2010


Hi,
I am currently trying to write a program implementing the binaural sound algorithm of Brown and Duda ("An efficient HRTF model for 3d sound", 1997). My objective is to make it take a non-spatialized sound file as input, and "make it" 3d by specifying an azimuth and a distance (no elevation for the moment). So basically, i am not dealing with real-time, only reading from a file and writing in another one.

Everything goes fine in my program, except when I reach the point where I try to implement a room-effect: all three of the reverb classes in the stk are making my program crash. 

It's compiling fine, but when I run the thing I get a Visual C++ error "Unhandled exception at 0x004247a6 in binaural.exe: 0xC0000005: Access violation writing location 0x00814000",
and the debugger tells me that the problem comes from the tick function of the reverb, at that point (second line):

  for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
    *oSamples++ = tick( *iSamples );
    *oSamples = lastFrame_[1];
  }

I tried all three of the reverbs, and for each I tried the two tick functions (the ones taking StkFrames as argument): all gave me the same error. The funny thing is that when I try to use an effect other than a reverb, for example the echo, everything works fine; it's just concerning the 3 reverb objects. I copy-pasted my code right under this mail...

I hope someone can help!
Thanks in advance!

Emmanuel Parzy 



#define __LITTLE_ENDIAN__ 
#define __WINDOWS_MM__
#define __WINDOWS_DS__

#include "FileLoop.h"
#include "FileWvOut.h"
#include "BiQuad.h"
#include "NRev.h"
#include "Delay.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>


#define BETA 7778.0
#define FREQ 44100.0

#define ANGLE 60.0
#define TSIXTY 0.2

using namespace stk;


long getDelay1 (float *angle)
{
	//LEFT delay if positive angle; RIGHT delay if negative angle
	long delay = (((0.0875 + 0.0875*(*angle))/340.29) * FREQ) +2000;
	std::cout<<"Delay 1: "<<delay<<"\n";
	return delay;
}






long getDelay2 (float *angle)
{
	//RIGHT delay if positive angle; LEFT delay if negative angle
	long delay = (((0.0875 - 0.0875* sin(*angle)) / 340.29) * FREQ) +2000;
	std::cout<<"Delay 2: "<<delay<<"\n";
	return delay;
}





void getLeftBiQuadCoef (float *angle, StkFloat *biQuadCoef)
{
	//Variable Alpha (varies from 2 to 0 with angle from -90 to 90
	float a = (-2.0/180.0)*(*angle) + 1.0;

	//Coefficient b0
	*biQuadCoef = (2.0*a + BETA*(1.0/FREQ)) / (2.0 + BETA*(1.0/FREQ));

	//Coefficient b1
	*(biQuadCoef +1) = ((-2.0*a) + BETA*(1.0/FREQ)) / (2.0 + BETA*(1.0/FREQ));

	//Coefficient a1
	*(biQuadCoef +2) = (-2.0 + BETA*(1.0/FREQ)) / (2.0 + BETA*(1.0/FREQ));
	
	std::cout<<"Angle: "<<*angle<<"\n";
	std::cout<<"Left Alpha: "<<a<<"\n";
	std::cout<<"Left filter coefficients: "<<*biQuadCoef<<"  "<<*(biQuadCoef +1)<<"  "<<*(biQuadCoef +2)<<"  "<<"\n";
}






void getRightBiQuadCoef (float *angle, StkFloat *biQuadCoef)
{
	//Variable Alpha (varies from 2 to 0 with angle from 90 to -90
	float a = (2.0/180.0)*(*angle) + 1.0;

	//Coefficient a0
	*biQuadCoef = (2.0*a + BETA*(1.0/FREQ)) / (2.0 + BETA*(1.0/FREQ));

	//Coefficient a1
	*(biQuadCoef +1) = ((-2.0*a) + BETA*(1.0/FREQ)) / (2.0 + BETA*(1.0/FREQ));

	//Coefficient b1
	*(biQuadCoef +2) = (-2.0 + BETA*(1.0/FREQ)) / (2.0 + BETA*(1.0/FREQ));

	std::cout<<"Angle: "<<*angle<<"\n";
	std::cout<<"Right Alpha: "<<a<<"\n";
	std::cout<<"Right filter coefficients: "<<*biQuadCoef<<"  "<<*(biQuadCoef +1)<<"  "<<*(biQuadCoef +2)<<"  "<<"\n";
}






int main()
{
  // Set the global sample rate before creating class instances.
  Stk::setSampleRate( 44100.0 );

  int nFrames = 50000;
  FileWvIn input;
  FileWvIn input2;
  FileWvOut output;

  //Angle of incidence of the sound source
  float angle = ANGLE;	



  // LOAD A FILE
    try {
    input.openFile( "soundfiles/Hello.wav", false );
    // Open a 16-bit, two-channels WAV formatted output file
    output.openFile( "output.wav", 2, FileWrite::FILE_WAV, Stk::STK_SINT16 );
  }
  catch ( StkError & ) {
    exit( 1 );
  }



  //FILTERS
  BiQuad leftBiQuad;
  BiQuad rightBiQuad;
  StkFloat leftBiQuadCoef [3];
  StkFloat rightBiQuadCoef [3];
  //Calculation and setting of the filters coefficients
  getLeftBiQuadCoef (&angle, &leftBiQuadCoef[0]);
  getRightBiQuadCoef (&angle, &rightBiQuadCoef[0]);
  leftBiQuad.setCoefficients(leftBiQuadCoef[0], leftBiQuadCoef[1], 0, leftBiQuadCoef[2], 0);
  rightBiQuad.setCoefficients(rightBiQuadCoef[0], rightBiQuadCoef[1], 0, rightBiQuadCoef[2], 0);


  //DELAYS
  Delay leftDelay;
  Delay rightDelay;
  //Calculation and setting of the delays
  if (angle>0)
  {
	  leftDelay.setDelay(getDelay1(&angle));
	  rightDelay.setDelay(getDelay2(&angle));
  }else
  {
	  leftDelay.setDelay(getDelay2(&angle));
	  rightDelay.setDelay(getDelay1(&angle));
  }

  //REVERB
  NRev rev;
  float t60 = TSIXTY;
  rev.setT60(t60);	


  // StkFrames computations and output
  StkFrames revFrames( nFrames, 2);
  StkFrames frames( nFrames, 2 );
  
  try {
	  input.tick(frames);

	  rev.tick(frames, revFrames);
		
	  leftBiQuad.tick(frames, 0); 
	  rightBiQuad.tick(frames, 1);

	  leftDelay.tick(frames, 0);
	  rightDelay.tick(frames, 1);

	  output.tick(revFrames);
  }
  catch ( StkError & ) {
    exit( 1 );
  }
	
  //while(1){}



  return 0;
}




More information about the Stk mailing list