Переглянути джерело

Add AudioSynthNoisePink

dds
PaulStoffregen 10 роки тому
джерело
коміт
95113cedde
4 змінених файлів з 256 додано та 0 видалено
  1. +1
    -0
      Audio.h
  2. +46
    -0
      gui/list.html
  3. +148
    -0
      synth_pinknoise.cpp
  4. +61
    -0
      synth_pinknoise.h

+ 1
- 0
Audio.h Переглянути файл

@@ -75,6 +75,7 @@
#include "synth_waveform.h"
#include "synth_dc.h"
#include "synth_whitenoise.h"
#include "synth_pinknoise.h"
#include "multiplier.h"

// TODO: more audio processing objects....

+ 46
- 0
gui/list.html Переглянути файл

@@ -901,6 +901,52 @@



<script type="text/javascript">
RED.nodes.registerType('AudioSynthNoisePink',{
shortName: "pink",
inputs:0,
outputs:1,
category: 'synth-function',
color:"#E6E0F8",
icon: "arrow-in.png"
});
</script>
<script type="text/x-red" data-help-name="AudioSynthNoisePink">
<h3>Summary</h3>
<div>
<p>Create pink noise, using Stefan Stenzel's "New Shade Of Pink" algorithm.
</p>
<!--<p align=center><img src="whitenoise.png"></p>-->
</div>
<h3>Audio Connections</h3>
<table class=doc align=center cellpadding=3>
<tr class=top><th>Port</th><th>Purpose</th></tr>
<tr class=odd><td align=center>Out 0</td><td>Pink Noise</td></tr>
</table>
<h3>Functions</h3>
<p class=func><span class=keyword>amplitude</span>(level);</p>
<p class=desc>Set the output peak level, from 0 (off) to 1.0.
The default is off. Noise is generated only after setting
to a non-zero level.
</p>
<h3>Notes</h3>
<p>Setting the amplitude to zero causes this object to stop using
CPU time. CPU usage is approx 3% on Teensy 3.1.
</p>
<p>Stefan Stenzel's
<a href="http://stenzel.waldorfmusic.de/post/pink/" target="_blank">New Shade Of Pink</a>
algorithm. Stefan's terms of use are "Use for any purpose. If used
in a commercial product, you should give me one."
</p>
</script>
<script type="text/x-red" data-template-name="AudioSynthNoisePink">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>




<script type="text/javascript">

+ 148
- 0
synth_pinknoise.cpp Переглянути файл

@@ -0,0 +1,148 @@
/* Audio Library for Teensy 3.X
* Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
*
* Development of this audio library was funded by PJRC.COM, LLC by sales of
* Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
* open source software by purchasing Teensy or other PJRC products.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice, development funding notice, and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

// http://stenzel.waldorfmusic.de/post/pink/
// https://github.com/Stenzel/newshadeofpink
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// New Shade of Pink
// (c) 2014 Stefan Stenzel
// stefan at waldorfmusic.de
//
// Terms of use:
// Use for any purpose. If used in a commercial product, you should give me one.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#include "synth_pinknoise.h"

int16_t AudioSynthNoisePink::instance_cnt = 0;

// Let preprocessor and compiler calculate two lookup tables for 12-tap FIR Filter
// with these coefficients: 1.190566, 0.162580, 0.002208, 0.025475, -0.001522,
// 0.007322, 0.001774, 0.004529, -0.001561, 0.000776, -0.000486, 0.002017
#define Fn(cf,m,shift) (2048*cf*(2*((m)>>shift&1)-1))
#define FA(n) Fn(1.190566,n,0)+Fn(0.162580,n,1)+Fn(0.002208,n,2)+\
Fn(0.025475,n,3)+Fn(-0.001522,n,4)+Fn(0.007322,n,5)
#define FB(n) Fn(0.001774,n,0)+Fn(0.004529,n,1)+Fn(-0.001561,n,2)+\
Fn(0.000776,n,3)+Fn(-0.000486,n,4)+Fn(0.002017,n,5)
#define FA8(n) FA(n),FA(n+1),FA(n+2),FA(n+3),FA(n+4),FA(n+5),FA(n+6),FA(n+7)
#define FB8(n) FB(n),FB(n+1),FB(n+2),FB(n+3),FB(n+4),FB(n+5),FB(n+6),FB(n+7)
const int32_t AudioSynthNoisePink::pfira[64] = // 1st FIR lookup table
{FA8(0),FA8(8),FA8(16),FA8(24),FA8(32),FA8(40),FA8(48),FA8(56)};
const int32_t AudioSynthNoisePink::pfirb[64] = // 2nd FIR lookup table
{FB8(0),FB8(8),FB8(16),FB8(24),FB8(32),FB8(40),FB8(48),FB8(56)};

// bitreversed lookup table
#define PM16(n) n,0x80,0x40,0x80,0x20,0x80,0x40,0x80,0x10,0x80,0x40,0x80,0x20,0x80,0x40,0x80
const uint8_t AudioSynthNoisePink::pnmask[256] = {
PM16(0),PM16(8),PM16(4),PM16(8),PM16(2),PM16(8),PM16(4),PM16(8),
PM16(1),PM16(8),PM16(4),PM16(8),PM16(2),PM16(8),PM16(4),PM16(8)
};

#define PINT(bitmask, out) /* macro for processing: */\
bit = lfsr >> 31; /* spill random to all bits */\
dec &= ~bitmask; /* blank old decrement bit */\
lfsr <<= 1; /* shift lfsr */\
dec |= inc & bitmask; /* copy increment to decrement bit */\
inc ^= bit & bitmask; /* new random bit */\
accu += inc - dec; /* integrate */\
lfsr ^= bit & taps; /* update lfsr */\
out = accu + /* save output */\
pfira[lfsr & 0x3F] + /* add 1st half precalculated FIR */\
pfirb[lfsr >> 6 & 0x3F] /* add 2nd half, also correts bias */

