Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/Unit1.pas

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations) (download) (as text)
Mon Feb 23 13:24:55 2015 UTC (9 years, 3 months ago) by yamat0jp
File MIME type: text/x-pascal
File size: 8009 byte(s)


1 unit Unit1;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, mmsystem, StdCtrls, ComCtrls, Menus, DateUtils;
8
9 type
10 TMidiKey = record
11 tdata: TDateTime;
12 key: integer;
13 vol: integer;
14 end;
15
16 TForm1 = class(TForm)
17 Label1: TLabel;
18 Label2: TLabel;
19 TrackBar1: TTrackBar;
20 Button1: TButton;
21 Label3: TLabel;
22 Label4: TLabel;
23 Label5: TLabel;
24 Label6: TLabel;
25 Label8: TLabel;
26 Label7: TLabel;
27 procedure FormCreate(Sender: TObject);
28 procedure FormDestroy(Sender: TObject);
29 procedure FormKeyDown(Sender: TObject; var Key: Word;
30 Shift: TShiftState);
31 procedure FormKeyUp(Sender: TObject; var Key: Word;
32 Shift: TShiftState);
33 procedure TrackBar1Change(Sender: TObject);
34 procedure Button1Click(Sender: TObject);
35 private
36 { Private éŒž }
37 MidiOut: hMidiOut;
38 piano: DWord;
39 down: array [0..12] of Boolean;
40 Keys: array [0..12] of Word;
41 vol: DWord;
42 sharp: boolean;
43 List: TList;
44 tdata: TDateTime;
45 procedure ClearRecord;
46 procedure PlayRecorded;
47 function ReadInt(var Int: integer; const Key: Word): Boolean;
48 function KeySetting(const KeyArray: array of Char): Boolean;
49 function MidiOutMessage(iStatus, iChannel, iData1, iData2: integer): Cardinal;
50 public
51 { Public éŒž }
52 end;
53
54 var
55 Form1: TForm1;
56
57 implementation
58
59 {$R *.dfm}
60
61 procedure TForm1.FormCreate(Sender: TObject);
62 var
63 Res: DWORD;
64 msg: string;
65 begin
66 Res:=midiOutOpen(@MidiOut,MIDI_MAPPER,handle,0,CALLBACK_WINDOW);
67 if Res <> 0 then
68 begin
69 case Res of
70 MMSYSERR_BADDEVICEID:
71 msg:='”͈͊O‚Ě';
72 MMSYSERR_ALLOCATED:
73 msg:='Šů‚ÉŠ„‚č“–‚Ä';
74 MMSYSERR_NOMEM:
75 msg:='ƒƒ‚ƒŠ‚ĚŠ„‚č“–‚Ä';
76 MIDIERR_NOMAP:
77 msg:='‚l‚h‚c‚hƒ}ƒbƒv‚Ş';
78 MIDIERR_NODEVICE:
79 msg:='ƒ|[ƒg‚Ş';
80 else
81 msg:='MIDI Open error.';
82 end;
83 MessageBox(Handle,PChar(msg),'Error',MB_OK);
84 end;
85 piano:=$90;
86 vol:=$7F;
87 sharp:=false;
88 List:=TList.Create;
89 KeySetting(['D','R','M','F','S','L','I','W']);
90 end;
91
92 procedure TForm1.FormDestroy(Sender: TObject);
93 begin
94 midiOutClose(MidiOut);
95 ClearRecord;
96 List.Free;
97 end;
98
99 function TForm1.MidiOutMessage(iStatus, iChannel, iData1, iData2: integer): Cardinal;
100 const
101 s: integer = $3C;
102 begin
103 result:=(iStatus shl 24)or(iChannel shl 16)or((iData1+s) shl 8)or iData2;
104 end;
105
106 procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
107 Shift: TShiftState);
108 var
109 i: integer;
110 s: ^TMidiKey;
111 t: Boolean;
112 begin
113 t:=false;
114 case Key of
115 VK_SPACE:
116 if sharp = false then
117 begin
118 sharp:=true;
119 end;
120 VK_RETURN:
121 if Label5.Caption = 'Record' then
122 begin
123 Label5.Caption:='';
124 end else
125 if Label5.Caption = '' then
126 begin
127 tdata:=Now;
128 ClearRecord;
129 Label5.Caption:='Record';
130 end;
131 VK_NUMPAD0:
132 begin
133 Label5.Caption:='Play';
134 PlayRecorded;
135 end;
136 else
137 t:=ReadInt(i,Key);
138 end;
139 if Label5.Caption = 'Play' then
140 begin
141 Label5.Caption:='';
142 end;
143 if t = false then
144 begin
145 Exit;
146 end;
147 if down[i] = false then
148 begin
149 down[i]:=true;
150 if sharp = true then
151 begin
152 if i in [5,7,9,11] then
153 begin
154 inc(i);
155 end else
156 if i in [2,4] then
157 begin
158 dec(i);
159 end else
160 begin
161 Exit;
162 end;
163 end;
164 if Shift = [ssShift] then
165 begin
166 inc(i,12);
167 end else
168 if Shift = [ssCtrl] then
169 begin
170 dec(i,12);
171 end;
172 if Label5.Caption = 'Record' then
173 begin
174 New(s);
175 s^.tdata:=Now-tdata;
176 s^.key:=i;
177 s^.vol:=vol;
178 List.Add(s);
179 end;
180 i:=MiDiOutMessage($00,vol,i,piano);
181 midiOutShortMsg(MidiOut,i);
182 end;
183 end;
184
185 procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
186 Shift: TShiftState);
187 var
188 i: integer;
189 s: ^TMidiKey;
190 begin
191 case Key of
192 VK_SPACE:
193 begin
194 if sharp = true then
195 begin
196 sharp:=false;
197 end;
198 Exit;
199 end;
200 else
201 if ReadInt(i,Key) = false then
202 begin
203 Exit;
204 end;
205 end;
206 if down[i] = true then
207 begin
208 down[i]:=false;
209 if Shift = [ssShift] then
210 begin
211 inc(i,12);
212 end else
213 if Shift = [ssCtrl] then
214 begin
215 dec(i,12);
216 end;
217 if Label5.Caption = 'Record' then
218 begin
219 New(s);
220 s^.tdata:=Now-tdata;
221 s^.key:=i;
222 s^.vol:=0;
223 List.Add(s);
224 end;
225 i:=MidiOutMessage($00,$00,i,piano);
226 midiOutShortMsg(MidiOut,i);
227 end;
228 end;
229
230 procedure TForm1.TrackBar1Change(Sender: TObject);
231 begin
232 vol:=TrackBar1.Position;
233 end;
234
235 procedure TForm1.Button1Click(Sender: TObject);
236 begin
237 Close;
238 end;
239
240 procedure TForm1.ClearRecord;
241 var
242 i: integer;
243 begin
244 for i:=0 to List.Count-1 do
245 begin
246 Dispose(List[i]);
247 end;
248 List.Clear;
249 end;
250
251 procedure TForm1.PlayRecorded;
252 var
253 i, j, k, m, n: Word;
254 c: cardinal;
255 s: TMidiKey;
256 t: TDateTime;
257 u: string;
258 begin
259 j:=0;
260 if List.Count > 0 then
261 begin
262 Label7.Caption:='';
263 t:=TMidiKey(List[0]^).tdata;
264 for i:=0 to List.Count-1 do
265 begin
266 s:=TMidiKey(List[i]^);
267 DecodeTime(s.tdata-t,j,k,m,n);
268 Application.ProcessMessages;
269 if Label5.Caption <> 'Play' then
270 begin
271 break;
272 end;
273 Sleep(1000*m+n);
274 t:=s.tdata;
275 c:=MidiOutMessage($00,s.vol,s.key,piano);
276 midiOutShortMsg(MidiOut,c);
277 if s.vol > 0 then
278 begin
279 k:=2;
280 if s.key > 12 then
281 begin
282 s.key:=s.key-12;
283 k:=3;
284 end else
285 if s.key < 0 then
286 begin
287 s.key:=s.key+12;
288 k:=1;
289 end;
290 case s.key of
291 0:
292 u:='ÄŢ';
293 1:
294 u:='Ú/';
295 2:
296 u:='Ú';
297 3:
298 u:='Đ/';
299 4:
300 u:='Đ';
301 5:
302 u:='̧';
303 6:
304 u:='̧*';
305 7:
306 u:='ż';
307 8:
308 u:='ż*';
309 9:
310 u:='×';
311 10:
312 u:='×*';
313 11:
314 u:='ź';
315 12:
316 u:='ÄŢ^';
317 else
318 u:='';
319 end;
320 case k of
321 1:
322 u:='^'+u;
323 3:
324 u:=u+'^';
325 end;
326 if Label7.Caption = '' then
327 begin
328 Label7.Caption:=u;
329 end else
330 begin
331 Label7.Caption:=Label7.Caption+','+u;
332 end;
333 end;
334 end;
335 end;
336 Label5.Caption:='';
337 end;
338
339 function TForm1.ReadInt(var Int: integer; const Key: Word): Boolean;
340 var
341 i, j: integer;
342 begin
343 i:=Ord(Key);
344 result:=false;
345 for j:=0 to 7 do
346 begin
347 if i = Keys[j] then
348 begin
349 case j of
350 0:
351 Int:=0;
352 1:
353 Int:=2;
354 2:
355 Int:=4;
356 3:
357 Int:=5;
358 4:
359 Int:=7;
360 5:
361 Int:=9;
362 6:
363 Int:=11;
364 7:
365 Int:=12;
366 end;
367 result:=true;
368 break;
369 end;
370 end;
371 end;
372
373 function TForm1.KeySetting(const KeyArray: array of Char): Boolean;
374 var
375 i: integer;
376 begin
377 if High(KeyArray) = 7 then
378 begin
379 for i:=0 to 7 do
380 begin
381 Keys[i]:=Ord(KeyArray[i]);
382 end;
383 result:=true;
384 end else
385 begin
386 result:=false;
387 end;
388 end;
389
390 end.

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26