Beatmup
signal_fragment.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 //#pragma once
20 #include "../context.h"
21 #include "../memory.h"
22 #include "../exception.h"
23 #include "../fragments/fragment.h"
24 #include "sample_arithmetic.h"
25 #include "signal.h"
26 #include <algorithm>
27 
28 
29 namespace Beatmup {
30 namespace Audio {
31  /**
32  A piece of sound.
33  Audio signals in Beatmup can be fragmented. SignalFragment is a continuous piece of audio signal in memory.
34  A lookup structure is implemented allowing to measure the signal dynamics efficiently.
35 
36  FOR INTERNAL USE, should not be included anywhere except audio_signal.cpp
37  */
39  private:
40 
41  /**
42  Data structure allowing to plot efficiently audio signal graphs
43  */
45  private:
46  DynamicsLookup* prev; //!< previous (finer scale) level
47  void *minmax; //!< (2*size) points per channel, channel-wise multiplexed, interleaved minima and maxima
48  unsigned char channelCount;
49  int size; //!< buffer size in points in one channel, i.e., size in bytes is 2*channelCount*sizeof(magnitude)*size
50  int step; //!< step (block) size in points w.r.t. previous layer (points = samples if there's no previous layer)
51  int stepTime; //!< step (block) size in absolute time units
52 
53  DynamicsLookup(const DynamicsLookup&) = delete; //!< disabling copying constructor
54 
55  public:
56  DynamicsLookup() : prev(nullptr), minmax(nullptr), channelCount(0), size(0), step(0), stepTime(0) {}
58 
59  /**
60  Frees the current level and all previous
61  */
62  void disposeTree();
63 
64  /**
65  Sets up the tree structure
66  \param channelCount Number of channels
67  \param levelCount Number of detail levels
68  \param fineStepSize The most detailed level step size in samples
69  \param coarserStepSize Size of step in points for every upper (less detailed) level
70  */
71  void configureTree(unsigned char channelCount, int levelCount, int fineStepSize, int coarserStepSize);
72 
73 
74  /**
75  Updates tree from raw sample data
76  \param data Pointer to the input data
77  \param sampleCount Number of samples pointed by the data in each channel
78  */
79  template<typename sample> void updateTree(const sample* data, int sampleCount);
80 
81 
82  /**
83  Measures dynamics from time0 to time1 in each channels separately.
84  \param time0 Start time
85  \param time1 Stop time
86  \param min Channelwise multiplexed minima
87  \param max Channelwise multiplexed maxima
88  \param data Channelwise multiplexed sample data for a more precise measurement; may be null
89  */
90  template<typename sample> void measure(dtime time0, dtime time1, sample* min, sample* max, const void* data) const;
91 
92 
93  inline bool isReady() const { return minmax != NULL; }
94  };
95 
96  private:
98  unsigned char channelCount; //!< number of channels
99  unsigned char blockSize; //!< size in bytes of a channelwise-multiplexed sample (block containing 1 sample per channel)
101 
102  struct Plot {
106  } plot;
107 
108  public:
109  SignalFragment(AudioSampleFormat format, unsigned char channels, int samples);
110 
111  virtual SignalFragment* clone() const;
112 
113  inline sample8* getData() { return data.ptr<sample8>(); }
114  inline const AudioSampleFormat getAudioFormat() const { return format; }
115  inline msize getSizeBytes() const { return getSampleCount() * blockSize; }
116  inline unsigned char getBlockSize() const { return blockSize; }
117  inline unsigned char getChannelCount() const { return channelCount; }
118 
119  void zero();
120 
121  inline bool isDynamicsLookupAvailable() const { return plot.lookup.isReady(); }
122  void updateDynamicsLookup();
123 
124  /**
125  Measures dynamics from time0 to time1 in each channels separately.
126  \param time0 Start time
127  \param time1 Stop time
128  \param min Channelwise multiplexed minima
129  \param max Channelwise multiplexed maxima
130  \param mode Measurement mode
131  */
132  template<typename sample> void measureDynamics(int time0, int time1, sample* min, sample* max, Signal::Meter::MeasuringMode mode);
133  };
134 
135 }
136 }
Aligned memory buffer.
Definition: memory.h:27
datatype * ptr(int offset=0)
Definition: memory.h:46
Data structure allowing to plot efficiently audio signal graphs.
void updateTree(const sample *data, int sampleCount)
Updates tree from raw sample data.
void configureTree(unsigned char channelCount, int levelCount, int fineStepSize, int coarserStepSize)
Sets up the tree structure.
int stepTime
step (block) size in absolute time units
DynamicsLookup * prev
previous (finer scale) level
int step
step (block) size in points w.r.t. previous layer (points = samples if there's no previous layer)
void measure(dtime time0, dtime time1, sample *min, sample *max, const void *data) const
Measures dynamics from time0 to time1 in each channels separately.
int size
buffer size in points in one channel, i.e., size in bytes is 2*channelCount*sizeof(magnitude)*size
void * minmax
(2*size) points per channel, channel-wise multiplexed, interleaved minima and maxima
void disposeTree()
Frees the current level and all previous.
DynamicsLookup(const DynamicsLookup &)=delete
disabling copying constructor
unsigned char getChannelCount() const
unsigned char blockSize
size in bytes of a channelwise-multiplexed sample (block containing 1 sample per channel)
SignalFragment(AudioSampleFormat format, unsigned char channels, int samples)
struct Beatmup::Audio::SignalFragment::Plot plot
void measureDynamics(int time0, int time1, sample *min, sample *max, Signal::Meter::MeasuringMode mode)
Measures dynamics from time0 to time1 in each channels separately.
unsigned char channelCount
number of channels
unsigned char getBlockSize() const
virtual SignalFragment * clone() const
const AudioSampleFormat getAudioFormat() const
MeasuringMode
Specifies how to compute signal dynamics (minima and maxima in a given period of time)
Definition: signal.h:76
Represents a continuous set of data samples.
Definition: fragment.h:31
int sampleCount
number of samples within this frame
Definition: fragment.h:39
const int getSampleCount() const
Definition: fragment.h:44
uint32_t msize
memory size
Definition: basic_types.h:30
int dtime
discrete time
Definition: basic_types.h:37
AudioSampleFormat
Format of audio samples.
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 jobject jint jint jint channels
JNIEnv jlong jint mode