sequencer
transfer_function.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <sequencer/assert.hpp>
4 
5 #include <algorithm>
6 #include <cassert>
7 #include <cmath>
8 
9 namespace sequencer::audio
10 {
11  constexpr auto ideal_high_pass( double freq, double cutoff ) noexcept
12  {
13  return freq > cutoff ? 1 : 0;
14  }
15 
16  constexpr auto ideal_low_pass( double freq, double cutoff ) noexcept
17  {
18  return freq < cutoff ? 1 : 0;
19  }
20 
21  constexpr auto low_pass( double freq, double gain, double transition )
22  {
23  return ( gain * transition ) / ( freq + transition );
24  }
25 
26  constexpr auto high_pass( double freq, double gain, double transition )
27  {
28  return low_pass( 1 / freq, gain, transition );
29  }
30 
31  constexpr auto low_shelf( double freq, double gain, double transition )
32  {
33  return 1 + low_pass( freq, gain, transition );
34  }
35 
36  constexpr auto high_shelf( double freq, double gain, double transition )
37  {
38  return 1 + high_pass( freq, gain, transition );
39  }
40 
41  template < class Container, class TransferFunction >
42  void filter( Container& c, TransferFunction f, double base_frequency )
43  {
44  using std::begin;
45  using std::end;
46  auto counter = 0;
47  std::for_each( begin( c ), end( c ),
48  [&]( auto& value ) { value *= f( ( counter++ ) * base_frequency ); } );
49  }
50 
51  template < class Container, class TransferFunction1, class TransferFunction2 >
52  void filter( Container& c, TransferFunction1 f, TransferFunction2 g, double base_frequency )
53  {
54 
55  using std::begin;
56  using std::end;
57  auto counter = 0;
58  std::for_each( begin( c ), end( c ), [&]( auto& value ) {
59  const auto freq = ( counter++ ) * base_frequency;
60  value *= f( freq ) * g( freq );
61  } );
62  }
63 
64  namespace window
65  {
66  inline double hamming( int n, int N )
67  {
68  SEQUENCER_ASSERT( n >= 0 )
69  SEQUENCER_ASSERT( n <= N )
70 
71  return 0.54 + 0.46 * std::cos( 2 * n * M_PI / N );
72  }
73 
74  inline double blackman( int n, int N )
75  {
76  SEQUENCER_ASSERT( n >= 0 )
77  SEQUENCER_ASSERT( n <= N )
78 
79  return 0.42 - 0.5 * std::cos( 2 * n * M_PI / N ) +
80  0.08 * std::cos( ( 4 * n * M_PI / N ) );
81  }
82  } // namespace window
83 } // namespace sequencer::audio
void filter(Container &c, TransferFunction f, double base_frequency)
Definition: transfer_function.hpp:42
double blackman(int n, int N)
Definition: transfer_function.hpp:74
#define SEQUENCER_ASSERT(cond)
Definition: assert.hpp:8
constexpr auto ideal_high_pass(double freq, double cutoff) noexcept
Definition: transfer_function.hpp:11
double hamming(int n, int N)
Definition: transfer_function.hpp:66
constexpr auto ideal_low_pass(double freq, double cutoff) noexcept
Definition: transfer_function.hpp:16
Definition: delay.hpp:12
constexpr auto low_shelf(double freq, double gain, double transition)
Definition: transfer_function.hpp:31
constexpr auto high_pass(double freq, double gain, double transition)
Definition: transfer_function.hpp:26
constexpr auto high_shelf(double freq, double gain, double transition)
Definition: transfer_function.hpp:36
constexpr auto low_pass(double freq, double gain, double transition)
Definition: transfer_function.hpp:21