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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
/***************************************************************************
PitchShiftFilter.cpp - filter for modifying the "pitch_shift"
-------------------
begin : Wed Nov 28 2007
copyright : (C) 2007 by Thomas Eschenbacher
email : Thomas.Eschenbacher@gmx.de
based on synth_pitch_shift_impl.cc from the aRts project
copyright (C) 2000 Jeff Tranter <tranter@pobox.com>
(C) 1999 Stefan Westerfeld <stefan@space.twc.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 <math.h>
#include "libkwave/Sample.h"
#include "libkwave/Utils.h"
#include "PitchShiftFilter.h"
//***************************************************************************
Kwave::PitchShiftFilter::PitchShiftFilter()
:Kwave::SampleSource(nullptr), m_buffer(blockSize()),
m_speed(1.0), m_frequency(0.5), m_dbuffer(),
m_lfopos(0), m_b1pos(0), m_b2pos(0), m_b1inc(0), m_b2inc(0),
m_b1reset(false), m_b2reset(false), m_dbpos(0)
{
initFilter();
}
//***************************************************************************
Kwave::PitchShiftFilter::~PitchShiftFilter()
{
}
//***************************************************************************
void Kwave::PitchShiftFilter::goOn()
{
emit output(m_buffer);
}
//***************************************************************************
void Kwave::PitchShiftFilter::initFilter()
{
m_dbuffer.resize(MAXDELAY);
for (m_dbpos = 0; m_dbpos < MAXDELAY; m_dbpos++)
m_dbuffer[m_dbpos] = 0;
m_dbpos = 0;
m_lfopos = 0;
if (m_speed <= float(1.0)) {
m_b1pos = m_b2pos = 0.0;
m_b1inc = m_b2inc = 1.0f - m_speed;
} else {
/* not yet sure what would be a nice initialization here? */
m_b1pos = m_b2pos = 0.0;
m_b1inc = m_b2inc = 0.0;
}
}
//***************************************************************************
void Kwave::PitchShiftFilter::input(Kwave::SampleArray data)
{
const Kwave::SampleArray &in = data;
Q_ASSERT(Kwave::toInt(in.size()) <= m_dbuffer.size());
bool ok = m_buffer.resize(in.size());
Q_ASSERT(ok);
Q_UNUSED(ok)
const float pi2 = 2 * float(M_PI);
const float lfoposinc = static_cast<float>(m_frequency);
for (unsigned int pos = 0; pos < m_buffer.size(); pos++) {
/*
* fill delay buffer with the input signal
*/
m_dbuffer[m_dbpos] = sample2float(in[pos]);
m_lfopos += lfoposinc;
m_lfopos -= floorf(m_lfopos);
if (m_lfopos < float(0.25)) {
m_b1reset = m_b2reset = false;
}
/*
* _speed < 1.0 (downpitching)
*
* start with current sample and increase delay slowly
*
* _speed > 1.0 (uppitching)
*
* start with a sample from long ago and slowly decrease delay
*/
if (!m_b1reset && m_lfopos > float(0.25)) {
if (m_speed <= float(1.0)) {
m_b1pos = 0;
m_b1inc = 1.0f - m_speed;
} else {
m_b1inc = 1.0f - m_speed;
m_b1pos = 10.0f + ((- m_b1inc) * (1.0f / lfoposinc));
/* 10+ are not strictly necessary */
}
m_b1reset = true;
}
if (!m_b2reset && (m_lfopos > 0.75f)) {
if (m_speed <= float(1.0)) {
m_b2pos = 0;
m_b2inc = 1.0f - m_speed;
} else{
m_b2inc = 1.0f - m_speed;
m_b2pos = 10.0f + ((-m_b2inc) * (1.0f / lfoposinc));
/* 10+ are not strictly necessary */
}
m_b2reset = true;
}
m_b1pos += m_b1inc;
m_b2pos += m_b2inc;
int position, position1;
float error, int_pos;
/*
* Interpolate value from buffer position 1
*/
error = modff(m_b1pos, &int_pos);
position = m_dbpos - Kwave::toInt(int_pos);
if (position < 0)
position += MAXDELAY;
position1 = position - 1;
if (position1 < 0)
position1 += MAXDELAY;
const float b1value = m_dbuffer[position] * (1 - error) +
m_dbuffer[position1] * error;
/*
* Interpolate value from buffer position 2
*/
error = modff(m_b2pos,&int_pos);
position = m_dbpos - Kwave::toInt(int_pos);
if (position < 0)
position += MAXDELAY;
position1 = position-1;
if ( position1 < 0)
position1 += MAXDELAY;
const float b2value = m_dbuffer[position] * (1 - error) +
m_dbuffer[position1] * error;
/*
* Calculate output signal from these two buffers
*/
const float lfo = (sinf(pi2 * m_lfopos) + 1.0f) / 2.0f;
/* position sin lfo variable
*------------------------------------------------------------------
* lfo value: 0.25 1 1 => buffer 2 is used
* 0.75 -1 0 => buffer 1 is used
*/
m_buffer[pos] = float2sample(b1value * (1.0f - lfo) + b2value * lfo);
/*
* increment delay buffer position
*/
m_dbpos++;
if (m_dbpos == MAXDELAY)
m_dbpos = 0;
}
}
//***************************************************************************
void Kwave::PitchShiftFilter::setSpeed(const QVariant speed)
{
float new_speed = QVariant(speed).toFloat();
if (qFuzzyCompare(new_speed, m_speed)) return; // nothing to do
m_speed = new_speed;
initFilter();
}
//***************************************************************************
void Kwave::PitchShiftFilter::setFrequency(const QVariant freq)
{
float new_freq = QVariant(freq).toFloat();
if (qFuzzyCompare(new_freq, m_frequency)) return; // nothing to do
m_frequency = new_freq;
initFilter();
}
//***************************************************************************
//***************************************************************************
#include "moc_PitchShiftFilter.cpp"
|