| 1 |
{ |
| 2 |
Copyright 2017-2021 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Main form. } |
| 17 |
unit FormMain; |
| 18 |
|
| 19 |
{$mode objfpc}{$H+} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses |
| 24 |
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, |
| 25 |
ComCtrls, ExtCtrls, CastleSoundEngine, CastleDialogs, CastleTimeUtils; |
| 26 |
|
| 27 |
type |
| 28 |
TMainForm = class(TForm) |
| 29 |
ButtonSoundEngineInformation: TButton; |
| 30 |
ButtonPlay: TButton; |
| 31 |
ButtonOpen: TButton; |
| 32 |
ButtonStop: TButton; |
| 33 |
CheckBoxLoop: TCheckBox; |
| 34 |
LabelOffset: TLabel; |
| 35 |
LabelOffset1: TLabel; |
| 36 |
LabelVolume: TLabel; |
| 37 |
OpenDialogSound: TCastleOpenDialog; |
| 38 |
LabelCaption: TLabel; |
| 39 |
LabelSoundInfo: TLabel; |
| 40 |
Timer1: TTimer; |
| 41 |
TrackOffset: TTrackBar; |
| 42 |
TrackVolume: TTrackBar; |
| 43 |
procedure ButtonOpenClick(Sender: TObject); |
| 44 |
procedure ButtonPlayClick(Sender: TObject); |
| 45 |
procedure ButtonSoundEngineInformationClick(Sender: TObject); |
| 46 |
procedure ButtonStopClick(Sender: TObject); |
| 47 |
procedure CheckBoxLoopChange(Sender: TObject); |
| 48 |
procedure FormCreate(Sender: TObject); |
| 49 |
procedure Timer1Timer(Sender: TObject); |
| 50 |
procedure TrackOffsetChange(Sender: TObject); |
| 51 |
procedure TrackVolumeChange(Sender: TObject); |
| 52 |
private |
| 53 |
PlayingSound: TCastlePlayingSound; |
| 54 |
public |
| 55 |
|
| 56 |
end; |
| 57 |
|
| 58 |
var |
| 59 |
MainForm: TMainForm; |
| 60 |
|
| 61 |
implementation |
| 62 |
|
| 63 |
uses CastleUtils, CastleVectors, CastleLCLUtils, CastleUriUtils, |
| 64 |
CastleApplicationProperties, |
| 65 |
FormSoundEngineInfo; |
| 66 |
|
| 67 |
{$R *.lfm} |
| 68 |
|
| 69 |
procedure TMainForm.ButtonSoundEngineInformationClick(Sender: TObject); |
| 70 |
begin |
| 71 |
SoundEngineInfoForm.Show; |
| 72 |
end; |
| 73 |
|
| 74 |
procedure TMainForm.ButtonStopClick(Sender: TObject); |
| 75 |
begin |
| 76 |
PlayingSound.Stop; |
| 77 |
end; |
| 78 |
|
| 79 |
procedure TMainForm.CheckBoxLoopChange(Sender: TObject); |
| 80 |
begin |
| 81 |
PlayingSound.Loop := CheckBoxLoop.Checked; |
| 82 |
end; |
| 83 |
|
| 84 |
procedure TMainForm.FormCreate(Sender: TObject); |
| 85 |
begin |
| 86 |
PlayingSound := TCastlePlayingSound.Create(Self); |
| 87 |
PlayingSound.Sound := TCastleSound.Create(Self); |
| 88 |
FileFiltersToDialog(LoadSound_FileFilters, OpenDialogSound); |
| 89 |
end; |
| 90 |
|
| 91 |
procedure TMainForm.Timer1Timer(Sender: TObject); |
| 92 |
begin |
| 93 |
{ When application doesn't use any TCastleWindow or TCastleControl, |
| 94 |
this must be called manually to update some CGE state, |
| 95 |
e.g. detect unused sounds. } |
| 96 |
ApplicationProperties._Update; |
| 97 |
|
| 98 |
if PlayingSound.Playing then |
| 99 |
begin |
| 100 |
LabelOffset.Caption := FormatDot('Offset within played sound: %fs', [PlayingSound.Offset]); |
| 101 |
|
| 102 |
{ Show PlayingSound.Offset in UI. |
| 103 |
|
| 104 |
Temporary unassign the TrackOffset.OnChange handler, otherwise |
| 105 |
we would change sound offset in reaction to this, causing slight "popping" |
| 106 |
in the sound. |
| 107 |
|
| 108 |
Note: This is OK even when not playing, |
| 109 |
PlayingSound.Offset is guaranteed to be zero then. } |
| 110 |
TrackOffset.OnChange := nil; |
| 111 |
TrackOffset.Position := Round(PlayingSound.Offset * 1000); |
| 112 |
TrackOffset.OnChange := @TrackOffsetChange; |
| 113 |
end else |
| 114 |
begin |
| 115 |
LabelOffset.Caption := 'Offset within played sound: Not Playing'; |
| 116 |
|
| 117 |
{ Do not update TrackOffset.Position in this case, |
| 118 |
let user manipulate TrackOffset to set InitialOffset. } |
| 119 |
end; |
| 120 |
end; |
| 121 |
|
| 122 |
procedure TMainForm.TrackOffsetChange(Sender: TObject); |
| 123 |
begin |
| 124 |
{ Note: This is OK even when not playing, |
| 125 |
setting PlayingSound.Offset is guaranteed to be ignored then. } |
| 126 |
PlayingSound.Offset := TrackOffset.Position / 1000; |
| 127 |
end; |
| 128 |
|
| 129 |
procedure TMainForm.TrackVolumeChange(Sender: TObject); |
| 130 |
begin |
| 131 |
PlayingSound.Sound.Volume := TrackVolume.Position / 1000; |
| 132 |
end; |
| 133 |
|
| 134 |
procedure TMainForm.ButtonOpenClick(Sender: TObject); |
| 135 |
begin |
| 136 |
OpenDialogSound.URL := PlayingSound.Sound.URL; |
| 137 |
if OpenDialogSound.Execute then |
| 138 |
begin |
| 139 |
PlayingSound.Stop; |
| 140 |
PlayingSound.Sound.URL := OpenDialogSound.URL; |
| 141 |
LabelSoundInfo.Caption := Format('Sound File:' + NL + |
| 142 |
'%s' + NL + |
| 143 |
'Duration: %f', [ |
| 144 |
UriDisplay(PlayingSound.Sound.Url), |
| 145 |
PlayingSound.Sound.Duration |
| 146 |
]); |
| 147 |
TrackOffset.Min := 0; |
| 148 |
TrackOffset.Max := Round(PlayingSound.Sound.Duration * 1000); |
| 149 |
TrackOffset.Position := 0; |
| 150 |
end; |
| 151 |
end; |
| 152 |
|
| 153 |
procedure TMainForm.ButtonPlayClick(Sender: TObject); |
| 154 |
begin |
| 155 |
PlayingSound.Stop; |
| 156 |
{ start playback from InitialOffset } |
| 157 |
PlayingSound.InitialOffset := TrackOffset.Position / 1000; |
| 158 |
SoundEngine.Play(PlayingSound); |
| 159 |
end; |
| 160 |
|
| 161 |
end. |