OpenTTD
allegro_s.cpp
Go to the documentation of this file.
1 /* $Id: allegro_s.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifdef WITH_ALLEGRO
13 
14 #include "../stdafx.h"
15 
16 #include "../mixer.h"
17 #include "../debug.h"
18 #include "allegro_s.h"
19 #include <allegro.h>
20 
21 #include "../safeguards.h"
22 
23 static FSoundDriver_Allegro iFSoundDriver_Allegro;
25 static AUDIOSTREAM *_stream = NULL;
27 static int _buffer_size;
28 
30 {
31  /* We haven't opened a stream yet */
32  if (_stream == NULL) return;
33 
34  void *data = get_audio_stream_buffer(_stream);
35  /* We don't have to fill the stream yet */
36  if (data == NULL) return;
37 
38  /* Mix the samples */
39  MxMixSamples(data, _buffer_size);
40 
41  /* Allegro sound is always unsigned, so we need to correct that */
42  uint16 *snd = (uint16*)data;
43  for (int i = 0; i < _buffer_size * 2; i++) snd[i] ^= 0x8000;
44 
45  /* Tell we've filled the stream */
46  free_audio_stream_buffer(_stream);
47 }
48 
53 extern int _allegro_instance_count;
54 
55 const char *SoundDriver_Allegro::Start(const char * const *parm)
56 {
57  if (_allegro_instance_count == 0 && install_allegro(SYSTEM_AUTODETECT, &errno, NULL)) {
58  DEBUG(driver, 0, "allegro: install_allegro failed '%s'", allegro_error);
59  return "Failed to set up Allegro";
60  }
61  _allegro_instance_count++;
62 
63  /* Initialise the sound */
64  if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL) != 0) {
65  DEBUG(driver, 0, "allegro: install_sound failed '%s'", allegro_error);
66  return "Failed to set up Allegro sound";
67  }
68 
69  /* Okay, there's no soundcard */
70  if (digi_card == DIGI_NONE) {
71  DEBUG(driver, 0, "allegro: no sound card found");
72  return "No sound card found";
73  }
74 
75  int hz = GetDriverParamInt(parm, "hz", 44100);
76  _buffer_size = GetDriverParamInt(parm, "samples", 1024) * hz / 11025;
77  _stream = play_audio_stream(_buffer_size, 16, true, hz, 255, 128);
78  MxInitialize(hz);
79  return NULL;
80 }
81 
83 {
84  if (_stream != NULL) {
85  stop_audio_stream(_stream);
86  _stream = NULL;
87  }
88  remove_sound();
89 
90  if (--_allegro_instance_count == 0) allegro_exit();
91 }
92 
93 #endif /* WITH_ALLEGRO */