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 18 matching lines...) Expand all Loading... |
29 #include "config.h" | 29 #include "config.h" |
30 | 30 |
31 #if ENABLE(WEB_AUDIO) | 31 #if ENABLE(WEB_AUDIO) |
32 | 32 |
33 #include "platform/audio/HRTFKernel.h" | 33 #include "platform/audio/HRTFKernel.h" |
34 | 34 |
35 #include "platform/audio/AudioChannel.h" | 35 #include "platform/audio/AudioChannel.h" |
36 #include "platform/FloatConversion.h" | 36 #include "platform/FloatConversion.h" |
37 #include "wtf/MathExtras.h" | 37 #include "wtf/MathExtras.h" |
38 | 38 |
39 using namespace std; | |
40 | |
41 namespace blink { | 39 namespace blink { |
42 | 40 |
43 // Takes the input AudioChannel as an input impulse response and calculates the
average group delay. | 41 // Takes the input AudioChannel as an input impulse response and calculates the
average group delay. |
44 // This represents the initial delay before the most energetic part of the impul
se response. | 42 // This represents the initial delay before the most energetic part of the impul
se response. |
45 // The sample-frame delay is removed from the impulseP impulse response, and thi
s value is returned. | 43 // The sample-frame delay is removed from the impulseP impulse response, and thi
s value is returned. |
46 // the length of the passed in AudioChannel must be a power of 2. | 44 // the length of the passed in AudioChannel must be a power of 2. |
47 static float extractAverageGroupDelay(AudioChannel* channel, size_t analysisFFTS
ize) | 45 static float extractAverageGroupDelay(AudioChannel* channel, size_t analysisFFTS
ize) |
48 { | 46 { |
49 ASSERT(channel); | 47 ASSERT(channel); |
50 | 48 |
(...skipping 22 matching lines...) Expand all Loading... |
73 { | 71 { |
74 ASSERT(channel); | 72 ASSERT(channel); |
75 | 73 |
76 // Determine the leading delay (average group delay) for the response. | 74 // Determine the leading delay (average group delay) for the response. |
77 m_frameDelay = extractAverageGroupDelay(channel, fftSize / 2); | 75 m_frameDelay = extractAverageGroupDelay(channel, fftSize / 2); |
78 | 76 |
79 float* impulseResponse = channel->mutableData(); | 77 float* impulseResponse = channel->mutableData(); |
80 size_t responseLength = channel->length(); | 78 size_t responseLength = channel->length(); |
81 | 79 |
82 // We need to truncate to fit into 1/2 the FFT size (with zero padding) in o
rder to do proper convolution. | 80 // We need to truncate to fit into 1/2 the FFT size (with zero padding) in o
rder to do proper convolution. |
83 size_t truncatedResponseLength = min(responseLength, fftSize / 2); // trunca
te if necessary to max impulse response length allowed by FFT | 81 size_t truncatedResponseLength = std::min(responseLength, fftSize / 2); // t
runcate if necessary to max impulse response length allowed by FFT |
84 | 82 |
85 // Quick fade-out (apply window) at truncation point | 83 // Quick fade-out (apply window) at truncation point |
86 unsigned numberOfFadeOutFrames = static_cast<unsigned>(sampleRate / 4410); /
/ 10 sample-frames @44.1KHz sample-rate | 84 unsigned numberOfFadeOutFrames = static_cast<unsigned>(sampleRate / 4410); /
/ 10 sample-frames @44.1KHz sample-rate |
87 ASSERT(numberOfFadeOutFrames < truncatedResponseLength); | 85 ASSERT(numberOfFadeOutFrames < truncatedResponseLength); |
88 if (numberOfFadeOutFrames < truncatedResponseLength) { | 86 if (numberOfFadeOutFrames < truncatedResponseLength) { |
89 for (unsigned i = truncatedResponseLength - numberOfFadeOutFrames; i < t
runcatedResponseLength; ++i) { | 87 for (unsigned i = truncatedResponseLength - numberOfFadeOutFrames; i < t
runcatedResponseLength; ++i) { |
90 float x = 1.0f - static_cast<float>(i - (truncatedResponseLength - n
umberOfFadeOutFrames)) / numberOfFadeOutFrames; | 88 float x = 1.0f - static_cast<float>(i - (truncatedResponseLength - n
umberOfFadeOutFrames)) / numberOfFadeOutFrames; |
91 impulseResponse[i] *= x; | 89 impulseResponse[i] *= x; |
92 } | 90 } |
93 } | 91 } |
(...skipping 15 matching lines...) Expand all Loading... |
109 } | 107 } |
110 | 108 |
111 // Interpolates two kernels with x: 0 -> 1 and returns the result. | 109 // Interpolates two kernels with x: 0 -> 1 and returns the result. |
112 PassRefPtr<HRTFKernel> HRTFKernel::createInterpolatedKernel(HRTFKernel* kernel1,
HRTFKernel* kernel2, float x) | 110 PassRefPtr<HRTFKernel> HRTFKernel::createInterpolatedKernel(HRTFKernel* kernel1,
HRTFKernel* kernel2, float x) |
113 { | 111 { |
114 ASSERT(kernel1 && kernel2); | 112 ASSERT(kernel1 && kernel2); |
115 if (!kernel1 || !kernel2) | 113 if (!kernel1 || !kernel2) |
116 return nullptr; | 114 return nullptr; |
117 | 115 |
118 ASSERT(x >= 0.0 && x < 1.0); | 116 ASSERT(x >= 0.0 && x < 1.0); |
119 x = min(1.0f, max(0.0f, x)); | 117 x = std::min(1.0f, std::max(0.0f, x)); |
120 | 118 |
121 float sampleRate1 = kernel1->sampleRate(); | 119 float sampleRate1 = kernel1->sampleRate(); |
122 float sampleRate2 = kernel2->sampleRate(); | 120 float sampleRate2 = kernel2->sampleRate(); |
123 ASSERT(sampleRate1 == sampleRate2); | 121 ASSERT(sampleRate1 == sampleRate2); |
124 if (sampleRate1 != sampleRate2) | 122 if (sampleRate1 != sampleRate2) |
125 return nullptr; | 123 return nullptr; |
126 | 124 |
127 float frameDelay = (1 - x) * kernel1->frameDelay() + x * kernel2->frameDelay
(); | 125 float frameDelay = (1 - x) * kernel1->frameDelay() + x * kernel2->frameDelay
(); |
128 | 126 |
129 OwnPtr<FFTFrame> interpolatedFrame = FFTFrame::createInterpolatedFrame(*kern
el1->fftFrame(), *kernel2->fftFrame(), x); | 127 OwnPtr<FFTFrame> interpolatedFrame = FFTFrame::createInterpolatedFrame(*kern
el1->fftFrame(), *kernel2->fftFrame(), x); |
130 return HRTFKernel::create(interpolatedFrame.release(), frameDelay, sampleRat
e1); | 128 return HRTFKernel::create(interpolatedFrame.release(), frameDelay, sampleRat
e1); |
131 } | 129 } |
132 | 130 |
133 } // namespace blink | 131 } // namespace blink |
134 | 132 |
135 #endif // ENABLE(WEB_AUDIO) | 133 #endif // ENABLE(WEB_AUDIO) |
OLD | NEW |