Beatmup
Beatmup::Audio::Signal::Meter Class Reference

Signal dynamics meter. More...

#include <signal.h>

Inheritance diagram for Beatmup::Audio::Signal::Meter:
Beatmup::Audio::Signal::Pointer

Public Types

enum class  MeasuringMode { preciseUsingSamples , approximateUsingLookup , preciseUsingLookupAndSamples }
 Specifies how to compute signal dynamics (minima and maxima in a given period of time) More...
 

Public Member Functions

 Meter (Signal &signal, dtime time, MeasuringMode mode=MeasuringMode::approximateUsingLookup)
 Constructs a new meter. More...
 
template<typename sample >
void measure (dtime len, int resolution, sample min[], sample max[])
 Measures signal dynamics in a given period of time. More...
 
void setMode (MeasuringMode newMode)
 
template<>
void measure (dtime len, int resolution, sample16 min[], sample16 max[])
 
- Public Member Functions inherited from Beatmup::Audio::Signal::Pointer
 Pointer (Signal &signal, dtime time, bool writing)
 
virtual void releaseBuffer ()
 
unsigned char getChannelCount () const
 

Static Public Member Functions

static void prepareSignal (Signal &signal, bool runTask=true)
 Precomputes the dynamics lookup all over the signal, where needed. More...
 

Private Member Functions

template<typename sample >
void measureInFragments (dtime len, int resolution, sample *min, sample *max)
 Measures signal dynamics in a given period of time. More...
 

Static Private Member Functions

static void prepare (Signal &signal, int skipOnStart=0, int numSteps=1)
 

Private Attributes

MeasuringMode mode
 

Detailed Description

Signal dynamics meter.

Definition at line 71 of file signal.h.

Member Enumeration Documentation

◆ MeasuringMode

Specifies how to compute signal dynamics (minima and maxima in a given period of time)

Enumerator
preciseUsingSamples 

Just run across all samples.

This does not require neither precomputing nor extra memory, but slow as hell.

approximateUsingLookup 

Find an approximated range using a lookup tree.

It is guaranteed that the resulting approximated dynamic range covers the precise range. Very fast for less fragmented signals, does not hit the sample data in memory, but requires precomputing and increases the memory footprint.

preciseUsingLookupAndSamples 

Use lookup and then precise the measurement using sample data.

Generally fast, except highly fragmented signals. Requires precomputing and increases the memory footprint (same as approximateUsingLookup);

Definition at line 76 of file signal.h.

76  {
77  /**
78  Just run across all samples.
79  This does not require neither precomputing nor extra memory, but slow as hell.
80  */
81  preciseUsingSamples,
82 
83  /**
84  Find an approximated range using a lookup tree.
85  It is guaranteed that the resulting approximated dynamic range covers the precise range. Very fast for less
86  fragmented signals, does not hit the sample data in memory, but requires precomputing and increases the
87  memory footprint.
88  */
89  approximateUsingLookup,
90 
91  /**
92  Use lookup and then precise the measurement using sample data.
93  Generally fast, except highly fragmented signals. Requires precomputing and increases the memory footprint
94  (same as `approximateUsingLookup`);
95  */
96  preciseUsingLookupAndSamples
97  };

Constructor & Destructor Documentation

◆ Meter()

Signal::Meter::Meter ( Signal signal,
dtime  time,
MeasuringMode  mode = MeasuringMode::approximateUsingLookup 
)

Constructs a new meter.

Parameters
signalThe signal to measure
timeInitial time to start measurements from
modeAlgorithm used to measure the signal dynamics

Definition at line 197 of file signal.cpp.

197  :
198  Pointer(signal, time, false), mode(mode)
199 {}
Pointer(Signal &signal, dtime time, bool writing)
Definition: signal.cpp:143
jlong jlong jint time

Member Function Documentation

◆ measureInFragments()

template<typename sample >
template void Signal::Meter::measureInFragments ( dtime  len,
int  resolution,
sample *  min,
sample *  max 
)
private

Measures signal dynamics in a given period of time.

Deals with the fragmentation. The sample type must be coherent to the actual sample format of the signal.

