Skip to content

Commit c8043d7

Browse files
committed
minim examples
1 parent a914ff1 commit c8043d7

File tree

21 files changed

+525
-77
lines changed

21 files changed

+525
-77
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# JRubyArt-examples for JRubyArt-1.7.0+
1+
# JRubyArt-examples for JRubyArt-2.2.0+
22
Replaces `AppRender` with `GfxRender`, and removal of bashisms in autorun Rakefiles
33

44
Description

external_library/gem/geomerative/hello_world.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def setup
1313
background(255)
1414
fill(255, 102, 0)
1515
stroke(0)
16-
@grp = RG.getText('Hola Mundo!', data_path('FreeSans.ttf'), 72, CENTER)
16+
@grp = RG.get_text('Hola Mundo!', data_path('FreeSans.ttf'), 72, CENTER)
1717
end
1818

1919
def draw
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# frozen_string_literal: true
2+
3+
# ADSRExample is an example of using the ADSR envelope within an instrument.
4+
# For more information about Minim and additional features,
5+
# visit http://code.compartmental.net/minim/
6+
# author: Anderson Mills<br/>
7+
# Anderson Mills's work was supported by numediart (www.numediart.org)
8+
9+
load_libraries :minim, :tone_instrument
10+
java_import 'ddf.minim.Minim'
11+
12+
attr_reader :out
13+
14+
def setup
15+
sketch_title 'ADSR Example'
16+
minim = Minim.new(self)
17+
@out = minim.get_line_out(Minim::MONO, 2048)
18+
# pause time when adding a bunch of notes at once
19+
out.pause_notes
20+
# make four repetitions of the same pattern
21+
4.times do |i|
22+
# add some low notes
23+
out.play_note(1.25 + i * 2.0, 0.3, ToneInstrument.new(out, 75, 0.49))
24+
out.play_note(2.50 + i * 2.0, 0.3, ToneInstrument.new(out, 75, 0.49))
25+
26+
# add some middle notes
27+
out.play_note(1.75 + i * 2.0, 0.3, ToneInstrument.new(out, 175, 0.4))
28+
out.play_note(2.75 + i * 2.0, 0.3, ToneInstrument.new(out, 175, 0.4))
29+
30+
# add some high notes
31+
out.play_note(1.25 + i * 2.0, 0.3, ToneInstrument.new(out, 3750, 0.07))
32+
out.play_note(1.5 + i * 2.0, 0.3, ToneInstrument.new(out, 1750, 0.02))
33+
out.play_note(1.75 + i * 2.0, 0.3, ToneInstrument.new(out, 3750, 0.07))
34+
out.play_note(2.0 + i * 2.0, 0.3, ToneInstrument.new(out, 1750, 0.02))
35+
out.play_note(2.25 + i * 2.0, 0.3, ToneInstrument.new(out, 3750, 0.07))
36+
out.play_note(2.5 + i * 2.0, 0.3, ToneInstrument.new(out, 5550, 0.09))
37+
out.play_note(2.75 + i * 2.0, 0.3, ToneInstrument.new(out, 3750, 0.07))
38+
end
39+
# resume time after a bunch of notes are added at once
40+
out.resumeNotes
41+
end
42+
43+
# draw is run many times
44+
def draw
45+
# erase the window to black
46+
background(0)
47+
# draw using a white stroke
48+
stroke(255)
49+
# draw the waveforms
50+
(0...(out.bufferSize - 1)).each do |i|
51+
# find the x position of each buffer value
52+
x1 = map1d(i, 0..out.bufferSize, 0..width)
53+
x2 = map1d(i + 1, 0..out.bufferSize, 0..width)
54+
# draw a line from one buffer position to the next for both channels
55+
line(x1, 50 + out.left.get(i) * 50, x2, 50 + out.left.get(i + 1) * 50)
56+
line(x1, 150 + out.right.get(i) * 50, x2, 150 + out.right.get(i + 1) * 50)
57+
end
58+
end
59+
60+
def settings
61+
size(512, 200, P2D)
62+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
# This sketch demonstrates how to create synthesized sound with Minim
4+
# using an AudioOutput and an Instrument we define. By using the
5+
# playNote method you can schedule notes to played at some point in the
6+
# future, essentially allowing to you create musical scores with code.
7+
# Because they are constructed with code, they can be either
8+
# deterministic or different every time. This sketch creates a
9+
# deterministic score, meaning it is the same every time you run the
10+
# sketch. For more complex examples of using playNote check out
11+
# algorithmicCompExample and compositionExample. For more information
12+
# about Minim and additional features, visit
13+
# http://code.compartmental.net/minim/
14+
15+
load_libraries :minim, :sine_instrument
16+
java_import 'ddf.minim.Minim'
17+
18+
attr_reader :out
19+
20+
def settings
21+
size(512, 200, P2D)
22+
end
23+
24+
def setup
25+
sketch_title 'Create Instrument'
26+
minim = Minim.new(self)
27+
@out = minim.getLineOut
28+
# when providing an Instrument, we always specify start time and duration
29+
out.playNote(0.0, 0.9, SineInstrument.new(out, 97.99))
30+
out.playNote(1.0, 0.9, SineInstrument.new(out, 123.47))
31+
# we can use the Frequency class to create frequencies from pitch names
32+
out.playNote(2.0, 2.9, SineInstrument.new(out, Frequency.ofPitch('C3').asHz))
33+
out.playNote(3.0, 1.9, SineInstrument.new(out, Frequency.ofPitch('E3').asHz))
34+
out.playNote(4.0, 0.9, SineInstrument.new(out, Frequency.ofPitch('G3').asHz))
35+
end
36+
37+
def draw
38+
background(0)
39+
stroke(255)
40+
# draw the waveforms
41+
(out.bufferSize - 1).times do |i|
42+
line(i, 50 + out.left.get(i) * 50, i + 1, 50 + out.left.get(i + 1) * 50)
43+
line(i, 150 + out.right.get(i) * 50, i + 1, 150 + out.right.get(i + 1) * 50)
44+
end
45+
end
5.91 KB
Binary file not shown.
27.4 KB
Binary file not shown.
7.03 KB
Binary file not shown.
8.03 KB
Binary file not shown.
424 KB
Binary file not shown.
22 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
# This sketch is a more involved use of AudioSamples to create a simple drum machine.
4+
# Click on the buttons to toggle them on and off. The buttons that are on will trigger
5+
# samples when the beat marker passes over their column. You can change the tempo by
6+
# clicking in the BPM box and dragging the mouse up and down.
7+
# We achieve the timing by using AudioOutput's playNote method and a cleverly written Instrument.
8+
# For more information about Minim and additional features,
9+
# visit http://code.compartmental.net/minim/
10+
11+
load_libraries :minim, :tick
12+
java_import 'ddf.minim.Minim'
13+
java_import 'ddf.minim.ugens.Sampler'
14+
attr_reader :minim, :out, :kick, :snare, :hat, :bpm, :buttons
15+
attr_reader :kikRow, :snrRow, :hatRow
16+
attr_accessor :beat
17+
def setup
18+
sketch_title 'Drum Machine'
19+
minim = Minim.new(self)
20+
@out = minim.getLineOut
21+
@hatRow = Array.new(16, false)
22+
@snrRow = Array.new(16, false)
23+
@kikRow = Array.new(16, false)
24+
@buttons = []
25+
@bpm = 120
26+
@beat = 0
27+
# load all of our samples, using 4 voices for each.
28+
# this will help ensure we have enough voices to handle even
29+
# very fast tempos.
30+
@kick = Sampler.new(data_path('BD.wav'), 4, minim)
31+
@snare = Sampler.new(data_path('SD.wav'), 4, minim)
32+
@hat = Sampler.new(data_path('CHH.wav'), 4, minim)
33+
# patch samplers to the output
34+
kick.patch(out)
35+
snare.patch(out)
36+
hat.patch(out)
37+
16.times do |i|
38+
buttons << Rect.new(10 + i * 24, 50, hatRow, i)
39+
buttons << Rect.new(10 + i * 24, 100, snrRow, i)
40+
buttons << Rect.new(10 + i * 24, 150, kikRow, i)
41+
end
42+
# start the sequencer
43+
out.setTempo(bpm)
44+
out.playNote(0, 0.25, Tick.new)
45+
end
46+
47+
def draw
48+
background(0)
49+
fill(255)
50+
# text(frameRate, width - 60, 20)
51+
buttons.each(&:draw)
52+
stroke(128)
53+
(beat % 4).zero? ? fill(200, 0, 0) : fill(0, 200, 0)
54+
# beat marker
55+
rect(10 + beat * 24, 35, 14, 9)
56+
end
57+
58+
def mouse_pressed
59+
buttons.each(&:mouse_pressed)
60+
end
61+
62+
def settings
63+
size(395, 200)
64+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
%w[Frequency Instrument Line Oscil Waves].each do |klass|
4+
java_import "ddf.minim.ugens.#{klass}"
5+
end
6+
7+
# to make an Instrument we must define a class
8+
# that includes Instrument interface (as a module).
9+
class SineInstrument
10+
include Instrument
11+
12+
attr_reader :wave, :amp_env, :out, :tone_instrument
13+
14+
def initialize(out, frequency)
15+
# make a sine wave oscillator
16+
# the amplitude is zero because
17+
# we are going to patch a Line to it anyway
18+
@out = out
19+
@wave = Oscil.new(frequency, 0, Waves::SINE)
20+
@amp_env = Line.new
21+
amp_env.patch(wave.amplitude)
22+
end
23+
24+
# this is called by the sequencer when this instrument
25+
# should start making sound. the duration is expressed in seconds.
26+
def noteOn(duration)
27+
# start the amplitude envelope
28+
amp_env.activate(duration, 0.5, 0)
29+
# attach the oscil to the output so it makes sound
30+
wave.patch(out)
31+
end
32+
33+
# this is called by the sequencer when the instrument should
34+
# stop making sound
35+
def noteOff
36+
wave.unpatch(out)
37+
end
38+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
# simple class for drawing the gui
4+
class Rect
5+
include Processing::Proxy
6+
attr_reader :x, :y, :w, :h, :steps, :stepId
7+
8+
def initialize(_x, _y, _steps, _id)
9+
@x = _x
10+
@y = _y
11+
@w = 14
12+
@h = 30
13+
@steps = _steps
14+
@stepId = _id
15+
end
16+
17+
def draw
18+
steps[stepId] ? fill(0, 255, 0) : fill(255, 0, 0)
19+
rect(x, y, w, h)
20+
end
21+
22+
def mouse_pressed
23+
if mouse_x >= x && mouse_x <= x + w && mouse_y >= y && mouse_y <= y + h
24+
steps[stepId] = !steps[stepId]
25+
end
26+
end
27+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
java_import 'ddf.minim.ugens.Instrument'
4+
# class Tick can access app variables by including Processing::App module
5+
# But we must use instance variable to set the beat
6+
class Tick
7+
include Instrument
8+
include Processing::Proxy
9+
10+
def noteOn(_dur)
11+
hat.trigger if hatRow[beat]
12+
snare.trigger if snrRow[beat]
13+
kick.trigger if kikRow[beat]
14+
end
15+
16+
def noteOff
17+
# next beat
18+
Processing.app.beat = (beat + 1) % 16
19+
# set the new tempo
20+
out.setTempo(bpm)
21+
# play this again right now, with a sixteenth note duration
22+
out.playNote(0, 0.25, self)
23+
end
24+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require_relative 'lib/tick'
2+
require_relative 'lib/rect'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
# java_import 'ddf.minim.Minim'
4+
5+
%w[ADSR Instrument Oscil Waves].each do |klass|
6+
java_import "ddf.minim.ugens.#{klass}"
7+
end
8+
9+
# Every instrument must implement the Instrument interface so
10+
# playNote can call the instrument's methods.
11+
class ToneInstrument
12+
include Instrument
13+
attr_reader :adsr, :out
14+
15+
# constructor for this instrument NB: includes line_out
16+
def initialize(out, frequency, amplitude)
17+
# create new instances of any UGen objects as necessary
18+
sineOsc = Oscil.new(frequency, amplitude, Waves::TRIANGLE)
19+
@adsr = ADSR.new(0.5, 0.01, 0.05, 0.5, 0.5)
20+
@out = out
21+
# patch everything together up to the final output
22+
sineOsc.patch(adsr)
23+
end
24+
25+
# every instrument must have a noteOn method
26+
def noteOn(dur)
27+
# turn on the ADSR
28+
adsr.noteOn
29+
# patch to the output
30+
adsr.patch(out)
31+
end
32+
33+
# every instrument must have a noteOff method
34+
def noteOff
35+
# tell the ADSR to unpatch after the release is finished
36+
adsr.unpatchAfterRelease(out)
37+
# call the noteOff
38+
adsr.noteOff
39+
end
40+
end

