Смекни!
smekni.com

Система обработки аудиоинформации Подсистема фильтрации и обработки сигнала (стр. 19 из 24)

if (Status<>'waiting')and(Status<>'starting') then Exit;

Status := 'editing';

SaveUndoInfo;

TempAudio := TAudioData.Create;

TempAudio.nChannels := AudioData.nChannels;

TempAudio.nSamplesPerSec := AudioData.nSamplesPerSec;

TempAudio.nBitsPerSample := AudioData.nBitsPerSample;

TempAudio.Calculate_nBlockAlign;

b := 0;

for i := 1 to TempAudio.nSamplesPerSec*SilenceTime.Value*TempAudio.nBlockAlign do

TempAudio.Data.Write(b, 1);

InsertAudio(TempAudio, AudioData, AudioPosition);

DeleteMarkers;

SetAudioPosition;

SetMarker;

AudioPosition := AudioPosition + TempAudio.Data.Size div AudioData.nBlockAlign - 1;

SetAudioPosition;

SetMarker;

TempAudio.Destroy;

Status := 'waiting';

end;

procedure TMainForm.ReverseButtonClick(Sender: TObject);

var

AudioSize: Cardinal;

begin

if Status<>'waiting' then Exit;

Status := 'deformation';

SaveUndoInfo;

with AudioData do

AudioSize := Data.Size div nBlockAlign;

with Selection do

begin

if not StartExists or not FinishExists then

begin

DeleteMarkers;

Start := 0;

Finish := AudioSize-1;

end;

ReverseAudio(AudioData, Start, Finish-Start+1);

end;

Status := 'waiting';

end;

procedure TMainForm.NormalizeButtonClick(Sender: TObject);

var

AudioSize: Cardinal;

begin

if Status<>'waiting' then Exit;

Status := 'deformation';

with AudioData do

AudioSize := Data.Size div nBlockAlign;

with Selection do

begin

if not StartExists or not FinishExists then

begin

DeleteMarkers;

Start := 0;

Finish := AudioSize-1;

end;

Normalize(AudioData, Start, Finish-Start+1);

end;

Status := 'waiting';

PaintBox1.Repaint;

end;

procedure TMainForm.SetSpeedButtonClick(Sender: TObject);

var

AudioSize: Cardinal;

begin

if Status<>'waiting' then Exit;

Status := 'deformation';

with AudioData do

AudioSize := Data.Size div nBlockAlign;

with Selection do

begin

if not StartExists or not FinishExists then

begin

DeleteMarkers;

Start := 0;

Finish := AudioSize-1;

end;

SetSpeedOfAudio(AudioData, Start, Finish-Start+1, SpeedEdit.Value/100);

DeleteMarkers;

AudioPosition := Start;

SetMarker;

AudioPosition := Trunc(Start+(Finish-Start)*100/SpeedEdit.Value);

SetMarker;

AudioPosition := Start;

SetAudioPosition;

end;

Status := 'waiting';

end;

procedure TMainForm.ChangePropertie(Sender: TObject);

var

S: String;

Value, Code: Cardinal;

begin

if (Status<>'waiting')and(Status<>'starting') then Exit;

Status := 'editing';

if Sender = AudioOptionsForm.nSamplesPerSecButton then

begin

Val(AudioOptionsForm.nSamplesBox.Text, Value, Code);

SetnSamplesPerSec(AudioData, Value);

end;

if Sender = AudioOptionsForm.nBitsPerSampleButton then

begin

Val(AudioOptionsForm.nBitsBox.Text, Value, Code);

SetnBitsPerSample(AudioData, Value);

end;

if Sender = AudioOptionsForm.nChannelsButton then

begin

SetnChannels(AudioData, AudioOptionsForm.nChannelsBox.ItemIndex+1);

end;

AudioData.Calculate_nBlockAlign;

DeleteMarkers;

AudioPosition := 0;

SetAudioPosition;

Str(AudioData.nChannels, S);

AudioOptionsForm.nChannelsText.Caption := S + ' channels';

Str(AudioData.nBitsPerSample, S);

AudioOptionsForm.nBitsPerSampleText.Caption := S + ' bits';

Str(AudioData.nSamplesPerSec, S);

AudioOptionsForm.nSamplesPerSecText.Caption := S + ' Hz';

Status := 'waiting';

end;

procedure TMainForm.SetVolumeButtonClick(Sender: TObject);

var

AudioSize: Cardinal;

begin

if Status<>'waiting' then Exit;

Status := 'deformation';

with AudioData do

