【发布时间】:2018-01-25 01:37:45
【问题描述】:
我发现了一些用于创建 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 格式分配给字节数组需要什么语法?感谢您的帮助。
【问题讨论】:
-
您将
midCmd标记为常量。常量值无法更改。 -
感谢您的快速回复!将 const 更改为变量可以修复它。