view Source/Decoder.cpp @ 0:2fa9c10568d1

Initial setup.
author martinm_home <martin.morrell@eecs.qmul.ac.uk>
date Tue, 14 Aug 2012 09:34:15 +0100
parents
children e85bd6381a58
line wrap: on
line source
/*
 *  Decoder.cpp
 *  SoundField
 *
 *  Created by Martin Morrell on 14/06/2012.
 *  Copyright 2012 Queen Mary University of London. All rights reserved.
 *
 */


#include "Decoder.h"
#define _USE_MATH_DEFINES
#include <cmath>
#include <stdlib.h>
#define RearVerbWidth 55
#define RearVerbPattern 0.5
#define HiVerbWidth 30
#define HiVerbPattern 0.5


double Decoder::degRad(double angle)
{
	return (angle/180*M_PI);
};


double Decoder::radDeg(double angle)
{
	return (angle/M_PI*180);
};


//Overall Functions
void Decoder::monoDecoder(double &w, double &x, double &y, double &z)
{
	//Sound field rotations. Only called if the value is not 0 
	if (Rotate!=0.0f)
		rotateField(x, y);
	
	if (Tilt!=0.0f)
		tiltField(y, z);
	
	if (Tumble!=0.0f)
		tumbleField(x, z);
	
	
	//Zoom function. Only called if the value is not 0 
	if (Zoom!=0.0f) {
		switch (ZoomMethod) {
			case 0:
				dominanceZoom(w, x, y, z);
				break;
			case 1:
				pressZoom(w, x, y, z);
				break;
			case 2:
				pushZoom(w, x, y, z);
				break;
			case 3:
				focusZoom(w, x, y, z);
				break;
				
			default:
				break;
		}
	}
	
	
	//Centre virtual mic
    centreMic(w, x);
	
};


void Decoder::stereoDecoder(double &w, double &x, double &y, double &z)
{
	//Sound field rotations. Only called if the value is not 0 
	if (Rotate!=0.0f)
		rotateField(x, y);
	
	if (Tilt!=0.0f)
		tiltField(y, z);
	
	if (Tumble!=0.0f)
		tumbleField(x, z);
	
	
	//Zoom function. Only called if the value is not 0 
	if (Zoom!=0.0f) {
		switch (ZoomMethod) {
			case 0:
				dominanceZoom(w, x, y, z);
				break;
			case 1:
				pressZoom(w, x, y, z);
				break;
			case 2:
				pushZoom(w, x, y, z);
				break;
			case 3:
				focusZoom(w, x, y, z);
				break;
				
			default:
				break;
		}
	}
	
	
	switch (Mode) {
		case 0:
			xyDecode(w, x, y, z);
			break;
		case 1:
			msDecode(w, x, y, z);
			break;	
		default:
			break;
	}
	

	if (RearVerb > -40) {
		rearVerb(w, x, y, z);
	}
	
	if (HiVerb >-40) {
		hiVerb(w, x, y, z);
	}
};


void Decoder::twoOneDecoder(double &w, double &x, double &y, double &z)
{
	//Sound field rotations. Only called if the value is not 0 
	if (Rotate!=0.0f)
		rotateField(x, y);
	
	if (Tilt!=0.0f)
		tiltField(y, z);
	
	if (Tumble!=0.0f)
		tumbleField(x, z);
	
	
	//Zoom function. Only called if the value is not 0 
	if (Zoom!=0.0f) {
		switch (ZoomMethod) {
			case 0:
				dominanceZoom(w, x, y, z);
				break;
			case 1:
				pressZoom(w, x, y, z);
				break;
			case 2:
				pushZoom(w, x, y, z);
				break;
			case 3:
				focusZoom(w, x, y, z);
				break;
				
			default:
				break;
		}
	}
	
    //Subwoofer signal and filtering
    subSignal(w);
    filterLF();
    if (Fc>0) {
        filterHF(w, x, y, z);
    }
    
    
    
    //Front virtual mics
 	switch (Mode) {
		case 0:
			xyDecode(w, x, y, z);
			break;
		case 1:
			msDecode(w, x, y, z);
			break;	
		default:
			break;
	}   
    
    
    //Reverbs
    if (RearVerb > -40) {
		rearVerb(w, x, y, z);
	}
	
	if (HiVerb >-40) {
		hiVerb(w, x, y, z);
	}
	
};



