OpenTTD Source  20241111-master-gce64d5f5d9
allegro_s.cpp
Go to the documentation of this file.
1 /*
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/>.
6  */
7 
10 #ifdef WITH_ALLEGRO
11 
12 #include "../stdafx.h"
13 
14 #include "../mixer.h"
15 #include "../debug.h"
16 #include "allegro_s.h"
17 #include <allegro.h>
18 
19 #include "../safeguards.h"
20 
21 static FSoundDriver_Allegro iFSoundDriver_Allegro;
23 static AUDIOSTREAM *_stream = nullptr;
25 static int _buffer_size;
26 
28 {
29  /* We haven't opened a stream yet */
30  if (_stream == nullptr) return;
31 
32  void *data = get_audio_stream_buffer(_stream);
33  /* We don't have to fill the stream yet */
34  if (data == nullptr) return;
35 
36  /* Mix the samples */
37  MxMixSamples(data, _buffer_size);
38 
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;
42 
43  /* Tell we've filled the stream */
44  free_audio_stream_buffer(_stream);
45 }
46 
51 extern int _allegro_instance_count;
52 
53 std::optional<std::string_view> SoundDriver_Allegro::Start(const StringList &parm)
54 {
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";
58  }
59  _allegro_instance_count++;
60 
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";
65  }
66 
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";
71  }
72 
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);
76  MxInitialize(hz);
77  return std::nullopt;
78 }
79 
81 {
82  if (_stream != nullptr) {
83  stop_audio_stream(_stream);
84  _stream = nullptr;
85  }
86  remove_sound();
87 
88  if (--_allegro_instance_count == 0) allegro_exit();
89 }
90 
91 #endif /* WITH_ALLEGRO */
Base for playing sound via Allegro.
Factory for the allegro sound driver.
Definition: allegro_s.h:27
void Stop() override
Stop this driver.
void MainLoop() override
Called once every tick.
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
int GetDriverParamInt(const StringList &parm, const char *name, int def)
Get an integer parameter the list of parameters.
Definition: driver.cpp:76
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:60