Drowning a real time graph of a Wave file

Hi to all,
I'm a newbie Delphi programmer. I've developed a simple application that opens and reproduces Wave files. Now I Would like to make a graph of the wave while it's playing (maybe using a TFastLineSeries??). Thank you for help, good afternoon.

here is the main.pas file (Sorry for this):

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Menus, ComCtrls, DXSounds, MMSystem, Wave, AudioStream,
Math, dspIIRfilters, Buttons, syncobjs, dspFFT, dspTypes, dspWindow, SpecList,
DXClass;

type
TMainForm = class(TForm)
Panel1: TPanel;
PlayButton: TButton;
PauseButton: TButton;
StopButton: TButton;
OpenButton: TButton;
OpenDialog: TOpenDialog;
VolumeTrackBar: TTrackBar;
VolumeLabel: TLabel;
ProgressTrackBar: TTrackBar;
PositionLabel: TLabel;
Bevel1: TBevel;
StatusBar1: TStatusBar;
Bevel2: TBevel;
ProgressLabel: TLabel;
Timer: TTimer;
procedure TimerTimer(Sender: TObject);
procedure StopButtonClick(Sender: TObject);
procedure FileOpenClick(Sender: TObject);
procedure PlayButtonClick(Sender: TObject);
procedure VolumeTrackBarChange(Sender: TObject);
procedure PauseButtonClick(Sender: TObject);
procedure DXSoundFinalize(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);


private { Playing sound }
FUpdating: Boolean;

DXSound: TDXSound;
Audio: TdspAudioFileStream;

procedure CloseSound;
procedure UpdateInfo;
procedure UpdateFileInfo;

procedure ProgressMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ProgressMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);

private {Sound Processing}
CS: TCriticalSection;

SamplesPerSec: Integer;
BitsPerSample: Integer;
NumOfChannels: Integer;
end;

var
MainForm: TMainForm;

implementation

{$R *.dfm}

Type
TMyTrackBar=class(TTrackBar)
public
property OnMouseDown;
property OnMouseUp;
end;

{==============================================================================}
{ Player }
{==============================================================================}

procedure TMainForm.FormCreate(Sender: TObject);
var I: Integer;
begin
Application.Title:= 'GroundFloorStudio - ' + Caption;

CS:=TCriticalSection.Create;

{Initialization for soun playing}
DXSound:=TDXSound.Create(Self);
DXSound.Options:=[soGlobalFocus];
DXSound.OnFinalize:=DXSoundFinalize;
// Timer1.Enabled:=True;
UpdateInfo;
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
Timer.Enabled:=False;
// Timer1.Enabled:=False;
CloseSound;
DXSound.Free;
CS.Free;
end;

procedure TMainForm.DXSoundFinalize(Sender: TObject);
begin
Audio.Free;
Audio:=nil;
end;

Procedure TMainForm.FileOpenClick(Sender: TObject);
var WaveFormat: TWaveFormatEx;
begin
if not OpenDialog.Execute then Exit;

CloseSound;

try
Caption:= Application.Title + ' - ' + ExtractFileName(OpenDialog.FileName);
DXSound.Initialize;

{Create the audio stream}
Audio:=TdspAudioFileStream.Create(DXSound);
Audio.BufferLength:=1000; // Buffer Lenght = 1 second
Audio.FileName:=OpenDialog.FileName;

SamplesPerSec:=Audio.Format^.nSamplesPerSec;
BitsPerSample:=Audio.Format^.wBitsPerSample;
NumOfChannels:=Audio.Format^.nChannels;

{Settings of format of primary buffer}
MakePCMWaveFormatEx(WaveFormat, 44100, 16, 2);
DXSound.Primary.SetFormat(WaveFormat);
UpdateFileInfo;

PlayButton.Enabled:=True;
PauseButton.Enabled:=False;
StopButton.Enabled:=False;
VolumeTrackBar.Enabled:=True;
ProgressTrackBar.Enabled:=True;
ProgressLabel.Enabled:=True;

