Is there a command that we can make our Delphi application to emule sound where we can choose the numeric tone and duration just like in basic?
To produce a pure sine tone, you can use
Windows.Beep(400, 1000)
which will sound a 400 Hz pure sine tone for a duration of 1000 milliseconds.
If you want to play a real instrument (piano, guitar, or any of the 125 (?) other options), you can use MIDI. Simply use the MMSystem
unit and do
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;
After you have initialised the MIDI system, you can try
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 programming is so underrated!
Compiled demo EXE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With