audio_wavefiles_2.0.0_8e1162c5/src/audio-wavefiles-generic_fixed_wav_io.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
------------------------------------------------------------------------------
--                                                                          --
--          THIS IS AN AUTOMATICALLY GENERATED FILE! DO NOT EDIT!           --
--                                                                          --
--                               WAVEFILES                                  --
--                                                                          --
--                      Wavefile data I/O operations                        --
--                                                                          --
--  The MIT License (MIT)                                                   --
--                                                                          --
--  Copyright (c) 2015 -- 2021 Gustavo A. Hoffmann                          --
--                                                                          --
--  Permission is hereby granted, free of charge, to any person obtaining   --
--  a copy of this software and associated documentation files (the         --
--  "Software"), to deal in the Software without restriction, including     --
--  without limitation the rights to use, copy, modify, merge, publish,     --
--  distribute, sublicense, and / or sell copies of the Software, and to    --
--  permit persons to whom the Software is furnished to do so, subject to   --
--  the following conditions:                                               --
--                                                                          --
--  The above copyright notice and this permission notice shall be          --
--  included in all copies or substantial portions of the Software.         --
--                                                                          --
--  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,         --
--  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF      --
--  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  --
--  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY    --
--  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,    --
--  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE       --
--  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                  --
------------------------------------------------------------------------------

package body Audio.Wavefiles.Generic_Fixed_Wav_IO is

   procedure Read_Wav_Sample_Bytes
     (File_Access :     Ada.Streams.Stream_IO.Stream_Access;
      Sample      : out Wav_Sample)
     with Inline;
   procedure Write_Wav_Sample_Bytes
     (File_Access :    Ada.Streams.Stream_IO.Stream_Access;
      Sample      :    Wav_Sample)
     with Inline;
   procedure Read_Wav_MC_Sample (WF  : in out Wavefile;
                                 Wav :    out Wav_MC_Sample)
     with Inline;
   procedure Write_Wav_MC_Sample (WF  : in out Wavefile;
                                  Wav :        Wav_MC_Sample)
     with Inline;

   ---------------------------
   -- Read_Wav_Sample_Bytes --
   ---------------------------

   procedure Read_Wav_Sample_Bytes
     (File_Access :     Ada.Streams.Stream_IO.Stream_Access;
      Sample      : out Wav_Sample)
   is
      Bytes : Byte_Array (1 .. Sample'Size / 8)
        with Address => Sample'Address, Import, Volatile;

      Last_Valid_Byte : constant Long_Integer := Wav_Sample'Size / 8;

      use type Byte;
   begin
      Byte_Array'Read (File_Access,
                       Bytes (1 .. Wav_Sample'Size / 8));

      --  Account for sign bit in internal representation,
      --  which might not match the wavefile representation.
      if Sample'Size > Wav_Sample'Size then
         Bytes (Last_Valid_Byte + 1 .. Bytes'Last) :=
           (others => (if Bytes (Last_Valid_Byte) >= 16#80#
                       then 16#FF# else 16#00#));
      end if;
   end Read_Wav_Sample_Bytes;

   ----------------------------
   -- Write_Wav_Sample_Bytes --
   ----------------------------

   procedure Write_Wav_Sample_Bytes
     (File_Access :    Ada.Streams.Stream_IO.Stream_Access;
      Sample      :    Wav_Sample)
   is
      Bytes : Byte_Array (1 .. Wav_Sample'Size / 8)
        with Address => Sample'Address, Import, Volatile;
   begin
      Byte_Array'Write (File_Access, Bytes);
   end Write_Wav_Sample_Bytes;

   ------------------------
   -- Read_Wav_MC_Sample --
   ------------------------

   procedure Read_Wav_MC_Sample
     (WF  : in out Wavefile;
      Wav :    out Wav_MC_Sample)
   is
      N_Ch   : constant Positive := Number_Of_Channels (WF);
      Sample : Wav_Sample;

      use Ada.Streams.Stream_IO;

      Prev_File_Index  : constant Positive_Count := Index (WF.File)
        with Ghost;
      Expected_Byte_IO : constant Positive_Count
        := Positive_Count
          (To_Positive (WF.Wave_Format.Bits_Per_Sample) * N_Ch / 8)
        with Ghost;
   begin
      for J in Wav'Range loop

         --  Patch for 24-bit wavefiles
         if Wav_Sample'Size = 24 then
            Read_Wav_Sample_Bytes (WF.File_Access, Sample);
         else
            Wav_Sample'Read (WF.File_Access, Sample);
         end if;

         Wav (J) := Sample;
         if Ada.Streams.Stream_IO.End_Of_File (WF.File) and then
           J < Wav'Last
         then
            --  Cannot read data for all channels
            WF.Set_Error (Wavefile_Error_File_Too_Short);
         end if;
      end loop;

      WF.Sample_Pos.Current := WF.Sample_Pos.Current + 1;

      pragma Assert (Ada.Streams.Stream_IO.Index (WF.File) =
                       Prev_File_Index + Expected_Byte_IO);
   end Read_Wav_MC_Sample;

   -------------------------
   -- Write_Wav_MC_Sample --
   -------------------------

   procedure Write_Wav_MC_Sample
     (WF  : in out Wavefile;
      Wav :        Wav_MC_Sample)
   is
      N_Ch : constant Positive := Number_Of_Channels (WF);

      use Ada.Streams.Stream_IO;

      Prev_File_Index  : constant Positive_Count := Index (WF.File)
        with Ghost;
      Expected_Byte_IO : constant Positive_Count
        := Positive_Count
          (To_Positive (WF.Wave_Format.Bits_Per_Sample) * N_Ch / 8)
        with Ghost;
   begin
      if Wav_Sample'Size = 24 then
         for Sample of Wav loop
            Write_Wav_Sample_Bytes (WF.File_Access, Sample);
         end loop;
      else
         Wav_MC_Sample'Write (WF.File_Access, Wav);
      end if;

      WF.Sample_Pos := (Total   => WF.Sample_Pos.Total   + 1,
                        Current => WF.Sample_Pos.Current + 1);

      pragma Assert (Ada.Streams.Stream_IO.Index (WF.File) =
                       Prev_File_Index + Expected_Byte_IO);
   end Write_Wav_MC_Sample;

   ---------
   -- Get --
   ---------

   function Get (WF  : in out Wavefile) return Wav_MC_Sample
   is
      N_Ch   : constant Positive := Number_Of_Channels (WF);

      Channel_Range_Valid_Last : constant Channel_Range :=
        Channel_Range'Val (N_Ch - 1
                           + Channel_Range'Pos (Channel_Range'First));

      subtype Valid_Channel_Range is Channel_Range range
        Channel_Range'First .. Channel_Range_Valid_Last;
   begin
      return Wav : Wav_MC_Sample (Valid_Channel_Range) do
         Read_Wav_MC_Sample (WF, Wav);
      end return;
   end Get;

   ---------
   -- Get --
   ---------

   procedure Get (WF  : in out Wavefile;
                  Wav :    out Wav_MC_Sample) is
   begin
      Read_Wav_MC_Sample (WF, Wav);
   end Get;

   ---------
   -- Put --
   ---------

   procedure Put (WF  : in out Wavefile;
                  Wav :        Wav_MC_Sample) is
   begin
      Write_Wav_MC_Sample (WF, Wav);
   end Put;

end Audio.Wavefiles.Generic_Fixed_Wav_IO;