|
| 1 | +#!/usr/bin/env jruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require 'propane' |
| 5 | + |
| 6 | +# ADSRExample is an example of using the ADSR envelope within an instrument. |
| 7 | +# For more information about Minim and additional features, |
| 8 | +# visit http://code.compartmental.net/minim/ |
| 9 | +# author: Anderson Mills<br/> |
| 10 | +# Anderson Mills's work was supported by numediart (www.numediart.org) |
| 11 | +class ADSRExample < Propane::App |
| 12 | + load_libraries :minim, :tone_instrument |
| 13 | + java_import 'ddf.minim.Minim' |
| 14 | + |
| 15 | + attr_reader :out |
| 16 | + |
| 17 | + def setup |
| 18 | + sketch_title 'ADSR Example' |
| 19 | + minim = Minim.new(self) |
| 20 | + @out = minim.get_line_out(Minim::MONO, 2048) |
| 21 | + # pause time when adding a bunch of notes at once |
| 22 | + out.pause_notes |
| 23 | + # make four repetitions of the same pattern |
| 24 | + 4.times do |i| |
| 25 | + # add some low notes |
| 26 | + out.play_note(1.25 + i * 2.0, 0.3, ToneInstrument.new(out, 75, 0.49)) |
| 27 | + out.play_note(2.50 + i * 2.0, 0.3, ToneInstrument.new(out, 75, 0.49)) |
| 28 | + |
| 29 | + # add some middle notes |
| 30 | + out.play_note(1.75 + i * 2.0, 0.3, ToneInstrument.new(out, 175, 0.4)) |
| 31 | + out.play_note(2.75 + i * 2.0, 0.3, ToneInstrument.new(out, 175, 0.4)) |
| 32 | + |
| 33 | + # add some high notes |
| 34 | + out.play_note(1.25 + i * 2.0, 0.3, ToneInstrument.new(out, 3750, 0.07)) |
| 35 | + out.play_note(1.5 + i * 2.0, 0.3, ToneInstrument.new(out, 1750, 0.02)) |
| 36 | + out.play_note(1.75 + i * 2.0, 0.3, ToneInstrument.new(out, 3750, 0.07)) |
| 37 | + out.play_note(2.0 + i * 2.0, 0.3, ToneInstrument.new(out, 1750, 0.02)) |
| 38 | + out.play_note(2.25 + i * 2.0, 0.3, ToneInstrument.new(out, 3750, 0.07)) |
| 39 | + out.play_note(2.5 + i * 2.0, 0.3, ToneInstrument.new(out, 5550, 0.09)) |
| 40 | + out.play_note(2.75 + i * 2.0, 0.3, ToneInstrument.new(out, 3750, 0.07)) |
| 41 | + end |
| 42 | + # resume time after a bunch of notes are added at once |
| 43 | + out.resumeNotes |
| 44 | + end |
| 45 | + |
| 46 | + # draw is run many times |
| 47 | + def draw |
| 48 | + # erase the window to black |
| 49 | + background(0) |
| 50 | + # draw using a white stroke |
| 51 | + stroke(255) |
| 52 | + # draw the waveforms |
| 53 | + (0...(out.bufferSize - 1)).each do |i| |
| 54 | + # find the x position of each buffer value |
| 55 | + x1 = map1d(i, 0..out.bufferSize, 0..width) |
| 56 | + x2 = map1d(i + 1, 0..out.bufferSize, 0..width) |
| 57 | + # draw a line from one buffer position to the next for both channels |
| 58 | + line(x1, 50 + out.left.get(i) * 50, x2, 50 + out.left.get(i + 1) * 50) |
| 59 | + line(x1, 150 + out.right.get(i) * 50, x2, 150 + out.right.get(i + 1) * 50) |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + def settings |
| 64 | + size(512, 200, P2D) |
| 65 | + end |
| 66 | +end |
| 67 | + |
| 68 | +ADSRExample.new |
0 commit comments