From de50899d1b8148f25a25472f1ef3ba3764807ae5 Mon Sep 17 00:00:00 2001 From: Chip Audette Date: Sun, 19 Feb 2017 11:03:48 -0500 Subject: [PATCH] Add whitenoise that doesn't need modded AudioStream.h --- OpenAudio_ArduinoLibrary.h | 2 +- synth_whitenoise_f32.cpp | 125 +++++++++++++++++++++++++++++++++++++ synth_whitenoise_f32.h | 61 ++++++++++++++++++ 3 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 synth_whitenoise_f32.cpp create mode 100644 synth_whitenoise_f32.h diff --git a/OpenAudio_ArduinoLibrary.h b/OpenAudio_ArduinoLibrary.h index 7f52d1c..61bdfa0 100644 --- a/OpenAudio_ArduinoLibrary.h +++ b/OpenAudio_ArduinoLibrary.h @@ -14,6 +14,6 @@ #include "record_queue_f32.h" //include "synth_pinknoise_f32.h" #include -//include "synth_whitenoise_f32.h" +#include "synth_whitenoise_f32.h" #include "synth_sine_f32.h" #include "output_i2s_f32.h" diff --git a/synth_whitenoise_f32.cpp b/synth_whitenoise_f32.cpp new file mode 100644 index 0000000..34b179d --- /dev/null +++ b/synth_whitenoise_f32.cpp @@ -0,0 +1,125 @@ +/* + Extended to F32 + Created: 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. + */ + +#include "synth_whitenoise_f32.h" +#include "input_i2s_f32.h" //for the audio_convert_i16_to_f32 routine + +// Park-Miller-Carta Pseudo-Random Number Generator +// http://www.firstpr.com.au/dsp/rand31/ + +void AudioSynthNoiseWhite_F32::update(void) +{ + audio_block_t *block; + audio_block_f32_t *block_f32; + uint32_t *p, *end; + int32_t n1, n2, gain; + uint32_t lo, hi, val1, val2; + + //Serial.println("synth_whitenoise: update()"); + gain = level; + if (gain == 0) { + //Serial.println(": Gain = 0, returning."); + return; + } + block = AudioStream::allocate(); + block_f32 = AudioStream_F32::allocate_f32(); + if (!block | !block_f32) { + //Serial.println(": NULL block. returning."); + return; + } + p = (uint32_t *)(block->data); + //end = p + AUDIO_BLOCK_SAMPLES/2; + end = p + (block_f32->length)/2; + + lo = seed; + do { +#if defined(KINETISK) + hi = multiply_16bx16t(16807, lo); // 16807 * (lo >> 16) + lo = 16807 * (lo & 0xFFFF); + lo += (hi & 0x7FFF) << 16; + lo += hi >> 15; + lo = (lo & 0x7FFFFFFF) + (lo >> 31); + n1 = signed_multiply_32x16b(gain, lo); + hi = multiply_16bx16t(16807, lo); // 16807 * (lo >> 16) + lo = 16807 * (lo & 0xFFFF); + lo += (hi & 0x7FFF) << 16; + lo += hi >> 15; + lo = (lo & 0x7FFFFFFF) + (lo >> 31); + n2 = signed_multiply_32x16b(gain, lo); + val1 = pack_16b_16b(n2, n1); + hi = multiply_16bx16t(16807, lo); // 16807 * (lo >> 16) + lo = 16807 * (lo & 0xFFFF); + lo += (hi & 0x7FFF) << 16; + lo += hi >> 15; + lo = (lo & 0x7FFFFFFF) + (lo >> 31); + n1 = signed_multiply_32x16b(gain, lo); + hi = multiply_16bx16t(16807, lo); // 16807 * (lo >> 16) + lo = 16807 * (lo & 0xFFFF); + lo += (hi & 0x7FFF) << 16; + lo += hi >> 15; + lo = (lo & 0x7FFFFFFF) + (lo >> 31); + n2 = signed_multiply_32x16b(gain, lo); + val2 = pack_16b_16b(n2, n1); + *p++ = val1; + *p++ = val2; +#elif defined(KINETISL) + hi = 16807 * (lo >> 16); + lo = 16807 * (lo & 0xFFFF); + lo += (hi & 0x7FFF) << 16; + lo += hi >> 15; + lo = (lo & 0x7FFFFFFF) + (lo >> 31); + n1 = signed_multiply_32x16b(gain, lo); + hi = 16807 * (lo >> 16); + lo = 16807 * (lo & 0xFFFF); + lo += (hi & 0x7FFF) << 16; + lo += hi >> 15; + lo = (lo & 0x7FFFFFFF) + (lo >> 31); + n2 = signed_multiply_32x16b(gain, lo); + val1 = pack_16b_16b(n2, n1); + *p++ = val1; +#endif + } while (p < end); + seed = lo; + + //convert int16 to f32 + AudioInputI2S_F32::convert_i16_to_f32(block->data,block_f32->data,block_f32->length); + + AudioStream_F32::transmit(block_f32); + AudioStream_F32::release(block_f32); + AudioStream::release(block); + //Serial.println(" Done."); +} + +uint16_t AudioSynthNoiseWhite_F32::instance_count = 0; + + diff --git a/synth_whitenoise_f32.h b/synth_whitenoise_f32.h new file mode 100644 index 0000000..f7e01ce --- /dev/null +++ b/synth_whitenoise_f32.h @@ -0,0 +1,61 @@ +/* + synth_whitenoise_F32 + + 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. + */ + +#ifndef synth_whitenoise_f32_h_ +#define synth_whitenoise_f32_h_ +#include "Arduino.h" +#include "AudioStream.h" +#include "AudioStream_F32.h" +#include "utility/dspinst.h" + +class AudioSynthNoiseWhite_F32 : public AudioStream_F32 +{ +public: + AudioSynthNoiseWhite_F32() : AudioStream_F32(0, NULL) { + level = 0; + seed = 1 + instance_count++; + } + 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: + int32_t level; // 0=off, 65536=max + uint32_t seed; // must start at 1 + static uint16_t instance_count; +}; + +#endif