kwave  18.07.70
Osc.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  Osc.cpp - simple sine oscillator
3  -------------------
4  begin : Tue Nov 06 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 <math.h>
21 
22 #include "libkwave/modules/Osc.h"
23 
24 //***************************************************************************
26  :Kwave::SampleSource(),
27  m_buffer(blockSize()), m_omega_t(0.0), m_f(44.1), m_a(1.0)
28 {
29 }
30 
31 //***************************************************************************
33 {
34 }
35 
36 //***************************************************************************
38 {
39  unsigned int samples = m_buffer.size();
40  const double two_pi = 2.0 * M_PI;
41 
42  Q_ASSERT(!qFuzzyIsNull(m_f));
43  if (qFuzzyIsNull(m_f)) return;
44 
45  double omega = two_pi / m_f;
46  for (unsigned int sample = 0; sample < samples; sample++) {
47  // calculate one sample as sin(w * t)
48  m_buffer[sample] = double2sample(m_a * sin(m_omega_t));
49 
50  // next time, t++
51  m_omega_t += omega;
52 
53  // limit the argument of sin() to [0 ... 4*Pi]
54  while (m_omega_t > two_pi)
55  m_omega_t -= two_pi;
56  }
57 
58  emit output(m_buffer);
59 }
60 
61 //***************************************************************************
62 void Kwave::Osc::setFrequency(const QVariant &f)
63 {
64  m_f = QVariant(f).toDouble();
65 }
66 
67 //***************************************************************************
68 void Kwave::Osc::setPhase(const QVariant &p)
69 {
70  m_omega_t = QVariant(p).toDouble();
71 }
72 
73 //***************************************************************************
74 void Kwave::Osc::setAmplitude(const QVariant &a)
75 {
76  m_a = QVariant(a).toDouble();
77 }
78 
79 //***************************************************************************
80 //***************************************************************************
virtual void goOn() Q_DECL_OVERRIDE
Definition: Osc.cpp:37
void output(Kwave::SampleArray data)
Definition: App.h:33
void setFrequency(const QVariant &f)
Definition: Osc.cpp:62
void setAmplitude(const QVariant &a)
Definition: Osc.cpp:74
Kwave::SampleArray m_buffer
Definition: Osc.h:73
double m_f
Definition: Osc.h:79
virtual ~Osc() Q_DECL_OVERRIDE
Definition: Osc.cpp:32
double m_omega_t
Definition: Osc.h:76
void setPhase(const QVariant &p)
Definition: Osc.cpp:68
static sample_t double2sample(const double f)
Definition: Sample.h:81
double m_a
Definition: Osc.h:82
unsigned int size() const
Osc()
Definition: Osc.cpp:25