现在我点击了两次按钮,第一次是我用来打开 wav 文件并向其应用一些失真。我在这个过程中使用了opendialog,现在我想播放我在第一个程序中使用opendialog打开的声音文件。我该怎么做?因为第一个过程中定义的变量(对于我通过 opendialog 打开的文件)不能传递给另一个过程。 如果有人能为我解决这个问题,我将不胜感激。
我希望我能解决这个问题,我可以播放我在上一过程中选择的文件。 这是我当前的两个过程的代码,我尝试在第二个过程中再次使用 opendialog,但结果出乎意料。
procedure TForm1.Button1Click(Sender: TObject);
type
TWavheader = record
idRIFF: array[0..3] of Ansichar;
Chunksize: longint;
idWave: array[0..3] of Ansichar;
idFormat: array[0..3] of Ansichar;
Subchunksieze: longint;
Audiofmt: smallint;
Numchan: smallint;
Samplerate: longint;
Byterate: longint;
Allign: smallint;
Bps: smallint;
end;
TDataheader = record
idData: array[0..3] of Ansichar;
datasize: longint;
end;
var
myFile: TFilestream;
Waveheader: TWavheader;
Dataheader: TDataheader;
datain, dataout: array of SmallInt;
datafactorin: single;
datafactorout: smallint;
Sample: single;
i: integer;
numofsamp, frequency: integer;
channel, bitpersamp: Smallint;
Inputfile: string;
Outputfile: string;
threshold1: single;
threshold2: single;
begin
Opendialog1.execute();
Inputfile := Opendialog1.filename;
threshold1 := StrToFloat(Upperthreshold.Text);
threshold2 := StrToFloat(Lowerthreshold.Text);
myFile := TFileStream.Create(inputfile, fmOpenRead);
myFile.readbuffer(Waveheader, sizeof(TWavheader));
if (Waveheader.idRIFF <> 'RIFF') or (Waveheader.idWave <> 'WAVE') then
raise Exception.Create('Selected file is not a valid WAV file.');
bitpersamp := Waveheader.Bps;
channel := Waveheader.Numchan;
frequency := Waveheader.Samplerate;
if (bitpersamp = 0) or (channel = 0) then
raise Exception.Create('Invalid WAV header: bitspersamp or channel is zero.');
myFile.readbuffer(Dataheader, sizeof(TDataheader));
numofsamp := Dataheader.datasize div (channel * (bitpersamp div 8));
Setlength(datain, numofsamp);
SetLength(dataout, numofsamp);
myFile.Readbuffer(datain[0], Dataheader.datasize);
if numofsamp > 0 then
for i := 0 to numofsamp -1 do
begin
Sample := datain[i];
datafactorin := Sample/32767;
if datafactorin > threshold1 then datafactorin := threshold1;
if datafactorin < threshold2 then datafactorin := threshold2;
datafactorout := round(datafactorin * 32767);
dataout[i] := datafactorout;
end;
Dataheader.datasize := Length(dataout) * SizeOf(SmallInt);
Outputfile := 'D:\output.wav';
if numofsamp > 0 then
myFile := TFileStream.Create(Outputfile, fmCreate);
myFile.Writebuffer(Waveheader, sizeof(TWavheader));
myFile.Writebuffer(Dataheader, sizeof(TDataheader));
myFile.Writebuffer(dataout[0], Dataheader.datasize);
myFile.free;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
opendialog1.execute();
sndPlaySound(PChar(opendialog1.filename), SND_NODEFAULT);
end;
end.
您有 opendialog1。正如我猜测的那样,它定义了形式。 OpenDialog 有您需要的 FileName 属性。 建议:不要将程序逻辑放在事件处理程序中。从来没有