OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 15 matching lines...) Expand all Loading... |
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 */ | 27 */ |
28 | 28 |
29 #include "platform/audio/AudioBus.h" | 29 #include "platform/audio/AudioBus.h" |
30 #include "platform/audio/AudioFileReader.h" | 30 #include "platform/audio/AudioFileReader.h" |
31 #include "platform/audio/DenormalDisabler.h" | 31 #include "platform/audio/DenormalDisabler.h" |
32 #include "platform/audio/SincResampler.h" | 32 #include "platform/audio/SincResampler.h" |
33 #include "platform/audio/VectorMath.h" | 33 #include "platform/audio/VectorMath.h" |
34 #include "public/platform/Platform.h" | 34 #include "public/platform/Platform.h" |
35 #include "public/platform/WebAudioBus.h" | 35 #include "public/platform/WebAudioBus.h" |
36 #include "wtf/OwnPtr.h" | 36 #include "wtf/PtrUtil.h" |
| 37 #include <algorithm> |
37 #include <assert.h> | 38 #include <assert.h> |
38 #include <math.h> | 39 #include <math.h> |
39 #include <algorithm> | 40 #include <memory> |
40 | 41 |
41 namespace blink { | 42 namespace blink { |
42 | 43 |
43 using namespace VectorMath; | 44 using namespace VectorMath; |
44 | 45 |
45 const unsigned MaxBusChannels = 32; | 46 const unsigned MaxBusChannels = 32; |
46 | 47 |
47 PassRefPtr<AudioBus> AudioBus::create(unsigned numberOfChannels, size_t length,
bool allocate) | 48 PassRefPtr<AudioBus> AudioBus::create(unsigned numberOfChannels, size_t length,
bool allocate) |
48 { | 49 { |
49 ASSERT(numberOfChannels <= MaxBusChannels); | 50 ASSERT(numberOfChannels <= MaxBusChannels); |
50 if (numberOfChannels > MaxBusChannels) | 51 if (numberOfChannels > MaxBusChannels) |
51 return nullptr; | 52 return nullptr; |
52 | 53 |
53 return adoptRef(new AudioBus(numberOfChannels, length, allocate)); | 54 return adoptRef(new AudioBus(numberOfChannels, length, allocate)); |
54 } | 55 } |
55 | 56 |
56 AudioBus::AudioBus(unsigned numberOfChannels, size_t length, bool allocate) | 57 AudioBus::AudioBus(unsigned numberOfChannels, size_t length, bool allocate) |
57 : m_length(length) | 58 : m_length(length) |
58 , m_busGain(1) | 59 , m_busGain(1) |
59 , m_isFirstTime(true) | 60 , m_isFirstTime(true) |
60 , m_sampleRate(0) | 61 , m_sampleRate(0) |
61 { | 62 { |
62 m_channels.reserveInitialCapacity(numberOfChannels); | 63 m_channels.reserveInitialCapacity(numberOfChannels); |
63 | 64 |
64 for (unsigned i = 0; i < numberOfChannels; ++i) { | 65 for (unsigned i = 0; i < numberOfChannels; ++i) { |
65 PassOwnPtr<AudioChannel> channel = allocate ? adoptPtr(new AudioChannel(
length)) : adoptPtr(new AudioChannel(0, length)); | 66 std::unique_ptr<AudioChannel> channel = allocate ? wrapUnique(new AudioC
hannel(length)) : wrapUnique(new AudioChannel(0, length)); |
66 m_channels.append(std::move(channel)); | 67 m_channels.append(std::move(channel)); |
67 } | 68 } |
68 | 69 |
69 m_layout = LayoutCanonical; // for now this is the only layout we define | 70 m_layout = LayoutCanonical; // for now this is the only layout we define |
70 } | 71 } |
71 | 72 |
72 void AudioBus::setChannelMemory(unsigned channelIndex, float* storage, size_t le
ngth) | 73 void AudioBus::setChannelMemory(unsigned channelIndex, float* storage, size_t le
ngth) |
73 { | 74 { |
74 if (channelIndex < m_channels.size()) { | 75 if (channelIndex < m_channels.size()) { |
75 channel(channelIndex)->set(storage, length); | 76 channel(channelIndex)->set(storage, length); |
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
482 // FIXME: this value may need tweaking. | 483 // FIXME: this value may need tweaking. |
483 const float epsilon = 0.001f; | 484 const float epsilon = 0.001f; |
484 float gainDiff = fabs(totalDesiredGain - gain); | 485 float gainDiff = fabs(totalDesiredGain - gain); |
485 | 486 |
486 // Number of frames to de-zipper before we are close enough to the target ga
in. | 487 // Number of frames to de-zipper before we are close enough to the target ga
in. |
487 // FIXME: framesToDezipper could be smaller when target gain is close enough
within this process loop. | 488 // FIXME: framesToDezipper could be smaller when target gain is close enough
within this process loop. |
488 unsigned framesToDezipper = (gainDiff < epsilon) ? 0 : framesToProcess; | 489 unsigned framesToDezipper = (gainDiff < epsilon) ? 0 : framesToProcess; |
489 | 490 |
490 if (framesToDezipper) { | 491 if (framesToDezipper) { |
491 if (!m_dezipperGainValues.get() || m_dezipperGainValues->size() < frames
ToDezipper) | 492 if (!m_dezipperGainValues.get() || m_dezipperGainValues->size() < frames
ToDezipper) |
492 m_dezipperGainValues = adoptPtr(new AudioFloatArray(framesToDezipper
)); | 493 m_dezipperGainValues = wrapUnique(new AudioFloatArray(framesToDezipp
er)); |
493 | 494 |
494 float* gainValues = m_dezipperGainValues->data(); | 495 float* gainValues = m_dezipperGainValues->data(); |
495 for (unsigned i = 0; i < framesToDezipper; ++i) { | 496 for (unsigned i = 0; i < framesToDezipper; ++i) { |
496 gain += (totalDesiredGain - gain) * DezipperRate; | 497 gain += (totalDesiredGain - gain) * DezipperRate; |
497 | 498 |
498 // FIXME: If we are clever enough in calculating the framesToDezippe
r value, we can probably get | 499 // FIXME: If we are clever enough in calculating the framesToDezippe
r value, we can probably get |
499 // rid of this DenormalDisabler::flushDenormalFloatToZero() call. | 500 // rid of this DenormalDisabler::flushDenormalFloatToZero() call. |
500 gain = DenormalDisabler::flushDenormalFloatToZero(gain); | 501 gain = DenormalDisabler::flushDenormalFloatToZero(gain); |
501 *gainValues++ = gain; | 502 *gainValues++ = gain; |
502 } | 503 } |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
705 | 706 |
706 // If the bus needs no conversion then return as is. | 707 // If the bus needs no conversion then return as is. |
707 if ((!mixToMono || audioBus->numberOfChannels() == 1) && audioBus->sampleRat
e() == sampleRate) | 708 if ((!mixToMono || audioBus->numberOfChannels() == 1) && audioBus->sampleRat
e() == sampleRate) |
708 return audioBus; | 709 return audioBus; |
709 | 710 |
710 return AudioBus::createBySampleRateConverting(audioBus.get(), mixToMono, sam
pleRate); | 711 return AudioBus::createBySampleRateConverting(audioBus.get(), mixToMono, sam
pleRate); |
711 } | 712 } |
712 | 713 |
713 } // namespace blink | 714 } // namespace blink |
714 | 715 |
OLD | NEW |