New class, replace synth_whitenoise_f32

master
boblark 4 days ago
parent e1ba74006b
commit e4c519e9a8
  1. 66
      synth_UniformWhiteNoise_F32.cpp
  2. 80
      synth_UniformWhiteNoise_F32.h

@ -0,0 +1,66 @@
/* synth_UniformWhiteNoise_F32.cpp
License: MIT License. Use at your own risk.
*/
/* 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.
*/
#include "synth_UniformWhiteNoise_F32.h"
#define I32_TO_F32_NORM_FACTOR (2.3283064E-10)
#define Q31_TO_F32_NORM_FACTOR (4.6566129E-10)
void AudioSynthUniformWhiteNoise_F32::update(void)
{
audio_block_f32_t *block_f32;
float32_t *pf;
#if !defined(__ARM_ARCH_7EM__)
return]
#else
if (level < 0.000001f)
return; // special case, turn off update() if level==0.
block_f32 = AudioStream_F32::allocate_f32();
if (!block_f32)
return;
pf = block_f32->data;
for(int i=0; i<block_f32->length; i++)
{
uint64_t x = rnState;
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
rnState = x;
*pf++ = level*((Q31_TO_F32_NORM_FACTOR * (float32_t)((x * UINT64_C(2685821657736338717)) >> 32)) - 1.0f);
}
AudioStream_F32::transmit(block_f32);
AudioStream_F32::release(block_f32);
#endif
}

@ -0,0 +1,80 @@
/* synth_UniformWhiteNoise_F32.h --- New July 2025
Generates a block of uniformly distributed, uncorrelated, white
noise of a range of (-level, +level) as set by amplitude(level)
with a default value of level = 0.0. This replaces the class
synth_whitenoise_F32 and corrects the need for I16 memory as well
providing output that uses the full F32 mantissa of 24-bits.
Method is xor shift 64 credited to George Marsaglia. See
https://en.wikipedia.org/wiki/Xorshift and
https://forum.pjrc.com/index.php?threads/teensy-4-1-random-number-generator.61125
post 13 by #Nominal Animal.
Bob Larkin July 2025. Thanks #grrrr
Derived from synth_whitenoise_F32 that was
extended by: Chip Audette, OpenAudio, Feb 2017
License: MIT License. Use at your own risk.
*/
/* 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.
*/
// ** This does not support Teensy LC (KINETISL). Teensy 3.x and 4.x OK. **
// Execution time 15 uSec for 128 size block on T4.x. 12 of these are
// the conversionfrom uint32_t to F32 with offset for DC=0.
#ifndef synth_uniform_white_noise_f32_h_
#define synth_uniform_white_noise_f32_h_
#include "Arduino.h"
#include "AudioStream_F32.h"
#include "utility/dspinst.h"
class AudioSynthUniformWhiteNoise_F32 : public AudioStream_F32
{
//GUI: inputs:0, outputs:1 //this line used for automatic generation of GUI node
//GUI: shortName:uniformwhitenoise //this line used for automatic generation of GUI node
public:
AudioSynthUniformWhiteNoise_F32() : AudioStream_F32(0, NULL) { setDefaultValues(); }
AudioSynthUniformWhiteNoise_F32(const AudioSettings_F32 &settings) :
AudioStream_F32(0, NULL) { setDefaultValues(); }
void setDefaultValues(void) {
level = 0.0f;
rnState = 1ULL + instance_count++;
}
void amplitude(float nL) {
if (nL < 0.0) nL = 0.0;
level = nL;
}
virtual void update(void);
private:
float32_t level = 0.0f;
uint64_t rnState;
uint64_t instance_count=0ULL;
};
#endif
Loading…
Cancel
Save