Skip to content

Commit

Permalink
Clean #processBuffer a bit and rely on Goertzel#eachDownsample for do…
Browse files Browse the repository at this point in the history
…wnsampling.
  • Loading branch information
Ben Titcomb committed Mar 1, 2016
1 parent d065674 commit c367867
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 47 deletions.
26 changes: 11 additions & 15 deletions build/dtmf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 13 additions & 10 deletions build/goertzel.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

coffee -o build -c -b src/goertzel.coffee
coffee -o build -c -b src/dtmf.coffee
coffee -c -b spec/goertzel.spec.coffee
coffee -c -b spec/goertzel.spec.coffee
coffee -c -b spec/dtmf.spec.coffee
19 changes: 6 additions & 13 deletions src/dtmf.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,26 @@ class DTMF

processBuffer: (buffer) ->
value = ''
intSample = undefined
register = undefined
windowedSample = undefined
energy = undefined
highEnergies = []
lowEnergies = []
frequency = undefined
result = []
# Downsample by choosing every Nth sample.
Goertzel.Utilities.eachDownsample buffer, @downsampleRate, (sample,i,downSampledBufferLength)=>
windowedSample = Goertzel.Utilities.exactBlackman(sample, i, downSampledBufferLength)
@goertzel.processSample(windowedSample)
value = @energyProfileToCharacter(@goertzel)
i = 0
while i < buffer.length
intSample = buffer[i]
windowedSample = Goertzel.Utilities.exactBlackman(intSample, i, buffer.length / @downsampleRate)
register = @goertzel.processSample(windowedSample)
value = @energyProfileToCharacter(register)
i += @downsampleRate
# END DOWNSAMPLE
# Run peak test to throw out samples with too many energy spectrum peaks or where the difference between energies is not great enough.
highEnergies = []
while i < @highFrequencies.length
f = @highFrequencies[i]
highEnergies.push register.energies[f]
highEnergies.push @goertzel.energies[f]
i++
lowEnergies = []
while i < @lowFrequencies.length
freq = @lowFrequencies[i]
lowEnergies.push register.energies[freq]
lowEnergies.push @goertzel.energies[freq]
i++
badPeaks = Goertzel.Utilities.doublePeakFilter(highEnergies, lowEnergies, @peakFilterSensitivity)
if badPeaks == false
Expand Down
20 changes: 12 additions & 8 deletions src/goertzel.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,27 @@ class Goertzel
Math.round intSample

downsampleBuffer: (buffer, downsampleRate, mapSample) ->
downsampledBuffer = []
bufferLength = buffer.length
# Prefer Uint8ClampedArray for performance
downsampledBuffer = new (Uint8ClampedArray or Array)(bufferLength / downsampleRate)
i = 0
while i < buffer.length
while i < bufferLength
sample = buffer[i]
if mapSample
downsampledBuffer.push mapSample(sample, i, buffer.length, downsampleRate)
downsampledBuffer[i] = mapSample(sample, i, buffer.length, downsampleRate)
else
downsampledBuffer.push sample
downsampledBuffer[i] = sample
i += downsampleRate
downsampledBuffer

eachDownsample: (buffer, downsampleRate, fn) ->
eachDownsample: (buffer, downSampleRate, fn) ->
i = 0
while i < buffer.length
bufferLength = buffer.length
downSampledBufferLength = bufferLength / downSampleRate
while i < bufferLength
sample = buffer[i]
fn?(sample, i, buffer.length, downsampleRate)
i += downsampleRate
fn?(sample, i, downSampledBufferLength)
i += downSampleRate

hamming: (sample, sampleIndex, bufferSize) ->
sample * (0.54 - 0.46 * Math.cos(2 * Math.PI * sampleIndex / bufferSize))
Expand Down

0 comments on commit c367867

Please sign in to comment.