17 | function SoundMixer:new(customMt) |
18 | |
19 | local self = {} |
20 | setmetatable(self, customMt or SoundMixer_mt) |
21 | |
22 | g_messageCenter:subscribe(MessageType.GAME_STATE_CHANGED, self.onGameStateChanged, self) |
23 | |
24 | local xmlFilename = "dataS/soundMixer.xml" |
25 | local xmlFile = loadXMLFile("soundMixerXML", xmlFilename) |
26 | |
27 | self.volumeFactors = {} |
28 | for _, groupIndex in pairs(AudioGroup) do |
29 | self.volumeFactors[groupIndex] = 1 |
30 | end |
31 | |
32 | self.masterVolume = 1 |
33 | |
34 | self.gameStates = {} |
35 | |
36 | if xmlFile ~= nil and xmlFile ~= 0 then |
37 | local i = 0 |
38 | while true do |
39 | local key = string.format("soundMixer.gameState(%d)", i) |
40 | if not hasXMLProperty(xmlFile, key) then |
41 | break |
42 | end |
43 | |
44 | local gameStateName = getXMLString(xmlFile, key.."#name") |
45 | local gameStateIndex = g_gameStateManager:getGameStateIndexByName(gameStateName) |
46 | if gameStateIndex ~= nil then |
47 | local gameState = {} |
48 | gameState.audioGroups = {} |
49 | |
50 | local j = 0 |
51 | while true do |
52 | local audioGroupKey = string.format("%s.audioGroup(%d)", key, j) |
53 | if not hasXMLProperty(xmlFile, audioGroupKey) then |
54 | break |
55 | end |
56 | |
57 | local name = getXMLString(xmlFile, audioGroupKey.."#name") |
58 | local volume = getXMLFloat(xmlFile, audioGroupKey.."#volume") or 1.0 |
59 | local audioGroupIndex = AudioGroup.getAudioGroupIndexByName(name) |
60 | |
61 | if audioGroupIndex ~= nil then |
62 | local group = {} |
63 | group.index = audioGroupIndex |
64 | group.volume = volume |
65 | gameState.audioGroups[audioGroupIndex] = group |
66 | else |
67 | print(string.format("Warning: Audio-Group '%s' is not defined for audio-group '%s'!", tostring(name), key)) |
68 | end |
69 | |
70 | j = j + 1 |
71 | end |
72 | |
73 | self.gameStates[gameStateIndex] = gameState |
74 | else |
75 | print(string.format("Warning: Game-State '%s' is not defined for state '%s'!", tostring(gameStateName), key)) |
76 | end |
77 | |
78 | i = i + 1 |
79 | end |
80 | else |
81 | print("Error: SoundMixer could not load configuration file!") |
82 | end |
83 | |
84 | delete(xmlFile) |
85 | |
86 | self.volumes = {} |
87 | local gameState = self.gameStates[GameState.LOADING] |
88 | for _, groupIndex in ipairs(AudioGroup.groups) do |
89 | local volume = gameState.audioGroups[groupIndex].volume or 1 |
90 | self.volumes[groupIndex] = {volume = volume, listeners = {}} |
91 | setAudioGroupVolume(groupIndex, volume) |
92 | end |
93 | |
94 | return self |
95 | end |