Added duplicity backup makefile
[mirrors/Programs.git] / arduino / Sound.pde
CommitLineData
c20f5792
H
1/*
2 * Bit-bang sound
3 * <~~Harvie 2oo8
4 */
5
6#define sndout 13
7
8void 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
20float 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
36void sound_test() { //Check da' sound ;))
37 sound(sndout, 440, 500);
38 sound(sndout, get_tone('a', 1), 500);
39 return;
40}
41
42void play_melody(char sndpin, char *melody, char octave, int duration) {
43 for(char i=0;melody[i]!=0;i++) sound(sndpin, get_tone(melody[i], octave), duration);
44 return;
45}
46
47void stupnice_priklad() {
48 char oktava = 1;
49 int delka = 500;
50 char stupnice[]="cdefgab";
51
52 play_melody(sndout, stupnice, oktava, delka);
53 sound(sndout, get_tone(stupnice[0], oktava+1), delka); //C
54}
55
56void setup() { // run once, when the sketch starts
57
58}
59
60void loop() { // run over and over again
61 //delay(10000); return;//Silence! I'll kill you!
62 //--->Fun with sound (uncomment one):
63 //sound_test();
64 //stupnice_priklad();
65 //while(1) sound(sndout, rand()/20, 10);
66 //int i=0; while(1) sound(sndout, i++, 10);
67 //float i=0; while(1) sound(sndout, sin(i+=0.01)*600, 10);
68 //float i=0; while(1) { sound(sndout, sin(i+=0.01)*550, 10); sound(sndout, cos(i)*400, 10); }
69 //sound(sndout, 440, 30000);
70
71 int octave, duration;
72 //You can use lot of more old siemens/ericson monotone ringtones found on net, google or http://www.gsmarena.com/ringlist.php3?idMaker=3
73 char melody[]="CCDEECCFFAAGGE-CCDEGGEFEDDCC-CCDEECCFFAAGGE-CCDEGGEFEDDC"; octave=1; duration=300; //Bob Marley's Redemption song - intro ;D
74 //char melody[]="GGGGEEEEEEEEEE" "EEEEEEEEGGCCCC" "DEEEEEEEE----FFFF" "FFFFFFFEEEE" "EEEEDDDD" "EEDDDDGGGGEEEE" "EEEEEEEEEEEEEE" "GGCCCCDEEEEEEEE----" "FFFFFFFFFFF" "EEEEEEGG" "GGFFDDCCCC"; octave=2; duration=100; //Jingle Bells
75 //char melody[]="D-F-GG-D-F-GGG--D-F-GG-F-DD"; octave=2; duration=180; //Depp Purple - Smoke on the water //alternative: "cpdpfppcpepffppcpdpfppdpc"
76 //char melody[]="CC-E-F-AGG-E-C-AFFFGG--FFFGAPCCCC"; octave=3; duration=180; //The Simpsons theme
77 //char melody[]="CFGG-GG-GG-GC-------CFGGG-GG-GG-GD"; octave=2; duration=300; //X-Files theme
78 //char melody[]="CDCEE--EE-EE----EEDEGG--EDECC"; octave=2; duration=300; //Beatles - Hard days night
79 //char melody[]="HHHH--AAGGAAHHAAGGAAGG--GG-----HHHH--GG--AAHHGGAA--"; octave=2; duration=300; //Offspring - Why don't you get a job
80 //char melody[]="AA-AEEGGAA-ADDBBAA-AEEGGAA-ADDBB"; octave=2; duration=300; //Off Spring - Pretty Flyv
81 //char melody[]="AAgacfaAgadDdcAgacfaAgadDdc AgaaffGv"; octave=2; duration=200; //Eiffel 65 - Blue
82 //char melody[]="HHHHHHAAAAAAGGAAA-HHHHHH" "AAAAAAFFGGEE-HHHHHHAAAAAA" "AAAAAA-HHHHHHAAAAAAFFGGEE"; octave=2; duration=300; //Bob Dylan - Knockin On Heavens Door
83 //char melody[]="bBbBpFFpbepdpcpbBbBpFpBepdpcpbBbBp FpbepdpbepCC"; octave=1; duration=200; //Star Wars
84 //char melody[]="f p c p d p e p f p c p d p e p f p c p d p e p F p p g c e g e G p g c f g f GG"; octave=2; duration=100; //NBA
85
86 play_melody(sndout, melody, octave, duration);
87 delay(2000);
88}
89
This page took 0.498414 seconds and 4 git commands to generate.