2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 /** @file allegro_s.cpp Playing sound via Allegro. */
12 #include "../stdafx.h"
16 #include "allegro_s.h"
19 #include "../safeguards.h"
21 static FSoundDriver_Allegro iFSoundDriver_Allegro
;
22 /** The stream we are writing too */
23 static AUDIOSTREAM
*_stream
= nullptr;
24 /** The number of samples in the buffer */
25 static int _buffer_size
;
27 void SoundDriver_Allegro::MainLoop()
29 /* We haven't opened a stream yet */
30 if (_stream
== nullptr) return;
32 void *data
= get_audio_stream_buffer(_stream
);
33 /* We don't have to fill the stream yet */
34 if (data
== nullptr) return;
37 MxMixSamples(data
, _buffer_size
);
39 /* Allegro sound is always unsigned, so we need to correct that */
40 uint16_t *snd
= (uint16_t*)data
;
41 for (int i
= 0; i
< _buffer_size
* 2; i
++) snd
[i
] ^= 0x8000;
43 /* Tell we've filled the stream */
44 free_audio_stream_buffer(_stream
);
48 * There are multiple modules that might be using Allegro and
49 * Allegro can only be initiated once.
51 extern int _allegro_instance_count
;
53 std::optional
<std::string_view
> SoundDriver_Allegro::Start(const StringList
&parm
)
55 if (_allegro_instance_count
== 0 && install_allegro(SYSTEM_AUTODETECT
, &errno
, nullptr)) {
56 Debug(driver
, 0, "allegro: install_allegro failed '{}'", allegro_error
);
57 return "Failed to set up Allegro";
59 _allegro_instance_count
++;
61 /* Initialise the sound */
62 if (install_sound(DIGI_AUTODETECT
, MIDI_AUTODETECT
, nullptr) != 0) {
63 Debug(driver
, 0, "allegro: install_sound failed '{}'", allegro_error
);
64 return "Failed to set up Allegro sound";
67 /* Okay, there's no soundcard */
68 if (digi_card
== DIGI_NONE
) {
69 Debug(driver
, 0, "allegro: no sound card found");
70 return "No sound card found";
73 int hz
= GetDriverParamInt(parm
, "hz", 44100);
74 _buffer_size
= GetDriverParamInt(parm
, "samples", 1024) * hz
/ 11025;
75 _stream
= play_audio_stream(_buffer_size
, 16, true, hz
, 255, 128);
80 void SoundDriver_Allegro::Stop()
82 if (_stream
!= nullptr) {
83 stop_audio_stream(_stream
);
88 if (--_allegro_instance_count
== 0) allegro_exit();
91 #endif /* WITH_ALLEGRO */