File: Mul.cpp

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (180 lines) | stat: -rw-r--r-- 5,271 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/***************************************************************************
                Mul.cpp  -  multiplier
                             -------------------
    begin                : Thu Nov 01 2007
    copyright            : (C) 2007 by Thomas Eschenbacher
    email                : Thomas.Eschenbacher@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "config.h"

#include <QMutexLocker>
#include <QVariant>

#include "libkwave/Sample.h"
#include "libkwave/SampleArray.h"
#include "libkwave/modules/Mul.h"

/***************************************************************************/
Kwave::Mul::Mul()
    :Kwave::SampleSource(),
     m_queue_a(), m_queue_b(),
     m_sem_a(0), m_sem_b(0),
     m_a(), m_b(),
     m_buffer_x(blockSize()),
     m_a_is_const(false), m_b_is_const(false),
     m_value_a(0), m_value_b(0),
     m_lock()
{
}

/***************************************************************************/
Kwave::Mul::~Mul()
{
}

/***************************************************************************/
void Kwave::Mul::goOn()
{
}

/***************************************************************************/
void Kwave::Mul::multiply()
{
    unsigned int count = blockSize();
    float a = 0;
    float b = 0;
    const sample_t *p_a = nullptr;
    const sample_t *p_b = nullptr;
    sample_t       *p_x = nullptr;

    // get input A
    if (!m_a_is_const) {
        m_sem_a.acquire();
        {
            QMutexLocker lock(&m_lock);
            m_a = m_queue_a.dequeue();
        }
        if (m_a.size() < count) count = m_a.size();
    }
    else
    {
        QMutexLocker lock(&m_lock);
        a = m_value_a;
    }

    // get input B
    if (!m_b_is_const) {
        m_sem_b.acquire();
        {
            QMutexLocker lock(&m_lock);
            m_b = m_queue_b.dequeue();
        }
        if (m_b.size() < count) count = m_b.size();
    }
    else
    {
        QMutexLocker lock(&m_lock);
        b = m_value_b;
    }

    // check sizes of the buffers
    if (!m_a_is_const && (count > m_a.size()))
        count = m_a.size();
    if (!m_b_is_const && (count > m_b.size()))
        count = m_b.size();

    // special handling for zero length input
    if (!count) {
        emit output(Kwave::SampleArray()); // emit zero length output
        return;                            // and bail out
    }

//     if (!m_a_is_const && !m_b_is_const && (m_a.size() != m_b.size()))
//      qWarning("Kwave::Mul: block sizes differ: %u x %u -> shrinked to %u",
//          m_a.size(), m_b.size(), count);

    bool ok = m_buffer_x.resize(count);
    Q_ASSERT(ok);
    Q_UNUSED(ok)

    // get pointers to the buffer's raw data
    p_a = m_a.constData();
    p_b = m_b.constData();
    p_x = m_buffer_x.data();
    Q_ASSERT((m_a_is_const || p_a) && (m_b_is_const || p_b) && p_x);
    if ((!m_a_is_const && !p_a) || (!m_b_is_const && !p_b) || !p_x)
        return;

    // do the multiplication of the whole buffer
    for (; count; count--)
    {
        if (!m_a_is_const) { a = sample2float(*p_a); ++p_a; }
        if (!m_b_is_const) { b = sample2float(*p_b); ++p_b; }
        float y = a * b;
        if (y > float( 1.0)) y = float( 1.0);
        if (y < float(-1.0)) y = float(-1.0);
        *(p_x++) = float2sample(y);
    }

    // emit the result
    emit output(m_buffer_x);
}

/***************************************************************************/
void Kwave::Mul::input_a(Kwave::SampleArray data)
{
    {
        QMutexLocker lock(&m_lock);

        m_queue_a.enqueue(data);
        m_sem_a.release();
        m_a_is_const = false;
    }
    if (m_b_is_const || !m_queue_b.isEmpty()) multiply();
}

/***************************************************************************/
void Kwave::Mul::input_b(Kwave::SampleArray data)
{
    {
        QMutexLocker lock(&m_lock);

        m_queue_b.enqueue(data);
        m_sem_b.release();
        m_b_is_const = false;
    }
    if (m_a_is_const || !m_queue_a.isEmpty()) multiply();
}

/***************************************************************************/
void Kwave::Mul::set_a(const QVariant &a)
{
    QMutexLocker lock(&m_lock);

    m_value_a = QVariant(a).toFloat();
    m_a_is_const = true;
}

/***************************************************************************/
void Kwave::Mul::set_b(const QVariant &b)
{
    QMutexLocker lock(&m_lock);

    m_value_b = QVariant(b).toFloat();
    m_b_is_const = true;
}

/***************************************************************************/
/***************************************************************************/

#include "moc_Mul.cpp"