# allegro_s.cpp

Go to the documentation of this file.

    00001 /* $Id: allegro_s.cpp 20286 2010-08-01 19:44:49Z frosch $ */
    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
    00051 extern int _allegro_instance_count;
    00052
    00053 const char *SoundDriver_Allegro::Start(const char * const *parm)
    00054 {
    00055   if (_allegro_instance_count == 0 && install_allegro(SYSTEM_AUTODETECT, &errno, NULL)) {
    00056     DEBUG(driver, 0, "allegro: install_allegro failed '%s'", allegro_error);
    00057     return "Failed to set up Allegro";
    00058   }
    00059   _allegro_instance_count++;
    00060
    00061   /* Initialise the sound */
    00062   if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL) != 0) {
    00063     DEBUG(driver, 0, "allegro: install_sound failed '%s'", allegro_error);
    00064     return "Failed to set up Allegro sound";
    00065   }
    00066
    00067   /* Okay, there's no soundcard */
    00068   if (digi_card == DIGI_NONE) {
    00069     DEBUG(driver, 0, "allegro: no sound card found");
    00070     return "No sound card found";
    00071   }
    00072
    00073   int hz = GetDriverParamInt(parm, "hz", 44100);
    00074   _buffer_size = GetDriverParamInt(parm, "samples", 1024) * hz / 11025;
    00075   _stream = play_audio_stream(_buffer_size, 16, true, hz, 255, 128);
    00076   MxInitialize(hz);
    00077   return NULL;
    00078 }
    00079
    00080 void SoundDriver_Allegro::Stop()
    00081 {
    00082   if (_stream != NULL) {
    00083     stop_audio_stream(_stream);
    00084     _stream = NULL;
    00085   }
    00086   remove_sound();
    00087
    00088   if (--_allegro_instance_count == 0) allegro_exit();
    00089 }
    00090
    00091 #endif /* WITH_ALLEGRO */