diff --git a/projekte/soundz/karplus_strong_totally_borken/karplus_strong_totally_borken.pde b/projekte/soundz/karplus_strong_totally_borken/karplus_strong_totally_borken.pde index 9df36f3..a503021 100644 --- a/projekte/soundz/karplus_strong_totally_borken/karplus_strong_totally_borken.pde +++ b/projekte/soundz/karplus_strong_totally_borken/karplus_strong_totally_borken.pde @@ -23,42 +23,11 @@ void setup() { // Set initial pulse width to the first sample. OCR2A = 0; + + // DEBUG + Serial.begin(9600); } - -int play (uint16_t f, uint8_t T) { - sendKarplusStrongSound(f, T); -} - - -// http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1249193795 -int sendKarplusStrongSound(const uint16_t f /*Hz*/, const uint8_t T /*sec*/) { - const uint8_t N = 32; - const uint32_t sr = f * N; // sample rate: 800(25Hz)..47619(1488Hz) - - int16_t buf[N]; - for (uint8_t i=0; i0; i--) { - const int8_t v = (int8_t) (buf[bh] >> 8); - - OCR2A = v; - _delay_us(dt); // or do something else for
usecs - - uint8_t nbh = bh!=N-1 ? bh+1 : 0; - int32_t avg = buf[bh] + (int32_t)buf[nbh]; - //avg = (avg << 10) - avg; // subtract avg more than once to get faster volume decrease - avg = (avg << 10) - avg -avg; - buf[bh] = avg >> 11; // no division, just shift - bh = nbh; - } -} - // Frequencies (in Hz) of notes #define FSH_4 370 #define A_4 440 @@ -72,6 +41,81 @@ int sendKarplusStrongSound(const uint16_t f /*Hz*/, const uint8_t T /*sec*/) { #define REST 0 + +int play (uint16_t f, uint8_t T) { + #define MAGIC_TUNE_FAC 3 + if (f == REST) { + _delay_ms(T*1000/16*MAGIC_TUNE_FAC); + } else { + sendKarplusStrongSound(f, T*MAGIC_TUNE_FAC); + } +} + + +// http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1249193795 +int sendKarplusStrongSound(uint16_t f /*Hz*/, uint8_t T /* 1/16 sec*/) { + uint32_t sr = 11025; + uint8_t N = sr/f; + //uint8_t N=32; + //uint32_t sr = (uint32_t) N*f; + + Serial.print("N = "); + Serial.print(N, DEC); + Serial.println(); + + unsigned long millis_alt = millis(); + + int buf[N]; + for (uint8_t i=0; i