source: OniSplit/Sound/OsbdXmlExporter.cs@ 1186

Last change on this file since 1186 was 1114, checked in by iritscen, 5 years ago

Adding OniSplit source code (v0.9.99.0). Many thanks to Neo for all his work over the years.

File size: 4.8 KB
RevLine 
[1114]1using System;
2using System.Xml;
3using Oni.Metadata;
4using Oni.Xml;
5
6namespace Oni.Sound
7{
8 internal sealed class OsbdXmlExporter : RawXmlExporter
9 {
10 private OsbdXmlExporter(BinaryReader reader, XmlWriter xml)
11 : base(reader, xml)
12 {
13 }
14
15 public static void Export(BinaryReader reader, XmlWriter xml)
16 {
17 var exporter = new OsbdXmlExporter(reader, xml);
18 exporter.Export();
19 }
20
21 private void Export()
22 {
23 int tag = Reader.ReadInt32();
24 int size = Reader.ReadInt32();
25 int version = Reader.ReadInt32();
26
27 if (version > 6)
28 throw new NotSupportedException("Sound version {0} is not supported");
29
30 switch (tag)
31 {
32 case SoundMetadata.OSAm:
33 Xml.WriteStartElement("AmbientSound");
34 ExportAmbient(version);
35 break;
36
37 case SoundMetadata.OSGr:
38 Xml.WriteStartElement("SoundGroup");
39 ExportGroup(version);
40 break;
41
42 case SoundMetadata.OSIm:
43 Xml.WriteStartElement("ImpulseSound");
44 ExportImpulse(version);
45 break;
46
47 default:
48 throw new NotSupportedException(string.Format("Unknown sound binary data tag {0:X}", tag));
49 }
50
51 Xml.WriteEndElement();
52 }
53
54 private void ExportGroup(int version)
55 {
56 if (version < 6)
57 {
58 float volume = 1.0f;
59 float pitch = 1.0f;
60 var flags = SoundMetadata.OSGrFlags.None;
61 int channelCount;
62 int permutationCount;
63
64 if (version >= 2)
65 volume = Reader.ReadSingle();
66
67 if (version >= 3)
68 pitch = Reader.ReadSingle();
69
70 channelCount = Reader.ReadInt32();
71 permutationCount = Reader.ReadInt32();
72
73 if (permutationCount >= 4)
74 flags |= SoundMetadata.OSGrFlags.PreventRepeat;
75
76 Xml.WriteElementString("Volume", XmlConvert.ToString(volume));
77 Xml.WriteElementString("Pitch", XmlConvert.ToString(pitch));
78 Xml.WriteStartElement("Flags");
79 Xml.WriteString(MetaEnum.ToString(flags));
80 Xml.WriteEndElement();
81 Xml.WriteElementString("NumberOfChannels", XmlConvert.ToString(channelCount));
82 Xml.WriteStartElement("Permutations");
83
84 for (int i = 0; i < permutationCount; i++)
85 {
86 Xml.WriteStartElement("Permutation");
87 SoundMetadata.osgrPermutation.Accept(this);
88 Xml.WriteEndElement();
89 }
90
91 Xml.WriteEndElement();
92 }
93 else
94 {
95 SoundMetadata.osgr6.Accept(this);
96 }
97 }
98
99 private void ExportAmbient(int version)
100 {
101 if (version <= 4)
102 {
103 SoundMetadata.osam4.Accept(this);
104 Xml.WriteElementString("Treshold", "3");
105 Xml.WriteElementString("MinOcclusion", "0");
106 }
107 else if (version <= 5)
108 {
109 SoundMetadata.osam5.Accept(this);
110 Xml.WriteElementString("MinOcclusion", "0");
111 }
112 else
113 {
114 SoundMetadata.osam6.Accept(this);
115 }
116 }
117
118 private void ExportImpulse(int version)
119 {
120 if (version <= 3)
121 {
122 SoundMetadata.osim3.Accept(this);
123 Xml.WriteStartElement("AlternateImpulse");
124 Xml.WriteElementString("Treshold", "0");
125 Xml.WriteStartElement("Impulse");
126 Xml.WriteString("");
127 Xml.WriteEndElement();
128 Xml.WriteEndElement();
129 Xml.WriteElementString("ImpactVelocity", "0");
130 Xml.WriteElementString("MinOcclusion", "0");
131 }
132 else if (version <= 4)
133 {
134 SoundMetadata.osim4.Accept(this);
135 Xml.WriteElementString("ImpactVelocity", "0");
136 Xml.WriteElementString("MinOcclusion", "0");
137 }
138 else if (version <= 5)
139 {
140 SoundMetadata.osim5.Accept(this);
141 Xml.WriteElementString("MinOcclusion", "0");
142 }
143 else
144 {
145 SoundMetadata.osim6.Accept(this);
146 }
147 }
148 }
149}
Note: See TracBrowser for help on using the repository browser.