Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edumo fft #304

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ For a more fine-grained look at ideas and bugs, take a look at <a href="https://
* Thomas van den Berg aka noio: support for midi input, refactoring of event system, tweaking of several bpm-related functions.
* Matthew Lawrence: replaced the old "plasters and regexes" autocoder hack with a much more proper lexer-based solution.
* Julien Dorra: contributed to french version, refactoring and great UX/UI feedback.
* Eduardo Moriana: contributed the audio input and FFT analysis code.

#### Documentation ####
Please check the docs-overview file in the docs directory for an
Expand Down
2 changes: 2 additions & 0 deletions src/app/programs/programs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ programs.demos.industrial = require('./demos/soundindustrial')
programs.demos.overscratch = require('./demos/soundoverscratch')
programs.demos.trythemall = require('./demos/soundtrythemall')
programs.demos.djcastro = require('./demos/sounddjcastro')
programs.demos.fftmic = require('../../programs/demos/soundmicdemo')
programs.demos.fftwave = require('../../programs/demos/soundwaveformdemo')



Expand Down
18 changes: 17 additions & 1 deletion src/app/sound/sound-system.coffee
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class SoundSystem

addToScope: (scope) ->
scope.addFunction('play', (soundName, pattern) => @play(soundName, pattern))
scope.addFunction('getFFT', () => @getFFT())
scope.addFunction('getWaveForm', () => @getWaveForm())
scope.addFunction('setSmoothingTimeConstant', (value) => @setSmoothingTimeConstant(value))
scope.addFunction('setNumVars', (value) => @setNumVars(value))
scope.addFunction('getFFTvalue', (value) => @getFFT()[0][value])#TODO this not working

clearPatterns: ->
@playPatterns = []
Expand All @@ -33,11 +38,22 @@ class SoundSystem
pattern: pattern
})

getFFT: () ->
@audioApi.getFFT()

getWaveForm: (value) ->
@audioApi.getWaveForm()

setSmoothingTimeConstant: (value) ->
@audioApi.setSmoothingTimeConstant value

setNumVars: (value) ->
@audioApi.setNumVars value

playSounds: (beat) =>
for p in @playPatterns
if (@patternPlayer.runPattern(p.pattern, beat))
@audioApi.play(p.name)
@clearPatterns()

module.exports = SoundSystem

96 changes: 95 additions & 1 deletion src/app/sound/webAudioApi.coffee
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,105 @@ class WebAudioApi
@context = new AudioContext()
@soundout = @context.destination
@samples = {}
@bufferSize = 1024
@total = 0
@fft = []
@waveform = []
@analyser
@numbars = 14
@getUserMedia audio:true, @gotStream

getTime: () =>
@context.currentTime * 1000

getUserMedia: (dictionary, callback) ->
try
navigator.getMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia

navigator.getMedia dictionary, callback, (err) ->
console.log("The following error occured: " + err);
catch e
throw "Could not getMedia"


callback: (data) =>
console.log('ESTAMOS YA EN EL CALLBACCKKKKKSS')


gotStream: (stream) =>
@bufLength = @length * @context.sampleRate

# get an AudioNode from the stream
@mediaStreamSource = @context.createMediaStreamSource stream
@analyser = @context.createAnalyser()

@analyser.fftSize = 1024
@analyser.smoothingTimeConstant = 0.3
# binding to window because otherwise it'll
# get garbage collected
window.microphoneProcessingNode = @createNode()
@mediaStreamSource.connect @analyser;
# @analyser.connect @mediaStreamSource;

@mediaStreamSource.connect window.microphoneProcessingNode
window.microphoneProcessingNode.connect @context.destination

createNode: =>
node = @context.createScriptProcessor @bufferSize, 2, 2
node.onaudioprocess = (e) =>

#console.log('received audio ' +left[0])
# clone the samples

freqByteData = new Uint8Array @analyser.frequencyBinCount
timeByteData = new Uint8Array @analyser.frequencyBinCount

@analyser.getByteFrequencyData freqByteData;
@analyser.getByteTimeDomainData timeByteData;
@waveform = timeByteData
#numbars = 14

for i in [0...@numbars]
multipliers = @analyser.frequencyBinCount / @numbars

magnitude = 0
multipliers = Math.floor ( multipliers )
offset = i * multipliers
#gotta sum/average the block, or we miss narrow-bandwidth spikes
for j in [0...multipliers]
magnitude += freqByteData[offset + j]

magnitude = magnitude / multipliers
@fft[i] = magnitude

@total += @bufferSize
if @total > @bufLength
outBuffer = @prepareBuffer()
@callback outBuffer

node

activateMic: () =>
console.log('hola +++++++++++++++++++++++')

readMic: () =>
@fft

getFFT: () =>
@fft

getWaveForm: () =>
@waveform

setNumVars: (value) =>
@numbars = value

setSmoothingTimeConstant: (smooth) =>
@analyser.smoothingTimeConstant = smooth

loadSample: (name, path) =>
url = path
request = new XMLHttpRequest()
Expand All @@ -38,4 +133,3 @@ class WebAudioApi
source.start(0)

module.exports = WebAudioApi

13 changes: 13 additions & 0 deletions src/programs/demos/soundmicdemo.lcl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
submenu: Sound
title: Microphone Demo
code: |
// draw lines like this
audioRaw = (getFFT 0)
setSmoothingTimeConstant 0.8
setNumVars 19
for i in [0...audioRaw.length]
//console.log 'h'+i
move 0.1, 0, 0
//rotate audioRaw[i]
//console.log audioRaw[i]
box 0.05, 0.1 + audioRaw[i] * 0.02, 0.2
11 changes: 11 additions & 0 deletions src/programs/demos/soundwaveformdemo.lcl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
submenu: Sound
title: Waveform Demo
code: |
// draw lines like this
audioRaw = (getWaveForm 0)
setSmoothingTimeConstant 0.8
setNumVars 19
move -0.5, -2, 0
for i in [0...audioRaw.length]
move 0.005*i, audioRaw[i] * 0.02, 0
box 0.0005, 0.05, 0.2