aboutsummaryrefslogtreecommitdiffstats
path: root/medley_new/src/lib/midi/demo/MidiTest.pas
blob: 793db73021d34f017c41aa776d67e62aceef4124 (plain) (blame)
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
238
239
240
241
242
243
244
245
246
247
248
249
// Test application for TMidiFile

unit MidiTest;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, MidiFile,  ExtCtrls, MidiOut, MidiType, MidiScope, Grids;
type
  TMidiPlayer = class(TForm)
    OpenDialog1: TOpenDialog;
    Button1: TButton;
    Button3: TButton;
    Button4: TButton;
    MidiOutput1: TMidiOutput;
    cmbInput: TComboBox;
    MidiFile1: TMidiFile;
    MidiScope1: TMidiScope;
    Label3: TLabel;
    edtBpm: TEdit;
    Memo2: TMemo;
    edtTime: TEdit;
    Button2: TButton;
    TrackGrid: TStringGrid;
    TracksGrid: TStringGrid;
    edtLength: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure MidiFile1MidiEvent(event: PMidiEvent);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cmbInputChange(Sender: TObject);
    procedure MidiFile1UpdateEvent(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure edtBpmKeyPress(Sender: TObject; var Key: Char);
    procedure TracksGridSelectCell(Sender: TObject; Col, Row: Integer;
      var CanSelect: Boolean);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
    MidiOpened : boolean;
    procedure SentAllNotesOff;

    procedure MidiOpen;
    procedure MidiClose;

  public
    { Public declarations }
  end;

var
  MidiPlayer: TMidiPlayer;

implementation

{$R *.DFM}

procedure TMidiPlayer.Button1Click(Sender: TObject);
var
  i,j: integer;
  track : TMidiTrack;
  event : PMidiEvent;
begin
  if opendialog1.execute then
  begin
    midifile1.filename := opendialog1.filename;
    midifile1.readfile;
//    label1.caption := IntToStr(midifile1.NumberOfTracks);
    edtBpm.text := IntToStr(midifile1.Bpm);
//    TracksGrid.cells.clear;
    for i := 0 to midifile1.NumberOfTracks-1 do
    begin
      track := midifile1.getTrack(i);
      TracksGrid.cells[0,i] := 'Tr: '+ track.getName + ' '+ track.getInstrument ;
    end;
    edtLength.Text := MyTimeToStr(MidiFile1.GetTrackLength);
  end;
end;

procedure TMidiPlayer.MidiFile1MidiEvent(event: PMidiEvent);
var mEvent : TMyMidiEvent;
begin
  mEvent := TMyMidiEvent.Create;
  if not (event.event = $FF) then
  begin
    mEvent.MidiMessage := event.event;
    mEvent.data1 := event.data1;
    mEvent.data2 := event.data2;
    midioutput1.PutMidiEvent(mEvent);
  end
  else
  begin
    if (event.data1 >= 1) and (event.data1 < 15) then
    begin
      memo2.Lines.add(IntToStr(event.data1) + ' '+ event.str);
    end
  end;
  midiScope1.MidiEvent(event.event,event.data1,event.data2);
  mEvent.Destroy;
end;

procedure TMidiPlayer.SentAllNotesOff;
var mEvent : TMyMidiEvent;
channel : integer;
begin
  mEvent := TMyMidiEvent.Create;
  for channel:= 0 to 15 do
  begin
    mEvent.MidiMessage := $B0 + channel;
    mEvent.data1 := $78;
    mEvent.data2 := 0;
    if MidiOpened then
      midioutput1.PutMidiEvent(mEvent);
    midiScope1.MidiEvent(mEvent.MidiMessage,mEvent.data1,mEvent.data2);
  end;
  mEvent.Destroy;
end;

procedure TMidiPlayer.Button3Click(Sender: TObject);
begin
  midifile1.StartPlaying;
end;

procedure TMidiPlayer.Button4Click(Sender: TObject);
begin
  midifile1.StopPlaying;
  SentAllNotesOff;
end;

procedure TMidiPlayer.MidiOpen;
begin
  if not (cmbInput.Text = '') then
  begin
    MidiOutput1.ProductName := cmbInput.Text;
    MidiOutput1.OPEN;
    MidiOpened := true;
  end;
end;

procedure TMidiPlayer.MidiClose;
begin
  if MidiOpened then
  begin
    MidiOutput1.Close;
    MidiOpened := false;
  end;
end;


procedure TMidiPlayer.FormCreate(Sender: TObject);
var thisDevice : integer;
begin
  for thisDevice := 0 to MidiOutput1.NumDevs - 1 do
  begin
    MidiOutput1.DeviceID := thisDevice;
    cmbInput.Items.Add(MidiOutput1.ProductName);
  end;
  cmbInput.ItemIndex := 0;
  MidiOpened := false;
  MidiOpen;
end;

procedure TMidiPlayer.cmbInputChange(Sender: TObject);
begin
  MidiClose;
  MidiOPen;
end;

procedure TMidiPlayer.MidiFile1UpdateEvent(Sender: TObject);
begin
  edtTime.Text := MyTimeToStr(MidiFile1.GetCurrentTime);
  edtTime.update;
  if MidiFile1.ready then
  begin
    midifile1.StopPlaying;
    SentAllNotesOff;
  end;
end;

procedure TMidiPlayer.Button2Click(Sender: TObject);
begin
  MidiFile1.ContinuePlaying;
end;

procedure TMidiPlayer.edtBpmKeyPress(Sender: TObject; var Key: Char);
begin
 if Key = char(13) then
 begin
   MidiFile1.Bpm := StrToInt(edtBpm.Text);
   edtBpm.text := IntToStr(midifile1.Bpm);
   abort;
 end;

end;

procedure TMidiPlayer.TracksGridSelectCell(Sender: TObject; Col,
  Row: Integer; var CanSelect: Boolean);
var
  MidiTrack : TMidiTrack;
  i         : integer;
  j         : integer;
  event     : PMidiEvent;
begin
  CanSelect := false;
  if Row < MidiFile1.NumberOfTracks then
  begin
    CanSelect := true;
    MidiTrack := MidiFile1.GetTrack(Row);
    TrackGrid.RowCount := 2;
    TrackGrid.RowCount := MidiTrack.getEventCount;
    j := 1;
    for i := 0 to MidiTrack.GetEventCount-1 do
    begin
      event := MidiTrack.getEvent(i);
      if not (event.len = -1) then
      begin // do not print when
        TrackGrid.cells[0,j] := IntToStr(i);
        TrackGrid.cells[1,j] := MyTimeToStr(event.time);
        TrackGrid.cells[2,j] := IntToHex(event.event,2);
        if not (event.event = $FF) then
        begin
          TrackGrid.cells[3,j] := IntToStr(event.len);
          TrackGrid.cells[4,j] := KeyToStr(event.data1);
          TrackGrid.cells[5,j] := IntToStr(event.data2);
        end
        else
        begin
          TrackGrid.cells[3,j] := IntToStr(event.data1);
          TrackGrid.cells[4,j] := '';
          TrackGrid.cells[5,j] := event.str;
        end;
        inc(j);
      end;
    end;
    TrackGrid.RowCount := j;
  end;
end;

procedure TMidiPlayer.FormShow(Sender: TObject);
begin
  TrackGrid.ColWidths[0] := 30;
  TrackGrid.ColWidths[2] := 30;
  TrackGrid.ColWidths[3] := 30;
  TrackGrid.ColWidths[4] := 30;
  TrackGrid.ColWidths[5] := 100;
end;

end.