void Decoder::lrcsDecoder(double &w, double &x, double &y, double &z)
{
	//Sound field rotations. Only called if the value is not 0 
	if (Rotate!=0.0f)
		rotateField(x, y);
	
	if (Tilt!=0.0f)
		tiltField(y, z);
	
	if (Tumble!=0.0f)
		tumbleField(x, z);
	
	
	//Zoom function. Only called if the value is not 0 
	if (Zoom!=0.0f) {
		switch (ZoomMethod) {
			case 0:
				dominanceZoom(w, x, y, z);
				break;
			case 1:
				pressZoom(w, x, y, z);
				break;
			case 2:
				pushZoom(w, x, y, z);
				break;
			case 3:
				focusZoom(w, x, y, z);
				break;
				
			default:
				break;
		}
	}
	
    
    //Front virtual mics
 	switch (Mode) {
		case 0:
			xyDecode(w, x, y, z);
			break;
		case 1:
			msDecode(w, x, y, z);
			break;	
		default:
			break;
	}   
    
	//Rear virtual mics
	switch (surMode) {
		case 0:
			xySurDecode(w, x, y, z);
			break;
		case 1:
			msSurDecode(w, x, y, z);
			break;	
		default:
			break;
	}
	
    
};



void Decoder::fiveDecoder(double &w, double &x, double &y, double &z)
{
	//Sound field rotations. Only called if the value is not 0 
	if (Rotate!=0.0f)
		rotateField(x, y);
	
	if (Tilt!=0.0f)
		tiltField(y, z);
	
	if (Tumble!=0.0f)
		tumbleField(x, z);
	
	
	//Zoom function. Only called if the value is not 0 
	if (Zoom!=0.0f) {
		switch (ZoomMethod) {
			case 0:
				dominanceZoom(w, x, y, z);
				break;
			case 1:
				pressZoom(w, x, y, z);
				break;
			case 2:
				pushZoom(w, x, y, z);
				break;
			case 3:
				focusZoom(w, x, y, z);
				break;
				
			default:
				break;
		}
	}
	
    
    //Centre virtual mic
    centreMic(w, x);
    
    //Front virtual mics
 	switch (Mode) {
		case 0:
			xyDecode(w, x, y, z);
			break;
		case 1:
			msDecode(w, x, y, z);
			break;	
		default:
			break;
	}   
    
	//Rear virtual mics
	switch (surMode) {
		case 0:
			xySurDecode(w, x, y, z);
			break;
		case 1:
			msSurDecode(w, x, y, z);
			break;	
		default:
			break;
	}
	
    
};



void Decoder::fiveOneDecoder(double &w, double &x, double &y, double &z)
{
	//Sound field rotations. Only called if the value is not 0 
	if (Rotate!=0.0f)
		rotateField(x, y);
	
	if (Tilt!=0.0f)
		tiltField(y, z);
	
	if (Tumble!=0.0f)
		tumbleField(x, z);
	
	
	//Zoom function. Only called if the value is not 0 
	if (Zoom!=0.0f) {
		switch (ZoomMethod) {
			case 0:
				dominanceZoom(w, x, y, z);
				break;
			case 1:
				pressZoom(w, x, y, z);
				break;
			case 2:
				pushZoom(w, x, y, z);
				break;
			case 3:
				focusZoom(w, x, y, z);
				break;
				
			default:
				break;
		}
	}
	
    //Subwoofer signal and filtering
    subSignal(w);
    filterLF();
    if (Fc>0) {
        filterHF(w, x, y, z);
    }
    
    //Centre virtual mic
    centreMic(w, x);
    
    //Front virtual mics
 	switch (Mode) {
		case 0:
			xyDecode(w, x, y, z);
			break;
		case 1:
			msDecode(w, x, y, z);
			break;	
		default:
			break;
	}   
    
	//Rear virtual mics
	switch (surMode) {
		case 0:
			xySurDecode(w, x, y, z);
			break;
		case 1:
			msSurDecode(w, x, y, z);
			break;	
		default:
			break;
	}
	
    
};






