# 2.4 持续时间

在前面的部分中，我们学习了如何使用“sleep”指令来控制何时触发我们的声音。

为了给我们一个简单但强大的“控制声音持续时间”的方法，Sonic Pi提供了“ADSR振幅包络”的概念（我们将在本节后面介绍ADSR的含义）。幅度包络提供了两个有用的控制方面：

控制声音的持续时间 控制声音的幅度

## 持续时间

持续时间是声音持续的长度。一个长的持续时间意味着你会更长的听到你的声音。Sonic Pi的声音都具有可控制的幅度包络，该包络的总持续时间是声音的持续时间。因此，通过控制包络，您可以控制持续时间。

## 振幅

ADSR包络不仅可以控制持续时间，还可以“精确控制声音的振幅”。所有可听声音开始和结束都是静音，并且中间包含一些非静音部分。信封允许您滑动并保持声音的非静音部分的振幅。这就像给某人指示如何调高吉他放大器的音量。例如，你可能会要求某人“从沉默开始，慢慢向上移动到最大音量，保持一段时间，然后迅速回到沉默状态。” Sonic Pi允许您使用信封准确编程此行为。

回顾一下，正如我们之前看到的，振幅为0是静音，幅度为1是正常音量。

现在, 让我们依次查看信封的各个部分。

## 释放阶段

默认情况下使用的信封的唯一部分是淡出时间。这是合成器声音淡出所需的时间。所有合成器的释放时间均为1，这意味着默认情况下它们的持续时间为1拍（默认BPM为60时为1秒）：

    play 70

The note will be audible for 1 second. Go ahead and time it :-) This is short hand for the longer more explicit version:

    play 70, release: 1

注意这听起来完全一样（声音持续一秒钟）。但是，现在很容易通过修改“release：opt”的值来改变持续时间：

    play 60, release: 2

通过使用非常短的释放时间，我们可以在很短的时间内完成播放声音：

    play 60, release: 0.2

释放声音的持续时间称为“释放阶段”，默认情况下是线性过渡（即直线）。下图说明了这种转变：

图表最左侧的垂直线显示声音从0幅度开始，但立即上升到全幅度（这是我们接下来要介绍的渐强阶段）。一旦达到全振幅，它就会以“释放：”指定的时间量直线向下移动到零。更长的释放时间会产生更长的合成淡出效果。

因此，您可以通过更改减弱时间来更改声音的持续时间。播放时为您的音乐添加减弱时间。

## 渐强阶段

默认情况下，所有合成器的“渐强阶段”为0，这意味着它们立即从0幅度移动到1。这为合成器提供了初始的打击声。但是，您可能希望淡入您的声音。这可以通过“attack:opt”来实现。尝试淡化一些声音：

    play 60, attack: 2
    sleep 3
    play 65, attack: 0.5

您可以同时使用多个参数。例如，对于短渐强和长释放尝试：

    play 60, attack: 0.7, release: 4

这个短渐强和长释放包络如下图所示：

当然，你可以随意的改变这些。试试长的渐强和短淡出：

    play 60, attack: 4, release: 0.7

Finally, you can also have both short attack and release times for shorter sounds.

    play 60, attack: 0.5, release: 0.5

## Sustain Phase

In addition to specifying attack and release times, you may also specify a sustain time to control the sustain phase. This is the time for which the sound is maintained at full amplitude between the attack and release phases.

    play 60, attack: 0.3, sustain: 1, release: 1

The sustain time is useful for important sounds you wish to give full presence in the mix before entering an optional release phase. Of course, it’s totally valid to set both the `attack:` and `release:` opts to 0 and just use the sustain to have absolutely no fade in or fade out to the sound. However, be warned, a release of 0 can produce clicks in the audio and it’s often better to use a very small value such as 0.2.

## Decay Phase

For an extra level of control, you can also specify a decay time. This is a phase of the envelope that fits between the attack and sustain phases and specifies a time where the amplitude will drop from the `attack_level:` to the `decay_level:` (which unless you explicitly set it will be set to the `sustain_level:`). By default, the `decay:` opt is 0 and both the attack and sustain levels are 1 so you’ll need to specify them for the decay time to have any effect:

    play 60, attack: 0.1, attack_level: 1, decay: 0.2, sustain_level: 0.4, sustain: 1, release: 0.5

## Decay Level

One last trick is that although the `decay_level:` opt defaults to be the same value as `sustain_level:` you can explicitly set them to different values for full control over the envelope. This allows you to to create envelopes such as the following:

    play 60, attack: 0.1, attack_level: 1, decay: 0.2, decay_level: 0.3, sustain: 1, sustain_level: 0.4, release: 0.5

It’s also possible to set the `decay_level:` to be higher than `sustain_level:`:

    play 60, attack: 0.1, attack_level: 0.1, decay: 0.2, decay_level: 1, sustain: 0.5, sustain_level: 0.8, release: 1.5

## ADSR Envelopes

So to summarise, Sonic Pi’s ADSR envelopes have the following phases:

attack - time from 0 amplitude to the `attack_level`, decay - time to move amplitude from `attack_level` to `decay_level`, sustain - time to move the amplitude from `decay_level` to `sustain_level`, release - time to move amplitude from `sustain_level` to 0

It’s important to note that the duration of a sound is the summation of the times of each of these phases. Therefore the following sound will have a duration of 0.5 + 1 + 2 + 0.5 = 4 beats:

    play 60, attack: 0.5, attack_level: 1, decay: 1, sustain_level: 0.4, sustain: 2, release: 0.5

Now go and have a play adding envelopes to your sounds…