The Battle for Wesnoth  1.19.24+dev
sdl3_audio.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2022 - 2025
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include <stdlib.h>
16 #include <stdio.h>
17 
18 #include <SDL3/SDL.h>
19 #include <SDL3_mixer/SDL_mixer.h>
20 #include <iostream>
21 
22 int main(int, char** argv)
23 {
24  if (!SDL_Init(SDL_INIT_AUDIO)) {
25  fprintf(stdout, "Cannot initialize SDL Audio: %s\n", SDL_GetError());
26  return (EXIT_FAILURE);
27  }
28 
29  if(!MIX_Init()) {
30  fprintf(stdout, "Cannot initialize SDL Mixer: %s\n", SDL_GetError());
31  return (EXIT_FAILURE);
32  }
33 
34  SDL_AudioSpec spec;
35  spec.freq = 44100;
36  spec.format = SDL_AUDIO_S16;
37  spec.channels = 2;
38  MIX_Mixer* mixer = MIX_CreateMixerDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec);
39  if (!mixer) {
40  fprintf(stdout, "Cannot initialize audio device: %s\n", SDL_GetError());
41  return (EXIT_FAILURE);
42  }
43 
44  int n = MIX_GetNumAudioDecoders();
45  bool found_vorbis = false;
46  for(int i = 0; i < n; i++) {
47  if(strcmp(MIX_GetAudioDecoder(i), "VORBIS") == 0) {
48  found_vorbis = true;
49  }
50  }
51  if(!found_vorbis) {
52  fprintf(stdout, "VORBIS codec not available\n");
53  return (EXIT_FAILURE);
54  }
55 
56  MIX_Audio* music = MIX_LoadAudio(mixer, argv[1], false);
57  if (music == nullptr) {
58  fprintf(stdout, "Cannot load music file: %s\n", SDL_GetError());
59  MIX_DestroyMixer(mixer);
60  return (EXIT_FAILURE);
61  }
62 
63  fprintf(stdout, "Success\n");
64  MIX_DestroyAudio(music);
65  MIX_DestroyMixer(mixer);
66  MIX_Quit();
67  return (EXIT_SUCCESS);
68 }
std::size_t i
Definition: function.cpp:1031
MIX_Mixer * mixer
Definition: sound.cpp:53
int main(int, char **argv)
Definition: sdl3_audio.cpp:22
static map_location::direction n