Skip to content

Commit

Permalink
Merge pull request #6 from Lulu04/ALSound_dev
Browse files Browse the repository at this point in the history
V2.0.0
  • Loading branch information
Lulu04 authored Feb 15, 2023
2 parents 78cc04c + f75f300 commit 3e5e943
Show file tree
Hide file tree
Showing 23 changed files with 921 additions and 626 deletions.
102 changes: 51 additions & 51 deletions example/Loopback/loopback.lps
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
<ResourceBaseClass Value="Form"/>
<UnitName Value="Unit1"/>
<IsVisibleTab Value="True"/>
<TopLine Value="147"/>
<CursorPos X="80" Y="163"/>
<TopLine Value="50"/>
<CursorPos X="12" Y="64"/>
<UsageCount Value="200"/>
<Loaded Value="True"/>
<LoadedDesigner Value="True"/>
Expand Down Expand Up @@ -312,8 +312,8 @@
<Filename Value="..\..\source\alsound.pas"/>
<UnitName Value="ALSound"/>
<EditorIndex Value="1"/>
<TopLine Value="29"/>
<CursorPos X="6" Y="33"/>
<TopLine Value="619"/>
<CursorPos X="15" Y="634"/>
<UsageCount Value="100"/>
<Loaded Value="True"/>
</Unit42>
Expand Down Expand Up @@ -428,123 +428,123 @@
<JumpHistory Count="30" HistoryIndex="29">
<Position1>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="773" Column="61" TopLine="753"/>
<Caret Line="961" Column="13" TopLine="946"/>
</Position1>
<Position2>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="689" Column="3" TopLine="679"/>
<Caret Line="1603" Column="13" TopLine="1588"/>
</Position2>
<Position3>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="4168" Column="54" TopLine="4144"/>
<Caret Line="1636" Column="31" TopLine="1622"/>
</Position3>
<Position4>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="623" Column="18" TopLine="605"/>
<Caret Line="1676" TopLine="1664"/>
</Position4>
<Position5>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="2410" Column="3" TopLine="2391"/>
<Caret Line="1016" Column="15" TopLine="1001"/>
</Position5>
<Position6>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="685" Column="22" TopLine="670"/>
<Caret Line="1654" TopLine="1650"/>
</Position6>
<Position7>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="4109" Column="3" TopLine="4105"/>
<Caret Line="963" Column="26" TopLine="953"/>
</Position7>
<Position8>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="697" Column="27" TopLine="675"/>
<Caret Line="1475" TopLine="1463"/>
</Position8>
<Position9>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="4218" Column="21" TopLine="4205"/>
<Filename Value="unit1.pas"/>
<Caret Line="130" Column="39" TopLine="116"/>
</Position9>
<Position10>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="4601" Column="28" TopLine="4587"/>
<Filename Value="unit1.pas"/>
<Caret Line="132" Column="14" TopLine="116"/>
</Position10>
<Position11>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="346" Column="14" TopLine="332"/>
<Filename Value="unit1.pas"/>
<Caret Line="323" Column="10" TopLine="306"/>
</Position11>
<Position12>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="4473" Column="20" TopLine="4459"/>
<Filename Value="unit1.pas"/>
<Caret Line="130" TopLine="116"/>
</Position12>
<Position13>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="697" Column="23" TopLine="687"/>
<Caret Line="634" Column="22" TopLine="619"/>
</Position13>
<Position14>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="4281" Column="28" TopLine="4275"/>
<Caret Line="3777" Column="36" TopLine="3774"/>
</Position14>
<Position15>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="4282" Column="74" TopLine="4275"/>
<Filename Value="unit1.pas"/>
<Caret Line="130" TopLine="116"/>
</Position15>
<Position16>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="4476" Column="60" TopLine="4452"/>
<Filename Value="unit1.pas"/>
<Caret Line="297" Column="16" TopLine="276"/>
</Position16>
<Position17>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="4283" Column="6" TopLine="4271"/>
<Filename Value="unit1.pas"/>
<Caret Line="312" Column="47" TopLine="298"/>
</Position17>
<Position18>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="857" Column="18" TopLine="842"/>
<Filename Value="unit1.pas"/>
<Caret Line="98" Column="35" TopLine="82"/>
</Position18>
<Position19>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="43" Column="24" TopLine="22"/>
<Filename Value="unit1.pas"/>
<Caret Line="436" Column="31" TopLine="427"/>
</Position19>
<Position20>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="1116" Column="55" TopLine="1098"/>
<Filename Value="unit1.pas"/>
<Caret Line="130" Column="21" TopLine="127"/>
</Position20>
<Position21>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="1032" Column="14" TopLine="1017"/>
<Filename Value="unit1.pas"/>
<Caret Line="90" Column="14" TopLine="78"/>
</Position21>
<Position22>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="2097" Column="10" TopLine="2093"/>
<Filename Value="unit1.pas"/>
<Caret Line="324" Column="53" TopLine="303"/>
</Position22>
<Position23>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="1117" Column="51" TopLine="1109"/>
<Filename Value="unit1.pas"/>
<Caret Line="131" Column="21" TopLine="127"/>
</Position23>
<Position24>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="1032" Column="14" TopLine="1017"/>
<Filename Value="unit1.pas"/>
<Caret Line="97" Column="29" TopLine="75"/>
</Position24>
<Position25>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="2095" TopLine="2094"/>
<Filename Value="unit1.pas"/>
<Caret Line="458" Column="23" TopLine="430"/>
</Position25>
<Position26>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="38" Column="28" TopLine="25"/>
<Filename Value="unit1.pas"/>
<Caret Line="64" Column="12" TopLine="50"/>
</Position26>
<Position27>
<Filename Value="..\..\source\alsound.pas"/>
<Caret Line="33" Column="6" TopLine="29"/>
<Filename Value="unit1.pas"/>
<Caret Line="338" Column="6" TopLine="321"/>
</Position27>
<Position28>
<Filename Value="unit1.pas"/>
<Caret Line="288" Column="21" TopLine="271"/>
<Caret Line="427" Column="10" TopLine="403"/>
</Position28>
<Position29>
<Filename Value="unit1.pas"/>
<Caret Line="75" Column="20" TopLine="66"/>
<Caret Line="64" Column="12" TopLine="50"/>
</Position29>
<Position30>
<Filename Value="unit1.pas"/>
<Caret Line="160" Column="29" TopLine="156"/>
<Caret Line="427" Column="10" TopLine="403"/>
</Position30>
</JumpHistory>
<RunParams>
Expand Down
54 changes: 32 additions & 22 deletions example/Loopback/unit1.pas
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,17 @@ TForm1 = class(TForm)
FLoopbackContext: TALSLoopbackContext;
// tracks instance
FTracks: array[0..2] of TTrack;
FMixingTime: double;
FCanceled: boolean;
procedure InitTracks;
function GetSampleRate: integer;
function GetChannel: TALSLoopbackChannel;
function GetSampleType: TALSLoopbackSampleType;
procedure EnableMix(aState: boolean);
procedure EnableMixGUI(aState: boolean);
private
procedure ProcessLoopbackContextOnProgress(Sender: TALSLoopbackContext;
aTimePos: double; const aFrameBuffer: TALSLoopbackFrameBuffer;
var SaveBufferToFile: boolean; var Done: boolean);
var SaveBufferToFile, Cancel: boolean);
public

