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/>.
10 /** @file allegro_s.cpp Playing sound via Allegro. */
14 #include "../stdafx.h"
18 #include "allegro_s.h"
21 #include "../safeguards.h"
23 static FSoundDriver_Allegro iFSoundDriver_Allegro
;
24 /** The stream we are writing too */
25 static AUDIOSTREAM
*_stream
= nullptr;
26 /** The number of samples in the buffer */
27 static int _buffer_size
;
29 void SoundDriver_Allegro::MainLoop()
31 /* We haven't opened a stream yet */
32 if (_stream
== nullptr) return;
34 void *data
= get_audio_stream_buffer(_stream
);
35 /* We don't have to fill the stream yet */
36 if (data
== nullptr) return;
39 MxMixSamples(data
, _buffer_size
);
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;
45 /* Tell we've filled the stream */
46 free_audio_stream_buffer(_stream
);
50 * There are multiple modules that might be using Allegro and
51 * Allegro can only be initiated once.
53 extern int _allegro_instance_count
;
55 const char *SoundDriver_Allegro::Start(const char * const *parm
)
57 if (_allegro_instance_count
== 0 && install_allegro(SYSTEM_AUTODETECT
, &errno
, nullptr)) {
58 DEBUG(driver
, 0, "allegro: install_allegro failed '%s'", allegro_error
);
59 return "Failed to set up Allegro";
61 _allegro_instance_count
++;
63 /* Initialise the sound */
64 if (install_sound(DIGI_AUTODETECT
, MIDI_AUTODETECT
, nullptr) != 0) {
65 DEBUG(driver
, 0, "allegro: install_sound failed '%s'", allegro_error
);
66 return "Failed to set up Allegro sound";
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";
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);
82 void SoundDriver_Allegro::Stop()
84 if (_stream
!= nullptr) {
85 stop_audio_stream(_stream
);
90 if (--_allegro_instance_count
== 0) allegro_exit();
93 #endif /* WITH_ALLEGRO */