Skip to content

Commit

Permalink
Add MLS+ noise
Browse files Browse the repository at this point in the history
  • Loading branch information
psmokotnin committed Mar 1, 2023
1 parent e176888 commit a8b584a
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
2 changes: 2 additions & 0 deletions OpenSoundMeter.pro
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ SOURCES += src/main.cpp \
src/filtersource.cpp \
src/generator/burstnoise.cpp \
src/generator/channelmodel.cpp \
src/generator/mlsplus.cpp \
src/generator/wav.cpp \
src/inputdevice.cpp \
src/common/appearance.cpp \
Expand Down Expand Up @@ -157,6 +158,7 @@ HEADERS += \
src/filtersource.h \
src/generator/burstnoise.h \
src/generator/channelmodel.h \
src/generator/mlsplus.h \
src/generator/wav.h \
src/inputdevice.h \
src/common/appearance.h \
Expand Down
3 changes: 3 additions & 0 deletions src/generator/generatorthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "sinsweep.h"
#include "mnoise.h"
#include "burstnoise.h"
#include "mlsplus.h"

GeneratorThread *GeneratorThread::s_instance = nullptr;

Expand Down Expand Up @@ -102,6 +103,8 @@ void GeneratorThread::init()
m_sources << new SinSweep(this);
m_sources << new MNoise(this);
m_sources << new BurstNoise(this);
m_sources << new MLSPlus(this);

for (auto &source : m_sources) {
connect(source, &OutputDevice::sampleError, this, &GeneratorThread::deviceError);
connect(source, &OutputDevice::sampleOut, this, &GeneratorThread::sampleOut, Qt::DirectConnection);
Expand Down
67 changes: 67 additions & 0 deletions src/generator/mlsplus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* OSM
* Copyright (C) 2023 Pavel Smokotnin
* 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 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mlsplus.h"

MLSPlus::MLSPlus(QObject *parent)
: OutputDevice{parent}, m_state{}
{
m_name = "MLS+";
std::fill(m_state.begin(), m_state.end(), false);
m_state[0] = true;
}

Sample MLSPlus::sample()
{
static std::size_t counter = 0;
if (++counter == 65'536) {
counter = 0;
return { 0 };
}
/*
* W. Stahnke.
* Primitive binary polynomials.
* Mathematics of Computation, 27:977-980, 1973.
*
* @url https://www.ams.org/journals/mcom/1973-27-124/S0025-5718-1973-0327722-7/S0025-5718-1973-0327722-7.pdf
*/
bool input = m_state[16];
m_state[16] = m_state[15];
m_state[15] = m_state[14];
m_state[14] = m_state[13];
m_state[13] = m_state[12];
m_state[12] = m_state[11];
m_state[11] = m_state[10];
m_state[10] = m_state[9];
m_state[9] = m_state[8];
m_state[8] = m_state[7];
m_state[7] = m_state[6];
m_state[6] = m_state[5];
m_state[5] = m_state[4] != input;
m_state[4] = m_state[3];
m_state[3] = m_state[2] != input;
m_state[2] = m_state[1] != input;
m_state[1] = m_state[0];
m_state[0] = input;

Sample s = { m_state[16] ? 1.f : -1.f };

// -18dB to align rms level with pink noise
s.f *= m_gain / 8;

return s;
}
35 changes: 35 additions & 0 deletions src/generator/mlsplus.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* OSM
* Copyright (C) 2023 Pavel Smokotnin
* 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 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MLSPLUS_H
#define MLSPLUS_H

#include "outputdevice.h"

class MLSPlus : public OutputDevice
{
Q_OBJECT

public:
explicit MLSPlus(QObject *parent = nullptr);
Sample sample() override;

private:
std::array<bool, 17> m_state;
};

#endif // MLSPLUS_H

0 comments on commit a8b584a

Please sign in to comment.