void AudioSynthNoisePink::update(void)
{
audio_block_t *block;
uint32_t *p, *end;
int32_t n1, n2;
int32_t gain;
int32_t inc, dec, accu, bit, lfsr;
int32_t taps;

gain = level;
if (gain == 0) return;
block = allocate();
if (!block) return;
p = (uint32_t *)(block->data);
end = p + AUDIO_BLOCK_SAMPLES/2;
taps = 0x46000001;
inc = pinc;
dec = pdec;
accu = paccu;
lfsr = plfsr;
do {
int32_t mask = pnmask[pncnt++];
PINT(mask, n1);
n1 = signed_multiply_32x16b(gain, n1);
PINT(0x0800, n2);
n2 = signed_multiply_32x16b(gain, n2);
*p++ = pack_16b_16b(n2, n1);
PINT(0x0400, n1);
n1 = signed_multiply_32x16b(gain, n1);
PINT(0x0800, n2);
n2 = signed_multiply_32x16b(gain, n2);
*p++ = pack_16b_16b(n2, n1);
PINT(0x0200, n1);
n1 = signed_multiply_32x16b(gain, n1);
PINT(0x0800, n2);
n2 = signed_multiply_32x16b(gain, n2);
*p++ = pack_16b_16b(n2, n1);
PINT(0x0400, n1);
n1 = signed_multiply_32x16b(gain, n1);
PINT(0x0800, n2);
n2 = signed_multiply_32x16b(gain, n2);
*p++ = pack_16b_16b(n2, n1);
PINT(0x0100, n1);
n1 = signed_multiply_32x16b(gain, n1);
PINT(0x0800, n2);
n2 = signed_multiply_32x16b(gain, n2);
*p++ = pack_16b_16b(n2, n1);
PINT(0x0400, n1);
n1 = signed_multiply_32x16b(gain, n1);
PINT(0x0800, n2);
n2 = signed_multiply_32x16b(gain, n2);
*p++ = pack_16b_16b(n2, n1);
PINT(0x0200, n1);
n1 = signed_multiply_32x16b(gain, n1);
PINT(0x0800, n2);
n2 = signed_multiply_32x16b(gain, n2);
*p++ = pack_16b_16b(n2, n1);
PINT(0x0400, n1);
n1 = signed_multiply_32x16b(gain, n1);
PINT(0x0800, n2);
n2 = signed_multiply_32x16b(gain, n2);
*p++ = pack_16b_16b(n2, n1);
} while (p < end);
pinc = inc;
pdec = dec;
paccu = accu;
plfsr = lfsr;
transmit(block);
release(block);
}




+ 61
- 0
synth_pinknoise.h Переглянути файл

@@ -0,0 +1,61 @@
/* Audio Library for Teensy 3.X
* Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
*
* Development of this audio library was funded by PJRC.COM, LLC by sales of
* Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
* open source software by purchasing Teensy or other PJRC products.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice, development funding notice, and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef synth_pinknoise_h_
#define synth_pinknoise_h_
#include "AudioStream.h"
#include "utility/dspinst.h"

class AudioSynthNoisePink : public AudioStream
{
public:
AudioSynthNoisePink() : AudioStream(0, NULL) {
plfsr = 0x5EED41F5 + instance_cnt++;
paccu = 0;
pncnt = 0;
pinc = 0x0CCC;
pdec = 0x0CCC;
}
void amplitude(float n) {
if (n < 0.0) n = 0.0;
else if (n > 1.0) n = 1.0;
level = (int32_t)(n * 65536.0);
}
virtual void update(void);
private:
static const uint8_t pnmask[256];
static const int32_t pfira[64];
static const int32_t pfirb[64];
static int16_t instance_cnt;
int32_t plfsr; // linear feedback shift register
int32_t pinc; // increment for all noise sources (bits)
int32_t pdec; // decrement for all noise sources
int32_t paccu; // accumulator
uint8_t pncnt; // overflowing counter as index to pnmask[]
int32_t level; // 0=off, 65536=max
};

#endif

Завантаження…
Відмінити
Зберегти