From 324d2bbef03a288fe435a65fed2dfe25d63b2166 Mon Sep 17 00:00:00 2001 From: Chip Audette Date: Sun, 19 Feb 2017 11:13:54 -0500 Subject: [PATCH] Add pinknoise that doens't require modded AudioStream.h --- OpenAudio_ArduinoLibrary.h | 2 +- keywords.txt | 5 ++ synth_pinknoise_f32.cpp | 167 +++++++++++++++++++++++++++++++++++++ synth_pinknoise_f32.h | 75 +++++++++++++++++ synth_whitenoise_f32.h | 2 + 5 files changed, 250 insertions(+), 1 deletion(-) create mode 100644 synth_pinknoise_f32.cpp create mode 100644 synth_pinknoise_f32.h diff --git a/OpenAudio_ArduinoLibrary.h b/OpenAudio_ArduinoLibrary.h index 61bdfa0..9cba9aa 100644 --- a/OpenAudio_ArduinoLibrary.h +++ b/OpenAudio_ArduinoLibrary.h @@ -12,7 +12,7 @@ #include "input_i2s_f32.h" #include "play_queue_f32.h" #include "record_queue_f32.h" -//include "synth_pinknoise_f32.h" +#include "synth_pinknoise_f32.h" #include #include "synth_whitenoise_f32.h" #include "synth_sine_f32.h" diff --git a/keywords.txt b/keywords.txt index de3c08a..2d1d8d6 100644 --- a/keywords.txt +++ b/keywords.txt @@ -36,6 +36,11 @@ AudioMultiply_F32 KEYWORD1 AudioOutputI2S_F32 KEYWORD1 AudioPlayQueue_F32 KEYWORD1 AudioRecordQueue_F32 KEYWORD1 + +AudioSettings_F32 KEYWORD1 +sample_rate_Hz KEYWORD2 +audio_block_samples KEYWORD2 + AudioSynthNoiseWhite_F32 KEYWORD1 AudioSynthNoisePink_F32 KEYWORD1 AudioSynthWaveform_F32 KEYWORD1 diff --git a/synth_pinknoise_f32.cpp b/synth_pinknoise_f32.cpp new file mode 100644 index 0000000..bdde1d7 --- /dev/null +++ b/synth_pinknoise_f32.cpp @@ -0,0 +1,167 @@ +/* + Extended to f32 data + 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. + */ + +// 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_f32.h" +#include "input_i2s_f32.h" //for the audio_convert_i16_to_f32 routine + +int16_t AudioSynthNoisePink_F32::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) (int32_t)(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) (int32_t)(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_F32::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_F32::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_F32::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_F32::update(void) +{ + audio_block_t *block; + audio_block_f32_t *block_f32; + uint32_t *p, *end; + int32_t n1, n2; + int32_t gain; + int32_t inc, dec, accu, bit, lfsr; + int32_t taps; + + if (!enabled) return; + + gain = level; + if (gain == 0) return; + block = AudioStream::allocate(); + block_f32 = AudioStream_F32::allocate_f32(); + if (!block | !block_f32) return; + p = (uint32_t *)(block->data); + //end = p + AUDIO_BLOCK_SAMPLES/2; + end = p + (block_f32->length)/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; + + //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); + +} + + + diff --git a/synth_pinknoise_f32.h b/synth_pinknoise_f32.h new file mode 100644 index 0000000..d010abb --- /dev/null +++ b/synth_pinknoise_f32.h @@ -0,0 +1,75 @@ +/* + Extended to f32 data + 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. + */ + +#ifndef synth_pinknoise_f32_h_ +#define synth_pinknoise_f32_h_ +#include "Arduino.h" +#include "AudioStream.h" +#include "AudioStream_F32.h" +#include "utility/dspinst.h" + +class AudioSynthNoisePink_F32 : public AudioStream_F32 +{ +//GUI: inputs:0, outputs:1 //this line used for automatic generation of GUI node +//GUI: shortName:pinknoise //this line used for automatic generation of GUI node +public: + AudioSynthNoisePink_F32() : AudioStream_F32(0, NULL) { + plfsr = 0x5EED41F5 + instance_cnt++; + paccu = 0; + pncnt = 0; + pinc = 0x0CCC; + pdec = 0x0CCC; + + enabled = 1; + } + 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); + int enabled = 0; +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 diff --git a/synth_whitenoise_f32.h b/synth_whitenoise_f32.h index f7e01ce..2f6fa36 100644 --- a/synth_whitenoise_f32.h +++ b/synth_whitenoise_f32.h @@ -41,6 +41,8 @@ class AudioSynthNoiseWhite_F32 : public AudioStream_F32 { +//GUI: inputs:0, outputs:1 //this line used for automatic generation of GUI node +//GUI: shortName:whitenoise //this line used for automatic generation of GUI node public: AudioSynthNoiseWhite_F32() : AudioStream_F32(0, NULL) { level = 0;