vsqx 0.1.0

Serializer and deserializer for .vsqx and .vpr (VOCALOID 3/4/5 sequence) file.
Documentation
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! VOCALOID3 Editorから出力される.vsqx形式

use crate::Result;
use serde::Deserialize;

pub mod serializer;

/// VOCALOID 3用のVsqx構造体。
///
/// ## Caveats
/// `vsqx::Vsqx4`とバイナリ互換であることを前提にしているので、ここを変更する場合は`Vsqx4`の変更も必要。
/// もし何かが足りない場合には、
/// ```ignore
/// //...
/// #[serde(skip_serializing_if = "Option::is_none")]
/// pub hoge: Option<i64>,
/// //...
/// ```
/// のように、
/// * Optionで包む
/// * 空の場合のデシリアライズを行わない
///
/// ようにすればいい。
///
/// また、特定のバージョンで必要な情報を変更するときには、
/// `From`トレイトでサニタイズを行うように。
#[derive(Clone, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase", rename = "vsqx4")]
pub struct Vsqx3 {
    /* XML関連タグ */
    #[serde(rename = "xmlns", default = "_vsqx3_default_xmlns")]
    xmlns: String,
    #[serde(rename = "xmlns:xsi", default = "_vsqx3_default_xmlns_xsi")]
    xmlns_xsi: String,
    #[serde(
        rename = "xsi:schemaLocation",
        default = "_vsqx3_default_xsi_schema_location"
    )]
    xsi_schema_location: String,
    /// バージョン情報
    #[serde(default = "_vsqx3_default_version")]
    pub version: String,
    #[serde(default = "_vsqx3_default_vendor")]
    pub vender: String,
    #[serde(default, rename = "vVoiceTable")]
    pub voice_table: VoiceTable,
    pub mixer: Mixer,
    pub master_track: MasterTrack,
    pub vs_track: Vec<VsTrack>,
    pub se_track: SeTrack,
    pub karaoke_track: KaraokeTrack,
    #[serde(default)]
    pub aux: Vec<Aux>,
}

impl Vsqx3 {
    pub fn write<P: AsRef<std::path::Path>>(&self, path: P) -> Result<()> {
        use super::write_xml::WriteXml;
        use quick_xml::Writer;
        use std::fs::File;
        use std::io::BufWriter;

        let writer = File::create(path)?;
        let writer = BufWriter::new(writer);
        let mut writer = Writer::new(writer);
        writer.write(br#"<?xml version="1.0" encoding="UTF-8" standalone="no"?>"#)?;
        writer.write(b"\n")?;
        <Self as WriteXml>::tagged(self, &mut writer, b"vsq3")?;

        Ok(())
    }

    pub fn to_string(&self) -> Result<String> {
        use super::write_xml::WriteXml;
        use quick_xml::Writer;
        use std::io::Cursor;

        let mut writer = Writer::new(Cursor::new(Vec::new()));
        writer.write(br#"<?xml version="1.0" encoding="UTF-8" standalone="no"?>"#)?;
        writer.write(b"\n")?;
        <Self as WriteXml>::tagged(self, &mut writer, b"vsq3")?;

        let res = writer.into_inner().into_inner();

        Ok(String::from_utf8_lossy(&res).into())
    }

    pub fn open<P: AsRef<std::path::Path>>(path: P) -> Result<Self> {
        use std::fs::File;
        use std::io::BufReader;
        let file = File::open(path)?;
        let file = BufReader::new(file);

        Ok(quick_xml::de::from_reader(file)?)
    }
}

use std::str::FromStr;

impl FromStr for Vsqx3 {
    type Err = crate::Error;

    fn from_str(string: &str) -> Result<Self> {
        Ok(quick_xml::de::from_str(string)?)
    }
}

impl From<super::vsqx4::Vsqx4> for Vsqx3 {
    // ここでVsqxのサニタイズを行うこと。
    fn from(v4: super::vsqx4::Vsqx4) -> Self {
        assert_eq!(
            std::mem::size_of::<super::vsqx4::Vsqx4>(),
            std::mem::size_of::<Self>()
        );

        // バイナリ互換性を前提にしている
        let mut this: Self = unsafe { std::mem::transmute(v4) };

        this.xmlns = _vsqx3_default_xmlns();
        this.xmlns_xsi = _vsqx3_default_xmlns_xsi();
        this.version = _vsqx3_default_version();
        this.xsi_schema_location = _vsqx3_default_xsi_schema_location();

        this
    }
}

impl From<super::vpr::Vpr> for Vsqx3 {
    fn from(vpr: super::vpr::Vpr) -> Self {
        super::v5to4::convert_vpr_to_vsqx4(&vpr).into()
    }
}

#[derive(Clone, Deserialize, Debug, PartialEq, Default)]
pub struct VoiceTable {
    #[serde(rename = "vVoice")]
    pub voices: Vec<Voice>,
}

#[derive(Clone, Deserialize, Debug, PartialEq)]
pub struct Voice {
    #[serde(rename = "vBS")]
    bs: i64,
    #[serde(rename = "vPC")]
    pc: i64,
    #[serde(rename = "compID")]
    id: String,
    #[serde(rename = "vVoiceName")]
    name: String,
    #[serde(rename = "vVoiceParam")]
    parameters: VoiceParameters,
}

#[derive(Clone, Deserialize, Debug, PartialEq)]
pub struct VoiceParameters {
    #[serde(rename = "bre")]
    pub breathiness: i64,
    #[serde(rename = "bri")]
    pub brightness: i64,
    #[serde(rename = "cle")]
    pub clearness: i64,
    #[serde(rename = "gen")]
    pub gender: i64,
    #[serde(rename = "ope")]
    pub openness: i64,
}

#[derive(Clone, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Mixer {
    pub master_unit: MasterUnit,
    pub vs_unit: Vec<VsUnit>,
    pub se_unit: Vec<SeUnit>,
    pub karaoke_unit: Vec<KaraokeUnit>,
}

#[derive(Clone, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct MasterUnit {
    #[serde(rename = "outDev")]
    output_device: i64,
    #[serde(rename = "retLevel")]
    return_level: i64,
    #[serde(rename = "vol")]
    volume: i64,
}

