Skip to content

Commit 4d278ce

Browse files
committed
sound library examples
1 parent af7e7f2 commit 4d278ce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1372
-0
lines changed

library/sound/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@ picrate --install Sound
66
```
77

88
To install other contributed libraries eg `minim` [see documentation][docs]
9+
especially on Buster you may need to install `pulseaudio`:-
10+
```
11+
sudo apt-get install pulseaudio
12+
```
13+
However it might not such a good idea unless you have a RaspberryPI4.
914

1015
[docs]:https://ruby-processing.github.io/picrate/contributed
File renamed without changes.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
require 'picrate'
4+
# Play a sound sample and pass it through a tape delay, changing the delay
5+
# parameters based on the mouse position.
6+
class WaveformAnalysis < Processing::App
7+
load_library :sound
8+
java_import 'processing.sound.Waveform'
9+
java_import 'processing.sound.SoundFile'
10+
11+
attr_reader :waveform, :sample
12+
SAMPLES = 100
13+
def settings
14+
size(640, 360)
15+
end
16+
17+
def setup
18+
sketch_title 'Waveform Analyser'
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 waveform tracker
23+
@waveform = Waveform.new(self, SAMPLES)
24+
waveform.input(sample)
25+
end
26+
27+
def draw
28+
# Set background color, noFill and stroke style
29+
background(0)
30+
stroke(255)
31+
strokeWeight(2)
32+
noFill
33+
34+
# Perform the analysis
35+
waveform.analyze
36+
37+
begin_shape
38+
SAMPLES.times do |i|
39+
# Draw current data of the waveform
40+
# Each sample in the data array is between -1 and +1
41+
vertex(
42+
map1d(i, 0..SAMPLES, 0..width),
43+
map1d(waveform.data[i], -1..1, 0..height)
44+
)
45+
end
46+
end_shape
47+
end
48+
end
49+
50+
WaveformAnalysis.new

library/sound/analysis/data/beat.aiff

678 KB
Binary file not shown.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
require 'picrate'
4+
# Play a sound sample and pass it through a tape delay, changing the delay
5+
# parameters based on the mouse position.
6+
class FFTSpectrum < Processing::App
7+
load_library :sound
8+
java_import 'processing.sound.FFT'
9+
java_import 'processing.sound.SoundFile'
10+
11+
attr_reader :fft, :sample, :sum, :bar_width
12+
SMOOTHING_FACTOR = 0.25
13+
BANDS = 128
14+
SCALE = 5
15+
def settings
16+
size(640, 360)
17+
end
18+
19+
def setup
20+
sketch_title 'FFT Spectrum'
21+
@bar_width = width / BANDS.to_f
22+
# Load and play a soundfile and loop it
23+
@sample = SoundFile.new(self, data_path('beat.aiff'))
24+
sample.loop
25+
# Create and patch the fft tracker
26+
@fft = FFT.new(self)
27+
fft.input(sample)
28+
@sum = Array.new(BANDS, 0.0)
29+
end
30+
31+
def draw
32+
# Set background color, noStroke and fill color
33+
background(125, 255, 125)
34+
noStroke
35+
fill(255, 0, 150)
36+
fft.analyze
37+
BANDS.times do |i|
38+
# Smooth the FFT spectrum data by smoothing factor
39+
sum[i] += (fft.spectrum[i] - sum[i]) * SMOOTHING_FACTOR
40+
rect(i * bar_width, height, bar_width, -sum[i] * height * SCALE)
41+
end
42+
end
43+
end
44+
45+
FFTSpectrum.new
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
require 'picrate'
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 < Processing::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

library/sound/effects/Rakefile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Simple demo Rakefile to autorun samples in current directory
2+
# adjust path to rp5 executable, and or opts as required
3+
4+
SAMPLES_DIR = './'
5+
6+
desc 'run demo'
7+
task default: [:demo]
8+
9+
desc 'demo'
10+
task :demo do
11+
samples_list.shuffle.each { |sample| run_sample sample }
12+
end
13+
14+
def samples_list
15+
files = []
16+
Dir.chdir(SAMPLES_DIR)
17+
Dir.glob('*.rb').each do |file|
18+
files << File.join(SAMPLES_DIR, file)
19+
end
20+
return files
21+
end
22+
23+
def run_sample(sample_name)
24+
puts "Running #{sample_name}...quit to run next sample"
25+
open("|jruby #{sample_name}", 'r') do |io|
26+
while l = io.gets
27+
puts(l.chop)
28+
end
29+
end
30+
end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
require 'picrate'
4+
# Play a sound sample and pass it through a tape source, changing the source
5+
# parameters based on the mouse position.
6+
class BandPassFilter < Processing::App
7+
load_library :sound
8+
java_import 'processing.sound.WhiteNoise'
9+
java_import 'processing.sound.BandPass'
10+
11+
attr_reader :filter, :source
12+
13+
def settings
14+
size(640, 360)
15+
end
16+
17+
def setup
18+
background(255)
19+
sketch_title 'Band Pass Filter'
20+
# Create the noise generator + filter
21+
@filter = BandPass.new(self)
22+
@source = WhiteNoise.new(self)
23+
# Connect the filter to the source unit
24+
source.play
25+
filter.process(source)
26+
end
27+
28+
def draw
29+
# Map the left/right mouse position to a cutoff frequency between 20 and 10000 Hz
30+
frequency = map1d(mouseX, 0..width, 20..10_000)
31+
# And the vertical mouse position to the width of the band to be passed through
32+
bandwidth = map1d(mouseY, 0..height, 1_000..100)
33+
filter.freq(frequency)
34+
filter.bw(bandwidth)
35+
# Draw a circle indicating the position + width of the frequency window
36+
# that is allowed to pass through
37+
background(125, 255, 125)
38+
no_stroke
39+
fill(255, 0, 150)
40+
end
41+
end
42+
43+
BandPassFilter.new
4.71 MB
Binary file not shown.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
require 'picrate'
4+
# Play a sound sample and pass it through a tape source, changing the source
5+
# parameters based on the mouse position.
6+
class HighPassFilter < Processing::App
7+
load_library :sound
8+
java_import 'processing.sound.WhiteNoise'
9+
java_import 'processing.sound.HighPass'
10+
11+
attr_reader :filter, :source
12+
13+
def settings
14+
size(640, 360)
15+
end
16+
17+
def setup
18+
background(255)
19+
sketch_title 'High Pass Filter'
20+
# Create the noise generator + filter
21+
@filter = HighPass.new(self)
22+
@source = WhiteNoise.new(self)
23+
# Connect the filter to the source unit
24+
source.play
25+
filter.process(source)
26+
end
27+
28+
def draw
29+
# Map the left/right mouse position to a cutoff frequency between 10 and 15000 Hz
30+
cutoff = map1d(mouseX, 0..width, 10..15_000)
31+
filter.freq(cutoff)
32+
# Draw a circle indicating the position + width of the frequencies passed through
33+
background(125, 255, 125)
34+
noStroke
35+
fill(255, 0, 150)
36+
ellipse(0, height, 2 * (width - mouseX), 2 * (width - mouseX))
37+
end
38+
end
39+
40+
HighPassFilter.new
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
require 'picrate'
4+
# Play a sound sample and pass it through a tape source, changing the source
5+
# parameters based on the mouse position.
6+
class Reverberation < Processing::App
7+
load_library :sound
8+
java_import 'processing.sound.WhiteNoise'
9+
java_import 'processing.sound.LowPass'
10+
11+
attr_reader :filter, :source
12+
13+
def settings
14+
size(640, 360)
15+
end
16+
17+
def setup
18+
background(255)
19+
sketch_title 'Variable Reverb'
20+
# Create the noise generator + filter
21+
@filter = LowPass.new(self)
22+
@source = WhiteNoise.new(self)
23+
# Connect the filter to the source unit
24+
source.play
25+
filter.process(source)
26+
end
27+
28+
def draw
29+
# Map the left/right mouse position to a cutoff frequency between 20 and 10000 Hz
30+
cutoff = map1d(mouseX, 0..width, 20..10_000)
31+
filter.freq(cutoff)
32+
# Draw a circle indicating the position + width of the frequencies passed through
33+
background(125, 255, 125)
34+
noStroke
35+
fill(255, 0, 150)
36+
ellipse(0, height, 2 * mouseX, 2 * mouseX)
37+
end
38+
end
39+
40+
Reverberation.new
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
require 'picrate'
4+
# Play a sound sample and pass it through a tape reverb, changing the reverb
5+
# parameters based on the mouse position.
6+
class Reverberation < Processing::App
7+
load_library :sound
8+
java_import 'processing.sound.Reverb'
9+
java_import 'processing.sound.SoundFile'
10+
11+
attr_reader :soundfile, :reverb
12+
13+
def settings
14+
size(640, 360)
15+
end
16+
17+
def setup
18+
background(255)
19+
sketch_title 'Variable Reverb'
20+
# Load a soundfile
21+
@soundfile = SoundFile.new(self, data_path('vibraphon.aiff'))
22+
# Create the reverb effect
23+
@reverb = Reverb.new(self)
24+
# Play the file in a loop
25+
soundfile.loop
26+
# Connect the soundfile to the reverb unit
27+
reverb.process(soundfile)
28+
end
29+
30+
def draw
31+
# Change the roomsize of the reverb
32+
room_size = map1d(mouseX, 0..width, 0..1.0)
33+
reverb.room(room_size)
34+
35+
# Change the high frequency dampening parameter
36+
damping = map1d(mouseX, 0..width, 0..1.0)
37+
reverb.damp(damping)
38+
39+
# Change the wet/dry relation of the effect
40+
effect_strength = map1d(mouseY, 0..height, 0..1.0)
41+
reverb.wet(effect_strength)
42+
end
43+
end
44+
45+
Reverberation.new

0 commit comments

Comments
 (0)