AudioSize := Data.Size div nBlockAlign;

with Selection do

begin

if not StartExists or not FinishExists then

begin

DeleteMarkers;

Start := 0;

Finish := AudioSize-1;

end;

SetVolumeOfAudio(AudioData, Start, Finish-Start+1, VolumeEdit.Value/100);

end;

Status := 'waiting';

end;

procedure TMainForm.AboutButtonClick(Sender: TObject);

begin

MessageBox(MainForm.Handle, 'AudioEditor v02.2006'#13#13'Выполнил Суханов М.А.'#13#13'ИжГТУ, 2006'#13#13'smike@pochta.ru', 'О программе', MB_OK);

end;

procedure TMainForm.HomePageLinkClick(Sender: TObject);

begin

ShellExecute(Handle, 'open', PChar('http://Andrei512.narod.ru'), '', '', SW_Show);

end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);

begin

AudioData.Destroy;

AudioClipBoard.Destroy;

UndoInfo.AudioData.Destroy;

end;

procedure TMainForm.SetSpeedBitBtnClick(Sender: TObject);

var

AudioSize: Cardinal;

begin

if Status<>'waiting' then Exit;

Status := 'deformation';

SaveUndoInfo;

with AudioData do

AudioSize := Data.Size div nBlockAlign;

with Selection do

begin

if not StartExists or not FinishExists then

begin

DeleteMarkers;

Start := 0;

Finish := AudioSize-1;

end;

SetSpeedOfAudio(AudioData, Start, Finish-Start+1, SpeedEdit.Value/100);

DeleteMarkers;

AudioPosition := Start;

SetMarker;

AudioPosition := Trunc(Start+(Finish-Start)*100/SpeedEdit.Value);

SetMarker;

AudioPosition := Start;

SetAudioPosition;

end;

Status := 'waiting';

end;

procedure TMainForm.ChangeSpeedButtonClick(Sender: TObject);

var

AudioSize, NewCount: Cardinal;

begin

if Status<>'waiting' then Exit;

Status := 'deformation';

SaveUndoInfo;

with AudioData do

AudioSize := Data.Size div nBlockAlign;

with Selection do

begin

if not StartExists or not FinishExists then

begin

DeleteMarkers;

Start := 0;

Finish := AudioSize-1;

end;

NewCount := ChangeSpeedOfAudio(AudioData, Start, Finish-Start+1, SpeedEdit.Value/100);

DeleteMarkers;

AudioPosition := Start;

SetMarker;

AudioPosition := Start+NewCount;

SetMarker;

AudioPosition := Start;

SetAudioPosition;

end;

Status := 'waiting';

end;

procedure TMainForm.SetVolumeBitBtnClick(Sender: TObject);

var

AudioSize: Cardinal;

begin

if Status<>'waiting' then Exit;

Status := 'deformation';

SaveUndoInfo;

with AudioData do

AudioSize := Data.Size div nBlockAlign;

with Selection do

begin

if not StartExists or not FinishExists then

begin

DeleteMarkers;

Start := 0;

Finish := AudioSize-1;

end;

SetVolumeOfAudio(AudioData, Start, Finish-Start+1, VolumeEdit.Value/100);

end;

Status := 'waiting';

PaintBox1.Repaint;

end;

procedure TMainForm.ChangeVolumeBitBtnClick(Sender: TObject);

var

AudioSize: Cardinal;

begin

if Status<>'waiting' then Exit;

Status := 'deformation';

SaveUndoInfo;

with AudioData do

AudioSize := Data.Size div nBlockAlign;

with Selection do

begin

if not StartExists or not FinishExists then

begin

DeleteMarkers;

Start := 0;

Finish := AudioSize-1;

end;

ChangeVolumeOfAudio(AudioData, Start, Finish-Start+1, VolumeEdit.Value/100);

end;

Status := 'waiting';

PaintBox1.Repaint;

end;

procedure TMainForm.NormalizeBitBtnClick(Sender: TObject);

var

AudioSize: Cardinal;

begin

if Status<>'waiting' then Exit;

Status := 'deformation';

SaveUndoInfo;

with AudioData do

AudioSize := Data.Size div nBlockAlign;

with Selection do

begin

if not StartExists or not FinishExists then

begin

DeleteMarkers;

Start := 0;

Finish := AudioSize-1;

end;

Normalize(AudioData, Start, Finish-Start+1);

end;

Status := 'waiting';

end;

procedure TMainForm.EffectButtonClick(Sender: TObject);

var

AudioSize: Cardinal;

begin

if Status<>'waiting' then Exit;