//SoundField Rotations
void Decoder::rotateField(double &x, double &y)
{	
	temp=x;
	x = cos(degRad(Rotate))*temp - sin(degRad(Rotate))*y;
	y = cos(degRad(Rotate))*y + sin(degRad(Rotate))*temp;
	
};


void Decoder::tiltField(double &y, double &z)
{
	temp=y;
	y = cos(degRad(Tilt))*temp - sin(degRad(Tilt))*z;
	z = cos(degRad(Tilt))*z + sin(degRad(Tilt))*temp;	
};


void Decoder::tumbleField(double &x, double &z)
{
	temp=x;
	x = cos(degRad(Tumble))*temp - sin(degRad(Tumble))*z;
	z = cos(degRad(Tumble))*z + sin(degRad(Tumble))*temp;
};




//Zoom Methods
void Decoder::dominanceZoom(double &w, double &x, double &y, double &z)
{
	double lambda = pow(10,(Zoom*0.24/20));
	temp2[0]=w;
	temp2[1]=x;
	temp2[2]=y;
	temp2[3]=z;
	
	w = 0.5*(lambda+pow(lambda,-1))*temp2[0] + pow(8,-0.5)*(lambda-pow(lambda,-1))*temp2[1];
	x = 0.5*(lambda+pow(lambda,-1))*temp2[1] + pow(2,-0.5)*(lambda-pow(lambda,-1))*temp2[0];
};


void Decoder::pressZoom(double &w, double &x, double &y, double &z)
{
	temp2[0]=w;
	temp2[1]=x;
	temp2[2]=y;
	temp2[3]=z;
	
	x = (sqrt(2.0)*std::abs(sin(degRad(Zoom*0.9)))*sin(degRad(Zoom*0.9))*temp2[0]) + (pow(cos(degRad(Zoom*0.9)),2)*temp2[1]);
	y = cos(degRad(Zoom*0.9))*temp2[2];
	z = cos(degRad(Zoom*0.9))*temp2[3];
};


void Decoder::pushZoom(double &w, double &x, double &y, double &z)
{
	temp2[0]=w;
	temp2[1]=x;
	temp2[2]=y;
	temp2[3]=z;
    
	x = (sqrt(2.0)*std::abs(sin(degRad(Zoom*0.9)))*sin(degRad(Zoom*0.9))*temp2[0]) + (pow(cos(degRad(Zoom*0.9)),2)*temp2[1]);
	y = pow(cos(degRad(Zoom*0.9)),2)*temp2[2];
	z = pow(cos(degRad(Zoom*0.9)),2)*temp2[3];
};


void Decoder::focusZoom(double &w, double &x, double &y, double &z)
{
	temp2[0]=w;
	temp2[1]=x;
	temp2[2]=y;
	temp2[3]=z;
	
	w = ((1/(1+std::abs(sin(degRad(Zoom*0.9)))))*temp2[0]) + ((1/sqrt(2.0))*(sin(degRad(Zoom*0.9))/(1+std::abs(sin(degRad(Zoom*0.9))))))* temp2[1];
	x = sqrt(2.0)*(sin(degRad(Zoom*0.9))/(1+std::abs(sin(degRad(Zoom*0.9)))))*temp2[0] + (1/(1+std::abs(sin(degRad(Zoom*0.9)))))*temp2[1];
	y = sqrt((1-std::abs(sin(degRad(Zoom*0.9))))/(1+std::abs(sin(degRad(Zoom*0.9)))))*temp2[2];
	z = sqrt((1-std::abs(sin(degRad(Zoom*0.9))))/(1+std::abs(sin(degRad(Zoom*0.9)))))*temp2[3];
};



