Audio.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Audio.h"
  25. #include "Context.h"
  26. #include "CoreEvents.h"
  27. #include "Graphics.h"
  28. #include "GraphicsEvents.h"
  29. #include "Log.h"
  30. #include "ProcessUtils.h"
  31. #include "Profiler.h"
  32. #include "Sound.h"
  33. #include "SoundSource3D.h"
  34. #ifdef USE_OPENGL
  35. #include <portaudio.h>
  36. #else
  37. #define DIRECTSOUND_VERSION 0x0800
  38. #include "Thread.h"
  39. #include "Timer.h"
  40. #include <windows.h>
  41. #include <mmsystem.h>
  42. #include <dsound.h>
  43. #endif
  44. #include "DebugNew.h"
  45. static const int MIN_BUFFERLENGTH = 20;
  46. static const int MIN_MIXRATE = 11025;
  47. static const int MAX_MIXRATE = 48000;
  48. static const int AUDIO_FPS = 100;
  49. #ifdef USE_OPENGL
  50. static unsigned numInstances = 0;
  51. static int AudioCallback(const void* inputBuffer, void* outputBuffer, unsigned long framesPerBuffer,
  52. const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void* userData);
  53. #else
  54. /// DirectSound audio output stream.
  55. class AudioStream : public Thread
  56. {
  57. public:
  58. /// Construct.
  59. AudioStream(Audio* owner) :
  60. owner_(owner),
  61. dsObject_(0),
  62. dsBuffer_(0)
  63. {
  64. }
  65. /// Destruct.
  66. ~AudioStream()
  67. {
  68. Close();
  69. if (dsObject_)
  70. {
  71. dsObject_->Release();
  72. dsObject_ = 0;
  73. }
  74. }
  75. /// Create the DirectSound buffer.
  76. bool Open(unsigned windowHandle, int bufferLengthMSec, int mixRate, bool stereo)
  77. {
  78. Close();
  79. if (!dsObject_)
  80. {
  81. if (DirectSoundCreate(0, &dsObject_, 0) != DS_OK)
  82. return false;
  83. }
  84. if (dsObject_->SetCooperativeLevel((HWND)windowHandle, DSSCL_PRIORITY) != DS_OK)
  85. return false;
  86. WAVEFORMATEX waveFormat;
  87. waveFormat.wFormatTag = WAVE_FORMAT_PCM;
  88. waveFormat.nSamplesPerSec = mixRate;
  89. waveFormat.wBitsPerSample = 16;
  90. if (stereo)
  91. waveFormat.nChannels = 2;
  92. else
  93. waveFormat.nChannels = 1;
  94. sampleSize_ = waveFormat.nChannels * waveFormat.wBitsPerSample / 8;
  95. unsigned numSamples = (bufferLengthMSec * mixRate) / 1000;
  96. waveFormat.nAvgBytesPerSec = mixRate * sampleSize_;
  97. waveFormat.nBlockAlign = sampleSize_;
  98. waveFormat.cbSize = 0;
  99. DSBUFFERDESC bufferDesc;
  100. memset(&bufferDesc, 0, sizeof(bufferDesc));
  101. bufferDesc.dwSize = sizeof(bufferDesc);
  102. bufferDesc.dwFlags = DSBCAPS_STICKYFOCUS;
  103. bufferDesc.dwBufferBytes = numSamples * sampleSize_;
  104. bufferDesc.lpwfxFormat = &waveFormat;
  105. bufferSize_ = bufferDesc.dwBufferBytes;
  106. return dsObject_->CreateSoundBuffer(&bufferDesc, &dsBuffer_, 0) == DS_OK;
  107. }
  108. /// Destroy the DirectSound buffer.
  109. void Close()
  110. {
  111. StopPlayback();
  112. if (dsBuffer_)
  113. {
  114. dsBuffer_->Release();
  115. dsBuffer_ = 0;
  116. }
  117. }
  118. /// Start playback.
  119. bool StartPlayback()
  120. {
  121. if (IsStarted())
  122. return true;
  123. if (!dsBuffer_)
  124. return false;
  125. // Clear the buffer before starting playback
  126. DWORD bytes1, bytes2;
  127. void *ptr1, *ptr2;
  128. if (dsBuffer_->Lock(0, bufferSize_, &ptr1, &bytes1, &ptr2, &bytes2, 0) == DS_OK)
  129. {
  130. if (bytes1)
  131. memset(ptr1, 0, bytes1);
  132. if (bytes2)
  133. memset(ptr2, 0, bytes2);
  134. dsBuffer_->Unlock(ptr1, bytes1, ptr2, bytes2);
  135. }
  136. if (Start())
  137. {
  138. SetPriority(THREAD_PRIORITY_ABOVE_NORMAL);
  139. return true;
  140. }
  141. else
  142. return false;
  143. }
  144. /// Stop playback.
  145. void StopPlayback()
  146. {
  147. if (dsBuffer_ && IsStarted())
  148. Stop();
  149. }
  150. /// Mixing thread function.
  151. void ThreadFunction()
  152. {
  153. DWORD playCursor = 0;
  154. DWORD writeCursor = 0;
  155. while (shouldRun_)
  156. {
  157. Timer audioUpdateTimer;
  158. // Restore buffer / restart playback if necessary
  159. DWORD status;
  160. dsBuffer_->GetStatus(&status);
  161. if (status == DSBSTATUS_BUFFERLOST)
  162. {
  163. dsBuffer_->Restore();
  164. dsBuffer_->GetStatus(&status);
  165. }
  166. if (!(status & DSBSTATUS_PLAYING))
  167. {
  168. dsBuffer_->Play(0, 0, DSBPLAY_LOOPING);
  169. writeCursor = 0;
  170. }
  171. // Get current buffer position
  172. dsBuffer_->GetCurrentPosition(&playCursor, 0);
  173. playCursor %= bufferSize_;
  174. playCursor &= -((int)sampleSize_);
  175. if (playCursor != writeCursor)
  176. {
  177. int writeBytes = playCursor - writeCursor;
  178. if (writeBytes < 0)
  179. writeBytes += bufferSize_;
  180. // Try to lock buffer
  181. DWORD bytes1, bytes2;
  182. void *ptr1, *ptr2;
  183. if (dsBuffer_->Lock(writeCursor, writeBytes, &ptr1, &bytes1, &ptr2, &bytes2, 0) == DS_OK)
  184. {
  185. // Mix sound to locked positions
  186. {
  187. MutexLock Lock(owner_->GetMutex());
  188. if (bytes1)
  189. owner_->MixOutput(ptr1, bytes1 / sampleSize_);
  190. if (bytes2)
  191. owner_->MixOutput(ptr2, bytes2 / sampleSize_);
  192. }
  193. // Unlock buffer and update write cursor
  194. dsBuffer_->Unlock(ptr1, bytes1, ptr2, bytes2);
  195. writeCursor += writeBytes;
  196. if (writeCursor >= bufferSize_)
  197. writeCursor -= bufferSize_;
  198. }
  199. }
  200. // Sleep the remaining time of the audio update period
  201. int audioSleepTime = Max(1000 / AUDIO_FPS - (int)audioUpdateTimer.GetMSec(false), 0);
  202. Sleep(audioSleepTime);
  203. }
  204. dsBuffer_->Stop();
  205. }
  206. private:
  207. /// Audio subsystem.
  208. Audio* owner_;
  209. /// DirectSound interface.
  210. IDirectSound* dsObject_;
  211. /// DirectSound buffer.
  212. IDirectSoundBuffer* dsBuffer_;
  213. /// Sound buffer size in bytes.
  214. unsigned bufferSize_;
  215. /// Sound buffer sample size.
  216. unsigned sampleSize_;
  217. /// Playing flag.
  218. bool playing_;
  219. };
  220. #endif
  221. OBJECTTYPESTATIC(Audio);
  222. Audio::Audio(Context* context) :
  223. Object(context),
  224. stream_(0),
  225. sampleSize_(0),
  226. playing_(false),
  227. listenerPosition_(Vector3::ZERO),
  228. listenerRotation_(Quaternion::IDENTITY)
  229. {
  230. SubscribeToEvent(E_RENDERUPDATE, HANDLER(Audio, HandleRenderUpdate));
  231. for (unsigned i = 0; i < MAX_SOUND_TYPES; ++i)
  232. masterGain_[i] = 1.0f;
  233. // Initialize PortAudio under static mutex in case this is the first instance
  234. #ifdef USE_OPENGL
  235. {
  236. MutexLock lock(GetStaticMutex());
  237. if (!numInstances)
  238. {
  239. if (Pa_Initialize() != paNoError)
  240. LOGERROR("Could not initialize PortAudio");
  241. }
  242. ++numInstances;
  243. }
  244. #endif
  245. }
  246. Audio::~Audio()
  247. {
  248. Release();
  249. // Uninitialize PortAudio under static mutex in case this is the last instance
  250. #ifdef USE_OPENGL
  251. {
  252. MutexLock lock(GetStaticMutex());
  253. --numInstances;
  254. if (!numInstances)
  255. Pa_Terminate();
  256. }
  257. #else
  258. delete (AudioStream*)stream_;
  259. stream_ = 0;
  260. #endif
  261. }
  262. bool Audio::SetMode(int bufferLengthMSec, int mixRate, bool stereo, bool interpolate)
  263. {
  264. Release();
  265. bufferLengthMSec = Max(bufferLengthMSec, MIN_BUFFERLENGTH);
  266. mixRate = Clamp(mixRate, MIN_MIXRATE, MAX_MIXRATE);
  267. // Guarantee a fragment size that is low enough so that Vorbis decoding buffers do not wrap
  268. fragmentSize_ = NextPowerOfTwo(mixRate >> 6);
  269. #ifdef USE_OPENGL
  270. PaStreamParameters outputParams;
  271. outputParams.device = Pa_GetDefaultOutputDevice();
  272. outputParams.channelCount = stereo ? 2 : 1;
  273. outputParams.sampleFormat = paInt16;
  274. outputParams.suggestedLatency = bufferLengthMSec / 1000.0;
  275. outputParams.hostApiSpecificStreamInfo = 0;
  276. if (Pa_OpenStream(&stream_, 0, &outputParams, mixRate, fragmentSize_, 0, AudioCallback, this) != paNoError)
  277. {
  278. LOGERROR("Failed to open audio stream");
  279. return false;
  280. }
  281. #else
  282. if (!stream_)
  283. stream_ = new AudioStream(this);
  284. unsigned windowHandle = 0;
  285. Graphics* graphics = GetSubsystem<Graphics>();
  286. if (graphics)
  287. windowHandle = graphics->GetWindowHandle();
  288. if (!((AudioStream*)stream_)->Open(windowHandle, bufferLengthMSec, mixRate, stereo))
  289. {
  290. LOGERROR("Failed to open audio stream");
  291. return false;
  292. }
  293. #endif
  294. clipBuffer_ = new int[stereo ? fragmentSize_ << 1 : fragmentSize_];
  295. sampleSize_ = sizeof(short);
  296. if (stereo)
  297. sampleSize_ <<= 1;
  298. mixRate_ = mixRate;
  299. stereo_ = stereo;
  300. interpolate_ = interpolate;
  301. LOGINFO("Set audio mode " + String(mixRate_) + " Hz " + (stereo_ ? "stereo" : "mono") + " " +
  302. (interpolate_ ? "interpolated" : ""));
  303. return Play();
  304. }
  305. void Audio::Update(float timeStep)
  306. {
  307. PROFILE(UpdateAudio);
  308. MutexLock Lock(audioMutex_);
  309. // Update in reverse order, because sound sources might remove themselves
  310. for (unsigned i = soundSources_.Size() - 1; i < soundSources_.Size(); --i)
  311. soundSources_[i]->Update(timeStep);
  312. }
  313. bool Audio::Play()
  314. {
  315. if (playing_)
  316. return true;
  317. if (!stream_)
  318. {
  319. LOGERROR("No audio mode set, can not start playback");
  320. return false;
  321. }
  322. #ifdef USE_OPENGL
  323. if (Pa_StartStream(stream_) != paNoError)
  324. {
  325. LOGERROR("Failed to start playback");
  326. return false;
  327. }
  328. #else
  329. if (!((AudioStream*)stream_)->StartPlayback())
  330. {
  331. LOGERROR("Failed to start playback");
  332. return false;
  333. }
  334. #endif
  335. playing_ = true;
  336. return true;
  337. }
  338. void Audio::Stop()
  339. {
  340. if (!stream_ || !playing_)
  341. return;
  342. #ifdef USE_OPENGL
  343. Pa_StopStream(stream_);
  344. #else
  345. ((AudioStream*)stream_)->StopPlayback();
  346. #endif
  347. playing_ = false;
  348. }
  349. void Audio::SetMasterGain(SoundType type, float gain)
  350. {
  351. if (type >= MAX_SOUND_TYPES)
  352. return;
  353. masterGain_[type] = Clamp(gain, 0.0f, 1.0f);
  354. }
  355. void Audio::SetListenerPosition(const Vector3& position)
  356. {
  357. listenerPosition_ = position;
  358. }
  359. void Audio::SetListenerRotation(const Quaternion& rotation)
  360. {
  361. listenerRotation_ = rotation;
  362. }
  363. void Audio::SetListenerTransform(const Vector3& position, const Quaternion& rotation)
  364. {
  365. listenerPosition_ = position;
  366. listenerRotation_ = rotation;
  367. }
  368. void Audio::StopSound(Sound* soundClip)
  369. {
  370. for (PODVector<SoundSource*>::Iterator i = soundSources_.Begin(); i != soundSources_.End(); ++i)
  371. {
  372. if ((*i)->GetSound() == soundClip)
  373. (*i)->Stop();
  374. }
  375. }
  376. float Audio::GetMasterGain(SoundType type) const
  377. {
  378. if (type >= MAX_SOUND_TYPES)
  379. return 0.0f;
  380. return masterGain_[type];
  381. }
  382. void Audio::AddSoundSource(SoundSource* channel)
  383. {
  384. MutexLock Lock(audioMutex_);
  385. soundSources_.Push(channel);
  386. }
  387. void Audio::RemoveSoundSource(SoundSource* channel)
  388. {
  389. MutexLock Lock(audioMutex_);
  390. PODVector<SoundSource*>::Iterator i = soundSources_.Find(channel);
  391. if (i != soundSources_.End())
  392. soundSources_.Erase(i);
  393. }
  394. #ifdef USE_OPENGL
  395. int AudioCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo*
  396. timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
  397. {
  398. Audio* audio = static_cast<Audio*>(userData);
  399. {
  400. MutexLock Lock(audio->GetMutex());
  401. audio->MixOutput(outputBuffer, framesPerBuffer);
  402. }
  403. return 0;
  404. }
  405. #endif
  406. void Audio::MixOutput(void *dest, unsigned samples)
  407. {
  408. while (samples)
  409. {
  410. // If sample count exceeds the fragment (clip buffer) size, split the work
  411. unsigned workSamples = Min((int)samples, (int)fragmentSize_);
  412. unsigned clipSamples = workSamples;
  413. if (stereo_)
  414. clipSamples <<= 1;
  415. // Clear clip buffer
  416. memset(clipBuffer_.Get(), 0, clipSamples * sizeof(int));
  417. int* clipPtr = clipBuffer_.Get();
  418. // Mix samples to clip buffer
  419. for (PODVector<SoundSource*>::Iterator i = soundSources_.Begin(); i != soundSources_.End(); ++i)
  420. (*i)->Mix(clipPtr, workSamples, mixRate_, stereo_, interpolate_);
  421. // Copy output from clip buffer to destination
  422. clipPtr = clipBuffer_.Get();
  423. short* destPtr = (short*)dest;
  424. while (clipSamples--)
  425. *destPtr++ = Clamp(*clipPtr++, -32768, 32767);
  426. samples -= workSamples;
  427. ((unsigned char*&)destPtr) += sampleSize_ * workSamples;
  428. }
  429. }
  430. void Audio::HandleRenderUpdate(StringHash eventType, VariantMap& eventData)
  431. {
  432. using namespace RenderUpdate;
  433. Update(eventData[P_TIMESTEP].GetFloat());
  434. }
  435. void Audio::Release()
  436. {
  437. Stop();
  438. if (stream_)
  439. {
  440. #ifdef USE_OPENGL
  441. Pa_CloseStream(stream_);
  442. stream_ = 0;
  443. #else
  444. ((AudioStream*)stream_)->Close();
  445. #endif
  446. clipBuffer_.Reset();
  447. }
  448. }
  449. void RegisterAudioLibrary(Context* context)
  450. {
  451. Sound::RegisterObject(context);
  452. SoundSource::RegisterObject(context);
  453. SoundSource3D::RegisterObject(context);
  454. }