kwave  18.07.70
Mul.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  Mul.cpp - multiplier
3  -------------------
4  begin : Thu Nov 01 2007
5  copyright : (C) 2007 by Thomas Eschenbacher
6  email : Thomas.Eschenbacher@gmx.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "config.h"
19 
20 #include <QMutexLocker>
21 #include <QVariant>
22 
23 #include "libkwave/Sample.h"
24 #include "libkwave/SampleArray.h"
25 #include "libkwave/modules/Mul.h"
26 
27 /***************************************************************************/
29  :Kwave::SampleSource(),
30  m_queue_a(), m_queue_b(),
31  m_sem_a(0), m_sem_b(0),
32  m_a(), m_b(),
33  m_buffer_x(blockSize()),
34  m_a_is_const(false), m_b_is_const(false),
35  m_value_a(0), m_value_b(0),
36  m_lock()
37 {
38 }
39 
40 /***************************************************************************/
42 {
43 }
44 
45 /***************************************************************************/
47 {
48 }
49 
50 /***************************************************************************/
52 {
53  unsigned int count = blockSize();
54  float a = 0;
55  float b = 0;
56  const sample_t *p_a = Q_NULLPTR;
57  const sample_t *p_b = Q_NULLPTR;
58  sample_t *p_x = Q_NULLPTR;
59 
60  // get input A
61  if (!m_a_is_const) {
62  m_sem_a.acquire();
63  {
64  QMutexLocker lock(&m_lock);
65  m_a = m_queue_a.dequeue();
66  }
67  if (m_a.size() < count) count = m_a.size();
68  }
69  else
70  {
71  QMutexLocker lock(&m_lock);
72  a = m_value_a;
73  }
74 
75  // get input B
76  if (!m_b_is_const) {
77  m_sem_b.acquire();
78  {
79  QMutexLocker lock(&m_lock);
80  m_b = m_queue_b.dequeue();
81  }
82  if (m_b.size() < count) count = m_b.size();
83  }
84  else
85  {
86  QMutexLocker lock(&m_lock);
87  b = m_value_b;
88  }
89 
90  // check sizes of the buffers
91  if (!m_a_is_const && (count > m_a.size()))
92  count = m_a.size();
93  if (!m_b_is_const && (count > m_b.size()))
94  count = m_b.size();
95 
96  // special handling for zero length input
97  if (!count) {
98  emit output(Kwave::SampleArray()); // emit zero length output
99  return; // and bail out
100  }
101 
102 // if (!m_a_is_const && !m_b_is_const && (m_a.size() != m_b.size()))
103 // qWarning("Kwave::Mul: block sizes differ: %u x %u -> shrinked to %u",
104 // m_a.size(), m_b.size(), count);
105 
106  bool ok = m_buffer_x.resize(count);
107  Q_ASSERT(ok);
108  Q_UNUSED(ok);
109 
110  // get pointers to the buffer's raw data
111  p_a = m_a.constData();
112  p_b = m_b.constData();
113  p_x = m_buffer_x.data();
114  Q_ASSERT((m_a_is_const || p_a) && (m_b_is_const || p_b) && p_x);
115  if ((!m_a_is_const && !p_a) || (!m_b_is_const && !p_b) || !p_x)
116  return;
117 
118  // do the multiplication of the whole buffer
119  for (; count; count--)
120  {
121  if (!m_a_is_const) { a = sample2float(*p_a); ++p_a; }
122  if (!m_b_is_const) { b = sample2float(*p_b); ++p_b; }
123  float y = a * b;
124  if (y > float( 1.0)) y = float( 1.0);
125  if (y < float(-1.0)) y = float(-1.0);
126  *(p_x++) = float2sample(y);
127  }
128 
129  // emit the result
130  emit output(m_buffer_x);
131 }
132 
133 /***************************************************************************/
135 {
136  {
137  QMutexLocker lock(&m_lock);
138 
139  m_queue_a.enqueue(data);
140  m_sem_a.release();
141  m_a_is_const = false;
142  }
143  if (m_b_is_const || !m_queue_b.isEmpty()) multiply();
144 }
145 
146 /***************************************************************************/
148 {
149  {
150  QMutexLocker lock(&m_lock);
151 
152  m_queue_b.enqueue(data);
153  m_sem_b.release();
154  m_b_is_const = false;
155  }
156  if (m_a_is_const || !m_queue_a.isEmpty()) multiply();
157 }
158 
159 /***************************************************************************/
160 void Kwave::Mul::set_a(const QVariant &a)
161 {
162  QMutexLocker lock(&m_lock);
163 
164  m_value_a = QVariant(a).toFloat();
165  m_a_is_const = true;
166 }
167 
168 /***************************************************************************/
169 void Kwave::Mul::set_b(const QVariant &b)
170 {
171  QMutexLocker lock(&m_lock);
172 
173  m_value_b = QVariant(b).toFloat();
174  m_b_is_const = true;
175 }
176 
177 /***************************************************************************/
178 /***************************************************************************/
QSemaphore m_sem_a
Definition: Mul.h:82
Definition: App.h:33
virtual ~Mul() Q_DECL_OVERRIDE
Definition: Mul.cpp:41
Kwave::SampleArray m_b
Definition: Mul.h:91
void input_a(Kwave::SampleArray data)
Definition: Mul.cpp:134
void input_b(Kwave::SampleArray data)
Definition: Mul.cpp:147
Kwave::SampleArray m_a
Definition: Mul.h:88
virtual void goOn() Q_DECL_OVERRIDE
Definition: Mul.cpp:46
bool m_b_is_const
Definition: Mul.h:100
float m_value_a
Definition: Mul.h:103
void output(Kwave::SampleArray data)
Kwave::SampleArray m_buffer_x
Definition: Mul.h:94
static float sample2float(const sample_t s)
Definition: Sample.h:65
QSemaphore m_sem_b
Definition: Mul.h:85
void set_b(const QVariant &b)
Definition: Mul.cpp:169
const sample_t * constData() const
Definition: SampleArray.h:54
float m_value_b
Definition: Mul.h:106
virtual void multiply()
Definition: Mul.cpp:51
Mul()
Definition: Mul.cpp:28
virtual unsigned int blockSize() const
void set_a(const QVariant &a)
Definition: Mul.cpp:160
unsigned int size() const
QMutex m_lock
Definition: Mul.h:109
sample_t * data()
Definition: SampleArray.h:62
QQueue< Kwave::SampleArray > m_queue_a
Definition: Mul.h:76
static sample_t float2sample(const float f)
Definition: Sample.h:57
QQueue< Kwave::SampleArray > m_queue_b
Definition: Mul.h:79
bool resize(unsigned int size) Q_REQUIRED_RESULT
bool m_a_is_const
Definition: Mul.h:97
qint32 sample_t
Definition: Sample.h:37