SpectMorph
smrtmemory.hh
1 // Licensed GNU LGPL v2.1 or later: http://www.gnu.org/licenses/lgpl-2.1.html
2 
3 #pragma once
4 
5 #include <vector>
6 #include <cassert>
7 
8 #include "smmath.hh"
9 #include "smutils.hh"
10 #include "smaudio.hh"
11 
12 namespace SpectMorph
13 {
14 
16 {
17  SPECTMORPH_CLASS_NON_COPYABLE (RTMemoryArea);
18 
19  std::vector<unsigned char> m_mem;
20  std::vector<void *> m_malloc_mem;
21  size_t m_used = 0;
22  static constexpr size_t initial_size = 1024 * 1024;
23  static constexpr size_t grow_size = 32 * 1024;
24 public:
25  RTMemoryArea() :
26  m_mem (initial_size)
27  {
28  }
29  void *
30  alloc (size_t n)
31  {
32  n = ((n + 63) / 64) * 64;
33 
34  void *result;
35  if (m_used + n > m_mem.size())
36  {
37  /* if we don't have enough memory available, fall back to malloc */
38  result = malloc (n);
39  m_malloc_mem.push_back (result);
40  }
41  else
42  {
43  result = m_mem.data() + m_used;
44  }
45  m_used += n;
46  return result;
47  }
48  void
49  free_all()
50  {
51  for (auto ptr : m_malloc_mem)
52  free (ptr);
53  m_malloc_mem.clear();
54 
55  if (m_used > m_mem.size())
56  {
57  /* if we didn't have enough memory last time, make the memory area larger
58  *
59  * this really should *never* happen (the initial size should be large
60  * enough), but we do the fallback-to-malloc to not crash in cases
61  * where the initial size was too small
62  */
63  m_mem.resize (m_used + grow_size);
64  }
65 
66  m_used = 0;
67  }
68 };
69 
70 template<class T>
71 class RTVector
72 {
73  SPECTMORPH_CLASS_NON_COPYABLE (RTVector);
74 
75  RTMemoryArea *m_memory_area = nullptr;
76  T *m_start = nullptr;
77  size_t m_size = 0;
78  size_t m_capacity = 0;
79 public:
80  RTVector (RTMemoryArea *memory_area) :
81  m_memory_area (memory_area)
82  {
83  }
84  void
85  assign (const std::vector<T>& vec)
86  {
87  assert (m_size == 0 && m_capacity == 0);
88 
89  set_capacity (vec.size());
90  std::copy (vec.begin(), vec.end(), m_start);
91  m_size = vec.size();
92  }
93  void
94  assign (const RTVector<T>& vec)
95  {
96  assert (m_size == 0 && m_capacity == 0);
97 
98  set_capacity (vec.size());
99  std::copy (vec.m_start, vec.m_start + vec.m_size, m_start);
100  m_size = vec.size();
101  }
102  size_t
103  size() const
104  {
105  return m_size;
106  }
107  void
108  set_capacity (size_t capacity)
109  {
110  assert (m_size == 0 && m_capacity == 0);
111 
112  m_start = (T *) m_memory_area->alloc (sizeof (T) * capacity);
113  m_capacity = capacity;
114  }
115  void
116  push_back (const T& t)
117  {
118  assert (m_size < m_capacity);
119  m_start[m_size++] = t;
120  }
121  T&
122  back()
123  {
124  return m_start[m_size - 1];
125  }
126  T&
127  operator[] (size_t idx)
128  {
129  return m_start[idx];
130  }
131  const T&
132  operator[] (size_t idx) const
133  {
134  return m_start[idx];
135  }
136 };
137 
139 {
140 public:
141  RTAudioBlock (RTMemoryArea *memory_area) :
142  freqs (memory_area),
143  mags (memory_area),
144  noise (memory_area)
145  {
146  }
147  void
148  assign (const RTAudioBlock& audio_block)
149  {
150  freqs.assign (audio_block.freqs);
151  mags.assign (audio_block.mags);
152  noise.assign (audio_block.noise);
153  }
154  void
155  assign (const AudioBlock& audio_block)
156  {
157  freqs.assign (audio_block.freqs);
158  mags.assign (audio_block.mags);
159  noise.assign (audio_block.noise);
160  }
161  RTVector<uint16_t> freqs;
162  RTVector<uint16_t> mags;
163  RTVector<uint16_t> noise;
164 
165  double
166  freqs_f (size_t i) const
167  {
168  return sm_ifreq2freq (freqs[i]);
169  }
170 
171  double
172  mags_f (size_t i) const
173  {
174  return sm_idb2factor (mags[i]);
175  }
176 
177  double
178  noise_f (size_t i) const
179  {
180  return sm_idb2factor (noise[i]);
181  }
182 
183  void sort_freqs();
184 };
185 
186 }
Block of audio data, encoded in SpectMorph parametric format.
Definition: smaudio.hh:28
std::vector< uint16_t > noise
noise envelope, representing the original signal minus sine components
Definition: smaudio.hh:30
std::vector< uint16_t > freqs
frequencies of the sine components of this frame
Definition: smaudio.hh:31
std::vector< uint16_t > mags
magnitudes of the sine components
Definition: smaudio.hh:32
Definition: smrtmemory.hh:139
Definition: smrtmemory.hh:16
Definition: smrtmemory.hh:72