Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Multimedia (https://www.delphipraxis.net/16-multimedia/)
-   -   Delphi Erzeugen einer Schwingung in Stereo 16Bit (https://www.delphipraxis.net/10219-erzeugen-einer-schwingung-stereo-16bit.html)

cheezy 14. Okt 2003 09:53


Erzeugen einer Schwingung in Stereo 16Bit
 
Hallo Leute!!
Folgendes......

Ich habe hier ein Programm, dass mir einen Ton über die Soundkarte erzeugt und ausgiebt.
Mein Problem ist, dass das Programm mit Mono und 8 Bit arbeitet....ich möchte aber Stereo und 16 Bit haben!!
Sobald ich aber die Werte umstelle bekomme ich nen falschen Ton und eine falsche Zeitdauer!!!

Was muss ich änderen, dass das trotzdem funkt??!
Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
MMSystem,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;
  WaveFormatEx : TWaveFormatEx;

implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
  i, TempInt,
  DataCount,
  RiffCount   : integer;
  SoundValue  : byte;
  w           : double;
  MS          : TMemoryStream;

const
  Mono      : Word = $0001;
  SampleRate : integer = 11025;
  RiffId    : string = 'RIFF';
  WaveId    : string = 'WAVE';
  FmtId     : string = 'fmt ';
  DataId    : string = 'data';
  Duration  : integer=1500;
  Frequency : integer=1500;
    begin
  with WaveFormatEx do begin
    wFormatTag := WAVE_FORMAT_PCM;
    nChannels := Mono;
    nSamplesPerSec := SampleRate;
    wBitsPerSample := $0008;
    nAvgBytesPerSec := nSamplesPerSec * nBlockAlign;
    nBlockAlign := (nChannels * wBitsPerSample) div 8;
    cbSize := 0;
  end;
  MS := TMemoryStream.Create;
  with MS do begin
    DataCount := (Duration *  SampleRate) div 1000; // sound data
    RiffCount := Length(WaveId)
                 + Length(FmtId) + SizeOf(DWord)
                 + SizeOf(TWaveFormatEx)
                 + Length(DataId) + SizeOf(DWord)
                 + DataCount; // file data
    Write(RiffId[1], 4);
    Write(RiffCount, SizeOf(DWord));
    Write(WaveId[1], Length(WaveId));          // 'WAVE'
    Write(FmtId[1], Length(FmtId));            // 'fmt '
    TempInt := SizeOf(TWaveFormatEx);
    Write(TempInt, SizeOf(DWord));
    Write(WaveFormatEx, SizeOf(TWaveFormatEx));
    Write(DataId[1], Length(DataId));          // 'data'
    Write(DataCount, SizeOf(DWord));
    w := 2 * Pi * Frequency; // omega
    for i := 0 to DataCount - 1 do begin
      SoundValue := 127 + trunc(127 * sin(i * w / SampleRate));
      Write(SoundValue, SizeOf(Byte));
    end;
    sndPlaySound(MS.Memory, SND_MEMORY or SND_SYNC);
    MS.Free;
  end;
end;
end.
Danke für euer Hilfe

mfg
christoph


Alle Zeitangaben in WEZ +1. Es ist jetzt 11:34 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz