From ed02634895209b9a937297838c1ae04f072f9c79 Mon Sep 17 00:00:00 2001
From: Jedidiah Barber <contact@jedbarber.id.au>
Date: Mon, 9 Oct 2023 12:14:14 +1300
Subject: Figured out and fixed up array aliasing issue

---
 example/sine_block.adb    |  5 ++--
 src/portaudio-streams.adb | 36 ++++++++++++++---------------
 src/portaudio-streams.ads | 58 +++++++++++++++++++++++------------------------
 3 files changed, 49 insertions(+), 50 deletions(-)

diff --git a/example/sine_block.adb b/example/sine_block.adb
index b28b7cd..538d1d9 100644
--- a/example/sine_block.adb
+++ b/example/sine_block.adb
@@ -27,8 +27,9 @@ procedure Sine_Block is
     Channels    : constant Natural           := 2;
     Per_Buffer  : constant Pstm.Frame_Amount := 1024;
 
-    Sample_Array : aliased Pstm.Float_32_Array (1 .. Natural (Per_Buffer) * Channels) :=
-        (others => 0.0);
+    --  Note how this array specifies the bounds in the initial value rather than the type.
+    --  This is important because otherwise the aliasing would cause subtype mismatch errors.
+    Sample_Array : aliased Pstm.Float_32_Array := (1 .. Natural (Per_Buffer) * Channels => 0.0);
     Sample_Buffer : Pstm.Buffer := Pstm.Wrap (Sample_Array, Per_Buffer, Channels);
 
     type Table_Index is mod 200;
diff --git a/src/portaudio-streams.adb b/src/portaudio-streams.adb
index 6b05008..18a091e 100644
--- a/src/portaudio-streams.adb
+++ b/src/portaudio-streams.adb
@@ -485,9 +485,9 @@ package body Portaudio.Streams is
     end Put;
 
     function Wrap
-           (Store    : in Float_32_Array;
-            Frames   : in Frame_Amount;
-            Channels : in Natural)
+           (Store    : aliased in Float_32_Array;
+            Frames   :         in Frame_Amount;
+            Channels :         in Natural)
         return Buffer is
     begin
         return
@@ -498,9 +498,9 @@ package body Portaudio.Streams is
     end Wrap;
 
     function Wrap
-           (Store    : in Int_32_Array;
-            Frames   : in Frame_Amount;
-            Channels : in Natural)
+           (Store    : aliased in Int_32_Array;
+            Frames   :         in Frame_Amount;
+            Channels :         in Natural)
         return Buffer is
     begin
         return
@@ -511,9 +511,9 @@ package body Portaudio.Streams is
     end Wrap;
 
     function Wrap
-           (Store    : in Int_24_Array;
-            Frames   : in Frame_Amount;
-            Channels : in Natural)
+           (Store    : aliased in Int_24_Array;
+            Frames   :         in Frame_Amount;
+            Channels :         in Natural)
         return Buffer is
     begin
         return
@@ -524,9 +524,9 @@ package body Portaudio.Streams is
     end Wrap;
 
     function Wrap
-           (Store    : in Int_16_Array;
-            Frames   : in Frame_Amount;
-            Channels : in Natural)
+           (Store    : aliased in Int_16_Array;
+            Frames   :         in Frame_Amount;
+            Channels :         in Natural)
         return Buffer is
     begin
         return
@@ -537,9 +537,9 @@ package body Portaudio.Streams is
     end Wrap;
 
     function Wrap
-           (Store    : in Int_8_Array;
-            Frames   : in Frame_Amount;
-            Channels : in Natural)
+           (Store    : aliased in Int_8_Array;
+            Frames   :         in Frame_Amount;
+            Channels :         in Natural)
         return Buffer is
     begin
         return
@@ -550,9 +550,9 @@ package body Portaudio.Streams is
     end Wrap;
 
     function Wrap
-           (Store    : in UInt_8_Array;
-            Frames   : in Frame_Amount;
-            Channels : in Natural)
+           (Store    : aliased in UInt_8_Array;
+            Frames   :         in Frame_Amount;
+            Channels :         in Natural)
         return Buffer is
     begin
         return
diff --git a/src/portaudio-streams.ads b/src/portaudio-streams.ads
index 1f14733..c8d3e92 100644
--- a/src/portaudio-streams.ads
+++ b/src/portaudio-streams.ads
@@ -37,12 +37,16 @@ package Portaudio.Streams is
     type Integer_24 is range -2 ** 23 .. 2 ** 23 - 1;
     for Integer_24'Size use 24;
 
