Audio Processing Framework (APF) version 0.5.0
fftwtools.h
Go to the documentation of this file.
1/******************************************************************************
2 Copyright (c) 2012-2016 Institut für Nachrichtentechnik, Universität Rostock
3 Copyright (c) 2006-2012 Quality & Usability Lab
4 Deutsche Telekom Laboratories, TU Berlin
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23*******************************************************************************/
24
25// https://AudioProcessingFramework.github.io/
26
29
30#ifndef APF_FFTWTOOLS_H
31#define APF_FFTWTOOLS_H
32
33#include <fftw3.h>
34
35#include <utility> // for std::forward
36#include <limits> // for std::numeric_limits
37
38namespace apf
39{
40
44template<typename T> struct fftw {}; // Most general case is empty, see below!
45
47#define APF_FFTW_TRAITS(longtype, shorttype) \
48 \
49 \
50template<> \
51struct fftw<longtype> { \
52 using plan = fftw ## shorttype ## plan; \
53 static void* malloc(size_t n) { return fftw ## shorttype ## malloc(n); } \
54 static void free(void* p) { fftw ## shorttype ## free(p); } \
55 static void destroy_plan(plan p) { \
56 fftw ## shorttype ## destroy_plan(p); p = nullptr; } \
57 static void execute(const plan p) { fftw ## shorttype ## execute(p); } \
58 static void execute_r2r(const plan p, longtype *in, longtype *out) { \
59 fftw ## shorttype ## execute_r2r(p, in, out); } \
60 static plan plan_r2r_1d(int n, longtype* in, longtype* out \
61 , fftw_r2r_kind kind, unsigned flags) { \
62 return fftw ## shorttype ## plan_r2r_1d(n, in, out, kind, flags); } \
63 class scoped_plan { \
64 public: \
65 template<typename Func, typename... Args> \
66 scoped_plan(Func func, Args... args) \
67 : _plan(func(std::forward<Args>(args)...)), _owning(true) {} \
68 scoped_plan(scoped_plan&& other) \
69 : _plan(std::move(other._plan)), _owning(true) { \
70 other._owning = false; } \
71 ~scoped_plan() { if (_owning) destroy_plan(_plan); } \
72 operator const plan&() { return _plan; } \
73 private: \
74 plan _plan; bool _owning; }; \
75};
76
79APF_FFTW_TRAITS(long double, l_)
80
81#undef APF_FFTW_TRAITS
82
84template<typename T>
86{
87 using size_type = size_t;
88 using difference_type = ptrdiff_t;
89 using pointer = T*;
90 using const_pointer = const T*;
91 using reference = T&;
92 using const_reference = const T&;
93 using value_type = T;
94
95 pointer allocate(size_type n, const void* hint = nullptr)
96 {
97 (void)hint;
98 return static_cast<pointer>(fftw<T>::malloc(sizeof(value_type) * n));
99 }
100
101 void deallocate(pointer p, size_type n)
102 {
103 (void)n;
104 fftw<T>::free(p);
105 }
106
107 void construct(pointer p, const T& t) { new (p) T(t); }
108 void destroy(pointer p) { p->~T(); }
109
110 size_type max_size() const
111 {
112 return std::numeric_limits<size_type>::max() / sizeof(T);
113 }
114
115 template<typename U>
116 struct rebind { using other = fftw_allocator<U>; };
117
118 // Not sure if the following are necessary ...
119
120 fftw_allocator() {}
122 template<typename U> fftw_allocator(const fftw_allocator<U>&) {}
123
124 pointer address(reference value) { return &value; }
125 const_pointer address(const_reference value) { return &value; }
126
127 template<typename U>
128 bool operator==(const fftw_allocator<U>&) { return true; }
129
130 template<typename U>
131 bool operator!=(const fftw_allocator<U>&) { return false; }
132};
133
134} // namespace apf
135
136#endif
#define APF_FFTW_TRAITS(longtype, shorttype)
Macro to create traits classes for float/double/long double.
Definition: fftwtools.h:47
Audio Processing Framework.
Definition: iterator.h:61
Traits class to select float/double/long double versions of FFTW functions.
Definition: fftwtools.h:44