move work on TinyAudio.

This commit is contained in:
Erwin Coumans
2017-04-28 11:35:00 -07:00
parent e6759cfa4f
commit 37a16f7e6b
8 changed files with 325 additions and 102 deletions

View File

@@ -1,28 +1,76 @@
#include "TinyAudioExample.h"
#include "../CommonInterfaces/CommonExampleInterface.h"
#include "../CommonInterfaces/CommonGUIHelperInterface.h"
#include "Bullet3Common/b3AlignedObjectArray.h"
#include "Bullet3Common/b3HashMap.h"
#include "b3SoundEngine.h"
#include "b3SoundSource.h"
#include <string>
///very basic hashable string implementation, compatible with b3HashMap
struct MyHashString
{
std::string m_string;
unsigned int m_hash;
B3_FORCE_INLINE unsigned int getHash()const
{
return m_hash;
}
MyHashString(const char* 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 FNVMultiple = 16777619u;
/* Fowler / Noll / Vo (FNV) Hash */
unsigned int hash = InitialFNV;
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 */
}
m_hash = hash;
}
bool equals(const MyHashString& other) const
{
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 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;
b3SoundSource* m_soundSource;
int m_wavId;
b3HashMap<MyHashString,int> m_keyToSoundSource;
public:
TinyAudioExample(struct GUIHelperInterface* helper)
:m_guiHelper(helper),
m_soundSource(0)
:m_guiHelper(helper)
{
}
@@ -32,21 +80,18 @@ public:
virtual void initPhysics()
{
m_soundEngine.init();
int sampleRate = m_soundEngine.getSampleRate();
int numSoundSources = 8;
bool useRealTimeDac = true;
m_soundSource = new b3SoundSource();
m_soundSource->setWavFile(1,"wav/xylophone.rosewood.ff.C5B5_1.wav", sampleRate);
m_soundSource->setWavFile(0,"wav/xylophone.rosewood.ff.C5B5_1.wav", sampleRate);
m_soundSource->setOscillatorAmplitude(0,1);
m_soundSource->setOscillatorAmplitude(1,1);
m_soundEngine.addSoundSource(m_soundSource);
m_soundSource->startSound();
m_soundEngine.init(numSoundSources, useRealTimeDac);
m_wavId = m_soundEngine.loadWavFile("wav/xylophone.rosewood.ff.C5B5_1.wav");
int sampleRate = m_soundEngine.getSampleRate();
}
virtual void exitPhysics()
{
m_soundSource->stopSound();
m_soundEngine.exit();
}
@@ -69,37 +114,75 @@ public:
{
return false;
}
virtual bool keyboardCallback(int key, int state)
{
if (key=='v' || key=='b')
if (key>='a' && key<='z')
{
char keyStr[2];
keyStr[0] = (char)key;
keyStr[1] = 0;
MyHashString hs (keyStr);
if (state)
if (state)
{
int soundSourceIndex = m_soundEngine.getAvailableSoundSource();
if (soundSourceIndex>=0)
{
if (key=='b')
{
m_soundSource->setOscillatorFrequency(0, 442);
m_soundSource->setOscillatorFrequency(1, 442);
}
if (key=='v')
{
m_soundSource->setOscillatorFrequency(0, 2*442);
m_soundSource->setOscillatorFrequency(1, 2*442);
}
}
if (state==1)
{
m_soundSource->startSound();
int note = key-(97-58);
double freq = MidiPitch2Frequency(note);
b3SoundMessage msg;
msg.m_type = B3_SOUND_SOURCE_SINE_OSCILLATOR;
msg.m_frequency = freq;
msg.m_amplitude = .25;
//msg.m_type = B3_SOUND_SOURCE_WAV_FILE;
//msg.m_wavId = m_wavId;
m_soundEngine.startSound(soundSourceIndex, msg);
m_keyToSoundSource.insert(hs,soundSourceIndex);
//printf("soundSourceIndex:%d\n", soundSourceIndex);
#if 0
b3SoundSource* soundSource = this->m_soundSourcesPool[soundSourceIndex];
soundSource->setOscillatorFrequency(0, freq );
soundSource->setOscillatorFrequency(1, freq );
soundSource->startSound();
{
int* soundSourceIndexPtr = m_keyToSoundSource[hs];
if (soundSourceIndexPtr)
{
int newIndex = *soundSourceIndexPtr;
printf("just inserted: %d\n", newIndex);
}
}
#endif
}
else
} else
{
int* soundSourceIndexPtr = m_keyToSoundSource[hs];
if (soundSourceIndexPtr)
{
m_soundSource->stopSound();
int soundSourceIndex = *soundSourceIndexPtr;
//printf("releaseSound: %d\n", soundSourceIndex);
m_soundEngine.releaseSound(soundSourceIndex);
}
#if 0
if (soundSourceIndex>=0)
{
printf("releasing %d\n", soundSourceIndex);
b3SoundSource* soundSource = this->m_soundSourcesPool[soundSourceIndex];
soundSource->stopSound();
}
}
#endif
}
}