TinyAudioExample.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include "TinyAudioExample.h"
  2. #include "../CommonInterfaces/CommonExampleInterface.h"
  3. #include "../CommonInterfaces/CommonGUIHelperInterface.h"
  4. #include "Bullet3Common/b3AlignedObjectArray.h"
  5. #include "Bullet3Common/b3HashMap.h"
  6. #include "b3SoundEngine.h"
  7. #include "b3SoundSource.h"
  8. #include <string>
  9. ///very basic hashable string implementation, compatible with b3HashMap
  10. struct MyHashString
  11. {
  12. std::string m_string;
  13. unsigned int m_hash;
  14. B3_FORCE_INLINE unsigned int getHash() const
  15. {
  16. return m_hash;
  17. }
  18. MyHashString(const char* name)
  19. : m_string(name)
  20. {
  21. /* magic numbers from http://www.isthe.com/chongo/tech/comp/fnv/ */
  22. static const unsigned int InitialFNV = 2166136261u;
  23. static const unsigned int FNVMultiple = 16777619u;
  24. /* Fowler / Noll / Vo (FNV) Hash */
  25. unsigned int hash = InitialFNV;
  26. for (int i = 0; m_string[i]; i++)
  27. {
  28. hash = hash ^ (m_string[i]); /* xor the low 8 bits */
  29. hash = hash * FNVMultiple; /* multiply by the magic number */
  30. }
  31. m_hash = hash;
  32. }
  33. bool equals(const MyHashString& other) const
  34. {
  35. return (m_string == other.m_string);
  36. }
  37. };
  38. double base_frequency = 440.0;
  39. double base_pitch = 69.0;
  40. double MidiPitch2Frequency(double incoming_note)
  41. {
  42. return base_frequency * pow(2.0, (incoming_note - base_pitch) / 12.0);
  43. }
  44. double FrequencytoMidiPitch(double incoming_frequency)
  45. {
  46. return base_pitch + (12.0 * log(incoming_frequency / base_frequency) / log(2));
  47. }
  48. class TinyAudioExample : public CommonExampleInterface
  49. {
  50. GUIHelperInterface* m_guiHelper;
  51. b3SoundEngine m_soundEngine;
  52. int m_wavId;
  53. b3HashMap<MyHashString, int> m_keyToSoundSource;
  54. public:
  55. TinyAudioExample(struct GUIHelperInterface* helper)
  56. : m_guiHelper(helper)
  57. {
  58. }
  59. virtual ~TinyAudioExample()
  60. {
  61. }
  62. virtual void initPhysics()
  63. {
  64. int numSoundSources = 32;
  65. bool useRealTimeDac = true;
  66. m_soundEngine.init(numSoundSources, useRealTimeDac);
  67. m_wavId = m_soundEngine.loadWavFile("wav/xylophone.rosewood.ff.C5B5_1.wav");
  68. int sampleRate = m_soundEngine.getSampleRate();
  69. }
  70. virtual void exitPhysics()
  71. {
  72. m_soundEngine.exit();
  73. }
  74. virtual void renderScene()
  75. {
  76. }
  77. virtual void stepSimulation(float deltaTime)
  78. {
  79. }
  80. virtual void physicsDebugDraw(int debugFlags)
  81. {
  82. }
  83. virtual bool mouseMoveCallback(float x, float y)
  84. {
  85. return false;
  86. }
  87. virtual bool mouseButtonCallback(int button, int state, float x, float y)
  88. {
  89. return false;
  90. }
  91. virtual bool keyboardCallback(int key, int state)
  92. {
  93. if (key >= 'a' && key <= 'z')
  94. {
  95. char keyStr[2];
  96. keyStr[0] = (char)key;
  97. keyStr[1] = 0;
  98. MyHashString hs(keyStr);
  99. if (state)
  100. {
  101. int soundSourceIndex = m_soundEngine.getAvailableSoundSource();
  102. if (soundSourceIndex >= 0)
  103. {
  104. int note = key - (97 - 58);
  105. double freq = MidiPitch2Frequency(note);
  106. b3SoundMessage msg;
  107. msg.m_type = B3_SOUND_SOURCE_SINE_OSCILLATOR;
  108. msg.m_frequency = freq;
  109. msg.m_amplitude = 1;
  110. msg.m_type = B3_SOUND_SOURCE_WAV_FILE;
  111. msg.m_wavId = m_wavId;
  112. msg.m_attackRate = 1;
  113. msg.m_sustainLevel = 1;
  114. msg.m_releaseRate = 0.001;
  115. m_soundEngine.startSound(soundSourceIndex, msg);
  116. m_keyToSoundSource.insert(hs, soundSourceIndex);
  117. //printf("soundSourceIndex:%d\n", soundSourceIndex);
  118. #if 0
  119. b3SoundSource* soundSource = this->m_soundSourcesPool[soundSourceIndex];
  120. soundSource->setOscillatorFrequency(0, freq );
  121. soundSource->setOscillatorFrequency(1, freq );
  122. soundSource->startSound();
  123. {
  124. int* soundSourceIndexPtr = m_keyToSoundSource[hs];
  125. if (soundSourceIndexPtr)
  126. {
  127. int newIndex = *soundSourceIndexPtr;
  128. printf("just inserted: %d\n", newIndex);
  129. }
  130. }
  131. #endif
  132. }
  133. }
  134. else
  135. {
  136. int* soundSourceIndexPtr = m_keyToSoundSource[hs];
  137. if (soundSourceIndexPtr)
  138. {
  139. int soundSourceIndex = *soundSourceIndexPtr;
  140. //printf("releaseSound: %d\n", soundSourceIndex);
  141. m_soundEngine.releaseSound(soundSourceIndex);
  142. }
  143. #if 0
  144. if (soundSourceIndex>=0)
  145. {
  146. printf("releasing %d\n", soundSourceIndex);
  147. b3SoundSource* soundSource = this->m_soundSourcesPool[soundSourceIndex];
  148. soundSource->stopSound();
  149. }
  150. }
  151. #endif
  152. }
  153. }
  154. return false;
  155. }
  156. void resetCamera()
  157. {
  158. float dist = 4;
  159. float pitch = 52;
  160. float yaw = 35;
  161. float targetPos[3] = {0, 0, 0};
  162. m_guiHelper->resetCamera(dist, pitch, yaw, targetPos[0], targetPos[1], targetPos[2]);
  163. }
  164. };
  165. CommonExampleInterface* TinyAudioExampleCreateFunc(CommonExampleOptions& options)
  166. {
  167. return new TinyAudioExample(options.m_guiHelper);
  168. }
  169. B3_STANDALONE_EXAMPLE(TinyAudioExampleCreateFunc)