allegro_s.cpp

Go to the documentation of this file.
00001 /* $Id: allegro_s.cpp 16804 2009-07-13 10:16:50Z 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 "Failed to set up Allegro";
00049   _allegro_instance_count++;
00050 
00051   /* Initialise the sound */
00052   if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL) != 0) return "Failed to set up Allegro sound";
00053 
00054   /* Okay, there's no soundcard */
00055   if (digi_card == DIGI_NONE) {
00056     DEBUG(driver, 0, "allegro: no sound card found");
00057     return "No sound card found";
00058   }
00059 
00060   _stream = play_audio_stream(BUFFER_SIZE, 16, true, 11025, 255, 128);
00061   MxInitialize(11025);
00062   return NULL;
00063 }
00064 
00065 void SoundDriver_Allegro::Stop()
00066 {
00067   if (_stream != NULL) {
00068     stop_audio_stream(_stream);
00069     _stream = NULL;
00070   }
00071   remove_sound();
00072 
00073   if (--_allegro_instance_count == 0) allegro_exit();
00074 }
00075 
00076 #endif /* WITH_ALLEGRO */

Generated on Sun Sep 13 08:19:19 2009 for OpenTTD by  doxygen 1.5.6