1424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 2424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// found in the LICENSE file. 4424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 5424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// A set of utility functions to perform WSOLA. 6424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 7424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#ifndef MEDIA_FILTERS_WSOLA_INTERNALS_H_ 8424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#define MEDIA_FILTERS_WSOLA_INTERNALS_H_ 9424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 10424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include <utility> 11424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 12424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "media/base/media_export.h" 13424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 14424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)namespace media { 15424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 16424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)class AudioBus; 17424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 18424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)namespace internal { 19424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 20424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)typedef std::pair<int, int> Interval; 21424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 22424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Dot-product of channels of two AudioBus. For each AudioBus an offset is 23424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// given. |dot_product[k]| is the dot-product of channel |k|. The caller should 24424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// allocate sufficient space for |dot_product|. 25424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)MEDIA_EXPORT void MultiChannelDotProduct(const AudioBus* a, 26424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) int frame_offset_a, 27424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const AudioBus* b, 28424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) int frame_offset_b, 29424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) int num_frames, 30424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) float* dot_product); 31424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 32424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Energies of sliding windows of channels are interleaved. 33424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// The number windows is |input->frames()| - (|frames_per_window| - 1), hence, 34424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// the method assumes |energy| must be, at least, of size 35424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// (|input->frames()| - (|frames_per_window| - 1)) * |input->channels()|. 36424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)MEDIA_EXPORT void MultiChannelMovingBlockEnergies(const AudioBus* input, 37424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) int frames_per_window, 38424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) float* energy); 39424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 40424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Fit the curve f(x) = a * x^2 + b * x + c such that 41e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// f(-1) = y[0] 42e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// f(0) = y[1] 43e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// f(1) = y[2] 44e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// and return the maximum, assuming that y[0] <= y[1] >= y[2]. 45e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen MurdochMEDIA_EXPORT void QuadraticInterpolation(const float* y_values, 46e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch float* extremum, 47e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch float* extremum_value); 48424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 49424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Search a subset of all candid blocks. The search is performed every 50424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// |decimation| frames. This reduces complexity by a factor of about 51424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// 1 / |decimation|. A cubic interpolation is used to have a better estimate of 52424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// the best match. 53424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)MEDIA_EXPORT int DecimatedSearch(int decimation, 54424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) Interval exclude_interval, 55424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const AudioBus* target_block, 56424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const AudioBus* search_segment, 57424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const float* energy_target_block, 58424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const float* energy_candid_blocks); 59424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 60424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Search [|low_limit|, |high_limit|] of |search_segment| to find a block that 61424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// is most similar to |target_block|. |energy_target_block| is the energy of the 62424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// |target_block|. |energy_candidate_blocks| is the energy of all blocks within 63424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// |search_block|. 64424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)MEDIA_EXPORT int FullSearch(int low_limit, 65424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) int hight_limimit, 66424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) Interval exclude_interval, 67424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const AudioBus* target_block, 68424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const AudioBus* search_block, 69424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const float* energy_target_block, 70424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const float* energy_candidate_blocks); 71424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 72424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Find the index of the block, within |search_block|, that is most similar 73424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// to |target_block|. Obviously, the returned index is w.r.t. |search_block|. 74424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// |exclude_interval| is an interval that is excluded from the search. 75424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)MEDIA_EXPORT int OptimalIndex(const AudioBus* search_block, 76424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const AudioBus* target_block, 77424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) Interval exclude_interval); 78424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 79424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Return a "periodic" Hann window. This is the first L samples of an L+1 80424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Hann window. It is perfect reconstruction for overlap-and-add. 81424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)MEDIA_EXPORT void GetSymmetricHanningWindow(int window_length, float* window); 82424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 83424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} // namespace internal 84424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 85424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} // namespace media 86424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 87424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#endif // MEDIA_FILTERS_WSOLA_INTERNALS_H_ 88