OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2011, Google Inc. All rights reserved. | 2 * Copyright (C) 2011, 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 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 13 matching lines...) Expand all Loading... |
24 | 24 |
25 #include "config.h" | 25 #include "config.h" |
26 | 26 |
27 #if ENABLE(WEB_AUDIO) | 27 #if ENABLE(WEB_AUDIO) |
28 | 28 |
29 #include "modules/webaudio/WaveShaperDSPKernel.h" | 29 #include "modules/webaudio/WaveShaperDSPKernel.h" |
30 | 30 |
31 #include "modules/webaudio/WaveShaperProcessor.h" | 31 #include "modules/webaudio/WaveShaperProcessor.h" |
32 #include <algorithm> | 32 #include <algorithm> |
33 | 33 |
| 34 const unsigned RenderingQuantum = 128; |
| 35 |
34 using namespace std; | 36 using namespace std; |
35 | 37 |
36 namespace WebCore { | 38 namespace WebCore { |
37 | 39 |
| 40 WaveShaperDSPKernel::WaveShaperDSPKernel(WaveShaperProcessor* processor) |
| 41 : AudioDSPKernel(processor) |
| 42 { |
| 43 if (processor->oversample() != WaveShaperProcessor::OverSampleNone) |
| 44 lazyInitializeOversampling(); |
| 45 } |
| 46 |
| 47 void WaveShaperDSPKernel::lazyInitializeOversampling() |
| 48 { |
| 49 ASSERT(isMainThread()); |
| 50 |
| 51 if (!m_tempBuffer) { |
| 52 m_tempBuffer = adoptPtr(new AudioFloatArray(RenderingQuantum * 2)); |
| 53 m_tempBuffer2 = adoptPtr(new AudioFloatArray(RenderingQuantum * 4)); |
| 54 m_upSampler = adoptPtr(new UpSampler(RenderingQuantum)); |
| 55 m_downSampler = adoptPtr(new DownSampler(RenderingQuantum * 2)); |
| 56 m_upSampler2 = adoptPtr(new UpSampler(RenderingQuantum * 2)); |
| 57 m_downSampler2 = adoptPtr(new DownSampler(RenderingQuantum * 4)); |
| 58 } |
| 59 } |
| 60 |
38 void WaveShaperDSPKernel::process(const float* source, float* destination, size_
t framesToProcess) | 61 void WaveShaperDSPKernel::process(const float* source, float* destination, size_
t framesToProcess) |
39 { | 62 { |
| 63 switch (waveShaperProcessor()->oversample()) { |
| 64 case WaveShaperProcessor::OverSampleNone: |
| 65 processCurve(source, destination, framesToProcess); |
| 66 break; |
| 67 case WaveShaperProcessor::OverSample2x: |
| 68 processCurve2x(source, destination, framesToProcess); |
| 69 break; |
| 70 case WaveShaperProcessor::OverSample4x: |
| 71 processCurve4x(source, destination, framesToProcess); |
| 72 break; |
| 73 |
| 74 default: |
| 75 ASSERT_NOT_REACHED(); |
| 76 } |
| 77 } |
| 78 |
| 79 void WaveShaperDSPKernel::processCurve(const float* source, float* destination,
size_t framesToProcess) |
| 80 { |
40 ASSERT(source && destination && waveShaperProcessor()); | 81 ASSERT(source && destination && waveShaperProcessor()); |
41 | 82 |
42 Float32Array* curve = waveShaperProcessor()->curve(); | 83 Float32Array* curve = waveShaperProcessor()->curve(); |
43 if (!curve) { | 84 if (!curve) { |
44 // Act as "straight wire" pass-through if no curve is set. | 85 // Act as "straight wire" pass-through if no curve is set. |
45 memcpy(destination, source, sizeof(float) * framesToProcess); | 86 memcpy(destination, source, sizeof(float) * framesToProcess); |
46 return; | 87 return; |
47 } | 88 } |
48 | 89 |
49 float* curveData = curve->data(); | 90 float* curveData = curve->data(); |
50 int curveLength = curve->length(); | 91 int curveLength = curve->length(); |
51 | 92 |
52 ASSERT(curveData); | 93 ASSERT(curveData); |
53 | 94 |
54 if (!curveData || !curveLength) { | 95 if (!curveData || !curveLength) { |
55 memcpy(destination, source, sizeof(float) * framesToProcess); | 96 memcpy(destination, source, sizeof(float) * framesToProcess); |
56 return; | 97 return; |
57 } | 98 } |
58 | 99 |
59 // Apply waveshaping curve. | 100 // Apply waveshaping curve. |
60 for (unsigned i = 0; i < framesToProcess; ++i) { | 101 for (unsigned i = 0; i < framesToProcess; ++i) { |
61 const float input = source[i]; | 102 const float input = source[i]; |
62 | 103 |
63 // Calculate an index based on input -1 -> +1 with 0 being at the center
of the curve data. | 104 // Calculate a virtual index based on input -1 -> +1 with 0 being at the
center of the curve data. |
64 int index = (curveLength * (input + 1)) / 2; | 105 // Then linearly interpolate between the two points in the curve. |
| 106 double virtualIndex = 0.5 * (input + 1) * curveLength; |
| 107 int index1 = static_cast<int>(virtualIndex); |
| 108 int index2 = index1 + 1; |
| 109 double interpolationFactor = virtualIndex - index1; |
65 | 110 |
66 // Clip index to the input range of the curve. | 111 // Clip index to the input range of the curve. |
67 // This takes care of input outside of nominal range -1 -> +1 | 112 // This takes care of input outside of nominal range -1 -> +1 |
68 index = max(index, 0); | 113 index1 = max(index1, 0); |
69 index = min(index, curveLength - 1); | 114 index1 = min(index1, curveLength - 1); |
70 destination[i] = curveData[index]; | 115 index2 = max(index2, 0); |
| 116 index2 = min(index2, curveLength - 1); |
| 117 |
| 118 double value1 = curveData[index1]; |
| 119 double value2 = curveData[index2]; |
| 120 |
| 121 double output = (1.0 - interpolationFactor) * value1 + interpolationFact
or * value2; |
| 122 destination[i] = output; |
71 } | 123 } |
72 } | 124 } |
73 | 125 |
| 126 void WaveShaperDSPKernel::processCurve2x(const float* source, float* destination
, size_t framesToProcess) |
| 127 { |
| 128 bool isSafe = framesToProcess == RenderingQuantum; |
| 129 ASSERT(isSafe); |
| 130 if (!isSafe) |
| 131 return; |
| 132 |
| 133 float* tempP = m_tempBuffer->data(); |
| 134 |
| 135 m_upSampler->process(source, tempP, framesToProcess); |
| 136 |
| 137 // Process at 2x up-sampled rate. |
| 138 processCurve(tempP, tempP, framesToProcess * 2); |
| 139 |
| 140 m_downSampler->process(tempP, destination, framesToProcess * 2); |
| 141 } |
| 142 |
| 143 void WaveShaperDSPKernel::processCurve4x(const float* source, float* destination
, size_t framesToProcess) |
| 144 { |
| 145 bool isSafe = framesToProcess == RenderingQuantum; |
| 146 ASSERT(isSafe); |
| 147 if (!isSafe) |
| 148 return; |
| 149 |
| 150 float* tempP = m_tempBuffer->data(); |
| 151 float* tempP2 = m_tempBuffer2->data(); |
| 152 |
| 153 m_upSampler->process(source, tempP, framesToProcess); |
| 154 m_upSampler2->process(tempP, tempP2, framesToProcess * 2); |
| 155 |
| 156 // Process at 4x up-sampled rate. |
| 157 processCurve(tempP2, tempP2, framesToProcess * 4); |
| 158 |
| 159 m_downSampler2->process(tempP2, tempP, framesToProcess * 4); |
| 160 m_downSampler->process(tempP, destination, framesToProcess * 2); |
| 161 } |
| 162 |
| 163 void WaveShaperDSPKernel::reset() |
| 164 { |
| 165 if (m_upSampler) { |
| 166 m_upSampler->reset(); |
| 167 m_downSampler->reset(); |
| 168 m_upSampler2->reset(); |
| 169 m_downSampler2->reset(); |
| 170 } |
| 171 } |
| 172 |
| 173 double WaveShaperDSPKernel::latencyTime() const |
| 174 { |
| 175 size_t latencyFrames = 0; |
| 176 WaveShaperDSPKernel* kernel = const_cast<WaveShaperDSPKernel*>(this); |
| 177 |
| 178 switch (kernel->waveShaperProcessor()->oversample()) { |
| 179 case WaveShaperProcessor::OverSampleNone: |
| 180 break; |
| 181 case WaveShaperProcessor::OverSample2x: |
| 182 latencyFrames += m_upSampler->latencyFrames(); |
| 183 latencyFrames += m_downSampler->latencyFrames(); |
| 184 break; |
| 185 case WaveShaperProcessor::OverSample4x: |
| 186 { |
| 187 // Account for first stage upsampling. |
| 188 latencyFrames += m_upSampler->latencyFrames(); |
| 189 latencyFrames += m_downSampler->latencyFrames(); |
| 190 |
| 191 // Account for second stage upsampling. |
| 192 // and divide by 2 to get back down to the regular sample-rate. |
| 193 size_t latencyFrames2 = (m_upSampler2->latencyFrames() + m_downSampl
er2->latencyFrames()) / 2; |
| 194 latencyFrames += latencyFrames2; |
| 195 break; |
| 196 } |
| 197 default: |
| 198 ASSERT_NOT_REACHED(); |
| 199 } |
| 200 |
| 201 return static_cast<double>(latencyFrames) / sampleRate(); |
| 202 } |
| 203 |
74 } // namespace WebCore | 204 } // namespace WebCore |
75 | 205 |
76 #endif // ENABLE(WEB_AUDIO) | 206 #endif // ENABLE(WEB_AUDIO) |
OLD | NEW |