create basic audio, towards sound sensors etc.
This commit is contained in:
132
examples/TinyAudio/b3AudioListener.cpp
Normal file
132
examples/TinyAudio/b3AudioListener.cpp
Normal file
@@ -0,0 +1,132 @@
|
||||
#include "b3AudioListener.h"
|
||||
#include "b3SoundSource.h"
|
||||
|
||||
template <class T>
|
||||
inline const T& MyMin(const T& a, const T& b)
|
||||
{
|
||||
return a < b ? a : b ;
|
||||
}
|
||||
#define MAX_SOUND_SOURCES 128
|
||||
|
||||
struct b3AudioListenerInternalData
|
||||
{
|
||||
int m_numControlTicks;
|
||||
double m_sampleRate;
|
||||
|
||||
b3SoundSource* m_soundSources[MAX_SOUND_SOURCES];
|
||||
|
||||
b3AudioListenerInternalData()
|
||||
:m_numControlTicks(64),
|
||||
m_sampleRate(48000)
|
||||
{
|
||||
for (int i=0;i<MAX_SOUND_SOURCES;i++)
|
||||
{
|
||||
m_soundSources[i] = 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
b3AudioListener::b3AudioListener()
|
||||
{
|
||||
m_data = new b3AudioListenerInternalData();
|
||||
}
|
||||
|
||||
b3AudioListener::~b3AudioListener()
|
||||
{
|
||||
delete m_data;
|
||||
}
|
||||
|
||||
int b3AudioListener::addSoundSource(b3SoundSource* source)
|
||||
{
|
||||
int soundIndex = -1;
|
||||
|
||||
for (int i=0;i<MAX_SOUND_SOURCES;i++)
|
||||
{
|
||||
if (m_data->m_soundSources[i]==0)
|
||||
{
|
||||
m_data->m_soundSources[i] = source;
|
||||
soundIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return soundIndex;
|
||||
}
|
||||
|
||||
void b3AudioListener::removeSoundSource(int soundSourceIndex)
|
||||
{
|
||||
if (soundSourceIndex >=0 && soundSourceIndex<MAX_SOUND_SOURCES)
|
||||
{
|
||||
m_data->m_soundSources[soundSourceIndex] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
b3AudioListenerInternalData* b3AudioListener::getTickData()
|
||||
{
|
||||
return m_data;
|
||||
}
|
||||
|
||||
const b3AudioListenerInternalData* b3AudioListener::getTickData() const
|
||||
{
|
||||
return m_data;
|
||||
}
|
||||
|
||||
double b3AudioListener::getSampleRate() const
|
||||
{
|
||||
return m_data->m_sampleRate;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int b3AudioListener::tick(void *outputBuffer,void *inputBuffer1,unsigned int nBufferFrames,
|
||||
double streamTime,unsigned int status,void *dataPointer)
|
||||
{
|
||||
b3AudioListenerInternalData *data = (b3AudioListenerInternalData *)dataPointer;
|
||||
register double outs[2],*samples = (double *)outputBuffer;
|
||||
register double tempOuts[2];
|
||||
int counter,nTicks = (int)nBufferFrames;
|
||||
bool done = false;
|
||||
|
||||
while(nTicks > 0 && !done)
|
||||
{
|
||||
counter = MyMin(nTicks,data->m_numControlTicks);
|
||||
bool newsynth = true;
|
||||
if(newsynth)
|
||||
{
|
||||
for(int i = 0; i < counter; i++)
|
||||
{
|
||||
outs[0] = 0.;
|
||||
outs[1] = 0.;
|
||||
//make_sound_double(outs,1);
|
||||
float numActiveSources = 0;
|
||||
|
||||
for (int i=0;i<MAX_SOUND_SOURCES;i++)
|
||||
{
|
||||
if (data->m_soundSources[i])
|
||||
{
|
||||
if (data->m_soundSources[i]->computeSamples(tempOuts,1, data->m_sampleRate))
|
||||
{
|
||||
numActiveSources++;
|
||||
//simple mixer
|
||||
outs[0] += tempOuts[0];
|
||||
outs[1] += tempOuts[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//simple mixer
|
||||
if (numActiveSources)
|
||||
{
|
||||
outs[0] *= 1./numActiveSources;
|
||||
outs[1] *= 1./numActiveSources;
|
||||
}
|
||||
|
||||
*samples++ = outs[0];
|
||||
*samples++ = outs[1];
|
||||
}
|
||||
nTicks -= counter;
|
||||
}
|
||||
if(nTicks == 0)
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user