├── .gitattributes
├── .gitignore
├── LICENSE
├── README.md
├── WEMSharp.sln
├── WEMSharp
├── BitStream.cs
├── CRC32.cs
├── CodebookLibrary.cs
├── OggStream.cs
├── Packet.cs
├── Packet8.cs
├── Properties
│ └── AssemblyInfo.cs
├── WEMFile.cs
└── WEMSharp.csproj
└── WEMSharpTests
├── App.config
├── Program.cs
├── Properties
└── AssemblyInfo.cs
└── WEMSharpTests.csproj
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.suo
8 | *.user
9 | *.userosscache
10 | *.sln.docstates
11 |
12 | # User-specific files (MonoDevelop/Xamarin Studio)
13 | *.userprefs
14 |
15 | # Build results
16 | [Dd]ebug/
17 | [Dd]ebugPublic/
18 | [Rr]elease/
19 | [Rr]eleases/
20 | x64/
21 | x86/
22 | bld/
23 | [Bb]in/
24 | [Oo]bj/
25 | [Ll]og/
26 |
27 | # Visual Studio 2015 cache/options directory
28 | .vs/
29 | # Uncomment if you have tasks that create the project's static files in wwwroot
30 | #wwwroot/
31 |
32 | # MSTest test Results
33 | [Tt]est[Rr]esult*/
34 | [Bb]uild[Ll]og.*
35 |
36 | # NUNIT
37 | *.VisualState.xml
38 | TestResult.xml
39 |
40 | # Build Results of an ATL Project
41 | [Dd]ebugPS/
42 | [Rr]eleasePS/
43 | dlldata.c
44 |
45 | # Benchmark Results
46 | BenchmarkDotNet.Artifacts/
47 |
48 | # .NET Core
49 | project.lock.json
50 | project.fragment.lock.json
51 | artifacts/
52 | **/Properties/launchSettings.json
53 |
54 | *_i.c
55 | *_p.c
56 | *_i.h
57 | *.ilk
58 | *.meta
59 | *.obj
60 | *.pch
61 | *.pdb
62 | *.pgc
63 | *.pgd
64 | *.rsp
65 | *.sbr
66 | *.tlb
67 | *.tli
68 | *.tlh
69 | *.tmp
70 | *.tmp_proj
71 | *.log
72 | *.vspscc
73 | *.vssscc
74 | .builds
75 | *.pidb
76 | *.svclog
77 | *.scc
78 |
79 | # Chutzpah Test files
80 | _Chutzpah*
81 |
82 | # Visual C++ cache files
83 | ipch/
84 | *.aps
85 | *.ncb
86 | *.opendb
87 | *.opensdf
88 | *.sdf
89 | *.cachefile
90 | *.VC.db
91 | *.VC.VC.opendb
92 |
93 | # Visual Studio profiler
94 | *.psess
95 | *.vsp
96 | *.vspx
97 | *.sap
98 |
99 | # TFS 2012 Local Workspace
100 | $tf/
101 |
102 | # Guidance Automation Toolkit
103 | *.gpState
104 |
105 | # ReSharper is a .NET coding add-in
106 | _ReSharper*/
107 | *.[Rr]e[Ss]harper
108 | *.DotSettings.user
109 |
110 | # JustCode is a .NET coding add-in
111 | .JustCode
112 |
113 | # TeamCity is a build add-in
114 | _TeamCity*
115 |
116 | # DotCover is a Code Coverage Tool
117 | *.dotCover
118 |
119 | # AxoCover is a Code Coverage Tool
120 | .axoCover/*
121 | !.axoCover/settings.json
122 |
123 | # Visual Studio code coverage results
124 | *.coverage
125 | *.coveragexml
126 |
127 | # NCrunch
128 | _NCrunch_*
129 | .*crunch*.local.xml
130 | nCrunchTemp_*
131 |
132 | # MightyMoose
133 | *.mm.*
134 | AutoTest.Net/
135 |
136 | # Web workbench (sass)
137 | .sass-cache/
138 |
139 | # Installshield output folder
140 | [Ee]xpress/
141 |
142 | # DocProject is a documentation generator add-in
143 | DocProject/buildhelp/
144 | DocProject/Help/*.HxT
145 | DocProject/Help/*.HxC
146 | DocProject/Help/*.hhc
147 | DocProject/Help/*.hhk
148 | DocProject/Help/*.hhp
149 | DocProject/Help/Html2
150 | DocProject/Help/html
151 |
152 | # Click-Once directory
153 | publish/
154 |
155 | # Publish Web Output
156 | *.[Pp]ublish.xml
157 | *.azurePubxml
158 | # Note: Comment the next line if you want to checkin your web deploy settings,
159 | # but database connection strings (with potential passwords) will be unencrypted
160 | *.pubxml
161 | *.publishproj
162 |
163 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
164 | # checkin your Azure Web App publish settings, but sensitive information contained
165 | # in these scripts will be unencrypted
166 | PublishScripts/
167 |
168 | # NuGet Packages
169 | *.nupkg
170 | # The packages folder can be ignored because of Package Restore
171 | **/packages/*
172 | # except build/, which is used as an MSBuild target.
173 | !**/packages/build/
174 | # Uncomment if necessary however generally it will be regenerated when needed
175 | #!**/packages/repositories.config
176 | # NuGet v3's project.json files produces more ignorable files
177 | *.nuget.props
178 | *.nuget.targets
179 |
180 | # Microsoft Azure Build Output
181 | csx/
182 | *.build.csdef
183 |
184 | # Microsoft Azure Emulator
185 | ecf/
186 | rcf/
187 |
188 | # Windows Store app package directories and files
189 | AppPackages/
190 | BundleArtifacts/
191 | Package.StoreAssociation.xml
192 | _pkginfo.txt
193 | *.appx
194 |
195 | # Visual Studio cache files
196 | # files ending in .cache can be ignored
197 | *.[Cc]ache
198 | # but keep track of directories ending in .cache
199 | !*.[Cc]ache/
200 |
201 | # Others
202 | ClientBin/
203 | ~$*
204 | *~
205 | *.dbmdl
206 | *.dbproj.schemaview
207 | *.jfm
208 | *.pfx
209 | *.publishsettings
210 | orleans.codegen.cs
211 |
212 | # Since there are multiple workflows, uncomment next line to ignore bower_components
213 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
214 | #bower_components/
215 |
216 | # RIA/Silverlight projects
217 | Generated_Code/
218 |
219 | # Backup & report files from converting an old project file
220 | # to a newer Visual Studio version. Backup files are not needed,
221 | # because we have git ;-)
222 | _UpgradeReport_Files/
223 | Backup*/
224 | UpgradeLog*.XML
225 | UpgradeLog*.htm
226 |
227 | # SQL Server files
228 | *.mdf
229 | *.ldf
230 | *.ndf
231 |
232 | # Business Intelligence projects
233 | *.rdl.data
234 | *.bim.layout
235 | *.bim_*.settings
236 |
237 | # Microsoft Fakes
238 | FakesAssemblies/
239 |
240 | # GhostDoc plugin setting file
241 | *.GhostDoc.xml
242 |
243 | # Node.js Tools for Visual Studio
244 | .ntvs_analysis.dat
245 | node_modules/
246 |
247 | # Typescript v1 declaration files
248 | typings/
249 |
250 | # Visual Studio 6 build log
251 | *.plg
252 |
253 | # Visual Studio 6 workspace options file
254 | *.opt
255 |
256 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
257 | *.vbw
258 |
259 | # Visual Studio LightSwitch build output
260 | **/*.HTMLClient/GeneratedArtifacts
261 | **/*.DesktopClient/GeneratedArtifacts
262 | **/*.DesktopClient/ModelManifest.xml
263 | **/*.Server/GeneratedArtifacts
264 | **/*.Server/ModelManifest.xml
265 | _Pvt_Extensions
266 |
267 | # Paket dependency manager
268 | .paket/paket.exe
269 | paket-files/
270 |
271 | # FAKE - F# Make
272 | .fake/
273 |
274 | # JetBrains Rider
275 | .idea/
276 | *.sln.iml
277 |
278 | # CodeRush
279 | .cr/
280 |
281 | # Python Tools for Visual Studio (PTVS)
282 | __pycache__/
283 | *.pyc
284 |
285 | # Cake - Uncomment if you are using it
286 | # tools/**
287 | # !tools/packages.config
288 |
289 | # Tabs Studio
290 | *.tss
291 |
292 | # Telerik's JustMock configuration file
293 | *.jmconfig
294 |
295 | # BizTalk build output
296 | *.btp.cs
297 | *.btm.cs
298 | *.odx.cs
299 | *.xsd.cs
300 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 | {one line to give the program's name and a brief idea of what it does.}
635 | Copyright (C) 2018 {name of author}
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | WEMSharp Copyright (C) 2018 Crauzer
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # WEMSharp
2 |
3 | WEMSharp is a C# library used to convert Audiokinetic Wwise WEM files to OGG files.
4 |
5 | This project was inspired by [ww2ogg](https://github.com/hcs64/ww2ogg).
6 |
7 | ## Usage
8 | ```csharp
9 |
10 | WEMFile wem = new WEMFile("locationOfYourWemFile", yourDesiredPacketForcing);
11 | wem.GenerateOGG("locationOfTheOggFile", "locationOfCodebooksToUse", inlineCodebooks, fullSetup);
12 |
13 | ```
14 |
15 | * Packet Format Forcing
16 | * NoForcePacketFormat - Uses the original Mod Packet Format from the WEM file.
17 | * ForceModPackets - Forces to modify the original packets.
18 | * ForceNoModPackets - Forces to not modify the original packets.
19 |
20 | * Inline Codebooks
21 |
22 | Whether codebooks should be loaded from the WEM file.
23 |
24 | If `false`, `codebooksLocation` must not be empty.
25 | If `true`, `codebooksLocation` should be empty.
26 |
27 | * Full Setup
28 |
29 | Whether the setup packet should be copied from the WEM file.
30 |
--------------------------------------------------------------------------------
/WEMSharp.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27004.2005
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WEMSharp", "WEMSharp\WEMSharp.csproj", "{2EDFD763-C9CF-459E-9227-AFEBCAD33850}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WEMSharpTests", "WEMSharpTests\WEMSharpTests.csproj", "{B44CD6C2-E676-4A51-A16B-7D6F8BEDF6FC}"
9 | EndProject
10 | Global
11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
12 | Debug|Any CPU = Debug|Any CPU
13 | Release|Any CPU = Release|Any CPU
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {2EDFD763-C9CF-459E-9227-AFEBCAD33850}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17 | {2EDFD763-C9CF-459E-9227-AFEBCAD33850}.Debug|Any CPU.Build.0 = Debug|Any CPU
18 | {2EDFD763-C9CF-459E-9227-AFEBCAD33850}.Release|Any CPU.ActiveCfg = Release|Any CPU
19 | {2EDFD763-C9CF-459E-9227-AFEBCAD33850}.Release|Any CPU.Build.0 = Release|Any CPU
20 | {B44CD6C2-E676-4A51-A16B-7D6F8BEDF6FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21 | {B44CD6C2-E676-4A51-A16B-7D6F8BEDF6FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
22 | {B44CD6C2-E676-4A51-A16B-7D6F8BEDF6FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 | {B44CD6C2-E676-4A51-A16B-7D6F8BEDF6FC}.Release|Any CPU.Build.0 = Release|Any CPU
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {487B818C-59F6-475B-A0B8-BBEE2AAD49A8}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/WEMSharp/BitStream.cs:
--------------------------------------------------------------------------------
1 | using System.IO;
2 |
3 | namespace WEMSharp
4 | {
5 | ///
6 | /// Wrapper around a Stream that supports reading bits
7 | ///
8 | internal class BitStream
9 | {
10 | private Stream _stream;
11 | private byte _bitBuffer;
12 | private int _bitsLeft;
13 | internal ulong TotalBitsRead { get; private set; }
14 |
15 | internal BitStream(Stream stream)
16 | {
17 | this._stream = stream;
18 | }
19 |
20 | ///
21 | /// Reads a bit from the Stream
22 | ///
23 | internal byte GetBit()
24 | {
25 | if (this._bitsLeft == 0)
26 | {
27 | this._bitBuffer = (byte)this._stream.ReadByte();
28 | this._bitsLeft = 8;
29 | }
30 |
31 | this.TotalBitsRead++;
32 | this._bitsLeft--;
33 |
34 | return (this._bitBuffer & (0x80 >> this._bitsLeft)) != 0 ? (byte)1 : (byte)0;
35 | }
36 |
37 | ///
38 | /// Read an arbitary amount of bits from the stream
39 | ///
40 | /// Amount of bits to read
41 | internal uint Read(int bitCount)
42 | {
43 | uint result = 0;
44 |
45 | for (int i = 0; i < bitCount; i++)
46 | {
47 | if (GetBit() == 1)
48 | {
49 | result |= (1U << i);
50 | }
51 | }
52 |
53 | return result;
54 | }
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/WEMSharp/CRC32.cs:
--------------------------------------------------------------------------------
1 | namespace WEMSharp
2 | {
3 | internal static class CRC32
4 | {
5 | private static readonly uint[] CRC_LOOKUP = new uint[]
6 | {
7 | 0x00000000,0x04c11db7,0x09823b6e,0x0d4326d9,
8 | 0x130476dc,0x17c56b6b,0x1a864db2,0x1e475005,
9 | 0x2608edb8,0x22c9f00f,0x2f8ad6d6,0x2b4bcb61,
10 | 0x350c9b64,0x31cd86d3,0x3c8ea00a,0x384fbdbd,
11 | 0x4c11db70,0x48d0c6c7,0x4593e01e,0x4152fda9,
12 | 0x5f15adac,0x5bd4b01b,0x569796c2,0x52568b75,
13 | 0x6a1936c8,0x6ed82b7f,0x639b0da6,0x675a1011,
14 | 0x791d4014,0x7ddc5da3,0x709f7b7a,0x745e66cd,
15 | 0x9823b6e0,0x9ce2ab57,0x91a18d8e,0x95609039,
16 | 0x8b27c03c,0x8fe6dd8b,0x82a5fb52,0x8664e6e5,
17 | 0xbe2b5b58,0xbaea46ef,0xb7a96036,0xb3687d81,
18 | 0xad2f2d84,0xa9ee3033,0xa4ad16ea,0xa06c0b5d,
19 | 0xd4326d90,0xd0f37027,0xddb056fe,0xd9714b49,
20 | 0xc7361b4c,0xc3f706fb,0xceb42022,0xca753d95,
21 | 0xf23a8028,0xf6fb9d9f,0xfbb8bb46,0xff79a6f1,
22 | 0xe13ef6f4,0xe5ffeb43,0xe8bccd9a,0xec7dd02d,
23 | 0x34867077,0x30476dc0,0x3d044b19,0x39c556ae,
24 | 0x278206ab,0x23431b1c,0x2e003dc5,0x2ac12072,
25 | 0x128e9dcf,0x164f8078,0x1b0ca6a1,0x1fcdbb16,
26 | 0x018aeb13,0x054bf6a4,0x0808d07d,0x0cc9cdca,
27 | 0x7897ab07,0x7c56b6b0,0x71159069,0x75d48dde,
28 | 0x6b93dddb,0x6f52c06c,0x6211e6b5,0x66d0fb02,
29 | 0x5e9f46bf,0x5a5e5b08,0x571d7dd1,0x53dc6066,
30 | 0x4d9b3063,0x495a2dd4,0x44190b0d,0x40d816ba,
31 | 0xaca5c697,0xa864db20,0xa527fdf9,0xa1e6e04e,
32 | 0xbfa1b04b,0xbb60adfc,0xb6238b25,0xb2e29692,
33 | 0x8aad2b2f,0x8e6c3698,0x832f1041,0x87ee0df6,
34 | 0x99a95df3,0x9d684044,0x902b669d,0x94ea7b2a,
35 | 0xe0b41de7,0xe4750050,0xe9362689,0xedf73b3e,
36 | 0xf3b06b3b,0xf771768c,0xfa325055,0xfef34de2,
37 | 0xc6bcf05f,0xc27dede8,0xcf3ecb31,0xcbffd686,
38 | 0xd5b88683,0xd1799b34,0xdc3abded,0xd8fba05a,
39 | 0x690ce0ee,0x6dcdfd59,0x608edb80,0x644fc637,
40 | 0x7a089632,0x7ec98b85,0x738aad5c,0x774bb0eb,
41 | 0x4f040d56,0x4bc510e1,0x46863638,0x42472b8f,
42 | 0x5c007b8a,0x58c1663d,0x558240e4,0x51435d53,
43 | 0x251d3b9e,0x21dc2629,0x2c9f00f0,0x285e1d47,
44 | 0x36194d42,0x32d850f5,0x3f9b762c,0x3b5a6b9b,
45 | 0x0315d626,0x07d4cb91,0x0a97ed48,0x0e56f0ff,
46 | 0x1011a0fa,0x14d0bd4d,0x19939b94,0x1d528623,
47 | 0xf12f560e,0xf5ee4bb9,0xf8ad6d60,0xfc6c70d7,
48 | 0xe22b20d2,0xe6ea3d65,0xeba91bbc,0xef68060b,
49 | 0xd727bbb6,0xd3e6a601,0xdea580d8,0xda649d6f,
50 | 0xc423cd6a,0xc0e2d0dd,0xcda1f604,0xc960ebb3,
51 | 0xbd3e8d7e,0xb9ff90c9,0xb4bcb610,0xb07daba7,
52 | 0xae3afba2,0xaafbe615,0xa7b8c0cc,0xa379dd7b,
53 | 0x9b3660c6,0x9ff77d71,0x92b45ba8,0x9675461f,
54 | 0x8832161a,0x8cf30bad,0x81b02d74,0x857130c3,
55 | 0x5d8a9099,0x594b8d2e,0x5408abf7,0x50c9b640,
56 | 0x4e8ee645,0x4a4ffbf2,0x470cdd2b,0x43cdc09c,
57 | 0x7b827d21,0x7f436096,0x7200464f,0x76c15bf8,
58 | 0x68860bfd,0x6c47164a,0x61043093,0x65c52d24,
59 | 0x119b4be9,0x155a565e,0x18197087,0x1cd86d30,
60 | 0x029f3d35,0x065e2082,0x0b1d065b,0x0fdc1bec,
61 | 0x3793a651,0x3352bbe6,0x3e119d3f,0x3ad08088,
62 | 0x2497d08d,0x2056cd3a,0x2d15ebe3,0x29d4f654,
63 | 0xc5a92679,0xc1683bce,0xcc2b1d17,0xc8ea00a0,
64 | 0xd6ad50a5,0xd26c4d12,0xdf2f6bcb,0xdbee767c,
65 | 0xe3a1cbc1,0xe760d676,0xea23f0af,0xeee2ed18,
66 | 0xf0a5bd1d,0xf464a0aa,0xf9278673,0xfde69bc4,
67 | 0x89b8fd09,0x8d79e0be,0x803ac667,0x84fbdbd0,
68 | 0x9abc8bd5,0x9e7d9662,0x933eb0bb,0x97ffad0c,
69 | 0xafb010b1,0xab710d06,0xa6322bdf,0xa2f33668,
70 | 0xbcb4666d,0xb8757bda,0xb5365d03,0xb1f740b4
71 | };
72 |
73 | internal static uint Compute(byte[] buffer, uint size)
74 | {
75 | uint hash = 0;
76 |
77 | for (int i = 0; i < size; i++)
78 | {
79 | hash = (hash << 8) ^ CRC_LOOKUP[((hash >> 24) & 0xFF) ^ buffer[i]];
80 | }
81 |
82 | return hash;
83 | }
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/WEMSharp/CodebookLibrary.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using System.Linq;
4 |
5 | namespace WEMSharp
6 | {
7 | internal class CodebookLibrary
8 | {
9 | public uint CodebookCount { get; private set; }
10 | private byte[] _codebookData;
11 | private uint[] _codebookOffsets;
12 |
13 | internal CodebookLibrary() { }
14 |
15 | internal CodebookLibrary(string fileLocation)
16 | {
17 | using (BinaryReader br = new BinaryReader(File.OpenRead(fileLocation)))
18 | {
19 | br.BaseStream.Seek(br.BaseStream.Length - 4, SeekOrigin.Begin);
20 |
21 | uint offsetOffset = br.ReadUInt32();
22 | this.CodebookCount = (uint)(br.BaseStream.Length - offsetOffset) / 4;
23 | this._codebookOffsets = new uint[this.CodebookCount];
24 |
25 | br.BaseStream.Seek(0, SeekOrigin.Begin);
26 |
27 | this._codebookData = br.ReadBytes((int)offsetOffset);
28 |
29 | for (int i = 0; i < this.CodebookCount; i++)
30 | {
31 | this._codebookOffsets[i] = br.ReadUInt32();
32 | }
33 | }
34 | }
35 |
36 | internal void Copy(BitStream bitStream, OggStream ogg)
37 | {
38 | //https://xiph.org/vorbis/doc/Vorbis_I_spec.pdf
39 |
40 | byte[] id = new byte[]
41 | {
42 | (byte)bitStream.Read(8),
43 | (byte)bitStream.Read(8),
44 | (byte)bitStream.Read(8)
45 | };
46 |
47 | ushort dimensions = (ushort)bitStream.Read(16);
48 | uint entries = bitStream.Read(24);
49 |
50 | if (id[0] != 0x42 || id[1] != 0x43 || id[2] != 0x56)
51 | {
52 | throw new Exception("Invalid Codebook Sync Pattern");
53 | }
54 |
55 | ogg.BitWrite(id[0]);
56 | ogg.BitWrite(id[1]);
57 | ogg.BitWrite(id[2]);
58 |
59 | ogg.BitWrite(dimensions);
60 | ogg.BitWrite(entries, 24);
61 |
62 | byte orderedFlag = (byte)bitStream.Read(1);
63 | ogg.WriteBit(orderedFlag);
64 | if (orderedFlag == 1)
65 | {
66 | byte initialLength = (byte)bitStream.Read(5);
67 | ogg.BitWrite(initialLength, 5);
68 |
69 | uint currentEntry = 0;
70 | while (currentEntry < entries)
71 | {
72 | uint bitCount = ILog(entries - currentEntry);
73 | uint number = bitStream.Read((int)bitCount);
74 | ogg.BitWrite(number, (byte)bitCount);
75 | currentEntry += number;
76 | }
77 |
78 | if (currentEntry > entries)
79 | {
80 | throw new Exception("There was an error copying a codebook");
81 | }
82 | }
83 | else
84 | {
85 | byte sparseFlag = (byte)bitStream.Read(1);
86 | ogg.WriteBit(sparseFlag);
87 |
88 | for (int i = 0; i < entries; i++)
89 | {
90 | bool presentBool = true;
91 |
92 | if (sparseFlag == 1)
93 | {
94 | byte sparsePresenceFlag = (byte)bitStream.Read(1);
95 | ogg.WriteBit(sparsePresenceFlag);
96 |
97 | presentBool = sparsePresenceFlag == 1;
98 | }
99 | else
100 | {
101 | byte codewordLength = (byte)bitStream.Read(5);
102 | ogg.BitWrite(codewordLength, 5);
103 | }
104 | }
105 | }
106 |
107 | byte lookupType = (byte)bitStream.Read(4);
108 | ogg.BitWrite(lookupType, 4);
109 | if (lookupType == 1)
110 | {
111 | ogg.BitWrite(bitStream.Read(32));
112 | ogg.BitWrite(bitStream.Read(32));
113 | byte valueLength = (byte)bitStream.Read(4);
114 | ogg.BitWrite(valueLength, 4);
115 | ogg.WriteBit((byte)bitStream.Read(1));
116 |
117 | uint quantValues = QuantValues(entries, dimensions);
118 | for (int i = 0; i < quantValues; i++)
119 | {
120 | uint value = bitStream.Read(valueLength + 1);
121 | ogg.BitWrite(value, (byte)(valueLength + 1));
122 | }
123 | }
124 | else
125 | {
126 | throw new Exception("There was an error copying a codebook");
127 | }
128 | }
129 |
130 | internal void Rebuild(uint index, OggStream ogg)
131 | {
132 | byte[] codebook = GetCodebook(index);
133 | uint codebookSize = GetCodebookSize(index);
134 |
135 | if (codebookSize == 0xFFFFFFFF || codebook == null)
136 | {
137 | throw new Exception("Invalid Codebook Index");
138 | }
139 |
140 | Rebuild(new BitStream(new MemoryStream(codebook)), codebookSize, ogg);
141 | }
142 |
143 | internal void Rebuild(BitStream bitStream, uint codebookSize, OggStream ogg)
144 | {
145 | byte dimensions = (byte)bitStream.Read(4);
146 | ushort entries = (ushort)bitStream.Read(14);
147 |
148 | ogg.BitWrite(0x564342, 24);
149 | ogg.BitWrite((ushort)dimensions, 16);
150 | ogg.BitWrite((uint)entries, 24);
151 |
152 | byte orderedFlag = (byte)bitStream.Read(1);
153 | ogg.WriteBit(orderedFlag);
154 | if (orderedFlag == 1)
155 | {
156 | byte initialLength = (byte)bitStream.Read(5);
157 | ogg.BitWrite(initialLength, 5);
158 |
159 | uint currentEntry = 0;
160 | while (currentEntry < entries)
161 | {
162 | uint bitCount = ILog(entries - currentEntry);
163 | uint number = bitStream.Read((int)bitCount);
164 | ogg.BitWrite(number, (byte)bitCount);
165 | currentEntry += number;
166 | }
167 |
168 | if (currentEntry > entries)
169 | {
170 | throw new Exception("There was an error rebuilding a codebook");
171 | }
172 | }
173 | else
174 | {
175 | byte codewordLengthLength = (byte)bitStream.Read(3);
176 | byte sparseFlag = (byte)bitStream.Read(1);
177 | ogg.WriteBit(sparseFlag);
178 |
179 | if (codewordLengthLength == 0 || codewordLengthLength > 5)
180 | {
181 | throw new Exception("There was an error rebuilding a codebook");
182 | }
183 |
184 | for (int i = 0; i < entries; i++)
185 | {
186 | bool presentBool = true;
187 |
188 | if (sparseFlag == 1)
189 | {
190 | byte sparsePresenceFlag = (byte)bitStream.Read(1);
191 | ogg.WriteBit(sparsePresenceFlag);
192 |
193 | presentBool = sparsePresenceFlag == 1;
194 | }
195 |
196 | if (presentBool)
197 | {
198 | byte codewordLength = (byte)bitStream.Read(codewordLengthLength);
199 | ogg.BitWrite(codewordLength, 5);
200 | }
201 | }
202 | }
203 |
204 | byte lookupType = (byte)bitStream.Read(1);
205 | ogg.BitWrite(lookupType, 4);
206 | if (lookupType == 1)
207 | {
208 | ogg.BitWrite(bitStream.Read(32));
209 | ogg.BitWrite(bitStream.Read(32));
210 | byte valueLength = (byte)bitStream.Read(4);
211 | ogg.BitWrite(valueLength, 4);
212 | ogg.WriteBit((byte)bitStream.Read(1));
213 |
214 | uint quantValues = QuantValues(entries, dimensions);
215 | for (int i = 0; i < quantValues; i++)
216 | {
217 | uint value = bitStream.Read(valueLength + 1);
218 | ogg.BitWrite(value, (byte)(valueLength + 1));
219 | }
220 | }
221 | else if (lookupType != 0)
222 | {
223 | throw new Exception("There was an error copying a codebook");
224 | }
225 |
226 | if (codebookSize != 0 && bitStream.TotalBitsRead / 8 + 1 != codebookSize)
227 | {
228 | throw new Exception("There was an error rebuilding a codebook");
229 | }
230 | }
231 |
232 | internal byte[] GetCodebook(uint index)
233 | {
234 | if (this._codebookData == null || this._codebookOffsets == null)
235 | {
236 | throw new Exception("This Codebook Library is not initialized");
237 | }
238 |
239 | if (index >= this.CodebookCount - 1)
240 | {
241 | return null;
242 | }
243 |
244 | return this._codebookData.ToList().GetRange((int)this._codebookOffsets[index], (int)GetCodebookSize(index)).ToArray();
245 | }
246 |
247 | internal uint GetCodebookSize(uint index)
248 | {
249 | if (this._codebookData == null || this._codebookOffsets == null)
250 | {
251 | throw new Exception("This Codebook Library is not initialized");
252 | }
253 |
254 | if (index >= this.CodebookCount - 1)
255 | {
256 | return 0xFFFFFFFF;
257 | }
258 |
259 | return this._codebookOffsets[index + 1] - this._codebookOffsets[index];
260 | }
261 |
262 | //https://xiph.org/vorbis/doc/Vorbis_I_spec.pdf#subsubsection.9.2.1
263 | private uint ILog(uint value)
264 | {
265 | uint ret = 0;
266 |
267 | while (value != 0)
268 | {
269 | ret++;
270 | value >>= 1;
271 | }
272 |
273 | return ret;
274 | }
275 |
276 | private uint QuantValues(uint entries, uint dimensions)
277 | {
278 | uint bits = ILog(entries);
279 | uint values = entries >> (int)((bits - 1) * (dimensions - 1) / dimensions);
280 |
281 | while (true)
282 | {
283 | ulong acc = 1;
284 | ulong acc1 = 1;
285 | for (int i = 0; i < dimensions; i++)
286 | {
287 | acc *= values;
288 | acc1 *= values + 1;
289 | }
290 | if (acc <= entries && acc1 > entries)
291 | {
292 | return values;
293 | }
294 | else
295 | {
296 | if (acc > entries)
297 | {
298 | values--;
299 | }
300 | else
301 | {
302 | values++;
303 | }
304 | }
305 | }
306 | }
307 | }
308 | }
309 |
--------------------------------------------------------------------------------
/WEMSharp/OggStream.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using System.Text;
4 |
5 | namespace WEMSharp
6 | {
7 | internal class OggStream : BinaryWriter
8 | {
9 | private const uint HEADER_SIZE = 27;
10 | private const uint MAX_SEGMENTS = 255;
11 | private const uint SEGMENT_SIZE = 255;
12 |
13 | private byte _bitBuffer;
14 | private int _bitsStored;
15 | private uint _payloadBytes;
16 | private bool _first = true;
17 | private bool _continued;
18 | private byte[] _pageBuffer = new byte[HEADER_SIZE + MAX_SEGMENTS + SEGMENT_SIZE * MAX_SEGMENTS];
19 | private uint _granule;
20 | private uint _sequenceNumber;
21 |
22 | internal OggStream(Stream file) : base(file) { }
23 | ~OggStream()
24 | {
25 | FlushPage();
26 | }
27 |
28 | internal void WriteBit(byte bit)
29 | {
30 | if (bit == 1)
31 | {
32 | this._bitBuffer |= (byte)(1U << this._bitsStored);
33 | }
34 |
35 | this._bitsStored++;
36 |
37 | if (this._bitsStored == 8)
38 | {
39 | FlushBits();
40 | }
41 | }
42 |
43 | internal void FlushBits()
44 | {
45 | if (this._bitsStored != 0)
46 | {
47 | if (this._payloadBytes == SEGMENT_SIZE * MAX_SEGMENTS)
48 | {
49 | throw new Exception("Ran out of space in an OGG packet");
50 | }
51 |
52 | this._pageBuffer[HEADER_SIZE + MAX_SEGMENTS + this._payloadBytes] = this._bitBuffer;
53 | this._payloadBytes++;
54 |
55 | this._bitBuffer = 0;
56 | this._bitsStored = 0;
57 | }
58 | }
59 |
60 | internal void FlushPage(bool nextContinued = false, bool last = false)
61 | {
62 | if (this._payloadBytes != MAX_SEGMENTS * SEGMENT_SIZE)
63 | {
64 | FlushBits();
65 | }
66 |
67 | if (this._payloadBytes != 0)
68 | {
69 | uint segments = (this._payloadBytes + SEGMENT_SIZE) / SEGMENT_SIZE;
70 | if (segments == MAX_SEGMENTS + 1)
71 | {
72 | segments = MAX_SEGMENTS;
73 | }
74 |
75 | for (int i = 0; i < this._payloadBytes; i++)
76 | {
77 | this._pageBuffer[HEADER_SIZE + segments + i] = this._pageBuffer[HEADER_SIZE + MAX_SEGMENTS + i];
78 | }
79 |
80 | this._pageBuffer[0] = (byte)'O';
81 | this._pageBuffer[1] = (byte)'g';
82 | this._pageBuffer[2] = (byte)'g';
83 | this._pageBuffer[3] = (byte)'S';
84 | this._pageBuffer[4] = 0;
85 | this._pageBuffer[5] = (byte)((this._continued ? 1 : 0) | (this._first ? 2 : 0) | (last ? 4 : 0));
86 | Buffer.BlockCopy(BitConverter.GetBytes(this._granule), 0, this._pageBuffer, 6, 4);
87 |
88 | if (this._granule == 0xFFFFFFFF)
89 | {
90 | this._pageBuffer[10] = 0xFF;
91 | this._pageBuffer[11] = 0xFF;
92 | this._pageBuffer[12] = 0xFF;
93 | this._pageBuffer[13] = 0xFF;
94 | }
95 | else
96 | {
97 | this._pageBuffer[10] = 0;
98 | this._pageBuffer[11] = 0;
99 | this._pageBuffer[12] = 0;
100 | this._pageBuffer[13] = 0;
101 | }
102 |
103 | this._pageBuffer[14] = 1;
104 | Buffer.BlockCopy(BitConverter.GetBytes(this._sequenceNumber), 0, this._pageBuffer, 18, 4);
105 | this._pageBuffer[22] = 0;
106 | this._pageBuffer[23] = 0;
107 | this._pageBuffer[24] = 0;
108 | this._pageBuffer[25] = 0;
109 | this._pageBuffer[26] = (byte)segments;
110 |
111 | for (uint i = 0, bytesLeft = this._payloadBytes; i < segments; i++)
112 | {
113 | if (bytesLeft >= SEGMENT_SIZE)
114 | {
115 | bytesLeft -= SEGMENT_SIZE;
116 | this._pageBuffer[27 + i] = (byte)SEGMENT_SIZE;
117 | }
118 | else
119 | {
120 | this._pageBuffer[27 + i] = (byte)bytesLeft;
121 | }
122 | }
123 |
124 | Buffer.BlockCopy(BitConverter.GetBytes(CRC32.Compute(this._pageBuffer, HEADER_SIZE + segments + this._payloadBytes)), 0, this._pageBuffer, 22, 4);
125 |
126 | for (int i = 0; i < HEADER_SIZE + segments + this._payloadBytes; i++)
127 | {
128 | Write(this._pageBuffer[i]);
129 | }
130 |
131 | this._sequenceNumber++;
132 | this._first = false;
133 | this._continued = nextContinued;
134 | this._payloadBytes = 0;
135 | }
136 | }
137 |
138 | internal void BitWrite(byte value, byte bitCount = 8)
139 | {
140 | BitWrite(value, (int)bitCount);
141 | }
142 |
143 | internal void BitWrite(ushort value, byte bitCount = 16)
144 | {
145 | BitWrite(value, (int)bitCount);
146 | }
147 |
148 | internal void BitWrite(uint value, byte bitCount = 32)
149 | {
150 | BitWrite(value, (int)bitCount);
151 | }
152 |
153 | private void BitWrite(uint value, int size)
154 | {
155 | for (int i = 0; i < size; i++)
156 | {
157 | WriteBit((value & (1 << i)) != 0 ? (byte)1 : (byte)0);
158 | }
159 | }
160 |
161 | internal void WriteVorbisHeader(byte type)
162 | {
163 | byte[] vorbisString = Encoding.UTF8.GetBytes("vorbis");
164 |
165 | BitWrite(type);
166 | for (int i = 0; i < vorbisString.Length; i++)
167 | {
168 | BitWrite(vorbisString[i]);
169 | }
170 | }
171 |
172 | internal void SetGranule(uint granule)
173 | {
174 | this._granule = granule;
175 | }
176 | }
177 | }
178 |
--------------------------------------------------------------------------------
/WEMSharp/Packet.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 |
4 | namespace WEMSharp
5 | {
6 | internal struct Packet
7 | {
8 | private uint _offset;
9 | private ushort _size;
10 | private uint _absoluteGranule;
11 | private bool _noGranule;
12 |
13 | internal Packet(Stream stream, uint offset, bool noGranule = false)
14 | {
15 | this._offset = offset;
16 | this._size = 0xFFFF;
17 | this._absoluteGranule = 0;
18 | this._noGranule = noGranule;
19 |
20 | stream.Seek(this._offset, SeekOrigin.Begin);
21 |
22 | byte[] sizeBuffer = new byte[2];
23 | stream.Read(sizeBuffer, 0, 2);
24 | this._size = BitConverter.ToUInt16(sizeBuffer, 0);
25 |
26 | if (!this._noGranule)
27 | {
28 | byte[] granuleBuffer = new byte[4];
29 | stream.Read(granuleBuffer, 0, 4);
30 | this._absoluteGranule = BitConverter.ToUInt32(granuleBuffer, 0);
31 | }
32 | }
33 |
34 | internal uint GetHeaderSize()
35 | {
36 | return this._noGranule ? (uint)2 : 6;
37 | }
38 |
39 | internal uint GetOffset()
40 | {
41 | return GetHeaderSize() + this._offset;
42 | }
43 |
44 | internal ushort GetSize()
45 | {
46 | return this._size;
47 | }
48 |
49 | internal uint GetGranule()
50 | {
51 | return this._absoluteGranule;
52 | }
53 |
54 | internal uint NextOffset()
55 | {
56 | return this._offset + GetHeaderSize() + this._size;
57 | }
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/WEMSharp/Packet8.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 |
4 | namespace WEMSharp
5 | {
6 | internal struct Packet8
7 | {
8 | private uint _offset;
9 | private uint _size;
10 | private uint _absoluteGranule;
11 |
12 | internal Packet8(Stream stream, uint offset)
13 | {
14 | this._offset = offset;
15 | this._size = 0xFFFF;
16 | this._absoluteGranule = 0;
17 |
18 | stream.Seek(this._offset, SeekOrigin.Begin);
19 |
20 | byte[] sizeBuffer = new byte[4];
21 | stream.Read(sizeBuffer, 0, 4);
22 | this._size = BitConverter.ToUInt32(sizeBuffer, 0);
23 |
24 | byte[] granuleBuffer = new byte[4];
25 | stream.Read(granuleBuffer, 0, 4);
26 | this._absoluteGranule = BitConverter.ToUInt32(granuleBuffer, 0);
27 | }
28 |
29 | internal uint GetHeaderSize()
30 | {
31 | return 8;
32 | }
33 |
34 | internal uint GetOffset()
35 | {
36 | return GetHeaderSize() + this._offset;
37 | }
38 |
39 | internal uint GetSize()
40 | {
41 | return this._size;
42 | }
43 |
44 | internal uint GetGranule()
45 | {
46 | return this._absoluteGranule;
47 | }
48 |
49 | internal uint NextOffset()
50 | {
51 | return this._offset + GetHeaderSize() + this._size;
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/WEMSharp/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("WEMSharp")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("WEMSharp")]
13 | [assembly: AssemblyCopyright("Copyright © 2018")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("2edfd763-c9cf-459e-9227-afebcad33850")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/WEMSharp/WEMFile.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using System.Linq;
4 | using System.Text;
5 |
6 | namespace WEMSharp
7 | {
8 | public class WEMFile
9 | {
10 | private FileStream _wemFile;
11 |
12 | private uint _fmtChunkOffset = 0xFFFFFFFF;
13 | private uint _fmtChunkSize = 0xFFFFFFFF;
14 | private uint _cueChunkOffset = 0xFFFFFFFF;
15 | private uint _cueChunkSize = 0xFFFFFFFF;
16 | private uint _listChunkOffset = 0xFFFFFFFF;
17 | private uint _listChunkSize = 0xFFFFFFFF;
18 | private uint _smplChunkOffset = 0xFFFFFFFF;
19 | private uint _smplChunkSize = 0xFFFFFFFF;
20 | private uint _vorbChunkOffset = 0xFFFFFFFF;
21 | private uint _vorbChunkSize = 0xFFFFFFFF;
22 | private uint _dataChunkOffset = 0xFFFFFFFF;
23 | private uint _dataChunkSize = 0xFFFFFFFF;
24 |
25 | ///
26 | /// Channel Count
27 | ///
28 | public ushort Channels { get; private set; }
29 | ///
30 | /// Sample Rate
31 | ///
32 | public uint SampleRate { get; private set; }
33 | public uint AverageBytesPerSecond { get; private set; }
34 |
35 | private uint _cueCount;
36 | private uint _loopCount;
37 | private uint _loopStart;
38 | private uint _loopEnd;
39 | private uint _sampleCount;
40 | private bool _noGranule;
41 | private bool _modPackets;
42 | private uint _setupPacketOffset;
43 | private uint _firstAudioPacketOffset;
44 | private bool _headerTriadPresent;
45 | private bool _oldPacketHeaders;
46 | private uint _uid;
47 | private byte _blocksize0Pow;
48 | private byte _blocksize1Pow;
49 |
50 | public WEMFile(string fileLocation, WEMForcePacketFormat forcePacketFormat)
51 | {
52 | this._wemFile = File.OpenRead(fileLocation);
53 |
54 | using (BinaryReader br = new BinaryReader(this._wemFile, Encoding.UTF8, true))
55 | {
56 | string magic = Encoding.ASCII.GetString(br.ReadBytes(4));
57 | if (magic != "RIFF")
58 | {
59 | throw new Exception("This is either not a WEM file or is of an unsupported type");
60 | }
61 |
62 | uint riffSize = br.ReadUInt32() + 8;
63 |
64 | string waveMagic = Encoding.ASCII.GetString(br.ReadBytes(4));
65 | if (waveMagic != "WAVE")
66 | {
67 | throw new Exception("Missing WAVE magic");
68 | }
69 |
70 | uint chunkOffset = 12;
71 | while (chunkOffset < riffSize)
72 | {
73 | br.BaseStream.Seek(chunkOffset, SeekOrigin.Begin);
74 |
75 | string chunkName = Encoding.ASCII.GetString(br.ReadBytes(4));
76 | uint chunkSize = br.ReadUInt32();
77 |
78 | if (chunkName.Substring(0, 3) == "fmt")
79 | {
80 | this._fmtChunkOffset = chunkOffset + 8;
81 | this._fmtChunkSize = chunkSize;
82 | }
83 | else if (chunkName.Substring(0, 3) == "cue")
84 | {
85 | this._cueChunkOffset = chunkOffset + 8;
86 | this._cueChunkSize = chunkSize;
87 | }
88 | else if (chunkName == "LIST")
89 | {
90 | this._listChunkOffset = chunkOffset + 8;
91 | this._listChunkSize = chunkSize;
92 | }
93 | else if (chunkName == "smpl")
94 | {
95 | this._smplChunkOffset = chunkOffset + 8;
96 | this._smplChunkSize = chunkSize;
97 | }
98 | else if (chunkName == "vorb")
99 | {
100 | this._vorbChunkOffset = chunkOffset + 8;
101 | this._vorbChunkSize = chunkSize;
102 | }
103 | else if (chunkName == "data")
104 | {
105 | this._dataChunkOffset = chunkOffset + 8;
106 | this._dataChunkSize = chunkSize;
107 | }
108 |
109 | chunkOffset += chunkSize + 8;
110 | }
111 |
112 | if (chunkOffset > riffSize)
113 | {
114 | throw new Exception("There was an error reading the file");
115 | }
116 |
117 | if (this._fmtChunkOffset == 0xFFFFFFFF && this._dataChunkOffset == 0xFFFFFFFF)
118 | {
119 | throw new Exception("There was an error reading the file");
120 | }
121 |
122 | //Read FMT Chunk
123 | if (this._vorbChunkOffset == 0xFFFFFFFF && this._fmtChunkSize != 0x42)
124 | {
125 | throw new Exception("There was an error reading the file");
126 | }
127 | if (this._vorbChunkOffset != 0xFFFFFFFF && this._fmtChunkSize != 0x28 && this._fmtChunkSize != 0x18 && this._fmtChunkSize != 0x12)
128 | {
129 | throw new Exception("There was an error reading the file");
130 | }
131 | if (this._vorbChunkOffset == 0xFFFFFFFF && this._fmtChunkSize == 0x42)
132 | {
133 | this._vorbChunkOffset = this._fmtChunkOffset + 0x18;
134 | }
135 |
136 | br.BaseStream.Seek(this._fmtChunkOffset, SeekOrigin.Begin);
137 |
138 | ushort codecID = br.ReadUInt16();
139 | if (codecID != 0xFFFF)
140 | {
141 | throw new Exception("FMT Chunk - Wrong Codec ID");
142 | }
143 |
144 | this.Channels = br.ReadUInt16();
145 | this.SampleRate = br.ReadUInt32();
146 | this.AverageBytesPerSecond = br.ReadUInt32();
147 |
148 | ushort blockAlign = br.ReadUInt16();
149 | if (blockAlign != 0)
150 | {
151 | throw new Exception("FMT Chunk - Wrong Block Align");
152 | }
153 |
154 | ushort bitsPerSample = br.ReadUInt16();
155 | if (bitsPerSample != 0)
156 | {
157 | throw new Exception("FMT Chunk - Wrong Bits Per Sample");
158 | }
159 |
160 | ushort extraFmtLength = br.ReadUInt16();
161 | if (extraFmtLength != (this._fmtChunkSize - 0x12))
162 | {
163 | throw new Exception("FMT Chunk - Wrong Extra FMT Chunk Size");
164 | }
165 |
166 | if (this._fmtChunkSize == 0x28)
167 | {
168 | byte[] unknownBufferCheck = new byte[] { 1, 0, 0, 0, 0, 0, 0x10, 0, 0x80, 0, 0, 0xAA, 0, 0x38, 0x9b, 0x71 };
169 | byte[] unknownBuffer;
170 |
171 | unknownBuffer = br.ReadBytes(16);
172 |
173 | if (unknownBuffer.SequenceEqual(unknownBufferCheck))
174 | {
175 | throw new Exception("FMT Chunk - Wrong Unknown Buffer Signature");
176 | }
177 | }
178 |
179 | //Read CUE Chunk
180 | if (this._cueChunkOffset != 0xFFFFFFFF)
181 | {
182 | br.BaseStream.Seek(this._cueChunkOffset, SeekOrigin.Begin);
183 |
184 | this._cueCount = br.ReadUInt32();
185 | }
186 |
187 | //Read SMPL Chunk
188 | if (this._smplChunkOffset != 0xFFFFFFFF)
189 | {
190 | br.BaseStream.Seek(this._smplChunkOffset, SeekOrigin.Begin);
191 |
192 | this._loopCount = br.ReadUInt32();
193 | if (this._loopCount != 1)
194 | {
195 | throw new Exception("SMPL Chunk - Wrong Loop Count");
196 | }
197 |
198 | br.BaseStream.Seek(this._smplChunkOffset + 0x2C, SeekOrigin.Begin);
199 |
200 | this._loopStart = br.ReadUInt32();
201 | this._loopEnd = br.ReadUInt32();
202 | }
203 |
204 | //Read VORB Chunk
205 | switch (this._vorbChunkSize)
206 | {
207 | case 0xFFFFFFFF:
208 | case 0x28:
209 | case 0x2A:
210 | case 0x2C:
211 | case 0x32:
212 | case 0x34:
213 | br.BaseStream.Seek(this._vorbChunkOffset, SeekOrigin.Begin);
214 | break;
215 | default:
216 | throw new Exception("VORB Chunk - Wrong VORB Chunk Size");
217 | }
218 |
219 | this._sampleCount = br.ReadUInt32();
220 |
221 | switch (this._vorbChunkSize)
222 | {
223 | case 0xFFFFFFFF:
224 | case 0x2A:
225 | {
226 | this._noGranule = true;
227 |
228 | br.BaseStream.Seek(this._vorbChunkOffset + 4, SeekOrigin.Begin);
229 |
230 | uint modSignal = br.ReadUInt32();
231 |
232 | if (modSignal != 0x4A && modSignal != 0x4B && modSignal != 0x69 && modSignal != 0x70)
233 | {
234 | this._modPackets = true;
235 | }
236 |
237 | br.BaseStream.Seek(this._vorbChunkOffset + 0x10, SeekOrigin.Begin);
238 |
239 | break;
240 | }
241 |
242 | default:
243 | br.BaseStream.Seek(this._vorbChunkOffset + 0x18, SeekOrigin.Begin);
244 | break;
245 | }
246 |
247 | if (forcePacketFormat == WEMForcePacketFormat.ForceNoModPackets)
248 | {
249 | this._modPackets = false;
250 | }
251 | else if (forcePacketFormat == WEMForcePacketFormat.ForceModPackets)
252 | {
253 | this._modPackets = true;
254 | }
255 |
256 | this._setupPacketOffset = br.ReadUInt32();
257 | this._firstAudioPacketOffset = br.ReadUInt32();
258 |
259 | switch (this._vorbChunkSize)
260 | {
261 | case 0xFFFFFFFF:
262 | case 0x2A:
263 | br.BaseStream.Seek(this._vorbChunkOffset + 0x24, SeekOrigin.Begin);
264 | break;
265 |
266 | case 0x32:
267 | case 0x34:
268 | br.BaseStream.Seek(this._vorbChunkOffset + 0x2C, SeekOrigin.Begin);
269 | break;
270 | }
271 |
272 | switch (this._vorbChunkSize)
273 | {
274 | case 0x28:
275 | case 0x2C:
276 | this._headerTriadPresent = true;
277 | this._oldPacketHeaders = true;
278 | break;
279 |
280 | case 0xFFFFFFFF:
281 | case 0x2A:
282 | case 0x32:
283 | case 0x34:
284 | this._uid = br.ReadUInt32();
285 | this._blocksize0Pow = br.ReadByte();
286 | this._blocksize1Pow = br.ReadByte();
287 | break;
288 | }
289 |
290 | if (this._loopCount != 0)
291 | {
292 | if (this._loopEnd == 0)
293 | {
294 | this._loopEnd = this._sampleCount;
295 | }
296 | else
297 | {
298 | this._loopEnd++;
299 | }
300 |
301 | if (this._loopStart >= this._sampleCount || this._loopEnd > this._sampleCount || this._loopStart > this._loopEnd)
302 | {
303 | throw new Exception("Loops out of range");
304 | }
305 | }
306 | }
307 | }
308 |
309 | public void GenerateOGG(string fileLocation, string codebooksLocation, bool inlineCodebooks, bool fullSetup)
310 | {
311 | if (inlineCodebooks && string.IsNullOrEmpty(codebooksLocation))
312 | {
313 | throw new ArgumentException("Only one of Inline Codebooks or Codebooks Location can be set");
314 | }
315 | else if (!inlineCodebooks && string.IsNullOrEmpty(codebooksLocation))
316 | {
317 | throw new ArgumentException("Either Inline Codebooks or Codebooks Location must be set");
318 | }
319 |
320 | using (OggStream ogg = new OggStream(File.Create(fileLocation)))
321 | {
322 | bool[] modeBlockFlag = null;
323 | uint modeBits = 0;
324 | bool previousBlockFlag = false;
325 |
326 | if (this._headerTriadPresent)
327 | {
328 | GenerateOGGHeaderTriad(ogg);
329 | }
330 | else
331 | {
332 | GenerateOGGHeader(ogg, codebooksLocation, inlineCodebooks, fullSetup, ref modeBlockFlag, ref modeBits);
333 | }
334 |
335 | {
336 | uint offset = this._dataChunkOffset + this._firstAudioPacketOffset;
337 |
338 | while (offset < this._dataChunkOffset + this._dataChunkSize)
339 | {
340 | uint size;
341 | uint granule;
342 | uint packetHeaderSize;
343 | uint packetPayloadOffset;
344 | uint nextOffset;
345 |
346 | if (this._oldPacketHeaders)
347 | {
348 | Packet8 audioPacket = new Packet8(this._wemFile, offset);
349 | packetHeaderSize = audioPacket.GetHeaderSize();
350 | size = audioPacket.GetSize();
351 | packetPayloadOffset = audioPacket.GetOffset();
352 | granule = audioPacket.GetGranule();
353 | nextOffset = audioPacket.NextOffset();
354 | }
355 | else
356 | {
357 | Packet audioPacket = new Packet(this._wemFile, offset, this._noGranule);
358 | packetHeaderSize = audioPacket.GetHeaderSize();
359 | size = audioPacket.GetSize();
360 | packetPayloadOffset = audioPacket.GetOffset();
361 | granule = audioPacket.GetGranule();
362 | nextOffset = audioPacket.NextOffset();
363 | }
364 |
365 | if (offset + packetHeaderSize > this._dataChunkOffset + this._dataChunkSize)
366 | {
367 | throw new Exception("There was an error generating a vorbis packet");
368 | }
369 |
370 | offset = packetPayloadOffset;
371 |
372 | this._wemFile.Seek(offset, SeekOrigin.Begin);
373 |
374 | if (granule == 0xFFFFFFFF)
375 | {
376 | ogg.SetGranule(1);
377 | }
378 | else
379 | {
380 | ogg.SetGranule(granule);
381 | }
382 |
383 | if (this._modPackets)
384 | {
385 | if (modeBlockFlag == null)
386 | {
387 | throw new Exception("There was an error generating a vorbis packet");
388 | }
389 |
390 | byte packetType = 0;
391 | ogg.WriteBit(packetType);
392 |
393 | uint modeNumber = 0;
394 | uint remainder = 0;
395 |
396 | {
397 | BitStream bitStream = new BitStream(this._wemFile);
398 |
399 | modeNumber = bitStream.Read((int)modeBits);
400 | ogg.BitWrite(modeNumber, (byte)modeBits);
401 |
402 | remainder = bitStream.Read(8 - (int)modeBits);
403 | }
404 |
405 | if (modeBlockFlag[modeNumber])
406 | {
407 | this._wemFile.Seek(nextOffset, SeekOrigin.Begin);
408 |
409 | bool nextBlockFlag = false;
410 | if (nextOffset + packetHeaderSize <= this._dataChunkOffset + this._dataChunkSize)
411 | {
412 | Packet audioPacket = new Packet(this._wemFile, nextOffset, this._noGranule);
413 | uint nextPacketSize = audioPacket.GetSize();
414 |
415 | if (nextPacketSize != 0xFFFFFFFF)
416 | {
417 | this._wemFile.Seek(audioPacket.GetOffset(), SeekOrigin.Begin);
418 |
419 | BitStream bitStream = new BitStream(this._wemFile);
420 | uint nextModeNumber = bitStream.Read((int)modeBits);
421 |
422 | nextBlockFlag = modeBlockFlag[nextModeNumber];
423 | }
424 | }
425 |
426 | byte previousWindowType = previousBlockFlag ? (byte)1 : (byte)0;
427 | ogg.WriteBit(previousWindowType);
428 |
429 | byte nextWindowType = previousBlockFlag ? (byte)1 : (byte)0;
430 | ogg.WriteBit(nextWindowType);
431 |
432 | this._wemFile.Seek(offset + 1, SeekOrigin.Begin);
433 | }
434 |
435 | previousBlockFlag = modeBlockFlag[modeNumber];
436 | ogg.BitWrite(remainder, (byte)(8 - modeBits));
437 | }
438 | else
439 | {
440 | int b = this._wemFile.ReadByte();
441 | if (b < 0)
442 | {
443 | throw new Exception("There was an error generating a vorbis packet");
444 | }
445 |
446 | ogg.BitWrite((byte)b);
447 | }
448 |
449 | for (int i = 1; i < size; i++)
450 | {
451 | int b = this._wemFile.ReadByte();
452 | if (b < 0)
453 | {
454 | throw new Exception("There was an error generating a vorbis packet");
455 | }
456 |
457 | ogg.BitWrite((byte)b);
458 | }
459 |
460 | offset = nextOffset;
461 | ogg.FlushPage(false, (offset == this._dataChunkOffset + this._dataChunkSize));
462 | }
463 |
464 | if (offset > this._dataChunkOffset + this._dataChunkSize)
465 | {
466 | throw new Exception("There was an error while creating the OGG file");
467 | }
468 | }
469 | }
470 | }
471 |
472 | private void GenerateOGGHeaderTriad(OggStream ogg)
473 | {
474 | uint offset = this._dataChunkOffset + this._setupPacketOffset;
475 |
476 | //Information Packet
477 | {
478 | Packet8 informationPacket = new Packet8(this._wemFile, offset);
479 | uint informationPacketSize = informationPacket.GetSize();
480 |
481 | if (informationPacket.GetGranule() != 0)
482 | {
483 | throw new Exception("There was an error while creating the header");
484 | }
485 |
486 | this._wemFile.Seek(informationPacket.GetOffset(), SeekOrigin.Begin);
487 |
488 | byte[] informationPacketType = new byte[1];
489 | this._wemFile.Read(informationPacketType, 0, 1);
490 | if (informationPacketType[0] != 1)
491 | {
492 | throw new Exception("There was an error while creating the header");
493 | }
494 | ogg.BitWrite(informationPacketType[0]);
495 |
496 | byte[] b = new byte[1];
497 | for (int i = 0; i < informationPacketSize; i++)
498 | {
499 | this._wemFile.Read(b, 0, 1);
500 | ogg.BitWrite(b[0]);
501 | }
502 |
503 | ogg.FlushPage();
504 | offset = informationPacket.NextOffset();
505 | }
506 |
507 | //Comment Packet
508 | {
509 | Packet8 commentPacket = new Packet8(this._wemFile, offset);
510 | uint commentPacketSize = commentPacket.GetSize();
511 |
512 | if (commentPacket.GetGranule() != 0)
513 | {
514 | throw new Exception("There was an error while creating the header");
515 | }
516 |
517 | this._wemFile.Seek(commentPacket.GetOffset(), SeekOrigin.Begin);
518 |
519 | byte[] commentPacketType = new byte[1];
520 | this._wemFile.Read(commentPacketType, 0, 1);
521 | if (commentPacketType[0] != 3)
522 | {
523 | throw new Exception("There was an error while creating the header");
524 | }
525 | ogg.BitWrite(commentPacketType[0]);
526 |
527 | byte[] b = new byte[1];
528 | for (int i = 0; i < commentPacketSize; i++)
529 | {
530 | this._wemFile.Read(b, 0, 1);
531 | ogg.BitWrite(b[0]);
532 | }
533 |
534 | ogg.FlushPage();
535 | offset = commentPacket.NextOffset();
536 | }
537 |
538 | //Setup Packet
539 | {
540 | Packet8 setupPacket = new Packet8(this._wemFile, offset);
541 | this._wemFile.Seek(setupPacket.GetOffset(), SeekOrigin.Begin);
542 |
543 | if (setupPacket.GetGranule() != 0)
544 | {
545 | throw new Exception("There was an error while creating the header");
546 | }
547 |
548 | BitStream bitStream = new BitStream(this._wemFile);
549 | byte setupPacketType = (byte)bitStream.Read(8);
550 | if (setupPacketType != 5)
551 | {
552 | throw new Exception("There was an error while creating the header");
553 | }
554 | ogg.BitWrite(setupPacketType);
555 |
556 | for (int i = 0; i < 6; i++)
557 | {
558 | ogg.BitWrite((byte)bitStream.Read(8));
559 | }
560 |
561 | byte codebookCount = (byte)bitStream.Read(8);
562 | ogg.BitWrite(codebookCount);
563 | codebookCount++;
564 |
565 | CodebookLibrary codebook = new CodebookLibrary();
566 | for (int i = 0; i < codebookCount; i++)
567 | {
568 | codebook.Copy(bitStream, ogg);
569 | }
570 |
571 | while (bitStream.TotalBitsRead < setupPacket.GetSize() * 8)
572 | {
573 | ogg.WriteBit((byte)bitStream.Read(1));
574 | }
575 |
576 | ogg.FlushPage();
577 | offset = setupPacket.NextOffset();
578 | }
579 |
580 | if (offset != this._dataChunkOffset + this._firstAudioPacketOffset)
581 | {
582 | throw new Exception("There was an error while creating the header");
583 | }
584 | }
585 |
586 | private void GenerateOGGHeader(OggStream ogg, string codebooksLocation, bool inlineCodebooks, bool fullSetup, ref bool[] modeBlockFlag, ref uint modeBits)
587 | {
588 | ogg.WriteVorbisHeader(1);
589 | ogg.BitWrite((uint)0);
590 | ogg.BitWrite((byte)this.Channels);
591 | ogg.BitWrite(this.SampleRate);
592 | ogg.BitWrite((uint)0);
593 | ogg.BitWrite(this.AverageBytesPerSecond * 8);
594 | ogg.BitWrite((uint)0);
595 | ogg.BitWrite(this._blocksize0Pow, 4);
596 | ogg.BitWrite(this._blocksize1Pow, 4);
597 | ogg.WriteBit(1);
598 | ogg.FlushPage();
599 |
600 | //Comment Packet
601 | {
602 | ogg.WriteVorbisHeader(3);
603 |
604 | byte[] vendor = Encoding.UTF8.GetBytes("Converted from Audiokinetic Wwise by WEMSharp");
605 | ogg.BitWrite((uint)vendor.Length);
606 | for (int i = 0; i < vendor.Length; i++)
607 | {
608 | ogg.BitWrite(vendor[i]);
609 | }
610 |
611 | if (this._loopCount == 0)
612 | {
613 | ogg.BitWrite((uint)0);
614 | }
615 | else
616 | {
617 | ogg.BitWrite((uint)2);
618 |
619 | byte[] loopStart = Encoding.UTF8.GetBytes("LoopStart=" + this._loopStart.ToString());
620 | ogg.BitWrite((uint)loopStart.Length);
621 | for (int i = 0; i < loopStart.Length; i++)
622 | {
623 | ogg.BitWrite(loopStart[i]);
624 | }
625 |
626 | byte[] loopEnd = Encoding.UTF8.GetBytes("LoopEnd=" + this._loopEnd.ToString());
627 | ogg.BitWrite((uint)loopEnd.Length);
628 | for (int i = 0; i < loopEnd.Length; i++)
629 | {
630 | ogg.BitWrite(loopEnd[i]);
631 | }
632 | }
633 |
634 | ogg.WriteBit(1);
635 | ogg.FlushPage();
636 | }
637 |
638 | //Setup Packet
639 | {
640 | ogg.WriteVorbisHeader(5);
641 |
642 | Packet setupPacket = new Packet(this._wemFile, this._dataChunkOffset + this._setupPacketOffset, this._noGranule);
643 |
644 | this._wemFile.Seek(setupPacket.GetOffset(), SeekOrigin.Begin);
645 |
646 | if (setupPacket.GetGranule() != 0)
647 | {
648 | throw new Exception("There was an error generating a vorbis packet");
649 | }
650 |
651 | BitStream bitStream = new BitStream(this._wemFile);
652 |
653 | uint codebookCount = bitStream.Read(8);
654 | ogg.BitWrite((byte)codebookCount);
655 | codebookCount++;
656 |
657 | if (inlineCodebooks)
658 | {
659 | CodebookLibrary codebook = new CodebookLibrary();
660 |
661 | for (int i = 0; i < codebookCount; i++)
662 | {
663 | if (fullSetup)
664 | {
665 | codebook.Copy(bitStream, ogg);
666 | }
667 | else
668 | {
669 | codebook.Rebuild(bitStream, 0, ogg);
670 | }
671 | }
672 | }
673 | else
674 | {
675 | CodebookLibrary codebook = new CodebookLibrary(codebooksLocation);
676 |
677 | for (int i = 0; i < codebookCount; i++)
678 | {
679 | ushort codebookID = (ushort)bitStream.Read(10);
680 | codebook.Rebuild(codebookID, ogg);
681 | }
682 | }
683 |
684 | byte timeCountLess1 = 0;
685 | ushort dummyTimeValue = 0;
686 | ogg.BitWrite(timeCountLess1, 6);
687 | ogg.BitWrite(dummyTimeValue);
688 |
689 | if (fullSetup)
690 | {
691 | while (bitStream.TotalBitsRead < setupPacket.GetSize() * 8U)
692 | {
693 | ogg.WriteBit((byte)bitStream.Read(1));
694 | }
695 | }
696 | else
697 | {
698 | byte floorCount = (byte)bitStream.Read(6);
699 | ogg.BitWrite(floorCount, 6);
700 | floorCount++;
701 |
702 | for (int i = 0; i < floorCount; i++)
703 | {
704 | ushort floorType = 1;
705 | ogg.BitWrite(floorType);
706 |
707 | byte floor1Partitions = (byte)bitStream.Read(5);
708 | ogg.BitWrite(floor1Partitions, 5);
709 |
710 | uint[] floor1PartitionClassList = new uint[floor1Partitions];
711 | uint maximumClass = 0;
712 | for (int j = 0; j < floor1Partitions; j++)
713 | {
714 | byte floor1PartitionClass = (byte)bitStream.Read(4);
715 | ogg.BitWrite(floor1PartitionClass, 4);
716 |
717 | floor1PartitionClassList[j] = floor1PartitionClass;
718 |
719 | if (floor1PartitionClass > maximumClass)
720 | {
721 | maximumClass = floor1PartitionClass;
722 | }
723 | }
724 |
725 | uint[] floor1ClassDimensionList = new uint[maximumClass + 1];
726 | for (int j = 0; j <= maximumClass; j++)
727 | {
728 | byte classDimension = (byte)bitStream.Read(3);
729 | ogg.BitWrite(classDimension, 3);
730 |
731 | floor1ClassDimensionList[j] = classDimension + 1U;
732 |
733 | byte classSubclasses = (byte)bitStream.Read(2);
734 | ogg.BitWrite(classSubclasses, 2);
735 |
736 | if (classSubclasses != 0)
737 | {
738 | byte masterbook = (byte)bitStream.Read(8);
739 | ogg.BitWrite(masterbook);
740 |
741 | if (maximumClass >= codebookCount)
742 | {
743 | throw new Exception("There was an error generating a vorbis packet.");
744 | }
745 | }
746 |
747 | for (int k = 0; k < (1 << classSubclasses); k++)
748 | {
749 | byte subclassBook = (byte)bitStream.Read(8);
750 | ogg.BitWrite(subclassBook);
751 |
752 | if ((subclassBook - 1) >= 0 && (subclassBook - 1) >= codebookCount)
753 | {
754 | throw new Exception("There was an error generating a vorbis packet.");
755 | }
756 | }
757 | }
758 |
759 | byte floor1Multiplier = (byte)bitStream.Read(2);
760 | ogg.BitWrite(floor1Multiplier, 2);
761 |
762 | byte rangeBits = (byte)bitStream.Read(4);
763 | ogg.BitWrite(rangeBits, 4);
764 |
765 | for (int j = 0; j < floor1Partitions; j++)
766 | {
767 | uint currentClassNumber = floor1PartitionClassList[j];
768 | for (int k = 0; k < floor1ClassDimensionList[currentClassNumber]; k++)
769 | {
770 | ogg.BitWrite(bitStream.Read(rangeBits), rangeBits);
771 | }
772 | }
773 | }
774 |
775 | byte residueCount = (byte)bitStream.Read(6);
776 | ogg.BitWrite(residueCount, 6);
777 | residueCount++;
778 |
779 | for (int i = 0; i < residueCount; i++)
780 | {
781 | byte residueType = (byte)bitStream.Read(2);
782 | ogg.BitWrite((ushort)residueType);
783 |
784 | if (residueType > 2)
785 | {
786 | throw new Exception("There was an error generating a vorbis packet.");
787 | }
788 |
789 | uint residueBegin = bitStream.Read(24);
790 | uint residueEnd = bitStream.Read(24);
791 | uint residuePartitionSize = bitStream.Read(24);
792 | byte residueClassifications = (byte)bitStream.Read(6);
793 | byte residueClassbook = (byte)bitStream.Read(8);
794 |
795 | ogg.BitWrite(residueBegin, 24);
796 | ogg.BitWrite(residueEnd, 24);
797 | ogg.BitWrite(residuePartitionSize, 24);
798 | ogg.BitWrite(residueClassifications, 6);
799 | ogg.BitWrite(residueClassbook, 8);
800 |
801 | residueClassifications++;
802 |
803 | if (residueClassbook >= codebookCount)
804 | {
805 | throw new Exception("There was an error generating a vorbis packet.");
806 | }
807 |
808 | uint[] residueCascade = new uint[residueClassifications];
809 | for (int j = 0; j < residueClassifications; j++)
810 | {
811 | byte highBits = 0;
812 | byte lowBits = (byte)bitStream.Read(3);
813 |
814 | ogg.BitWrite(lowBits, 3);
815 |
816 | byte bitFlag = (byte)bitStream.Read(1);
817 | ogg.WriteBit(bitFlag);
818 |
819 | if (bitFlag == 1)
820 | {
821 | highBits = (byte)bitStream.Read(5);
822 | ogg.BitWrite(highBits, 5);
823 | }
824 |
825 | residueCascade[j] = highBits * 8U + lowBits;
826 | }
827 |
828 | for (int j = 0; j < residueClassifications; j++)
829 | {
830 | for (int k = 0; k < 8; k++)
831 | {
832 | if ((residueCascade[j] & (1 << k)) != 0)
833 | {
834 | byte residueBook = (byte)bitStream.Read(8);
835 | ogg.BitWrite(residueBook);
836 |
837 | if (residueBook >= codebookCount)
838 | {
839 | throw new Exception("There was an error generating a vorbis packet.");
840 | }
841 | }
842 | }
843 | }
844 | }
845 |
846 | byte mappingCount = (byte)bitStream.Read(6);
847 | ogg.BitWrite(mappingCount, 6);
848 | mappingCount++;
849 |
850 | for (int i = 0; i < mappingCount; i++)
851 | {
852 | ushort mappingType = 0;
853 | ogg.BitWrite(mappingType);
854 |
855 | byte submapsFlag = (byte)bitStream.Read(1);
856 | ogg.WriteBit(submapsFlag);
857 |
858 | uint submaps = 1;
859 | if (submapsFlag == 1)
860 | {
861 | byte submapsLess = (byte)bitStream.Read(4);
862 | submaps = submapsLess + 1U;
863 | ogg.BitWrite(submapsLess, 4);
864 | }
865 |
866 | byte squarePolarFlag = (byte)bitStream.Read(1);
867 | ogg.WriteBit(squarePolarFlag);
868 |
869 | if (squarePolarFlag == 1)
870 | {
871 | byte couplingSteps = (byte)bitStream.Read(8);
872 | ogg.BitWrite(couplingSteps);
873 | couplingSteps++;
874 |
875 | for (int j = 0; j < couplingSteps; j++)
876 | {
877 | uint magnitude = bitStream.Read((int)ILog(this.Channels - 1U));
878 | uint angle = bitStream.Read((int)ILog(this.Channels - 1U));
879 |
880 | ogg.BitWrite(magnitude, (byte)ILog(this.Channels - 1U));
881 | ogg.BitWrite(angle, (byte)ILog(this.Channels - 1U));
882 |
883 | if (angle == magnitude || magnitude >= this.Channels || angle >= this.Channels)
884 | {
885 | throw new Exception("There was an error generating a vorbis packet.");
886 | }
887 | }
888 | }
889 |
890 | byte mappingReserved = (byte)bitStream.Read(2);
891 | ogg.BitWrite(mappingReserved, 2);
892 |
893 | if (mappingReserved != 0)
894 | {
895 | throw new Exception("There was an error generating a vorbis packet.");
896 | }
897 |
898 | if (submaps > 1)
899 | {
900 | for (int j = 0; j < this.Channels; j++)
901 | {
902 | byte mappingMux = (byte)bitStream.Read(4);
903 | ogg.BitWrite(mappingMux, 4);
904 |
905 | if (mappingMux >= submaps)
906 | {
907 | throw new Exception("There was an error generating a vorbis packet.");
908 | }
909 | }
910 | }
911 |
912 | for (int j = 0; j < submaps; j++)
913 | {
914 | byte timeConfig = (byte)bitStream.Read(8);
915 | ogg.BitWrite(timeConfig);
916 |
917 | byte floorNumber = (byte)bitStream.Read(8);
918 | ogg.BitWrite(floorNumber);
919 | if (floorNumber >= floorCount)
920 | {
921 | throw new Exception("There was an error generating a vorbis packet.");
922 | }
923 |
924 | byte residueNumber = (byte)bitStream.Read(8);
925 | ogg.BitWrite(residueNumber);
926 | if (residueNumber >= residueCount)
927 | {
928 | throw new Exception("There was an error generating a vorbis packet.");
929 | }
930 | }
931 | }
932 |
933 | byte modeCount = (byte)bitStream.Read(6);
934 | ogg.BitWrite(modeCount, 6);
935 | modeCount++;
936 |
937 | modeBlockFlag = new bool[modeCount];
938 | modeBits = ILog(modeCount - 1U);
939 |
940 | for (int i = 0; i < modeCount; i++)
941 | {
942 | byte blockFlag = (byte)bitStream.Read(1);
943 | ogg.WriteBit(blockFlag);
944 |
945 | modeBlockFlag[i] = blockFlag == 1;
946 |
947 | ushort windowType = 0;
948 | ushort transformType = 0;
949 | ogg.BitWrite(windowType);
950 | ogg.BitWrite(transformType);
951 |
952 | byte mapping = (byte)bitStream.Read(8);
953 | ogg.BitWrite(mapping, 8);
954 |
955 | if (mapping >= mappingCount)
956 | {
957 | throw new Exception("There was an error generating a vorbis packet.");
958 | }
959 | }
960 |
961 | ogg.WriteBit(1);
962 | }
963 |
964 | ogg.FlushPage();
965 |
966 | if ((bitStream.TotalBitsRead + 7) / 8 != setupPacket.GetSize())
967 | {
968 | throw new Exception("There was an error generating a vorbis packet.");
969 | }
970 |
971 | if (setupPacket.NextOffset() != this._dataChunkOffset + this._firstAudioPacketOffset)
972 | {
973 | throw new Exception("There was an error generating a vorbis packet.");
974 | }
975 | }
976 | }
977 |
978 | //https://xiph.org/vorbis/doc/Vorbis_I_spec.pdf#subsubsection.9.2.1
979 | private uint ILog(uint value)
980 | {
981 | uint ret = 0;
982 |
983 | while (value != 0)
984 | {
985 | ret++;
986 | value >>= 1;
987 | }
988 |
989 | return ret;
990 | }
991 | }
992 |
993 | ///
994 | /// Forcing of the OGG Packet Format
995 | ///
996 | public enum WEMForcePacketFormat
997 | {
998 | ///
999 | /// Uses the original Mod Packet Format from the WEM file
1000 | ///
1001 | NoForcePacketFormat,
1002 | ///
1003 | /// Forces to modify the original packets
1004 | ///
1005 | ForceModPackets,
1006 | ///
1007 | /// Forces to not modify the original packets
1008 | ///
1009 | ForceNoModPackets
1010 | }
1011 | }
--------------------------------------------------------------------------------
/WEMSharp/WEMSharp.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {2EDFD763-C9CF-459E-9227-AFEBCAD33850}
8 | Library
9 | Properties
10 | WEMSharp
11 | WEMSharp
12 | v4.6.2
13 | 512
14 |
15 |
16 | true
17 | full
18 | false
19 | bin\Debug\
20 | DEBUG;TRACE
21 | prompt
22 | 4
23 |
24 |
25 | pdbonly
26 | true
27 | bin\Release\
28 | TRACE
29 | prompt
30 | 4
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/WEMSharpTests/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/WEMSharpTests/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using WEMSharp;
7 |
8 | namespace WEMSharpTests
9 | {
10 | class Program
11 | {
12 | static void Main(string[] args)
13 | {
14 | WEMFile wem = new WEMFile("LoL_MUSIC_TreelineINTRO.wem", WEMForcePacketFormat.NoForcePacketFormat);
15 | wem.GenerateOGG("LoL_MUSIC_TreelineINTRO.ogg", "packed_codebooks_aoTuV_603.bin", false, false);
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/WEMSharpTests/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("WEMSharpTests")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("WEMSharpTests")]
13 | [assembly: AssemblyCopyright("Copyright © 2018")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("b44cd6c2-e676-4a51-a16b-7d6f8bedf6fc")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/WEMSharpTests/WEMSharpTests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {B44CD6C2-E676-4A51-A16B-7D6F8BEDF6FC}
8 | Exe
9 | WEMSharpTests
10 | WEMSharpTests
11 | v4.6.2
12 | 512
13 | true
14 |
15 |
16 | AnyCPU
17 | true
18 | full
19 | false
20 | bin\Debug\
21 | DEBUG;TRACE
22 | prompt
23 | 4
24 |
25 |
26 | AnyCPU
27 | pdbonly
28 | true
29 | bin\Release\
30 | TRACE
31 | prompt
32 | 4
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 | {2edfd763-c9cf-459e-9227-afebcad33850}
54 | WEMSharp
55 |
56 |
57 |
58 |
--------------------------------------------------------------------------------