From 049d2a9a337331295b4a2d4ad13061bc73536236 Mon Sep 17 00:00:00 2001 From: Jedidiah Barber Date: Sun, 2 Jul 2023 21:36:34 +1200 Subject: Initial commit --- src/libsndfile.ads | 452 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 452 insertions(+) create mode 100644 src/libsndfile.ads (limited to 'src/libsndfile.ads') diff --git a/src/libsndfile.ads b/src/libsndfile.ads new file mode 100644 index 0000000..67e355f --- /dev/null +++ b/src/libsndfile.ads @@ -0,0 +1,452 @@ + + +-- Programmed by Jedidiah Barber +-- Released into the public domain + + +private with + + Interfaces.C, + System; + + +package Libsndfile is + + + --------------------------------- + -- Data Types and Structures -- + --------------------------------- + + type Count_Type is new Long_Long_Integer; + + type Sound_File is tagged limited private; + + type File_Mode is (Read_Only, Write_Only, Read_Write); + + type Seek_From is (From_Start, From_Current, From_End); + + type Short_Data is array (Positive range <>) of Short_Integer; + type Integer_Data is array (Positive range <>) of Integer; + type Float_Data is array (Positive range <>) of Float; + type Double_Data is array (Positive range <>) of Long_Float; + + type Raw_Data is new String; + + type Metadata is + (Title_String, + Copyright_String, + Software_String, + Artist_String, + Comment_String, + Date_String, + Album_String, + License_String, + Track_Number_String, + Genre_String); + + type File_Info is private; + + type Major_Format is + (Format_Unknown, + Wav_Format, + Aiff_Format, + Au_Format, + Raw_Format, + Paf_Format, + Svx_Format, + Nist_Format, + Voc_Format, + Ircam_Format, + W64_Format, + Mat4_Format, + Mat5_Format, + Pvf_Format, + Xi_Format, + Htk_Format, + Sds_Format, + Avr_Format, + Wavex_Format, + Sd2_Format, + Flac_Format, + Caf_Format, + Wve_Format, + Ogg_Format, + Mpc2k_Format, + Rf64_Format, + Mpeg_Format); + + type Minor_Format is + (Encoding_Unknown, + Pcm_S8_Encoding, + Pcm_16_Encoding, + Pcm_24_Encoding, + Pcm_32_Encoding, + Pcm_U8_Encoding, + Float_Encoding, + Double_Encoding, + Ulaw_Encoding, + Alaw_Encoding, + Ima_Adpcm_Encoding, + Ms_Adpcm_Encoding, + Gsm610_Encoding, + Vox_Adpcm_Encoding, + Nms_Adpcm_16_Encoding, + Nms_Adpcm_24_Encoding, + Nms_Adpcm_32_Encoding, + G721_32_Encoding, + G723_24_Encoding, + G723_40_Encoding, + Dwvw_12_Encoding, + Dwvw_16_Encoding, + Dwvw_24_Encoding, + Dwvw_N_Encoding, + Dpcm_8_Encoding, + Dpcm_16_Encoding, + Vorbis_Encoding, + Opus_Encoding, + Alac_16_Encoding, + Alac_20_Encoding, + Alac_24_Encoding, + Alac_32_Encoding, + Mpeg_Layer_I_Encoding, + Mpeg_Layer_II_Encoding, + Mpeg_Layer_III_Encoding); + + type Endianness is + (Default_Endian, + Little_Endian, + Big_Endian, + Machine_Native); + + -- Use a copy of this for reading non-raw files + Blank_Info : constant File_Info; + + -- Use this for reading raw files and writing files + function Create + (Rate : in Positive; + Channels : in Positive; + Major : in Major_Format; + Minor : in Minor_Format; + Endian : in Endianness := Default_Endian) + return File_Info; + + function Frames + (Info : in File_Info) + return Count_Type; + + function Rate + (Info : in File_Info) + return Natural; + + function Channels + (Info : in File_Info) + return Natural; + + function Major + (Info : in File_Info) + return Major_Format; + + function Minor + (Info : in File_Info) + return Minor_Format; + + function Endian + (Info : in File_Info) + return Endianness; + + function Sections + (Info : in File_Info) + return Natural; + + function Seekable + (Info : in File_Info) + return Boolean; + + + + + ------------------ + -- Exceptions -- + ------------------ + + -- May be raised by Open + Unrecognised_Format_Error : exception; + + -- May be raised by Open + Unsupported_Encoding_Error : exception; + + -- May be raised by Open + Malformed_File_Error : exception; + + -- May be raised by Open, Close? + System_Error : exception; + + -- May be raised by Seek + Seek_Error : exception; + + -- May be raised by Current_Byterate + Unknown_Byterate_Error : exception; + + -- May be raised by Open, Close + General_Failure : exception; + + -- Program_Error may be raised if libsndfile in general does something out of spec + -- Set_Meta may raise errors but it is unclear which ones + + + + + --------------- + -- Utility -- + --------------- + + function Is_Open + (File : in Sound_File) + return Boolean; + + + + + --------------------- + -- API Interface -- + --------------------- + + procedure Open + (File : in out Sound_File; + Name : in String; + Mode : in File_Mode; + Info : in out File_Info) + with Pre => not Is_Open (File), + Post => Is_Open (File); + + function Format_Check + (Info : in File_Info) + return Boolean; + + function Seek + (File : in Sound_File; + Offset : in Count_Type; + Whence : in Seek_From; + Mode : in File_Mode := Read_Write) + return Count_Type + with Pre => Is_Open (File); + + procedure Close + (File : in out Sound_File) + with Pre => Is_Open (File), + Post => not Is_Open (File); + + procedure Write_Sync + (File : in Sound_File) + with Pre => Is_Open (File); + + function Read_Short + (File : in Sound_File; + Data : out Short_Data; + Frames : in Count_Type) + return Count_Type + with Pre => Is_Open (File); + + function Read_Integer + (File : in Sound_File; + Data : out Integer_Data; + Frames : in Count_Type) + return Count_Type + with Pre => Is_Open (File); + + function Read_Float + (File : in Sound_File; + Data : out Float_Data; + Frames : in Count_Type) + return Count_Type + with Pre => Is_Open (File); + + function Read_Double + (File : in Sound_File; + Data : out Double_Data; + Frames : in Count_Type) + return Count_Type + with Pre => Is_Open (File); + + function Write_Short + (File : in Sound_File; + Data : in Short_Data; + Frames : in Count_Type) + return Count_Type + with Pre => Is_Open (File); + + function Write_Integer + (File : in Sound_File; + Data : in Integer_Data; + Frames : in Count_Type) + return Count_Type + with Pre => Is_Open (File); + + function Write_Float + (File : in Sound_File; + Data : in Float_Data; + Frames : in Count_Type) + return Count_Type + with Pre => Is_Open (File); + + function Write_Double + (File : in Sound_File; + Data : in Double_Data; + Frames : in Count_Type) + return Count_Type + with Pre => Is_Open (File); + + function Read_Raw + (File : in Sound_File; + Data : out Raw_Data; + Bytes : in Count_Type) + return Count_Type + with Pre => Is_Open (File); + + function Write_Raw + (File : in Sound_File; + Data : in Raw_Data; + Bytes : in Count_Type) + return Count_Type + with Pre => Is_Open (File); + + function Get_Meta + (File : in Sound_File; + Kind : in Metadata) + return String + with Pre => Is_Open (File); + + procedure Set_Meta + (File : in Sound_File; + Kind : in Metadata; + Value : in String) + with Pre => Is_Open (File); + + function Version_String + return String; + + function Current_Byterate + (File : in Sound_File) + return Natural + with Pre => Is_Open (File); + + -- RIFF chunk API goes here + + +private + + + pragma Linker_Options ("-lsndfile"); + + pragma Inline (Is_Open); + pragma Inline (Write_Sync); + pragma Inline (Read_Raw); + pragma Inline (Write_Raw); + pragma Inline (Version_String); + + + + + type Sound_File is tagged limited record + Ptr : System.Address := System.Null_Address; + FMode : File_Mode := Read_Only; + Chans : Interfaces.C.int := 0; + end record; + + + type C_Short_Data is array (Positive range <>) of Interfaces.C.short; + type C_Integer_Data is array (Positive range <>) of Interfaces.C.int; + type C_Float_Data is array (Positive range <>) of Interfaces.C.C_float; + type C_Double_Data is array (Positive range <>) of Interfaces.C.double; + + + -- This cannot correspond to the C-side SF_INFO since sf_count_t can vary + type File_Info is record + My_Frames : Interfaces.Integer_64; + My_Sample_Rate : Interfaces.C.int; + My_Channels : Interfaces.C.int; + My_Major : Interfaces.C.int; + My_Minor : Interfaces.C.int; + My_Endian : Interfaces.C.int; + My_Sections : Interfaces.C.int; + My_Seekable : Interfaces.C.int; + end record with Convention => C; + + + Blank_Info : constant File_Info := (My_Frames => 0, others => 0); + + + + + procedure Raise_Error + (Num : in Interfaces.C.int); + + function To_Major + (Num : in Interfaces.C.int) + return Major_Format; + + function To_Minor + (Num : in Interfaces.C.int) + return Minor_Format; + + function To_Endian + (Num : in Interfaces.C.int) + return Endianness; + + function To_Cint + (Major : in Major_Format) + return Interfaces.C.int; + + function To_Cint + (Minor : in Minor_Format) + return Interfaces.C.int; + + function To_Cint + (Endian : in Endianness) + return Interfaces.C.int; + + + + + sf_false : constant Interfaces.C.int; + pragma Import (C, sf_false, "sf_false"); + + sf_true : constant Interfaces.C.int; + pragma Import (C, sf_true, "sf_true"); + + + + + sfm_read : constant Interfaces.C.int; + pragma Import (C, sfm_read, "sfm_read"); + + sfm_write : constant Interfaces.C.int; + pragma Import (C, sfm_write, "sfm_write"); + + sfm_rdwr : constant Interfaces.C.int; + pragma Import (C, sfm_rdwr, "sfm_rdwr"); + + + + + sf_seek_set : constant Interfaces.C.int; + pragma Import (C, sf_seek_set, "sf_seek_set"); + + sf_seek_cur : constant Interfaces.C.int; + pragma Import (C, sf_seek_cur, "sf_seek_cur"); + + sf_seek_end : constant Interfaces.C.int; + pragma Import (C, sf_seek_end, "sf_seek_end"); + + + + + function sf_error + (File : in System.Address) + return Interfaces.C.int; + pragma Import (C, sf_error, "sf_error"); + + +end Libsndfile; + + -- cgit