-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMP.CPP
237 lines (229 loc) · 6.48 KB
/
MP.CPP
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* LICENSE
BSD 2-Clause License
Copyright (c) 2022, ChenPi11
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
/* ChenPi Music Player MS-DOS Mode v1.0
* encoding GBK(cp936)
* edit by Turbo C++ v3.0
*/
#include <stdio.h>
#include <dos.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#define bool unsigned char
#define true (1)
#define false (0)
#define NOTE_S 12
#define DELAY_S 11
#define TEXT_S 256
unsigned long StrToUnsignedLong(char* str)
{
long ll=atol(str);
if(ll>0){return (unsigned long)ll;}
else {return 0;}
}
void ZeroMem(char cs[],int L)//Clear Memory(memory,length of memory)
{
for(int i=0;i<L;i++)
{
cs[i]=0;
}
}
char* strrpc(char* str,char* oldstr,char* newstr)
{
char bstr[256];
ZeroMem(bstr,256);
for(int i = 0;i < strlen(str);i++)
{
if(!strncmp(str+i,oldstr,strlen(oldstr)))
{
strcat(bstr,newstr);
i+=strlen(oldstr);
}
strncat(bstr,str + i,1);
}
strcpy(str,bstr);
return str;
}
unsigned long denote(char cs[])//decode note(note id) -> rate of note
{
if(strcmp(cs,"0")==0)return 0;//stop
else if(strcmp(cs,".1")==0)return 262;
else if(strcmp(cs,".2")==0)return 294;
else if(strcmp(cs,".3")==0)return 330;
else if(strcmp(cs,".4")==0)return 350;
else if(strcmp(cs,".5")==0)return 392;
else if(strcmp(cs,".6")==0)return 440;
else if(strcmp(cs,".7")==0)return 494;
else if(strcmp(cs,"1")==0)return 523;
else if(strcmp(cs,"2")==0)return 578;
else if(strcmp(cs,"3")==0)return 659;
else if(strcmp(cs,"4")==0)return 698;
else if(strcmp(cs,"5")==0)return 784;
else if(strcmp(cs,"6")==0)return 880;
else if(strcmp(cs,"7")==0)return 998;
else if(strcmp(cs,"1.")==0)return 1046;
else if(strcmp(cs,"2.")==0)return 1175;
else if(strcmp(cs,"3.")==0)return 1318;
else if(strcmp(cs,"4.")==0)return 1400;
else if(strcmp(cs,"5.")==0)return 1568;
else if(strcmp(cs,"6.")==0)return 1760;
else if(strcmp(cs,"7.")==0)return 1976;
else if(strlen(cs)>1)
{
if(cs[0]=='r')
{
return StrToUnsignedLong(cs+1);
}
else
{
return 0;//error
}
}
else return 0;//error
}
void beep(unsigned long h,unsigned long dly)//beep(rate,delay)
{
if(h==0)
{
nosound();
}
else
{
sound(h);
}
delay(dly);
}
struct Note
{
unsigned long v;//rate(Hz)
unsigned long t;//delay time(ms)
char text[256];//print text
};
bool readline(FILE* fp,char buff[],unsigned long length)
{
ZeroMem(buff,length);
int l=0;
char c=0;
while(1)
{
c=fgetc(fp);
if(c==EOF)
{
return false;
}
if(c!=1 && c)
{
if(c=='\n'){return true;}
else if(l<length)
{
buff[l]=c;
l++;
}
else{return true;}
}
}
}
//Chinese:������"������ ������ ʱ��" -> �����������ֻ����������
Note split(char line[])
{
char sNote[NOTE_S]; char sDelay[DELAY_S]; char sText[TEXT_S];
ZeroMem(sNote,NOTE_S);ZeroMem(sDelay,DELAY_S);ZeroMem(sText,TEXT_S);
unsigned char mode=0;
int sI=0;
for(int i=0;i<strlen(line);i++)
{
if(line[i])
{
if(line[i]==' ')
{
mode++;sI=0;
}
else if(mode==0)
{
sNote[sI]=line[i];
sI++;
}
else if(mode==2)
{
sDelay[sI]=line[i];
sI++;
}
else if(mode==3)
{
sText[sI]=line[i];
sI++;
}
}
}
Note n;
if(strcmp(sNote,"#")!=0)
{
n.v=denote(sNote);
n.t=StrToUnsignedLong(sDelay);
strcpy(n.text,sText);
}
else
{
n.v=0;
n.t=0;
strcpy(n.text,"");
}
return n;
}
int main(int argc,char* argv[])//MusicPlayer MS-DOS
{
/*WARNING:
* length of ANY line <=276!
* only GBK text file!
* main melody note -> strlen<=11!
* main melody time -> strlen<=10!
* text -> strlen<=255!
* usage:mp.exe <music file(text)>
* don't press Control-C when playing!
* if you press Control-C,input "mp.exe" to stop sound.
*/
if(argc>1)
{
printf("open %s...\n",argv[1]);
}
else
{
printf("usage:mp.exe <music file(text)>\n");
nosound();
return 0;
}
FILE* f;
f=fopen(argv[1],"r");
//-----------------main--------------
char data[276];
while(readline(f,data,276))
{
Note n=split(data);
printf(strrpc(strrpc(strrpc(strrpc(strrpc(strrpc(strrpc(n.text,"\\n","\n"),"\\t","\t"),"|",""),"\\|","|"),"~"," "),"\\|","|"),"\\~","~"));
beep(n.v,n.t);
}
//------------------------------------
printf("\n");
fclose(f);
nosound();
return 0;
}