Commit | Line | Data |
---|---|---|
fe4da282 H |
1 | /* |
2 | * Bit-bang sound | |
3 | * <~~Harvie 2oo8 | |
4 | */ | |
5 | ||
6 | #define sndout 13 | |
7 | ||
8 | void sound(char sndpin, float freq, float duration) { //Play bit-bang sound | |
9 | if(duration<=0) return; if(freq<=0) { delay(duration); return; } | |
10 | freq=((1000000/2)/freq); //Convert freq to delay (us) | |
11 | duration*=1000; //Convert duration to us | |
12 | pinMode(sndpin, OUTPUT); | |
13 | for(;duration>0;duration-=2*freq) { | |
14 | digitalWrite(sndpin, HIGH); delayMicroseconds(freq); | |
15 | digitalWrite(sndpin, LOW); delayMicroseconds(freq); | |
16 | } | |
17 | pinMode(sndpin, INPUT); //Close pin to avoid noise (optional) | |
18 | } | |
19 | ||
20 | float get_tone(char tone, char octave) { //Return tone frequency or 0 | |
21 | if(octave+tone<=0) return 0; | |
22 | float freq; | |
23 | switch (tone) { //THX2MS GW-BASIC Reference 4freqs muhaha ;D | |
24 | case 'c': case 'C': freq=130.81; break; | |
25 | case 'd': case 'D': freq=146.83; break; | |
26 | case 'e': case 'E': freq=164.81; break; | |
27 | case 'f': case 'F': freq=174.61; break; | |
28 | case 'g': case 'G': freq=196; break; | |
29 | case 'a': case 'A': freq=220; break; | |
30 | case 'b': case 'B': freq=246.94; break; | |
31 | default: return 0; | |
32 | } | |
33 | return (freq*pow(2,octave-1)); | |
34 | } | |
35 | ||
36 | void play_melody(char sndpin, char *melody, char octave, int duration) { | |
37 | for(char i=0;melody[i]!=0;i++) sound(sndpin, get_tone(melody[i], octave), duration); | |
38 | return; | |
39 | } | |
40 | ||
41 | void setup() { // run once, when the sketch starts | |
42 | Serial.begin(115200); | |
43 | } | |
44 | ||
45 | void play_drum(char sndpin, char drum) { | |
46 | char c; int i; float f; | |
47 | switch (drum) { | |
48 | //BassDrums | |
49 | case 'b': for(f=0;f<30;f+=4) {sound(sndpin, 60, 20); delay(f);} break; | |
50 | case 'B': for(i=0;i<=150;i+=10) sound(sndpin, i, 10); break; | |
51 | case 'd': for(i=150;i>=0;i-=10) sound(sndpin, i, 10); break; | |
52 | case 'D': for(i=100;i>=40;i-=10) sound(sndpin, i, 6); break; | |
53 | //Snares | |
54 | case 's': for(f=0;f<1;f+=0.1) sound(sndpin, sin(f)*1000, 5); break; | |
55 | case 'S': for(f=0;f<1;f+=0.1) sound(sndpin, sin(f)*1000+(rand()/100), 5); break; | |
56 | case 'z': for(f=0;f<1;f+=0.15) { sound(sndpin, sin(f)*1000+(rand()/1000), 5); delay(f*10); } break; | |
57 | //Crashes | |
58 | case 'c': for(f=1.2;f<1.5;f+=0.01) { sound(sndpin, 700+(rand()/500), 2); delay(1/sin(f)); } break; | |
59 | case 'C': for(f=1;f<1.3;f+=0.01) { sound(sndpin, (70+(rand()/500))*(f/0.1), 2); /*delay(1/sin(f));*/ } break; | |
60 | //Ambient | |
61 | case 'r': for(i=0;i<300;i++) sound(sndpin, rand()*100, 5); break; | |
62 | //Intros | |
63 | case 'i': for(i=0;i<150;i++) sound(sndpin, i*100, 5); break; | |
64 | case 'I': for(f=0;f<1;f+=0.01) {sound(sndout, sin(f)*550, 10); sound(sndout, cos(f)*400, 10); } break; | |
65 | //Outros | |
66 | case 'o': for(f=1;f>0.3;f-=0.01) { sound(sndpin, sin(f)*1000+(rand()/100), 5); delay(40/f); } break; | |
67 | default: delay(100); | |
68 | } | |
69 | } | |
70 | ||
71 | void play_rythm(char sndpin, char *rythm) { | |
72 | for(char i=0;rythm[i]!=0;i++) play_drum(sndpin, rythm[i]); | |
73 | return; | |
74 | } | |
75 | ||
76 | void loop() { // run over and over again | |
77 | //delay(10000); return;//Silence! I'll kill you! | |
78 | //while(1) sound(sndout, rand()/20, 10); | |
79 | //int i=0; while(1) sound(sndout, i++, 10); | |
80 | //float i=0; while(1) sound(sndout, sin(i+=0.01)*600, 10); | |
81 | //float i=0; while(1) { sound(sndout, sin(i+=0.01)*550, 10); sound(sndout, cos(i)*400, 10); } | |
82 | //sound(sndout, 440, 30000); | |
83 | ||
84 | //play_drum(sndout, 'D'); delay(1000); return; | |
85 | //play_drum(sndout, Serial.read()); return; | |
86 | ||
87 | /* | |
88 | play_rythm(sndout, "r iI "); | |
89 | play_melody(sndout, "c c c cc dd d eee ", 1, 100); | |
90 | play_rythm(sndout, "B D B d s B D s B d c B s D C B d S B D S B b s d So "); | |
91 | */ | |
92 | ||
93 | sound(sndout, analogRead(0), 10); return; | |
94 | ||
95 | /* | |
96 | char rythm[]= | |
97 | "b " | |
98 | "D D D d D d " | |
99 | "D D D d D d " | |
100 | "i " | |
101 | "D D D d D d " | |
102 | "D D D d D d " | |
103 | "I " | |
104 | "D s D S D d " | |
105 | "D D z d D d " | |
106 | "ddDDb " | |
107 | "D D D d D s " | |
108 | "D D D d D s " | |
109 | "o"; | |
110 | ||
111 | play_rythm(sndout, rythm); | |
112 | */ | |
113 | /*int octave, duration; | |
114 | char melody[]="CCDEECCFFAAGGE-CCDEGGEFEDDCC-CCDEECCFFAAGGE-CCDEGGEFEDDC"; octave=1; duration=300; //Bob Marley's Redemption song - intro ;D | |
115 | play_melody(sndout, melody, octave, duration);*/ | |
116 | delay(4000); | |
117 | } | |
118 |