# 波封设置后的样本(Enveloped Samples)

用 ADSR 波封去调整样本的 持续时间(duration) 和 振幅(amplitude) 也是同样可能的。然而，这会和运用在合成音上的 ADSR 波封有些许不同。样本波封只允许你减少样本的振幅和持续时间——而永远无法增加。这段样本将会在样本本身播放结束或波封完成的时候结束——这取决于哪种情况先发生。所以说，如果你选用一个很长的“release:”，也并不会增加样本的持续时间。

## Amen 波封

让我们回到我们信赖的伙伴Amen Break：

    sample :loop_amen

不做设置，我们听到全振幅下完整的样本。如果我们想让它1秒钟内淡入，我们用“attack:”参数：

    sample :loop_amen, attack: 1

至于短些的淡入，选一个短些的起音参数值：

    sample :loop_amen, attack: 0.3

## 自动延音

ADSR 波封的表现和标准合成音波封不同之处在于延持(sustain)值。在标准合成音波封里，延持默认值是0，除非你手动设置。对于样本，延持值默认为魔力自动(automagical)值——余下的演奏样本剩余部分的时间。这就是为什么当我们没有输入任何首选项我们还能听到完整样本。要是起音、衰减、延持和释音的值都是0，那永远只会是一片死寂。所以Sonic Pi计算样本长度，推断出一些起音、衰退和释音，用这个结果作延持时间。如果起音、衰退和释音的值加起来总和多于样本持续时间，延持(sustain)会被简单设为0.

## 淡出

探索一波这个选项，我们来细致斟酌一下我们的Amen break。如果我们问Sonic Pi这个样本有多长：

    print sample_duration :loop_amen

它会输出“1.753310657596372”，也就是以秒为单位的样本长度。这里为了方便我们姑且把它约为1.75。好，如果我们把释音设置成“0.75”，惊人的事情会发生：

    sample :loop_amen, release: 0.75

这行代码会以全振幅演奏样本的第一秒，而后在0.75秒内淡出。这是自动延持(auto sustain)的实践。初始状态下，释音通常从样本的结尾生效。如果我们的样本时长为10.75秒，程序会以全振幅演奏前10秒，而后在0.75秒内淡出。

记住：初始状态下，“release:”在样本结尾淡出。

## 淡入和淡出

我们可以同时用“attack:”和“release:”这两个参数和自动延持行为一起在样本持续演奏时淡入和淡出：

    sample :loop_amen, attack: 0.75, release: 0.75

As the full duration of the sample is 1.75s and our attack and release phases add up to 1.5s, the sustain is automatically set to 0.25s. This allows us to easily fade the sample in and out.

## Explicit sustain

We can easily get back to our normal synth ADSR behaviour by manually setting `sustain:` to a value such as 0:

    sample :loop_amen, sustain: 0, release: 0.75

Now, our sample only plays for 0.75 seconds in total. With the default for `attack:` and `decay:` at 0, the sample jumps straight to full amplitude, sustains there for 0s then releases back down to 0 amplitude over the release period - 0.75s.

## Percussive cymbals

We can use this behaviour to good effect to turn longer sounding samples into shorter, more percussive versions. Consider the sample `:drum_cymbal_open`:

    sample :drum_cymbal_open

You can hear the cymbal sound ringing out over a period of time. However, we can use our envelope to make it more percussive:

    sample :drum_cymbal_open, attack: 0.01, sustain: 0, release: 0.1

You can then emulate hitting the cymbal and then dampening it by increasing the sustain period:

    sample :drum_cymbal_open, attack: 0.01, sustain: 0.3, release: 0.1

Now go and have fun putting envelopes over the samples. Try changing the rate too for really interesting results.