Skip to content

Commit c8bce9e

Browse files
author
monkstone
committed
add keyboard example
1 parent 7134c9c commit c8bce9e

File tree

6 files changed

+84
-0
lines changed

6 files changed

+84
-0
lines changed
130 KB
Binary file not shown.
113 KB
Binary file not shown.
160 KB
Binary file not shown.
150 KB
Binary file not shown.
123 KB
Binary file not shown.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# This example shows how to make a simple sampler and sequencer with the Sound
2+
# library. In this sketch 5 different short samples are loaded and played back
3+
# at different pitches, in this when 5 different octaves. The sequencer
4+
# triggers and event every 200-1000 mSecs randomly. Each time a sound is
5+
# played a colored rect with a random color is displayed.
6+
load_library :sound
7+
8+
include_package 'processing.sound'
9+
10+
# Define the number of samples
11+
NUM_SOUNDS = 5
12+
attr_reader :device, :file, :value
13+
14+
def setup
15+
size(640, 360)
16+
background(255)
17+
# Create a Sound renderer and an array of empty soundfiles
18+
@device = AudioDevice.new(self, 48_000, 32)
19+
@file = []
20+
@value = Array.new(3, 0)
21+
# Load 5 soundfiles from a folder in a for loop. By naming the files 1., 2.,
22+
# 3., n.aif it is easy to iterate through the folder and load all files in
23+
# one line of code.
24+
NUM_SOUNDS.times do |i|
25+
file << SoundFile.new(self, format('%d.aif', (i + 1)))
26+
end
27+
end
28+
29+
def draw
30+
background(*value) # splat array values
31+
end
32+
33+
def key_pressed
34+
defined = true
35+
case key
36+
when 'a'
37+
file[0].play(0.5, 1.0)
38+
when 's'
39+
file[1].play(0.5, 1.0)
40+
when 'd'
41+
file[2].play(0.5, 1.0)
42+
when 'f'
43+
file[3].play(0.5, 1.0)
44+
when 'g'
45+
file[4].play(0.5, 1.0)
46+
when 'h'
47+
file[0].play(1.0, 1.0)
48+
when 'j'
49+
file[1].play(1.0, 1.0)
50+
when 'k'
51+
file[2].play(1.0, 1.0)
52+
when 'l'
53+
file[3].play(1.0, 1.0)
54+
when 'ö'
55+
file[4].play(1.0, 1.0)
56+
when 'ä'
57+
file[0].play(2.0, 1.0)
58+
when 'q'
59+
file[1].play(2.0, 1.0)
60+
when 'w'
61+
file[2].play(2.0, 1.0)
62+
when 'e'
63+
file[3].play(2.0, 1.0)
64+
when 'r'
65+
file[4].play(2.0, 1.0)
66+
when 't'
67+
file[0].play(3.0, 1.0)
68+
when 'z'
69+
file[1].play(3.0, 1.0)
70+
when 'u'
71+
file[2].play(3.0, 1.0)
72+
when 'i'
73+
file[3].play(3.0, 1.0)
74+
when 'o'
75+
file[4].play(3.0, 1.0)
76+
when 'p'
77+
file[0].play(4.0, 1.0)
78+
when 'ü'
79+
file[1].play(4.0, 1.0)
80+
else
81+
defined = false # only set background color value, if key is defined
82+
end
83+
@value = [rand(0..255), rand(0..255), rand(0..255)] if defined
84+
end

0 commit comments

Comments
 (0)