Skip to content

Commit b6f5dd1

Browse files
committed
expand sound library examples
1 parent 5f40a08 commit b6f5dd1

Some content is hidden

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

48 files changed

+1444
-328
lines changed

Rakefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ task :all do
1212
Rake::Task[:shaders].execute
1313
Rake::Task[:slider].execute
1414
Rake::Task[:demo].execute
15+
Rake::Task[:sound].execute
1516
end
1617

1718
desc 'run contributed samples'
@@ -100,6 +101,19 @@ task :hemesh do
100101
system 'rake'
101102
end
102103

104+
desc 'sound'
105+
task :sound do
106+
FileUtils.cd(
107+
File.join(
108+
PRWD,
109+
'processing_app',
110+
'library',
111+
'sound'
112+
)
113+
)
114+
system 'rake'
115+
end
116+
103117
desc 'pbox2d'
104118
task :pbox2d do
105119
FileUtils.cd(
Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
1-
# Simple demo Rakefile to autorun samples in current directory
2-
# adjust path to rp5 executable, and or opts as required
1+
# -*- encoding: utf-8 -*-
32

4-
SAMPLES_DIR = './'
3+
PRWD = File.expand_path(__dir__)
54

65
desc 'run demo'
7-
task default: [:demo]
6+
task :default => [:all]
87

9-
desc 'demo'
10-
task :demo do
11-
samples_list.shuffle.each { |sample| run_sample sample }
8+
desc 'autorun all samples'
9+
task :all do
10+
Rake::Task[:soundfile].execute
11+
Rake::Task[:oscillators].execute
12+
Rake::Task[:noise].execute
13+
Rake::Task[:effects].execute
1214
end
1315

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
16+
desc 'run soundfile samples'
17+
task :soundfile do
18+
sh "cd #{PRWD}/soundfile && rake"
2119
end
2220

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

processing_app/library/sound/brown_noise.rb

Lines changed: 0 additions & 33 deletions
This file was deleted.
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 'propane'
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 < Propane::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

0 commit comments

Comments
 (0)