From 543cd19ab514ec632d965acd5177c5bf6695520f Mon Sep 17 00:00:00 2001
From: Jedidiah Barber <contact@jedbarber.id.au>
Date: Sat, 15 Jul 2023 20:18:26 +1200
Subject: Initial commit

---
 example/device_list.adb | 202 ++++++++++++++++++++++++++++++++++++++++++++++++
 example/saw_back.adb    |  80 +++++++++++++++++++
 2 files changed, 282 insertions(+)
 create mode 100644 example/device_list.adb
 create mode 100644 example/saw_back.adb

(limited to 'example')

diff --git a/example/device_list.adb b/example/device_list.adb
new file mode 100644
index 0000000..f9f6e65
--- /dev/null
+++ b/example/device_list.adb
@@ -0,0 +1,202 @@
+
+
+--  Programmed by Jedidiah Barber
+--  Released into the public domain
+
+
+with
+
+    Ada.Characters.Latin_1,
+    Ada.Text_IO,
+    Portaudio;
+
+use type
+
+    Portaudio.Device_Index,
+    Portaudio.Time;
+
+
+procedure Device_List is
+
+
+    package Latin renames Ada.Characters.Latin_1;
+    package TIO   renames Ada.Text_IO;
+    package Paud  renames Portaudio;
+
+
+    function Image
+           (Num : in Integer)
+        return String
+    is
+        Test_Out : String := Integer'Image (Num);
+    begin
+        if Test_Out (Test_Out'First) = ' ' then
+            return Test_Out (Test_Out'First + 1 .. Test_Out'Last);
+        else
+            return Test_Out;
+        end if;
+    end Image;
+
+
+    function Image
+           (Num : in Paud.Time)
+        return String is
+    begin
+        if Num < 0.0 then
+            return "N/A";
+        else
+            return Paud.Image (Num * 1000.0) & "ms";
+        end if;
+    end Image;
+
+
+    function Image
+           (Num : in Paud.Hertz)
+        return String is
+    begin
+        return Paud.Hertz_Image (Num / 1000.0) & "kHz";
+    end Image;
+
+
+    procedure Put_Supported_Standard_Sample_Rates
+           (In_Params  : access Paud.Stream_Parameters;
+            Out_Params : access Paud.Stream_Parameters)
+    is
+        Standard_Sample_Rates : array (Positive range <>) of Paud.Hertz :=
+            (8000.0,  9600.0, 11025.0, 12000.0,  16000.0, 22050.0, 24000.0,
+            32000.0, 44100.0, 48000.0, 88200.0, 96000.0, 192000.0);
+        Put_Counter : Natural := 0;
+    begin
+        for Rate of Standard_Sample_Rates loop
+            if Paud.Is_Format_Supported (In_Params, Out_Params, Rate) then
+                case Put_Counter is
+                    when 0 =>
+                        TIO.Put (Latin.HT & Image (Rate));
+                        Put_Counter := 1;
+                    when 4 =>
+                        TIO.Put (Latin.LF & Latin.HT & Image (Rate));
+                        Put_Counter := 1;
+                    when others =>
+                        TIO.Put (", " & Image (Rate));
+                        Put_Counter := Put_Counter + 1;
+                end case;
+            end if;
+        end loop;
+        if Put_Counter = 0 then
+            TIO.Put_Line ("None");
+        else
+            TIO.New_Line;
+        end if;
+    end Put_Supported_Standard_Sample_Rates;
+
+
+    Num_Devices : Natural;
+
+    Current_Device    : Paud.Device_Info;
+    Current_Host_API  : Paud.Host_API_Info;
+    Displayed_Default : Boolean;
+
+    Input_Params  : aliased Paud.Stream_Parameters;
+    Output_Params : aliased Paud.Stream_Parameters;
+
+
+begin
+
+
+    TIO.Put_Line ("PortAudio version: " & Paud.Image (Paud.Get_Version));
+    TIO.Put_Line ("Version text: " & Paud.Get_Version_Info.Text);
+
+    Num_Devices := Paud.Get_Device_Count;
+    TIO.Put_Line ("Number of devices = " & Image (Num_Devices));
+
+    for Index in Paud.Device_Index (1) .. Paud.Device_Index (Num_Devices) loop
+        Current_Device    := Paud.Get_Device_Info (Index);
+        Current_Host_API  := Paud.Get_Host_API_Info (Current_Device.Host_API);
+        Displayed_Default := False;
+
+        TIO.New_Line;
+        TIO.Put_Line ("--------------------------------------- device #" &
+            Image (Integer (Index)));
+
+        if Index = Paud.Get_Default_Input_Device then
+            TIO.Put ("[ Default Input");
+            Displayed_Default := True;
+        elsif Index = Current_Host_API.Default_Input_Device then
+            TIO.Put ("[ Default " & Current_Host_API.Name & " Input");
+            Displayed_Default := True;
+        end if;
+
+        if Index = Paud.Get_Default_Output_Device then
+            if Displayed_Default then
+                TIO.Put (",");
+            else
+                TIO.Put ("[");
+            end if;
+            TIO.Put (" Default Output");
+            Displayed_Default := True;
+        elsif Index = Current_Host_API.Default_Output_Device then
+            if Displayed_Default then
+                TIO.Put (",");
+            else
+                TIO.Put ("[");
+            end if;
+            TIO.Put (" Default " & Current_Host_API.Name & " Output");
+            Displayed_Default := True;
+        end if;
+
+        if Displayed_Default then
+            TIO.Put_Line (" ]");
+        end if;
+
+        TIO.Put_Line ("Name                        = " & Current_Device.Name);
+        TIO.Put_Line ("Host API                    = " & Current_Host_API.Name);
+        TIO.Put_Line ("Max inputs = " & Image (Current_Device.Max_Input_Channels) &
+            ", Max outputs = " & Image (Current_Device.Max_Output_Channels));
+
+        TIO.Put_Line ("Default low input latency   = " & Image
+            (Current_Device.Default_Low_Input_Latency));
+        TIO.Put_Line ("Default low output latency  = " & Image
+            (Current_Device.Default_Low_Output_Latency));
+        TIO.Put_Line ("Default high input latency  = " & Image
+            (Current_Device.Default_High_Input_Latency));
+        TIO.Put_Line ("Default high output latency = " & Image
+            (Current_Device.Default_High_Output_Latency));
+
+        TIO.Put_Line ("Default sample rate         = " & Image
+            (Current_Device.Default_Sample_Rate));
+
+        Input_Params := Paud.Create
+            (Index, Current_Device.Max_Input_Channels, Paud.Int_16_Sample, 0.0);
+        Output_Params := Paud.Create
+            (Index, Current_Device.Max_Output_Channels, Paud.Int_16_Sample, 0.0);
+
+        if Current_Device.Max_Input_Channels > 0 then
+            TIO.Put_Line ("Supported standard sample rates");
+            TIO.Put_Line (" for half-duplex 16 bit " &
+                Image (Current_Device.Max_Input_Channels) & " channel input =");
+            Put_Supported_Standard_Sample_Rates (Input_Params'Access, null);
+        end if;
+
+        if Current_Device.Max_Output_Channels > 0 then
+            TIO.Put_Line ("Supported standard sample rates");
+            TIO.Put_Line (" for half-duplex 16 bit " &
+                Image (Current_Device.Max_Output_Channels) & " channel output =");
+            Put_Supported_Standard_Sample_Rates (null, Output_Params'Access);
+        end if;
+
+        if Current_Device.Max_Input_Channels > 0 and Current_Device.Max_Output_Channels > 0 then
+            TIO.Put_Line ("Supported standard sample rates");
+            TIO.Put_Line (" for full-duplex 16 bit " &
+                Image (Current_Device.Max_Input_Channels) & " channel input, " &
+                Image (Current_Device.Max_Output_Channels) & " channel output =");
+            Put_Supported_Standard_Sample_Rates (Input_Params'Access, Output_Params'Access);
+        end if;
+    end loop;
+
+    TIO.New_Line;
+    TIO.Put_Line ("----------------------------------------------");
+
+
+end Device_List;
+
+
diff --git a/example/saw_back.adb b/example/saw_back.adb
new file mode 100644
index 0000000..c266964
--- /dev/null
+++ b/example/saw_back.adb
@@ -0,0 +1,80 @@
+
+
+--  Programmed by Jedidiah Barber
+--  Released into the public domain
+
+
+with
+
+    Ada.Text_IO,
+    Portaudio;
+
+use type
+
+    Portaudio.Float_32;
+
+
+procedure Saw_Back is
+
+
+    Left_Phase, Right_Phase : Portaudio.Float_32 := 0.0;
+
+
+    function Saw_Callback
+           (Input  : in Portaudio.Sample_Buffer;
+            Output : in Portaudio.Sample_Buffer;
+            Frames : in Portaudio.Frame_Amount;
+            Timing : in Portaudio.Callback_Time_Info;
+            Flags  : in Portaudio.Callback_Flags)
+        return Portaudio.Callback_Result is
+    begin
+
+        for Frame in 1 .. Frames loop
+            Output.Put (Frame, 1, Left_Phase);
+            Output.Put (Frame, 2, Right_Phase);
+
+            Left_Phase := Left_Phase + 0.01;
+            if Left_Phase >= 1.0 then
+                Left_Phase := -1.0;
+            end if;
+
+            Right_Phase := Right_Phase + 0.03;
+            if Right_Phase >= 1.0 then
+                Right_Phase := -1.0;
+            end if;
+        end loop;
+
+        return Portaudio.Continue;
+
+    end Saw_Callback;
+
+
+    Saw_Stream : Portaudio.Audio_Stream;
+
+
+begin
+
+
+    Ada.Text_IO.Put_Line ("PortAudio Test: output sawtooth wave.");
+
+    Saw_Stream.Open_Default
+       (Input_Channels  => 0,
+        Output_Channels => 2,
+        Format          => Portaudio.Float_32_Sample,
+        Sample_Rate     => 44100.0,
+        Buffer_Frames   => 256,
+        Callback        => Saw_Callback'Unrestricted_Access);
+
+    Saw_Stream.Start;
+
+    delay 4.0;
+
+    Saw_Stream.Stop;
+    Saw_Stream.Close;
+
+    Ada.Text_IO.Put_Line ("Test finished.");
+
+
+end Saw_Back;
+
+
-- 
cgit