Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(453)

Side by Side Diff: Source/modules/webaudio/WaveShaperDSPKernel.cpp

Issue 15619003: Add support for WaveShaperNode.oversample (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: thread-safety Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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;
Ken Russell (switch to Gerrit) 2013/05/23 02:29:25 Should be in an anonymous namespace.
Chris Rogers 2013/05/24 20:09:04 This is const, so should already be limited to fil
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 float* tempP = m_tempBuffer->data();
129
130 m_upSampler->process(source, tempP, framesToProcess);
Ken Russell (switch to Gerrit) 2013/05/23 02:29:25 Some assertions and checks are needed to ensure th
Chris Rogers 2013/05/24 20:09:04 Done.
131
132 // Process at 2x up-sampled rate.
133 processCurve(tempP, tempP, framesToProcess * 2);
134
135 m_downSampler->process(tempP, destination, framesToProcess * 2);
136 }
137
138 void WaveShaperDSPKernel::processCurve4x(const float* source, float* destination , size_t framesToProcess)
139 {
140 float* tempP = m_tempBuffer->data();
141 float* tempP2 = m_tempBuffer2->data();
142
143 m_upSampler->process(source, tempP, framesToProcess);
Ken Russell (switch to Gerrit) 2013/05/23 02:29:25 Same here regarding framesToProcess.
Chris Rogers 2013/05/24 20:09:04 Done.
144 m_upSampler2->process(tempP, tempP2, framesToProcess * 2);
145
146 // Process at 4x up-sampled rate.
147 processCurve(tempP2, tempP2, framesToProcess * 4);
148
149 m_downSampler2->process(tempP2, tempP, framesToProcess * 4);
150 m_downSampler->process(tempP, destination, framesToProcess * 2);
151 }
152
153 void WaveShaperDSPKernel::reset()
154 {
155 if (m_upSampler) {
156 m_upSampler->reset();
157 m_downSampler->reset();
158 m_upSampler2->reset();
159 m_downSampler2->reset();
160 }
161 }
162
163 double WaveShaperDSPKernel::latencyTime() const
164 {
165 size_t latencyFrames = 0;
166 WaveShaperDSPKernel* kernel = const_cast<WaveShaperDSPKernel*>(this);
167
168 switch (kernel->waveShaperProcessor()->oversample()) {
169 case WaveShaperProcessor::OverSampleNone:
170 break;
171 case WaveShaperProcessor::OverSample2x:
172 latencyFrames += m_upSampler->latencyFrames();
173 latencyFrames += m_downSampler->latencyFrames();
174 break;
175 case WaveShaperProcessor::OverSample4x:
176 {
177 // Account for first stage upsampling.
178 latencyFrames += m_upSampler->latencyFrames();
179 latencyFrames += m_downSampler->latencyFrames();
180
181 // Account for second stage upsampling.
182 // and divide by 2 to get back down to the regular sample-rate.
183 size_t latencyFrames2 = (m_upSampler2->latencyFrames() + m_downSampl er2->latencyFrames()) / 2;
184 latencyFrames += latencyFrames2;
185 break;
186 }
187 default:
188 ASSERT_NOT_REACHED();
189 }
190
191 return static_cast<double>(latencyFrames) / sampleRate();
192 }
193
74 } // namespace WebCore 194 } // namespace WebCore
75 195
76 #endif // ENABLE(WEB_AUDIO) 196 #endif // ENABLE(WEB_AUDIO)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698