This example effect chart:
Code files:
Unit Unit1;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls, Extctrls, Comctrls;
Type
TForm1 = Class (Tform)
Opendialog1:topendialog;
Paintbox1:tpaintbox;
Button1:tbutton;
Button2:tbutton;
Button3:tbutton;
Colorbox1:tcolorbox;
Colorbox2:tcolorbox;
Procedure Formcreate (Sender:tobject);
Procedure Button1Click (Sender:tobject);
Procedure Button2click (Sender:tobject);
Procedure Button3click (Sender:tobject);
Procedure Colorbox1change (Sender:tobject);
Procedure Colorbox2change (Sender:tobject);
Procedure Formdestroy (Sender:tobject);
Procedure Paintbox1paint (Sender:tobject);
Private
Procedure Draw;
End
Var
Form1:tform1;
Implementation
{$R *.DFM}
Uses Bass;
Var
Hs:hstream; {Stream Handle}
Data:array of Cardinal;
Bit:tbitmap;
Procedure Tform1.formcreate (Sender:tobject);
Begin
bit: = Tbitmap.create;
Paintbox1.align: = Altop;
colorbox1.selected: = Clblack;
colorbox2.selected: = Cllime;
If HiWord (bass_getversion) <> Bassversion Then
MessageBox (0, "Bass.dll" file version is not suitable!) ', nil, mb_iconerror);
If not Bass_init ( -1, 44100, 0, 0, nil) then ShowMessage (' initialization error ');
End
Open
Procedure Tform1.button1click (Sender:tobject);
Var
mp3path:ansistring;
i:cardinal;
time:double;
Hs2:hstream;
Begin
Bass_streamfree (HS);
Opendialog1.filter: = ' Mp3 file (*.mp3) |*.mp3| Wav file (*.wav) |*wav ';
If Opendialog1.execute Then
Mp3path: = ansistring (Opendialog1.filename);
HS: = Bass_streamcreatefile (False, Pansichar (Mp3path), 0, 0, 0);
If HS < bass_error_ended Then
Text: = ' Open failed '
ELSE begin
Text: = string (Mp3path);
Bit. Free;
bit: = Tbitmap.create;
Paintbox1.repaint;
{The following lines are not good to understand}
{HS2 the file stream, the final parameter is: Bass_stream_decode, so that the waveform data can be read in advance}
HS2: = Bass_streamcreatefile (False, Pansichar (Mp3path), 0, 0, Bass_stream_decode);
{When using Bass_channelgetlevel to obtain peak value, it is in 20ms as a unit; Get total time First}
Time: = Bass_channelbytes2seconds (HS2, Bass_channelgetlength (HS, bass_pos_byte));
{Time * 1000 div 20 + 1 is the total peak data that can be obtained, as well as the size required for the array}
SetLength (Data, Trunc (Time * 50 + 1));
{Traverse peak data fill Array}
For I: = 0 to Length (Data)-1 do data[i]: = Bass_channelgetlevel (HS2);
{HS2 has completed its mission at this time, release it}
Bass_streamfree (HS2);
{Call Drawing process}
Draw;
End
End
Play
Procedure Tform1.button2click (Sender:tobject);
Begin
Bass_channelplay (HS, False);
End
Suspended
Procedure Tform1.button3click (Sender:tobject);
Begin
Bass_channelpause (HS);
End
{Background color}
Procedure Tform1.colorbox1change (Sender:tobject);
Begin
Draw;
End
{Foreground color}
Procedure Tform1.colorbox2change (Sender:tobject);
Begin
Draw;
End
Procedure Tform1.formdestroy (Sender:tobject);
Begin
Bass_free;
Bit. Free;
End
Refresh
Procedure Tform1.paintbox1paint (Sender:tobject);
Begin
PaintBox1.Canvas.StretchDraw (Bounds (0, 0, Paintbox1.width, paintbox1.height), bit);
End
{Draw Wave Diagram}
Procedure Tform1.draw;
Var
I,ch:integer;
L,r:smallint;
Begin
Bit. Width: = Length (Data);
Bit. Height: = Paintbox1.height;
CH: = bit. Height Div 2;
Bit. Canvas.Brush.Color: = colorbox1.selected;
Bit. Canvas.fillrect (Bounds (0, 0, bit). Width, bit. Height));
Bit. Canvas.Pen.Color: = colorbox2.selected;
For I: = 0 to Length (Data)-1 do
Begin
L: = LoWord (Data[i]);
R: = HiWord (Data[i]);
Bit. Canvas.moveto (i, Ch-trunc (l/32768*ch));
Bit. Canvas.lineto (i, CH + Trunc (r/32768*ch));
End
Paintbox1.repaint;
End
End.