-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathofApp.h
More file actions
134 lines (106 loc) · 4.27 KB
/
ofApp.h
File metadata and controls
134 lines (106 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#pragma once
#include "ofMain.h"
#include "phonon.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
float bufferIn[512];
void audioIn(float * input, int bufferSize, int nChannels);
void audioOut(float * output, int bufferSize, int nChannels);
ofSoundStream soundStream;
//==== PHONON 3D or Steam Audio
//store data for the IPLAudioBuffer
IPLfloat32 iplDataDry[512];
IPLfloat32 iplDataSpatilized[1024];
//buffer object
IPLAudioBuffer bufFromOf; //from OF to Phonon
IPLAudioBuffer bufFromPhoton; //from Phonon back to OF
//setup stuff
IPLContext iplContext;
IPLRenderingSettings iplRenderSettings;
//renderer & effects
IPLhandle *binRen; //binarual renderer
IPLhandle *binEff; //object based binaural effect
void setupPhonon()
{
iplRenderSettings.frameSize = 512;
iplRenderSettings.samplingRate = 44100;
iplRenderSettings.convolutionType = IPL_CONVOLUTIONTYPE_PHONON;
//-- buffer dry (for OF)
IPLAudioFormat formatIn;
formatIn.channelLayoutType = IPL_CHANNELLAYOUTTYPE_SPEAKERS; // intended to be played back by a single speaker
formatIn.channelLayout = IPL_CHANNELLAYOUT_STEREO; //play over headphones
formatIn.numSpeakers = 2; //number of channels in the audio data, input is mono, but buffer In MUST HAVE SAME CHANNELS WITH BUFFER OUT, so 2 here
formatIn.channelOrder = IPL_CHANNELORDER_INTERLEAVED; //LRLRLR...
//setup two speakers, one on left, one on right, assume radius is 400
IPLVector3 speakers[2];
speakers[0].x = -400;
speakers[0].y = 0;
speakers[0].z = 0;
speakers[1].x = 400;
speakers[1].y = 0;
speakers[1].z = 0;
formatIn.speakerDirections = speakers;
bufFromOf.format = formatIn;
bufFromOf.numSamples = 512;
bufFromOf.interleavedBuffer = new IPLfloat32[bufFromOf.numSamples * formatIn.numSpeakers];
//-- buffer spatialize (for Phonon)
IPLAudioFormat formatOut;
formatOut.channelLayoutType = IPL_CHANNELLAYOUTTYPE_SPEAKERS; // intended to be played back by a single speaker
formatOut.channelLayout = IPL_CHANNELLAYOUT_STEREO; //play over headphones
formatOut.numSpeakers = 2; //number of channels in the audio data
formatOut.channelOrder = IPL_CHANNELORDER_INTERLEAVED; //LRLRLR...
formatOut.speakerDirections = speakers;
bufFromPhoton.format = formatOut;
bufFromPhoton.numSamples = 512;
bufFromPhoton.interleavedBuffer = new IPLfloat32[bufFromPhoton.numSamples * formatOut.numSpeakers]; //output is stereo, so multiple by 2 channels
//--- create renderer & effects
binRen = new IPLhandle; //IMPORTANT, need to allocate it first, if not, will raise error
IPLerror check = iplCreateBinauralRenderer(iplContext, iplRenderSettings, NULL, binRen);
if (check != IPL_STATUS_SUCCESS)
cout << "ERROR iplCreateBinauralRenderer: " << check << endl;
binEff = new IPLhandle;
check = iplCreateBinauralEffect(*binRen, formatIn, formatOut, binEff); //IMPORTANT: use *binRen, if not, will error
if (check != IPL_STATUS_SUCCESS)
cout << "ERROR iplCreateBinauralEffect: " << check << endl;
}
void spatilizeIPL(ofVec3f dirFromListener, float *in, int inSize, float *out, int outSize)
{
//-- get the direction from listener to the sound source
IPLVector3 dir;
dir.x = dirFromListener.x;
dir.y = dirFromListener.y;
dir.z = dirFromListener.z;
//-- get the input samples to IPL buffer
for (int i = 0; i < inSize; ++i)
{
iplDataDry[i * 2] = in[i];
iplDataDry[i * 2 + 1] = in[i];
}
this->bufFromOf.interleavedBuffer = iplDataDry;
//-- spatialize
iplApplyBinauralEffect(*binEff, bufFromOf, dir, IPL_HRTFINTERPOLATION_NEAREST, bufFromPhoton);
//-- output
for (int i = 0; i < outSize; ++i)
out[i] = this->bufFromPhoton.interleavedBuffer[i];
}
void exit()
{
soundStream.stop();
iplDestroyBinauralEffect(binEff);
iplDestroyBinauralRenderer(binRen);
}
};