|
1 |
| -# |
| 1 | +#!/usr/bin/env jruby |
| 2 | +require 'propane' |
| 3 | + |
2 | 4 | # This is a sound file player.
|
3 | 5 | # NB: require jruby-complete to run
|
4 | 6 | # either --nojruby flag or use config
|
5 | 7 | #
|
6 |
| - |
7 |
| -load_library :sound |
8 |
| -include_package 'processing.sound' |
9 |
| - |
10 |
| -attr_reader :sound_file |
11 |
| - |
12 |
| -def setup |
13 |
| - sketch_title 'Sample' |
14 |
| - no_stroke |
15 |
| - # Load a soundfile |
16 |
| - @sound_file = SoundFile.new(self, data_path('vibraphon.aiff')) |
17 |
| - report_settings |
18 |
| - # Play the file in a loop |
19 |
| - sound_file.loop |
20 |
| -end |
21 |
| - |
22 |
| -def draw |
23 |
| - background 40, 1 |
24 |
| - red = map1d(mouse_x, (0..width), (30..255)) |
25 |
| - green = map1d(mouse_y, (height..0), (30..255)) |
26 |
| - fill(red, green, 0) |
27 |
| - ellipse(mouse_x, mouse_y, 30, 30) |
28 |
| - manipulate_sound |
29 |
| -end |
30 |
| - |
31 |
| -def manipulate_sound |
32 |
| - # Map mouse_x from 0.25 to 4.0 for playback rate. 1 equals original playback |
33 |
| - # speed 2 is an octave up 0.5 is an octave down. |
34 |
| - sound_file.rate(map1d(mouse_x, (0..width), (0.25..4.0))) |
35 |
| - # Map mouse_y from 0.2 to 1.0 for amplitude |
36 |
| - sound_file.amp(map1d(mouse_y, (0..width), (0.2..1.0))) |
37 |
| - # Map mouse_y from -1.0 to 1.0 for left to right |
38 |
| - sound_file.pan(map1d(mouse_y, (0..height), (-1.0..1.0))) |
39 |
| -end |
40 |
| - |
41 |
| -def report_settings |
42 |
| - # These methods return useful infos about the file |
43 |
| - p format('SFSampleRate= %d Hz', sound_file.sample_rate) |
44 |
| - p format('SFSamples= %d samples', sound_file.frames) |
45 |
| - p format('SFDuration= %d seconds', sound_file.duration) |
46 |
| -end |
47 |
| - |
48 |
| -def settings |
49 |
| - size 640, 360, P2D |
| 8 | +class Sample < Propane::App |
| 9 | + load_library :sound |
| 10 | + include_package 'processing.sound' |
| 11 | + |
| 12 | + attr_reader :sound_file |
| 13 | + |
| 14 | + def setup |
| 15 | + sketch_title 'Sample' |
| 16 | + no_stroke |
| 17 | + # Load a soundfile |
| 18 | + @sound_file = SoundFile.new(self, data_path('vibraphon.aiff')) |
| 19 | + report_settings |
| 20 | + # Play the file in a loop |
| 21 | + sound_file.loop |
| 22 | + end |
| 23 | + |
| 24 | + def draw |
| 25 | + background 40, 1 |
| 26 | + red = map1d(mouse_x, (0..width), (30..255)) |
| 27 | + green = map1d(mouse_y, (height..0), (30..255)) |
| 28 | + fill(red, green, 0) |
| 29 | + ellipse(mouse_x, mouse_y, 30, 30) |
| 30 | + manipulate_sound |
| 31 | + end |
| 32 | + |
| 33 | + def manipulate_sound |
| 34 | + # Map mouse_x from 0.25 to 4.0 for playback rate. 1 equals original playback |
| 35 | + # speed 2 is an octave up 0.5 is an octave down. |
| 36 | + sound_file.rate(map1d(mouse_x, (0..width), (0.25..4.0))) |
| 37 | + # Map mouse_y from 0.2 to 1.0 for amplitude |
| 38 | + sound_file.amp(map1d(mouse_y, (0..width), (0.2..1.0))) |
| 39 | + # Map mouse_y from -1.0 to 1.0 for left to right |
| 40 | + sound_file.pan(map1d(mouse_y, (0..height), (-1.0..1.0))) |
| 41 | + end |
| 42 | + |
| 43 | + def report_settings |
| 44 | + # These methods return useful infos about the file |
| 45 | + p format('SFSampleRate= %d Hz', sound_file.sample_rate) |
| 46 | + p format('SFSamples= %d samples', sound_file.frames) |
| 47 | + p format('SFDuration= %d seconds', sound_file.duration) |
| 48 | + end |
| 49 | + |
| 50 | + def settings |
| 51 | + size 640, 360, P2D |
| 52 | + end |
50 | 53 | end
|
51 | 54 |
|
| 55 | +Sample.new |
0 commit comments