kwave  18.07.70
NoiseGenerator.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  NoiseGenerator.cpp - simple noise generator, implemented as SampleSource
3  -------------------
4  begin : Sun Oct 07 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 <QtGlobal>
21 
22 #include "NoiseGenerator.h"
23 #include "libkwave/Sample.h"
24 
25 //***************************************************************************
27  :Kwave::SampleSource(parent), m_buffer(blockSize()), m_noise_level(1.0)
28 {
29 }
30 
31 //***************************************************************************
33 {
34 }
35 
36 //***************************************************************************
38 {
39  emit output(m_buffer);
40 }
41 
42 //***************************************************************************
44 {
45  bool ok = m_buffer.resize(data.size());
46  Q_ASSERT(ok);
47  Q_UNUSED(ok);
48 
49  m_buffer = data;
50 
51  const double alpha = (1.0 - m_noise_level);
52  const double scale = (m_noise_level * 2.0) / static_cast<double>(RAND_MAX);
53  for (unsigned i = 0; i < data.size(); ++i) {
54  const Kwave::SampleArray &in = data;
56  (sample2double(in[i]) * alpha) +
57  ((qrand() - (RAND_MAX / 2)) * scale)
58  );
59  }
60 }
61 
62 //***************************************************************************
63 void Kwave::NoiseGenerator::setNoiseLevel(const QVariant fc)
64 {
65  m_noise_level = QVariant(fc).toDouble();
66 }
67 
68 //***************************************************************************
69 //***************************************************************************
Definition: App.h:33
virtual ~NoiseGenerator() Q_DECL_OVERRIDE
void output(Kwave::SampleArray data)
NoiseGenerator(QObject *parent=Q_NULLPTR)
void setNoiseLevel(const QVariant fc)
static double sample2double(const sample_t s)
Definition: Sample.h:73
void input(Kwave::SampleArray data)
virtual void goOn() Q_DECL_OVERRIDE
static sample_t double2sample(const double f)
Definition: Sample.h:81
unsigned int size() const
Kwave::SampleArray m_buffer
bool resize(unsigned int size) Q_REQUIRED_RESULT