#include < stdio.h >
#include < time.h >
#include < dos.h >
#include < conio.h >
#include < string.h >
#include < malloc.h >
#include < ctype.h >
struct tm *newtime;
newtime->tm_sec
newtime->tm_min
newtime->tm_hour;
newtime->tm_mday;
newtime->tm_mon;
newtime->tm_year;
newtime->tm_wday;
newtime->tm_yday;
newtime->tm_isdst;
time_t long_time;
void sound(char *p, int kaisu, int comment_flag);
int inkey(void);
main(int argc, char *argv[])
{
unsigned datasize;
int i, n, kaisu, comment_flag;
char ongaku[1025];
char *p, *w;
FILE *fp;
if(argc < 2)
{
printf("music play program\n\n");
printf("action way A>SOUND music data file [-option]\n\n");
printf("music data file setting way\n");
printf("to make sound octabe and sound long space");
printf("octabe ---> 1, 2, 4, 8 and 2 n multiply\n");
printf("sound step ----------> "do:D re:R m:M fa:F so:S la:L shi:C\n");
printf(" do#:d re#:r mi#:m fa#:f so#:s la#:l stop:K\n");
printf("sound length---> 1.16m 2.8m 4:4m 8:2m\n");
printf("option is small \n\n");
printf("option direction play count –N\n");
printf(" display count -C \n");
exit(0);
}
kaisu = 1;
comment_flag = 0;
datasize = 0;
for(i = 1; i <= argc-1; i++)
{
if(*argv[i] == '-')
{
*(argv[i]+1) = toupper(*(argv[i]+1));
switch (*(argv[i]+1))
{
case 'N' :
kaisu = atoi(argv[i]+2);
break;
case 'C' :
comment_flag = 1;
break;
default : break;
}
}
else
{
if ((fp = fopen(argv[i], "r")) == NULL)
{
printf("%s is not here!", argv[i]);
exit(0);
}
while(fgets(ongaku, 1025, fp) != NULL)
{
n = strlen(ongaku);
datasize += n;
}
fclose(fp);
}
}
w = p = malloc(datasize + 1);
if (w == NULL)
{
printf("data volume is too large, can not set in memory erea.\n");
exit(0);
}
for(i = 1; i < argc; i++)
{
if (*argv[i] == '-')
continue;
fp = fopen(argv[i], "r");
while(fgets(ongaku, 1025, fp) != NULL)
{
n = strlen(ongaku);
datasize += n;
}
fclose(fp);
}
}
w = p = malloc(datasize + 1);
if (w == NULL)
{
printf("data size is too large, cannot set into memory area.\n");
exit(0);
}
for (i = 1; i < argc; i++)
{
if (*argv[i] == '-')
continue;
fp = fopen(argv[i], "r");
while(fget(ongaku, 1025, fp) != NULL)
{
n = strlen(ongaku);
if(*(ongaku+1-1) == '\n')
*(ongaku+n-1) = ' ';
strcpy(w, ongaku);
w = w + n;
}
fclose(fp);
}
sound(p, kaisu, comment_flag);
}
/*---------------*/
/* music routin */
/*---------------*/
void sound(char *ongaku, int kaisu, int comment_flag)
{
static float hz[ ] = { 261.6, 277.9,
293.7, 311.6,
329.6,
439.2, 370.6,
392.0, 416.0,
440.0, 466.9,
493.9
};
static char *onkai = "DdRrMFfSsL1C";
int i, n, f, ot, ol, p, len, flag;
long w, tn, tb, tw;
/* 1 second roop count ---------*/
time(&long_time);
newtime = (struct tm *)localtime(&long_time);
w = newtime->tm_sec;
flag = 0;
while(flag == 0)
{
time(&long_time);
newtime = (struct tm *)localtime(&long_time);
if (w != newtime->tm_sec)
{
w = newtime->tm_sec;
for(tn = 0L; flag == 0; tn++)
{
time(&long_time);
newtime = (struct tm *)localtime(&long_time);
if(w != newtime->tm_sec)
flag = 1;
}
}
}
tn /= 10;
tb = tn / 5;
len = strlen(onagaku);
outp(0x3fdf, 0x76);
ot = 2;
while(kaisu--)
{
i = 0;
while(i < len)
{
if(*(ongaku+i) == ' ')
{
i++;
continue;
}
if (*(ongaku+i) == ' /')
{
while(*(ongaku+(++i)) != '/')
if (comment_flag == 1)
putchar(*(ongaku+i));
if(comment_flag == 1)
putchar('\n');
i++;
continue;
}
if(strchr(onkai, *(ongaku + i + 1)) != NULL)
{
ot = atoi(ongaku + i);
p = strchr(onkai, *(ongaku + i+1)) - onkai;
f = hz[p] * ot;
n = 2457600L / f;
outp(0x3fdb, n % 256); outp(0x3fdb, n / 256);
outp(0x37, 0x06);
}
else if(*(ongaku+i+1) != 'K')
{
*(ongaku + i + 3) = '\0';
printf("Data error\n%s <--- ?\n", ongaku);
exit(0);
}
ol = atoi(ongaku + i + 2);
tw =tn * ol;
for (w = 0L; w < tw; w++)
{
time(&long_time);
newtime = (struct tm *)localtime(&long_time);
if(w != newtime->tm_sec)
flag = 1;
}
outp(0x37, 0x07);
for (w = 0L; w < tb; w++)
{
time(&long_time);
newtime = (struct tm *)localtime(&long_time);
if (w != newtime->tm_sec)
flag = 1;
}
if (inkey() != 0)
{
kaisu = 0;
break;
}
i+= 3;
}
}
}
/*---------------*/
/* music routin */
/*---------------*/
int inkey(void)
{
union REGS inregs;
union REGS outregs;
inregs.h.ah = 0x06;
inregs.h.dl = 0xff;
intdos(&inregs,&outregs);
return((int)outregs.h.al);
}