我找到了一些用于创建midi文件的旧代码。在较新的Delphi中,它给了我无法分配错误。我假设一个Unicode问题?但是如何解决呢?给出了这些行的错误
midCmd[2] := $90;
midCmd[3] := Note;
midCmd[4] := Velocity;
如果需要,和完整的程序
procedure SaveAsMidi(note: byte; FileName: string);
const // MIDI file header
midHdr: array[0..13]of byte =
($4D,$54,$68,$64, //[0..3] Always MThd
$00,$00,$00,$06, // [4..7] Always the same
$00,$01, // [8..9] Two bytes for file format
// $01 = synchronous multiple tracks
$00,$01, // [10..11] Two bytes for number of tracks
$01,$E0); // [12..13] Two bytes for ticks per quarter note
// MIDI track header
midTrk: array[0..7]of byte =
($4D,$54,$72,$6B, // [0..3] Always MTrk
$00,$00,$00,$0E); // [4..7] Four bytes for track size in bytes
// MIDI track end bytes
midEnd: array[0..3]of byte =
($00,$FF,$2F,$00); // [0..3] Always the same
// MIDI command
midCmd: array[0..4]of byte =
($80,$00, // [0..1] Two bytes for time in ticks
$90, // [2] Command:
// $90 = Note on at channel 0
// $80 = Note off at channel 0
$00,$00); // [3] Note and [4] velocity
// Constant velocity
Velocity=$FF;
// Constant duration
Time=$FF;
var midFile: TFileStream;
begin
// Create MIDI file
midFile := TFileStream.Create(FileName +'.mid', fmCreate);
with midFile do try
// Write MIDI header
Write(midHdr, SizeOf(midHdr));
// Write MIDI track
Write(midTrk, SizeOf(midTrk));
// Write Note On
// Play immediately = time is 0
midCmd[2] := $90;
midCmd[3] := Note;
midCmd[4] := Velocity;
Write(midCmd, SizeOf(midCmd));
// Write Note Off
// Separate time into two bytes
midCmd[0] := $80 or (Time div 128 mod 128);
midCmd[1] := $00 or (Time mod 128);
midCmd[2] := $80;
Write(midCmd, SizeOf(midCmd));
// Write MIDI track end
Write(midEnd, SizeOf(midEnd));
finally
midFile.Free;
end;
end;
将字节$ FF格式分配给字节数组需要什么语法?谢谢你的帮助。
答案 0 :(得分:6)
您已将midcmd
声明为类型常量,{$WRITEABLECONST}
编译器指令(现在)处于默认状态{$J-}
(或更详细:{$WRITEABLECONST OFF}
) ,意味着键入的常量不可写。
在Delphi和Object Pascal的早期版本中,类型化常量始终是可写的,这可以解释您在网络上找到的代码。或者,它可能已经特别声明编译器指令为{$J+}
(或{$WRITEABLECONST ON}
)。
要按原样使用代码,请更改指令。要保留默认指令,请将类型化常量midcmd
更改为变量(将其移至var
部分)。
您可以在Embarcadero's documentation中阅读有关可写类型的常量。