|
1 | 1 | import p5sound from './master';
|
2 | 2 | var ac = p5sound.audiocontext;
|
3 |
| - |
| 3 | +var panner; |
4 | 4 | // Stereo panner
|
5 | 5 | // if there is a stereo panner node use it
|
6 | 6 | if (typeof ac.createStereoPanner !== 'undefined') {
|
7 |
| - p5.Panner = function (input, output) { |
8 |
| - this.stereoPanner = this.input = ac.createStereoPanner(); |
9 |
| - input.connect(this.stereoPanner); |
10 |
| - this.stereoPanner.connect(output); |
11 |
| - }; |
12 |
| - |
13 |
| - p5.Panner.prototype.pan = function (val, tFromNow) { |
14 |
| - var time = tFromNow || 0; |
15 |
| - var t = ac.currentTime + time; |
16 |
| - |
17 |
| - this.stereoPanner.pan.linearRampToValueAtTime(val, t); |
18 |
| - }; |
19 |
| - |
20 |
| - //not implemented because stereopanner |
21 |
| - //node does not require this and will automatically |
22 |
| - //convert single channel or multichannel to stereo. |
23 |
| - //tested with single and stereo, not with (>2) multichannel |
24 |
| - p5.Panner.prototype.inputChannels = function () {}; |
25 |
| - |
26 |
| - p5.Panner.prototype.connect = function (obj) { |
27 |
| - this.stereoPanner.connect(obj); |
28 |
| - }; |
29 |
| - |
30 |
| - p5.Panner.prototype.disconnect = function () { |
31 |
| - if (this.stereoPanner) { |
32 |
| - this.stereoPanner.disconnect(); |
| 7 | + class Panner { |
| 8 | + constructor(input, output) { |
| 9 | + this.stereoPanner = this.input = ac.createStereoPanner(); |
| 10 | + input.connect(this.stereoPanner); |
| 11 | + this.stereoPanner.connect(output); |
| 12 | + } |
| 13 | + |
| 14 | + pan(val, tFromNow) { |
| 15 | + var time = tFromNow || 0; |
| 16 | + var t = ac.currentTime + time; |
| 17 | + |
| 18 | + this.stereoPanner.pan.linearRampToValueAtTime(val, t); |
| 19 | + } |
| 20 | + |
| 21 | + //not implemented because stereopanner |
| 22 | + //node does not require this and will automatically |
| 23 | + //convert single channel or multichannel to stereo. |
| 24 | + //tested with single and stereo, not with (>2) multichannel |
| 25 | + inputChannels() {} |
| 26 | + |
| 27 | + connect(obj) { |
| 28 | + this.stereoPanner.connect(obj); |
| 29 | + } |
| 30 | + |
| 31 | + disconnect() { |
| 32 | + if (this.stereoPanner) { |
| 33 | + this.stereoPanner.disconnect(); |
| 34 | + } |
33 | 35 | }
|
34 |
| - }; |
| 36 | + } |
| 37 | + |
| 38 | + panner = Panner; |
35 | 39 | } else {
|
36 | 40 | // if there is no createStereoPanner object
|
37 | 41 | // such as in safari 7.1.7 at the time of writing this
|
38 | 42 | // use this method to create the effect
|
39 |
| - p5.Panner = function (input, output, numInputChannels) { |
40 |
| - this.input = ac.createGain(); |
41 |
| - input.connect(this.input); |
42 |
| - |
43 |
| - this.left = ac.createGain(); |
44 |
| - this.right = ac.createGain(); |
45 |
| - this.left.channelInterpretation = 'discrete'; |
46 |
| - this.right.channelInterpretation = 'discrete'; |
47 |
| - |
48 |
| - // if input is stereo |
49 |
| - if (numInputChannels > 1) { |
50 |
| - this.splitter = ac.createChannelSplitter(2); |
51 |
| - this.input.connect(this.splitter); |
52 |
| - |
53 |
| - this.splitter.connect(this.left, 1); |
54 |
| - this.splitter.connect(this.right, 0); |
55 |
| - } else { |
56 |
| - this.input.connect(this.left); |
57 |
| - this.input.connect(this.right); |
58 |
| - } |
| 43 | + class Panner { |
| 44 | + constructor(input, output, numInputChannels) { |
| 45 | + this.input = ac.createGain(); |
| 46 | + input.connect(this.input); |
59 | 47 |
|
60 |
| - this.output = ac.createChannelMerger(2); |
61 |
| - this.left.connect(this.output, 0, 1); |
62 |
| - this.right.connect(this.output, 0, 0); |
63 |
| - this.output.connect(output); |
64 |
| - }; |
65 |
| - |
66 |
| - // -1 is left, +1 is right |
67 |
| - p5.Panner.prototype.pan = function (val, tFromNow) { |
68 |
| - var time = tFromNow || 0; |
69 |
| - var t = ac.currentTime + time; |
70 |
| - var v = (val + 1) / 2; |
71 |
| - var rightVal = Math.cos((v * Math.PI) / 2); |
72 |
| - var leftVal = Math.sin((v * Math.PI) / 2); |
73 |
| - this.left.gain.linearRampToValueAtTime(leftVal, t); |
74 |
| - this.right.gain.linearRampToValueAtTime(rightVal, t); |
75 |
| - }; |
76 |
| - |
77 |
| - p5.Panner.prototype.inputChannels = function (numChannels) { |
78 |
| - if (numChannels === 1) { |
79 |
| - this.input.disconnect(); |
80 |
| - this.input.connect(this.left); |
81 |
| - this.input.connect(this.right); |
82 |
| - } else if (numChannels === 2) { |
83 |
| - if (typeof this.splitter === 'undefined') { |
| 48 | + this.left = ac.createGain(); |
| 49 | + this.right = ac.createGain(); |
| 50 | + this.left.channelInterpretation = 'discrete'; |
| 51 | + this.right.channelInterpretation = 'discrete'; |
| 52 | + |
| 53 | + // if input is stereo |
| 54 | + if (numInputChannels > 1) { |
84 | 55 | this.splitter = ac.createChannelSplitter(2);
|
| 56 | + this.input.connect(this.splitter); |
| 57 | + |
| 58 | + this.splitter.connect(this.left, 1); |
| 59 | + this.splitter.connect(this.right, 0); |
| 60 | + } else { |
| 61 | + this.input.connect(this.left); |
| 62 | + this.input.connect(this.right); |
| 63 | + } |
| 64 | + |
| 65 | + this.output = ac.createChannelMerger(2); |
| 66 | + this.left.connect(this.output, 0, 1); |
| 67 | + this.right.connect(this.output, 0, 0); |
| 68 | + this.output.connect(output); |
| 69 | + } |
| 70 | + |
| 71 | + // -1 is left, +1 is right |
| 72 | + pan(val, tFromNow) { |
| 73 | + var time = tFromNow || 0; |
| 74 | + var t = ac.currentTime + time; |
| 75 | + var v = (val + 1) / 2; |
| 76 | + var rightVal = Math.cos((v * Math.PI) / 2); |
| 77 | + var leftVal = Math.sin((v * Math.PI) / 2); |
| 78 | + this.left.gain.linearRampToValueAtTime(leftVal, t); |
| 79 | + this.right.gain.linearRampToValueAtTime(rightVal, t); |
| 80 | + } |
| 81 | + |
| 82 | + inputChannels(numChannels) { |
| 83 | + if (numChannels === 1) { |
| 84 | + this.input.disconnect(); |
| 85 | + this.input.connect(this.left); |
| 86 | + this.input.connect(this.right); |
| 87 | + } else if (numChannels === 2) { |
| 88 | + if (typeof this.splitter === 'undefined') { |
| 89 | + this.splitter = ac.createChannelSplitter(2); |
| 90 | + } |
| 91 | + this.input.disconnect(); |
| 92 | + this.input.connect(this.splitter); |
| 93 | + this.splitter.connect(this.left, 1); |
| 94 | + this.splitter.connect(this.right, 0); |
85 | 95 | }
|
86 |
| - this.input.disconnect(); |
87 |
| - this.input.connect(this.splitter); |
88 |
| - this.splitter.connect(this.left, 1); |
89 |
| - this.splitter.connect(this.right, 0); |
90 | 96 | }
|
91 |
| - }; |
92 | 97 |
|
93 |
| - p5.Panner.prototype.connect = function (obj) { |
94 |
| - this.output.connect(obj); |
95 |
| - }; |
| 98 | + connect(obj) { |
| 99 | + this.output.connect(obj); |
| 100 | + } |
96 | 101 |
|
97 |
| - p5.Panner.prototype.disconnect = function () { |
98 |
| - if (this.output) { |
99 |
| - this.output.disconnect(); |
| 102 | + disconnect() { |
| 103 | + if (this.output) { |
| 104 | + this.output.disconnect(); |
| 105 | + } |
100 | 106 | }
|
101 |
| - }; |
| 107 | + } |
| 108 | + panner = Panner; |
102 | 109 | }
|
103 | 110 |
|
104 |
| -export default p5.Panner; |
| 111 | + |
| 112 | +export default panner; |
| 113 | + |
0 commit comments