I've been messing with audio.h a bit and decided to make a new effect. I stated with the code for the multiply effect and made this sample and hold / rate reducer / bit crusher.
It works ok but I have some questions about how.
"pa" is a pointer to the audio block but also the counter going through the block and the contents of the block?
What do "*pa++ = audio_in0;" and "mod_in0 = *pb++;" mean exactly? It's incrementing but also... what? They come from the multiply code, just renamed.
effect_sh.h:
effect_sh.cpp:
Put these in your audio folder and add the line "#include "effect_sh.h"" to audio.h to try it out
sketch:
It works ok but I have some questions about how.
"pa" is a pointer to the audio block but also the counter going through the block and the contents of the block?
What do "*pa++ = audio_in0;" and "mod_in0 = *pb++;" mean exactly? It's incrementing but also... what? They come from the multiply code, just renamed.
effect_sh.h:
Code:
#ifndef effect_sh_h_
#define effect_sh_h_
#include "AudioStream.h"
#include "utility/dspinst.h"
#define SAMPLE_AND_HOLD 0
#define RATE_RECUCTION 1
#define BIT_REDUCTION 2
class AudioEffectSH : public AudioStream
{
public:
AudioEffectSH() : AudioStream(2, inputQueueArray) { }
virtual void update(void);
private:
audio_block_t *inputQueueArray[2];
// short fx_mode;
};
#endif
effect_sh.cpp:
Code:
#include "effect_sh.h"
void AudioEffectSH::update(void){
audio_block_t *blocka, *blockb;
uint32_t *pa, *pb, *end;
static int32_t audio_in0, paudio_in0; //, a56, a78;
static short ch1,change0;
static int32_t mod_in0, pmod_in0,b_wut; //, b56, b78;
static uint32_t prev_sh, ccnt,hold_cnt,rate_cnt,rate_cnt_max;
blocka = receiveWritable(0);
blockb = receiveReadOnly(1);
if (!blocka) {
if (blockb) release(blockb);
return;
}
if (!blockb) {
release(blocka);
return;
}
pa = (uint32_t *)(blocka->data);
pb = (uint32_t *)(blockb->data);
end = (pa + AUDIO_BLOCK_SAMPLES/2);
while (pa < end) {
paudio_in0=audio_in0;
pmod_in0=mod_in0;
mod_in0 = *pb++;
if (pmod_in0 >=0 && mod_in0 <0){
change0=1;
rate_cnt_max=rate_cnt;
rate_cnt=0;
}
else{
change0=0;
rate_cnt++;
}
if (change0==1){
audio_in0 = *pa;
}
if (change0==0){
audio_in0 = paudio_in0;
}
*pa++ = audio_in0;
/*
if (millis() - prev_sh>300){
prev_sh=millis();
// Serial.println(hold_len);
Serial.println(rate_cnt_max);
Serial.println();
}
*/
}
transmit(blocka);
release(blocka);
release(blockb);
}
Put these in your audio folder and add the line "#include "effect_sh.h"" to audio.h to try it out
sketch:
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
long prev;
AudioSynthNoisePink pink1;
AudioOutputAnalog dac1;
AudioEffectSH SH1;
AudioSynthWaveformSine sine1;
AudioSynthWaveformSine modulator_sine2;
AudioConnection patchCord1(sine1, 0, SH1,0);
AudioConnection patchCord3(modulator_sine2, 0, SH1,1);
AudioConnection patchCord2(SH1, dac1);
void setup(void)
{
Serial.begin(9600);
AudioMemory(20);
pink1.amplitude(1);
sine1.amplitude(1);
sine1.frequency(100);
modulator_sine2.amplitude(1);
modulator_sine2.frequency(100);
analogReadResolution(12);
analogReadAveraging(21);
}
void loop(void)
{
uint32_t a1 = analogRead(A1)*2;
uint32_t a2 = analogRead(A2)*3;
sine1.frequency(a1);
modulator_sine2.frequency(a2);
if (millis() - prev>200){
prev=millis();
Serial.println(a2);
}
}