Can someone tell me what I am doing wrong?
I am trying to play 3 plucked strings more or less at the same time, but as soon as I uncomment the lines to play s1 or s2 the sound dies.
Any help is appreciated.
/**
* Trying to play 3 plucked strings at the same time
*
*
*
**/
#include "avr/pgmspace.h"
// table of 2 octaves 3 & 4
// Octave 3: C=46, C#=48, D=52, D#=54, E=57, F=61, F#=65, G=68, G#=72, A=76, A#=80, B=86
// Octave 4: C=91, C#=97, D=103, D#=109, E=114, F=120, F#=127, G=134, G#=147, A=155, A#=162, B=170
PROGMEM prog_uchar notes[] = {
46, 48, 52, 54, 57, 61, 65, 68, 72, 76, 80, 86,
91, 97, 103, 109, 114, 120, 127, 134, 147, 154, 161, 170,
182
};
// usage: v = pgm_read_byte_near(notes + nc);
#define SAMPLING_RATE 11025
//#define DELAY_TIME 100000/SAMPLING_RATE
#define DELAY_TIME 20000/SAMPLING_RATE
uint16_t v;
uint16_t vc;
uint8_t nbh;
int32_t avg;
long cnt;
// String 1
byte s1;
uint8_t N1;
int16_t buf1[250];
uint8_t bh1;
uint16_t f1;
uint16_t T1;
uint32_t iT1;
uint32_t iTMax1;
// String 2
byte s2;
uint8_t N2;
int16_t buf2[250];
uint8_t bh2;
uint16_t f2;
uint16_t T2;
uint32_t iT2;
uint32_t iTMax2;
// String 3
byte s3;
uint8_t N3;
int16_t buf3[250];
uint8_t bh3;
uint16_t f3;
uint16_t T3;
uint32_t iT3;
uint32_t iTMax3;
// -------------------------------------------------------------------------------------------------------------
// setup()
// -------------------------------------------------------------------------------------------------------------
void setup(){
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(13, OUTPUT);
PORTD = 0;
s1 = 0; // 0 = active
s2 = 0; // 0 = active
s3 = 0; // 0 = active
cnt = 0;
}
// -------------------------------------------------------------------------------------------------------------
// loop()
// -------------------------------------------------------------------------------------------------------------
void loop(){
cnt++;
if (cnt == 10000)
cnt=0;
// pluck string at pos 100
if (s1 == 0 && cnt > 100) {
f1 = 46; // freq
T1 = 200; // duration
// set buffer size
N1 = SAMPLING_RATE / f1; // f == 44Hz..65535Hz
// fill the buffer with white noise
for (uint8_t i=0; i!=N1; i++) {
buf1[i] = (int16_t) random(-32768,32767);
}
bh1 = 0;
iTMax1 = SAMPLING_RATE * T1;
iT1 = 0;
s1 = 1;
}
else {
// do something that takes the same time
//delayMicroseconds(5);
}
// pluck string at pos 4000
if (s2 == 0 && cnt > 4000) {
f2 = 57; // freq
T2 = 200; // duration
// set buffer size
N2 = SAMPLING_RATE / f2; // f == 44Hz..65535Hz
// fill the buffer with white noise
for (uint8_t i=0; i!=N2; i++) {
buf2[i] = (int16_t) random(-32768,32767);
}
bh2 = 0;
iTMax2 = SAMPLING_RATE * T2;
iT2 = 0;
s2 = 1;
}
else {
// do something that takes the same time
//delayMicroseconds(5);
}
// pluck string at pos 8000
if (s3 == 0 && cnt > 8000) {
//Serial.println("Sound started.");
f3 = 68; // freq
T3 = 200; // duration
// set buffer size
N3 = SAMPLING_RATE / f3; // f == 44Hz..65535Hz
// fill the buffer with white noise
for (uint8_t i=0; i!=N3; i++) {
buf3[i] = (int16_t) random(-32768,32767);
}
bh3 = 0;
iTMax3 = SAMPLING_RATE * T3;
iT3 = 0;
s3 = 1;
}
else {
// do something that takes the same time
//delayMicroseconds(5);
}
v = 0;
vc = 0;
/*
if (s1 == 1) {
v += (int8_t) (buf1[bh1] >> 8);
vc++;
nbh = bh1!=N1-1 ? bh1+1 : 0;
avg = buf1[bh1] + (int32_t)buf1[nbh];
avg = (avg << 10) - avg; // subtract avg more than once to get faster volume decrease
buf1[bh1] = avg >> 11; // no division, just shift
bh1 = nbh;
iT1++;
if (iT1 == iTMax1) {
s1 = 0;
}
}
else {
// do something that takes the same time
//delayMicroseconds(10);
}
*/
/*
if (s2 == 1) {
v += (int8_t) (buf2[bh2] >> 8);
vc++;
nbh = bh2!=N2-1 ? bh2+1 : 0;
avg = buf2[bh2] + (int32_t)buf2[nbh];
avg = (avg << 10) - avg; // subtract avg more than once to get faster volume decrease
buf2[bh2] = avg >> 11; // no division, just shift
bh2 = nbh;
iT2++;
if (iT2 == iTMax2) {
s2 = 0;
}
}
else {
// do something that takes the same time
//delayMicroseconds(10);
}
*/
if (s3 == 1) {
v += (int8_t) (buf3[bh3] >> 8);
vc++;
nbh = bh3!=N3-1 ? bh3+1 : 0;
avg = buf3[bh3] + (int32_t)buf3[nbh];
avg = (avg << 10) - avg; // subtract avg more than once to get faster volume decrease
buf3[bh3] = avg >> 11; // no division, just shift
bh3 = nbh;
iT3++;
if (iT3 == iTMax3) {
s3 = 0;
}
}
else {
// do something that takes the same time
//delayMicroseconds(10);
}
v = v / vc; // >> (PINB & B00000111); // tremolo effect
sendTo8bitDAC(v); // preferably by port operation, e. g. "PORTD = v;"
delayMicroseconds(DELAY_TIME);
}
void sendTo8bitDAC(int8_t v) {
PORTD = v >> 2; // we use a 6 bit R2R ladder
}