Code-style consistency improvement:
Apply clang-format-all.sh using the _clang-format file through all the cpp/.h files. make sure not to apply it to certain serialization structures, since some parser expects the * as part of the name, instead of type. This commit contains no other changes aside from adding and applying clang-format-all.sh
This commit is contained in:
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -4,7 +4,6 @@
|
||||
#include "Bullet3Common/b3AlignedObjectArray.h"
|
||||
#include "Bullet3Common/b3HashMap.h"
|
||||
|
||||
|
||||
#include "b3SoundEngine.h"
|
||||
#include "b3SoundSource.h"
|
||||
#include <string>
|
||||
@@ -13,27 +12,27 @@
|
||||
struct MyHashString
|
||||
{
|
||||
std::string m_string;
|
||||
unsigned int m_hash;
|
||||
unsigned int m_hash;
|
||||
|
||||
B3_FORCE_INLINE unsigned int getHash()const
|
||||
B3_FORCE_INLINE unsigned int getHash() const
|
||||
{
|
||||
return m_hash;
|
||||
}
|
||||
|
||||
MyHashString(const char* name)
|
||||
:m_string(name)
|
||||
: m_string(name)
|
||||
{
|
||||
/* magic numbers from http://www.isthe.com/chongo/tech/comp/fnv/ */
|
||||
static const unsigned int InitialFNV = 2166136261u;
|
||||
static const unsigned int InitialFNV = 2166136261u;
|
||||
static const unsigned int FNVMultiple = 16777619u;
|
||||
|
||||
/* Fowler / Noll / Vo (FNV) Hash */
|
||||
unsigned int hash = InitialFNV;
|
||||
|
||||
for(int i = 0; m_string[i]; i++)
|
||||
|
||||
for (int i = 0; m_string[i]; i++)
|
||||
{
|
||||
hash = hash ^ (m_string[i]); /* xor the low 8 bits */
|
||||
hash = hash * FNVMultiple; /* multiply by the magic number */
|
||||
hash = hash ^ (m_string[i]); /* xor the low 8 bits */
|
||||
hash = hash * FNVMultiple; /* multiply by the magic number */
|
||||
}
|
||||
m_hash = hash;
|
||||
}
|
||||
@@ -42,38 +41,36 @@ struct MyHashString
|
||||
{
|
||||
return (m_string == other.m_string);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
double base_frequency = 440.0;
|
||||
double base_pitch = 69.0;
|
||||
|
||||
double MidiPitch2Frequency(double incoming_note) {
|
||||
return base_frequency * pow (2.0, (incoming_note - base_pitch) / 12.0);
|
||||
double MidiPitch2Frequency(double incoming_note)
|
||||
{
|
||||
return base_frequency * pow(2.0, (incoming_note - base_pitch) / 12.0);
|
||||
}
|
||||
|
||||
double FrequencytoMidiPitch(double incoming_frequency) {
|
||||
return base_pitch + (12.0 * log(incoming_frequency / base_frequency) / log(2));
|
||||
double FrequencytoMidiPitch(double incoming_frequency)
|
||||
{
|
||||
return base_pitch + (12.0 * log(incoming_frequency / base_frequency) / log(2));
|
||||
}
|
||||
|
||||
|
||||
class TinyAudioExample : public CommonExampleInterface
|
||||
{
|
||||
|
||||
GUIHelperInterface* m_guiHelper;
|
||||
|
||||
|
||||
b3SoundEngine m_soundEngine;
|
||||
int m_wavId;
|
||||
|
||||
b3HashMap<MyHashString,int> m_keyToSoundSource;
|
||||
|
||||
b3HashMap<MyHashString, int> m_keyToSoundSource;
|
||||
|
||||
public:
|
||||
TinyAudioExample(struct GUIHelperInterface* helper)
|
||||
:m_guiHelper(helper)
|
||||
: m_guiHelper(helper)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
virtual ~TinyAudioExample()
|
||||
{
|
||||
}
|
||||
@@ -87,54 +84,50 @@ public:
|
||||
|
||||
m_wavId = m_soundEngine.loadWavFile("wav/xylophone.rosewood.ff.C5B5_1.wav");
|
||||
int sampleRate = m_soundEngine.getSampleRate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
virtual void exitPhysics()
|
||||
{
|
||||
|
||||
m_soundEngine.exit();
|
||||
}
|
||||
|
||||
virtual void renderScene()
|
||||
{
|
||||
}
|
||||
|
||||
virtual void stepSimulation(float deltaTime)
|
||||
|
||||
virtual void stepSimulation(float deltaTime)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void physicsDebugDraw(int debugFlags)
|
||||
virtual void physicsDebugDraw(int debugFlags)
|
||||
{
|
||||
}
|
||||
virtual bool mouseMoveCallback(float x,float y)
|
||||
virtual bool mouseMoveCallback(float x, float y)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
virtual bool mouseButtonCallback(int button, int state, float x, float y)
|
||||
virtual bool mouseButtonCallback(int button, int state, float x, float y)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
virtual bool keyboardCallback(int key, int state)
|
||||
virtual bool keyboardCallback(int key, int state)
|
||||
{
|
||||
if (key>='a' && key<='z')
|
||||
if (key >= 'a' && key <= 'z')
|
||||
{
|
||||
char keyStr[2];
|
||||
keyStr[0] = (char)key;
|
||||
keyStr[1] = 0;
|
||||
MyHashString hs (keyStr);
|
||||
MyHashString hs(keyStr);
|
||||
|
||||
if (state)
|
||||
{
|
||||
int soundSourceIndex = m_soundEngine.getAvailableSoundSource();
|
||||
if (soundSourceIndex>=0)
|
||||
if (soundSourceIndex >= 0)
|
||||
{
|
||||
|
||||
int note = key-(97-58);
|
||||
int note = key - (97 - 58);
|
||||
double freq = MidiPitch2Frequency(note);
|
||||
|
||||
|
||||
b3SoundMessage msg;
|
||||
msg.m_type = B3_SOUND_SOURCE_SINE_OSCILLATOR;
|
||||
msg.m_frequency = freq;
|
||||
@@ -147,10 +140,10 @@ public:
|
||||
msg.m_releaseRate = 0.001;
|
||||
|
||||
m_soundEngine.startSound(soundSourceIndex, msg);
|
||||
m_keyToSoundSource.insert(hs,soundSourceIndex);
|
||||
m_keyToSoundSource.insert(hs, soundSourceIndex);
|
||||
//printf("soundSourceIndex:%d\n", soundSourceIndex);
|
||||
|
||||
#if 0
|
||||
|
||||
#if 0
|
||||
b3SoundSource* soundSource = this->m_soundSourcesPool[soundSourceIndex];
|
||||
|
||||
soundSource->setOscillatorFrequency(0, freq );
|
||||
@@ -165,9 +158,10 @@ public:
|
||||
printf("just inserted: %d\n", newIndex);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
int* soundSourceIndexPtr = m_keyToSoundSource[hs];
|
||||
if (soundSourceIndexPtr)
|
||||
@@ -176,7 +170,7 @@ public:
|
||||
//printf("releaseSound: %d\n", soundSourceIndex);
|
||||
m_soundEngine.releaseSound(soundSourceIndex);
|
||||
}
|
||||
#if 0
|
||||
#if 0
|
||||
if (soundSourceIndex>=0)
|
||||
{
|
||||
printf("releasing %d\n", soundSourceIndex);
|
||||
@@ -184,11 +178,10 @@ public:
|
||||
soundSource->stopSound();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -197,17 +190,14 @@ public:
|
||||
float dist = 4;
|
||||
float pitch = 52;
|
||||
float yaw = 35;
|
||||
float targetPos[3]={0,0,0};
|
||||
m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]);
|
||||
float targetPos[3] = {0, 0, 0};
|
||||
m_guiHelper->resetCamera(dist, pitch, yaw, targetPos[0], targetPos[1], targetPos[2]);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
CommonExampleInterface* TinyAudioExampleCreateFunc(CommonExampleOptions& options)
|
||||
CommonExampleInterface* TinyAudioExampleCreateFunc(CommonExampleOptions& options)
|
||||
{
|
||||
return new TinyAudioExample(options.m_guiHelper);
|
||||
|
||||
}
|
||||
|
||||
|
||||
B3_STANDALONE_EXAMPLE(TinyAudioExampleCreateFunc)
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
#ifndef TINY_AUDIO_EXAMPLE_H
|
||||
#define TINY_AUDIO_EXAMPLE_H
|
||||
|
||||
class CommonExampleInterface* TinyAudioExampleCreateFunc(struct CommonExampleOptions& options);
|
||||
class CommonExampleInterface* TinyAudioExampleCreateFunc(struct CommonExampleOptions& options);
|
||||
|
||||
#endif //TINY_AUDIO_EXAMPLE_H
|
||||
#endif //TINY_AUDIO_EXAMPLE_H
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
//! ADSR envelope states.
|
||||
|
||||
|
||||
enum
|
||||
{
|
||||
ADSR_ATTACK, /*!< Attack */
|
||||
@@ -69,7 +68,6 @@ double b3ADSR::tick()
|
||||
{
|
||||
keyOff();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -96,7 +94,7 @@ void b3ADSR::keyOn(bool autoKeyOff)
|
||||
m_autoKeyOff = autoKeyOff;
|
||||
if (m_target <= 0.0)
|
||||
m_target = 1.0;
|
||||
if (m_attackRate==1)
|
||||
if (m_attackRate == 1)
|
||||
{
|
||||
m_value = 1.0;
|
||||
}
|
||||
@@ -108,5 +106,4 @@ void b3ADSR::keyOff()
|
||||
m_autoKeyOff = false;
|
||||
m_target = 0.0;
|
||||
m_state = ADSR_RELEASE;
|
||||
|
||||
}
|
||||
|
||||
@@ -12,8 +12,8 @@ class b3ADSR
|
||||
double m_releaseTime;
|
||||
double m_sustainLevel;
|
||||
bool m_autoKeyOff;
|
||||
public:
|
||||
|
||||
public:
|
||||
b3ADSR();
|
||||
virtual ~b3ADSR();
|
||||
|
||||
@@ -22,14 +22,13 @@ public:
|
||||
void keyOn(bool autoKeyOff);
|
||||
void keyOff();
|
||||
|
||||
void setValues(double attack,double decay,double sustain,double release)
|
||||
void setValues(double attack, double decay, double sustain, double release)
|
||||
{
|
||||
m_attackRate = attack;
|
||||
m_decayRate = decay;
|
||||
m_sustainLevel = sustain;
|
||||
m_releaseRate = release;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif //B3_ADSR_H
|
||||
#endif //B3_ADSR_H
|
||||
@@ -4,9 +4,9 @@
|
||||
#include "b3WriteWavFile.h"
|
||||
|
||||
template <class T>
|
||||
inline const T& MyMin(const T& a, const T& b)
|
||||
inline const T& MyMin(const T& a, const T& b)
|
||||
{
|
||||
return a < b ? a : b ;
|
||||
return a < b ? a : b;
|
||||
}
|
||||
#define MAX_SOUND_SOURCES 128
|
||||
#define B3_SAMPLE_RATE 48000
|
||||
@@ -18,17 +18,15 @@ struct b3AudioListenerInternalData
|
||||
|
||||
b3SoundSource* m_soundSources[MAX_SOUND_SOURCES];
|
||||
|
||||
|
||||
b3WriteWavFile m_wavOut2;
|
||||
bool m_writeWavOut;
|
||||
|
||||
b3AudioListenerInternalData()
|
||||
:m_numControlTicks(64),
|
||||
m_sampleRate(B3_SAMPLE_RATE),
|
||||
m_writeWavOut(false)
|
||||
: m_numControlTicks(64),
|
||||
m_sampleRate(B3_SAMPLE_RATE),
|
||||
m_writeWavOut(false)
|
||||
{
|
||||
|
||||
for (int i=0;i<MAX_SOUND_SOURCES;i++)
|
||||
for (int i = 0; i < MAX_SOUND_SOURCES; i++)
|
||||
{
|
||||
m_soundSources[i] = 0;
|
||||
}
|
||||
@@ -40,7 +38,7 @@ b3AudioListener::b3AudioListener()
|
||||
m_data = new b3AudioListenerInternalData();
|
||||
if (m_data->m_writeWavOut)
|
||||
{
|
||||
m_data->m_wavOut2.setWavFile("bulletAudio2.wav",B3_SAMPLE_RATE,2,false);
|
||||
m_data->m_wavOut2.setWavFile("bulletAudio2.wav", B3_SAMPLE_RATE, 2, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,9 +56,9 @@ int b3AudioListener::addSoundSource(b3SoundSource* source)
|
||||
{
|
||||
int soundIndex = -1;
|
||||
|
||||
for (int i=0;i<MAX_SOUND_SOURCES;i++)
|
||||
for (int i = 0; i < MAX_SOUND_SOURCES; i++)
|
||||
{
|
||||
if (m_data->m_soundSources[i]==0)
|
||||
if (m_data->m_soundSources[i] == 0)
|
||||
{
|
||||
m_data->m_soundSources[i] = source;
|
||||
soundIndex = i;
|
||||
@@ -72,9 +70,9 @@ int b3AudioListener::addSoundSource(b3SoundSource* source)
|
||||
|
||||
void b3AudioListener::removeSoundSource(b3SoundSource* source)
|
||||
{
|
||||
for (int i=0;i<MAX_SOUND_SOURCES;i++)
|
||||
for (int i = 0; i < MAX_SOUND_SOURCES; i++)
|
||||
{
|
||||
if (m_data->m_soundSources[i]==source)
|
||||
if (m_data->m_soundSources[i] == source)
|
||||
{
|
||||
m_data->m_soundSources[i] = 0;
|
||||
}
|
||||
@@ -101,69 +99,67 @@ void b3AudioListener::setSampleRate(double sampleRate)
|
||||
m_data->m_sampleRate = sampleRate;
|
||||
}
|
||||
|
||||
|
||||
int b3AudioListener::tick(void *outputBuffer,void *inputBuffer1,unsigned int nBufferFrames,
|
||||
double streamTime,unsigned int status,void *dataPointer)
|
||||
int b3AudioListener::tick(void* outputBuffer, void* inputBuffer1, unsigned int nBufferFrames,
|
||||
double streamTime, unsigned int status, void* dataPointer)
|
||||
{
|
||||
B3_PROFILE("b3AudioListener::tick");
|
||||
|
||||
b3AudioListenerInternalData *data = (b3AudioListenerInternalData *)dataPointer;
|
||||
register double outs[2],*samples = (double *)outputBuffer;
|
||||
b3AudioListenerInternalData* data = (b3AudioListenerInternalData*)dataPointer;
|
||||
register double outs[2], *samples = (double*)outputBuffer;
|
||||
register double tempOuts[2];
|
||||
int counter,nTicks = (int)nBufferFrames;
|
||||
int counter, nTicks = (int)nBufferFrames;
|
||||
bool done = false;
|
||||
|
||||
|
||||
int numSamples = 0;
|
||||
|
||||
while(nTicks > 0 && !done)
|
||||
while (nTicks > 0 && !done)
|
||||
{
|
||||
counter = MyMin(nTicks,data->m_numControlTicks);
|
||||
counter = MyMin(nTicks, data->m_numControlTicks);
|
||||
bool newsynth = true;
|
||||
if(newsynth)
|
||||
if (newsynth)
|
||||
{
|
||||
for(int i = 0; i < counter; i++)
|
||||
for (int i = 0; i < counter; i++)
|
||||
{
|
||||
outs[0] = 0.;
|
||||
outs[1] = 0.;
|
||||
//make_sound_double(outs,1);
|
||||
float numActiveSources = 0;
|
||||
float numActiveSources = 0;
|
||||
|
||||
for (int i=0;i<MAX_SOUND_SOURCES;i++)
|
||||
for (int i = 0; i < MAX_SOUND_SOURCES; i++)
|
||||
{
|
||||
if (data->m_soundSources[i])
|
||||
{
|
||||
if (data->m_soundSources[i])
|
||||
{
|
||||
tempOuts[0] = 0;
|
||||
tempOuts[1] = 0;
|
||||
tempOuts[0] = 0;
|
||||
tempOuts[1] = 0;
|
||||
|
||||
if (data->m_soundSources[i]->computeSamples(tempOuts,1, data->m_sampleRate))
|
||||
{
|
||||
numActiveSources++;
|
||||
//simple mixer
|
||||
outs[0] += tempOuts[0];
|
||||
outs[1] += tempOuts[1];
|
||||
}
|
||||
if (data->m_soundSources[i]->computeSamples(tempOuts, 1, data->m_sampleRate))
|
||||
{
|
||||
numActiveSources++;
|
||||
//simple mixer
|
||||
outs[0] += tempOuts[0];
|
||||
outs[1] += tempOuts[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//soft-clipping of sounds
|
||||
outs[0] = tanh(outs[0]);
|
||||
outs[1] = tanh(outs[1]);
|
||||
//soft-clipping of sounds
|
||||
outs[0] = tanh(outs[0]);
|
||||
outs[1] = tanh(outs[1]);
|
||||
|
||||
*samples++ = outs[0];
|
||||
*samples++ = outs[1];
|
||||
numSamples++;
|
||||
|
||||
}
|
||||
nTicks -= counter;
|
||||
}
|
||||
if(nTicks == 0)
|
||||
if (nTicks == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
//logging to wav file
|
||||
if (data->m_writeWavOut && numSamples)
|
||||
{
|
||||
data->m_wavOut2.tick( (double *)outputBuffer,numSamples);
|
||||
data->m_wavOut2.tick((double*)outputBuffer, numSamples);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
class b3SoundSource;
|
||||
|
||||
|
||||
class b3AudioListener
|
||||
{
|
||||
struct b3AudioListenerInternalData* m_data;
|
||||
@@ -11,9 +10,9 @@ class b3AudioListener
|
||||
public:
|
||||
b3AudioListener();
|
||||
virtual ~b3AudioListener();
|
||||
|
||||
static int tick(void *outputBuffer, void *inputBuffer1, unsigned int nBufferFrames,
|
||||
double streamTime, unsigned int status, void *dataPointer);
|
||||
|
||||
static int tick(void* outputBuffer, void* inputBuffer1, unsigned int nBufferFrames,
|
||||
double streamTime, unsigned int status, void* dataPointer);
|
||||
|
||||
int addSoundSource(b3SoundSource* source);
|
||||
void removeSoundSource(b3SoundSource* source);
|
||||
@@ -23,7 +22,6 @@ public:
|
||||
|
||||
double getSampleRate() const;
|
||||
void setSampleRate(double sampleRate);
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#endif //B3_AUDIO_LISTENER_H
|
||||
#endif //B3_AUDIO_LISTENER_H
|
||||
@@ -101,7 +101,7 @@ b3WavTicker b3ReadWavFile::createWavTicker(double sampleRate)
|
||||
|
||||
bool b3ReadWavFile::getWavInfo(const char *fileName)
|
||||
{
|
||||
fd_ = fopen(fileName,"rb");
|
||||
fd_ = fopen(fileName, "rb");
|
||||
if (fd_ == 0)
|
||||
return false;
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#ifndef B3_READ_WAV_FILE_H
|
||||
#define B3_READ_WAV_FILE_H
|
||||
|
||||
|
||||
#include "Bullet3Common/b3AlignedObjectArray.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -14,8 +13,6 @@ struct b3WavTicker
|
||||
double rate_;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class b3ReadWavFile
|
||||
{
|
||||
bool byteswap_;
|
||||
@@ -27,8 +24,8 @@ class b3ReadWavFile
|
||||
unsigned long dataOffset_;
|
||||
unsigned int channels_;
|
||||
bool m_machineIsLittleEndian;
|
||||
public:
|
||||
|
||||
public:
|
||||
b3ReadWavFile();
|
||||
virtual ~b3ReadWavFile();
|
||||
|
||||
@@ -47,11 +44,10 @@ public:
|
||||
|
||||
bool read(unsigned long startFrame, bool doNormalize);
|
||||
|
||||
int getNumFrames() const
|
||||
int getNumFrames() const
|
||||
{
|
||||
return m_numFrames;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //B3_READ_WAV_FILE_H
|
||||
|
||||
@@ -88,7 +88,7 @@ void b3SoundEngine::exit()
|
||||
}
|
||||
m_data->m_soundSources.clear();
|
||||
|
||||
for (int i=0;i<m_data->m_wavFiles.size();i++)
|
||||
for (int i = 0; i < m_data->m_wavFiles.size(); i++)
|
||||
{
|
||||
b3ReadWavFile** wavPtr = m_data->m_wavFiles.getAtIndex(i);
|
||||
if (wavPtr && *wavPtr)
|
||||
@@ -99,7 +99,6 @@ void b3SoundEngine::exit()
|
||||
}
|
||||
m_data->m_wavFiles.clear();
|
||||
m_data->m_name2wav.clear();
|
||||
|
||||
}
|
||||
|
||||
int b3SoundEngine::getAvailableSoundSource()
|
||||
@@ -117,10 +116,10 @@ int b3SoundEngine::getAvailableSoundSource()
|
||||
void b3SoundEngine::startSound(int soundSourceIndex, b3SoundMessage msg)
|
||||
{
|
||||
b3SoundSource* soundSource = m_data->m_soundSources[soundSourceIndex];
|
||||
soundSource->setOscillatorAmplitude(0,msg.m_amplitude);
|
||||
soundSource->setOscillatorAmplitude(1,msg.m_amplitude);
|
||||
soundSource->setOscillatorAmplitude(0, msg.m_amplitude);
|
||||
soundSource->setOscillatorAmplitude(1, msg.m_amplitude);
|
||||
|
||||
soundSource->setADSR(msg.m_attackRate,msg.m_decayRate,msg.m_sustainLevel,msg.m_releaseRate);
|
||||
soundSource->setADSR(msg.m_attackRate, msg.m_decayRate, msg.m_sustainLevel, msg.m_releaseRate);
|
||||
|
||||
switch (msg.m_type)
|
||||
{
|
||||
@@ -128,7 +127,7 @@ void b3SoundEngine::startSound(int soundSourceIndex, b3SoundMessage msg)
|
||||
{
|
||||
soundSource->setOscillatorFrequency(0, msg.m_frequency);
|
||||
soundSource->setOscillatorFrequency(1, msg.m_frequency);
|
||||
|
||||
|
||||
soundSource->startSound(msg.m_autoKeyOff);
|
||||
break;
|
||||
}
|
||||
@@ -138,8 +137,8 @@ void b3SoundEngine::startSound(int soundSourceIndex, b3SoundMessage msg)
|
||||
if (wavFilePtr)
|
||||
{
|
||||
b3ReadWavFile* wavFile = *wavFilePtr;
|
||||
soundSource->setWavFile(0,wavFile,getSampleRate());
|
||||
soundSource->setWavFile(1,wavFile,getSampleRate());
|
||||
soundSource->setWavFile(0, wavFile, getSampleRate());
|
||||
soundSource->setWavFile(1, wavFile, getSampleRate());
|
||||
soundSource->startSound(msg.m_autoKeyOff);
|
||||
}
|
||||
break;
|
||||
@@ -174,7 +173,7 @@ int b3SoundEngine::loadWavFile(const char* fileName)
|
||||
wavFile->normalize(1);
|
||||
int wavUID = m_data->m_wavFileUidGenerator++;
|
||||
m_data->m_wavFiles.insert(wavUID, wavFile);
|
||||
m_data->m_name2wav.insert(fileName,wavUID);
|
||||
m_data->m_name2wav.insert(fileName, wavUID);
|
||||
return wavUID;
|
||||
}
|
||||
return 0;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
struct b3SoundMessage
|
||||
{
|
||||
int m_type;//B3_SOUND_SOURCE_TYPE
|
||||
int m_type; //B3_SOUND_SOURCE_TYPE
|
||||
double m_amplitude;
|
||||
|
||||
double m_frequency;
|
||||
@@ -19,15 +19,15 @@ struct b3SoundMessage
|
||||
bool m_autoKeyOff;
|
||||
|
||||
b3SoundMessage()
|
||||
:m_type(B3_SOUND_SOURCE_SINE_OSCILLATOR),
|
||||
m_amplitude(0.5),
|
||||
m_frequency(440),
|
||||
m_wavId(-1),
|
||||
m_attackRate(0.001),
|
||||
m_decayRate(0.00001),
|
||||
m_sustainLevel(0.5),
|
||||
m_releaseRate(0.0005),
|
||||
m_autoKeyOff(false)
|
||||
: m_type(B3_SOUND_SOURCE_SINE_OSCILLATOR),
|
||||
m_amplitude(0.5),
|
||||
m_frequency(440),
|
||||
m_wavId(-1),
|
||||
m_attackRate(0.001),
|
||||
m_decayRate(0.00001),
|
||||
m_sustainLevel(0.5),
|
||||
m_releaseRate(0.0005),
|
||||
m_autoKeyOff(false)
|
||||
{
|
||||
}
|
||||
};
|
||||
@@ -35,9 +35,8 @@ struct b3SoundMessage
|
||||
class b3SoundEngine
|
||||
{
|
||||
struct b3SoundEngineInternalData* m_data;
|
||||
|
||||
public:
|
||||
|
||||
|
||||
public:
|
||||
b3SoundEngine();
|
||||
virtual ~b3SoundEngine();
|
||||
|
||||
@@ -51,7 +50,6 @@ class b3SoundEngine
|
||||
int loadWavFile(const char* fileName);
|
||||
|
||||
double getSampleRate() const;
|
||||
|
||||
};
|
||||
|
||||
#endif //B3_SOUND_ENGINE_H
|
||||
#endif //B3_SOUND_ENGINE_H
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
#include "b3SoundSource.h"
|
||||
|
||||
#define MY2PI (2.*3.14159265)
|
||||
#define MY2PI (2. * 3.14159265)
|
||||
#include <math.h>
|
||||
|
||||
|
||||
#include "Bullet3Common/b3FileUtils.h"
|
||||
#include "b3ReadWavFile.h"
|
||||
#include "b3ADSR.h"
|
||||
@@ -15,19 +14,18 @@ struct b3SoundOscillator
|
||||
double m_frequency;
|
||||
double m_amplitude;
|
||||
double m_phase;
|
||||
|
||||
b3WavTicker m_wavTicker;
|
||||
|
||||
b3WavTicker m_wavTicker;
|
||||
|
||||
double sampleSineWaveForm(double sampleRate)
|
||||
{
|
||||
while (m_phase >= MY2PI)
|
||||
m_phase -= MY2PI;
|
||||
|
||||
double z = sinf(m_phase);
|
||||
double sample = m_amplitude*z;
|
||||
double z = sinf(m_phase);
|
||||
double sample = m_amplitude * z;
|
||||
|
||||
m_phase += MY2PI * (1./sampleRate) * m_frequency;
|
||||
m_phase += MY2PI * (1. / sampleRate) * m_frequency;
|
||||
return sample;
|
||||
}
|
||||
|
||||
@@ -36,10 +34,10 @@ struct b3SoundOscillator
|
||||
while (m_phase >= MY2PI)
|
||||
m_phase -= MY2PI;
|
||||
|
||||
double z = 2.*(m_phase)/MY2PI-1.;
|
||||
double sample = m_amplitude*z;
|
||||
double z = 2. * (m_phase) / MY2PI - 1.;
|
||||
double sample = m_amplitude * z;
|
||||
|
||||
m_phase += MY2PI * (1./sampleRate) * m_frequency;
|
||||
m_phase += MY2PI * (1. / sampleRate) * m_frequency;
|
||||
return sample;
|
||||
}
|
||||
|
||||
@@ -49,12 +47,11 @@ struct b3SoundOscillator
|
||||
}
|
||||
|
||||
b3SoundOscillator()
|
||||
:m_type(0),
|
||||
m_frequency(442.),
|
||||
m_amplitude(1),
|
||||
m_phase(0)
|
||||
: m_type(0),
|
||||
m_frequency(442.),
|
||||
m_amplitude(1),
|
||||
m_phase(0)
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
#define MAX_OSCILLATORS 2
|
||||
@@ -65,7 +62,7 @@ struct b3SoundSourceInternalData
|
||||
b3ADSR m_envelope;
|
||||
b3ReadWavFile* m_wavFilePtr;
|
||||
b3SoundSourceInternalData()
|
||||
:m_wavFilePtr(0)
|
||||
: m_wavFilePtr(0)
|
||||
{
|
||||
}
|
||||
};
|
||||
@@ -80,30 +77,28 @@ b3SoundSource::~b3SoundSource()
|
||||
delete m_data;
|
||||
}
|
||||
|
||||
void b3SoundSource::setADSR( double attack, double decay, double sustain, double release)
|
||||
void b3SoundSource::setADSR(double attack, double decay, double sustain, double release)
|
||||
{
|
||||
m_data->m_envelope.setValues(attack,decay,sustain,release);
|
||||
m_data->m_envelope.setValues(attack, decay, sustain, release);
|
||||
}
|
||||
|
||||
|
||||
bool b3SoundSource::computeSamples(double* sampleBuffer, int numSamples, double sampleRate)
|
||||
{
|
||||
|
||||
double* outputSamples = sampleBuffer;
|
||||
int numActive = 0;
|
||||
|
||||
for (int i=0;i<numSamples;i++)
|
||||
for (int i = 0; i < numSamples; i++)
|
||||
{
|
||||
double samples[MAX_OSCILLATORS] ={0};
|
||||
double samples[MAX_OSCILLATORS] = {0};
|
||||
|
||||
double env = m_data->m_envelope.tick();
|
||||
if (env)
|
||||
{
|
||||
for (int osc=0;osc<MAX_OSCILLATORS;osc++)
|
||||
for (int osc = 0; osc < MAX_OSCILLATORS; osc++)
|
||||
{
|
||||
if (m_data->m_oscillators[osc].m_type == 0)
|
||||
{
|
||||
samples[osc] += env * m_data->m_oscillators[osc].sampleSineWaveForm(sampleRate);
|
||||
samples[osc] += env * m_data->m_oscillators[osc].sampleSineWaveForm(sampleRate);
|
||||
numActive++;
|
||||
}
|
||||
|
||||
@@ -116,14 +111,15 @@ bool b3SoundSource::computeSamples(double* sampleBuffer, int numSamples, double
|
||||
if (m_data->m_oscillators[osc].m_type == 128)
|
||||
{
|
||||
int frame = 0;
|
||||
double data = env * m_data->m_oscillators[osc].m_amplitude * m_data->m_wavFilePtr->tick(frame,&m_data->m_oscillators[osc].m_wavTicker);
|
||||
double data = env * m_data->m_oscillators[osc].m_amplitude * m_data->m_wavFilePtr->tick(frame, &m_data->m_oscillators[osc].m_wavTicker);
|
||||
samples[osc] += data;
|
||||
numActive++;
|
||||
}
|
||||
}
|
||||
} else
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int osc=0;osc<MAX_OSCILLATORS;osc++)
|
||||
for (int osc = 0; osc < MAX_OSCILLATORS; osc++)
|
||||
{
|
||||
if (m_data->m_oscillators[osc].m_type == 128)
|
||||
{
|
||||
@@ -140,10 +136,10 @@ bool b3SoundSource::computeSamples(double* sampleBuffer, int numSamples, double
|
||||
}
|
||||
|
||||
*outputSamples++ = sampleRight;
|
||||
*outputSamples++ = sampleLeft ;
|
||||
*outputSamples++ = sampleLeft;
|
||||
}
|
||||
|
||||
/* if (m_data->m_flags & looping)
|
||||
/* if (m_data->m_flags & looping)
|
||||
{
|
||||
for (int osc=0;osc<MAX_OSCILLATORS;osc++)
|
||||
{
|
||||
@@ -152,8 +148,8 @@ bool b3SoundSource::computeSamples(double* sampleBuffer, int numSamples, double
|
||||
}
|
||||
}
|
||||
*/
|
||||
return numActive>0;
|
||||
// return false;
|
||||
return numActive > 0;
|
||||
// return false;
|
||||
}
|
||||
|
||||
int b3SoundSource::getNumOscillators() const
|
||||
@@ -187,25 +183,24 @@ void b3SoundSource::startSound(bool autoKeyOff)
|
||||
{
|
||||
if (m_data->m_envelope.isIdle())
|
||||
{
|
||||
for (int osc=0;osc<MAX_OSCILLATORS;osc++)
|
||||
for (int osc = 0; osc < MAX_OSCILLATORS; osc++)
|
||||
{
|
||||
m_data->m_oscillators[osc].reset();
|
||||
|
||||
if (m_data->m_oscillators[osc].m_type == B3_SOUND_SOURCE_WAV_FILE)// .m_wavTicker.finished_)
|
||||
if (m_data->m_oscillators[osc].m_type == B3_SOUND_SOURCE_WAV_FILE) // .m_wavTicker.finished_)
|
||||
{
|
||||
|
||||
//test reverse playback of wav
|
||||
//m_data->m_oscillators[osc].m_wavTicker.rate_ *= -1;
|
||||
if (m_data->m_oscillators[osc].m_wavTicker.rate_<0)
|
||||
if (m_data->m_oscillators[osc].m_wavTicker.rate_ < 0)
|
||||
{
|
||||
m_data->m_oscillators[osc].m_wavTicker.time_ = m_data->m_wavFilePtr->getNumFrames()-1.;
|
||||
} else
|
||||
m_data->m_oscillators[osc].m_wavTicker.time_ = m_data->m_wavFilePtr->getNumFrames() - 1.;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_data->m_oscillators[osc].m_wavTicker.time_ = 0.f;
|
||||
}
|
||||
|
||||
m_data->m_oscillators[osc].m_wavTicker.finished_ = false;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -217,21 +212,20 @@ void b3SoundSource::stopSound()
|
||||
m_data->m_envelope.keyOff();
|
||||
}
|
||||
|
||||
|
||||
bool b3SoundSource::setWavFile(int oscillatorIndex, b3ReadWavFile* wavFilePtr, int sampleRate)
|
||||
{
|
||||
{
|
||||
m_data->m_wavFilePtr = wavFilePtr;
|
||||
m_data->m_oscillators[oscillatorIndex].m_wavTicker = m_data->m_wavFilePtr->createWavTicker(sampleRate);
|
||||
|
||||
// waveIn.openFile(resourcePath);
|
||||
// waveIn.openFile(resourcePath);
|
||||
double rate = 1.0;
|
||||
// rate = waveIn.getFileRate() / stkSampleRate;
|
||||
// waveIn.setRate( rate );
|
||||
// waveIn.ignoreSampleRateChange();
|
||||
// rate = waveIn.getFileRate() / stkSampleRate;
|
||||
// waveIn.setRate( rate );
|
||||
// waveIn.ignoreSampleRateChange();
|
||||
// Find out how many channels we have.
|
||||
// int channels = waveIn.channelsOut();
|
||||
// m_data->m_oscillators[oscillatorIndex].m_frames.resize( 1, channels );
|
||||
// int channels = waveIn.channelsOut();
|
||||
// m_data->m_oscillators[oscillatorIndex].m_frames.resize( 1, channels );
|
||||
m_data->m_oscillators[oscillatorIndex].m_type = 128;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -3,24 +3,22 @@
|
||||
|
||||
#include "b3Sound_C_Api.h"
|
||||
|
||||
|
||||
class b3SoundSource
|
||||
{
|
||||
struct b3SoundSourceInternalData* m_data;
|
||||
|
||||
public:
|
||||
|
||||
b3SoundSource();
|
||||
virtual ~b3SoundSource();
|
||||
|
||||
virtual bool computeSamples(double *sampleBuffer, int numSamples, double sampleRate);
|
||||
|
||||
virtual bool computeSamples(double* sampleBuffer, int numSamples, double sampleRate);
|
||||
|
||||
int getNumOscillators() const;
|
||||
void setOscillatorType(int oscillatorIndex, int type);
|
||||
void setOscillatorFrequency(int oscillatorIndex, double frequency);
|
||||
void setOscillatorAmplitude(int oscillatorIndex, double amplitude);
|
||||
void setOscillatorPhase(int oscillatorIndex, double phase);
|
||||
void setADSR( double attackRate, double decayRate, double sustainLevel, double releaseRate);
|
||||
void setADSR(double attackRate, double decayRate, double sustainLevel, double releaseRate);
|
||||
|
||||
bool setWavFile(int oscillatorIndex, class b3ReadWavFile* wavFilePtr, int sampleRate);
|
||||
|
||||
@@ -30,4 +28,4 @@ public:
|
||||
bool isAvailable() const;
|
||||
};
|
||||
|
||||
#endif //B3_SOUND_SOURCE_H
|
||||
#endif //B3_SOUND_SOURCE_H
|
||||
|
||||
@@ -12,20 +12,18 @@
|
||||
|
||||
enum B3_SOUND_SOURCE_TYPE
|
||||
{
|
||||
B3_SOUND_SOURCE_SINE_OSCILLATOR=1,
|
||||
B3_SOUND_SOURCE_SINE_OSCILLATOR = 1,
|
||||
B3_SOUND_SOURCE_SAW_OSCILLATOR,
|
||||
B3_SOUND_SOURCE_WAV_FILE,
|
||||
};
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif///B3_SOUND_C_API_H
|
||||
#endif ///B3_SOUND_C_API_H
|
||||
|
||||
@@ -3,31 +3,30 @@
|
||||
|
||||
inline void b3Swap16(unsigned char *ptr)
|
||||
{
|
||||
unsigned char val;
|
||||
unsigned char val;
|
||||
|
||||
// Swap 1st and 2nd bytes
|
||||
val = *(ptr);
|
||||
*(ptr) = *(ptr+1);
|
||||
*(ptr+1) = val;
|
||||
// Swap 1st and 2nd bytes
|
||||
val = *(ptr);
|
||||
*(ptr) = *(ptr + 1);
|
||||
*(ptr + 1) = val;
|
||||
}
|
||||
|
||||
inline void b3Swap32(unsigned char *ptr)
|
||||
{
|
||||
unsigned char val;
|
||||
unsigned char val;
|
||||
|
||||
// Swap 1st and 4th bytes
|
||||
val = *(ptr);
|
||||
*(ptr) = *(ptr+3);
|
||||
*(ptr+3) = val;
|
||||
// Swap 1st and 4th bytes
|
||||
val = *(ptr);
|
||||
*(ptr) = *(ptr + 3);
|
||||
*(ptr + 3) = val;
|
||||
|
||||
//Swap 2nd and 3rd bytes
|
||||
ptr += 1;
|
||||
val = *(ptr);
|
||||
*(ptr) = *(ptr+1);
|
||||
*(ptr+1) = val;
|
||||
//Swap 2nd and 3rd bytes
|
||||
ptr += 1;
|
||||
val = *(ptr);
|
||||
*(ptr) = *(ptr + 1);
|
||||
*(ptr + 1) = val;
|
||||
}
|
||||
|
||||
|
||||
inline void b3Swap64(unsigned char *ptr)
|
||||
{
|
||||
unsigned char val;
|
||||
@@ -56,4 +55,4 @@ inline void b3Swap64(unsigned char *ptr)
|
||||
*(ptr + 1) = val;
|
||||
}
|
||||
|
||||
#endif //B3_SWAP_UTILS_H
|
||||
#endif //B3_SWAP_UTILS_H
|
||||
|
||||
@@ -153,15 +153,14 @@ bool b3WriteWavFile::setWavFile(std::string fileName, int sampleRate, int numCha
|
||||
|
||||
char data[4] = {'d', 'a', 't', 'a'};
|
||||
int dataSize = 0;
|
||||
if (fwrite(&hdr, 1, bytesToWrite, m_data->m_file) != bytesToWrite)
|
||||
if (fwrite(&hdr, 1, bytesToWrite, m_data->m_file) != bytesToWrite)
|
||||
return false;
|
||||
if (fwrite(&data, 4, 1, m_data->m_file) != 1)
|
||||
if (fwrite(&data, 4, 1, m_data->m_file) != 1)
|
||||
return false;
|
||||
if (fwrite(&dataSize, 4, 1, m_data->m_file) != 1)
|
||||
if (fwrite(&dataSize, 4, 1, m_data->m_file) != 1)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void b3WriteWavFile::closeWavFile()
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
class b3WriteWavFile
|
||||
{
|
||||
void incrementFrame( void );
|
||||
void incrementFrame(void);
|
||||
void flush();
|
||||
|
||||
struct b3WriteWavFileInternalData* m_data;
|
||||
@@ -16,17 +16,14 @@ class b3WriteWavFile
|
||||
void flushData(int bufferSize);
|
||||
|
||||
public:
|
||||
|
||||
b3WriteWavFile();
|
||||
virtual ~b3WriteWavFile();
|
||||
|
||||
bool setWavFile(std::string fileName, int sampleRate, int numChannels, bool useDoublePrecision=true);
|
||||
bool setWavFile(std::string fileName, int sampleRate, int numChannels, bool useDoublePrecision = true);
|
||||
|
||||
void closeWavFile();
|
||||
|
||||
void tick( double* values, int numValues );
|
||||
|
||||
|
||||
void tick(double* values, int numValues);
|
||||
};
|
||||
|
||||
#endif //B3_WRITE_WAV_FILE_H
|
||||
|
||||
Reference in New Issue
Block a user