Skip to content

Commit

Permalink
Refactor soundio_example (#11)
Browse files Browse the repository at this point in the history
Use GNATCOLL.Opt_Parse for command line arguments parsing. For the
moment it's useless, but the aim is to have only one example program for
both backends and all examples.
  • Loading branch information
raph-amiard committed May 28, 2020
1 parent 0321d11 commit b8739ad
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 165 deletions.
135 changes: 135 additions & 0 deletions examples/asl_examples.ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
with Command; use Command;
with Utils; use Utils;
with Sound_Gen_Interfaces; use Sound_Gen_Interfaces;
with Effects; use Effects;
with Waves; use Waves;
with BLIT;

package ASL_Examples is

package Full_Demo is
BPM : constant := 120;

o : constant Sequencer_Note := No_Seq_Note;
K : constant Sequencer_Note := (Note => (G, 3), Duration => 3000);
Z : constant Sequencer_Note := (Note => (G, 3), Duration => 5000);
B : constant Sequencer_Note := (Note => (G, 3), Duration => 8000);

Kick_Source : constant Note_Generator_Access :=
Note_Generator_Access
(Create_Sequencer
(16, BPM, 2,
(K, o, o, K, o, o, K, o, o, o, B, o, o, o, o, o,
K, o, o, K, o, o, K, o, o, o, K, o, o, o, o, K)));

Kick : constant access Mixer :=
Create_Mixer
((
1 => (Create_Sine
(Create_Pitch_Gen
(0, Kick_Source, Proc => LFO (6.0, 200.0))),
0.1),

2 => (Create_Sine (Create_Pitch_Gen
(-24, Kick_Source,
Proc => new Attenuator'
(Level => 300.0,
Source => Create_ADSR (0, 50, 10000, 0.1, Kick_Source),
others => <>))),

0.7)
), Volume_Mod => Create_ADSR (10, 1000, 200, 0.2, Kick_Source));

Snare_Seq : constant access Simple_Sequencer :=
Create_Sequencer
(Nb_Steps => 16, BPM => BPM, Measures => 4,
Notes =>
(o, o, o, o, Z, o, o, o, o, o, o, o, K, o, o, o,
o, o, o, o, K, o, o, o, o, o, o, o, B, o, K, K,
o, o, o, o, Z, o, o, o, o, o, o, o, K, o, o, o,
o, o, o, o, K, o, o, K, o, o, Z, o, B, o, Z, o));

Snare_Source : constant Note_Generator_Access :=
Note_Generator_Access (Snare_Seq);
Snare : constant access Mixer :=
Create_Mixer
((
1 => (Create_Noise, 0.5),
2 => (Create_Sine
(Create_Pitch_Gen
(5, Snare_Source,
Proc =>
new Attenuator'
(Level => 300.0,
Source =>
Create_ADSR (0, 200, 10000, 0.5, Snare_Source),
others => <>))),
0.1)
), Volume_Mod => Create_ADSR (0, 100, 100, 0.2, Snare_Source));

Hat_Seq : constant access Simple_Sequencer :=
Create_Sequencer
(16, BPM,
Notes => (K, o, K, K, K, o, K, K, K, o, K, K, K, o, K, K));
Hat_Source : constant Note_Generator_Access :=
Note_Generator_Access (Hat_Seq);
Hat : constant access Mixer :=
Create_Mixer
((
1 => (Create_Noise, 0.5)
), Volume_Mod => Create_ADSR (0, 20, 0, 0.0, Hat_Source));

SNL : constant Sample_Period := 4000;
S1 : constant Sequencer_Note := ((C, 4), SNL);
S2 : constant Sequencer_Note := ((F, 4), SNL);
S3 : constant Sequencer_Note := ((D_Sh, 4), SNL);
S4 : constant Sequencer_Note := ((A_Sh, 4), SNL);
S5 : constant Sequencer_Note := ((G, 4), SNL);
S6 : constant Sequencer_Note := ((D_Sh, 4), SNL);

Synth_Seq : constant access Simple_Sequencer :=
Create_Sequencer
(8, BPM, 4,
(S1, S1, S1, S1, S1, S2, S2, S2,
S3, S3, S3, S3, S3, S4, S4, S4,
S1, S1, S1, S1, S1, S2, S2, S2,
S5, S5, S5, S5, S5, S6, S6, S6));

Synth_Source : constant Note_Generator_Access :=
Note_Generator_Access (Synth_Seq);

Synth : constant Generator_Access :=
Create_Dist
(Create_LP
(Create_Mixer
((
4 => (Create_Sine
(Create_Pitch_Gen
(-30, Synth_Source)), 0.6),
3 => (BLIT.Create_Saw
(Create_Pitch_Gen
(-24, Synth_Source)), 0.3),
2 => (BLIT.Create_Saw
(Create_Pitch_Gen
(-12, Synth_Source)), 0.3),
1 => (BLIT.Create_Saw
(Create_Pitch_Gen
(-17, Synth_Source)), 0.5))
),
Fixed (200.0,
Modulator => new Attenuator'
(Level => 1500.0,
Source => Create_ADSR (10, 150, 200, 0.005, Synth_Source),
others => <>)),
0.2), 1.00001, 1.5);

Main_Mixer : constant Generator_Access :=
Create_Mixer ((
(Kick, 0.5),
(Snare, 0.7),
(Hat, 0.6),
(Synth, 0.45)
));
end Full_Demo;

end ASL_Examples;
2 changes: 2 additions & 0 deletions examples/asl_examples_soundio.gpr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
with "../ada_synth_lib_soundio";
with "../ada-soundio/soundio.gpr";

with "gnatcoll";

project ASL_Examples_Soundio is

for Object_Dir use "obj";
Expand Down
133 changes: 0 additions & 133 deletions examples/simple_demo.ads

This file was deleted.

89 changes: 57 additions & 32 deletions examples/soundio_example.adb
Original file line number Diff line number Diff line change
@@ -1,41 +1,66 @@
with Soundio; use Soundio;
with Interfaces.C; use Interfaces.C;
with Ada.Text_IO; use Ada.Text_IO;
with Soundio_Output; use Soundio_Output;
with Poly_Demo;
with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C; use Interfaces.C;

with GNATCOLL.Opt_Parse; use GNATCOLL.Opt_Parse;

with Sound_Gen_Interfaces; use Sound_Gen_Interfaces;
with Soundio; use Soundio;
with Soundio_Output; use Soundio_Output;

with ASL_Examples;

procedure Soundio_Example is

package Arg is
Parser : Argument_Parser := Create_Argument_Parser
(Help => "Run an example for ada-synth-lib");

type Example_Kind is (Full_Demo);

package Example is new Parse_Enum_Option
(Parser,
"-e", "--example", "Which example to run",
Arg_Type => Example_Kind, Default_Val => Full_Demo);

function Generator return Generator_Access is
(case Arg.Example.Get is
when Full_Demo => ASL_Examples.Full_Demo.Main_Mixer);
end Arg;

IO : constant access Soundio.SoundIo := Create;
Default_Device_Index : int;
Device : access SoundIo_Device;
Out_Stream : access SoundIo_Out_Stream;
Err : SoundIo_Error;
pragma Unreferenced (Err);
Dummy : SoundIo_Error;

begin
-- SoundIO connection boilerplate
Err := Connect (IO);
Flush_Events (IO);
Default_Device_Index := Default_Output_Device_Index (IO);
Device := Get_Output_Device (IO, Default_Device_Index);
Out_Stream := Outstream_Create (Device);
Out_Stream.Format := Format_Float32NE;
Out_Stream.Write_Callback := Soundio_Output.Write_Callback'Access;

-- Set the main generator for the audio stream.
Set_Generator (Out_Stream => Out_Stream, G => Poly_Demo.Main_Mixer);
Err := Outstream_Open (Out_Stream);
Err := Outstream_Start (Out_Stream);

Put_Line ("Backend used : " & IO.current_backend'Img);

Play (Out_Stream);
loop
Wait_Events (IO);
end loop;

pragma Warnings (Off, "Unreachable");
Outstream_Destroy (Out_Stream);
Device_Unref (Device);
Destroy (IO);
if Arg.Parser.Parse then

-- SoundIO connection boilerplate
Dummy := Connect (IO);
Flush_Events (IO);
Default_Device_Index := Default_Output_Device_Index (IO);
Device := Get_Output_Device (IO, Default_Device_Index);
Out_Stream := Outstream_Create (Device);
Out_Stream.Format := Format_Float32NE;
Out_Stream.Write_Callback := Soundio_Output.Write_Callback'Access;

-- Set the main generator for the audio stream.
Set_Generator
(Out_Stream => Out_Stream, G => Arg.Generator);
Dummy := Outstream_Open (Out_Stream);
Dummy := Outstream_Start (Out_Stream);

Put_Line ("Backend used : " & IO.current_backend'Img);

Play (Out_Stream);
loop
Wait_Events (IO);
end loop;

pragma Warnings (Off, "Unreachable");
Outstream_Destroy (Out_Stream);
Device_Unref (Device);
Destroy (IO);
end if;
end Soundio_Example;

0 comments on commit b8739ad

Please sign in to comment.