12

有没有一个命令可以让我们的 Delphi 应用程序发出声音,我们可以像基本一样选择数字音调和持续时间?

4

1 回答 1

18

要产生纯正弦音,您可以使用

Windows.Beep(400, 1000)

这将发出 400 Hz 纯正弦音,持续 1000 毫秒。

如果您想演奏真正的乐器(钢琴、吉他或 125 (?) 其他选项中的任何一个),您可以使用 MIDI。只需使用该MMSystem装置并执行

var
  mo: HMIDIOUT;

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

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

procedure NoteOn(NewNote, NewIntensity: byte);
begin
  midiOutShortMsg(mo, MIDIEncodeMessage(MIDI_NOTE_ON, NewNote, NewIntensity));
end;

procedure NoteOff(NewNote, NewIntensity: byte);
begin
  midiOutShortMsg(mo, MIDIEncodeMessage(MIDI_NOTE_OFF, NewNote, NewIntensity));
end;

procedure SetInstrument(NewInstrument: byte);
begin
  midiOutShortMsg(mo, MIDIEncodeMessage(MIDI_CHANGE_INSTRUMENT, NewInstrument, 0));
end;

procedure InitMIDI;
begin
  midiOutOpen(@mo, 0, 0, 0, CALLBACK_NULL);
  midiOutShortMsg(mo, MIDIEncodeMessage(MIDI_CHANGE_INSTRUMENT, 0, 0));
end;

初始化 MIDI 系统后,您可以尝试

NoteOn(50, 127);
Sleep(500);
SetInstrument(60);
NoteOn(60, 127);
Sleep(500);
NoteOff(60, 127);
SetInstrument(80);
NoteOn(70, 127);
Sleep(500);
NoteOff(70, 127);
SetInstrument(90);
NoteOn(80, 127);
Sleep(500);
NoteOff(80, 127);
SetInstrument(100);
NoteOn(90, 127);
Sleep(500);
NoteOff(90, 127);
SetInstrument(12);
NoteOn(40, 127);
Sleep(1000);
NoteOff(40, 127);

MIDI 编程被低估了

编译演示EXE

于 2013-04-20T23:01:25.193 回答