end;
Expand Down Expand Up @@ -126,9 +128,7 @@ procedure TForm1.BitBtn1Click(Sender: TObject);
procedure TForm1.BCancelClick(Sender: TObject);
begin
// User want to cancel the mix
FLoopbackContext.CancelMix;

BCancel.Tag := 1;
FCanceled := True;
end;

procedure TForm1.ComboBox2Select(Sender: TObject);
Expand Down Expand Up @@ -259,7 +259,7 @@ procedure TForm1.BMixToFileClick(Sender: TObject);
end;


EnableMix(False); // Avoid any user's interaction.
EnableMixGUI(False); // Avoid any user's interaction.

// Customize our context attributes for a loopback context
FAttribs.InitDefault; // don't forget this first !
Expand Down Expand Up @@ -289,7 +289,7 @@ procedure TForm1.BMixToFileClick(Sender: TObject);
outputFilename := ChangeFileExt(Edit1.Text, '.wav');
outputFilename := ConcatPaths([DirectoryEdit1.Text, outputFilename]);

// In this demo, the file output major format is wav file and the bit width is
// In this demo, the file output major format is 'wav' and the bit width is
// the same as the loopback context.
case ComboBox3.ItemIndex of
0: fileFormat := ALSMakeFileFormat( SF_FORMAT_WAV, SF_FORMAT_PCM_16);
Expand All @@ -303,30 +303,39 @@ procedure TForm1.BMixToFileClick(Sender: TObject);
begin
ShowMessage('Can not create output file' + LineEnding + outputFilename);
FreeAndNil(FLoopbackContext);
EnableMix(True);
EnableMixGUI(True);
Exit;
end;

