allegro_s.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #ifdef WITH_ALLEGRO
00013 
00014 #include "../stdafx.h"
00015 
00016 #include "../driver.h"
00017 #include "../mixer.h"
00018 #include "../debug.h"
00019 #include "allegro_s.h"
00020 #include <allegro.h>
00021 
00022 static FSoundDriver_Allegro iFSoundDriver_Allegro;
00024 static AUDIOSTREAM *_stream = NULL;
00026 static const int BUFFER_SIZE = 4096;
00027 
00028 void SoundDriver_Allegro::MainLoop()
00029 {
00030   /* We haven't opened a stream yet */
00031   if (_stream == NULL) return;
00032 
00033   void *data = get_audio_stream_buffer(_stream);
00034   /* We don't have to fill the stream yet */
00035   if (data == NULL) return;
00036 
00037   /* Mix the samples */
00038   MxMixSamples(data, BUFFER_SIZE);
00039 
00040   /* Allegro sound is always unsigned, so we need to correct that */
00041   uint16 *snd = (uint16*)data;
00042   for (int i = 0; i < BUFFER_SIZE * 2; i++) snd[i] ^= 0x8000;
00043 
00044   /* Tell we've filled the stream */
00045   free_audio_stream_buffer(_stream);
00046 }
00047 
00050 extern int _allegro_instance_count;
00051 
00052 const char *SoundDriver_Allegro::Start(const char * const *parm)
00053 {
00054   if (_allegro_instance_count == 0 && install_allegro(SYSTEM_AUTODETECT, &errno, NULL)) {
00055     DEBUG(driver, 0, "allegro: install_allegro failed '%s'", allegro_error);
00056     return "Failed to set up Allegro";
00057   }
00058   _allegro_instance_count++;
00059 
00060   /* Initialise the sound */
00061   if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL) != 0) {
00062     DEBUG(driver, 0, "allegro: install_sound failed '%s'", allegro_error);
00063     return "Failed to set up Allegro sound";
00064   }
00065 
00066   /* Okay, there's no soundcard */
00067   if (digi_card == DIGI_NONE) {
00068     DEBUG(driver, 0, "allegro: no sound card found");
00069     return "No sound card found";
00070   }
00071 
00072   _stream = play_audio_stream(BUFFER_SIZE, 16, true, 44100, 255, 128);
00073   MxInitialize(44100);
00074   return NULL;
00075 }
00076 
00077 void SoundDriver_Allegro::Stop()
00078 {
00079   if (_stream != NULL) {
00080     stop_audio_stream(_stream);
00081     _stream = NULL;
00082   }
00083   remove_sound();
00084 
00085   if (--_allegro_instance_count == 0) allegro_exit();
00086 }
00087 
00088 #endif /* WITH_ALLEGRO */

Generated on Wed Dec 30 20:40:06 2009 for OpenTTD by  doxygen 1.5.6