Status := 'deformation';

SaveUndoInfo;

with AudioData do

AudioSize := Data.Size div nBlockAlign;

with Selection do

begin

if not StartExists or not FinishExists then

begin

DeleteMarkers;

Start := 0;

Finish := AudioSize-1;

end;

if Sender = BitBtn13 then Echo(AudioData, Start, Finish-Start+1, EffOptionsForm.nResponsesEdit.Value, EffOptionsForm.ResponseTimeEdit.Value, EffOptionsForm.ResponseVolumeEdit.Value/100);

if Sender = BitBtn14 then Reverberation(AudioData, Start, Finish-Start+1, EffOptionsForm.nEchosEdit.Value, EffOptionsForm.DelayEdit.Value, EffOptionsForm.EchoVolumeEdit.Value/100);

if Sender = BitBtn15 then ChangeVolumeOfAudio(AudioData, Start, Finish-Start+1, 0);

if Sender = BitBtn16 then ReChangeVolumeOfAudio(AudioData, Start, Finish-Start+1, 1);

{case EffectBox.ItemIndex of

0: ChangeVolumeOfAudio(AudioData, Start, Finish-Start+1, 0);

1: ReChangeVolumeOfAudio(AudioData, Start, Finish-Start+1, 1);

2: Echo(AudioData, Start, Finish-Start+1, nResponsesEdit.Value, ResponseTimeEdit.Value, ResponseVolumeEdit.Value/100);

3: Reverberation(AudioData, Start, Finish-Start+1, nEchosEdit.Value, DelayEdit.Value, EchoVolumeEdit.Value/100);

end;}

end;

Status := 'waiting';

PaintBox1.Repaint;

end;

procedure TMainForm.PaintAudioGraph;

var

PaintPos, MaxPaintPos: Cardinal;

AudioPos, {SamplesPerPoint,} LeftSamples, MaxAmplitude: Cardinal;

numChannels, Channel, i: Word;

Smp, Smp1: Integer;

begin

with PaintBox1.Canvas do

begin

Pen.Color := clBlack;

MoveTo(Round(PaintBox1.Width/2),0);

LineTo(Round(PaintBox1.Width/2), Round(PaintBox1.Height));

MoveTo(0, Round(PaintBox1.Height/2));

LineTo(PaintBox1.Width, Round(PaintBox1.Height/2));

Pen.Color := clGreen;

end;

MaxPaintPos := PaintBox1.Width;

if AudioPosition-PaintBox1.Width*SamplesPerPoint >= 0 then

begin

AudioPos := AudioPosition-PaintBox1.Width*SamplesPerPoint;

PaintPos := 0;

end

else

begin

AudioPos := 0;

PaintPos := PaintBox1.Width - Trunc(AudioPosition/SamplesPerPoint);

end;

numChannels := AudioData.nChannels;

MaxAmplitude := 1;

for i := 1 to AudioData.nBitsPerSample do

MaxAmplitude := MaxAmplitude*2;

Smp := 0;

for Channel := 0 to numChannels-1 do

begin

AudioData.ReadSample(AudioPos, Channel, Smp1);

Smp := Smp + Smp1;

end;

Smp := Round(Smp/numChannels);

PaintBox1.Canvas.MoveTo(PaintPos-Round(PaintBox1.Width/2), Round(PaintBox1.Height/2-Smp/MaxAmplitude*PaintBox1.Height));

LeftSamples := SamplesPerPoint;

while PaintPos<=(MaxPaintPos+Round(PaintBox1.Width/2)) do

begin

Smp := 0;

for Channel := 0 to numChannels-1 do

begin

AudioData.ReadSample(AudioPos, Channel, Smp1);

Smp := Smp + Smp1;

end;

if (Selection.StartExists)and(Selection.FinishExists)and

(AudioPos>Selection.Start)and(AudioPos<Selection.Finish)

then PaintBox1.Canvas.Pen.Color := clRed

else PaintBox1.Canvas.Pen.Color := clGreen;

Smp := Round(Smp/numChannels);

PaintBox1.Canvas.LineTo(PaintPos-Round(PaintBox1.Width/2), Round(PaintBox1.Height/2-Smp/MaxAmplitude*PaintBox1.Height));

Inc(AudioPos);

Dec(LeftSamples);

if LeftSamples = 0 then

begin

Inc(PaintPos);

LeftSamples := SamplesPerPoint;

end;

end;

end;

procedure TMainForm.PaintBox1Paint(Sender: TObject);

begin

PaintAudioGraph;

end;