// Define a callback to update our progress bar, vu-meters and controls
// the mixing process. This callback will be fired each time a buffer is
// Define a callback to update our progress bar, vu-meters and controls the
// mixing process. This callback will be fired each time a buffer is
// filled with audio
FLoopbackContext.OnProgress := @ProcessLoopbackContextOnProgress;

// Start the mixing -> keep the hand until the mixing is done or canceled.
// In the meantime OnProgress callback is fired.
FLoopbackContext.StartMixing;
FCanceled := False;

// We have to call this method before render audio.
FLoopbackContext.BeginOfMix;

repeat
// Ask the context to render 10Ms of audio.
FLoopbackContext.Mix(0.010);
until (FMixingTime >= FloatSpinEdit2.Value) or // mixing time reach the end of the interval
FCanceled; // user click cancel button

// We have to call this method at the end, to finalize the mixing process.
FLoopbackContext.EndOfMix;

// Checks error only if the mix was not canceled
if BCancel.Tag = 0 then // if user clicks cancel button, its Tag is sets to 1.
if not FCanceled then
begin
// Check mixing error
if FLoopbackContext.MixingError then
Showmessage(FLoopbackContext.MixingStrError)
else
ShowMessage('Mixdown saved to' + lineending +
outputFilename + lineending + 'SUCCESS');
end
else BCancel.Tag := 0;
outputFilename + lineending + 'WITH SUCCESS');
end;

// Free loopback context (and loopback device)
FreeAndNil(FLoopbackContext);
Expand All @@ -337,7 +346,7 @@ procedure TForm1.BMixToFileClick(Sender: TObject);
ProgressBar1.Position := 0;
ProgressBar2.Position := ALS_DECIBEL_MIN_VALUE;
ProgressBar3.Position := ALS_DECIBEL_MIN_VALUE;
EnableMix(True);
EnableMixGUI(True);
end;

procedure TForm1.TrackBar1Change(Sender: TObject);
Expand Down Expand Up @@ -405,7 +414,7 @@ function TForm1.GetSampleType: TALSLoopbackSampleType;
end;
end;

procedure TForm1.EnableMix(aState: boolean);
procedure TForm1.EnableMixGUI(aState: boolean);
begin
Panel1.Enabled := aState;
Panel2.Enabled := aState;
Expand All @@ -426,8 +435,10 @@ procedure TForm1.EnableMix(aState: boolean);
//
procedure TForm1.ProcessLoopbackContextOnProgress(Sender: TALSLoopbackContext;
aTimePos: double; const aFrameBuffer: TALSLoopbackFrameBuffer;
var SaveBufferToFile: boolean; var Done: boolean);
var SaveBufferToFile, Cancel: boolean);
begin
FMixingTime := aTimePos;

// update the progress bar according to the current mixing time position
ProgressBar1.Position := Round(ProgressBar1.Max*aTimePos/FloatSpinEdit2.Value);

Expand All @@ -443,8 +454,7 @@ procedure TForm1.ProcessLoopbackContextOnProgress(Sender: TALSLoopbackContext;
SaveBufferToFile := (aTimePos >= FloatSpinEdit1.Value) and
(aTimePos <= FloatSpinEdit2.Value);

// Stops the mixing when the current mixing time position reach the end time.
Done := aTimePos >= FloatSpinEdit2.Value;
Cancel := FCanceled;
end;

end.
2 changes: 1 addition & 1 deletion example/console_alplay/console_alplay.lpr
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
while OurMusic.State = ALS_PLAYING do
begin
write(#13+'Press a key to stop - Played '+
FormatFloat('0.0', OurMusic.GetTimePosition)+'/'+
FormatFloat('0.0', OurMusic.TimePosition)+'/'+
FormatFloat('0.0', OurMusic.TotalDuration)+' s');
sleep(200);
if KeyPressed then OurMusic.Stop;
Expand Down
Loading

0 comments on commit 3e5e943

Please sign in to comment.