# Sampling Tutorial 7: Sampling Review

## A sampling exercise

In this chapter we suggest an exercise to help you check your understanding of how to sample and play audio. Try completing this exercise in a new file of your own before you check the solution given in the example patch. (But don't have the example Patcher open while you design your own patch, or you will hear both patches when you turn audio on.) The exercise is to design a patch in which:

• Typing the a key on the computer keyboard turns audio on. Typing a again toggles audio off.
• Typing r on the computer keyboard makes a one-second recording of whatever audio is coming into the computer.
• Typing p plays the recording. Playback is to be at half speed, so that the sound lasts two seconds.
• An amplitude envelope is applied to the sample when it is played, tapering the amplitude slightly at the beginning and end so that there are no sudden clicks heard at either end of the sample.
• The sample is played back with a 3 Hz vibrato added to it. The depth of the vibrato is one semitone up and down (a factor of 2±1/12).

## Hints

You will need to store the sound in a buffer~ and play it back from memory.

You can record directly into the buffer~ with record~. (See Record and Play Samples.) The input to record~ will come from adc~ (or ezadc~).

The two obvious choices for playing a sample from a buffer~ at half speed are play~ and groove~. However, because we want to add vibrato to the sound - by continuously varying the playback speed - the better choice is groove~, which uses a (possibly time-varying) signal to control its playback speed directly. (See "Playback with Loops".)

The amplitude envelope is best generated by a line~ object which is sending its output to a *~ object to scale the amplitude of the output signal (coming from groove~). You might want to use a function object to draw the envelope, and send its output to line~ to describe the envelope. (See "Additive Synthesis".)

The computer keyboard will need to trigger messages to the adc~, record~, groove~, and line~ (or function) objects in order to perform the required tasks. Use the key object to get the keystrokes, and use select to detect the keys you want to use.

Use a sinusoidal wave from a cycle~ object to apply vibrato to the sample. The frequency of the cycle~ will determine the rate of the vibrato, and the amplitude of the sinusoid will determine the depth of vibrato. Therefore, you will need to scale the cycle~ object's amplitude with a *~ object to achieve the proper vibrato depth.

In the discussion of vibrato in the synthesis tutorials, we created vibrato by adding the output of the modulating oscillator to the frequency input of the carrier oscillator. However, two things are different in this exercise. First of all, the modulating oscillator needs to modulate the playback speed of groove~ rather than the frequency of another cycle~ object. Second, adding the output of the modulator to the input of the carrier - as in "Vibrato and Frequency Modulation" - creates a vibrato of equal frequency above and below the carrier frequency, but does not create a vibrato of equal pitch up and down (as required in this exercise). A change in pitch is achieved by multiplying the carrier frequency by a certain amount, rather than by adding an amount to it.

To raise the pitch of a tone by one semitone, you must multiply its frequency by the twelfth root of 2, which is a factor of 2 to the 1/12 power (approximately 1.06). To lower the pitch of a tone by one semitone, you must multiply its frequency by 2 to the -1/12 power (approximately 0.944). To calculate a signal value that changes continuously within this range, you may need to use an MSP object not yet discussed, pow~. Consult its description in the Objects section of this manual for details.

## Solution

• Scroll the example Patcher window all the way to the right to see a solution to this exercise.

The arguments to the buffer~ object specify a length in milliseconds (1000) and a number of channels (2). This determines how much memory will initially be allocated to the buffer~.

Since the memory allocated in the buffer~ is limited to one second, there is no need to tell the record~ object to stop when you record into the buffer~. It stops when it reaches the end of the buffer~.

The keystrokes from the computer keyboard are reported by key, and the select object is used to detect the a, r, and p keys. The bangs from select trigger the necessary messages to adc~, record~, and groove~.

The keystroke p is also used to trigger the amplitude envelope at the same time as the sample is played. This envelope is used to scale the output of groove~.

A sig~ 0.5 object sets the basic playback speed of groove~ at half speed. The amplitude of a 3 Hz cosine wave is scaled by a factor of 0.083333 (equal to 1/12, but more computationally efficient than dividing by 12) so that it varies from -1/12 to 1/12. This sinusoidal signal is used as the exponent in a power function in pow~ (2 to the power of the input), and the result is used as the factor by which to multiply the playback speed.