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/ExceptionState.h"
     36 #include "core/dom/ExceptionCode.h"
     37 #include "platform/audio/AudioBus.h"
     38 #include "platform/audio/AudioFileReader.h"
     39 #include "modules/webaudio/AudioContext.h"
     40 
     41 namespace WebCore {
     42 
     43 float AudioBuffer::minAllowedSampleRate()
     44 {
     45     return 22050;
     46 }
     47 
     48 float AudioBuffer::maxAllowedSampleRate()
     49 {
     50     return 96000;
     51 }
     52 
     53 PassRefPtr<AudioBuffer> AudioBuffer::create(unsigned numberOfChannels, size_t numberOfFrames, float sampleRate)
     54 {
     55     if (sampleRate < minAllowedSampleRate() || sampleRate > maxAllowedSampleRate() || numberOfChannels > AudioContext::maxNumberOfChannels() || !numberOfFrames)
     56         return 0;
     57 
     58     RefPtr<AudioBuffer> buffer = adoptRef(new AudioBuffer(numberOfChannels, numberOfFrames, sampleRate));
     59 
     60     if (!buffer->createdSuccessfully(numberOfChannels))
     61         return 0;
     62     return buffer;
     63 }
     64 
     65 PassRefPtr<AudioBuffer> AudioBuffer::createFromAudioFileData(const void* data, size_t dataSize, bool mixToMono, float sampleRate)
     66 {
     67     RefPtr<AudioBus> bus = createBusFromInMemoryAudioFile(data, dataSize, mixToMono, sampleRate);
     68     if (bus.get()) {
     69         RefPtr<AudioBuffer> buffer = adoptRef(new AudioBuffer(bus.get()));
     70         if (buffer->createdSuccessfully(bus->numberOfChannels()))
     71             return buffer;
     72     }
     73 
     74     return 0;
     75 }
     76 
     77 bool AudioBuffer::createdSuccessfully(unsigned desiredNumberOfChannels) const
     78 {
     79     return numberOfChannels() == desiredNumberOfChannels;
     80 }
     81 
     82 AudioBuffer::AudioBuffer(unsigned numberOfChannels, size_t numberOfFrames, float sampleRate)
     83     : m_gain(1.0)
     84     , m_sampleRate(sampleRate)
     85     , m_length(numberOfFrames)
     86 {
     87     ScriptWrappable::init(this);
     88     m_channels.reserveCapacity(numberOfChannels);
     89 
     90     for (unsigned i = 0; i < numberOfChannels; ++i) {
     91         RefPtr<Float32Array> channelDataArray = Float32Array::create(m_length);
     92         // If the channel data array could not be created, just return. The caller will need to
     93         // check that the desired number of channels were created.
     94         if (!channelDataArray) {
     95             return;
     96         }
     97 
     98         channelDataArray->setNeuterable(false);
     99         m_channels.append(channelDataArray);
    100     }
    101 }
    102 
    103 AudioBuffer::AudioBuffer(AudioBus* bus)
    104     : m_gain(1.0)
    105     , m_sampleRate(bus->sampleRate())
    106     , m_length(bus->length())
    107 {
    108     ScriptWrappable::init(this);
    109     // Copy audio data from the bus to the Float32Arrays we manage.
    110     unsigned numberOfChannels = bus->numberOfChannels();
    111     m_channels.reserveCapacity(numberOfChannels);
    112     for (unsigned i = 0; i < numberOfChannels; ++i) {
    113         RefPtr<Float32Array> channelDataArray = Float32Array::create(m_length);
    114         // If the channel data array could not be created, just return. The caller will need to
    115         // check that the desired number of channels were created.
    116         if (!channelDataArray)
    117             return;
    118 
    119         channelDataArray->setNeuterable(false);
    120         channelDataArray->setRange(bus->channel(i)->data(), m_length, 0);
    121         m_channels.append(channelDataArray);
    122     }
    123 }
    124 
    125 void AudioBuffer::releaseMemory()
    126 {
    127     m_channels.clear();
    128 }
    129 
    130 PassRefPtr<Float32Array> AudioBuffer::getChannelData(unsigned channelIndex, ExceptionState& exceptionState)
    131 {
    132     if (channelIndex >= m_channels.size()) {
    133         exceptionState.throwDOMException(IndexSizeError, "channel index (" + String::number(channelIndex) + ") exceeds number of channels (" + String::number(m_channels.size()) + ")");
    134         return 0;
    135     }
    136 
    137     Float32Array* channelData = m_channels[channelIndex].get();
    138     return Float32Array::create(channelData->buffer(), channelData->byteOffset(), channelData->length());
    139 }
    140 
    141 Float32Array* AudioBuffer::getChannelData(unsigned channelIndex)
    142 {
    143     if (channelIndex >= m_channels.size())
    144         return 0;
    145 
    146     return m_channels[channelIndex].get();
    147 }
    148 
    149 void AudioBuffer::zero()
    150 {
    151     for (unsigned i = 0; i < m_channels.size(); ++i) {
    152         if (getChannelData(i))
    153             getChannelData(i)->zeroRange(0, length());
    154     }
    155 }
    156 
    157 } // namespace WebCore
    158 
    159 #endif // ENABLE(WEB_AUDIO)
    160