#[derive(Clone, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct VsUnit {
    #[serde(rename = "vsTrackNo")]
    track_no: i64,
    #[serde(rename = "inGain")]
    input_gain: i64,
    #[serde(rename = "sendLevel")]
    send_level: i64,
    #[serde(rename = "sendEnable")]
    is_send_enabled: i64,
    #[serde(rename = "mute")]
    mute: i64,
    #[serde(rename = "solo")]
    solo: i64,
    #[serde(rename = "pan")]
    pan: i64,
    #[serde(rename = "vol")]
    volume: i64,
}

#[derive(Clone, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct SeUnit {
    #[serde(rename = "inGain")]
    input_gain: i64,
    #[serde(rename = "sendLevel")]
    send_level: i64,
    #[serde(rename = "sendEnable")]
    is_send_enabled: i64,
    #[serde(rename = "mute")]
    mute: i64,
    #[serde(rename = "solo")]
    solo: i64,
    #[serde(rename = "pan")]
    pan: i64,
    #[serde(rename = "vol")]
    volume: i64,
}

#[derive(Clone, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct KaraokeUnit {
    #[serde(rename = "inGain")]
    input_gain: i64,
    #[serde(rename = "mute")]
    mute: i64,
    #[serde(rename = "solo")]
    solo: i64,
    #[serde(rename = "vol")]
    volume: i64,
}

#[derive(Clone, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct MasterTrack {
    #[serde(rename = "seqName")]
    pub name: String,
    pub comment: String,
    pub resolution: i64,
    pub pre_measure: i64,
    #[serde(rename = "timeSig")]
    pub time_signatures: Vec<TimeSignature>,
    #[serde(rename = "tempo")]
    pub tempos: Vec<Tempo>,
}

#[derive(Clone, Deserialize, Debug, PartialEq)]
pub struct TimeSignature {
    #[serde(rename = "posMes")]
    pub position: i64,
    #[serde(rename = "nume")]
    pub numerator: i64,
    #[serde(rename = "denomi")]
    pub denominator: i64,
}

#[derive(Clone, Deserialize, Debug, PartialEq)]
pub struct Tempo {
    #[serde(rename = "posTick")]
    pub position: i64,
    #[serde(rename = "bpm")]
    pub value: i64,
}

