|
@@ -5,31 +5,6 @@
|
|
#include <avr/io.h>
|
|
#include <avr/io.h>
|
|
|
|
|
|
#define PI 3.14159265
|
|
#define PI 3.14159265
|
|
-#define CHANNEL OCR1C
|
|
|
|
-
|
|
|
|
-volatile uint16_t sample;
|
|
|
|
-uint16_t lastSample;
|
|
|
|
-
|
|
|
|
-const int sounddata_length=200;
|
|
|
|
-
|
|
|
|
-const unsigned char sounddata_data[] PROGMEM = {128,
|
|
|
|
-128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
|
|
|
|
-128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
|
|
|
|
-128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
|
|
|
|
-128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
|
|
|
|
-128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
|
|
|
|
-128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
|
|
|
|
-128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
|
|
|
|
-128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
|
|
|
|
-128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
|
|
|
|
-128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 129, 127, 129, 128, 127, 133,
|
|
|
|
-117, 109, 125, 121, 116, 132, 140, 126, 114, 114, 116, 120, 114, 93, 73, 66, 76, 116, 142, 129,
|
|
|
|
-128, 129, 120, 119, 118, 104, 87, 123, 181, 194, 196, 198, 189, 176, 160, 162, 172, 164, 164, 183,
|
|
|
|
-197, 188, 168, 167, 170, 165, 185, 209, 206, 196, 196, 199, 185, 162, 156, 167, 176, 173, 170, 166,
|
|
|
|
-151, 142, 140, 134, 130, 127, 113, 86, 67, 66, 69, 75, 73, 75, 86, 90, 91, 84, 65, 48,
|
|
|
|
-41, 30, 26, 56, 91, 88, 72, 70, 73, 82, 89, 73, 57, 60, 74, 89, 92, 77, 63, 60,
|
|
|
|
-53, 47, 56, 64, 63, 61, 56, 54, 52, 36, 16, 22, 51, 66, 67, 70, 76, 88, 99, 92,
|
|
|
|
-77, 74, 85, 100, 106, 97, 83, 85, 96, 108, 133, 160, 164};
|
|
|
|
|
|
|
|
void delay_us(int count) {
|
|
void delay_us(int count) {
|
|
while(count--) {
|
|
while(count--) {
|
|
@@ -37,202 +12,235 @@ void delay_us(int count) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-void beeps() {
|
|
+int voices = 0;
|
|
-
|
|
+double frequency = 0;
|
|
-
|
|
+int volume = 0;
|
|
-
|
|
+int position = 0;
|
|
-
|
|
+
|
|
-
|
|
+double frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
|
-
|
|
+int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
|
-
|
|
+bool sliding = false;
|
|
-
|
|
+#define RANGE 1000
|
|
-
|
|
+volatile int i=0;
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-play_notes();
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+void beeps() {
|
|
|
|
+ play_notes();
|
|
}
|
|
}
|
|
|
|
|
|
-void play_note(float freq, int length) {
|
|
+void send_freq(double freq, int vol) {
|
|
- DDRB |= (1<<7);
|
|
+ int duty = (((double)F_CPU) / freq);
|
|
- PORTB &= ~(1<<7);
|
|
+ ICR3 = duty;
|
|
|
|
+ OCR3A = duty >> (0x10 - vol);
|
|
|
|
+}
|
|
|
|
|
|
- if (freq > 0) {
|
|
+void stop_all_notes() {
|
|
- int frequency = 1000000/freq;
|
|
+ voices = 0;
|
|
- ICR1 = frequency;
|
|
+ TCCR3A = 0;
|
|
- OCR1C = frequency >> 1;
|
|
+ TCCR3B = 0;
|
|
|
|
+ frequency = 0;
|
|
|
|
+ volume = 0;
|
|
|
|
|
|
- TCCR1A = _BV(COM1C1) | _BV(WGM11);
|
|
+ for (int i = 0; i < 8; i++) {
|
|
- TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
|
|
+ frequencies[i] = 0;
|
|
|
|
+ volumes[i] = 0;
|
|
}
|
|
}
|
|
-
|
|
|
|
- for (int i = 0; i < length; i++) {
|
|
|
|
- _delay_us(50000);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- TCCR1A &= ~(_BV(COM1C1));
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
+void stop_note(double freq) {
|
|
-ISR(TIMER1_COMPA_vect) {
|
|
+ for (int i = 7; i >= 0; i--) {
|
|
- if (sample >= sounddata_length) {
|
|
+ if (frequencies[i] == freq) {
|
|
- if (sample == sounddata_length + lastSample) {
|
|
+ frequencies[i] = 0;
|
|
- TIMSK1 &= ~_BV(OCIE1A);
|
|
+ volumes[i] = 0;
|
|
-
|
|
+ for (int j = i; (j < 7); j++) {
|
|
-
|
|
+ frequencies[j] = frequencies[j+1];
|
|
- TCCR1B &= ~_BV(CS10);
|
|
+ frequencies[j+1] = 0;
|
|
- }
|
|
+ volumes[j] = volumes[j+1];
|
|
- else {
|
|
+ volumes[j+1] = 0;
|
|
- OCR1C = sounddata_length + lastSample - sample;
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- else {
|
|
+ voices--;
|
|
- OCR1C = pgm_read_byte(&sounddata_data[sample]);
|
|
+ if (voices < 0)
|
|
|
|
+ voices = 0;
|
|
|
|
+ if (voices == 0) {
|
|
|
|
+ TCCR3A = 0;
|
|
|
|
+ TCCR3B = 0;
|
|
|
|
+ frequency = 0;
|
|
|
|
+ volume = 0;
|
|
|
|
+ } else {
|
|
|
|
+ double freq = frequencies[voices - 1];
|
|
|
|
+ int vol = volumes[voices - 1];
|
|
|
|
+ if (frequency < freq) {
|
|
|
|
+ sliding = true;
|
|
|
|
+ for (double f = frequency; f <= freq; f += ((freq - frequency) / 500.0)) {
|
|
|
|
+ send_freq(f, vol);
|
|
|
|
+ }
|
|
|
|
+ sliding = false;
|
|
|
|
+ } else if (frequency > freq) {
|
|
|
|
+ sliding = true;
|
|
|
|
+ for (double f = frequency; f >= freq; f -= ((frequency - freq) / 500.0)) {
|
|
|
|
+ send_freq(f, vol);
|
|
|
|
+ }
|
|
|
|
+ sliding = false;
|
|
|
|
+ }
|
|
|
|
+ send_freq(freq, vol);
|
|
|
|
+ frequency = freq;
|
|
|
|
+ volume = vol;
|
|
}
|
|
}
|
|
-
|
|
|
|
- ++sample;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-void play_notes() {
|
|
+void init_notes() {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
|
|
+
|
|
|
|
|
|
-
|
|
+
|
|
-
|
|
+
|
|
|
|
|
|
- DDRB |= (1<<7);
|
|
+
|
|
- PORTB &= ~(1<<7);
|
|
+
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
|
|
|
|
-
|
|
+
|
|
- TCCR1A |= _BV(WGM21) | _BV(WGM20);
|
|
|
|
- TCCR1B &= ~_BV(WGM22);
|
|
|
|
|
|
|
|
-
|
|
+
|
|
-
|
|
+
|
|
- TCCR1A = (TCCR2A | _BV(COM2A1)) & ~_BV(COM2A0);
|
|
+
|
|
- TCCR1A &= ~(_BV(COM2B1) | _BV(COM2B0));
|
|
|
|
-
|
|
|
|
- TCCR1B = (TCCR1B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10);
|
|
|
|
|
|
|
|
-
|
|
+
|
|
- OCR1A = pgm_read_byte(&sounddata_data[0]);
|
|
+
|
|
|
|
+
|
|
|
|
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
- cli();
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ISR(TIMER3_COMPA_vect) {
|
|
|
|
+
|
|
|
|
+ if (ICR3 > 0 && !sliding) {
|
|
|
|
+ switch (position) {
|
|
|
|
+ case 0: {
|
|
|
|
+ int duty = (((double)F_CPU) / (frequency));
|
|
|
|
+ ICR3 = duty;
|
|
|
|
+ OCR3A = duty >> 1;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ case 1: {
|
|
|
|
+ int duty = (((double)F_CPU) / (frequency*2));
|
|
|
|
+ ICR3 = duty;
|
|
|
|
+ OCR3A = duty >> 1;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ case 2: {
|
|
|
|
+ int duty = (((double)F_CPU) / (frequency*3));
|
|
|
|
+ ICR3 = duty;
|
|
|
|
+ OCR3A = duty >> 1;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ position = (position + 1) % 3;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
-
|
|
+
|
|
-
|
|
|
|
- TCCR2B = (TCCR2B & ~_BV(WGM13)) | _BV(WGM12);
|
|
|
|
- TCCR2A = TCCR2A & ~(_BV(WGM11) | _BV(WGM10));
|
|
|
|
|
|
|
|
-
|
|
|
|
- TCCR2B = (TCCR2B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10);
|
|
|
|
|
|
|
|
-
|
|
+
|
|
-
|
|
+
|
|
-
|
|
+
|
|
-
|
|
|
|
- OCR2A = 2000;
|
|
|
|
|
|
|
|
-
|
|
+
|
|
- TIMSK1 |= _BV(OCIE2A);
|
|
+
|
|
|
|
+
|
|
|
|
|
|
- sample = 0;
|
|
+
|
|
- sei();
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
-void note(int x, float length) {
|
|
+void play_note(double freq, int vol) {
|
|
- DDRB |= (1<<1);
|
|
+
|
|
- int t = (int)(440*pow(2,-x/12.0));
|
|
+ if (freq > 0) {
|
|
- for (int y = 0; y < length*1000/t; y++) {
|
|
+ DDRC |= (1<<6);
|
|
- PORTB |= (1<<1);
|
|
+
|
|
- delay_us(t);
|
|
+ TCCR3A = (1 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
|
|
- PORTB &= ~(1<<1);
|
|
+ TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
|
|
- delay_us(t);
|
|
+
|
|
|
|
+ if (frequency != 0) {
|
|
|
|
+ if (frequency < freq) {
|
|
|
|
+ for (double f = frequency; f <= freq; f += ((freq - frequency) / 500.0)) {
|
|
|
|
+ send_freq(f, vol);
|
|
|
|
+ }
|
|
|
|
+ } else if (frequency > freq) {
|
|
|
|
+ for (double f = frequency; f >= freq; f -= ((frequency - freq) / 500.0)) {
|
|
|
|
+ send_freq(f, vol);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ send_freq(freq, vol);
|
|
|
|
+ frequency = freq;
|
|
|
|
+ volume = vol;
|
|
|
|
+
|
|
|
|
+ frequencies[voices] = frequency;
|
|
|
|
+ volumes[voices] = volume;
|
|
|
|
+ voices++;
|
|
}
|
|
}
|
|
- PORTB &= ~(1<<1);
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
-void true_note(float x, float y, float length) {
|
|
+
|
|
- for (uint32_t i = 0; i < length * 50; i++) {
|
|
+
|
|
- uint32_t v = (uint32_t) (round(sin(PI*2*i*640000*pow(2, x/12.0))*.5+1 + sin(PI*2*i*640000*pow(2, y/12.0))*.5+1) / 2 * pow(2, 8));
|
|
+
|
|
- for (int u = 0; u < 8; u++) {
|
|
+
|
|
- if (v & (1 << u) && !(PORTB&(1<<1)))
|
|
+
|
|
- PORTB |= (1<<1);
|
|
+
|
|
- else if (PORTB&(1<<1))
|
|
+
|
|
- PORTB &= ~(1<<1);
|
|
+
|
|
- }
|
|
+
|
|
- }
|
|
+
|
|
- PORTB &= ~(1<<1);
|
|
+
|
|
-}
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|