kwave  18.07.70
Kwave::PitchShiftFilter Class Reference

#include <PitchShiftFilter.h>

Inheritance diagram for Kwave::PitchShiftFilter:
Inheritance graph
Collaboration diagram for Kwave::PitchShiftFilter:
Collaboration graph

Public Slots

void input (Kwave::SampleArray data)
 
void setSpeed (const QVariant speed)
 
void setFrequency (const QVariant freq)
 

Signals

void output (Kwave::SampleArray data)
 
- Signals inherited from Kwave::StreamObject
void attributeChanged (const QVariant value)
 

Public Member Functions

 PitchShiftFilter ()
 
virtual ~PitchShiftFilter () Q_DECL_OVERRIDE
 
virtual void goOn () Q_DECL_OVERRIDE
 
- Public Member Functions inherited from Kwave::SampleSource
 SampleSource (QObject *parent=Q_NULLPTR)
 
virtual ~SampleSource ()
 
virtual bool done () const
 
- Public Member Functions inherited from Kwave::StreamObject
 StreamObject (QObject *parent=Q_NULLPTR)
 
virtual ~StreamObject ()
 
virtual unsigned int tracks () const
 
virtual Kwave::StreamObjectoperator[] (unsigned int track)
 
virtual unsigned int tracksOfPort (const char *port) const
 
virtual Kwave::StreamObjectport (const char *port, unsigned int track)
 
virtual unsigned int blockSize () const
 
void setAttribute (const char *attribute, const QVariant &value)
 

Private Types

enum  { MAXDELAY = 1000000 }
 

Private Member Functions

void initFilter ()
 

Private Attributes

Kwave::SampleArray m_buffer
 
float m_speed
 
float m_frequency
 
QVector< float > m_dbuffer
 
float m_lfopos
 
float m_b1pos
 
float m_b2pos
 
float m_b1inc
 
float m_b2inc
 
bool m_b1reset
 
bool m_b2reset
 
int m_dbpos
 

Additional Inherited Members

- Static Public Member Functions inherited from Kwave::StreamObject
static void setInteractive (bool interactive)
 

Detailed Description

Definition at line 38 of file PitchShiftFilter.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
private
Enumerator
MAXDELAY 

Definition at line 90 of file PitchShiftFilter.h.

Constructor & Destructor Documentation

◆ PitchShiftFilter()

Kwave::PitchShiftFilter::PitchShiftFilter ( )

Constructor

Definition at line 34 of file PitchShiftFilter.cpp.

References initFilter().

Here is the call graph for this function:

◆ ~PitchShiftFilter()

Kwave::PitchShiftFilter::~PitchShiftFilter ( )
virtual

Destructor

Definition at line 44 of file PitchShiftFilter.cpp.

45 {
46 }

Member Function Documentation

◆ goOn()

void Kwave::PitchShiftFilter::goOn ( )
virtual

does the calculation

Implements Kwave::SampleSource.

Definition at line 49 of file PitchShiftFilter.cpp.

References m_buffer, and output().

50 {
51  emit output(m_buffer);
52 }
Kwave::SampleArray m_buffer
void output(Kwave::SampleArray data)

◆ initFilter()

void Kwave::PitchShiftFilter::initFilter ( )
private

reset/initialize the filter and buffer

Definition at line 55 of file PitchShiftFilter.cpp.

References m_b1inc, m_b1pos, m_b2inc, m_b2pos, m_dbpos, m_dbuffer, m_lfopos, m_speed, and MAXDELAY.

Referenced by PitchShiftFilter(), setFrequency(), and setSpeed().

56 {
57  m_dbuffer.resize(MAXDELAY);
58  for (m_dbpos = 0; m_dbpos < MAXDELAY; m_dbpos++)
59  m_dbuffer[m_dbpos] = 0;
60 
61  m_dbpos = 0;
62  m_lfopos = 0;
63 
64  if (m_speed <= float(1.0)) {
65  m_b1pos = m_b2pos = 0.0;
66  m_b1inc = m_b2inc = 1.0f - m_speed;
67  } else {
68  /* not yet sure what would be a nice initialization here? */
69  m_b1pos = m_b2pos = 0.0;
70  m_b1inc = m_b2inc = 0.0;
71  }
72 }
QVector< float > m_dbuffer
Here is the caller graph for this function:

