hpsound.h

Go to the documentation of this file.
00001 
00003 
00004 //
00005 // Copyright (c) 1999-2004 California Institute of Technology
00006 // Copyright (c) 2004-2007 University of Southern California
00007 // Rob Peters <rjpeters at usc dot edu>
00008 //
00009 // created: Tue Oct 12 13:03:47 1999
00010 // commit: $Id: hpsound.h 10065 2007-04-12 05:54:56Z rjpeters $
00011 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/media/hpsound.h $
00012 //
00013 // --------------------------------------------------------------------
00014 //
00015 // This file is part of GroovX.
00016 //   [http://ilab.usc.edu/rjpeters/groovx/]
00017 //
00018 // GroovX is free software; you can redistribute it and/or modify it
00019 // under the terms of the GNU General Public License as published by
00020 // the Free Software Foundation; either version 2 of the License, or
00021 // (at your option) any later version.
00022 //
00023 // GroovX is distributed in the hope that it will be useful, but
00024 // WITHOUT ANY WARRANTY; without even the implied warranty of
00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00026 // General Public License for more details.
00027 //
00028 // You should have received a copy of the GNU General Public License
00029 // along with GroovX; if not, write to the Free Software Foundation,
00030 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00031 //
00033 
00034 #ifndef GROOVX_MEDIA_HPSOUND_H_UTC20050626084018_DEFINED
00035 #define GROOVX_MEDIA_HPSOUND_H_UTC20050626084018_DEFINED
00036 
00037 #include "media/soundrep.h"
00038 
00039 #include "rutz/error.h"
00040 #include "rutz/fstring.h"
00041 #include "rutz/sfmt.h"
00042 
00043 #include <Alib.h>
00044 
00045 #include "rutz/trace.h"
00046 #include "rutz/debug.h"
00047 
00048 namespace media
00049 {
00050   //  #######################################################
00051   //  =======================================================
00052 
00054 
00055   class hp_audio_sound_rep : public sound_rep
00056   {
00057   public:
00058     hp_audio_sound_rep(const char* filename = 0);
00059     virtual ~hp_audio_sound_rep() throw();
00060 
00061     virtual void play();
00062 
00063   private:
00064     SBucket* m_sbucket;
00065     SBPlayParams m_play_params;
00066 
00067     static Audio* s_audio = 0;
00068 
00069     static bool init_sound();
00070     static void close_sound();
00071     static long error_handler(Audio* audio, AErrorEvent* err);
00072   };
00073 
00074 }
00075 
00076 Audio* media::hp_audio_sound_rep::s_audio = 0;
00077 
00078 media::hp_audio_sound_rep::hp_audio_sound_rep(const char* filename) :
00079   m_sbucket(0),
00080   m_play_params()
00081 {
00082 GVX_TRACE("hp_audio_sound_rep::hp_audio_sound_rep");
00083   init_sound();
00084   if ( !s_audio )
00085     throw rutz::error("invalid HP audio server connection", SRC_POS);
00086 
00087   sound_rep::check_filename(filename);
00088 
00089   m_play_params.pause_first = 0;
00090   m_play_params.start_offset.type = ATTSamples;
00091   m_play_params.start_offset.u.samples = 0;
00092   m_play_params.loop_count = 0;
00093   m_play_params.previous_transaction = 0;
00094   m_play_params.gain_matrix = *ASimplePlayer(s_audio);
00095   m_play_params.priority = APriorityNormal;
00096   m_play_params.play_volume = AUnityGain;
00097   m_play_params.duration.type = ATTFullLength;
00098   m_play_params.event_mask = 0;
00099 
00100   AFileFormat file_format = AFFUnknown;
00101   AudioAttrMask attr_mask = 0;
00102   AudioAttributes attribs;
00103 
00104   m_sbucket = ALoadAFile(s_audio, const_cast<char *>(filename),
00105                          file_format, attr_mask, &attribs, NULL);
00106 }
00107 
00108 media::hp_audio_sound_rep::~hp_audio_sound_rep() throw()
00109 {
00110 GVX_TRACE("hp_audio_sound_rep::~hp_audio_sound_rep");
00111   if ( s_audio != 0 )
00112     {
00113       if (m_sbucket)
00114         ADestroySBucket( s_audio, m_sbucket, NULL );
00115     }
00116 }
00117 
00118 void media::hp_audio_sound_rep::play()
00119 {
00120 GVX_TRACE("hp_audio_sound_rep::play");
00121   init_sound();
00122   if ( !s_audio )
00123     throw rutz::error("invalid audio server connection", SRC_POS);
00124 
00125   if (m_sbucket)
00126     ATransID xid = APlaySBucket( s_audio, m_sbucket, &m_play_params, NULL );
00127 }
00128 
00129 bool media::hp_audio_sound_rep::init_sound()
00130 {
00131 GVX_TRACE("hp_audio_sound_rep::init_sound");
00132 
00133   if (s_audio != 0) return true;
00134 
00135   ASetErrorHandler(&error_handler);
00136 
00137   bool retval = false;
00138 
00139   // Open an audio connection to the default audio server, then
00140   // check to make sure connection succeeded. If the connection
00141   // fails, 'audio' is set to NULL.
00142   const char* server_name = "";
00143   long status = 0;
00144   s_audio = AOpenAudio( const_cast<char*>(server_name), &status );
00145   if ( status != 0 )
00146     {
00147       s_audio = NULL;
00148       retval = false;
00149     }
00150   else
00151     {
00152       ASetCloseDownMode( s_audio, AKeepTransactions, NULL );
00153       retval = true;
00154     }
00155 
00156   return retval;
00157 }
00158 
00159 void media::hp_audio_sound_rep::close_sound()
00160 {
00161 GVX_TRACE("hp_audio_sound_rep::close_sound");
00162   if ( s_audio )
00163     {
00164       ACloseAudio( s_audio, NULL );
00165       s_audio = 0;
00166     }
00167 }
00168 
00169 long media::hp_audio_sound_rep::error_handler(Audio* audio, AErrorEvent *err)
00170 {
00171 GVX_TRACE("media::hp_audio_sound_rep::error_handler");
00172 
00173   static char buf[128];
00174   AGetErrorText(audio, err->error_code, buf, 127);
00175 
00176   dbg_eval_nl(3, buf);
00177 
00178   throw rutz::error(rutz::sfmt("HP Audio Error: %s", buf), SRC_POS);
00179 
00180   // we'll never get here, but we need to placate the compiler
00181   return 0;
00182 }
00183 
00184 static const char __attribute__((used)) vcid_groovx_media_hpsound_h_utc20050626084018[] = "$Id: hpsound.h 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00185 #endif // !GROOVX_MEDIA_HPSOUND_H_UTC20050626084018_DEFINED

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:39 2008 by Doxygen version 1.5.5.