| 1 |
--[[ Copyright (c) 2009 Peter "Corsix" Cawley |
| 2 |
|
| 3 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 4 |
this software and associated documentation files (the "Software"), to deal in |
| 5 |
the Software without restriction, including without limitation the rights to |
| 6 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 7 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 8 |
so, subject to the following conditions: |
| 9 |
|
| 10 |
The above copyright notice and this permission notice shall be included in all |
| 11 |
copies or substantial portions of the Software. |
| 12 |
|
| 13 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 |
SOFTWARE. --]] |
| 20 |
|
| 21 |
local pathsep = package.config:sub(1, 1) |
| 22 |
local rnc = require("rnc") |
| 23 |
local lfs = require("lfs") |
| 24 |
local SDL = require("sdl") |
| 25 |
local TH = require("TH") |
| 26 |
local ipairs |
| 27 |
= ipairs |
| 28 |
|
| 29 |
--! Layer which handles the Lua-facing side of loading and playing audio. |
| 30 |
class "Audio" |
| 31 |
|
| 32 |
---@type Audio |
| 33 |
local Audio = _G["Audio"] |
| 34 |
|
| 35 |
function Audio:Audio(app) |
| 36 |
self.app = app |
| 37 |
|
| 38 |
self.has_bg_music = false |
| 39 |
self.not_loaded = not app.config.audio |
| 40 |
self.unused_played_callback_id = 0 |
| 41 |
self.played_sound_callbacks = {} |
| 42 |
self.entities_waiting_for_sound_to_be_enabled = {} |
| 43 |
self.midi_player = nil |
| 44 |
self.allowed_waveform_formats = { |
| 45 |
"OGG", "OPUS", "FLAC", "WV", "WAV", "WAVE", |
| 46 |
"MPG", "MPEG", "MP3", "MAD", "AIFF", "AIFC", "AIF" |
| 47 |
} |
| 48 |
self.allowed_instructional_formats = { |
| 49 |
"MID", "MIDI", "KAR", "669", "AMF", "AMS", "DBM", |
| 50 |
"DSM", "FAR", "GDM", "IT", "MED", "MDL", "MOD", "MOL", "MTM", "NST", "OKT", "PTM", |
| 51 |
"S3M", "STM", "ULT", "UMX", "WOW", "XM", "XMI" |
| 52 |
} |
| 53 |
end |
| 54 |
|
| 55 |
function Audio:clearCallbacks() |
| 56 |
self.unused_played_callback_id = 0 |
| 57 |
self.played_sound_callbacks = {} |
| 58 |
self.entities_waiting_for_sound_to_be_enabled = {} |
| 59 |
end |
| 60 |
|
| 61 |
local function GetFileData(path) |
| 62 |
local f, e = io.open(path, "rb") |
| 63 |
if not f then |
| 64 |
return f, e |
| 65 |
end |
| 66 |
e = f:read"*a" |
| 67 |
f:close() |
| 68 |
return e |
| 69 |
end |
| 70 |
|
| 71 |
function Audio:init() |
| 72 |
self.background_playlist = { |
| 73 |
-- {title = "", filename = "", enabled = true, music = nil}, |
| 74 |
} |
| 75 |
if self.not_loaded then |
| 76 |
return |
| 77 |
end |
| 78 |
|
| 79 |
local music = self.app.config.audio_music or self.app.config.audio_mp3 |
| 80 |
local music_dir |
| 81 |
if music then |
| 82 |
music_dir = music |
| 83 |
if music_dir:sub(-1) ~= pathsep then |
| 84 |
music_dir = music_dir .. pathsep |
| 85 |
end |
| 86 |
end |
| 87 |
|
| 88 |
local music_array = {} |
| 89 |
local function musicFileTable(filename) |
| 90 |
filename = filename:upper() |
| 91 |
local t = music_array[filename] |
| 92 |
if t == nil then |
| 93 |
t = {} |
| 94 |
music_array[filename] = t |
| 95 |
end |
| 96 |
return t |
| 97 |
end |
| 98 |
|
| 99 |
--[[ |
| 100 |
Find the music files on disk. |
| 101 |
----------------------------- |
| 102 |
|
| 103 |
- Will search through all the files in music_dir. |
| 104 |
- Adds most files supported by SDL2_mixer (regardless of local library's ability) |
| 105 |
- Prefers waveform audio (eg mp3) over instructional (eg midi) |
| 106 |
- Uses titles from MIDI.TXT if found, else the filename. |
| 107 |
--]] |
| 108 |
local midi_txt -- File name of midi.txt file, if any. |
| 109 |
local waveform = list_to_set(self.allowed_waveform_formats) |
| 110 |
local instructional = list_to_set(self.allowed_instructional_formats) |
| 111 |
|
| 112 |
local _f, _s, _v |
| 113 |
if music_dir then |
| 114 |
_f, _s, _v = lfs.dir(music_dir) |
| 115 |
else |
| 116 |
_f, _s, _v = pairs(self.app.fs:listFiles("Sound", "Midi") or {}) |
| 117 |
end |
| 118 |
for file in _f, _s, _v do |
| 119 |
local info |
| 120 |
local filename, ext = file:match("^(.+)%.([^.]+)$") |
| 121 |
ext = ext and ext:upper() |
| 122 |
if waveform[ext] or instructional[ext] then |
| 123 |
info = musicFileTable(filename) |
| 124 |
-- This title might be replaced later by the midi_txt. |
| 125 |
info.title = filename |
| 126 |
-- XMI files are supported with our own conversion |
| 127 |
info.is_xmi = ext == "XMI" |
| 128 |
end |
| 129 |
if instructional[ext] and not music_dir then |
| 130 |
info.filename = table.concat({"Sound", "Midi", file}, pathsep) |
| 131 |
-- User supplied music file found |
| 132 |
elseif waveform[ext] then |
| 133 |
info.filename_music = music_dir .. file |
| 134 |
-- Waveform can overwrite instructional, but not vice versa |
| 135 |
elseif instructional[ext] and not info.filename_music then |
| 136 |
info.filename_music = music_dir .. file |
| 137 |
elseif ext == "TXT" and (file:sub(1, 4):upper() == "MIDI" or |
| 138 |
file:sub(1, 5):upper() == "NAMES") then |
| 139 |
-- If it Looks like the midi.txt or equiv, then remember it for later. |
| 140 |
midi_txt = file |
| 141 |
end |
| 142 |
end |
| 143 |
|
| 144 |
-- Enable music files and add them to the playlist. |
| 145 |
for _, info in pairs(music_array) do |
| 146 |
info.enabled = true |
| 147 |
self.background_playlist[#self.background_playlist + 1] = info |
| 148 |
end |
| 149 |
|
| 150 |
-- This is later. If we found a midi.txt, go through it and add the titles to the files we know |
| 151 |
if midi_txt then |
| 152 |
local data |
| 153 |
if music_dir then |
| 154 |
data = assert(GetFileData(music_dir .. midi_txt)) |
| 155 |
else |
| 156 |
data = assert(self.app.fs:readContents("Sound", "Midi", midi_txt)) |
| 157 |
end |
| 158 |
local i = 1 |
| 159 |
for file, title in data:gmatch("([^\r\n\26]+).-([^\r\n\26]+)") do |
| 160 |
local info = musicFileTable(file:match("^(.*)%.") or file) |
| 161 |
if next(info) ~= nil then |
| 162 |
info.title = title |
| 163 |
info.index = i |
| 164 |
i = i + 1 |
| 165 |
else |
| 166 |
print('Notice: Background track "'.. file ..'" named in list file, '.. |
| 167 |
'but it does not exist.') |
| 168 |
end |
| 169 |
end |
| 170 |
-- Sort by midi.txt order if there is one, or by filename. |
| 171 |
table.sort(self.background_playlist, function(a,b) return a.index < b.index end) |
| 172 |
else |
| 173 |
table.sort(self.background_playlist, function(a,b) return a.title < b.title end) |
| 174 |
end |
| 175 |
if #self.background_playlist == 0 and self.app.good_install_folder then |
| 176 |
print("Notice: Audio system loaded, but found no background tracks") |
| 177 |
self.has_bg_music = false |
| 178 |
else |
| 179 |
self.has_bg_music = true |
| 180 |
end |
| 181 |
|
| 182 |
self:initMidiPlayer() |
| 183 |
|
| 184 |
local status, err = SDL.audio.init( |
| 185 |
self.app.config.audio_frequency, |
| 186 |
self.app.config.audio_channels, |
| 187 |
self.app.config.audio_buffer_size, |
| 188 |
self.app:findSoundFont()) |
| 189 |
if not status then |
| 190 |
print("Notice: Audio system could not initialise (SDL error: " .. tostring(err) .. ")") |
| 191 |
self.not_loaded = true |
| 192 |
self.has_bg_music = false |
| 193 |
self.background_playlist = {} |
| 194 |
return |
| 195 |
end |
| 196 |
end |
| 197 |
|
| 198 |
function Audio:initMidiPlayer() |
| 199 |
if self.midi_player then |
| 200 |
self.midi_player:close() |
| 201 |
self.midi_player = nil |
| 202 |
end |
| 203 |
|
| 204 |
if TH.GetCompileOptions().midi_device and self.app.config.midi_api then |
| 205 |
self.midi_player = TH.midiPlayer( |
| 206 |
self.app.config.midi_api, |
| 207 |
self.app.config.midi_port, |
| 208 |
self.app.config.midi_sysex_master_volume) |
| 209 |
end |
| 210 |
end |
| 211 |
|
| 212 |
function Audio:getMidiApiList() |
| 213 |
return TH.midiPlayer.getAvailableApis() |
| 214 |
end |
| 215 |
|
| 216 |
function Audio:getMidiPortList() |
| 217 |
if self.midi_player then |
| 218 |
return self.midi_player:portList() |
| 219 |
else |
| 220 |
return {} |
| 221 |
end |
| 222 |
end |
| 223 |
|
| 224 |
function Audio:initSpeech(speech_file) |
| 225 |
if self.not_loaded then |
| 226 |
return |
| 227 |
end |
| 228 |
|
| 229 |
local function load_sound_file(file) |
| 230 |
return self.app.fs:readContents("Sound", "Data", file) |
| 231 |
end |
| 232 |
|
| 233 |
speech_file = speech_file or "Sound-0.dat" |
| 234 |
if self.speech_file_name == speech_file then |
| 235 |
return |
| 236 |
end |
| 237 |
local archive_data, err = load_sound_file(speech_file) |
| 238 |
|
| 239 |
-- If sound file not found and language chosen is not English, |
| 240 |
-- maybe we can have more chance loading English sounds |
| 241 |
if not archive_data and speech_file ~= "Sound-0.dat" and self.app.good_install_folder then |
| 242 |
if self.speech_file_name == "Sound-0.dat" then |
| 243 |
return |
| 244 |
end |
| 245 |
print("Notice: Attempt to load English sounds as no SOUND/DATA/" .. speech_file .. " file found") |
| 246 |
speech_file = "Sound-0.dat" |
| 247 |
archive_data = load_sound_file(speech_file) |
| 248 |
end |
| 249 |
|
| 250 |
self.sound_archive = nil |
| 251 |
self.sound_fx = nil |
| 252 |
self.speech_file_name = nil |
| 253 |
|
| 254 |
if not archive_data then |
| 255 |
if self.app.good_install_folder then |
| 256 |
print("Notice: No sound effects as no SOUND/DATA/".. speech_file .. |
| 257 |
" file could be found / opened.") |
| 258 |
print("The reported error was: ".. err) |
| 259 |
end |
| 260 |
else |
| 261 |
if archive_data:sub(1, 3) == "RNC" then |
| 262 |
archive_data = assert(rnc.decompress(archive_data)) |
| 263 |
end |
| 264 |
self.sound_archive = TH.soundArchive() |
| 265 |
if not self.sound_archive:load(archive_data) then |
| 266 |
print("Notice: No sound effects as SOUND/DATA/" .. speech_file .. " could not be loaded") |
| 267 |
if #self.background_playlist == 0 then |
| 268 |
self.not_loaded = true |
| 269 |
end |
| 270 |
else |
| 271 |
self.speech_file_name = speech_file |
| 272 |
self.sound_fx = TH.soundEffects() |
| 273 |
self.sound_fx:setSoundArchive(self.sound_archive) |
| 274 |
self:setSoundStage() |
| 275 |
--self:dumpSoundArchive[[E:\CPP\2K8\CorsixTH\DataRaw\Sound\]] |
| 276 |
end |
| 277 |
end |
| 278 |
end |
| 279 |
|
| 280 |
--! Set the visual area for sound effects playback |
| 281 |
function Audio:setSoundStage() |
| 282 |
if self.sound_fx then |
| 283 |
local w, h = self.app.config.width / 2, self.app.config.height / 2 |
| 284 |
self.sound_fx:setCamera(math.floor(w), math.floor(h), math.floor((w^2 + h^2)^0.5)) |
| 285 |
end |
| 286 |
end |
| 287 |
|
| 288 |
function Audio:dumpSoundArchive(out_dir) |
| 289 |
local info,warning = io.open(out_dir .. "info.csv", "w") |
| 290 |
|
| 291 |
if info == nil then |
| 292 |
print("Error: Audio dump failed because info.csv couldn't be created and/or opened in the dump directory:" .. out_dir) |
| 293 |
print(warning) |
| 294 |
return |
| 295 |
end |
| 296 |
|
| 297 |
for i = 1, #self.sound_archive - 1 do |
| 298 |
local filename = self.sound_archive:getFilename(i) |
| 299 |
info:write(i, ",", filename, ",", self.sound_archive:getDuration(i), ",\n") |
| 300 |
local file = io.open(out_dir .. i .. "_" .. filename, "wb") |
| 301 |
file:write(self.sound_archive:getFileData(i)) |
| 302 |
file:close() |
| 303 |
print("".. i .. "/" .. #self.sound_archive - 1) |
| 304 |
end |
| 305 |
info:close() |
| 306 |
print("Sounds dumped to: " .. out_dir) |
| 307 |
end |
| 308 |
|
| 309 |
local wilcard_cache = permanent "audio_wildcard_cache" {} |
| 310 |
|
| 311 |
|
| 312 |
--! Play a sound from the sound archive |
| 313 |
--!param name (string) The name of the sound to be played. Can include |
| 314 |
-- wildcards (*). |
| 315 |
--!param where (Entity) The entity that is the source of the sound, or nil if |
| 316 |
-- the audio is not positional. |
| 317 |
--!param is_announcement (boolean) Whether the sound is an announcement |
| 318 |
-- (affects volume). |
| 319 |
--!param played_callback (function) A function to be called when the sound has |
| 320 |
-- finished playing. Can be nil. |
| 321 |
--!param played_callback_delay (integer) An optional delay in milliseconds |
| 322 |
-- before the played_callback is called. |
| 323 |
--!param loops (integer) number of times to play the audio. -1 for infinite. |
| 324 |
--!return (table) A `sound` table for passing into functions that act on the |
| 325 |
-- playing sound. The fields are an implementation detail that should not be |
| 326 |
-- used outside of the Audio class. |
| 327 |
function Audio:playSound(name, where, is_announcement, played_callback, played_callback_delay, loops) |
| 328 |
local sound_fx = self.sound_fx |
| 329 |
if sound_fx then |
| 330 |
name = self:resolveFilenameWildcard(name) |
| 331 |
local volume = is_announcement and self.app.config.announcement_volume or self.app.config.sound_volume |
| 332 |
local x, y |
| 333 |
local played_callback_id = nil |
| 334 |
if played_callback then |
| 335 |
played_callback_id = self.unused_played_callback_id |
| 336 |
self.unused_played_callback_id = self.unused_played_callback_id + 1 |
| 337 |
self.played_sound_callbacks[tostring(played_callback_id)] = played_callback |
| 338 |
end |
| 339 |
if where then |
| 340 |
x, y = Map:WorldToScreen(where.tile_x, where.tile_y) |
| 341 |
local dx, dy = where.th:getPosition() |
| 342 |
local ui = self.app.ui |
| 343 |
x = x + dx - ui.screen_offset_x |
| 344 |
y = y + dy - ui.screen_offset_y |
| 345 |
end |
| 346 |
local handle, warning = sound_fx:play(name, volume, x, y, played_callback_id, played_callback_delay, loops) |
| 347 |
|
| 348 |
if warning then |
| 349 |
-- Indicates something happened |
| 350 |
self.app.world:gameLog("Audio:playSound - Warning: " .. warning) |
| 351 |
end |
| 352 |
|
| 353 |
return { handle = handle, played_callback_id = played_callback_id } |
| 354 |
end |
| 355 |
end |
| 356 |
|
| 357 |
--! Pause or unpause a playing sound |
| 358 |
--!param sound (table) The `sound` table returned by `Audio:playSound`. |
| 359 |
function Audio:togglePauseSound(sound) |
| 360 |
local sound_fx = self.sound_fx |
| 361 |
if not sound_fx then |
| 362 |
return |
| 363 |
end |
| 364 |
|
| 365 |
if sound and sound.handle then |
| 366 |
sound_fx:togglePause(sound.handle, sound.played_callback_id) |
| 367 |
end |
| 368 |
end |
| 369 |
|
| 370 |
--! Stop a playing sound. |
| 371 |
-- A stopped sound is destroyed and cannot be resumed. The callback will be not |
| 372 |
-- be called for the stopped sound. |
| 373 |
--!param sound (table) The `sound` table returned by `Audio:playSound`. |
| 374 |
function Audio:stopSound(sound) |
| 375 |
local sound_fx = self.sound_fx |
| 376 |
if not sound_fx then |
| 377 |
return |
| 378 |
end |
| 379 |
|
| 380 |
if sound and sound.handle then |
| 381 |
sound_fx:stop(sound.handle, sound.played_callback_id) |
| 382 |
end |
| 383 |
end |
| 384 |
|
| 385 |
--! Determine if a given sound is still playing |
| 386 |
--!param sound (table) The `sound` table returned by `Audio:playSound`. |
| 387 |
function Audio:isPlaying(sound) |
| 388 |
local sound_fx = self.sound_fx |
| 389 |
if not sound_fx then |
| 390 |
return |
| 391 |
end |
| 392 |
|
| 393 |
if sound and sound.handle then |
| 394 |
return sound_fx:isPlaying(sound.handle) |
| 395 |
end |
| 396 |
return false |
| 397 |
end |
| 398 |
|
| 399 |
function Audio:cacheSoundFilenamesAssociatedWithName(name) |
| 400 |
local list = wilcard_cache[name] |
| 401 |
if not list then |
| 402 |
local filename |
| 403 |
list = {} |
| 404 |
wilcard_cache[name] = list |
| 405 |
local pattern = ("^" .. name:gsub("%*",".*") .. "$"):upper() |
| 406 |
for i = 1, #self.sound_archive - 1 do |
| 407 |
filename = self.sound_archive:getFilename(i):upper() |
| 408 |
if filename:find(pattern) then |
| 409 |
list[#list + 1] = filename |
| 410 |
end |
| 411 |
end |
| 412 |
end |
| 413 |
return list |
| 414 |
end |
| 415 |
|
| 416 |
--! Resolves sound filename containing wildcard character from its associated variants |
| 417 |
--!param name (string) Filename containing wildcard character to resolve |
| 418 |
function Audio:resolveFilenameWildcard(name) |
| 419 |
if name:find("*") then |
| 420 |
local list = self:cacheSoundFilenamesAssociatedWithName(name) |
| 421 |
name = list[1] and list[math.random(1, #list)] or name |
| 422 |
end |
| 423 |
return name |
| 424 |
end |
| 425 |
|
| 426 |
--[[ |
| 427 |
This function's integer array parameters for the min and max silence lengths should provide lengths |
| 428 |
for this game's different speeds, indexed as follows: |
| 429 |
[1] Slowest [2] Slow [3] Normal [4] Fast [5] Maximum |
| 430 |
|
| 431 |
!param min_silence_lengths (integer array) The desired minimum silence lengths for this game's different speeds. |
| 432 |
!param max_silence_lengths (integer array) The desired maximum silence lengths for this game's different speeds. |
| 433 |
!param num_silences (integer) How many silence lengths should be in the returned table of generated lengths. |
| 434 |
!return (table) A table of randomly ordered integers for the generated silence lengths. |
| 435 |
--]] |
| 436 |
local function getSilenceLengths(min_silence_lengths, max_silence_lengths, num_silences) |
| 437 |
local min_silence = min_silence_lengths[TheApp.world.tick_rate] |
| 438 |
local max_silence = max_silence_lengths[TheApp.world.tick_rate] |
| 439 |
|
| 440 |
local silences = {} |
| 441 |
if min_silence == max_silence then |
| 442 |
silences[1] = min_silence |
| 443 |
else |
| 444 |
for i = 1, num_silences do |
| 445 |
silences[i] = math.random(min_silence, max_silence) |
| 446 |
end |
| 447 |
end |
| 448 |
|
| 449 |
return silences |
| 450 |
end |
| 451 |
|
| 452 |
--[[ |
| 453 |
Plays related sounds at an entity in a random sequence, with random length silences between the sounds. |
| 454 |
|
| 455 |
This function's integer array parameters for the min and max silence lengths should provide lengths |
| 456 |
for this game's different speeds, indexed as follows: |
| 457 |
[1] Slowest [2] Slow [3] Normal [4] Fast [5] Maximum Speed |
| 458 |
|
| 459 |
!param names (string) A name pattern for the sequence of related sounds to be played for example: LAVA00*.wav |
| 460 |
!param entity : Where the sounds will be played at, the player won't hear the sounds being played at the entity |
| 461 |
when it isn't in their view. |
| 462 |
!param min_silence_lengths (integer array) The desired minimum silence lengths for this game's different speeds. |
| 463 |
!param max_silence_lengths (integer array) The desired maximum silence lengths for this game's different speeds. |
| 464 |
!param num_silences (integer) How many different silence lengths should be used, this can be a nil parameter. |
| 465 |
--]] |
| 466 |
function Audio:playEntitySounds(names, entity, min_silence_lengths, max_silence_lengths, num_silences) |
| 467 |
if self.sound_fx then |
| 468 |
self:cacheSoundFilenamesAssociatedWithName(names) |
| 469 |
local silences = getSilenceLengths(min_silence_lengths, max_silence_lengths, num_silences) |
| 470 |
self:entitySoundsHandler(wilcard_cache[names], entity, silences, 1) |
| 471 |
end |
| 472 |
end |
| 473 |
|
| 474 |
local function canSoundsBePlayed() |
| 475 |
return TheApp.config.play_sounds and TheApp.world and not TheApp.world:isPaused() |
| 476 |
end |
| 477 |
|
| 478 |
--[[ |
| 479 |
Called by the above function. |
| 480 |
|
| 481 |
This function's integer array parameters for the min and max silence lengths should provide lengths |
| 482 |
for this game's different speeds, indexed as follows: |
| 483 |
[1] Slowest [2] Slow [3] Normal [4] Fast [5] Maximum Speed |
| 484 |
|
| 485 |
!param sounds (string) A name pattern for the sequence of related sounds to be played for example: LAVA00*.wav |
| 486 |
!param entity : Where the sounds will be played at, the player won't hear the sounds being played at the entity |
| 487 |
when it isn't in their view. |
| 488 |
!param silences (integer array) the different pause durations to be used between the played sounds. |
| 489 |
!param silences_pointer (integer) the index for the pause duration which should be used after this call's sound has been played. |
| 490 |
--]] |
| 491 |
function Audio:entitySoundsHandler(sounds, entity, silences, silences_pointer) |
| 492 |
if entity.playing_sounds_in_random_sequence then |
| 493 |
local sound_played_callback = function() |
| 494 |
self:entitySoundsHandler(sounds, entity, silences, silences_pointer) |
| 495 |
end |
| 496 |
|
| 497 |
if canSoundsBePlayed() then |
| 498 |
local x, y = Map:WorldToScreen(entity.tile_x, entity.tile_y) |
| 499 |
local dx, dy = entity.th:getPosition() |
| 500 |
x = x + dx - self.app.ui.screen_offset_x |
| 501 |
y = y + dy - self.app.ui.screen_offset_y |
| 502 |
|
| 503 |
self.played_sound_callbacks[tostring(self.unused_played_callback_id)] = sound_played_callback |
| 504 |
self.sound_fx:play(sounds[math.random(1,#sounds)], self.app.config.sound_volume, |
| 505 |
x, y, self.unused_played_callback_id, silences_pointer) |
| 506 |
|
| 507 |
self.unused_played_callback_id = self.unused_played_callback_id + 1 |
| 508 |
if #silences > 1 then |
| 509 |
silences_pointer = (silences_pointer % #silences) + 1 |
| 510 |
end |
| 511 |
--If the sound can't be played now: |
| 512 |
else |
| 513 |
self.entities_waiting_for_sound_to_be_enabled[entity] = sound_played_callback |
| 514 |
entity:setWaitingForSoundEffectsToBeTurnedOn(true) |
| 515 |
end |
| 516 |
else |
| 517 |
if self.entities_waiting_for_sound_to_be_enabled[entity] then |
| 518 |
self.entities_waiting_for_sound_to_be_enabled[entity] = nil |
| 519 |
end |
| 520 |
end |
| 521 |
end |
| 522 |
|
| 523 |
function Audio:onEndPause() |
| 524 |
if TheApp.config.play_sounds then |
| 525 |
self:tellInterestedEntitiesTheyCanNowPlaySounds() |
| 526 |
end |
| 527 |
end |
| 528 |
|
| 529 |
function Audio:onSoundPlayed(played_callbacks_id) |
| 530 |
if TheApp.world ~= nil then |
| 531 |
if self.played_sound_callbacks[tostring(played_callbacks_id)] then |
| 532 |
self.played_sound_callbacks[tostring(played_callbacks_id)]() |
| 533 |
self.played_sound_callbacks[tostring(played_callbacks_id)] = nil |
| 534 |
end |
| 535 |
end |
| 536 |
end |
| 537 |
|
| 538 |
--! Returns whether the given sound (either a string or a number) |
| 539 |
--! exists in the sound archive |
| 540 |
--!param sound The sound to look for, either a string (name) or a |
| 541 |
-- number (position in the list of sounds) |
| 542 |
function Audio:soundExists(sound) |
| 543 |
if self.sound_archive then |
| 544 |
return self.sound_archive:soundExists(sound) |
| 545 |
else |
| 546 |
return false |
| 547 |
end |
| 548 |
end |
| 549 |
|
| 550 |
function Audio:playRandomBackgroundTrack() |
| 551 |
if self.not_loaded or #self.background_playlist == 0 then |
| 552 |
return |
| 553 |
end |
| 554 |
local enabled = {} |
| 555 |
for i, info in ipairs(self.background_playlist) do |
| 556 |
if info.enabled then |
| 557 |
enabled[#enabled + 1] = i |
| 558 |
end |
| 559 |
end |
| 560 |
if not enabled[1] then |
| 561 |
return |
| 562 |
end |
| 563 |
local index = enabled[math.random(1, #enabled)] |
| 564 |
self:playBackgroundTrack(index) |
| 565 |
end |
| 566 |
|
| 567 |
function Audio:findIndexOfCurrentTrack() |
| 568 |
for i, info in ipairs(self.background_playlist) do |
| 569 |
if info.music == self.background_music then |
| 570 |
return i |
| 571 |
end |
| 572 |
end |
| 573 |
|
| 574 |
return 1 |
| 575 |
end |
| 576 |
|
| 577 |
function Audio:playNextOrPreviousBackgroundTrack(direction) |
| 578 |
if self.not_loaded or #self.background_playlist == 0 then |
| 579 |
return |
| 580 |
end |
| 581 |
|
| 582 |
if not self.background_music then |
| 583 |
self:playRandomBackgroundTrack() |
| 584 |
return |
| 585 |
end |
| 586 |
|
| 587 |
local index = self:findIndexOfCurrentTrack() |
| 588 |
|
| 589 |
-- Find next/previous enabled track |
| 590 |
for i = 1, #self.background_playlist do |
| 591 |
local next_index = ((index + direction * i - 1) % #self.background_playlist) + 1 |
| 592 |
if self.background_playlist[next_index].enabled then |
| 593 |
self:playBackgroundTrack(next_index) |
| 594 |
return |
| 595 |
end |
| 596 |
end |
| 597 |
end |
| 598 |
|
| 599 |
function Audio:playNextBackgroundTrack() |
| 600 |
self:playNextOrPreviousBackgroundTrack(1) |
| 601 |
end |
| 602 |
|
| 603 |
function Audio:playPreviousBackgroundTrack() |
| 604 |
self:playNextOrPreviousBackgroundTrack(-1) |
| 605 |
end |
| 606 |
|
| 607 |
function Audio:isPlayingWithMidiPlayer() |
| 608 |
return self.midi_player and type(self.background_music) == 'number' |
| 609 |
end |
| 610 |
|
| 611 |
--! Pauses or unpauses background music depending on the current state. |
| 612 |
--! Returns whether music is currently paused or not after the call. |
| 613 |
--! If nil is returned music might either be playing or completely stopped. |
| 614 |
function Audio:pauseBackgroundTrack() |
| 615 |
assert(self.background_music, "Trying to pause music while music is stopped") |
| 616 |
|
| 617 |
local use_midi_player = self:isPlayingWithMidiPlayer() |
| 618 |
local status |
| 619 |
if self.background_paused then |
| 620 |
self.background_paused = nil |
| 621 |
if use_midi_player then |
| 622 |
self.midi_player:resume() |
| 623 |
status = true |
| 624 |
else |
| 625 |
status = SDL.audio.resumeMusic() |
| 626 |
end |
| 627 |
else |
| 628 |
self.background_paused = true |
| 629 |
if use_midi_player then |
| 630 |
self.midi_player:pause() |
| 631 |
status = true |
| 632 |
else |
| 633 |
status = SDL.audio.pauseMusic() |
| 634 |
end |
| 635 |
end |
| 636 |
|
| 637 |
-- NB: Explicit false check, as old C side returned nil in all cases |
| 638 |
if status == false then |
| 639 |
-- SDL doesn't seem to support pausing/resuming for this format/driver, |
| 640 |
-- so just stop the music instead. |
| 641 |
self:stopBackgroundTrack() |
| 642 |
self.background_paused = nil |
| 643 |
elseif not use_midi_player then |
| 644 |
-- SDL can also be odd and report music as paused even though it is still |
| 645 |
-- playing. If it really is paused, then there is no harm in muting it. |
| 646 |
-- If it wasn't really paused, then muting it is the next best thing that |
| 647 |
-- we can do (even though it'll continue playing). |
| 648 |
if self.background_paused then |
| 649 |
self.old_bg_music_volume = self.app.config.music_volume |
| 650 |
SDL.audio.setMusicVolume(0) |
| 651 |
else |
| 652 |
self.app.config.music_volume = self.old_bg_music_volume |
| 653 |
SDL.audio.setMusicVolume(self.old_bg_music_volume) |
| 654 |
self.old_bg_music_volume = nil |
| 655 |
end |
| 656 |
end |
| 657 |
self:notifyJukebox() |
| 658 |
return self.background_paused |
| 659 |
end |
| 660 |
|
| 661 |
--! Stops playing background music for the time being. |
| 662 |
--! Does not affect the configuration setting play_music. |
| 663 |
function Audio:stopBackgroundTrack() |
| 664 |
if self.background_paused then |
| 665 |
-- Resume first in order to clear the saved volume. |
| 666 |
self:pauseBackgroundTrack() |
| 667 |
end |
| 668 |
SDL.audio.stopMusic() |
| 669 |
if self.midi_player then |
| 670 |
self.midi_player:stop() |
| 671 |
end |
| 672 |
self.background_music = nil |
| 673 |
|
| 674 |
self:notifyJukebox() |
| 675 |
end |
| 676 |
|
| 677 |
function Audio:getFileData(index) |
| 678 |
local info = self.background_playlist[index] |
| 679 |
assert(info, "Index not valid") |
| 680 |
local data |
| 681 |
if info.filename_music then |
| 682 |
data = assert(GetFileData(info.filename_music)) |
| 683 |
else |
| 684 |
data = assert(self.app.fs:readContents(info.filename)) |
| 685 |
end |
| 686 |
if data:sub(1, 3) == "RNC" then |
| 687 |
data = assert(rnc.decompress(data)) |
| 688 |
end |
| 689 |
return data |
| 690 |
end |
| 691 |
|
| 692 |
--! Plays a given background track. |
| 693 |
--! Playback will only start if the configuration says it's ok. (play_music = true) |
| 694 |
--!param index Index of the track to play in the playlist. |
| 695 |
function Audio:playBackgroundTrack(index) |
| 696 |
local info = self.background_playlist[index] |
| 697 |
assert(info, "Index not valid") |
| 698 |
if self.app.config.play_music then |
| 699 |
local music = info.music |
| 700 |
if not music or type(music) == 'number' then |
| 701 |
local data = self:getFileData(index) |
| 702 |
if info.is_xmi then |
| 703 |
if self.midi_player then |
| 704 |
self.midi_player:setVolume(self.app.config.music_volume) |
| 705 |
self.midi_player:playXmi(data) |
| 706 |
|
| 707 |
-- info.music has to be equal to background_music and both values need |
| 708 |
-- to be truthy and unique for the jukebox to detect the track. Using |
| 709 |
-- the index is just a convenient way to achieve this. |
| 710 |
self.background_music = index |
| 711 |
info.music = index |
| 712 |
self:notifyJukebox() |
| 713 |
return |
| 714 |
end |
| 715 |
data = SDL.audio.transcodeXmiToMid(data) |
| 716 |
end |
| 717 |
-- Loading of music files can incur a slight pause, which is why it is |
| 718 |
-- done asynchronously. |
| 719 |
-- Someone might want to stop the player from |
| 720 |
-- starting to play once it's loaded though. |
| 721 |
self.load_music = true |
| 722 |
SDL.audio.loadMusicAsync(data, function(music_data, err) |
| 723 |
|
| 724 |
if music_data == nil then |
| 725 |
info.enabled = false |
| 726 |
local name, msg = (info.filename_music or info.filename) |
| 727 |
if not self.warned and TheApp.ui then -- Warn once per session |
| 728 |
TheApp.ui:addWindow(UIInformation(TheApp.ui, {_S.errors.music})) |
| 729 |
self.warned = true |
| 730 |
end |
| 731 |
if err == "No SoundFonts have been requested" then |
| 732 |
msg = "Required soundfont is not found, please download one. A suitable soundfont is linked from the CorsixTH wiki." |
| 733 |
elseif err == "XMP: Unrecognized file format" or err == "ModPlug_Load failed" then |
| 734 |
msg = "Music format not supported for file " .. name |
| 735 |
else |
| 736 |
msg = "Could not load music file " .. name .. ". Error: " .. err |
| 737 |
end |
| 738 |
if self.app.world then self.app.world:gameLog(msg) end |
| 739 |
if not self.app.world or not self.app.config.debug then print(msg) end |
| 740 |
else |
| 741 |
info.music = music_data |
| 742 |
-- Do we still want it to play? |
| 743 |
if self.load_music then |
| 744 |
return self:playBackgroundTrack(index) |
| 745 |
end |
| 746 |
end |
| 747 |
end) |
| 748 |
return |
| 749 |
end |
| 750 |
assert(SDL.audio.playMusic(music)) |
| 751 |
SDL.audio.setMusicVolume(self.app.config.music_volume) |
| 752 |
self.background_music = music |
| 753 |
|
| 754 |
self:notifyJukebox() |
| 755 |
end |
| 756 |
end |
| 757 |
|
| 758 |
function Audio:onMusicOver() |
| 759 |
if self.not_loaded or #self.background_playlist == 0 or self.background_music == nil then |
| 760 |
return |
| 761 |
end |
| 762 |
self:playNextBackgroundTrack() |
| 763 |
end |
| 764 |
|
| 765 |
function Audio:setBackgroundVolume(volume) |
| 766 |
if self.midi_player then |
| 767 |
self.app.config.music_volume = volume |
| 768 |
self.midi_player:setVolume(volume) |
| 769 |
end |
| 770 |
if self.background_paused then |
| 771 |
self.old_bg_music_volume = volume |
| 772 |
else |
| 773 |
self.app.config.music_volume = volume |
| 774 |
SDL.audio.setMusicVolume(volume) |
| 775 |
end |
| 776 |
end |
| 777 |
|
| 778 |
function Audio:setSoundVolume(volume) |
| 779 |
self.app.config.sound_volume = volume |
| 780 |
if self.sound_fx then |
| 781 |
-- Since some sounds are played automatically (using computers etc) |
| 782 |
-- we need to set a value on C level too. |
| 783 |
self.sound_fx:setSoundVolume(volume) |
| 784 |
end |
| 785 |
end |
| 786 |
|
| 787 |
function Audio:playSoundEffects(play_effects) |
| 788 |
self.app.config.play_sounds = play_effects |
| 789 |
if self.sound_fx then |
| 790 |
-- As above. |
| 791 |
self.sound_fx:setSoundEffectsOn(play_effects) |
| 792 |
end |
| 793 |
|
| 794 |
if canSoundsBePlayed() then |
| 795 |
self:tellInterestedEntitiesTheyCanNowPlaySounds() |
| 796 |
end |
| 797 |
end |
| 798 |
|
| 799 |
function Audio:tellInterestedEntitiesTheyCanNowPlaySounds() |
| 800 |
if not isTableEmpty(self.entities_waiting_for_sound_to_be_enabled) then |
| 801 |
for entity, callback in pairs(self.entities_waiting_for_sound_to_be_enabled) do |
| 802 |
callback() |
| 803 |
self.entities_waiting_for_sound_to_be_enabled[entity] = nil |
| 804 |
end |
| 805 |
end |
| 806 |
end |
| 807 |
|
| 808 |
function Audio:entityNoLongerWaitingForSoundsToBeTurnedOn(entity) |
| 809 |
self.entities_waiting_for_sound_to_be_enabled[entity] = nil |
| 810 |
end |
| 811 |
|
| 812 |
function Audio:setAnnouncementVolume(volume) |
| 813 |
self.app.config.announcement_volume = volume |
| 814 |
end |
| 815 |
|
| 816 |
-- search for jukebox and notify it to update its play button |
| 817 |
function Audio:notifyJukebox() |
| 818 |
local jukebox = TheApp.ui and TheApp.ui:getWindow(UIJukebox) |
| 819 |
if jukebox then |
| 820 |
jukebox:updatePlayButton() |
| 821 |
end |
| 822 |
end |
| 823 |
|
| 824 |
function Audio:reserveChannel() |
| 825 |
if self.sound_fx then |
| 826 |
return self.sound_fx:reserveChannel() |
| 827 |
else |
| 828 |
return -1 |
| 829 |
end |
| 830 |
end |
| 831 |
|
| 832 |
function Audio:releaseChannel(channel) |
| 833 |
if self.sound_fx and channel > -1 then |
| 834 |
self.sound_fx:releaseChannel(channel) |
| 835 |
end |
| 836 |
end |
| 837 |
|
| 838 |
function Audio:destroy() |
| 839 |
self.has_bg_music = false |
| 840 |
self.not_loaded = not TheApp.config.audio |
| 841 |
self.speech_file_name = nil |
| 842 |
self.sound_fx = nil |
| 843 |
SDL.audio.destroy() |
| 844 |
end |