FUpdating:=True;
try
VolumeTrackBar.Position:=Audio.Volume;
ProgressTrackBar.Max:=Audio.Size;
ProgressTrackBar.PageSize:=ProgressTrackBar.Max div 25;
TMyTrackBar(ProgressTrackBar).OnMouseUp:=ProgressMouseUp;
TMyTrackBar(ProgressTrackBar).OnMouseDown:=ProgressMouseDown;
finally
FUpdating:=False;
end;
except
CloseSound;
raise;
end;
end;

Procedure TMainForm.CloseSound;
begin
StopButtonClick(nil);
Timer.Enabled:=False;

DXSound.Finalize;
Caption:=Application.Title;

PlayButton.Enabled:=False;
PauseButton.Enabled:=False;
StopButton.Enabled:=False;
VolumeTrackBar.Enabled:=False;
ProgressTrackBar.Enabled:=False;
ProgressLabel.Enabled:=False;

UpdateInfo;
UpdateFileInfo;
end;

procedure TMainForm.VolumeTrackBarChange(Sender: TObject);
begin
if FUpdating then Exit;
Audio.Volume:=VolumeTrackBar.Position * 100;
UpdateInfo;
end;

procedure TMainForm.ProgressMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
CS.Acquire;

Timer.Enabled:=True;
Audio.Position:=ProgressTrackBar.Position;
UpdateInfo;
end;

procedure TMainForm.ProgressMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
CS.Acquire;

Timer.Enabled:=True;
Audio.Position:=ProgressTrackBar.Position;
UpdateInfo;
end;

procedure TMainForm.PlayButtonClick(Sender: TObject);
begin
Audio.Play;

PlayButton.Enabled:=False;
PauseButton.Enabled:=True;
StopButton.Enabled:=True;
Timer.Enabled:=True;
end;

procedure TMainForm.PauseButtonClick(Sender: TObject);
begin
Audio.Stop;

PlayButton.Enabled:=True;
PauseButton.Enabled:=False;
StopButton.Enabled:=False;
Timer.Enabled:=False;
UpdateInfo;
end;

procedure TMainForm.StopButtonClick(Sender: TObject);
begin
if Audio <> nil then
begin
Audio.Stop;
Audio.Position:=0;
end;

PlayButton.Enabled:=True;
PauseButton.Enabled:=False;
StopButton.Enabled:=False;
Timer.Enabled:=False;
UpdateInfo;
end;

procedure TMainForm.TimerTimer(Sender: TObject);
begin
if Audio.Playing then UpdateInfo else
if Audio.Position >= Audio.Size then StopButtonClick(Self);
end;

procedure TMainForm.UpdateInfo;
var P, Q:Integer;
begin
if(Audio <> nil) and (Audio.Format <> nil) then
begin
P:=(Audio.Position div Audio.Format.nBlockAlign) div (SamplesPerSec div 1000);

VolumeLabel.Caption:=Format('Volume: %ddB', [Audio.Volume div 100]);
ProgressLabel.Caption:=Format('Time: %.2d:%.2d:%.2d:%.2d',
[((P div 1000) div 60) div 60,((P div 1000) div 60) mod 60,
(P div 1000) mod 60, (P mod 1000)]);

FUpdating:=True;
try
ProgressTrackBar.Position:=Audio.Position;
finally
FUpdating:=False;
end;
end else
begin
VolumeLabel.Caption:='Volume';
ProgressLabel.Caption:='Time';
end;
end;

procedure TMainForm.UpdateFileInfo;
var FileInfo: String;
begin
if Audio <> nil then
begin
FileInfo:=Format('%d kHz,%d bit,',[SamplesPerSec,BitsPerSample]);
if NumOfChannels=1 then FileInfo:=FileInfo + 'Mono'
else FileInfo:=FileInfo + 'Stereo';

StatusBar1.Panels[0].Width:=StatusBar1.Canvas.TextWidth(FileInfo) + 10;
StatusBar1.Panels[0].Text:=FileInfo;
end else StatusBar1.Panels[0].Width:=0;
end;

end.


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories