Since there is currently no universal way to read live data from an audio track in JavaScript I’m using a small library/API to read volume data from a text file that I converted from an MP3 offline.
The string looks like this
!!!!!!!!!!!!!!!!!!!!!!!!!!###"~{~||ysvgfiw`gXg}i}|mbnTaac[Wb~v|xqsfSeYiV`R ][\Z^RdZ\XX`Ihb\O`3Z1W*I'D'H&J&J'O&M&O%O&I&M&S&R&R%U&W&T&V&m%\%n%[%Y%I&O'P'G 'L(V'X&I'F(O&a&h'[&W'P&C'](I&R&Y'\)\'Y'G(O'X'b'f&N&S&U'N&P&J'N)O'R)K'T(f|`|d //etc...
and the idea is basically that at a given point in the song the Unicode number of the character at the corresponding point in the text file yields a nominal value to represent volume.
The library translates the data (in this case, a stereo track) with the following (simplified here):
getVolume = function(sampleIndex,o) { o.left = Math.min(1,(this.data.charCodeAt(sampleIndex*2|0)-33)/93); o.right = Math.min(1,(this.data.charCodeAt(sampleIndex*2+1|0)-33)/93); }
I’d like some insight into how the file was encoded in the first place, and how I’m making use of it here.
What is the significance of 93 and 33?
What is the purpose of the bitwise |?
Is this a common means of porting information (ie, does it have a name), or is there a better way to do it?
33and93are used for normalizing values beween!and~.The
|0is there due to the fact thatsampleIndex*2orsampleIndex*2+1will yield a non-integer value when being passed a non-integersampleIndex.|0truncates the decimal part just in case someone sends in an incorrectly formattedsampleIndex(i.e. non-integer).