Skip to content

A simple decoder/player for WavPack audio files using WebAssembly.

License

Notifications You must be signed in to change notification settings

baadc0de/WebAssembly-WavPack

 
 

Repository files navigation

////////////////////////////////////////////////////////////////////////////////////////////////
//                            WavPack WebAssembly Decoder/Player                              //
//                          Copyright (c)2021-2022 Peter McQuillan                            //
//                                  All Rights Reserved.                                      //
//      Distributed under the BSD Software License (see wavpack_webassembly_license.txt)      //
////////////////////////////////////////////////////////////////////////////////////////////////

This package contains the source code for a WebAssembly decoder (and very simple player) for
WavPack files. This version is built on top of the WavPack Tiny Decoder, so it will not decode
(very old) version 3 files. This will also not decode DSD WavPack files.

The main WebAssembly code is written in C and emscripten is used to generate the WebAssembly
output. Javascript is used to communicate with the WebAssembly code.

To compile the C code, you can look at the included mybuild.bat file, though obviously this 
would need to be modified to point at the location on your local machine to emcc (Emscripten
compiler).
This will generate 2 files, wavpack.js and wavpack.wasm

There are 2 versions of HTML/JavaScript for interfacing with the WebAssembly code that you
can try:

A version where we're filling a buffer and using Web Audio to play the buffer.
For this version mywavpack.htm is the HTML file, this will let you try out the decoder.
This HTML file calls mywavpack.js which is the main JavaScript code, this calls the 
WebAssembly code. This version of the code may give small audio glitches when playing 
back some WavPack files.

A version using an AudioWorklet interface to Web Audio
For this version the HTML to load is index.htm
This HTML file calls mywavpack_worklet.js which in turn calls messenger-processor.js
This version is a more modern approach to Web Audio and should give more consistent results on 
supported browsers.
 
The HTML files have to be called via a web server, due to security issues it will not work by
simply opening the file with your web browser - however you can call it from any webserver 
including one on your own machine - for development purposes I used one from python, called 
(in the directory where I have all these files) with:
python -m http.server

I use a simple 'hack' in order to allow the WebAssembly code to read the contents of
the WavPack file you choose to play, it makes a copy of the file which is in the correct 
security context that the WebAssembly file can read from.
The buffer it uses to retrieve the decoded PCM data is shared with the WebAssembly code - I
use a malloc in the code to reserve a small amount of memory - I don't use a corresponding 
'free' as this reserved memory gets re-used if you load/play subsequent WavPack files.

My code is built on top of the WavPack tiny decoder, however this could probably be modified 
to use the full WavPack source code if you wanted to!

Please direct any questions or comments to beatofthedrum@gmail.com

About

A simple decoder/player for WavPack audio files using WebAssembly.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 73.1%
  • C 26.5%
  • Other 0.4%