home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 November / CDVD1105.ISO / Software / Freeware / programare / bass / Delphi / StreamTest / STMain.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2005-09-14  |  4.1 KB  |  162 lines

  1. unit STMain;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, BASS;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Button1: TButton;
  12.     Button2: TButton;
  13.     Button3: TButton;
  14.     GroupBox1: TGroupBox;
  15.     ScrollBar1: TScrollBar;
  16.     Label1: TLabel;
  17.     Label2: TLabel;
  18.     ScrollBar2: TScrollBar;
  19.     Label3: TLabel;
  20.     Label4: TLabel;
  21.     Label5: TLabel;
  22.     Label6: TLabel;
  23.     Button4: TButton;
  24.     Label7: TLabel;
  25.     Label8: TLabel;
  26.     procedure Button4Click(Sender: TObject);
  27.     procedure FormCreate(Sender: TObject);
  28.     procedure Button1Click(Sender: TObject);
  29.     procedure Button2Click(Sender: TObject);
  30.     procedure Button3Click(Sender: TObject);
  31.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  32.     procedure ScrollBar1Change(Sender: TObject);
  33.     procedure ScrollBar2Change(Sender: TObject);
  34.   private
  35.     { Private-Deklarationen }
  36.     SineStream: HSTREAM;
  37.     procedure Error(msg: string);
  38.   public
  39.     { Public-Deklarationen }
  40.   end;
  41.  
  42. var
  43.   Form1: TForm1;
  44.   SineCount, Frequency, Amplitude: Real;
  45.  
  46. implementation
  47.  
  48. {$R *.DFM}
  49.  
  50. function MakeSine(handle: HSTREAM; buffer: Pointer; length: DWORD; user: DWORD): DWORD; stdcall;
  51. var
  52.   buf: ^WORD;
  53.   i, len: Integer;
  54. begin
  55.   buf := buffer;
  56.   len := length div 2;
  57.   // write the sine function to the output stream
  58.   for i := 0 to len - 1 do begin
  59.     buf^ := Trunc(Sin(SineCount * PI) * Amplitude);
  60.     Inc(buf);
  61.     SineCount := SineCount + (Frequency / 44100);
  62.   end;
  63.   Result := length;
  64. end;
  65.  
  66. procedure TForm1.Error(msg: string);
  67. var
  68.   s: string;
  69. begin
  70.   // add the error code to the output string
  71.   s := msg + #13#10 + '(error code: ' + IntToStr(BASS_ErrorGetCode) + ')';
  72.   MessageBox(handle, PChar(s), 'BASS Error', MB_OK or MB_ICONERROR);
  73. end;
  74.  
  75. procedure TForm1.Button4Click(Sender: TObject);
  76. begin
  77.   Close;
  78. end;
  79.  
  80. procedure TForm1.FormCreate(Sender: TObject);
  81. begin
  82.   // enable the BASS Init button
  83.   Button1.Enabled := TRUE;
  84. end;
  85.  
  86. procedure TForm1.Button1Click(Sender: TObject);
  87. begin
  88.   // do we have the right BASS version?
  89.   if BASS_GetVersion <> DWORD(MAKELONG(2,2)) then begin
  90.     Error('BASS version 2.2 was not loaded');
  91.     Exit;
  92.   end;
  93.   // Initialize BASS with the default device
  94.   if not BASS_Init(-1, 44100, 0, handle, nil) then begin
  95.     Error('Could not initialize BASS');
  96.     Exit;
  97.   end;
  98.   // if all successful, enable the create stream button
  99.   Button1.Enabled := FALSE;
  100.   Button2.Enabled := TRUE;
  101. end;
  102.  
  103. procedure TForm1.Button2Click(Sender: TObject);
  104. begin
  105.   (*
  106.     create a stream with a sample rate of 44100Hz
  107.     the max. output rate is sample rate / 2
  108.     i.e. we have a 22050Hz stream!
  109.     however, we'll set the max. output frequency
  110.     of the sine wave lower becouse the human
  111.     ear isn't able to hear waves above 16KHz...
  112.   *)
  113.   SineStream := BASS_StreamCreate(44100, 2, 0, @MakeSine, 0);
  114.   if (SineStream = 0) then begin
  115.     Error('Could not create user stream');
  116.     Exit;
  117.   end;
  118.   // if successfully called, enable the play stream button
  119.   Button2.Enabled := FALSE;
  120.   Button3.Enabled := TRUE;
  121. end;
  122.  
  123. procedure TForm1.Button3Click(Sender: TObject);
  124. begin
  125.   // reset the sine counter
  126.   SineCount := 0;
  127.   // initialize the amplitude and the frequency
  128.   Frequency := ScrollBar1.Position;
  129.   Amplitude := ScrollBar2.Position;
  130.   if not BASS_ChannelPlay(SineStream, False) then begin
  131.     Error('Could not start stream playback');
  132.     Exit;
  133.   end;
  134.   // enable the potentiometers
  135.   Button3.Enabled := FALSE;
  136.   GroupBox1.Enabled := TRUE;
  137. end;
  138.  
  139. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  140. begin
  141.   // release it
  142.   // the stream will be released automatically
  143.   BASS_Free;
  144. end;
  145.  
  146. procedure TForm1.ScrollBar1Change(Sender: TObject);
  147. begin
  148.   // update the output frequency
  149.   Frequency := ScrollBar1.Position;
  150.   Label7.Caption := IntToStr(ScrollBar1.Position) + 'Hz';
  151. end;
  152.  
  153. procedure TForm1.ScrollBar2Change(Sender: TObject);
  154. begin
  155.   // update the output amplitude
  156.   Amplitude := ScrollBar2.Position;
  157.   Label8.Caption := IntToStr(ScrollBar2.Position * 100 div 32767) + '%';
  158. end;
  159.  
  160.  
  161. end.
  162.