/
PlayTune.c
184 lines (184 loc) · 2.99 KB
/
PlayTune.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "stdio.h"
#include "stdlib.h"
#include "dos.h"
#include "graphics.h"
typedef struct
{
int pitch;
char name[3];
}
NOTE;
char song[]="c3 c16 o2 b16 o3 c16 r16 o2 c8 c8 g16 f16 e16 g16 o3 c8 c16 e16"
"d16 c16 d16 r16 o2 d8 d8 d16 c16 o1 b16 o2 d16 g8 g8"
"o2 a16 b16 o3 c16 o2 b16"
"a16 g16 a16 g16"
"f16 e16 f16 e16"
"d16 c16 d16 c16"
"o1 b16 a16 g16 a16"
"b16 o2 c16 d16 e16"
"f16 g16 e8 c8 c8"
"o3 c16 o2 b16 o3 o16 r16 c8 c8"
"c16 o2 b16 a16 c16 o2 b16"
"a16 o2 g16 a16 g16"
"f16 o16 f16 e16"
"d16 c16 d16 c16"
"o1 b16 a16 g16 a16 b16 o2"
"c16 d16 e16 f16 g16 e8 c8 c8"
;
NOTE scale[]={262,"c",
278,"c#",
294,"d",
312,"d#",
330,"e",
349,"f",
371,"f#",
392,"g",
416,"g#",
440,"a",
468,"a#",
494,"b"
};
#define SCALESIZE (sizeof(scale)/sizeof(NOTE))
char *songpointer=NULL;
int octave=1;
int duration=4;
int pitch=-1;
int songindex=0;
void interrupt beNoisy();
main()
{
void interrupt(*oldTimer)();
oldTimer=getvect(0X1c);
setvect(0X1c,beNoisy);
playtune(song);
DoSomething();
setvect(0X1c,oldTimer);
nosound();
}
playtune(song)
char *song;
{
disable();
songindex=0;
songpointer=song;
enable();
}
void interrupt beNoisy()
{
static int count, busy;
int i,n,gotnote=0;
if(busy)
return;
if(songpointer==NULL)
return;
if(count>1)
{
--count;
return;
}
busy=1;
do
{
while(songpointer[songindex]==32)
++songindex;
switch(n=tolower(songpointer[songindex]))
{
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
for(i=0;i<SCALESIZE;++i)
{
if(n==scale[i].name[0])
{
++songindex;
if(songpointer[songindex]=='#')
{
++i;
++songindex;
}
pitch=scale[i].pitch;
break;
}
}
n=atoi(songpointer+songindex);
if(n>=1 && n<=32)
duration=32/n;
++songindex;
gotnote=1;
break;
case 'r':
++songindex;
n=atoi(songpointer+songindex);
if(n>=1 && n<=32)
duration=32/n;
pitch=-1;
++songindex;
gotnote=-1;
break;
case 'o':
++songindex;
n=atoi(songpointer+songindex);
if(n>=1 && n<=4)
octave=n;
pitch=-1;
++songindex;
break;
case 0:
songindex=0;
pitch=-1;
duration=1;
octave=1;
gotnote=1;
break;
default:
while(songpointer[songindex]!=32 && songpointer[songindex]!=0)
++songindex;
break;
}
}
while(!gotnote);
if(pitch==-1)
nosound();
else
{
nosound();
sound(pitch*(1<<(octave-1)));
}
count=duration;
busy=0;
}
DoSomething()
{
int screenwide, screendeep;
int d,m,i,n-0;
detectgraph(&d,&m);
if(d<0)
return(0);
initgraph(&d,&m,"");
if(graphresult()<0)
return(0);
setcolor(getmaxcolor());
screenwide=getmaxx();
screendeep=getmaxy();
do
{
setcolor(n++);
for(i=0;i<screenwide;i+=4)
{
line(screenwide/2,screendeep/2,i,0);
line(screenwide/2,screendeep/2,i,screendeep-1);
}
for(i=0;i<screendeep;i+=4)
{
line(screenwide/2,screendeep/2,0,i);
line(screenwide/2,screendeep/2,screenwide-1,i);
}
}
while(!kbhit());
getch();
closegraph();
}