//Stereo Decoders
void Decoder::xyDecode(double &w, double &x, double &y, double &z)
{	
    outputL = Pattern*sqrt(2.0)*w + (1-Pattern)*(cos(degRad(Width))*x + sin(degRad(Width))*y);
    outputR = Pattern*sqrt(2.0)*w + (1-Pattern)*(cos(degRad(Width))*x - sin(degRad(Width))*y);
};


void Decoder::msDecode(double &w, double &x, double &y, double &z)
{
	outputL = (cos(degRad(Width))*(Pattern*w*sqrt(2.0) + (1-Pattern)*x)) + (sin(degRad(Width))*y);
	outputR = (cos(degRad(Width))*(Pattern*w*sqrt(2.0) + (1-Pattern)*x)) - (sin(degRad(Width))*y);
};


//Stereo Reverbs
void Decoder::rearVerb(double &w, double &x, double &y, double &z)
{
	switch (Mode) {
		case 0:
			outputL += (RearVerbPattern*sqrt(2.0)*w + (1-RearVerbPattern)*(cos(degRad(RearVerbWidth))*-x + sin(degRad(RearVerbWidth))*y))*pow(10, RearVerb/20);
            outputR += (RearVerbPattern*sqrt(2.0)*w + (1-RearVerbPattern)*(cos(degRad(RearVerbWidth))*-x - sin(degRad(RearVerbWidth))*y))*pow(10, RearVerb/20);
			break;
		case 1:
            outputL += ((cos(degRad(RearVerbWidth))*(RearVerbPattern*w*sqrt(2.0) + (1-RearVerbPattern)*-x)) + (sin(degRad(RearVerbWidth))*y))*pow(10, RearVerb/20);
            outputR += ((cos(degRad(RearVerbWidth))*(RearVerbPattern*w*sqrt(2.0) + (1-RearVerbPattern)*-x)) - (sin(degRad(RearVerbWidth))*y))*pow(10, RearVerb/20);
			break;
	}
};

void Decoder::hiVerb(double &w, double &x, double &y, double &z)
{
	switch (Mode) {
		case 0:
			outputL += (HiVerbPattern*sqrt(2.0)*w + (1-HiVerbPattern)*(cos(degRad(HiVerbWidth))*z + sin(degRad(HiVerbWidth))*y))*pow(10, HiVerb/20);
            outputR += (HiVerbPattern*sqrt(2.0)*w + (1-HiVerbPattern)*(cos(degRad(HiVerbWidth))*z - sin(degRad(HiVerbWidth))*y))*pow(10, HiVerb/20);
			break;
		case 1:
            outputL += ((cos(degRad(HiVerbWidth))*(HiVerbPattern*w*sqrt(2.0) + (1-HiVerbPattern)*z)) + (sin(degRad(HiVerbWidth))*y))*pow(10, HiVerb/20);
            outputR += ((cos(degRad(HiVerbWidth))*(HiVerbPattern*w*sqrt(2.0) + (1-HiVerbPattern)*z)) - (sin(degRad(HiVerbWidth))*y))*pow(10, HiVerb/20);
			break;
	}
};


//Surround Decoders
void Decoder::xySurDecode(double &w, double &x, double &y, double &z)
{	
    outputSL = (surPattern*sqrt(2.0)*w + (1-surPattern)*(cos(degRad(surWidth))*-x + sin(degRad(surWidth))*y))*surGain;
    outputSR = (surPattern*sqrt(2.0)*w + (1-surPattern)*(cos(degRad(surWidth))*-x - sin(degRad(surWidth))*y))*surGain;
};