◆ input

void Kwave::PitchShiftFilter::input ( Kwave::SampleArray  data)
slot

receives input data

Definition at line 75 of file PitchShiftFilter.cpp.

References float2sample(), m_b1inc, m_b1pos, m_b1reset, m_b2inc, m_b2pos, m_b2reset, m_buffer, m_dbpos, m_dbuffer, m_frequency, m_lfopos, m_speed, MAXDELAY, Kwave::SampleArray::resize(), sample2float(), Kwave::SampleArray::size(), and Kwave::toInt().

76 {
77  const Kwave::SampleArray &in = data;
78 
79  Q_ASSERT(Kwave::toInt(in.size()) <= m_dbuffer.size());
80  bool ok = m_buffer.resize(in.size());
81  Q_ASSERT(ok);
82  Q_UNUSED(ok);
83 
84  const float pi2 = 2 * float(M_PI);
85  const float lfoposinc = static_cast<float>(m_frequency);
86 
87  for (unsigned int pos = 0; pos < m_buffer.size(); pos++) {
88  /*
89  * fill delay buffer with the input signal
90  */
91  m_dbuffer[m_dbpos] = sample2float(in[pos]);
92 
93  m_lfopos += lfoposinc;
94  m_lfopos -= floorf(m_lfopos);
95 
96  if (m_lfopos < float(0.25)) {
97  m_b1reset = m_b2reset = false;
98  }
99 
100  /*
101  * _speed < 1.0 (downpitching)
102  *
103  * start with current sample and increase delay slowly
104  *
105  * _speed > 1.0 (uppitching)
106  *
107  * start with a sample from long ago and slowly decrease delay
108  */
109  if (!m_b1reset && m_lfopos > float(0.25)) {
110  if (m_speed <= float(1.0)) {
111  m_b1pos = 0;
112  m_b1inc = 1.0f - m_speed;
113  } else {
114  m_b1inc = 1.0f - m_speed;
115  m_b1pos = 10.0f + ((- m_b1inc) * (1.0f / lfoposinc));
116  /* 10+ are not strictly necessary */
117  }
118  m_b1reset = true;
119  }
120 
121  if (!m_b2reset && (m_lfopos > 0.75f)) {
122  if (m_speed <= float(1.0)) {
123  m_b2pos = 0;
124  m_b2inc = 1.0f - m_speed;
125  } else{
126  m_b2inc = 1.0f - m_speed;
127  m_b2pos = 10.0f + ((-m_b2inc) * (1.0f / lfoposinc));
128  /* 10+ are not strictly necessary */
129  }
130  m_b2reset = true;
131  }
132 
133  m_b1pos += m_b1inc;
134  m_b2pos += m_b2inc;
135 
136  int position, position1;
137  float error, int_pos;
138 
139  /*
140  * Interpolate value from buffer position 1
141  */
142  error = modff(m_b1pos, &int_pos);
143 
144  position = m_dbpos - Kwave::toInt(int_pos);
145  if (position < 0)
146  position += MAXDELAY;
147  position1 = position - 1;
148  if (position1 < 0)
149  position1 += MAXDELAY;
150 
151  const float b1value = m_dbuffer[position] * (1 - error) +
152  m_dbuffer[position1] * error;
153 
154  /*
155  * Interpolate value from buffer position 2
156  */
157  error = modff(m_b2pos,&int_pos);
158 
159  position = m_dbpos - Kwave::toInt(int_pos);
160  if (position < 0)
161  position += MAXDELAY;
162  position1 = position-1;
163  if ( position1 < 0)
164  position1 += MAXDELAY;
165 
166  const float b2value = m_dbuffer[position] * (1 - error) +
167  m_dbuffer[position1] * error;
168 
169  /*
170  * Calculate output signal from these two buffers
171  */
172 
173  const float lfo = (sinf(pi2 * m_lfopos) + 1.0f) / 2.0f;
174 
175  /* position sin lfo variable
176  *------------------------------------------------------------------
177  * lfo value: 0.25 1 1 => buffer 2 is used
178  * 0.75 -1 0 => buffer 1 is used
179  */
180 
181  m_buffer[pos] = float2sample(b1value * (1.0f - lfo) + b2value * lfo);
182 
183  /*
184  * increment delay buffer position
185  */
186  m_dbpos++;
187  if (m_dbpos == MAXDELAY)
188  m_dbpos = 0;
189  }
190 
191 }
QVector< float > m_dbuffer
int toInt(T x)
Definition: Utils.h:127
static float sample2float(const sample_t s)
Definition: Sample.h:65
Kwave::SampleArray m_buffer
unsigned int size() const
static sample_t float2sample(const float f)
Definition: Sample.h:57
bool resize(unsigned int size) Q_REQUIRED_RESULT
Here is the call graph for this function:

