|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'propane' |
| 4 | +# Play a sound sample and pass it through a tape delay, changing the delay |
| 5 | +# parameters based on the mouse position. |
| 6 | +class VariableDelay < Propane::App |
| 7 | + load_library :sound |
| 8 | + java_import 'processing.sound.Amplitude' |
| 9 | + java_import 'processing.sound.SoundFile' |
| 10 | + |
| 11 | + attr_reader :rms, :sample, :sum |
| 12 | + SMOOTHING_FACTOR = 0.25 |
| 13 | + def settings |
| 14 | + size(640, 360) |
| 15 | + end |
| 16 | + |
| 17 | + def setup |
| 18 | + sketch_title 'Peak Amplitude' |
| 19 | + # Load and play a soundfile and loop it |
| 20 | + @sample = SoundFile.new(self, data_path('beat.aiff')) |
| 21 | + sample.loop |
| 22 | + # Create and patch the rms tracker |
| 23 | + @rms = Amplitude.new(self) |
| 24 | + rms.input(sample) |
| 25 | + @sum = 0 |
| 26 | + end |
| 27 | + |
| 28 | + def draw |
| 29 | + # Set background color, noStroke and fill color |
| 30 | + background(125, 255, 125) |
| 31 | + noStroke |
| 32 | + fill(255, 0, 150) |
| 33 | + |
| 34 | + # smooth the rms data by smoothing factor |
| 35 | + @sum += (rms.analyze - sum) * SMOOTHING_FACTOR |
| 36 | + |
| 37 | + # rms.analyze() return a value between 0 and 1. Its |
| 38 | + # scaled to height/2 and then multiplied by a fixed scale factor |
| 39 | + rms_scaled = sum * (height / 2) * 5 |
| 40 | + |
| 41 | + # We draw a circle whose size is coupled to the audio analysis |
| 42 | + ellipse(width / 2, height / 2, rms_scaled, rms_scaled) |
| 43 | + end |
| 44 | +end |
| 45 | + |
| 46 | +VariableDelay.new |
0 commit comments