plumb URDF/SDF audio_source into PhysicsServerCommandProcessor, allow to play sounds on collision !

See also https://youtu.be/eppOjTfx5Jg for a first test, and this modified URDF how to add sounds:
https://github.com/bulletphysics/bullet3/blob/master/data/plane_with_collision_audio.urdf
Add the --audio flag to enable sound in pybullet/Bullet-C-API
This commit is contained in:
Erwin Coumans
2017-05-01 11:14:09 -07:00
parent 3a330c4280
commit af6bf8ddc8
22 changed files with 389 additions and 108 deletions

View File

@@ -23,6 +23,7 @@ b3ADSR::b3ADSR()
m_releaseRate = 0.0005;
m_sustainLevel = 0.5;
m_state = ADSR_IDLE;
m_autoKeyOff = false;
}
b3ADSR::~b3ADSR()
@@ -51,6 +52,10 @@ double b3ADSR::tick()
{
m_value = m_sustainLevel;
m_state = ADSR_SUSTAIN;
if (m_autoKeyOff)
{
keyOff();
}
}
}
else
@@ -60,6 +65,11 @@ double b3ADSR::tick()
{
m_value = m_sustainLevel;
m_state = ADSR_SUSTAIN;
if (m_autoKeyOff)
{
keyOff();
}
}
}
break;
@@ -81,8 +91,9 @@ bool b3ADSR::isIdle() const
return m_state == ADSR_IDLE;
}
void b3ADSR::keyOn()
void b3ADSR::keyOn(bool autoKeyOff)
{
m_autoKeyOff = autoKeyOff;
if (m_target <= 0.0)
m_target = 1.0;
if (m_attackRate==1)
@@ -94,6 +105,7 @@ void b3ADSR::keyOn()
void b3ADSR::keyOff()
{
m_autoKeyOff = false;
m_target = 0.0;
m_state = ADSR_RELEASE;

View File

@@ -11,7 +11,7 @@ class b3ADSR
double m_releaseRate;
double m_releaseTime;
double m_sustainLevel;
bool m_autoKeyOff;
public:
b3ADSR();
@@ -19,7 +19,7 @@ public:
double tick();
bool isIdle() const;
void keyOn();
void keyOn(bool autoKeyOff);
void keyOff();
void setValues(double attack,double decay,double sustain,double release)

View File

@@ -7,6 +7,8 @@
#include "Bullet3Common/b3AlignedObjectArray.h"
#include "b3ReadWavFile.h"
#include "../Utils/b3ResourcePath.h"
#include "../Utils/b3HashString.h"
#include "Bullet3Common/b3HashMap.h"
// The default real-time audio input and output buffer size. If
@@ -24,6 +26,8 @@ struct b3SoundEngineInternalData
b3AlignedObjectArray<b3SoundSource*> m_soundSources;
b3HashMap<b3HashInt, b3ReadWavFile*> m_wavFiles;
b3HashMap<b3HashString, int> m_name2wav;
int m_wavFileUidGenerator;
b3SoundEngineInternalData()
@@ -84,6 +88,19 @@ void b3SoundEngine::exit()
delete m_data->m_soundSources[i];
}
m_data->m_soundSources.clear();
for (int i=0;i<m_data->m_wavFiles.size();i++)
{
b3ReadWavFile** wavPtr = m_data->m_wavFiles.getAtIndex(i);
if (wavPtr && *wavPtr)
{
b3ReadWavFile* wav = *wavPtr;
delete wav;
}
}
m_data->m_wavFiles.clear();
m_data->m_name2wav.clear();
}
int b3SoundEngine::getAvailableSoundSource()
@@ -113,7 +130,7 @@ void b3SoundEngine::startSound(int soundSourceIndex, b3SoundMessage msg)
soundSource->setOscillatorFrequency(0, msg.m_frequency);
soundSource->setOscillatorFrequency(1, msg.m_frequency);
soundSource->startSound();
soundSource->startSound(msg.m_autoKeyOff);
break;
}
case B3_SOUND_SOURCE_WAV_FILE:
@@ -124,7 +141,7 @@ void b3SoundEngine::startSound(int soundSourceIndex, b3SoundMessage msg)
b3ReadWavFile* wavFile = *wavFilePtr;
soundSource->setWavFile(0,wavFile,getSampleRate());
soundSource->setWavFile(1,wavFile,getSampleRate());
soundSource->startSound();
soundSource->startSound(msg.m_autoKeyOff);
}
break;
}
@@ -142,6 +159,11 @@ void b3SoundEngine::releaseSound(int soundSourceIndex)
int b3SoundEngine::loadWavFile(const char* fileName)
{
int* wavUidPtr = m_data->m_name2wav[fileName];
if (wavUidPtr)
{
return *wavUidPtr;
}
char resourcePath[1024];
if (b3ResourcePath::findResourcePath(fileName, resourcePath, 1024))
@@ -153,6 +175,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);
return wavUID;
}
return 0;

View File

@@ -16,6 +16,7 @@ struct b3SoundMessage
double m_decayRate;
double m_sustainLevel;
double m_releaseRate;
bool m_autoKeyOff;
b3SoundMessage()
:m_type(B3_SOUND_SOURCE_SINE_OSCILLATOR),
@@ -25,7 +26,8 @@ struct b3SoundMessage
m_attackRate(0.001),
m_decayRate(0.00001),
m_sustainLevel(0.5),
m_releaseRate(0.0005)
m_releaseRate(0.0005),
m_autoKeyOff(false)
{
}
};

View File

@@ -183,7 +183,7 @@ bool b3SoundSource::isAvailable() const
return m_data->m_envelope.isIdle();
}
void b3SoundSource::startSound()
void b3SoundSource::startSound(bool autoKeyOff)
{
if (m_data->m_envelope.isIdle())
{
@@ -209,7 +209,7 @@ void b3SoundSource::startSound()
}
}
}
m_data->m_envelope.keyOn();
m_data->m_envelope.keyOn(autoKeyOff);
}
void b3SoundSource::stopSound()

View File

@@ -24,7 +24,7 @@ public:
bool setWavFile(int oscillatorIndex, class b3ReadWavFile* wavFilePtr, int sampleRate);
void startSound();
void startSound(bool autoKeyOff);
void stopSound();
bool isAvailable() const;