# allegro_s.cpp

Go to the documentation of this file.

    00001 /* $Id: allegro_s.cpp 18821 2010-01-15 23:47:28Z rubidium $ */
    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 "../mixer.h"
    00017 #include "../debug.h"
    00018 #include "allegro_s.h"
    00019 #include <allegro.h>
    00020
    00021 static FSoundDriver_Allegro iFSoundDriver_Allegro;
    00023 static AUDIOSTREAM *_stream = NULL;
    00025 static int _buffer_size;
    00026
    00027 void SoundDriver_Allegro::MainLoop()
    00028 {
    00029   /* We haven't opened a stream yet */
    00030   if (_stream == NULL) return;
    00031
    00032   void *data = get_audio_stream_buffer(_stream);
    00033   /* We don't have to fill the stream yet */
    00034   if (data == NULL) return;
    00035
    00036   /* Mix the samples */
    00037   MxMixSamples(data, _buffer_size);
    00038
    00039   /* Allegro sound is always unsigned, so we need to correct that */
    00040   uint16 *snd = (uint16*)data;
    00041   for (int i = 0; i < _buffer_size * 2; i++) snd[i] ^= 0x8000;
    00042
    00043   /* Tell we've filled the stream */
    00044   free_audio_stream_buffer(_stream);
    00045 }
    00046
    00049 extern int _allegro_instance_count;
    00050
    00051 const char *SoundDriver_Allegro::Start(const char * const *parm)
    00052 {
    00053   if (_allegro_instance_count == 0 && install_allegro(SYSTEM_AUTODETECT, &errno, NULL)) {
    00054     DEBUG(driver, 0, "allegro: install_allegro failed '%s'", allegro_error);
    00055     return "Failed to set up Allegro";
    00056   }
    00057   _allegro_instance_count++;
    00058
    00059   /* Initialise the sound */
    00060   if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL) != 0) {
    00061     DEBUG(driver, 0, "allegro: install_sound failed '%s'", allegro_error);
    00062     return "Failed to set up Allegro sound";
    00063   }
    00064
    00065   /* Okay, there's no soundcard */
    00066   if (digi_card == DIGI_NONE) {
    00067     DEBUG(driver, 0, "allegro: no sound card found");
    00068     return "No sound card found";
    00069   }
    00070
    00071   int hz = GetDriverParamInt(parm, "hz", 44100);
    00072   _buffer_size = GetDriverParamInt(parm, "samples", 1024) * hz / 11025;
    00073   _stream = play_audio_stream(_buffer_size, 16, true, hz, 255, 128);
    00074   MxInitialize(hz);
    00075   return NULL;
    00076 }
    00077
    00078 void SoundDriver_Allegro::Stop()
    00079 {
    00080   if (_stream != NULL) {
    00081     stop_audio_stream(_stream);
    00082     _stream = NULL;
    00083   }
    00084   remove_sound();
    00085
    00086   if (--_allegro_instance_count == 0) allegro_exit();
    00087 }
    00088
    00089 #endif /* WITH_ALLEGRO */