/* * DoublePitch * * Transposes a pitch up an octave by "doubling the frequency" of the waveform. * To do this, make a copy of every other sample in the samples array, which is * analogous to how we scale images. * * Press 1 to play the original sound, 2 to play the sound with doubled pitch * Created 11 November 2007 by spc, modified 06 November 2019 * * Download the file e4.wav from http://scarl.sewanee.edu/CS276/Resources or use another file * * Created 11 November 2007 by spc */ import processing.sound.*; AudioSample dest; SoundFile ac; void setup() { // load the sound to be "doubled" ac = new SoundFile(this, "e4.wav"); int initialSize = ac.frames(); float[] samples = new float [initialSize]; float[] copy = new float [initialSize/2]; // read SoundFile's sample buffer into array ac.read(samples); // copy every other sample from the original AudioSample to the (first half) of the copy for (int j = 0, i = 0; j < initialSize/2; j++, i+=2) { // copy every other sample from original's sound buffer copy[j] = samples[i]; } dest = new AudioSample(this, copy); } void keyPressed() { if (key == '1') { ac.play(); } else if (key == '2') { dest.play(); } } void draw() { }