Subversion Repositories spacemission

Rev

Rev 1 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 daniel-mar 1
unit DXWaveEdit;
2
 
3
interface
4
 
5
uses
6
  Windows, SysUtils, Classes, Forms, Dialogs, Controls, StdCtrls, ExtCtrls,
4 daniel-mar 7
  Buttons, ComCtrls, DXSounds, DXWave, Graphics;
1 daniel-mar 8
 
9
type
10
 
11
  {  TDelphiXWaveEditForm  }
12
 
13
  TDelphiXWaveEditForm = class(TForm)
14
    Bevel2: TBevel;
15
    OKButton: TButton;
16
    CancelButton: TButton;
17
    SaveDialog: TSaveDialog;
18
    OpenDialog: TOpenDialog;
19
    PlayImage: TImage;
20
    StopImage: TImage;
21
    Timer: TTimer;
22
    DXSound: TDXSound;
23
    ClearButton: TButton;
24
    SaveButton: TButton;
25
    LoadButton: TButton;
26
    Panel1: TPanel;
27
    TrackBar: TTrackBar;
28
    TestButton: TSpeedButton;
29
    LengthLabel: TLabel;
30
    FrequencyLabel: TLabel;
31
    TypeLabel: TLabel;
32
    SizeLabel: TLabel;
33
    LengthValueLabel: TLabel;
34
    FrequencyValueLabel: TLabel;
35
    TypeValueLabel: TLabel;
36
    SizeValueLabel: TLabel;
37
    procedure OKButtonClick(Sender: TObject);
38
    procedure CancelButtonClick(Sender: TObject);
39
    procedure LoadButtonClick(Sender: TObject);
40
    procedure SaveButtonClick(Sender: TObject);
41
    procedure ClearButtonClick(Sender: TObject);
42
    procedure FormShow(Sender: TObject);
43
    procedure DXSoundInitialize(Sender: TObject);
44
    procedure DXSoundFinalize(Sender: TObject);
45
    procedure TestButtonClick(Sender: TObject);
46
    procedure TimerTimer(Sender: TObject);
47
    procedure TrackBarChange(Sender: TObject);
48
    procedure FormDestroy(Sender: TObject);
49
  private
50
    FChanged: Boolean;
51
    FAudio: TAudioStream;
52
    FPlaying: Boolean;
53
    FWaveStream: TCustomWaveStream;
54
    FWaveFileName: string;
55
    FUpdating: Boolean;
56
    procedure UpDateData;
57
    procedure SetPlaying(Value: Boolean);
58
    property Playing: Boolean read FPlaying write SetPlaying;
59
  public
60
    Wave: TWave;
61
  end;
62
 
63
var
64
  DelphiXWaveEditForm: TDelphiXWaveEditForm;
65
 
66
implementation
67
 
68
uses DXConsts;
69
 
70
{$R *.DFM}
71
 
72
procedure TDelphiXWaveEditForm.FormDestroy(Sender: TObject);
73
begin
74
  FWaveStream.Free;
75
end;
76
 
77
procedure TDelphiXWaveEditForm.FormShow(Sender: TObject);
78
begin
79
  if Wave.Size>0 then
80
    FWaveStream := TWaveObjectStream.Create(Wave);
81
 
82
  TestButton.Glyph := PlayImage.Picture.Bitmap;
83
  UpDateData;
84
end;
85
 
86
procedure TDelphiXWaveEditForm.OKButtonClick(Sender: TObject);
87
begin
88
  Playing := False;
89
 
90
  if FChanged then
91
  begin
92
    if FWaveStream=nil then
93
      Wave.Clear
94
    else
95
      Wave.LoadFromFile(FWaveFileName);
96
 
97
    Tag := 1;
98
  end;
99
 
100
  Close;
101
end;
102
 
103
procedure TDelphiXWaveEditForm.CancelButtonClick(Sender: TObject);
104
begin
105
  Close;
106
end;
107
 
108
procedure TDelphiXWaveEditForm.ClearButtonClick(Sender: TObject);
109
begin
110
  Playing := False;
111
 
112
  FChanged := True;
113
  FWaveStream.Free; FWaveStream := nil;
114
  UpdateData;
115
end;
116
 
117
procedure TDelphiXWaveEditForm.LoadButtonClick(Sender: TObject);
118
var
119
  Stream: TWaveFileStream;
120
begin
121
  if OpenDialog.Execute then
122
  begin
123
    Playing := False;
124
 
125
    Stream := TWaveFileStream.Create(OpenDialog.FileName, fmOpenRead or fmShareDenyWrite);
126
    Stream.Open(False);
127
 
128
    FWaveStream.Free; FWaveStream := Stream;
129
    FWaveFileName := OpenDialog.FileName;
130
    FChanged := True;
131
    UpdateData;
132
  end;
133
end;
134
 
135
procedure TDelphiXWaveEditForm.SaveButtonClick(Sender: TObject);
136
var
137
  Dest, Source: TStream;
138
begin
139
  if SaveDialog.Execute then
