Skip to content

Sample Editing

Elizabeth Hudnott edited this page Jun 27, 2019 · 14 revisions

This page contains information about techniques that manipulate a sample's raw sound data. For information about other aspects of using samples (including configuring loops) see Sampled Instruments.

Methods & Properties of Synth.Sample Objects

Some methods modify the sample "in place", that is, they overwrite the previous sample data, while others leave the original sample unchanged and return a new Synth.Sample object.

Category Applies To
Modification in place amplify(), normalize(), removeOffset(), reverse(), separateStereo()
Returns a new sample clone(), copy(), mixToMono(), pingPong(), remove()
Returns a promise resolving to a new sample chord(), insert(), mix()
Returns some other value amplitude(), findZero()
Modify buffer directly / Replace buffer buffer

Increasing or Decreasing a Sample's Volume

mySample.amplify(gain);

If gain is greater than 1 then the sample's volume is increased (using a linear scale) and if gain is less than 1 then the sample's volume is decreased. amplify() modifies the raw sound samples. The playback volume can also be altered without modifying the sample data by setting the Sample object's gain property.

Removing DC Offset

mySample.removeOffset();

Reversing a Sample

To reverse a sample:

mySample.reverse();

To create a new sample that has the sound of the original sample followed by a reversed copy:

newSample = mySample.pingPong();

Reversing Part of a Sample

mySample.reverse(from, to);

where from and to are offsets measured in numbers of individual sampling points.

Widening or Narrowing the Stereo Image

Half as wide:

mySample.separateStereo(0.5);

Twice as wide:

mySample.separateStereo(2);

Values that are too large will cause clipping.

Swapping Left and Right Channels

mySample.separateStereo(-1);

Copying a Whole Sample

newSample = mySample.clone();

Copying a Section from a Sample

newSample = mySample.copy(from, to);

from and to are measured in sample points.

"Removing" a Section from a Sample

newSample = mySample.remove(from, to);

The new sample is a copy of the original sample but with the relevant section removed. The original sample is unaffected. from and to are measured in sample points.

"Inserting" a New Section into a Sample

mySample.insert(otherSample, position).then(function (newSample) {
  ...
});

A new sample is created by combining the original sample and the other sample. The original sample is unaltered. position is measured in sample points.

Creating Chords

Because the synthesizer system has a limited number of sound channels, it's useful to be able to mix a sample with pitch shifted versions of itself so that a chord pattern can be played with a single playback instruction. For example, to create a new sample that plays a chord with 0.6 cents/semitone of stretched tuning:

mySample.chord([1, 5, 8], system.getNotes(0.6)).then(function (newSample) {
  ...
});

This creates chords with a bass note, a second note four semitones higher than the bass note, and a third note seven semitones higher than the bass note. It's a good idea to normalize the instrument after creating chords to prevent distorted playback.

Creating a Sample by Mixing Two Samples

mySample.mix(otherSample, position, loop).then(function (newSample) {
  ...
});

position is measured in sample points and determines when otherSample should begin playing into the mix. loop is true or false and determines whether or not otherSample will loop over until the end of mySample is reached.

Finding a Sample's Peak Amplitude

peakAmplitude = mySample.amplitude();

Finding the Maximum Amplitude During Part of a Sample

peakAmplitude = mySample.amplitude(from, to);

from and to are measured in sample points.

Finding Whereabouts a Sample's Waveform Crosses the Zero Line

nearestMinimum = mySample.findZero(targetPosition, channelNumber);

This finds the closest position to targetPosition where the sample waveform crosses the x-axis. The position that it computes may come before or after the original target. This method is useful for finding good positions to start and end a loop at to avoid creating a loud clicking sound each time the loop repeats.

Methods & Properties of Synth.SampledInstrument Objects

Reversing All of an Instrument's Samples

Simple reversal:

myInstrument.reverse();

Ping pong variant:

myInstrument.pingPong();

Normalizing an Instrument's Samples

myInstrument.normalize();

The instrument's samples will be amplified so that the peak amplitude (across all samples) becomes 1. It's a good idea to call removeOffset() first before normalizing.