procedure TMainForm.WaveOutButtonClick(Sender: TObject);

begin

ShellExecute(Handle, 'open', PChar('sndvol32.exe'), '', '', SW_Show);

end;

procedure TMainForm.WaveInButtonClick(Sender: TObject);

begin

ShellExecute(Handle, 'open', PChar('sndvol32.exe'), PChar('/r'), '', SW_Show);

end;

procedure TMainForm.CopyToFileButtonClick(Sender: TObject);

var

FileName, Ext, EncMode, StereoMode, BitRate: String;

i: Byte;

TempAudio: TAudioData;

Code: Integer;

PCM: TPCMFile;

MP3: TMP3File;

EM1: TEM1File;

AudioSize: Cardinal;

begin

if Status<>'waiting' then Exit;

if SaveDialog.Execute then

FileName := SaveDialog.FileName else Exit;

Ext := ExtractFileExt(FileName);

with AudioData do

AudioSize := Data.Size div nBlockAlign;

TempAudio := TAudioData.Create;

with Selection do

begin

if not StartExists or not FinishExists then

begin

DeleteMarkers;

Start := 0;

Finish := AudioSize-1;

end;

CopyAudio(AudioData, TempAudio, Start, Finish);

end;

for i := 1 to Length(Ext) do Ext[i] := UpCase(Ext[i]);

if Ext = '.WAV' then

begin

PCM := TPCMFile.Create(FileName, TempAudio);

PCM.Destroy;

end;

if Ext = '.MP3' then

begin

if RadioButton1.Checked then

begin

BitRate := MP3OptionsForm.ConstantBitRateComboBox.Text;

EncMode := '-b';

end;

if RadioButton2.Checked then

begin

BitRate := MP3OptionsForm.AverageBitRateComboBox.Text;

EncMode := '--abr';

end;

if RadioButton3.Checked then

begin

Str(MP3OptionsForm.VariableBitrateComboBox.ItemIndex, BitRate);

EncMode := '-V';

end;

case MP3OptionsForm.StereoModeComboBox.ItemIndex of

0: StereoMode := 's';

1: StereoMode := 'j';

2: StereoMode := 'f';

3: StereoMode := 'd';

4: StereoMode := 'm';

end;

MP3 := TMP3File.Create(FileName, TempAudio, BitRate, EncMode, StereoMode);

MP3.Destroy;

end;

if Ext = '.EM1' then

begin

EM1 := TEM1File.Create(FileName, TempAudio);

EM1.Destroy;

end;

TempAudio.Destroy;

end;

procedure TMainForm.SaveUndoInfo;

begin

if not UndoCheckBox.Checked then Exit;

UndoInfo.AudioPosition := AudioPosition;

UndoInfo.Selection.Start := Selection.Start;

UndoInfo.Selection.Finish := Selection.Finish;

UndoInfo.Selection.StartExists := Selection.StartExists;

UndoInfo.Selection.FinishExists := Selection.FinishExists;

UndoInfo.AudioData.Data.Clear;

CopyAudio(AudioData, UndoInfo.AudioData, 0, AudioData.Data.Size div AudioData.nBlockAlign - 1);

end;

procedure TMainForm.UndoButtonClick(Sender: TObject);

begin

if Status<>'waiting' then Exit;

if UndoInfo.AudioData.Data.Size = 0 then Exit;

Status := 'undo';

DeleteMarkers;

AudioData.Data.Clear;

CopyAudio(UndoInfo.AudioData, AudioData, 0, UndoInfo.AudioData.Data.Size div UndoInfo.AudioData.nBlockAlign - 1);

if UndoInfo.Selection.StartExists then

begin

AudioPosition := UndoInfo.Selection.Start;

SetMarker;

end;

if UndoInfo.Selection.FinishExists then

begin

AudioPosition := UndoInfo.Selection.Finish;

SetMarker;

end;

AudioPosition := UndoInfo.AudioPosition;

SetAudioPosition;

UndoInfo.AudioData.Data.Clear;

Status := 'waiting';

end;

procedure TMainForm.EMailButtonClick(Sender: TObject);

begin

ShellExecute(Handle, 'open', PChar('mailto:Andrei512@narod.ru'), PChar(''), '', SW_Show);

end;

procedure TMainForm.BrainWaveButtonClick(Sender: TObject);

var

AudioSize: Cardinal;

begin

if Status<>'waiting' then Exit;

Status := 'deformation';

SaveUndoInfo;

with AudioData do

AudioSize := Data.Size div nBlockAlign;

with Selection do

begin

if not StartExists or not FinishExists then