140
  begin
141
    Playing := False;
142
 
143
    if FChanged then
144
    begin
145
      if AnsiCompareFileName(FWaveFileName, SaveDialog.FileName)=0 then Exit;
146
 
147
      Source := TFileStream.Create(FWaveFileName, fmOpenRead or fmShareDenyWrite);
148
      try
149
        Dest := TFileStream.Create(SaveDialog.FileName, fmCreate);
150
        try
151
          Dest.CopyFrom(Source, Source.Size);
152
        finally
153
          Dest.Free;
154
        end;
155
      finally
156
        Source.Free;
157
      end;
158
    end else
159
    begin
160
      Wave.SaveToFile(SaveDialog.FileName);
161
    end;
162
  end;
163
end;
164
 
165
procedure TDelphiXWaveEditForm.SetPlaying(Value: Boolean);
166
begin
167
  if FPlaying<>Value then
168
  begin
169
    if FPlaying then
170
    begin
171
      Timer.Enabled := False;
172
      FAudio.Stop;
173
      TrackBar.Position := FAudio.Position;
174
      DXSound.Finalize;
175
    end;
176
 
177
    if Value then
178
    begin
179
      DXSound.Initialize;
180
      DXSound.Primary.SetFormat(FAudio.Format^);
181
 
182
      FAudio.Position := TrackBar.Position;
183
      FAudio.Play;
184
 
185
      Timer.Enabled := True;
186
 
187
      TestButton.Glyph := StopImage.Picture.Bitmap
188
    end else
189
      TestButton.Glyph := PlayImage.Picture.Bitmap;
190
 
191
    FPlaying := Value;
192
  end;
193
end;
194
 
195
procedure TDelphiXWaveEditForm.UpDateData;
196
begin
197
  TestButton.Enabled := False;
198
  TrackBar.Enabled := False;
199
  Timer.Enabled := False;
200
 
201
  if FWaveStream<>nil then
202
  begin
203
    ClearButton.Enabled := True;
204
    SaveButton.Enabled := True;
205
 
206
    TrackBar.Position := 0;
207
    TrackBar.Max := FWaveStream.Size;
208
    TestButton.Enabled := True;
209
    TrackBar.Enabled := True;
210
 
211
    LengthValueLabel.Caption := Format(SWaveLength, [FWaveStream.Size/FWaveStream.Format^.nAvgBytesPerSec]);
212
 
213
    FrequencyValueLabel.Caption := Format(SWaveFrequency, [FWaveStream.Format^.nSamplesPerSec]);
214
 
215
    TypeValueLabel.Caption := Format(SWaveBitCount, [FWaveStream.Format^.wBitsPerSample]) + ' ';
216
 
217
    case FWaveStream.Format^.nChannels of
218
      1: TypeValueLabel.Caption := TypeValueLabel.Caption + SWaveMono;
219
      2: TypeValueLabel.Caption := TypeValueLabel.Caption + SWaveStereo;
220
    end;
221
 
222
    SizeValueLabel.Caption := Format(SWaveSize, [FWaveStream.Size]);
223
  end else
224
  begin
225
    LengthValueLabel.Caption := Format(SWaveLength, [0.0]);
226
    FrequencyValueLabel.Caption := Format(SWaveFrequency, [0]);
227
    TypeValueLabel.Caption := Format(SWaveBitCount, [0]) + ' ' + SWaveMono;
228
    SizeValueLabel.Caption := Format(SWaveSize, [0]);
229
 
230
    ClearButton.Enabled := False;
231
    SaveButton.Enabled := False;
232
    TrackBar.Position := 0;
233
  end;
234
end;
235
 
236
procedure TDelphiXWaveEditForm.DXSoundInitialize(Sender: TObject);
237
begin
238
  FAudio := TAudioStream.Create(DXSound.DSound);
239
  FAudio.WaveStream := FWaveStream;
240
  FAudio.BufferLength := 200;
241
end;
242
 
243
procedure TDelphiXWaveEditForm.DXSoundFinalize(Sender: TObject);
244
begin
245
  FAudio.Free; FAudio := nil;
246
end;
247
 
248
procedure TDelphiXWaveEditForm.TestButtonClick(Sender: TObject);
249
begin
250
  Playing := not Playing;
251
end;
252
 
253
procedure TDelphiXWaveEditForm.TimerTimer(Sender: TObject);
254
begin
255
  FAudio.Update;
256
  if FAudio.Playing then
257
  begin
258
    FUpdating := True;
259
    try
260
      TrackBar.Position := FAudio.Position;
261
    finally
262
      FUpdating := False;
263
    end;
264
  end else
265
  begin
266
    Playing := False;
267
    TrackBar.Position := 0;
268
  end;
269
end;
270
 
271
procedure TDelphiXWaveEditForm.TrackBarChange(Sender: TObject);
272
begin                      
273
  if (FAudio<>nil) and (not FUpdating) then
274
    FAudio.Position := TrackBar.Position;
275
end;
276
 
277
 
278
end.