Skip to content

Commit e4c519e

Browse files
committed
New class, replace synth_whitenoise_f32
1 parent e1ba740 commit e4c519e

2 files changed

Lines changed: 146 additions & 0 deletions

File tree

synth_UniformWhiteNoise_F32.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* synth_UniformWhiteNoise_F32.cpp
2+
3+
License: MIT License. Use at your own risk.
4+
*/
5+
6+
/* Audio Library for Teensy 3.X
7+
* Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
8+
*
9+
* Development of this audio library was funded by PJRC.COM, LLC by sales of
10+
* Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
11+
* open source software by purchasing Teensy or other PJRC products.
12+
*
13+
* Permission is hereby granted, free of charge, to any person obtaining a copy
14+
* of this software and associated documentation files (the "Software"), to deal
15+
* in the Software without restriction, including without limitation the rights
16+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17+
* copies of the Software, and to permit persons to whom the Software is
18+
* furnished to do so, subject to the following conditions:
19+
*
20+
* The above copyright notice, development funding notice, and this permission
21+
* notice shall be included in all copies or substantial portions of the Software.
22+
*
23+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29+
* THE SOFTWARE.
30+
*/
31+
32+
#include "synth_UniformWhiteNoise_F32.h"
33+
#define I32_TO_F32_NORM_FACTOR (2.3283064E-10)
34+
#define Q31_TO_F32_NORM_FACTOR (4.6566129E-10)
35+
36+
void AudioSynthUniformWhiteNoise_F32::update(void)
37+
{
38+
audio_block_f32_t *block_f32;
39+
float32_t *pf;
40+
41+
#if !defined(__ARM_ARCH_7EM__)
42+
return]
43+
#else
44+
if (level < 0.000001f)
45+
return; // special case, turn off update() if level==0.
46+
47+
block_f32 = AudioStream_F32::allocate_f32();
48+
if (!block_f32)
49+
return;
50+
pf = block_f32->data;
51+
for(int i=0; i<block_f32->length; i++)
52+
{
53+
uint64_t x = rnState;
54+
x ^= x >> 12;
55+
x ^= x << 25;
56+
x ^= x >> 27;
57+
rnState = x;
58+
*pf++ = level*((Q31_TO_F32_NORM_FACTOR * (float32_t)((x * UINT64_C(2685821657736338717)) >> 32)) - 1.0f);
59+
}
60+
61+
AudioStream_F32::transmit(block_f32);
62+
AudioStream_F32::release(block_f32);
63+
#endif
64+
}
65+
66+

synth_UniformWhiteNoise_F32.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* synth_UniformWhiteNoise_F32.h --- New July 2025
2+
Generates a block of uniformly distributed, uncorrelated, white
3+
noise of a range of (-level, +level) as set by amplitude(level)
4+
with a default value of level = 0.0. This replaces the class
5+
synth_whitenoise_F32 and corrects the need for I16 memory as well
6+
providing output that uses the full F32 mantissa of 24-bits.
7+
8+
Method is xor shift 64 credited to George Marsaglia. See
9+
https://en.wikipedia.org/wiki/Xorshift and
10+
https://forum.pjrc.com/index.php?threads/teensy-4-1-random-number-generator.61125
11+
post 13 by #Nominal Animal.
12+
Bob Larkin July 2025. Thanks #grrrr
13+
14+
Derived from synth_whitenoise_F32 that was
15+
extended by: Chip Audette, OpenAudio, Feb 2017
16+
17+
License: MIT License. Use at your own risk.
18+
*/
19+
/* Audio Library for Teensy 3.X
20+
* Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
21+
*
22+
* Development of this audio library was funded by PJRC.COM, LLC by sales of
23+
* Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
24+
* open source software by purchasing Teensy or other PJRC products.
25+
*
26+
* Permission is hereby granted, free of charge, to any person obtaining a copy
27+
* of this software and associated documentation files (the "Software"), to deal
28+
* in the Software without restriction, including without limitation the rights
29+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
30+
* copies of the Software, and to permit persons to whom the Software is
31+
* furnished to do so, subject to the following conditions:
32+
*
33+
* The above copyright notice, development funding notice, and this permission
34+
* notice shall be included in all copies or substantial portions of the Software.
35+
*
36+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
39+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
40+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
41+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
42+
* THE SOFTWARE.
43+
*/
44+
45+
// ** This does not support Teensy LC (KINETISL). Teensy 3.x and 4.x OK. **
46+
// Execution time 15 uSec for 128 size block on T4.x. 12 of these are
47+
// the conversionfrom uint32_t to F32 with offset for DC=0.
48+
49+
#ifndef synth_uniform_white_noise_f32_h_
50+
#define synth_uniform_white_noise_f32_h_
51+
#include "Arduino.h"
52+
#include "AudioStream_F32.h"
53+
#include "utility/dspinst.h"
54+
55+
class AudioSynthUniformWhiteNoise_F32 : public AudioStream_F32
56+
{
57+
//GUI: inputs:0, outputs:1 //this line used for automatic generation of GUI node
58+
//GUI: shortName:uniformwhitenoise //this line used for automatic generation of GUI node
59+
public:
60+
AudioSynthUniformWhiteNoise_F32() : AudioStream_F32(0, NULL) { setDefaultValues(); }
61+
AudioSynthUniformWhiteNoise_F32(const AudioSettings_F32 &settings) :
62+
AudioStream_F32(0, NULL) { setDefaultValues(); }
63+
void setDefaultValues(void) {
64+
level = 0.0f;
65+
rnState = 1ULL + instance_count++;
66+
}
67+
68+
void amplitude(float nL) {
69+
if (nL < 0.0) nL = 0.0;
70+
level = nL;
71+
}
72+
73+
virtual void update(void);
74+
75+
private:
76+
float32_t level = 0.0f;
77+
uint64_t rnState;
78+
uint64_t instance_count=0ULL;
79+
};
80+
#endif

0 commit comments

Comments
 (0)