| 1 | /*************************************************************************** |
| 2 | * fheroes2: https://github.com/ihhub/fheroes2 * |
| 3 | * Copyright (C) 2019 - 2025 * |
| 4 | * * |
| 5 | * Free Heroes2 Engine: http://sourceforge.net/projects/fheroes2 * |
| 6 | * Copyright (C) 2008 by Andrey Afletdinov <[email protected]> * |
| 7 | * * |
| 8 | * This program is free software; you can redistribute it and/or modify * |
| 9 | * it under the terms of the GNU General Public License as published by * |
| 10 | * the Free Software Foundation; either version 2 of the License, or * |
| 11 | * (at your option) any later version. * |
| 12 | * * |
| 13 | * This program is distributed in the hope that it will be useful, * |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 | * GNU General Public License for more details. * |
| 17 | * * |
| 18 | * You should have received a copy of the GNU General Public License * |
| 19 | * along with this program; if not, write to the * |
| 20 | * Free Software Foundation, Inc., * |
| 21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 22 | ***************************************************************************/ |
| 23 | |
| 24 | #pragma once |
| 25 | |
| 26 | #include <cstdint> |
| 27 | #include <optional> |
| 28 | #include <string> |
| 29 | #include <utility> |
| 30 | #include <vector> |
| 31 | |
| 32 | struct ListFiles; |
| 33 | |
| 34 | namespace Audio |
| 35 | { |
| 36 | // Audio initialization and deinitialization functions are not designed to be called |
| 37 | // concurrently from different threads. They should be called from the main thread only. |
| 38 | void Init(); |
| 39 | void Quit(); |
| 40 | |
| 41 | void Mute(); |
| 42 | void Unmute(); |
| 43 | |
| 44 | bool isValid(); |
| 45 | } |
| 46 | |
| 47 | namespace Mixer |
| 48 | { |
| 49 | void SetChannels( const int num ); |
| 50 | |
| 51 | int getChannelCount(); |
| 52 | |
| 53 | // Starts playback of the given sound with the ability of looping it, as well as (optionally) |
| 54 | // the ability to specify the position of the sound source relative to the listener (the angle |
| 55 | // of direction to the sound source in degrees and the distance to the sound source). |
| 56 | int Play( const uint8_t * ptr, const uint32_t size, const bool loop, const std::optional<std::pair<int16_t, uint8_t>> position = {} ); |
| 57 | |
| 58 | void setVolume( const int volumePercentage ); |
| 59 | |
| 60 | // Sets the position of the sound source relative to the listener (the angle of direction to |
| 61 | // the sound source in degrees and the distance to the sound source) for the given channel. |
| 62 | void setPosition( const int channelId, const int16_t angle, const uint8_t distance ); |
| 63 | |
| 64 | void Stop( const int channelId = -1 ); |
| 65 | |
| 66 | bool isPlaying( const int channelId ); |
| 67 | } |
| 68 | |
| 69 | namespace Music |
| 70 | { |
| 71 | enum class PlaybackMode : uint8_t |
| 72 | { |
| 73 | // Play a music track once from the beginning, do not remember the position at which |
| 74 | // playback of this track was stopped (rewind it to the beginning when playback stops) |
| 75 | PLAY_ONCE, |
| 76 | // Play a music track in an endless loop, starting from its previously remembered position |
| 77 | // (or from the beginning if this track is being played for the first time) and remember |
| 78 | // the position at which playback of this track was stopped |
| 79 | RESUME_AND_PLAY_INFINITE, |
| 80 | // Play a music track in an endless loop, starting from the beginning, do not remember |
| 81 | // the position at which playback of this track was stopped (rewind it to the beginning |
| 82 | // when playback stops) |
| 83 | REWIND_AND_PLAY_INFINITE |
| 84 | }; |
| 85 | |
| 86 | // Music UID is used to store metadata of music tracks in the music database. It is caller's |
| 87 | // responsibility to generate them. This function searches for a music track with the specified |
| 88 | // UID in the database and starts playback if it is found. Returns true if the music track was |
| 89 | // found in the database, otherwise returns false. |
| 90 | bool Play( const uint64_t musicUID, const PlaybackMode playbackMode ); |
| 91 | |
| 92 | // Adds a music track from the memory buffer to the music database and starts playback. A music |
| 93 | // track with the specified UID should not already be present in the database. |
| 94 | void Play( const uint64_t musicUID, const std::vector<uint8_t> & v, const PlaybackMode playbackMode ); |
| 95 | |
| 96 | // Adds the music track available in the specified file to the music database and starts playback. |
| 97 | // A music track with the specified UID should not already be present in the database. |
| 98 | void Play( const uint64_t musicUID, const std::string & file, const PlaybackMode playbackMode ); |
| 99 | |
| 100 | void setVolume( const int volumePercentage ); |
| 101 | |
| 102 | void SetFadeInMs( const int timeMs ); |
| 103 | |
| 104 | void Stop(); |
| 105 | |
| 106 | bool isPlaying(); |
| 107 | |
| 108 | void setMidiSoundFonts( const ListFiles & files ); |
| 109 | void setMidiTimidityCfg( const std::string & path ); |
| 110 | |
| 111 | std::vector<uint8_t> Xmi2Mid( const std::vector<uint8_t> & buf ); |
| 112 | } |