From 208688441ccc6d9359273680d57058fa728ce2af Mon Sep 17 00:00:00 2001 From: Ben Titcomb Date: Tue, 1 Mar 2016 06:18:38 -0800 Subject: [PATCH] Remove the concept of a register/processor. --- build/dtmf.js | 22 +++++++++------------- build/goertzel.js | 29 ++++++++++------------------- package.json | 3 ++- src/dtmf.coffee | 16 +++++++--------- src/goertzel.coffee | 16 +++++++++------- 5 files changed, 37 insertions(+), 49 deletions(-) diff --git a/build/dtmf.js b/build/dtmf.js index 22689eb8..8a2283d3 100644 --- a/build/dtmf.js +++ b/build/dtmf.js @@ -85,19 +85,8 @@ DTMF = (function() { } }; - DTMF.prototype.floatBufferToInt = function(floatBuffer) { - var i, intBuffer; - intBuffer = []; - i = 0; - while (i < floatBuffer.length) { - intBuffer.push(Goertzel.Utilities.floatToIntSample(floatBuffer[i])); - i++; - } - return intBuffer; - }; - DTMF.prototype.processBuffer = function(buffer) { - var badPeaks, energy, f, freq, frequency, handler, highEnergies, i, intSample, j, len, lowEnergies, ref, register, value, windowedSample; + var badPeaks, energy, f, freq, frequency, handler, highEnergies, i, intSample, j, len, lowEnergies, ref, register, result, value, windowedSample; value = ''; intSample = void 0; register = void 0; @@ -106,6 +95,7 @@ DTMF = (function() { highEnergies = []; lowEnergies = []; frequency = void 0; + result = []; i = 0; while (i < buffer.length) { intSample = buffer[i]; @@ -131,6 +121,7 @@ DTMF = (function() { if (value === this.firstPreviousValue && value !== void 0) { this.repeatCounter += 1; if (this.repeatCounter === this.repeatMin) { + result.push(value); ref = this.decodeHandlers; for (j = 0, len = ref.length; j < len; j++) { handler = ref[j]; @@ -142,7 +133,8 @@ DTMF = (function() { this.firstPreviousValue = value; } } - return this.goertzel.refresh(); + this.goertzel.refresh(); + return result; }; DTMF.prototype.on = function(eventName, handler) { @@ -155,3 +147,7 @@ DTMF = (function() { return DTMF; })(); + +if (typeof module !== "undefined" && module !== null ? module.exports : void 0) { + module.exports = DTMF; +} diff --git a/build/goertzel.js b/build/goertzel.js index f0f360aa..70a491c6 100644 --- a/build/goertzel.js +++ b/build/goertzel.js @@ -50,25 +50,6 @@ Goertzel = (function() { return this; }; - Goertzel.prototype.max = function() { - var frequency, j, len, max, ref, results; - max = void 0; - ref = this.frequencies; - results = []; - for (j = 0, len = ref.length; j < len; j++) { - frequency = ref[j]; - if (max === void 0) { - results.push(max = { - frequency: frequency, - energy: this.energies[frequency] - }); - } else { - results.push(void 0); - } - } - return results; - }; - Goertzel.prototype._getEnergyOfFrequency = function(sample, frequency) { var coefficient, power, sine; this.currentSample = sample; @@ -177,6 +158,16 @@ Goertzel = (function() { i++; } return buffer; + }, + floatBufferToInt: function(floatBuffer) { + var i, intBuffer; + intBuffer = []; + i = 0; + while (i < floatBuffer.length) { + intBuffer.push(Goertzel.Utilities.floatToIntSample(floatBuffer[i])); + i++; + } + return intBuffer; } }; diff --git a/package.json b/package.json index bdbcbabd..a3c1f2c3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "goertzeljs", - "version": "2.0.0", + "version": "3.0.0", "description": "A pure JavaScript implementation of the Goertzel algorithm.", "main": "build/goertzel.js", "scripts": { @@ -29,6 +29,7 @@ "coffee-script": "^1.10.0", "finalhandler": "^0.4.1", "jasmine": "^2.4.1", + "jasmine-expect": "^2.0.2", "jasmine-node": "^1.14.5", "serve-static": "^1.10.2" } diff --git a/src/dtmf.coffee b/src/dtmf.coffee index a76c8c37..75fa8ba2 100644 --- a/src/dtmf.coffee +++ b/src/dtmf.coffee @@ -62,14 +62,6 @@ class DTMF return @frequencyTable[lowFrequency][highFrequency] or null return - floatBufferToInt: (floatBuffer) -> - intBuffer = [] - i = 0 - while i < floatBuffer.length - intBuffer.push Goertzel.Utilities.floatToIntSample(floatBuffer[i]) - i++ - intBuffer - processBuffer: (buffer) -> value = '' intSample = undefined @@ -79,6 +71,7 @@ class DTMF highEnergies = [] lowEnergies = [] frequency = undefined + result = [] # Downsample by choosing every Nth sample. i = 0 while i < buffer.length @@ -104,13 +97,18 @@ class DTMF if value == @firstPreviousValue and value != undefined @repeatCounter += 1 if @repeatCounter == @repeatMin + result.push value for handler in @decodeHandlers setTimeout handler(value), 0 else @repeatCounter = 0 @firstPreviousValue = value @goertzel.refresh() + result on: (eventName, handler) -> switch eventName - when "decode" then @decodeHandlers.push(handler) \ No newline at end of file + when "decode" then @decodeHandlers.push(handler) + + +module.exports = DTMF if module?.exports \ No newline at end of file diff --git a/src/goertzel.coffee b/src/goertzel.coffee index f7421b50..bce967ef 100644 --- a/src/goertzel.coffee +++ b/src/goertzel.coffee @@ -5,7 +5,7 @@ class Goertzel @frequencies = options.frequencies @refresh() - refresh: () -> + refresh: -> ## Re-initializes Goertzel when we are taking in a new buffer @[attr] = {} for attr in ['firstPrevious', 'secondPrevious', 'totalPower', 'filterLength', 'energies'] @_initializeCoefficients(@frequencies) unless @coefficient @@ -18,12 +18,6 @@ class Goertzel @_getEnergyOfFrequency sample, frequency @ # returning self would be most useful here - max: -> - max = undefined - for frequency in @frequencies - if max == undefined - max = {frequency: frequency, energy: @energies[frequency]} - ## private _getEnergyOfFrequency: (sample, frequency) -> ## Main algorithm @@ -115,4 +109,12 @@ class Goertzel i++ buffer + floatBufferToInt: (floatBuffer) -> + intBuffer = [] + i = 0 + while i < floatBuffer.length + intBuffer.push Goertzel.Utilities.floatToIntSample(floatBuffer[i]) + i++ + intBuffer + module.exports = Goertzel if module?.exports \ No newline at end of file