gpt4 book ai didi

delphi - 如何在Delphi中改变MIDI音符的音高和音量?

转载 作者:行者123 更新时间:2023-12-01 19:02:39 25 4
gpt4 key购买 nike

我在项目中使用以下代码在按键时播放 midi 音符(这是源代码中与 midi 相关的部分):

uses
MMSystem;

var
hMidi, midimsg, notenum, instrumNum :integer;

procedure TForm1.FormCreate(Sender: TObject);
begin
midiOutOpen(@hmidi, 0, 0, 0, 0);
midimsg := $C0+$100*29; // set midi instrument to overdriven guitar (29th in GM midi instrument list)
midiOutShortMsg (hmidi, midimsg);
end;

procedure playNote(var note:integer);
begin
midimsg := $90 + (note * $100) + (127 * $10000) + 0;
midiOutShortMsg (hmidi, midimsg);
end;

procedure stopNote(var note:integer);
begin
midimsg := $80 + (note * $100) + 0 ;
midiOutShortMsg (hmidi, midimsg);
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
playNote(60);
end;

procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
stopNote(60);
end;

procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
midiOutClose(hmidi);
end;

我知道有 MIDI 消息可以改变音高和音量。但我在delphi中找不到任何使用它们的例子。请帮助我修改 playNote 程序,使声音类似于吉他弯音效果(半音或全音调上演奏的音符的平滑音调变化),并类似地改变音符的音量(淡入)和淡出效果)。

提前谢谢您!

最佳答案

我找到了解决方案。

const
MIDI_NOTE_ON = $90;
MIDI_NOTE_OFF = $80;
MIDI_CHANGE_INSTRUMENT = $C0;
MIDI_PITCH_BEND = $E0;

function MIDIEncodeMessage(Msg, Param1, Param2: byte): integer;
begin
result := Msg + (Param1 shl 8) + (Param2 shl 16);
end;

procedure TForm4.FormClose(Sender: TObject; var Action: TCloseAction);
begin
midiOutClose(hMidi);
end;

procedure TForm4.FormCreate(Sender: TObject);
begin
playing := false;
midiOutOpen(@hMidi, 0, 0, 0, CALLBACK_NULL);
midiOutShortMsg(hMidi, MIDIEncodeMessage(MIDI_CHANGE_INSTRUMENT, 19, 0));
end;

procedure TForm4.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if playing then Exit;
ProgressBar1.Position := $2000;
midiOutShortMsg(hMidi, MIDIEncodeMessage(MIDI_PITCH_BEND,
lo(ProgressBar1.Position), hi(ProgressBar1.Position)));
midiOutShortMsg(hMidi, MIDIEncodeMessage(MIDI_NOTE_ON, 50, 127));
playing := true;
end;

procedure TForm4.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
midiOutShortMsg(hMidi, MIDIEncodeMessage(MIDI_NOTE_OFF, 50, 127));
playing := false;
end;

procedure TForm4.FormMouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
begin
ProgressBar1.StepBy(4*WheelDelta);
midiOutShortMsg(hMidi, MIDIEncodeMessage(MIDI_PITCH_BEND,
lo(ProgressBar1.Position), hi(ProgressBar1.Position)));
end;

删除TProgressBar在表单上,​​并设置其 MinMax016383 ,分别。

然后您可以通过滚动鼠标滚轮来“弯曲”音调。 (请注意,我在处理鼠标滚轮时使用的因子 4 可能不适合您的鼠标和当前的鼠标设置。)

示例:pitchbend.exe [我从我的网站中删除了该 EXE 文件,因为 Google Chrome 认为它是恶意软件。尽管这几乎肯定是误报,但我担心这会对我的 Google 排名产生负面影响。]

关于delphi - 如何在Delphi中改变MIDI音符的音高和音量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6885159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com