1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// SPDX-License-Identifier: GPL-3.0-only
use crate::prelude::*;
use crate::wad::chunk::gm_asset_list_chunk;
use crate::wad::elem::GMElement;
use crate::wad::elem::audio::Audio;
use crate::wad::elem::audio_group::AudioGroup;
use crate::wad::load::reader::DataReader;
use crate::wad::reference::GMRef;
use crate::wad::save::writer::DataWriter;
use crate::wad::version::GMVersion;
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Sounds {
pub elems: Vec<Option<Sound>>,
}
gm_asset_list_chunk!(SOND, Sounds, Sound);
impl GMElement for Sounds {
fn deserialize(reader: &mut DataReader) -> Result<Self> {
let elems: Vec<Option<Sound>> = reader.read_pointer_list_opt()?;
Ok(Self { elems })
}
fn serialize(&self, writer: &mut DataWriter) -> Result<()> {
writer.write_pointer_list_opt(&self.elems)?;
Ok(())
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Sound {
/// The name of the sound entry.
/// This name is used when referencing this entry from code.
pub name: GMRef<String>,
/// The flags of this sound.
/// This field is a bit unstable and may be changed in the future.
pub flags: SoundFlags,
/// The file format of the audio entry.
/// This includes the `.` from the file extension.
/// Possible values are:
/// - `.wav`
/// - `.mp3`
/// - `.ogg`
pub audio_type: GMRef<String>,
/// The original file name of the audio entry.
/// This is the full filename how it was loaded in the project.
/// This will be used if the sound effect is streamed from disk to find the
/// sound file.
///
/// This is used if the [`SoundFlags::EMBEDDED`] flag is not set.
pub file: GMRef<String>,
/// A pre-`GameMaker Studio` way of having certain effects on a sound
/// effect. Although the exact way this works is unknown, the following
/// values are possible:
/// - Chorus
/// - Echo
/// - Flanger
/// - Gargle
/// - Reverb
///
/// These can be combined with each other, apparently.
/// Discussion here: <https://discord.com/channels/566861759210586112/568950566122946580/957318910066196500>
pub effects: u32,
/// The volume the audio entry is played at.
///
/// Valid Range: `0.0` - `1.0`.
pub volume: f32,
/// **Unused, usually 0**
///
/// Maybe the pitch change of the audio entry (no idea where this comes from).
pub pitch: f32,
/// The audio group this audio entry belongs to.
/// These can only be used with the regular audio system.
/// This is used if the [`SoundFlags::REGULAR`] flag is set (always set for now).
/// For more information, see [`AudioGroup`].
pub audio_group: GMRef<AudioGroup>,
/// The reference to the [`Audio`] audio file.
/// This is used if the [`SoundFlags::EMBEDDED`] flag is set.
pub audio: GMRef<Audio>,
/// The precomputed length of the sound's audio data.
/// Introduced in GameMaker 2024.6.
/// DOCME: which unit?
pub audio_length: Option<f32>,
}
impl GMElement for Sound {
fn deserialize(reader: &mut DataReader) -> Result<Self> {
let name: GMRef<String> = reader.read_gm_string()?;
let flags = reader.read_u32()?;
let flags = SoundFlags::from_bits(flags)
.ok_or_else(|| format!("Invalid Sound Flags {flags:08X}"))?;
let audio_type: GMRef<String> = reader.read_gm_string()?;
let file: GMRef<String> = reader.read_gm_string()?;
let effects = reader.read_u32()?;
let volume = reader.read_f32()?;
let pitch = reader.read_f32()?;
let agrp = reader.version >= GMVersion::Wad14 && flags.contains(SoundFlags::REGULAR);
let audio_group: GMRef<AudioGroup> = if agrp {
reader.read_resource_by_id()?
} else {
let preload = reader.read_bool32().ctx("reading preload")?;
reader.assert_bool(preload, true, "Preload")?;
GMRef::none()
};
let audio: GMRef<Audio> = reader.read_resource_by_id()?;
let audio_length: Option<f32> = reader.deserialize_if_version(GMVersion::GM2024_6)?;
// // GMS1 asset compiler is stupid and keeps repeating the previous embedded audio id
// // if the current sound is external.
// if !flags.contains(SoundFlags::EMBEDDED) {
// audio = GMRef::none();
// }
Ok(Self {
name,
flags,
audio_type,
file,
effects,
volume,
pitch,
audio_group,
audio,
audio_length,
})
}
fn serialize(&self, writer: &mut DataWriter) -> Result<()> {
writer.write_gm_string(self.name)?;
writer.write_u32(self.flags.bits());
writer.write_gm_string(self.audio_type)?;
writer.write_gm_string(self.file)?;
writer.write_u32(self.effects);
writer.write_f32(self.volume);
writer.write_f32(self.pitch);
if writer.version() >= GMVersion::Wad14 && self.flags.contains(SoundFlags::REGULAR) {
writer.write_resource_id(self.audio_group);
} else {
writer.write_bool32(true); // Preload
}
writer.write_resource_id(self.audio);
writer.write_if_ver(&self.audio_length, "Audio Length", GMVersion::GM2024_6)?;
Ok(())
}
}
bitflags::bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SoundFlags: u32 {
const EMBEDDED = 0x1;
const COMPRESSED = 0x2;
const REGULAR = 0x64;
}
}