defaulted start values for SoundObject

updated + reorganized examples
changed process() to analyze for analyzers
changed play() to process() for effects
This commit is contained in:
wirsing
2014-06-27 19:26:15 -07:00
parent 3ad8315ffd
commit 3f470bfb2b
42 changed files with 63 additions and 69 deletions
@@ -0,0 +1,79 @@
/*
This sketch shows how to use envelopes and oscillators. Envelopes are pre-defined amplitude
distribution over time. The sound library provides an ASR envelope which stands for attach,
sustain, release. The amplitude rises then sustains at the maximum level and decays slowly
depending on pre defined time segments.
.________
. ---
. ---
. ---
A S R
*/
import processing.sound.*;
Sound stream;
TriOsc triOsc;
Env env;
// Times and levels for the ASR envelope
float attackTime = 0.001;
float sustainTime = 0.004;
float sustainLevel = 0.3;
float releaseTime = 0.4;
// This is an octave in MIDI notes.
int[] midiSequence = { 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72};
int duration = 200;
// Set the note trigger
int trigger = millis();
// An index to count up the notes
int note=0;
void setup() {
size(640, 360);
background(255);
//Create and start the Sound renderer
stream = new Sound(this);
// Create triangle wave and start it
triOsc = new TriOsc(this);
//triOsc.play();
// Create the envelope
env = new Env(this);
}
void draw() {
// If the determined trigger moment in time matches up with the computer clock and we if the
// sequence of notes hasn't been finished yet the next note gets played.
if ((millis() > trigger) && (note<midiSequence.length)){
// midiToFreq transforms the MIDI value into a frequency in Hz which we use to control the triangle oscillator
// with an amplitute of 0.8
triOsc.play(midiToFreq(midiSequence[note]),0.8);
// The envelope gets triggered with the oscillator as input and the times and levels we defined earlier
env.play(triOsc, attackTime, sustainTime, sustainLevel, releaseTime);
// Create the new trigger according to predefined durations and speed it up by deviding by 1.5
trigger = millis() + duration;
// Advance by one note in the midiSequence;
note++;
// Loop the sequence, notice the jitter
if(note == 12) {note = 0;}
}
}
// This function calculates the respective frequency of a MIDI note
float midiToFreq(int note){
return (pow(2, ((note-69)/12.0)))*440;
}
@@ -0,0 +1,137 @@
/*
This example shows how to make a simple sampler and sequencer with the Sound library. In this
sketch 5 different short samples are loaded and played back at different pitches, in this
case 5 different octaves. The sequencer triggers and event every 200-1000 mSecs randomly.
Each time a sound is played a colored rect with a random color is displayed.
*/
import processing.sound.*;
Sound stream;
SoundFile[] file;
// Define the number of samples
int numsounds = 5;
int value[] = {0,0,0};
void setup(){
size(640, 360);
background(255);
// Create a Sound renderer and an array of empty soundfiles
stream = new Sound(this, 44100, 32);
file = new SoundFile[numsounds];
// Load 5 soundfiles from a folder in a for loop. By naming the files 1., 2., 3., n.aif it is easy to iterate
// through the folder and load all files in one line of code.
for (int i = 0; i < numsounds; i++){
file[i] = new SoundFile(this, (i+1) + ".aif");
}
}
void draw(){
background(value[0],value[1],value[2]);
}
void keyPressed() {
for (int i=0; i < 3; i++) {
value[i]=int(random(255));
}
switch(key){
case 'a':
file[0].play(0.5, 1.0);
break;
case 's':
file[1].play(0.5, 1.0);
break;
case 'd':
file[2].play(0.5, 1.0);
break;
case 'f':
file[3].play(0.5, 1.0);
break;
case 'g':
file[4].play(0.5, 1.0);
break;
case 'h':
file[0].play(1.0, 1.0);
break;
case 'j':
file[1].play(1.0, 1.0);
break;
case 'k':
file[2].play(1.0, 1.0);
break;
case 'l':
file[3].play(1.0, 1.0);
break;
case 'ö':
file[4].play(1.0, 1.0);
break;
case 'ä':
file[0].play(2.0, 1.0);
break;
case 'q':
file[1].play(2.0, 1.0);
break;
case 'w':
file[2].play(2.0, 1.0);
break;
case 'e':
file[3].play(2.0, 1.0);
break;
case 'r':
file[4].play(2.0, 1.0);
break;
case 't':
file[0].play(3.0, 1.0);
break;
case 'z':
file[1].play(3.0, 1.0);
break;
case 'u':
file[2].play(3.0, 1.0);
break;
case 'i':
file[3].play(3.0, 1.0);
break;
case 'o':
file[4].play(3.0, 1.0);
break;
case 'p':
file[0].play(4.0, 1.0);
break;
case 'ü':
file[1].play(4.0, 1.0);
break;
}
}
@@ -0,0 +1,80 @@
/*
This example shows how to make a simple sampler and sequencer with the Sound library. In this
sketch 5 different short samples are loaded and played back at different pitches, in this
case 5 different octaves. The sequencer triggers and event every 200-1000 mSecs randomly.
Each time a sound is played a colored rect with a random color is displayed.
*/
import processing.sound.*;
Sound stream;
SoundFile[] file;
// Define the number of samples
int numsounds = 5;
// Create an array of values which represent the octaves. 1.0 is playback at normal speed, 0.5 is half and
// therefore one octave down. 2.0 is double so one octave up.
float[] octave = {0.25, 0.5, 1.0, 2.0, 4.0};
// The playSound array is defining how many samples will be played at each trigger event
int[] playSound = {1,1,1,1,1};
// The trigger is an integer number in milliseconds so we can schedule new events in the draw loop
int trigger;
// This array holds the pixel positions of the rectangles which are drawn each event
int[] posx = {0, 128, 256, 384, 512};
void setup(){
size(640, 360);
background(255);
// Create a Sound renderer and an array of empty soundfiles
stream = new Sound(this);
file = new SoundFile[numsounds];
// Load 5 soundfiles from a folder in a for loop. By naming the files 1., 2., 3., n.aif it is easy to iterate
// through the folder and load all files in one line of code.
for (int i = 0; i < numsounds; i++){
file[i] = new SoundFile(this, (i+1) + ".aif");
}
// Create a trigger which will be the basis for our random sequencer.
trigger = millis();
}
void draw(){
// If the determined trigger moment in time matches up with the computer clock events get triggered.
if (millis() > trigger){
// Redraw the background every time to erase old rects
background(255);
// By iterating through the playSound array we check for 1 or 0, 1 plays a sound and draws a rect,
// for 0 nothing happens.
for (int i = 0; i < numsounds; i++){
// Check which indexes are 1 and 0.
if (playSound[i] == 1){
float rate;
// Choose a random color and get set to noStroke()
fill(int(random(255)),int(random(255)),int(random(255)));
noStroke();
// Draw the rect in the positions we defined earlier in posx
rect(posx[i], 50, 128, 260);
// Choose a random index of the octave array
rate = octave[int(random(0,5))];
// Play the soundfile from the array with the respective rate and loop set to false
file[i].play(rate, 1.0);
}
// Renew the indexes of playSound so that at the next event the order is different and randomized.
playSound[i] = int(random(0,2));
}
// Create a new triggertime in the future, with a random offset between 200 and 1000 milliseconds
trigger = millis() + int(random(200,1000));
}
}
@@ -0,0 +1,60 @@
/*
This example shows how to create a cluster of sine oscillators, change the frequency and detune them
depending on the position of the mouse in the renderer window. The Y position determines the basic
frequency of the oscillator and X the detuning of the oscillator. The basic frequncy ranges between
150 and 1150 Hz.
*/
import processing.sound.*;
Sound stream;
SinOsc[] sineWaves;
// The number of oscillators
int numSines = 5;
// A float for calculating the amplitudes
float[] sineVolume;
void setup() {
size(500, 500);
background(255);
//Create and start the Sound renderer
stream = new Sound(this);
// Create the oscillators and amplitudes
sineWaves = new SinOsc[numSines];
sineVolume = new float[numSines];
for (int i = 0; i < numSines; i++) {
// The overall amplitude shouldn't exceed 1.0 which is prevented by 1.0/numSines.
// The ascending waves will get lower in volume the higher the frequency
sineVolume[i] = (1.0 / numSines) / (i + 1);
// Create the Sine Oscillators and start them
sineWaves[i] = new SinOsc(this);
sineWaves[i].play();
}
}
void draw() {
noStroke();
// Map mouseY to get values from 0.0 to 1.0
float yoffset = (height - mouseY) / float(height);
// Map that value logarithmically to 150 - 1150 Hz
float frequency = pow(1000, yoffset) + 150;
// Map mouseX from -0.5 to 0.5 to get a multiplier for detuning the oscillators
float detune = float(mouseX) / width - 0.5;
// Set the frequencies, detuning and volume
for (int i = 0; i < numSines; i++) {
sineWaves[i].freq(frequency * (i + 1 + i * detune));
sineWaves[i].amp(sineVolume[i]);
}
}
@@ -0,0 +1,46 @@
/*
This example shows how to use the Fast Fourier Transform function to get the spectrum
of a sound. This function calculates the FFT of a signal and returns the positive normalized
magnitude spectrum. This means we pass it the number of bands we want (the actual FFT size is
two times that size) and a float array with the same size.
*/
import processing.sound.*;
Sound stream;
SoundFile sample;
FFT fft;
int scale=1;
int bands=512;
float[] spec = new float[bands];
public void setup() {
size(bands,360);
background(255);
// Create and start the sound renderer
stream = new Sound(this, 44100, 256);
//Load and play a soundfile and loop it. This has to be called
// before the FFT is created.
sample = new SoundFile(this, "beat.aiff");
sample.play(true);
// Create and patch the rms tracker
fft = new FFT(this);
fft.input(sample, bands);
}
public void draw() {
background(255);
fft.analyze(spec);
for(int i = 0; i < bands; i++)
{
// The result of the FFT is normalized
// draw the line for frequency band i scaling it up by 5 to get more amplitude.
line( i, height, i, height - spec[i]*height*5 );
}
}