NAP
audiofunctions.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 #include <mathutils.h>
8 
9 namespace nap
10 {
11  namespace audio
12  {
13 
19  inline unsigned int wrap(unsigned int index, unsigned int bufferSize)
20  {
21  unsigned int bitMask = bufferSize - 1;
22  return index & bitMask;
23  }
24 
25 
33  template<typename T>
34  inline T lerp(const T& v0, const T& v1, const T& t)
35  {
36  return v0 + t * (v1 - v0);
37  }
38 
45  template<typename T>
46  inline void equalPowerPan(const T& panning, T& left, T& right)
47  {
48  left = cos(panning * 0.5 * math::PI);
49  right = sin(panning * 0.5 * math::PI);
50  }
51 
52 
58  inline float mtof(float pitch)
59  {
60  auto res = pitch - 57;
61  res /= 12.0;
62  res = pow(2.0, res);
63  res *= 220.0;
64  return res;
65  }
66 
67 
73  inline float toDB(float amplitude)
74  {
75  return 20 * log10(amplitude);
76  }
77 
78 
85  inline float dbToA(float db, float zero = -48)
86  {
87  if (db <= zero)
88  return 0;
89 
90  return powf(10, db / 20.0);
91  }
92 
93  }
94 }
nap::audio::dbToA
float dbToA(float db, float zero=-48)
Definition: audiofunctions.h:85
nap::audio::lerp
T lerp(const T &v0, const T &v1, const T &t)
Definition: audiofunctions.h:34
nap::math::PI
constexpr double PI
Definition: mathutils.h:24
nap
Definition: templateapp.h:17
nap::audio::mtof
float mtof(float pitch)
Definition: audiofunctions.h:58
nap::audio::toDB
float toDB(float amplitude)
Definition: audiofunctions.h:73
nap::audio::wrap
unsigned int wrap(unsigned int index, unsigned int bufferSize)
Definition: audiofunctions.h:19
nap::audio::equalPowerPan
void equalPowerPan(const T &panning, T &left, T &right)
Definition: audiofunctions.h:46