#[derive(Clone, Deserialize, Debug, PartialEq)]
pub struct VsTrack {
    #[serde(rename = "vsTrackNo")]
    pub track_no: i64,
    #[serde(rename = "trackName")]
    pub name: String,
    pub comment: String,
    #[serde(rename = "musicalPart", default)]
    pub parts: Vec<VsPart>,
}

#[derive(Clone, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct VsPart {
    #[serde(rename = "posTick")]
    pub position: i64,
    #[serde(rename = "stylePlugin")]
    pub style_plugin: StylePlugin,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub play_time: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
    #[serde(rename = "partStyle")]
    pub style: Style,
    #[serde(rename = "singer")]
    pub singers: Vec<Singer>,
    #[serde(rename = "cc", default)]
    pub control_changes: Vec<ControlChange>,
    #[serde(rename = "note")]
    pub notes: Vec<Note>,
    #[serde(default)]
    pub plane: i64,
}

#[derive(Clone, Deserialize, Debug, PartialEq)]
pub struct ControlChange<T = i64> {
    pub id: String,
    pub pos: i64,
    pub value: T,
}

#[derive(Clone, Deserialize, Debug, PartialEq)]
pub struct StylePlugin {
    #[serde(rename = "stylePluginID")]
    pub id: String,
    #[serde(rename = "stylePluginName")]
    pub name: String,
    pub version: String,
}

#[derive(Clone, Deserialize, Debug, PartialEq)]
pub struct Singer {
    #[serde(rename = "posTick")]
    position: i64,
    #[serde(rename = "vBS")]
    bs: i64,
    #[serde(rename = "vPC")]
    pc: i64,
}

#[derive(Clone, Deserialize, Debug, PartialEq)]
pub struct Note {
    #[serde(rename = "posTick")]
    pub position: i64,
    #[serde(rename = "durTick")]
    pub duration: i64,
    #[serde(rename = "noteNum")]
    pub note_num: i64,
    #[serde(rename = "velocity")]
    pub velocity: i64,
    #[serde(rename = "lyric")]
    pub lyric: String,
    #[serde(rename = "phnms")]
    pub phoneme: String,
    #[serde(rename = "noteStyle")]
    pub style: Style,
}

#[derive(Clone, Deserialize, Debug, PartialEq)]
pub struct Style {
    #[serde(rename = "attr")]
    pub styles: Vec<StyleKey>,
}

#[derive(Clone, Deserialize, Debug, PartialEq)]
pub struct StyleKey {
    pub id: String,
    #[serde(rename = "$value")]
    pub value: i64,
}

#[derive(Clone, Deserialize, Debug, PartialEq)]
pub struct SeTrack {}

#[derive(Clone, Deserialize, Debug, PartialEq)]
pub struct KaraokeTrack {}

#[derive(Clone, Deserialize, Debug, PartialEq)]
pub struct Aux {
    #[serde(rename = "auxID")]
    id: String,
    content: String,
}

/* デフォルト値 */

fn _vsqx3_default_xmlns() -> String {
    "http://www.yamaha.co.jp/vocaloid/schema/vsq3/".into()
}

fn _vsqx3_default_xmlns_xsi() -> String {
    "http://www.w3.org/2001/XMLSchema-instance".into()
}

fn _vsqx3_default_xsi_schema_location() -> String {
    "http://www.yamaha.co.jp/vocaloid/schema/vsq3/ vsq3.xsd".into()
}

fn _vsqx3_default_version() -> String {
    "3.0.0.11".into()
}

fn _vsqx3_default_vendor() -> String {
    "Yamaha corporation".into()
}

#[test]
#[cfg(test)]
fn test_vsqx3_parse() {
    let vsqx3 = include_str!("../test/v3.vsqx");
    let _v: Vsqx3 = quick_xml::de::from_str(&vsqx3).unwrap();
}

#[test]
#[cfg(test)]
fn test_vsqx3_convert() {
    use super::vsqx4::Vsqx4;

    let vsqx4 = include_str!("../test/v4.vsqx");
    let v: Vsqx4 = quick_xml::de::from_str(&vsqx4).unwrap();
    let _v: Vsqx3 = v.into();
}