-    type Float_32_Array is array (Positive range <>) of aliased Interfaces.IEEE_Float_32;
-    type Int_32_Array   is array (Positive range <>) of aliased Interfaces.Integer_32;
-    type Int_24_Array   is array (Positive range <>) of aliased Integer_24;
-    type Int_16_Array   is array (Positive range <>) of aliased Interfaces.Integer_16;
-    type Int_8_Array    is array (Positive range <>) of aliased Interfaces.Integer_8;
-    type UInt_8_Array   is array (Positive range <>) of aliased Interfaces.Unsigned_8;
+    --  When declaring these arrays, ensure that the bounds are specified by the
+    --  initial value and not the subtype. Otherwise you will get subtype mismatch
+    --  errors due to the aliasing required by the Wrap functions further down.
+    --  No, I'm not a fan of this quirk of Ada either.
+    type Float_32_Array is array (Positive range <>) of Interfaces.IEEE_Float_32;
+    type Int_32_Array   is array (Positive range <>) of Interfaces.Integer_32;
+    type Int_24_Array   is array (Positive range <>) of Integer_24;
+    type Int_16_Array   is array (Positive range <>) of Interfaces.Integer_16;
+    type Int_8_Array    is array (Positive range <>) of Interfaces.Integer_8;
+    type UInt_8_Array   is array (Positive range <>) of Interfaces.Unsigned_8;
 
 
     type Frame_Amount is new Interfaces.Unsigned_32;
@@ -152,51 +156,45 @@ package Portaudio.Streams is
             Value   : in Interfaces.Unsigned_8)
     with Pre => Store.Kind = UInt_8_Format;
 
-    --  !!!  ENSURE STORE IS ALIASED  !!!  --
     function Wrap
-           (Store    : in Float_32_Array;
-            Frames   : in Frame_Amount;
-            Channels : in Natural)
+           (Store    : aliased in Float_32_Array;
+            Frames   :         in Frame_Amount;
+            Channels :         in Natural)
         return Buffer
     with Pre => Store'Length > 0 and Store'Length = Frames * Frame_Amount (Channels);
 
-    --  !!!  ENSURE STORE IS ALIASED  !!!  --
     function Wrap
-           (Store    : in Int_32_Array;
-            Frames   : in Frame_Amount;
-            Channels : in Natural)
+           (Store    : aliased in Int_32_Array;
+            Frames   :         in Frame_Amount;
+            Channels :         in Natural)
         return Buffer
     with Pre => Store'Length > 0 and Store'Length = Frames * Frame_Amount (Channels);
 
-    --  !!!  ENSURE STORE IS ALIASED  !!!  --
     function Wrap
-           (Store    : in Int_24_Array;
-            Frames   : in Frame_Amount;
-            Channels : in Natural)
+           (Store    : aliased in Int_24_Array;
+            Frames   :         in Frame_Amount;
+            Channels :         in Natural)
         return Buffer
     with Pre => Store'Length > 0 and Store'Length = Frames * Frame_Amount (Channels);
 
-    --  !!!  ENSURE STORE IS ALIASED  !!!  --
     function Wrap
-           (Store    : in Int_16_Array;
-            Frames   : in Frame_Amount;
-            Channels : in Natural)
+           (Store    : aliased in Int_16_Array;
+            Frames   :         in Frame_Amount;
+            Channels :         in Natural)
         return Buffer
     with Pre => Store'Length > 0 and Store'Length = Frames * Frame_Amount (Channels);
 
-    --  !!!  ENSURE STORE IS ALIASED  !!!  --
     function Wrap
-           (Store    : in Int_8_Array;
-            Frames   : in Frame_Amount;
-            Channels : in Natural)
+           (Store    : aliased in Int_8_Array;
+            Frames   :         in Frame_Amount;
+            Channels :         in Natural)
         return Buffer
     with Pre => Store'Length > 0 and Store'Length = Frames * Frame_Amount (Channels);
 
-    --  !!!  ENSURE STORE IS ALIASED  !!!  --
     function Wrap
-           (Store    : in UInt_8_Array;
-            Frames   : in Frame_Amount;
-            Channels : in Natural)
+           (Store    : aliased in UInt_8_Array;
+            Frames   :         in Frame_Amount;
+            Channels :         in Natural)
         return Buffer
     with Pre => Store'Length > 0 and Store'Length = Frames * Frame_Amount (Channels);
 
-- 
cgit