UNPKG

chromaprint-fixed

Version:

A JavaScript implementation of AcoustID Chromaprint

162 lines (129 loc) 9.37 kB
<!DOCTYPE html> <html> <head> <title>fpcalc.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"> <link rel="stylesheet" media="all" href="../docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <ul id="jump_to"> <li> <a class="large" href="javascript:void(0);">Jump To &hellip;</a> <a class="small" href="javascript:void(0);">+</a> <div id="jump_wrapper"> <div id="jump_page_wrapper"> <div id="jump_page"> <a class="source" href="algorithms.html"> src/algorithms.coffee </a> <a class="source" href="calculator.html"> src/calculator.coffee </a> <a class="source" href="chroma.html"> src/chroma.coffee </a> <a class="source" href="fpcalc.html"> src/fpcalc.coffee </a> <a class="source" href="silenceRemover.html"> src/silenceRemover.coffee </a> </div> </div> </li> </ul> <ul class="sections"> <li id="title"> <div class="annotation"> <h1>fpcalc.coffee</h1> </div> </li> <li id="section-1"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-1">&#182;</a> </div> </div> <div class="content"><div class='highlight'><pre><span class="hljs-keyword">return</span> <span class="hljs-keyword">unless</span> <span class="hljs-built_in">require</span>? <span class="hljs-keyword">and</span> <span class="hljs-built_in">module</span>? <span class="hljs-comment"># protect the browser</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>) cli = <span class="hljs-built_in">require</span>(<span class="hljs-string">'commander'</span>) path = <span class="hljs-built_in">require</span>(<span class="hljs-string">'path'</span>) Promise = <span class="hljs-built_in">require</span>(<span class="hljs-string">'q'</span>) cli.description <span class="hljs-string">"Print the audio fingerprint of one or more files."</span> cli.usage <span class="hljs-string">'[options] &lt;file ...&gt;'</span> cli.version <span class="hljs-built_in">require</span>(<span class="hljs-string">'../package'</span>).version cli.option <span class="hljs-string">'-l, --length SECS'</span>, <span class="hljs-string">'seconds of audio to scan [120]'</span>, parseInt, <span class="hljs-number">120</span> cli.option <span class="hljs-string">'-r, --raw'</span>, <span class="hljs-string">'output the raw uncompressed fingerprint'</span> cli.option <span class="hljs-string">'-a, --algo TEST'</span>, <span class="hljs-string">'choose an algorithm (1-4) [2]'</span>, parseInt, <span class="hljs-number">2</span> <span class="hljs-function"> <span class="hljs-title">print</span> = <span class="hljs-params">(file, length, fingerprint)</span> -&gt;</span> <span class="hljs-built_in">console</span>.log <span class="hljs-string">"FILE=<span class="hljs-subst">#{file}</span>"</span> <span class="hljs-built_in">console</span>.log <span class="hljs-string">"DURATION=<span class="hljs-subst">#{length}</span>"</span> <span class="hljs-built_in">console</span>.log <span class="hljs-string">"FINGERPRINT=<span class="hljs-subst">#{fingerprint}</span>"</span> <span class="hljs-built_in">console</span>.log <span class="hljs-string">""</span> <span class="hljs-function"> <span class="hljs-title">fpcalc</span> = <span class="hljs-params">(file, length, raw, algorithm, callback)</span> -&gt;</span> offset = <span class="hljs-number">0</span> position = <span class="hljs-literal">null</span> l = <span class="hljs-number">2048</span> audioBuffer = <span class="hljs-keyword">new</span> Buffer(l) <span class="hljs-function"> <span class="hljs-title">readCallback</span> = <span class="hljs-params">(err, bytesRead, buffer)</span> -&gt;</span> <span class="hljs-built_in">console</span>.log(err) <span class="hljs-keyword">if</span> err? <span class="hljs-keyword">return</span> callback(err) <span class="hljs-keyword">if</span> err? <span class="hljs-built_in">console</span>.log <span class="hljs-string">"Read <span class="hljs-subst">#{bytesRead}</span> bytes =&gt; <span class="hljs-subst">#{buffer}</span>"</span> <span class="hljs-built_in">console</span>.log <span class="hljs-string">" audioBuffer is now: <span class="hljs-subst">#{buffer}</span>"</span> <span class="hljs-built_in">console</span>.log <span class="hljs-string">" ... I think I will stop now."</span> callback(<span class="hljs-literal">null</span>, audioBuffer) <span class="hljs-function"> <span class="hljs-title">read</span> = <span class="hljs-params">(err, fd)</span> -&gt;</span> <span class="hljs-built_in">console</span>.log(err) <span class="hljs-keyword">if</span> err? <span class="hljs-keyword">return</span> callback(err) <span class="hljs-keyword">if</span> err? fs.read(fd, audioBuffer, offset, l, position, callback) fs.exists path.resolve(file), <span class="hljs-function"><span class="hljs-params">(ok)</span> -&gt;</span> <span class="hljs-built_in">console</span>.log <span class="hljs-string">"Error - no such file"</span> <span class="hljs-keyword">return</span> fs.open(file, <span class="hljs-string">'r'</span>, read) <span class="hljs-keyword">if</span> ok callback Error(<span class="hljs-string">"<span class="hljs-subst">#{file}</span> - no such file "</span>) <span class="hljs-function"> <span class="hljs-title">fpcalcbytes</span> = <span class="hljs-params">(file, length, raw, algorithm, callback)</span> -&gt;</span> offset = <span class="hljs-number">0</span> position = <span class="hljs-literal">null</span> l = <span class="hljs-number">2048</span> audioBuffer = <span class="hljs-keyword">new</span> Buffer(l) <span class="hljs-function"> <span class="hljs-title">readCallback</span> = <span class="hljs-params">(err, bytesRead, buffer)</span> -&gt;</span> <span class="hljs-built_in">console</span>.log(err) <span class="hljs-keyword">if</span> err? <span class="hljs-keyword">return</span> callback(err) <span class="hljs-keyword">if</span> err? <span class="hljs-built_in">console</span>.log <span class="hljs-string">"Read <span class="hljs-subst">#{bytesRead}</span> bytes =&gt; <span class="hljs-subst">#{buffer}</span>"</span> <span class="hljs-built_in">console</span>.log <span class="hljs-string">" audioBuffer is now: <span class="hljs-subst">#{buffer}</span>"</span> <span class="hljs-built_in">console</span>.log <span class="hljs-string">" ... I think I will stop now."</span> callback(<span class="hljs-literal">null</span>, audioBuffer) <span class="hljs-function"> <span class="hljs-title">read</span> = <span class="hljs-params">(err, fd)</span> -&gt;</span> <span class="hljs-built_in">console</span>.log(err) <span class="hljs-keyword">if</span> err? <span class="hljs-keyword">return</span> callback(err) <span class="hljs-keyword">if</span> err? fs.read(fd, audioBuffer, offset, l, position, callback) fs.exists path.resolve(file), <span class="hljs-function"><span class="hljs-params">(ok)</span> -&gt;</span> <span class="hljs-built_in">console</span>.log <span class="hljs-string">"Error - no such file"</span> <span class="hljs-keyword">return</span> fs.open(file, <span class="hljs-string">'r'</span>, read) <span class="hljs-keyword">if</span> ok callback Error(<span class="hljs-string">"<span class="hljs-subst">#{file}</span> - no such file "</span>) <span class="hljs-function"> <span class="hljs-title">run</span> = <span class="hljs-params">(args = process.argv)</span> -&gt;</span> cli.parse(args) raw = !!cli.raw length = cli.length <span class="hljs-keyword">or</span> <span class="hljs-number">120</span> algorithm = cli.algorithm <span class="hljs-keyword">or</span> <span class="hljs-number">2</span> cli.help() <span class="hljs-keyword">unless</span> cli.args.length <span class="hljs-comment"># exits immediately</span> <span class="hljs-function"> <span class="hljs-title">f</span> = <span class="hljs-params">(file, cb)</span> -&gt;</span> fpcalc(file, length, raw, algorithm, cb) cli.args.reverse().reduce(<span class="hljs-function">(<span class="hljs-params">(p, n)</span> -&gt;</span> (<span class="hljs-function">-&gt;</span> f(n, p))), <span class="hljs-function">-&gt;</span>)() <span class="hljs-built_in">module</span>?.exports = {cli: run, fpcalc: fpcalc, fpcalcbytes: fpcalcbytes}</pre></div></div> </li> </ul> </div> </body> </html>