void AudioDeviceManager::playTestSound() { { // cunningly nested to swap, unlock and delete in that order. std::unique_ptr<AudioBuffer<float>> oldSound; { const ScopedLock sl (audioCallbackLock); std::swap (oldSound, testSound); } } testSoundPosition = 0; if (currentAudioDevice != nullptr) { auto sampleRate = currentAudioDevice->getCurrentSampleRate(); auto soundLength = (int) sampleRate; double frequency = 440.0; float amplitude = 0.5f; auto phasePerSample = MathConstants<double>::twoPi / (sampleRate / frequency); std::unique_ptr<AudioBuffer<float>> newSound (new AudioBuffer<float> (1, soundLength)); for (int i = 0; i < soundLength; ++i) newSound->setSample (0, i, amplitude * (float) std::sin (i * phasePerSample)); newSound->applyGainRamp (0, 0, soundLength / 10, 0.0f, 1.0f); newSound->applyGainRamp (0, soundLength - soundLength / 4, soundLength / 4, 1.0f, 0.0f); { const ScopedLock sl (audioCallbackLock); std::swap (testSound, newSound); } } }
Sound *SoundSystem::newSound( const char *path ){ return newSound( path, 1.0, false ); }