2014-10-01 17:42:18 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#ifndef COMMON_AUDIO_REAL_FOURIER_H_
|
|
|
|
|
#define COMMON_AUDIO_REAL_FOURIER_H_
|
2014-10-01 17:42:18 +00:00
|
|
|
|
2018-10-23 12:03:01 +02:00
|
|
|
#include <stddef.h>
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2014-10-01 17:42:18 +00:00
|
|
|
#include <complex>
|
2016-05-01 14:53:46 -07:00
|
|
|
#include <memory>
|
2014-10-01 17:42:18 +00:00
|
|
|
|
2018-03-22 14:11:52 +01:00
|
|
|
#include "rtc_base/memory/aligned_malloc.h"
|
2014-10-01 17:42:18 +00:00
|
|
|
|
|
|
|
|
// Uniform interface class for the real DFT and its inverse, for power-of-2
|
|
|
|
|
// input lengths. Also contains helper functions for buffer allocation, taking
|
|
|
|
|
// care of any memory alignment requirements the underlying library might have.
|
|
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
class RealFourier {
|
|
|
|
|
public:
|
|
|
|
|
// Shorthand typenames for the scopers used by the buffer allocation helpers.
|
2016-05-01 14:53:46 -07:00
|
|
|
typedef std::unique_ptr<float[], AlignedFreeDeleter> fft_real_scoper;
|
|
|
|
|
typedef std::unique_ptr<std::complex<float>[], AlignedFreeDeleter>
|
2015-02-26 14:34:55 +00:00
|
|
|
fft_cplx_scoper;
|
2014-10-01 17:42:18 +00:00
|
|
|
|
|
|
|
|
// The alignment required for all input and output buffers, in bytes.
|
Misc. small cleanups.
* Better param names
* Avoid using negative values for (bogus) placeholder channel counts (mostly in tests). Since channels will be changing to size_t, negative values will be illegal; it's sufficient to use 0 in these cases.
* Use arraysize()
* Use size_t for counting frames, samples, blocks, buffers, and bytes -- most of these are already size_t in most places, this just fixes some stragglers
* reinterpret_cast<int64_t>(void*) is not necessarily safe; use uintptr_t instead
* Remove unnecessary code, e.g. dead code, needlessly long/repetitive code, or function overrides that exactly match the base definition
* Fix indenting
* Use uint32_t for timestamps (matching how it's already a uint32_t in most places)
* Spelling
* RTC_CHECK_EQ(expected, actual)
* Rewrap
* Use .empty()
* Be more pedantic about matching int/int32_t/
* Remove pointless consts on input parameters to functions
* Add missing sanity checks
All this was found in the course of constructing https://codereview.webrtc.org/1316523002/ , and is being landed separately first.
BUG=none
TEST=none
Review URL: https://codereview.webrtc.org/1534193008
Cr-Commit-Position: refs/heads/master@{#11191}
2016-01-08 13:50:27 -08:00
|
|
|
static const size_t kFftBufferAlignment;
|
2014-10-01 17:42:18 +00:00
|
|
|
|
|
|
|
|
// Construct a wrapper instance for the given input order, which must be
|
|
|
|
|
// between 1 and kMaxFftOrder, inclusively.
|
2016-05-01 14:53:46 -07:00
|
|
|
static std::unique_ptr<RealFourier> Create(int fft_order);
|
2017-03-09 06:25:06 -08:00
|
|
|
virtual ~RealFourier() {}
|
2014-10-01 17:42:18 +00:00
|
|
|
|
2015-03-19 20:06:29 +00:00
|
|
|
// Helper to compute the smallest FFT order (a power of 2) which will contain
|
|
|
|
|
// the given input length.
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
static int FftOrder(size_t length);
|
2014-10-01 17:42:18 +00:00
|
|
|
|
2015-03-19 20:06:29 +00:00
|
|
|
// Helper to compute the input length from the FFT order.
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
static size_t FftLength(int order);
|
2015-03-19 20:06:29 +00:00
|
|
|
|
|
|
|
|
// Helper to compute the exact length, in complex floats, of the transform
|
|
|
|
|
// output (i.e. |2^order / 2 + 1|).
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
static size_t ComplexLength(int order);
|
2014-10-01 17:42:18 +00:00
|
|
|
|
2021-07-26 12:15:29 +02:00
|
|
|
// Buffer allocation helpers. The buffers are large enough to hold `count`
|
2014-10-01 17:42:18 +00:00
|
|
|
// floats/complexes and suitably aligned for use by the implementation.
|
|
|
|
|
// The returned scopers are set up with proper deleters; the caller owns
|
|
|
|
|
// the allocated memory.
|
|
|
|
|
static fft_real_scoper AllocRealBuffer(int count);
|
|
|
|
|
static fft_cplx_scoper AllocCplxBuffer(int count);
|
|
|
|
|
|
|
|
|
|
// Main forward transform interface. The output array need only be big
|
|
|
|
|
// enough for |2^order / 2 + 1| elements - the conjugate pairs are not
|
|
|
|
|
// returned. Input and output must be properly aligned (e.g. through
|
|
|
|
|
// AllocRealBuffer and AllocCplxBuffer) and input length must be
|
|
|
|
|
// |2^order| (same as given at construction time).
|
2015-03-19 20:06:29 +00:00
|
|
|
virtual void Forward(const float* src, std::complex<float>* dest) const = 0;
|
2014-10-01 17:42:18 +00:00
|
|
|
|
|
|
|
|
// Inverse transform. Same input format as output above, conjugate pairs
|
|
|
|
|
// not needed.
|
2015-03-19 20:06:29 +00:00
|
|
|
virtual void Inverse(const std::complex<float>* src, float* dest) const = 0;
|
2014-10-01 17:42:18 +00:00
|
|
|
|
2015-03-19 20:06:29 +00:00
|
|
|
virtual int order() const = 0;
|
2014-10-01 17:42:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#endif // COMMON_AUDIO_REAL_FOURIER_H_
|