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