Beatmup
Beatmup::Audio::SignalPlot Class Reference

AbstractTask drawing amplitude graph of a given Signal in a bitmap. More...

#include <signal_plot.h>

Inheritance diagram for Beatmup::Audio::SignalPlot:
Beatmup::AbstractTask Beatmup::BitmapContentLock Beatmup::Object

Public Member Functions

 SignalPlot ()
 
void setSignal (Signal *)
 Sets the input signal to plot. More...
 
void setBitmap (AbstractBitmap *)
 Sets the output bitmap. More...
 
void setPlotArea (IntRectangle)
 Specifies a rectangular area in pixels in the output bitmap where the plot will be drawn. More...
 
void setWindow (IntRectangle window, float scale)
 Specifies a time range (X coordinate) and a magnitude range (Y coordinate scaled by scale) that will be plotted. More...
 
void setPalette (color4i bgColor, color4i color1, color4i color2)
 Specifies plot colors. More...
 
void setChannels (int channels)
 Specifies which channels to plot. More...
 
AbstractBitmapgetBitmap () const
 
SignalgetSignal () const
 
- Public Member Functions inherited from Beatmup::AbstractTask
virtual bool processOnGPU (GraphicPipeline &gpu, TaskThread &thread)
 Executes the task on GPU. More...
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 

Protected Member Functions

virtual bool process (TaskThread &thread)
 Executes the task on CPU within a given thread. More...
 
virtual void beforeProcessing (ThreadIndex, ProcessingTarget target, GraphicPipeline *)
 Instruction called before the task is executed. More...
 
virtual void afterProcessing (ThreadIndex, GraphicPipeline *, bool)
 Instruction called after the task is executed. More...
 
virtual TaskDeviceRequirement getUsedDevices () const
 Communicates devices (CPU and/or GPU) the task is run on. More...
 
virtual ThreadIndex getMaxThreads () const
 Gives the upper limint on the number of threads the task may be performed by. More...
 

Private Member Functions

void getPlot (TaskThread &thread, std::vector< int > &data, int &left, int &right)
 
- Private Member Functions inherited from Beatmup::BitmapContentLock
 BitmapContentLock ()
 
 ~BitmapContentLock ()
 
void readLock (GraphicPipeline *gpu, AbstractBitmap *bitmap, ProcessingTarget target)
 Locks content of a bitmap for reading using a specific processing target device. More...
 
void writeLock (GraphicPipeline *gpu, AbstractBitmap *bitmap, ProcessingTarget target)
 Locks content of a bitmap for writing using a specific processing target device. More...
 
void unlock (AbstractBitmap *bitmap)
 Drops a lock to the bitmap. More...
 
void unlockAll ()
 Unlocks all the locked bitmaps unconditionally. More...
 
template<const ProcessingTarget target>
void lock (GraphicPipeline *gpu, AbstractBitmap *input, AbstractBitmap *output)
 
void lock (GraphicPipeline *gpu, ProcessingTarget target, AbstractBitmap *input, AbstractBitmap *output)
 
template<const ProcessingTarget target>
void lock (GraphicPipeline *gpu, std::initializer_list< AbstractBitmap * > read, std::initializer_list< AbstractBitmap * > write)
 
template<typename ... Args>
void unlock (AbstractBitmap *first, Args ... others)
 

Private Attributes

Signalsignal
 
AbstractBitmapbitmap
 
IntRectangle outputRect
 
IntRectangle signalWindow
 
float scale
 
int channels
 
std::vector< int > values
 
struct {
   color4i   bgColor
 
   color4i   color1
 
