Урок 21 Музыкальные инструменты. Пианино
Пример 1 Это приложение реализует музыкальный инструмент пианино. Для клавиатуры первой октавы пианино задействованы кнопки 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, –, =. В проект необходимо включить библиотеку MMSystem. Предварительно вы можете скачать готовый EXE–файл и посмотреть приложение в работе.
unit Unit13;
interface
uses
// добавили библиотеку MMSystem
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, MMSystem;
type
TForm13 = class(TForm)
procedure KeyPress(Sender: TObject; var Key: Char);
procedure Create(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form13: TForm13;
nota: Integer; // играемая нота
midimsg: Integer; // midi сообшение
hmidi: Integer; // устройство вывода звука
implementation
{$R *.dfm}
procedure TForm13.Create(Sender: TObject);
begin
// открытие midi выхода MidiOut
MidiOutOpen(@hmidi, 0, 0, 0, 0);
end;
procedure TForm13.KeyPress(Sender: TObject; var Key: Char);
begin
if key = '1' then nota := 60; // с ДО 1 октава
if key = '2' then nota := 62; // d РЕ
if key = '3' then nota := 64; // e МИ
if key = '4' then nota := 65; // f ФА
if key = '5' then nota := 67; // g СОЛЬ
if key = '6' then nota := 69 ; // a ЛЯ
if key = '7' then nota := 71; // b СИ
if key = '8' then nota := 72; // c ДО 2 октава
if key = '9' then nota := 74; // d РЕ
if key = '0' then nota := 76; // e МИ
if key = '-' then nota := 77; // f ФА
if key = '=' then nota := 79; // g СОЛЬ
midimsg := $90 + ((nota) * $100) + (127 * $10000) + 0; // формирование инструмента и ноты
midiOutShortMsg (hmidi, midimsg); // запуск воспроизведения ноты
end;
end.
Пример 2 Повторение первого примера с той лишь разницей что помощь будет озвучена. Строить это приложение лучше после изучения 22-го урока. Что бы посмотреть как работает приложение скачайте готовый ЕХЕ-файл.
unit Unit1;
interface
uses
// добавить библиотеку MMSystem
// импортировать библиотеку — в главном меню выбрать
// Component — Import Component — Import Type Library — Microsoft Speech Object Library
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, MMSystem, SpeechLib_TLB,
Vcl.Imaging.jpeg, Vcl.ExtCtrls, Vcl.Imaging.pngimage;
type
TForm1 = class(TForm)
Image1: TImage;
Image2: TImage;
procedure FormCreate(Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: Char);
procedure Image1Click(Sender: TObject);
procedure Image2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
nota: Integer; // играемая нота
midimsg: Integer; // midi сообшение
hmidi: Integer; // устройство вывода звука
gpIVTxt: ISpVoice; // интерфейс для работы с речью
Pool: LongWord;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
// открытие midi выхода MidiOut
MidiOutOpen(@hmidi, 0, 0, 0, 0);
gpIVTxt := CoSpVoice.Create as ISpVoice;
// проговаривание – метод Speak объекта SPVoice имеет три параметра
gpIVTxt.Speak (PChar ('Для клавиатуры первой октавы пианино задействованы кнопки 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, минус, =.'), 1, Pool);
Image2.Visible := false;
end;
procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
if key = '1' then nota := 60; // с ДО 1 октава
if key = '2' then nota := 62; // d РЕ
if key = '3' then nota := 64; // e МИ
if key = '4' then nota := 65; // f ФА
if key = '5' then nota := 67; // g СОЛЬ
if key = '6' then nota := 69 ; // a ЛЯ
if key = '7' then nota := 71; // b СИ
if key = '8' then nota := 72; // c ДО 2 октава
if key = '9' then nota := 74; // d РЕ
if key = '0' then nota := 76; // e МИ
if key = '-' then nota := 77; // f ФА
if key = '=' then nota := 79; // g СОЛЬ
// формирование инструмента и ноты
midimsg := $90 + ((nota) * $100) + (127 * $10000) + 0;
midiOutShortMsg (hmidi, midimsg); // запуск воспроизведения ноты
end;
procedure TForm1.Image1Click(Sender: TObject);
begin
gpIVTxt.Speak (PChar ('Корпорация Большой Камаган'), 1, Pool);
Image1.Visible := false;
Image2.Visible := true;
end;
procedure TForm1.Image2Click(Sender: TObject);
begin
Image2.Visible := false;
Image1.Visible := true;
end;
end.
Пример 3 Приложение проигрывает мелодию по указанным нотам.
Пример 4 В приложении можно выбирать музыкальный инструмент. Всего таких инструментов 128. Вот этот богатый набор.
TMIDIInstrument = (midiAcousticGrandPiano, midiBrightAcousticPiano,
midiElectricGrandPiano, midiHonkyTonkPiano,
midiRhodesPiano, midiChorusedPiano, midiHarpsichord,
midiClavinet, midiCelesta, midiGlockenspiel,
midiMusicBox, midiVibraphone, midiMarimba, midiXylophone,
midiTubularBells, midiDulcimer, midiHammondOrgan,
midiPercussiveOrgan, midiRockOrgan, midiChurchOrgan,
midiReedOrgan, midiAccordion, midiHarmonica,
midiTangoAccordion, midiAcousticGuitarNylon,
midiAcousticGuitarSteel, midiElectricGuitarJazz,
midiElectricGuitarClean, midiElectricGuitarMuted,
midiOverdrivenGuitar, midiDistortionGuitar,
midiGuitarHarmonics, midiAcousticBass, midiElectricBassFinger,
midiElectricBassPick, midiFretlessBass, midiSlapBass1,
midiSlapBass2, midiSynthBass1, midiSynthBass2, midiViolin,
midiViola, midiCello, midiContrabass, midiTremoloStrings,
midiPizzicatoStrings, midiOrchestralHarp, midiTimpani,
midiStringEnsemble1, midiStringEnsemble2, midiSynthStrings1,
midiSynthStrings2, midiChoirAahs, midiVoiceOohs,
midiSynthVoice, midiOrchestraHit, midiTrumpet, midiTrombone,
midiTuba, midiMutedTrumpet, midiFrenchHorn, midiBrassSection,
midiSynthBrass1, midiSynthBrass2, midiSopranoSax, midiAltoSax,
midiTenorSax, midiBaritoneSax, midiOboe, midiEnglishHorn,
midiBassoon, midiClarinet, midiPiccolo, midiFlute,
midiRecorder, midiPanFlute, midiBottleBlow, midiShakuhachi,
midiWhistle, midiOcarina, midiLead1Square,
midiLead2Sawtooth, midiLead3CalliopeLead, midiLead4ChiffLead,
midiLead5Charang, midiLead6Voice, midiLead7Fifths,
midiLead8BrassLead, midiPad1NewAge, midiPad2Warm,
midiPad3Polysynth, midiPad4Choir, midiPad5Bowed,
midiPad6Metallic, midiPad7Halo, midiPad8Sweep, midiEmpty0,
midiEmpty1, midiEmpty2, midiEmpty3, midiEmpty4, midiEmpty5,
midiEmpty6, midiEmpty7, midiEmpty8, midiEmpty9, midiEmpty10,
midiEmpty11, midiEmpty12, midiEmpty13, midiEmpty14,
midiEmpty15, midiEmpty16, midiEmpty17, midiEmpty18,
midiEmpty19, midiEmpty20, midiEmpty21, midiEmpty22,
midiEmpty23, midiGuitarFretNoise, midiBreathNoise,
midiSeashore, midiBirdTweet, midiTelephoneRing,
midiHelicopter, midiApplause, midiGunshot);
Выбери лучшее! allbest |