1. with Events;                            use Events; 
  2. with Events.Corrals;                    use Events.Corrals; 
  3. with Events.Listeners;                  use Events.Listeners; 
  4. with Objects;                           use Objects; 
  5.  
  6. private with Ada.Real_Time; 
  7. private with Ada.Strings.Unbounded; 
  8.  
  9. package Audio_Players is 
  10.  
  11.     -- Audio_Player objects listen for certain audio events and play sound 
  12.     -- effects or music on command. The player contains an internal task for 
  13.     -- playing audio in the background. 
  14.     type Audio_Player is new Limited_Object and Event_Listener with private; 
  15.     type A_Audio_Player is access all Audio_Player'Class; 
  16.  
  17.     -- Creates a new Audio_Player that will listen for audio events in 'corral'. 
  18.     function Create_Audio_Player( corral : not null A_Corral ) return A_Audio_Player; 
  19.     pragma Postcondition( Create_Audio_Player'Result /= null ); 
  20.  
  21.     -- Starts the Audio_Player's internal task. This must be called before any 
  22.     -- audio can be played. 
  23.     procedure Start( this : not null access Audio_Player'Class ); 
  24.  
  25.     -- Stops the audio player's internals task. The destructor will 
  26.     -- automatically call Stop on deletion if the player hasn't already been 
  27.     -- stopped. 
  28.     procedure Stop( this : not null access Audio_Player'Class ); 
  29.  
  30.     -- Deletes the Audio_Player. 
  31.     procedure Delete( this : in out A_Audio_Player ); 
  32.     pragma Postcondition( this = null ); 
  33.  
  34. private 
  35.  
  36.     use Ada.Real_Time; 
  37.     use Ada.Strings.Unbounded; 
  38.  
  39.     -- A Sound_Registry is a protected type that contains the set of sounds 
  40.     -- currently being played. Starting and Stopping sounds/music is done 
  41.     -- through the registry. The Polling_Task polls all sounds in the registry 
  42.     -- to keep the audio stream full. 
  43.     type Sound_Registry; 
  44.     type A_Sound_Registry is access all Sound_Registry; 
  45.  
  46.     -- Deletes a Sound_Registry. 
  47.     procedure Delete( registry : in out A_Sound_Registry ); 
  48.     pragma Postcondition( registry = null ); 
  49.  
  50.     -- Continuously polls audio files in a Sound_Registry to keep audio 
  51.     -- streaming. 
  52.     type Polling_Task; 
  53.     type A_Polling_Task is access all Polling_Task; 
  54.  
  55.     -- Deletes a Polling_Task after it has stopped. 
  56.     procedure Delete( pollTask : in out A_Polling_Task ); 
  57.     pragma Postcondition( pollTask = null ); 
  58.  
  59.     ---------------------------------------------------------------------------- 
  60.  
  61.     type Audio_Player is new Limited_Object and Event_Listener with 
  62.         record 
  63.             started   : Boolean := False; 
  64.             stopped   : Boolean := False; 
  65.             corral    : A_Corral := null; 
  66.             tickDelta : Time_Span := Time_Span_Zero; 
  67.             registry  : A_Sound_Registry := null; 
  68.             process   : A_Polling_Task := null; 
  69.             music     : Unbounded_String; 
  70.         end record; 
  71.  
  72.     procedure Construct( this : access Audio_Player; corral : not null A_Corral ); 
  73.  
  74.     procedure Delete( this : in out Audio_Player ); 
  75.  
  76.     -- Handles Play_Music, Stop_Music, and Play_Sound events. 
  77.     procedure Handle_Event( this : access Audio_Player; 
  78.                             evt  : in out A_Event; 
  79.                             resp : out Response_Type ); 
  80.     pragma Precondition( evt /= null ); 
  81.  
  82.     -- Called when a Play_Music event is received to begin playing a music track. 
  83.     -- Only one music track can play at a time so this will stop any previously 
  84.     -- playing music. Music tracks will loop. 
  85.     procedure Play_Music( this : not null access Audio_Player'Class; name : String ); 
  86.     pragma Precondition( name'Length > 0 ); 
  87.  
  88.     -- Called when a Play_Sound event is received to begin playing a sound once 
  89.     -- through. Multiple sounds can play simultaneously. 
  90.     procedure Play_Sound( this : not null access Audio_Player'Class; name : String ); 
  91.     pragma Precondition( name'Length > 0 ); 
  92.  
  93.     -- Called when a Stop_Music event is received. Any currently looping music 
  94.     -- will be stopped. 
  95.     procedure Stop_Music( this : not null access Audio_Player'Class ); 
  96.  
  97. end Audio_Players;