external_library/java/minim/loop.rb

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
# This sketch demonstrates how to use the loop method of a Playable class.
4+
# The class used here is AudioPlayer, but you can also loop an AudioSnippet.
5+
# When you call loop() it will make the Playable playback in an infinite loop.
6+
# If you want to make it stop looping you can call play() and it will finish the
7+
# current loop and then stop. Press 'l' to start the player looping.
8+
9+
load_library :minim
10+
java_import 'ddf.minim.Minim'
11+
12+
attr_reader :groove
13+
14+
def settings
15+
size(512, 200, P2D)
16+
end
17+
18+
def setup
19+
sketch_title "Press \'l\' key to loop"
20+
minim = Minim.new(self)
21+
@groove = minim.load_file(data_path('groove.mp3'), 2048)
22+
end
23+
24+
def draw
25+
background(0)
26+
stroke(255)
27+
(0...groove.buffer_size - 1).each do |i|
28+
line(i, 50 + groove.left.get(i) * 50, i + 1, 50 + groove.left.get(i + 1) * 50)
29+
line(i, 150 + groove.right.get(i) * 50, i + 1, 150 + groove.right.get(i + 1) * 50)
30+
end
31+
end
32+
33+
def key_pressed
34+
return unless key == 'l'
35+
36+
groove.loop
37+
end

0 commit comments

Comments
 (0)