◆ output

void Kwave::PitchShiftFilter::output ( Kwave::SampleArray  data)
signal

emits a block with the filtered data

Referenced by goOn().

Here is the caller graph for this function:

◆ setFrequency

void Kwave::PitchShiftFilter::setFrequency ( const QVariant  freq)
slot

Sets the frequency parameter

Parameters
freqthe normed frequency

Definition at line 204 of file PitchShiftFilter.cpp.

References initFilter(), and m_frequency.

205 {
206  float new_freq = QVariant(freq).toFloat();
207  if (qFuzzyCompare(new_freq, m_frequency)) return; // nothing to do
208 
209  m_frequency = new_freq;
210  initFilter();
211 }
Here is the call graph for this function:

◆ setSpeed

void Kwave::PitchShiftFilter::setSpeed ( const QVariant  speed)
slot

Sets the speed factor

Parameters
speedfactor as a double

Definition at line 194 of file PitchShiftFilter.cpp.

References initFilter(), and m_speed.

195 {
196  float new_speed = QVariant(speed).toFloat();
197  if (qFuzzyCompare(new_speed, m_speed)) return; // nothing to do
198 
199  m_speed = new_speed;
200  initFilter();
201 }
Here is the call graph for this function:

Member Data Documentation

◆ m_b1inc

float Kwave::PitchShiftFilter::m_b1inc
private

Definition at line 96 of file PitchShiftFilter.h.

Referenced by initFilter(), and input().

◆ m_b1pos

float Kwave::PitchShiftFilter::m_b1pos
private

Definition at line 94 of file PitchShiftFilter.h.

Referenced by initFilter(), and input().

◆ m_b1reset

bool Kwave::PitchShiftFilter::m_b1reset
private

Definition at line 98 of file PitchShiftFilter.h.

Referenced by input().

◆ m_b2inc

float Kwave::PitchShiftFilter::m_b2inc
private

Definition at line 97 of file PitchShiftFilter.h.

Referenced by initFilter(), and input().

◆ m_b2pos

float Kwave::PitchShiftFilter::m_b2pos
private

Definition at line 95 of file PitchShiftFilter.h.

Referenced by initFilter(), and input().

◆ m_b2reset

bool Kwave::PitchShiftFilter::m_b2reset
private

Definition at line 99 of file PitchShiftFilter.h.

Referenced by input().

◆ m_buffer

Kwave::SampleArray Kwave::PitchShiftFilter::m_buffer
private

buffer for input

Definition at line 82 of file PitchShiftFilter.h.

Referenced by goOn(), and input().

◆ m_dbpos

int Kwave::PitchShiftFilter::m_dbpos
private

Definition at line 100 of file PitchShiftFilter.h.

Referenced by initFilter(), and input().

◆ m_dbuffer

QVector<float> Kwave::PitchShiftFilter::m_dbuffer
private

Definition at line 92 of file PitchShiftFilter.h.

Referenced by initFilter(), and input().

◆ m_frequency

float Kwave::PitchShiftFilter::m_frequency
private

base frequency

Definition at line 88 of file PitchShiftFilter.h.

Referenced by input(), and setFrequency().

◆ m_lfopos

float Kwave::PitchShiftFilter::m_lfopos
private

Definition at line 93 of file PitchShiftFilter.h.

Referenced by initFilter(), and input().

◆ m_speed

float Kwave::PitchShiftFilter::m_speed
private

speed factor

Definition at line 85 of file PitchShiftFilter.h.

Referenced by initFilter(), input(), and setSpeed().


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