neingeist
/
arduinisten
Archived
1
0
Fork 0

slightly less borken...

master
entropia 15 years ago
parent 1c6ccf8eaf
commit 1d5191aafd

@ -43,24 +43,27 @@ void setup() {
int play (uint16_t f, uint8_t T) { int play (uint16_t f, uint8_t T) {
#define MAGIC_TUNE_FAC 3 #define DIVIDER 32
#define MAGIC_TUNE_FAC 6
if (f == REST) { if (f == REST) {
_delay_ms(T*1000/16*MAGIC_TUNE_FAC); _delay_ms(T*1000/DIVIDER*MAGIC_TUNE_FAC);
} else { } else {
sendKarplusStrongSound(f, T*MAGIC_TUNE_FAC); sendKarplusStrongSound(f, T*MAGIC_TUNE_FAC);
} }
} }
// http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1249193795 // original source from http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1249193795
int sendKarplusStrongSound(uint16_t f /*Hz*/, uint8_t T /* 1/16 sec*/) { int sendKarplusStrongSound(uint16_t f /*Hz*/, uint8_t T /* 1/DIVIDER sec*/) {
uint32_t sr = 11025; uint16_t sr = 22050;
uint8_t N = sr/f; uint8_t N = sr/f;
//uint8_t N=32; // uint8_t N = 32;
//uint32_t sr = (uint32_t) N*f; // uint16_t sr = (uint16_t) N*f;
Serial.print("N = "); Serial.print("N = ");
Serial.print(N, DEC); Serial.print(N, DEC);
Serial.print("sr = ");
Serial.print(sr, DEC);
Serial.println(); Serial.println();
unsigned long millis_alt = millis(); unsigned long millis_alt = millis();
@ -70,19 +73,18 @@ int sendKarplusStrongSound(uint16_t f /*Hz*/, uint8_t T /* 1/16 sec*/) {
buf[i] = random(65536); buf[i] = random(65536);
uint8_t bh = 0; uint8_t bh = 0;
int Tloop = 18 int Tloop = 18; // FIXME: tune
; // FIXME: tune
int dt = (1000000ul)/sr - Tloop; int dt = (1000000ul)/sr - Tloop;
Serial.print("dt = "); Serial.print("dt = ");
Serial.print(dt, DEC); Serial.print(dt, DEC);
Serial.println(); Serial.println();
for (uint32_t i=sr*T/16; i>0; i--) { for (uint32_t i=(uint32_t) sr*T/DIVIDER; i>0; i--) {
// current amplitude to play is the highest byte of buf[bh] // current amplitude to play is the highest byte of buf[bh]
#if 1 #if NOISEINDUSTRIAL
OCR2A = buf[bh] >> 8;
#else
OCR2A = buf[bh]; OCR2A = buf[bh];
#else
OCR2A = buf[bh] >> 8;
#endif #endif
// delay or do something else for <dt> usecs // delay or do something else for <dt> usecs
@ -97,13 +99,13 @@ int sendKarplusStrongSound(uint16_t f /*Hz*/, uint8_t T /* 1/16 sec*/) {
} }
// calculate avg // calculate avg
unsigned long avg = ((unsigned long) buf[bh] + (unsigned long) buf[nbh])/2; uint32_t avg = ((uint32_t) buf[bh] + (uint32_t) buf[nbh])/2;
// with gain<1 (1020/1024 ~ 0.996) // with gain<1 (1020/1024 ~ 0.996)
avg *= 1020; avg *= 1020;
avg /= 1024; avg /= 1024;
// put back in buffer // put back in buffer
buf[bh] = (int) avg; buf[bh] = (uint16_t) avg;
bh = nbh; bh = nbh;
} }