Home | History | Annotate | Download | only in webaudio
      1 /*
      2  * Copyright (C) 2010 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  *
      8  * 1.  Redistributions of source code must retain the above copyright
      9  *     notice, this list of conditions and the following disclaimer.
     10  * 2.  Redistributions in binary form must reproduce the above copyright
     11  *     notice, this list of conditions and the following disclaimer in the
     12  *     documentation and/or other materials provided with the distribution.
     13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     14  *     its contributors may be used to endorse or promote products derived
     15  *     from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include "config.h"
     30 
     31 #if ENABLE(WEB_AUDIO)
     32 
     33 #include "modules/webaudio/AudioBuffer.h"
     34 
     35 #include "bindings/v8/ExceptionMessages.h"
     36 #include "bindings/v8/ExceptionState.h"
     37 #include "core/dom/ExceptionCode.h"
     38 #include "platform/audio/AudioBus.h"
     39 #include "platform/audio/AudioFileReader.h"
     40 #include "modules/webaudio/AudioContext.h"
     41 
     42 namespace WebCore {
     43 
     44 float AudioBuffer::minAllowedSampleRate()
     45 {
     46     // crbug.com/344375
     47     return 3000;
     48 }
     49 
     50 float AudioBuffer::maxAllowedSampleRate()
     51 {
     52     // Windows can support up to this rate.
     53     return 192000;
     54 }
     55 
     56 PassRefPtrWillBeRawPtr<AudioBuffer> AudioBuffer::create(unsigned numberOfChannels, size_t numberOfFrames, float sampleRate)
     57 {
     58     if (sampleRate < minAllowedSampleRate() || sampleRate > maxAllowedSampleRate() || numberOfChannels > AudioContext::maxNumberOfChannels() || !numberOfChannels || !numberOfFrames)
     59         return nullptr;
     60 
     61     RefPtrWillBeRawPtr<AudioBuffer> buffer = adoptRefWillBeNoop(new AudioBuffer(numberOfChannels, numberOfFrames, sampleRate));
     62 
     63     if (!buffer->createdSuccessfully(numberOfChannels))
     64         return nullptr;
     65     return buffer;
     66 }
     67 
     68 PassRefPtrWillBeRawPtr<AudioBuffer> AudioBuffer::create(unsigned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState& exceptionState)
     69 {
     70     if (!numberOfChannels || numberOfChannels > AudioContext::maxNumberOfChannels()) {
     71         exceptionState.throwDOMException(
     72             NotSupportedError,
     73             ExceptionMessages::indexOutsideRange(
     74                 "number of channels",
     75                 numberOfChannels,
     76                 1u,
     77                 ExceptionMessages::InclusiveBound,
     78                 AudioContext::maxNumberOfChannels(),
     79                 ExceptionMessages::InclusiveBound));
     80         return nullptr;
     81     }
     82 
     83     if (sampleRate < AudioBuffer::minAllowedSampleRate() || sampleRate > AudioBuffer::maxAllowedSampleRate()) {
     84         exceptionState.throwDOMException(
     85             NotSupportedError,
     86             ExceptionMessages::indexOutsideRange(
     87                 "sample rate",
     88                 sampleRate,
     89                 AudioBuffer::minAllowedSampleRate(),
     90                 ExceptionMessages::InclusiveBound,
     91                 AudioBuffer::maxAllowedSampleRate(),
     92                 ExceptionMessages::InclusiveBound));
     93         return nullptr;
     94     }
     95 
     96     if (!numberOfFrames) {
     97         exceptionState.throwDOMException(
     98             NotSupportedError,
     99             ExceptionMessages::indexExceedsMinimumBound(
    100                 "number of frames",
    101                 numberOfFrames,
    102                 static_cast<size_t>(0)));
    103         return nullptr;
    104     }
    105 
    106     RefPtrWillBeRawPtr<AudioBuffer> audioBuffer = create(numberOfChannels, numberOfFrames, sampleRate);
    107 
    108     if (!audioBuffer.get()) {
    109         exceptionState.throwDOMException(
    110             NotSupportedError,
    111             "createBuffer("
    112             + String::number(numberOfChannels) + ", "
    113             + String::number(numberOfFrames) + ", "
    114             + String::number(sampleRate)
    115             + ") failed.");
    116     }
    117 
    118     return audioBuffer;
    119 }
    120 
    121 PassRefPtrWillBeRawPtr<AudioBuffer> AudioBuffer::createFromAudioFileData(const void* data, size_t dataSize, bool mixToMono, float sampleRate)
    122 {
    123     RefPtr<AudioBus> bus = createBusFromInMemoryAudioFile(data, dataSize, mixToMono, sampleRate);
    124     if (bus.get()) {
    125         RefPtrWillBeRawPtr<AudioBuffer> buffer = adoptRefWillBeNoop(new AudioBuffer(bus.get()));
    126         if (buffer->createdSuccessfully(bus->numberOfChannels()))
    127             return buffer;
    128     }
    129 
    130     return nullptr;
    131 }
    132 
    133 PassRefPtrWillBeRawPtr<AudioBuffer> AudioBuffer::createFromAudioBus(AudioBus* bus)
    134 {
    135     if (!bus)
    136         return nullptr;
    137     RefPtrWillBeRawPtr<AudioBuffer> buffer = adoptRefWillBeNoop(new AudioBuffer(bus));
    138     if (buffer->createdSuccessfully(bus->numberOfChannels()))
    139         return buffer;
    140     return nullptr;
    141 }
    142 
    143 bool AudioBuffer::createdSuccessfully(unsigned desiredNumberOfChannels) const
    144 {
    145     return numberOfChannels() == desiredNumberOfChannels;
    146 }
    147 
    148 AudioBuffer::AudioBuffer(unsigned numberOfChannels, size_t numberOfFrames, float sampleRate)
    149     : m_sampleRate(sampleRate)
    150     , m_length(numberOfFrames)
    151 {
    152     ScriptWrappable::init(this);
    153     m_channels.reserveCapacity(numberOfChannels);
    154 
    155     for (unsigned i = 0; i < numberOfChannels; ++i) {
    156         RefPtr<Float32Array> channelDataArray = Float32Array::create(m_length);
    157         // If the channel data array could not be created, just return. The caller will need to
    158         // check that the desired number of channels were created.
    159         if (!channelDataArray) {
    160             return;
    161         }
    162 
    163         channelDataArray->setNeuterable(false);
    164         m_channels.append(channelDataArray);
    165     }
    166 }
    167 
    168 AudioBuffer::AudioBuffer(AudioBus* bus)
    169     : m_sampleRate(bus->sampleRate())
    170     , m_length(bus->length())
    171 {
    172     ScriptWrappable::init(this);
    173     // Copy audio data from the bus to the Float32Arrays we manage.
    174     unsigned numberOfChannels = bus->numberOfChannels();
    175     m_channels.reserveCapacity(numberOfChannels);
    176     for (unsigned i = 0; i < numberOfChannels; ++i) {
    177         RefPtr<Float32Array> channelDataArray = Float32Array::create(m_length);
    178         // If the channel data array could not be created, just return. The caller will need to
    179         // check that the desired number of channels were created.
    180         if (!channelDataArray)
    181             return;
    182 
    183         channelDataArray->setNeuterable(false);
    184         channelDataArray->setRange(bus->channel(i)->data(), m_length, 0);
    185         m_channels.append(channelDataArray);
    186     }
    187 }
    188 
    189 PassRefPtr<Float32Array> AudioBuffer::getChannelData(unsigned channelIndex, ExceptionState& exceptionState)
    190 {
    191     if (channelIndex >= m_channels.size()) {
    192         exceptionState.throwDOMException(IndexSizeError, "channel index (" + String::number(channelIndex) + ") exceeds number of channels (" + String::number(m_channels.size()) + ")");
    193         return nullptr;
    194     }
    195 
    196     Float32Array* channelData = m_channels[channelIndex].get();
    197     return Float32Array::create(channelData->buffer(), channelData->byteOffset(), channelData->length());
    198 }
    199 
    200 Float32Array* AudioBuffer::getChannelData(unsigned channelIndex)
    201 {
    202     if (channelIndex >= m_channels.size())
    203         return 0;
    204 
    205     return m_channels[channelIndex].get();
    206 }
    207 
    208 void AudioBuffer::zero()
    209 {
    210     for (unsigned i = 0; i < m_channels.size(); ++i) {
    211         if (getChannelData(i))
    212             getChannelData(i)->zeroRange(0, length());
    213     }
    214 }
    215 
    216 } // namespace WebCore
    217 
    218 #endif // ENABLE(WEB_AUDIO)
    219