WaveShaperProcessor.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (C) 2011, Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  17. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  20. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include "config.h"
  25. #if ENABLE(WEB_AUDIO)
  26. #include "WaveShaperProcessor.h"
  27. #include "WaveShaperDSPKernel.h"
  28. namespace WebCore {
  29. WaveShaperProcessor::WaveShaperProcessor(float sampleRate, size_t numberOfChannels)
  30. : AudioDSPKernelProcessor(sampleRate, numberOfChannels)
  31. {
  32. }
  33. WaveShaperProcessor::~WaveShaperProcessor()
  34. {
  35. if (isInitialized())
  36. uninitialize();
  37. }
  38. PassOwnPtr<AudioDSPKernel> WaveShaperProcessor::createKernel()
  39. {
  40. return adoptPtr(new WaveShaperDSPKernel(this));
  41. }
  42. void WaveShaperProcessor::setCurve(Float32Array* curve)
  43. {
  44. // This synchronizes with process().
  45. MutexLocker processLocker(m_processLock);
  46. m_curve = curve;
  47. }
  48. void WaveShaperProcessor::process(const AudioBus* source, AudioBus* destination, size_t framesToProcess)
  49. {
  50. if (!isInitialized()) {
  51. destination->zero();
  52. return;
  53. }
  54. bool channelCountMatches = source->numberOfChannels() == destination->numberOfChannels() && source->numberOfChannels() == m_kernels.size();
  55. ASSERT(channelCountMatches);
  56. if (!channelCountMatches)
  57. return;
  58. // The audio thread can't block on this lock, so we call tryLock() instead.
  59. MutexTryLocker tryLocker(m_processLock);
  60. if (tryLocker.locked()) {
  61. // For each channel of our input, process using the corresponding WaveShaperDSPKernel into the output channel.
  62. for (unsigned i = 0; i < m_kernels.size(); ++i)
  63. m_kernels[i]->process(source->channel(i)->data(), destination->channel(i)->mutableData(), framesToProcess);
  64. } else {
  65. // Too bad - the tryLock() failed. We must be in the middle of a setCurve() call.
  66. destination->zero();
  67. }
  68. }
  69. } // namespace WebCore
  70. #endif // ENABLE(WEB_AUDIO)