Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
2010-10-18 Chris Rogers <crogers@google.com>
Reviewed by Chris Fleizach. Add AudioResampler files https://bugs.webkit.org/show_bug.cgi?id=47623 No new tests since audio API is not yet implemented. * platform/audio/AudioResampler.cpp: Added. (WebCore::AudioResampler::AudioResampler): (WebCore::AudioResampler::configureChannels): (WebCore::AudioResampler::process): (WebCore::AudioResampler::setRate): (WebCore::AudioResampler::reset): * platform/audio/AudioResampler.h: Added. (WebCore::AudioResampler::~AudioResampler): (WebCore::AudioResampler::rate): Canonical link: https://commits.webkit.org/60580@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@70015 268f45cc-cd09-0410-ab3c-d52691b4dbfc
- Loading branch information
Chris Rogers
committed
Oct 19, 2010
1 parent
0563b77
commit b4f204ee179c008c3840d22a14158a19b7965eb3
Showing
3 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,128 @@ | ||
/* | ||
* Copyright (C) 2010, Google Inc. All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* 1. Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY | ||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY | ||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#include "config.h" | ||
|
||
#if ENABLE(WEB_AUDIO) | ||
|
||
#include "AudioResampler.h" | ||
|
||
#include "AudioBus.h" | ||
#include <algorithm> | ||
#include <math.h> | ||
|
||
using namespace std; | ||
|
||
namespace WebCore { | ||
|
||
const double AudioResampler::MaxRate = 8.0; | ||
|
||
AudioResampler::AudioResampler() | ||
: m_rate(1.0) | ||
{ | ||
m_kernels.append(adoptPtr(new AudioResamplerKernel(this))); | ||
m_sourceBus = adoptPtr(new AudioBus(1, 0, false)); | ||
} | ||
|
||
AudioResampler::AudioResampler(unsigned numberOfChannels) | ||
: m_rate(1.0) | ||
{ | ||
for (unsigned i = 0; i < numberOfChannels; ++i) | ||
m_kernels.append(adoptPtr(new AudioResamplerKernel(this))); | ||
|
||
m_sourceBus = adoptPtr(new AudioBus(numberOfChannels, 0, false)); | ||
} | ||
|
||
void AudioResampler::configureChannels(unsigned numberOfChannels) | ||
{ | ||
unsigned currentSize = m_kernels.size(); | ||
if (numberOfChannels == currentSize) | ||
return; // already setup | ||
|
||
// First deal with adding or removing kernels. | ||
if (numberOfChannels > currentSize) { | ||
for (unsigned i = currentSize; i < numberOfChannels; ++i) | ||
m_kernels.append(adoptPtr(new AudioResamplerKernel(this))); | ||
} else | ||
m_kernels.resize(numberOfChannels); | ||
|
||
// Reconfigure our source bus to the new channel size. | ||
m_sourceBus = adoptPtr(new AudioBus(numberOfChannels, 0, false)); | ||
} | ||
|
||
void AudioResampler::process(AudioSourceProvider* provider, AudioBus* destinationBus, size_t framesToProcess) | ||
{ | ||
ASSERT(provider); | ||
if (!provider) | ||
return; | ||
|
||
unsigned numberOfChannels = m_kernels.size(); | ||
|
||
// Make sure our configuration matches the bus we're rendering to. | ||
bool channelsMatch = (destinationBus && destinationBus->numberOfChannels() == numberOfChannels); | ||
ASSERT(channelsMatch); | ||
if (!channelsMatch) | ||
return; | ||
|
||
// Setup the source bus. | ||
for (unsigned i = 0; i < numberOfChannels; ++i) { | ||
// Figure out how many frames we need to get from the provider, and a pointer to the buffer. | ||
size_t framesNeeded; | ||
float* fillPointer = m_kernels[i]->getSourcePointer(framesToProcess, &framesNeeded); | ||
ASSERT(fillPointer); | ||
if (!fillPointer) | ||
return; | ||
|
||
m_sourceBus->setChannelMemory(i, fillPointer, framesNeeded); | ||
} | ||
|
||
// Ask the provider to supply the desired number of source frames. | ||
provider->provideInput(m_sourceBus.get(), m_sourceBus->length()); | ||
|
||
// Now that we have the source data, resample each channel into the destination bus. | ||
// FIXME: optimize for the common stereo case where it's faster to process both left/right channels in the same inner loop. | ||
for (unsigned i = 0; i < numberOfChannels; ++i) { | ||
float* destination = destinationBus->channel(i)->data(); | ||
m_kernels[i]->process(destination, framesToProcess); | ||
} | ||
} | ||
|
||
void AudioResampler::setRate(double rate) | ||
{ | ||
if (isnan(rate) || isinf(rate) || rate <= 0.0) | ||
return; | ||
|
||
m_rate = min(AudioResampler::MaxRate, rate); | ||
} | ||
|
||
void AudioResampler::reset() | ||
{ | ||
unsigned numberOfChannels = m_kernels.size(); | ||
for (unsigned i = 0; i < numberOfChannels; ++i) | ||
m_kernels[i]->reset(); | ||
} | ||
|
||
} // namespace WebCore | ||
|
||
#endif // ENABLE(WEB_AUDIO) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (C) 2010, Google Inc. All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* 1. Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY | ||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY | ||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#ifndef AudioResampler_h | ||
#define AudioResampler_h | ||
|
||
#include "AudioBus.h" | ||
#include "AudioResamplerKernel.h" | ||
#include "AudioSourceProvider.h" | ||
#include <wtf/OwnPtr.h> | ||
#include <wtf/Vector.h> | ||
|
||
namespace WebCore { | ||
|
||
// AudioResampler resamples the audio stream from an AudioSourceProvider. | ||
// The audio stream may be single or multi-channel. | ||
// The default constructor defaults to single-channel (mono). | ||
|
||
class AudioResampler { | ||
public: | ||
AudioResampler(); | ||
AudioResampler(unsigned numberOfChannels); | ||
~AudioResampler() { } | ||
|
||
// Given an AudioSourceProvider, process() resamples the source stream into destinationBus. | ||
void process(AudioSourceProvider*, AudioBus* destinationBus, size_t framesToProcess); | ||
|
||
// Resets the processing state. | ||
void reset(); | ||
|
||
void configureChannels(unsigned numberOfChannels); | ||
|
||
// 0 < rate <= MaxRate | ||
void setRate(double rate); | ||
double rate() const { return m_rate; } | ||
|
||
static const double MaxRate; | ||
|
||
private: | ||
double m_rate; | ||
Vector<OwnPtr<AudioResamplerKernel> > m_kernels; | ||
OwnPtr<AudioBus> m_sourceBus; | ||
}; | ||
|
||
} // namespace WebCore | ||
|
||
#endif // AudioResampler_h |