Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
adds 3D ( as pan and fade ) to ofxiOSSoundPlayer
//
// ofxiOSSoundPlayer3D.h
// iPhone_OSX_BuildTheo
//
// Created by Theodore Watson on 5/9/13.
//
//
#pragma once
#include "ofxiOSSoundPlayer.h"
class ofxiOSSoundPlayer3D : public ofxiOSSoundPlayer{
public:
ofxiOSSoundPlayer3D();
void setLocation(ofPoint pos);
void setLocation(float x, float y, float z);
void update();
void setVolume(float vol);
static void setListenerLocation(float x, float y, float z);
static void setListenerLocation(ofPoint pos);
static void setListenerLookAt(float x, float y, float z, ofPoint cameraUp);
static void setListenerLookAt(ofPoint lookAt, ofPoint cameraUp);
static void setListenerVelocity(ofPoint vel);
static void setListenerVelocity(float x, float y, float z);
static void setListenerGain(float gain);
static void setReferenceDistance(float refDistance);
static void setMaxDistance(float maxDist);
ofPoint soundLocation;
float originalVolume;
};
//
// ofxiOSSoundPlayer3D.cpp
// iPhone_OSX_BuildTheo
//
// Created by Theodore Watson on 5/9/13.
//
//
#include "ofxiOSSoundPlayer3D.h"
static float maxDistance = 3000.0;
static float minDistance = 5.0;
static float listenerGain = 1.0;
static ofPoint listenerPos(0, 0, 100);
static ofPoint listenerLookAt(0, 0, -1);
static ofPoint cameraUp(0, 1, 0);
ofxiOSSoundPlayer3D::ofxiOSSoundPlayer3D(){
originalVolume = 1.0;
}
void ofxiOSSoundPlayer3D::setLocation(ofPoint pos){
soundLocation = pos;
}
void ofxiOSSoundPlayer3D::setLocation(float x, float y, float z){
setLocation(ofPoint(x, y, z));
}
void ofxiOSSoundPlayer3D::setVolume(float vol){
originalVolume = vol;
ofxiOSSoundPlayer::setVolume(vol);
}
void ofxiOSSoundPlayer3D::update(){
ofPoint delta = soundLocation - listenerPos;
float dist = delta.length();
//first the pan
ofPoint lookAtN = listenerLookAt.normalized();
ofPoint leftRight = lookAtN.crossed(cameraUp);
delta.normalize();
float dotP = leftRight.dot(delta);
ofxiOSSoundPlayer::setPan(dotP);
//to simulate more sound going into a single ear when the sound is just on one side.
float gainComp = 1.0 + powf(fabs(dotP), 2) * 0.55;
//now the gain
float gain = powf(ofMap(dist, minDistance, maxDistance, 1, 0, true), 3) * originalVolume * listenerGain * gainComp;
ofxiOSSoundPlayer::setVolume(gain);
}
//static methods
void ofxiOSSoundPlayer3D::setListenerLocation(ofPoint pos){
listenerPos = pos;
}
void ofxiOSSoundPlayer3D::setListenerLocation(float x, float y, float z){
ofxiOSSoundPlayer3D::setListenerLocation(ofPoint(x, y, z));
}
void ofxiOSSoundPlayer3D::setListenerLookAt(ofPoint lookAt, ofPoint camUp){
listenerLookAt = lookAt;
cameraUp = camUp.normalized();
}
void ofxiOSSoundPlayer3D::setListenerLookAt(float x, float y, float z, ofPoint camUp){
ofxiOSSoundPlayer3D::setListenerLookAt(ofPoint(x, y, z), camUp);
}
void ofxiOSSoundPlayer3D::setListenerVelocity(ofPoint vel){
cout << " setListenerVelocity - not implemented " << endl;
}
void ofxiOSSoundPlayer3D::setListenerVelocity(float x, float y, float z){
ofxiOSSoundPlayer3D::setListenerVelocity(ofPoint(x, y, z));
}
void ofxiOSSoundPlayer3D::setListenerGain(float gain){
listenerGain = gain;
}
void ofxiOSSoundPlayer3D::setReferenceDistance(float refDistance){
minDistance = refDistance;
}
void ofxiOSSoundPlayer3D::setMaxDistance(float maxDist){
maxDistance = maxDist;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment