commit b1adb512d6ddff7e6d3695f75460fa338f5e5676 Author: Joachim Fenkes Date: Tue Jul 31 21:42:53 2012 +0200 initial AVR code diff --git a/avr/.cproject b/avr/.cproject new file mode 100644 index 0000000..4200d6a --- /dev/null +++ b/avr/.cproject @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/avr/.project b/avr/.project new file mode 100644 index 0000000..b7afda8 --- /dev/null +++ b/avr/.project @@ -0,0 +1,83 @@ + + + noiseplug + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + ?name? + + + + org.eclipse.cdt.make.core.append_environment + true + + + org.eclipse.cdt.make.core.autoBuildTarget + all + + + org.eclipse.cdt.make.core.buildArguments + + + + org.eclipse.cdt.make.core.buildCommand + make + + + org.eclipse.cdt.make.core.buildLocation + ${workspace_loc:/mucke/Release} + + + org.eclipse.cdt.make.core.cleanBuildTarget + clean + + + org.eclipse.cdt.make.core.contents + org.eclipse.cdt.make.core.activeConfigSettings + + + org.eclipse.cdt.make.core.enableAutoBuild + false + + + org.eclipse.cdt.make.core.enableCleanBuild + true + + + org.eclipse.cdt.make.core.enableFullBuild + true + + + org.eclipse.cdt.make.core.fullBuildTarget + all + + + org.eclipse.cdt.make.core.stopOnError + true + + + org.eclipse.cdt.make.core.useDefaultBuildCmd + true + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + de.innot.avreclipse.core.avrnature + + diff --git a/avr/noiseplug.c b/avr/noiseplug.c new file mode 100644 index 0000000..97c4ead --- /dev/null +++ b/avr/noiseplug.c @@ -0,0 +1,111 @@ +/* + * main.c + * + * Created on: 30.10.2011 + * Author: dojoe + */ + +#include +#include +#include +#include +#include +#include + +//register uint8_t int_ctr asm("r18"); +uint8_t int_ctr; + +#if defined(__AVR_ATmega32U4__) +ISR(TIMER0_OVF_vect) +#elif defined(__AVR_ATtiny9__) +ISR(TIM0_OVF_vect) +#else +#error "warghs" +#endif +{ + int_ctr = (int_ctr + 1) & 3; +} + +#define NOISEPLUG_BIATCH +#include "sound1.c" + +#if 0 +static inline uint8_t next_sample(const uint32_t t) +{ +// return t*(42&t>>10); +// return t*((42&t>>10)%14); +// return (t*5&t>>7)|(t*3&t>>10); +// return t*9&t>>4|t*5&t>>7|t*3&t/1024; +// return (t*9&t>>4|t*5&t>>7|t*3&t/1024)-1; +// return t>>4|t&((t>>5)/(t>>7-(t>>15)&-t>>7-(t>>15))); +// return (int)(t/1e7*t*t+t)%127|t>>4|t>>5|t%127+(t>>16)|t; + uint8_t a = t>>6&1?t>>5:-t>>4; + uint8_t b = t>>6^t>>8|t>>12|t&63; + + return random(); +// return ((t>>6^t>>8|t>>12|t&63)&127)+((t>>6&1?t>>6:-t>>5)&63); +// return t << 3; +} +#endif + +int main() +{ +#if defined(__AVR_ATmega32U4__) +#define SAMPLE_L OCR0A + clock_prescale_set(clock_div_2); + + DDRB = 1 << PB7 | 1 << PB2; // PB7 == OC0A + TCNT0 = 0; + OCR0A = 0; + TCCR0A = 3 << WGM00 | 2 << COM0A0; // Fast PWM mode, positive polarity + TCCR0B = 1 << CS00; // Select main clock, no prescaling + TIMSK0 = 1 << TOIE0; // enable overflow interrupt + TIFR0 = 1 << TOV0; // make sure it happens + + set_sleep_mode(SLEEP_MODE_IDLE); + sei(); +#elif defined(__AVR_ATtiny9__) +#define SAMPLE_L OCR0AL + + CCP = 0xD8; + CLKMSR = 0; + CCP = 0xD8; + CLKPSR = 0; + PUEB = 0; + DDRB = (1 << PB0) | (1 << PB2); + TCNT0 = 0; + OCR0A = 0; + TCCR0A = (1 << WGM00) | (2 << COM0A0); + TCCR0B = (1 << WGM02) | (1 << CS00); + TCCR0C = 0; + TIMSK0 = 1 << TOIE0; + + set_sleep_mode(SLEEP_MODE_IDLE); + sei(); + TIFR0 = 1 << TOV0; +#else +#error "not supported" +#endif + + uint8_t sample = 0; + +#ifdef __AVR_ATmega32U4__ + DDRE = 1 << PE6; + uint16_t t = 0; +#endif + + while (1) { + sleep_mode(); + if (int_ctr == 0) { + //PORTB = 1 << PB2; + SAMPLE_L = sample; + sample = next_sample(); + //PORTB = 0; + +#ifdef __AVR_ATmega32U4__ + PORTE = (t & 4096) ? 1 << PE6 : 0; + t++; +#endif + } + } +} diff --git a/avr/sound.c b/avr/sound.c new file mode 100644 index 0000000..481c1e7 --- /dev/null +++ b/avr/sound.c @@ -0,0 +1,18 @@ + + + + +int main(r) +{ + +unsigned short rnd = 13373; +while(1) +{ + unsigned char f1 = (rnd&(3<<13))>>13; + unsigned char f2 = (rnd&(3<<10))>>10; + rnd <<=1; + rnd |= f1^f2; + putchar(rnd); + +} +} diff --git a/avr/sound1.c b/avr/sound1.c new file mode 100644 index 0000000..1304007 --- /dev/null +++ b/avr/sound1.c @@ -0,0 +1,180 @@ +#include + +#ifndef PROGMEM +#define PROGMEM /* nix */ +#endif + +#define arpeggio_DELAY 128 + +#define BIT(x,n) (((x)&(1<<(n)))>>(n)) +#define SO(x) (sizeof((x))/sizeof(*(x))) + +#define A1 0 +#define As1 1 +#define B1 2 +#define C1 3 +#define Cs1 4 +#define D1 5 +#define Ds1 6 +#define E1 7 +#define F1 8 +#define Fs1 9 +#define G1 10 +#define Gs1 11 +#define A2 12 +#define Aa2 13 +#define B2 14 +#define C2 15 +#define Cs2 16 +#define D2 17 +#define Ds2 18 +#define E2 19 +#define F2 20 +#define Fs2 21 +#define G2 22 +#define Gs2 23 +#define HOLD 24 + +static const PROGMEM uint8_t sin[] = {0, 49, 97, 141, 180, 212, 235, 250, 254, 250, 235, 212, 180, 141, 97, 49 }; +static const PROGMEM uint8_t octave_delay[] = { 36, 34, 32, 31, 29, 27, 26, 24, 23, 22, 20, 19, 18, 17, 16, 15, 14, 14, 13, 12, 11, 11, 10, 10 }; +static const PROGMEM struct { uint8_t a; uint8_t b; } synth[] = { { 7, 6}, {7, 5}, {7,5}, {6,5} }; +static const PROGMEM uint8_t arpeggiobase[] = { 3, 4, 4, 5 }; +static const PROGMEM uint8_t bassdrum[] = { 1, 1, 1, 1 }; +static const PROGMEM uint8_t snare[] = { 1, 1, 1, 1 }; +static const PROGMEM uint8_t melody[] = +{ + D2, 0, D2, 0, 0, 0, 0, 0, D2, 0, + A1, 0, B1, 0, D2, 0, D2, 0, D2, 0 +}; + +static inline uint8_t next_note() +{ + + static uint8_t idx=0; + + const uint8_t v = melody[idx++]; + + if(idx == SO(melody)) + idx = 0; + + return v; +} + +static inline uint8_t next_rnd() +{ + static unsigned short rnd = 13373; + const uint8_t f1 = (rnd&(3<<13))>>13; + const uint8_t f2 = (rnd&(3<<10))>>10; + rnd <<=1; + rnd |= f1^f2; + + return sin[((uint8_t)rnd)%SO(sin)]; +} + +static inline uint8_t next_sin(const uint8_t step) +{ + static uint8_t sinoff=SO(sin)-1; + sinoff += step; + sinoff &= SO(sin) - 1; + return sin[sinoff]; +} + + +static inline uint8_t next_sample() +{ +static uint16_t t=0; + +static uint8_t t8=0; +static uint8_t barevent=0; +static uint8_t bars=0; + +static uint8_t pc = 0; +static uint8_t arpeggiocnt = 1; + +static uint8_t next_sin_time = 0; +static uint8_t current_tone = 0; +static uint8_t current_tone_base = 12; + +static unsigned short snaredelay; +static unsigned short bassdelay; + +static uint8_t synth1; +static uint8_t synth2; + +if(t%1024 == 0) +{ + // implicit rollover of t roughly every 2 seconds + if(t==0) t8 = 0; + else ++t8; + + // determine which note we're playing + barevent |= 8; + if(t8%2 == 0) barevent |= 4; + if(t8%4 == 0) barevent |= 2; + if(t8%8 == 0) barevent |= 1; +} +else barevent = 0; + +if(barevent & 8) +{ + current_tone = current_tone_base = next_note(); +} + +// increment bar counter +if(barevent & 1) ++bars; + +// increment pattern counter +if(bars % 8 == 0) +{ + ++pc; + pc %= SO(arpeggiobase); +} + +// increment arpeggio +if(t % arpeggio_DELAY == 0) +{ + // arpeggio + ++arpeggiocnt; + arpeggiocnt &= 3; + current_tone = current_tone_base + 4 * arpeggiocnt; +} + +// render synth + +synth1 = ((t&(t>>(synth[pc].a))) | (t&(t>>(synth[pc].b))) << 1); + +if(t == next_sin_time) +{ + next_sin_time += octave_delay[current_tone]; + synth2 = next_sin(5); +} + +// mix two synth lines +unsigned int mix = (synth1>>1) | (synth2>>1); + +// load or decrement snare delay +if(barevent & 8 && snare[pc]) snaredelay = 800; +else if(snaredelay > 0) --snaredelay; + +// add snare drum +if(snaredelay>0) mix = (next_rnd() & 7) << 3; + + +// load or decrement bass delay +if(barevent & 4 && bassdrum[pc]) bassdelay = 800; +else if(bassdelay > 0) --bassdelay; + +// add bass drum +if(bassdelay>0) mix = next_sin(1); + +++t; +// here comes the noize! +return mix; +} + +#ifndef NOISEPLUG_BIATCH +int main() +{ + while(1) putchar(next_sample()); +} +#endif diff --git a/avr/sound3.c b/avr/sound3.c new file mode 100644 index 0000000..320750f --- /dev/null +++ b/avr/sound3.c @@ -0,0 +1,35 @@ + +#define SO(x) (sizeof((x))/sizeof(*(x))) + +static unsigned char sin[] = {0, 49, 97, 141, 180, 212, 235, 250, 254, 250, 235, 212, 180, 141, 97, 49 }; + +unsigned char getRand() +{ + static unsigned short rnd = 13373; + unsigned char f1 = (rnd&(3<<13))>>13; + unsigned char f2 = (rnd&(3<<10))>>10; + rnd <<=1; + rnd |= f1^f2; + return (unsigned char)(rnd&0x0f); +} + +static inline unsigned char rnd_adv() +{ + return sin[getRand()%SO(sin)]; +} + +static inline unsigned char sin_adv() +{ + static unsigned char sinoff=SO(sin)-1; + ++sinoff; + sinoff %= SO(sin); + return sin[sinoff]; +} + +int main() +{ + while(1) + { + putchar(sin_adv()); + } +}