allegro_s.cpp

Go to the documentation of this file.
00001 /* $Id: allegro_s.cpp 15305 2009-02-01 11:52:41Z rubidium $ */
00002 
00005 #ifdef WITH_ALLEGRO
00006 
00007 #include "../stdafx.h"
00008 
00009 #include "../driver.h"
00010 #include "../mixer.h"
00011 #include "../sdl.h"
00012 #include "../debug.h"
00013 #include "allegro_s.h"
00014 #include <allegro.h>
00015 
00016 static FSoundDriver_Allegro iFSoundDriver_Allegro;
00018 static AUDIOSTREAM *_stream = NULL;
00020 static const int BUFFER_SIZE = 512;
00021 
00022 void SoundDriver_Allegro::MainLoop()
00023 {
00024   /* We haven't opened a stream yet */
00025   if (_stream == NULL) return;
00026 
00027   void *data = get_audio_stream_buffer(_stream);
00028   /* We don't have to fill the stream yet */
00029   if (data == NULL) return;
00030 
00031   /* Mix the samples */
00032   MxMixSamples(data, BUFFER_SIZE);
00033 
00034   /* Allegro sound is always unsigned, so we need to correct that */
00035   uint16 *snd = (uint16*)data;
00036   for (int i = 0; i < BUFFER_SIZE * 2; i++) snd[i] ^= 0x8000;
00037 
00038   /* Tell we've filled the stream */
00039   free_audio_stream_buffer(_stream);
00040 }
00041 
00044 extern int _allegro_instance_count;
00045 
00046 const char *SoundDriver_Allegro::Start(const char * const *parm)
00047 {
00048   if (_allegro_instance_count == 0 && install_allegro(SYSTEM_AUTODETECT, &errno, NULL)) return NULL;
00049   _allegro_instance_count++;
00050 
00051   /* Initialise the sound */
00052   if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL) != 0) return NULL;
00053 
00054   /* Okay, there's no soundcard */
00055   if (digi_card == DIGI_NONE) {
00056     DEBUG(driver, 0, "allegro: no sound card found");
00057     return NULL;
00058   }
00059 
00060   _stream = play_audio_stream(BUFFER_SIZE, 16, true, 11025, 255, 128);
00061   return NULL;
00062 }
00063 
00064 void SoundDriver_Allegro::Stop()
00065 {
00066   if (_stream != NULL) {
00067     stop_audio_stream(_stream);
00068     _stream = NULL;
00069   }
00070   remove_sound();
00071 
00072   if (--_allegro_instance_count == 0) allegro_exit();
00073 }
00074 
00075 #endif /* WITH_ALLEGRO */

Generated on Mon Jun 8 23:04:07 2009 for OpenTTD by  doxygen 1.5.6