Parameters
lenPeriod length in samples
resolutionNumber of output points
minChannelwise multiplexed magnitude minima, (resolution) points per channel
maxChannelwise multiplexed magnitude maxima, (resolution) points per channel

Definition at line 227 of file signal.cpp.

227  {
228  const int channelCount = ((SignalFragment*)pointer.fragment)->getChannelCount();
229  const dtime startTime = getTime();
230  dtime t1 = startTime;
231  sample *pMin = min, *pMax = max;
232 
233  // for all bins
234  for (int bin = 1; bin <= resolution; bin++) {
235  // determining bin bounds
236  dtime t0 = t1;
237  t1 = startTime + (long long)len * bin / resolution;
238 
239  // determining start time within fragment
240  dtime fragStart = t0 - getTime() + pointer.offset;
241 
242  // initializing min and max values
243  for (int ch = 0; ch < channelCount; ch++) {
244  pMin[ch].x = sample::MAX_VALUE;
245  pMax[ch].x = sample::MIN_VALUE;
246  }
247 
248  // scanning fragments
249  while (getTime() + pointer.length < t1) {
250  ((SignalFragment*)pointer.fragment)->measureDynamics(fragStart, pointer.offset + pointer.length, pMin, pMax, mode);
251  step();
252  fragStart = pointer.offset;
253  }
254  ((SignalFragment*)pointer.fragment)->measureDynamics(fragStart, pointer.offset + t1 - getTime(), pMin, pMax, mode);
255 
256  // if no values defined
257  for (int ch = 0; ch < channelCount; ch++)
258  if (pMax[ch] < pMin[ch])
259  pMin[ch].x = pMax[ch].x = 0;
260  pMin += channelCount;
261  pMax += channelCount;
262  }
263 }
unsigned char channelCount
number of channels
Definition: signal.h:191
int dtime
discrete time
Definition: basic_types.h:37
CustomPoint< numeric > min(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:724
CustomPoint< numeric > max(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:728
JNIEnv jlong jint t1
jlong jint jfloat step

◆ prepare()

void Signal::Meter::prepare ( Signal signal,
int  skipOnStart = 0,
int  numSteps = 1 
)
staticprivate

Definition at line 183 of file signal.cpp.

183  {
185  for (int skip = 0; skip < skipOnStart; skip++)
186  me.step();
187  while (me.hasData()) {
188  SignalFragment* fragment = (SignalFragment*)me.pointer.fragment;
189  if (fragment && !fragment->isDynamicsLookupAvailable())
190  fragment->updateDynamicsLookup();
191  for (int skip = 0; skip < numSteps; skip++)
192  me.step();
193  }
194 }
Meter(Signal &signal, dtime time, MeasuringMode mode=MeasuringMode::approximateUsingLookup)
Constructs a new meter.
Definition: signal.cpp:197
@ approximateUsingLookup
Find an approximated range using a lookup tree.
JNIEnv jobject jint jint jint jfloat fragment
if(nativeObj)

◆ prepareSignal()

void Signal::Meter::prepareSignal ( Signal signal,
bool  runTask = true 
)
static

Precomputes the dynamics lookup all over the signal, where needed.

Parameters
signalThe signal to process
runTaskIf true, a task will be run in the context of the signal allowing to process it in parallel; otherwise the computation is done directly in the calling thread

Definition at line 202 of file signal.cpp.

202  {
203  if (runTask) {
204 
205  class DynamicsLookupPreparation : public AbstractTask {
206  public:
207  Signal& signal;
208  bool process(TaskThread& thread) {
209  // interleaving fragments among threads
210  Meter::prepare(signal, thread.currentThread(), thread.numThreads());
211  return true;
212  }
213 
214  TaskDeviceRequirement getExecutionMode() const { return AbstractTask::TaskDeviceRequirement::CPU_ONLY; }
215  ThreadIndex getMaxThreads() const { return MAX_THREAD_INDEX; }
216  DynamicsLookupPreparation(Signal& signal) : signal(signal) {}
217  };
218 
219  DynamicsLookupPreparation task(signal);
220  signal.getContext().performTask(task);
221  }
222  else
223  Meter::prepare(signal);
224 }
Task: an operation that can be executed by multiple threads in parallel.
Definition: parallelism.h:90
@ CPU_ONLY
this task does not use GPU
static void prepare(Signal &signal, int skipOnStart=0, int numSteps=1)
Definition: signal.cpp:183
An audio signal.
Definition: signal.h:36
Context & getContext() const
Definition: signal.h:242
float performTask(AbstractTask &task, const PoolIndex pool=DEFAULT_POOL)
Performs a given task.
Definition: context.cpp:240
Thread executing tasks.
Definition: parallelism.h:154
virtual ThreadIndex numThreads() const =0
ThreadIndex currentThread() const
Definition: parallelism.h:165
unsigned char ThreadIndex
number of threads / thread index
Definition: parallelism.h:68
static const ThreadIndex MAX_THREAD_INDEX
maximum possible thread index value
Definition: parallelism.h:71
Beatmup::NNets::InferenceTask * task

◆ measure() [1/2]

template<typename sample >
void Beatmup::Audio::Signal::Meter::measure ( dtime  len,
int  resolution,
sample  min[],
sample  max[] 
)

Measures signal dynamics in a given period of time.

Parameters
lenPeriod length in samples
resolutionNumber of output points
minChannelwise multiplexed magnitude minima, (resolution) points per channel
maxChannelwise multiplexed magnitude maxima, (resolution) points per channel

◆ setMode()

void Beatmup::Audio::Signal::Meter::setMode ( MeasuringMode  newMode)
inline

Definition at line 140 of file signal.h.

140 { mode = newMode; }

◆ measure() [2/2]

template<>
void Beatmup::Audio::Signal::Meter::measure ( dtime  len,
int  resolution,
sample16  min[],
sample16  max[] 
)

Definition at line 273 of file signal.cpp.

273  {
274  const AudioSampleFormat fmt = ((SignalFragment*)pointer.fragment)->getAudioFormat();
275  if (fmt == Int16) {
276  measureInFragments(len, resolution, (sample16*)min, (sample16*)max);
277  return;
278  }
279  // converting min/max types
280  const int size = resolution * ((SignalFragment*)pointer.fragment)->getChannelCount();
281  switch (fmt) {
282  case Int8: {
283  sample8 *tmin = new sample8[size], *tmax = new sample8[size];
284  measureInFragments(len, resolution, tmin, tmax);
285  convertSamples<sample8, sample16>(tmin, min, resolution);
286  convertSamples<sample8, sample16>(tmax, max, resolution);
287  delete[] tmin;
288  delete[] tmax;
289  break;
290  }
291  case Int32: {
292  sample32 *tmin = new sample32[size], *tmax = new sample32[size];
293  measureInFragments(len, resolution, tmin, tmax);
294  convertSamples<sample32, sample16>(tmin, min, resolution);
295  convertSamples<sample32, sample16>(tmax, max, resolution);
296  delete[] tmin;
297  delete[] tmax;
298  break;
299  }
300  case Float32: {
301  sample32f *tmin = new sample32f[size], *tmax = new sample32f[size];
302  measureInFragments(len, resolution, tmin, tmax);
303  convertSamples<sample32f, sample16>(tmin, min, resolution);
304  convertSamples<sample32f, sample16>(tmax, max, resolution);
305  delete[] tmin;
306  delete[] tmax;
307  break;
308  }
309  default:
310  Insanity::insanity("Unknown audio format");
311  }
312  }
void measureInFragments(dtime len, int resolution, sample *min, sample *max)
Measures signal dynamics in a given period of time.
Definition: signal.cpp:227
static void insanity(const char *message)
Definition: exception.h:136
AudioSampleFormat
Format of audio samples.
@ Int8
signed integer, 8 bit per sample
@ Int32
signed integer, 32 bit per sample
@ Float32
floating point, 32 bit per sample
@ Int16
signed integer, 16 bit per sample
jlong jobject size

Member Data Documentation

◆ mode

MeasuringMode Beatmup::Audio::Signal::Meter::mode
private

Definition at line 112 of file signal.h.


The documentation for this class was generated from the following files: