aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/oscillatornode/setperiodicwave/index.html
blob: 43f27ec69809e4a48d78299ae5ccfc5ff4b616c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
---
title: OscillatorNode.setPeriodicWave()
slug: Web/API/OscillatorNode/setPeriodicWave
translation_of: Web/API/OscillatorNode/setPeriodicWave
---
<p>{{ APIRef("Web Audio API") }}</p>

<p> {{ domxref("OscillatorNode") }} 接口的 <strong><code>setPeriodicWave() </code></strong><code>方法</code>用来指向 {{domxref("PeriodicWave")}},PeriodicWave 定义了一个周期性波形能够形成oscillator的输出, 当{{domxref("OscillatorNode.type", "type")}} 是 <code>custom 的时候</code>.</p>

<div class="note">
<p>该方法取代了废弃的 {{ domxref("OscillatorNode.setWaveTable()")}}.</p>
</div>

<h2 id="语法">语法</h2>

<pre class="syntaxbox"><em>OscillatorNode</em>.setPeriodicWave(<em>wave</em>);</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>wave</code></dt>
 <dd>一个 {{domxref("PeriodicWave")}} 对象, 表示特定的波形用来形成oscillator的输出。</dd>
</dl>

<h3 id="返回值">返回值</h3>

<p>{{jsxref("undefined")}}</p>

<h2 id="示例">示例</h2>

<p>下面示例说明了 <code>createPeriodicWave() 方法的简单使用</code>, 从一个周期波形中重新生成了一个正弦波形。</p>

<pre class="brush: js;highlight[13]">var real = new Float32Array(2);
var imag = new Float32Array(2);
var ac = new AudioContext();
var osc = ac.createOscillator();

real[0] = 0;
imag[0] = 0;
real[1] = 1;
imag[1] = 0;

var wave = ac.createPeriodicWave(real, imag);

osc.setPeriodicWave(wave);

osc.connect(ac.destination);

osc.start();
osc.stop(2);</pre>

<p>这段代码可以运行因为通过定义一个正弦波形使得声音包含了基础的音调。<br>
  </p>

<p>这里,我们用两个值创建一个{{domxref("PeriodicWave")}}。第一个值是直流偏移,是oscillator开始的时候的值。这里 0 很合适,因为我们想要在 [-1.0; 1.0] 这个范围的中间值开始。</p>

<p>第二个和后面的值是正弦和余弦内容。可以把它看做傅里叶变换的结果,使得可以从时间阈值得到频率阈值。这里通过 <code>createPeriodicWave() 方法,可以指定频率,并且浏览器执行逆傅里叶变换来得到一个时间阈值缓冲。</code>Here, we only set one component at full volume (1.0) on the fundamental tone, so we get a sine wave.</p>

<h2 id="说明">说明</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('Web Audio API', '#widl-OscillatorNode-setPeriodicWave-void-PeriodicWave-periodicWave', 'setPeriodicWave')}}</td>
   <td>{{Spec2('Web Audio API')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Edge</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>14 {{property_prefix("webkit")}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>23</td>
   <td>{{CompatNo}}</td>
   <td>15 {{property_prefix("webkit")}}<br>
    22 (unprefixed)</td>
   <td>6 {{property_prefix("webkit")}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome</th>
   <th>Edge</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>Firefox OS</th>
   <th>IE Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>28 {{property_prefix("webkit")}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>25</td>
   <td>1.2</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{property_prefix("webkit")}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web_Audio_API/Using_Web_Audio_API">Using the Web Audio API</a></li>
 <li><a href="/en-US/docs/Web/API/AudioContext/createPeriodicWave">AudioContext.createPeriodicWave</a></li>
</ul>