Einzelnen Beitrag anzeigen

Flips

Registriert seit: 17. Feb 2005
Ort: Sankt Wendel
491 Beiträge
 
Delphi 7 Professional
 
#3

Re: TMemoryStream - Instanzbildung scheitert?

  Alt 8. Jan 2010, 15:04
Ich habs jetzt mal quick'n'dirty per global Variable in der neuen Anwendung gemacht, da klappts auch...Aber keine Ahnung, wieso folgender Code etwas am Stream selbst ändert. Der Code ist übrigends dieser hier, nur für meine Zwecke ein wenig geändert.

Delphi-Quellcode:
 private
    FMS : TMemoryStream;
 public
    property MS : TMemoryStream read FMS write FMS;
  end;

[...]

procedure TForm2.GenerateSound(FromFQ, ToFQ, FQJumps, FQDuration: Integer; Volume: TVolumeLevel);
  {writes tone to memory and plays it}
var
  WaveFormatEx: TWaveFormatEx;
  MS: TMemoryStream;
  i, TempInt, DataCount, RiffCount, Frequency: integer;
  SoundValue: byte;
  w: double; // omega ( 2 * pi * frequency)
const
  Mono: Word = $0001;
  SampleRate: Integer = 44100; // 8000, 11025, 22050, or 44100
  RiffId: string = 'RIFF';
  WaveId: string = 'WAVE';
  FmtId: string = 'fmt ';
  DataId: string = 'data';
begin
  with WaveFormatEx do
  begin
    wFormatTag := WAVE_FORMAT_PCM;
    nChannels := Mono;
    nSamplesPerSec := SampleRate;
    wBitsPerSample := $0008;
    nBlockAlign := (nChannels * wBitsPerSample) div 8;
    nAvgBytesPerSec := nSamplesPerSec * nBlockAlign;
    cbSize := 0;
  end;

  with FMS do
  begin
    {Calculate length of sound data and of file data}
    DataCount := (ceil((ToFQ-FromFQ)/FQJumps) * FQDuration * SampleRate) div 1000; // sound data
    RiffCount := Length(WaveId) + Length(FmtId) + SizeOf(DWORD) +
      SizeOf(TWaveFormatEx) + Length(DataId) + SizeOf(DWORD) + DataCount; // file data
    {write out the wave header}

    Write(RiffId[1], 4); // 'RIFF' -> ########################## Hier krachts
    Write(RiffCount, SizeOf(DWORD)); // file data size
    Write(WaveId[1], Length(WaveId)); // 'WAVE'
    Write(FmtId[1], Length(FmtId)); // 'fmt '
    TempInt := SizeOf(TWaveFormatEx);
    Write(TempInt, SizeOf(DWORD)); // TWaveFormat data size
    Write(WaveFormatEx, SizeOf(TWaveFormatEx)); // WaveFormatEx record
    Write(DataId[1], Length(DataId)); // 'data'
    Write(DataCount, SizeOf(DWORD)); // sound data size
    {calculate and write out the tone signal} // now the data values
    Frequency := FromFQ;
    while Frequency <= ToFQ do
    begin
      w := 2 * Pi * Frequency; // omega
      Memo1.Lines.Add('Schreibe ' +inttostr(Frequency)+'Hz');
      for i := 0 to ((FQDuration * SampleRate) div 1000) - 1 do
      begin
        SoundValue := 127 + trunc(Volume * sin(i * w / SampleRate)); // wt = w * i / SampleRate
        Write(SoundValue, SizeOf(Byte));
      end;
      inc(Frequency,FQJumps);
    end;

  end;
end;


procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FMS.Free;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
FMS := TMemoryStream.Create;
end;
Philipp F.
  Mit Zitat antworten Zitat