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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use smart_default::SmartDefault;
use crate::AudioGroupId;
#[derive(Debug, Serialize, Deserialize, Default, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Sound {
#[serde(flatten)]
pub common_data: crate::CommonData<ConstGmSound>,
pub compression: Compression,
pub volume: f64,
pub preload: bool,
pub bit_rate: BitRate,
pub sample_rate: SampleRate,
#[serde(rename = "type")]
pub output: Output,
pub bit_depth: BitDepth,
pub audio_group_id: AudioGroupId,
pub sound_file: String,
pub duration: f64,
pub parent: crate::ViewPath,
}
#[derive(Debug, Copy, SmartDefault, Deserialize_repr, Serialize_repr, PartialEq, Eq, Clone)]
#[repr(u8)]
pub enum Compression {
#[default]
Uncompressed,
Compressed,
UncompressedOnLoad,
CompressedStreamed,
}
#[derive(Debug, Copy, SmartDefault, Deserialize_repr, Serialize_repr, PartialEq, Eq, Clone)]
#[repr(u8)]
pub enum BitDepth {
#[default]
EightBit,
SixteenBit,
}
#[derive(Debug, Copy, Serialize, Deserialize, PartialEq, Eq, Clone)]
#[repr(transparent)]
#[serde(transparent)]
pub struct BitRate(u32);
impl BitRate {
pub fn new(bitrate: u32) -> Option<Self> {
if Self::is_valid_bitrate(bitrate) {
return None;
}
Some(Self(bitrate))
}
pub fn set_bitrate(&mut self, rate: u32) -> Result<(), InvalidBitRate> {
if Self::is_valid_bitrate(rate) {
self.0 = rate;
Ok(())
} else {
Err(InvalidBitRate)
}
}
pub fn bitrate(&self) -> u32 {
self.0
}
pub fn is_valid_bitrate(bitrate: u32) -> bool {
match bitrate {
0..=64 => {
if bitrate % 8 != 0 {
return false;
}
}
80..=160 => {
if bitrate % 16 != 0 {
return false;
}
}
192..=256 => {
if bitrate % 32 != 0 {
return false;
}
}
320 | 512 => {}
_ => {
return false;
}
}
true
}
}
impl Default for BitRate {
fn default() -> Self {
Self(128)
}
}
#[derive(Debug, Clone, Copy)]
pub struct InvalidBitRate;
impl std::fmt::Display for InvalidBitRate {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Invalid Bit Rate")
}
}
impl std::error::Error for InvalidBitRate {}
#[derive(Debug, Copy, Serialize, Deserialize, PartialEq, Eq, Clone)]
#[repr(transparent)]
#[serde(transparent)]
pub struct SampleRate(u32);
impl SampleRate {
pub fn new(bitrate: u32) -> Option<Self> {
if Self::is_valid_sample_rate(bitrate) {
return None;
}
Some(Self(bitrate))
}
pub fn is_valid_sample_rate(bitrate: u32) -> bool {
matches!(bitrate, 5512 | 11025 | 22050 | 32000 | 44100 | 48000)
}
pub fn set_sample_rate(&mut self, sample_rate: u32) -> Result<(), InvalidSampleRate> {
if Self::is_valid_sample_rate(sample_rate) {
self.0 = sample_rate;
Ok(())
} else {
Err(InvalidSampleRate)
}
}
pub fn sample_rate(&self) -> u32 {
self.0
}
pub fn valid_sample_rates() -> [u32; 6] {
[5512, 11025, 22050, 32000, 44100, 48000]
}
}
impl Default for SampleRate {
fn default() -> Self {
Self(128)
}
}
#[derive(Debug, Clone, Copy)]
pub struct InvalidSampleRate;
impl std::fmt::Display for InvalidSampleRate {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Invalid Sample Rate")
}
}
impl std::error::Error for InvalidSampleRate {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize_repr, Deserialize_repr, SmartDefault)]
#[repr(u8)]
pub enum Output {
#[default]
Mono,
Stereo,
ThreeDee,
}
#[derive(Debug, Copy, Serialize, Deserialize, SmartDefault, PartialEq, Eq, Clone)]
pub enum ConstGmSound {
#[serde(rename = "GMSound")]
#[default]
Const,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn serialize_deserialize() {
let input = r#"
{
"compression": 2,
"volume": 1.0,
"preload": false,
"bitRate": 128,
"sampleRate": 44100,
"type": 0,
"bitDepth": 1,
"audioGroupId": {
"name": "audiogroup_default",
"path": "audiogroups/audiogroup_default"
},
"soundFile": "snd_summer_ambiance_day.mp3",
"duration": 60.0583344,
"parent": {
"name": "Sounds",
"path": "folders/Sounds.yy"
},
"resourceVersion": "1.0",
"name": "snd_summer_ambiance_day",
"tags": [],
"resourceType": "GMSound"
}"#;
let _: Sound = serde_json::from_str(input).unwrap();
}
}