forked from VictorYXL/MyOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.c
144 lines (138 loc) · 3.47 KB
/
player.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
#include"nasmfunc.h"
#include"memory.h"
#include"sheet.h"
#include"graphic.h"
#include"buffer.h"
#include"timer.h"
#include"mtask.h"
#include"keyboard.h"
#include"mouse.h"
#include"timer.h"
#include"player.h"
#include<stdio.h>
#include<string.h>
void playOneBeat(int eax)
{
int i=0;
//停止播放
if (eax==0)
{
i=io_in8(0x61);
io_out8(0x61,i&0x0d);
}else//播放
{
i=1193180000/eax;
io_out8(0x43,0xb6);
io_out8(0x42,i&0xff);
io_out8(0x42,i>>8);
i=io_in8(0x61);
io_out8(0x61,(i | 0x03) & 0x0f);
}
}
void loadMusicFile(char *fileName,struct Task *task,struct Sheet *musicPlayerSheet)
{
int tone[12] = {1071618315,1135340056,1202850889,1274376125,1350154473,1430438836,
1515497155,1605613306,1701088041,1802240000,1909406767,2022946002};
int note[7] = {9,11,0,2,4,5,7};
//初始化缓冲区
char bufferArray;
struct Buffer bufferTime;
initBuffer(&bufferTime,1,&bufferArray);
//初始化定时器
struct Timer *timerPlayer;
timerPlayer=allocTimer();
initTimer(timerPlayer,&bufferTime,1);
char data;
int i,o=4,t=0;
char str[10];
if (strcmp(fileName,"\\Music\\Mus1")==0)
{
char music[50]="CCGGAAG.FFDDEEC.GGFFEED.GGFFEED.CCGGAAG.FFDDEEC...";
sprintf (str,"Playing");
putStrAndBackOnSht(musicPlayerSheet,70,28,BLACK,WHITE,str,8);
while (1)
{
for (int p=0;p<50;p++)
{
if (music[p]!='.')
{
i=o*12+note[music[p]-'A']+12;
playOneBeat(tone[i%12]>>(17-i/12));
}else playOneBeat(0);
setTimer(timerPlayer,20);
while (1)
if (getBuffer(&bufferTime,&data))
break;
else if (getBuffer(&task->bufAll.key,&data) && keyboard.keyTable[data]==' ')
{
playOneBeat(0);
sprintf (str,"Suspend");
putStrAndBackOnSht(musicPlayerSheet,70,28,BLACK,WHITE,str,8);
while (!(getBuffer(&task->bufAll.key,&data) && keyboard.keyTable[data]==' '));
sprintf (str,"Playing");
putStrAndBackOnSht(musicPlayerSheet,70,28,BLACK,WHITE,str,8);
}
playOneBeat(0);
setTimer(timerPlayer,10);
while (1)
if (getBuffer(&bufferTime,&data))
break;
else if (getBuffer(&task->bufAll.key,&data) && keyboard.keyTable[data]==' ')
{
playOneBeat(0);
sprintf (str,"Suspend");
putStrAndBackOnSht(musicPlayerSheet,70,28,BLACK,WHITE,str,8);
while (!(getBuffer(&task->bufAll.key,&data) && keyboard.keyTable[data]==' '));
sprintf (str,"Playing");
putStrAndBackOnSht(musicPlayerSheet,70,28,BLACK,WHITE,str,8);
}
}
}
}
playOneBeat(0);
}
void playTask_Main(struct Task *task)
{
//初始化缓冲区
char bufferArray[128];
struct Buffer bufferTime;
initBuffer(&bufferTime,128,bufferArray);
//显示窗口
struct Sheet *musicPlayerSheet;
unsigned char *musicPlayerBuffer;
musicPlayerSheet=allocSheet();
slideSheet(musicPlayerSheet,500,120);
musicPlayerBuffer=(unsigned char *)allocMem(200*68,"Player UI");//申请内存空间
setBufInSheet(musicPlayerSheet,musicPlayerBuffer,200,68,-1);
makeWindow(musicPlayerSheet,200,68,"Player");
setHeightSheet(musicPlayerSheet,task->winID+1);
int flag=0;
unsigned char data;
loadMusicFile(task->par[0],task,musicPlayerSheet);
while (1)
{
flag=0;
io_cli();
if (getBuffer(&task->bufAll.mouse,&data))
{
io_sti();
flag=2;
switch(data)
{
//左键移动
case 0:
slideSheet(musicPlayerSheet,mdec.x,mdec.y);
break;
//右键关闭
case 2:
freeSheet(musicPlayerSheet);
freeMem((unsigned int)musicPlayerBuffer,200*68);
deleteWindow(task);
deleteTask(task);
break;
}
}
if (flag==0)
io_sti();
}
}