void Decoder::msSurDecode(double &w, double &x, double &y, double &z)
{
	outputSL = ((cos(degRad(surWidth))*(surPattern*w*sqrt(2.0) + (1-surPattern)*-x)) + (sin(degRad(surWidth))*y))*surGain;
	outputSR = ((cos(degRad(surWidth))*(surPattern*w*sqrt(2.0) + (1-surPattern)*-x)) - (sin(degRad(surWidth))*y))*surGain;
};


void Decoder::centreMic(double &w, double &x)
{
    outputC = pow((centrePattern*sqrt(2.0)*w + (1-centrePattern)*x), centreOrder) * centreGain;
};


void Decoder::subSignal(double &w)
{
    outputS = sqrt(2.0)*w*subGain;
};


void Decoder::filterCoefs(){
    int k = tan((M_PI*abs(Fc))/Fs);
    double denominator = pow(k, 2) + 2*k + 1;
    
    //a values
    a[0] = 1;
    a[1] = (2*(pow(k,2)-1))/denominator;
    a[2] = (pow(k, 2) - 2*k + 1)/denominator;
    
    //b LF value
    bLF[0] = pow(k, 2) / denominator;
    bLF[1] = 2 * bLF[0];
    bLF[2] = bLF[0];
    
    //b HF values
    bHF[0] = 1/denominator;
    bHF[1] = -2 * bHF[0];
    bHF[2] = bHF[0];
};


void Decoder::filterLF(){
    double outLF;
    outLF = (double)outputS*bLF[0] + prevIn[0]*bLF[1] + prevIn[1]*bLF[2] + prevOut[0]*a[1] + prevOut[1]*a[2];
    
    prevOut[1] = prevOut[0];
    prevOut[0] = outLF;
    
    prevIn[1] = prevIn[0];
    prevIn[0] = (double)outputS;
    
    outputS = outLF;
};


void Decoder::filterHF(double &w, double &x, double &y, double &z){
    double outHFw, outHFx, outHFy, outHFz;
    
    //w Filtering
    outHFw = -((double)w*bHF[0] + prevInw[0]*bHF[1] + prevInw[1]*bHF[2] + prevOutw[0]*a[1] + prevOutw[1]*a[2]);
    prevOutw[1] = prevOutw[0];
    prevOutw[0] = outHFw;
    prevInw[1] = prevInw[0];
    prevInw[0] = (double)w;
    w = outHFw;
    
    //x Filtering
    outHFx = -((double)x*bHF[0] + prevInx[0]*bHF[1] + prevInx[1]*bHF[2] + prevOutx[0]*a[1] + prevOutx[1]*a[2]);
    prevOutx[1] = prevOutx[0];
    prevOutx[0] = outHFx;
    prevInx[1] = prevInx[0];
    prevInx[0] = (double)x;
    x = outHFx;
    
    //y Filtering
    outHFy = -((double)y*bHF[0] + prevIny[0]*bHF[1] + prevIny[1]*bHF[2] + prevOuty[0]*a[1] + prevOuty[1]*a[2]);
    prevOuty[1] = prevOuty[0];
    prevOuty[0] = outHFy;
    prevIny[1] = prevIny[0];
    prevIny[0] = (double)y;
    y = outHFy;
    
    //z Filtering
    outHFz = -((double)z*bHF[0] + prevInz[0]*bHF[1] + prevInz[1]*bHF[2] + prevOutz[0]*a[1] + prevOutz[1]*a[2]);
    prevOutz[1] = prevOutz[0];
    prevOutz[0] = outHFz;
    prevInz[1] = prevInz[0];
    prevInz[0] = (double)z;
    z = outHFz;
};


void Decoder::clearFilter(){
    for (int i=0; i<2; i++) {
        prevIn[i] = 0;
        prevOut[i] = 0;
        prevInw[i] = 0;
        prevOutw[i] = 0;
        prevInx[i] = 0;
        prevOutx[i] = 0;
        prevIny[i] = 0;
        prevOuty[i] = 0;
        prevInz[i] = 0;
        prevOutz[i] = 0;
    }
};