   color4i   color2
 
palette
 

Additional Inherited Members

- Public Types inherited from Beatmup::AbstractTask
enum class  TaskDeviceRequirement { CPU_ONLY , GPU_OR_CPU , GPU_ONLY }
 Specifies which device (CPU and/or GPU) is used to run the task. More...
 
- Static Public Member Functions inherited from Beatmup::AbstractTask
static ThreadIndex validThreadCount (int number)
 Valid thread count from a given integer value. More...
 

Detailed Description

AbstractTask drawing amplitude graph of a given Signal in a bitmap.

Definition at line 31 of file signal_plot.h.

Constructor & Destructor Documentation

◆ SignalPlot()

SignalPlot::SignalPlot ( )

Definition at line 191 of file signal_plot.cpp.

191  :
192  signal(NULL), bitmap(NULL), outputRect(0,0,100,100), signalWindow(0,-32768,100,32767), scale(1.0f), channels(-1)
193  //fixme: set reasonable values
194 {
195  palette.bgColor = Color::WHITE;
198 }
struct Beatmup::Audio::SignalPlot::@0 palette
AbstractBitmap * bitmap
Definition: signal_plot.h:34
static const color4i DARK_SEA_GREEN2
Definition: constants.h:29
static const color4i DARK_SEA_GREEN1
Definition: constants.h:28
static const color4i WHITE
Definition: constants.h:26

Member Function Documentation

◆ getPlot()

void SignalPlot::getPlot ( TaskThread thread,
std::vector< int > &  data,
int &  left,
int &  right 
)
private

Definition at line 130 of file signal_plot.cpp.

130  {
131  const dtime
132  startTime = signalWindow.a.x + thread.currentThread() * signalWindow.width() / thread.numThreads(),
133  stopTime = signalWindow.a.x + (thread.currentThread() + 1) * signalWindow.width() / thread.numThreads(),
134  length = stopTime - startTime;
135  left = thread.currentThread() * outputRect.width() / thread.numThreads();
136  right = (thread.currentThread() + 1) * outputRect.width() / thread.numThreads();
137 
138  const int width = right - left;
139 
140  const float
141  mag = (float)signalWindow.height(),
142  heightMag = (outputRect.height() - 1) / mag;
143 
144  // measure!
145  const int count = width * signal->getChannelCount();
146  typedef sample16 sample;
147  sample *min = new sample[count], *max = new sample[count];
148  Signal::Meter ptr(*signal, startTime);
149  ptr.measure<sample>(length, width, min, max);
150 
151  // compute bin heights in function of the channel mode
152  // single channel
153  if (0 <= channels && channels < signal->getChannelCount()) {
154  data.reserve(2 * width);
155  sample* pMin = min + channels, *pMax = max + channels;
156  for (int x = 0; x < width; x++) {
157  data.push_back( outputRect.b.y - (int)std::min(std::max(.0f, pMin->x * scale - signalWindow.a.y), mag) * heightMag );
158  data.push_back( outputRect.b.y - (int)std::min(std::max(.0f, pMax->x * scale - signalWindow.a.y), mag) * heightMag );
159  pMin += channels;
160  pMax += channels;
161  }
162  }
163  // combined channels: minmin --- minmax *** maxmin --- maxmax
164  else {
165  data.reserve(4 * width);
166  sample* pMin = min, *pMax = max;
167  for (int x = 0; x < width; x++) {
168  sample ymm = *pMin++, ymM = ymm, yMm = *pMax++, yMM = yMm;
169  for (int ch = 1; ch < signal->getChannelCount(); ch++) {
170  ymm = std::min(ymm, *pMin);
171  ymM = std::max(ymM, *pMin++);
172  yMm = std::min(yMm, *pMax);
173  yMM = std::max(yMM, *pMax++);
174  }
175  if (yMm < ymM) {
176  sample _ = ymM;
177  ymM = yMm;
178  yMm = _;
179  }
180  data.push_back(outputRect.b.y - (int)std::min(std::max(.0f, yMM.x * scale - signalWindow.a.y), mag) * heightMag);
181  data.push_back(outputRect.b.y - (int)std::min(std::max(.0f, yMm.x * scale - signalWindow.a.y), mag) * heightMag);
182  data.push_back(outputRect.b.y - (int)std::min(std::max(.0f, ymM.x * scale - signalWindow.a.y), mag) * heightMag);
183  data.push_back(outputRect.b.y - (int)std::min(std::max(.0f, ymm.x * scale - signalWindow.a.y), mag) * heightMag);
184  }
185  }
186  delete[] min;
187  delete[] max;
188 }
Signal dynamics meter.
Definition: signal.h:71
unsigned char getChannelCount() const
Definition: signal.h:232
numeric height() const
Definition: geometry.h:178
CustomPoint< numeric > b
Definition: geometry.h:131
numeric width() const
Definition: geometry.h:174
CustomPoint< numeric > a
Definition: geometry.h:131
virtual ThreadIndex numThreads() const =0
ThreadIndex currentThread() const
Definition: parallelism.h:165
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
return signal getChannelCount()
JNIEnv jlong jint jint count
jlong jint width
jobject jlong jint x
jlong jlong jint jint jint jint jint left

◆ process()

bool SignalPlot::process ( TaskThread thread)
protectedvirtual

Executes the task on CPU within a given thread.

Generally called by multiple threads.

Parameters
threadassociated task execution context
Returns
true if the execution is finished correctly, false otherwise

Implements Beatmup::AbstractTask.

Definition at line 201 of file signal_plot.cpp.

201  {
202  std::vector<int> yyy;
203  int x0, x1;
204  getPlot(thread, yyy, x0, x1);
205  thread.synchronize();
206  auto y = yyy.begin();
207  // single channel
208  if (0 <= channels && channels < signal->getChannelCount())
209  for (int x = x0; x < x1 && !thread.isTaskAborted(); x++) {
210  int y0 = *y++, y1 = *y++;
211  BitmapProcessing::write<Kernels::DrawBars3>(*bitmap, x, outputRect.a.y, y0, y1, outputRect.b.y, palette.bgColor, palette.color1);
212  }
213  // all channels
214  else
215  for (int x = x0; x < x1 && !thread.isTaskAborted(); x++) {
216  int y0 = *y++, y1 = *y++, y2 = *y++, y3 = *y++;
217  BitmapProcessing::write<Kernels::DrawBars5>(
218  *bitmap, x, outputRect.a.y, y0, y1, y2, y3, outputRect.b.y,
219  palette.bgColor, palette.color1, palette.color2
220  );
221  }
222  return true;
223 }
void getPlot(TaskThread &thread, std::vector< int > &data, int &left, int &right)
virtual void synchronize()=0
Blocks until all the other threads running the same task reach the same point.
virtual bool isTaskAborted() const =0
Returns true if the task is asked to stop from outside.
JNIEnv jlong jint jint jint jint y2
JNIEnv jlong jint x1
JNIEnv jlong jint jint jint y1
jobject jlong jint jint y

◆ beforeProcessing()

void SignalPlot::beforeProcessing ( ThreadIndex  threadCount,
ProcessingTarget  target,
GraphicPipeline gpu 
)
protectedvirtual

Instruction called before the task is executed.

Parameters
threadCountNumber of threads used to perform the task
targetDevice used to perform the task
gpuA graphic pipeline instance; may be null.

Reimplemented from Beatmup::AbstractTask.

Definition at line 226 of file signal_plot.cpp.

226  {
227  NullTaskInput::check(signal, "input signal");
228  NullTaskInput::check(bitmap, "output bitmap");
232 }
void writeLock(GraphicPipeline *gpu, AbstractBitmap *bitmap, ProcessingTarget target)
Locks content of a bitmap for writing using a specific processing target device.
void normalize()
Flips corners coordinates guaranteeing that it has a non negative area, i.e.
Definition: geometry.h:192
static void check(const void *pointer, const char *which)
Definition: exception.h:115

◆ afterProcessing()

void SignalPlot::afterProcessing ( ThreadIndex  threadCount,
GraphicPipeline gpu,
bool  aborted 
)
protectedvirtual

Instruction called after the task is executed.

Parameters
threadCountNumber of threads used to perform the task
gpuGPU to be used to execute the task; may be null.
abortedtrue if the task was aborted

Reimplemented from Beatmup::AbstractTask.

Definition at line 235 of file signal_plot.cpp.

235  {
236  unlock(bitmap);
237 }
void unlock(AbstractBitmap *bitmap)
Drops a lock to the bitmap.

◆ getUsedDevices()

AbstractTask::TaskDeviceRequirement SignalPlot::getUsedDevices ( ) const
protectedvirtual

Communicates devices (CPU and/or GPU) the task is run on.

Reimplemented from Beatmup::AbstractTask.

Definition at line 240 of file signal_plot.cpp.

240  {
242 }
@ CPU_ONLY
this task does not use GPU

◆ getMaxThreads()

ThreadIndex SignalPlot::getMaxThreads ( ) const
protectedvirtual

Gives the upper limint on the number of threads the task may be performed by.

The actual number of threads running a specific task may be less or equal to the returned value, depending on the number of workers in ThreadPool running the task.

Reimplemented from Beatmup::AbstractTask.

Definition at line 245 of file signal_plot.cpp.

245  {
246  BEATMUP_ASSERT_DEBUG(bitmap != nullptr);
247  return validThreadCount(bitmap->getWidth());
248 }
static ThreadIndex validThreadCount(int number)
Valid thread count from a given integer value.
Definition: parallelism.cpp:45
virtual const int getWidth() const =0
Width of the texture in pixels.
#define BEATMUP_ASSERT_DEBUG(C)
Definition: exception.h:163

◆ setSignal()

void SignalPlot::setSignal ( Signal signal)

Sets the input signal to plot.

Definition at line 251 of file signal_plot.cpp.

251  {
252  this->signal = signal;
253 }

◆ setBitmap()

void SignalPlot::setBitmap ( AbstractBitmap bitmap)

Sets the output bitmap.

Definition at line 256 of file signal_plot.cpp.

256  {
257  this->bitmap = bitmap;
258 }

◆ setPlotArea()

void SignalPlot::setPlotArea ( IntRectangle  rectangle)

Specifies a rectangular area in pixels in the output bitmap where the plot will be drawn.

Definition at line 261 of file signal_plot.cpp.

261  {
262  this->outputRect = rectangle;
263 }

◆ setWindow()

void SignalPlot::setWindow ( IntRectangle  window,
float  scale 
)

Specifies a time range (X coordinate) and a magnitude range (Y coordinate scaled by scale) that will be plotted.

Parameters
windowa rectangle in time-value plane containing the two ranges
scalemagnitude scaling factor

Definition at line 266 of file signal_plot.cpp.

266  {
267  this->signalWindow = window;
268  this->scale = scale;
269 }

◆ setPalette()

void SignalPlot::setPalette ( color4i  bgColor,
color4i  color1,
color4i  color2 
)

Specifies plot colors.

Parameters
bgColorbackground color
color1main plotting color
color2second plotting color used when plotting all the channels together (see setChannels())

Definition at line 272 of file signal_plot.cpp.

272  {
273  palette.bgColor = bgColor;
274  palette.color1 = color1;
275  palette.color2 = color2;
276 }

◆ setChannels()

void SignalPlot::setChannels ( int  channels)

Specifies which channels to plot.

Parameters
channelsa channel number (counted from 0) to plot a single channel; any number out of correct range to plot all channels

Definition at line 279 of file signal_plot.cpp.

279  {
280  this->channels = channels;
281 }

◆ getBitmap()

AbstractBitmap* Beatmup::Audio::SignalPlot::getBitmap ( ) const
inline

Definition at line 98 of file signal_plot.h.

98 { return bitmap; }

◆ getSignal()

Signal* Beatmup::Audio::SignalPlot::getSignal ( ) const
inline

Definition at line 99 of file signal_plot.h.

99 { return signal; }

Member Data Documentation

◆ signal

Signal* Beatmup::Audio::SignalPlot::signal
private

Definition at line 33 of file signal_plot.h.

◆ bitmap

AbstractBitmap* Beatmup::Audio::SignalPlot::bitmap
private

Definition at line 34 of file signal_plot.h.

◆ outputRect

IntRectangle Beatmup::Audio::SignalPlot::outputRect
private

Definition at line 35 of file signal_plot.h.

◆ signalWindow

IntRectangle Beatmup::Audio::SignalPlot::signalWindow
private

Definition at line 36 of file signal_plot.h.

◆ scale

float Beatmup::Audio::SignalPlot::scale
private

Definition at line 37 of file signal_plot.h.

◆ channels

int Beatmup::Audio::SignalPlot::channels
private

Definition at line 38 of file signal_plot.h.

◆ values

std::vector<int> Beatmup::Audio::SignalPlot::values
private

Definition at line 40 of file signal_plot.h.

◆ bgColor

color4i Beatmup::Audio::SignalPlot::bgColor

Definition at line 43 of file signal_plot.h.

◆ color1

color4i Beatmup::Audio::SignalPlot::color1

Definition at line 43 of file signal_plot.h.

◆ color2

color4i Beatmup::Audio::SignalPlot::color2

Definition at line 43 of file signal_plot.h.

◆ 

struct { ... } Beatmup::Audio::SignalPlot::palette

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