forked from scratchfoundation/scratch-flash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundEditor.as
More file actions
239 lines (203 loc) · 7.19 KB
/
SoundEditor.as
File metadata and controls
239 lines (203 loc) · 7.19 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* Scratch Project Editor and Player
* Copyright (C) 2014 Massachusetts Institute of Technology
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// SoundEditor.as
// John Maloney, June 2012
package soundedit {
import flash.display.*;
import flash.events.KeyboardEvent;
import flash.geom.*;
import flash.media.Microphone;
import flash.text.*;
import assets.Resources;
import ui.parts.*;
import uiwidgets.*;
public class SoundEditor extends Sprite {
private const waveHeight:int = 170;
private const borderColor:int = 0x606060;
private const bgColor:int = 0xF0F0F0;
private const cornerRadius:int = 20;
public var app:Scratch;
private static var microphone:Microphone = Microphone.getMicrophone();
public var waveform:WaveformView;
public var levelMeter:SoundLevelMeter;
public var scrollbar:Scrollbar;
private var buttons:Array = [];
private var playButton:IconButton;
private var stopButton:IconButton;
private var recordButton:IconButton;
private var editButton:IconButton;
private var effectsButton:IconButton;
private var recordIndicator:Shape;
private var playIndicator:Shape;
private var micVolumeLabel:TextField;
private var micVolumeSlider:Slider;
public function SoundEditor(app:Scratch, soundsPart:SoundsPart) {
this.app = app;
addChild(levelMeter = new SoundLevelMeter(12, waveHeight));
addChild(waveform = new WaveformView(this, soundsPart));
addChild(scrollbar = new Scrollbar(10, 10, waveform.setScroll));
addControls();
addIndicators();
addEditAndEffectsButtons();
addMicVolumeSlider();
updateIndicators();
}
public static function strings():Array {
var editor:SoundEditor = new SoundEditor(null, null);
editor.editMenu(Menu.dummyButton());
editor.effectsMenu(Menu.dummyButton());
return ['Edit', 'Effects', 'Microphone Volume:'];
}
public function updateTranslation():void {
if (editButton.parent) {
removeChild(editButton);
removeChild(effectsButton);
}
addEditAndEffectsButtons();
setWidthHeight(width, height);
}
public function shutdown():void { waveform.stopAll() }
public function setWidthHeight(w:int, h:int):void {
levelMeter.x = 0;
levelMeter.y = 0;
waveform.x = 23;
waveform.y = 0;
scrollbar.x = 25;
scrollbar.y = waveHeight + 5;
var waveWidth:int = w - waveform.x;
waveform.setWidthHeight(waveWidth, waveHeight);
scrollbar.setWidthHeight(waveWidth, 10);
var nextX:int = waveform.x - 2;
var buttonY:int = waveform.y + waveHeight + 25;
for each (var b:IconButton in buttons) {
b.x = nextX;
b.y = buttonY;
nextX += b.width + 8;
}
editButton.x = nextX + 20;
editButton.y = buttonY;
effectsButton.x = editButton.x + editButton.width + 15;
effectsButton.y = editButton.y;
recordIndicator.x = recordButton.x + 9;
recordIndicator.y = recordButton.y + 8;
playIndicator.x = playButton.x + 12;
playIndicator.y = playButton.y + 7;
}
private function addControls():void {
playButton = new IconButton(waveform.startPlaying, 'playSnd', null, true);
stopButton = new IconButton(waveform.stopAll, 'stopSnd', null, true);
recordButton = new IconButton(waveform.toggleRecording, 'recordSnd', null, true);
buttons = [playButton, stopButton, recordButton];
for each (var b:IconButton in buttons) {
if (b is IconButton) b.isMomentary = true;
addChild(b);
}
}
private function addEditAndEffectsButtons():void {
addChild(editButton = UIPart.makeMenuButton('Edit', editMenu, true, CSS.textColor));
addChild(effectsButton = UIPart.makeMenuButton('Effects', effectsMenu, true, CSS.textColor));
}
private function addMicVolumeSlider():void {
function setMicLevel(level:Number):void { microphone.gain = level }
addChild(micVolumeLabel = Resources.makeLabel('Microphone volume:', CSS.normalTextFormat, 22, 240));
micVolumeSlider = new Slider(130, 5, setMicLevel);
micVolumeSlider.min = 1;
micVolumeSlider.max = 100;
micVolumeSlider.value = 50;
micVolumeSlider.x = micVolumeLabel.x + micVolumeLabel.textWidth + 15;
micVolumeSlider.y = micVolumeLabel.y + 7;
addChild(micVolumeSlider);
}
private function addIndicators():void {
recordIndicator = new Shape();
var g:Graphics = recordIndicator.graphics;
g.beginFill(0xFF0000);
g.drawCircle(8, 8, 8)
g.endFill();
addChild(recordIndicator);
playIndicator = new Shape();
g = playIndicator.graphics;
g.beginFill(0xFF00);
g.moveTo(0, 0);
g.lineTo(11, 8);
g.lineTo(11, 10);
g.lineTo(0, 18);
g.endFill();
addChild(playIndicator);
}
public function updateIndicators():void {
recordIndicator.visible = waveform.isRecording();
playIndicator.visible = waveform.isPlaying();
if (microphone) micVolumeSlider.value = microphone.gain;
}
/* Menus */
private function editMenu(b:IconButton):void {
var m:Menu = new Menu();
m.addItem('undo', waveform.undo);
m.addItem('redo', waveform.redo);
m.addLine();
m.addItem('cut', waveform.cut);
m.addItem('copy', waveform.copy);
m.addItem('paste', waveform.paste);
m.addLine();
m.addItem('delete', waveform.deleteSelection);
m.addItem('select all', waveform.selectAll);
var p:Point = b.localToGlobal(new Point(0, 0));
m.showOnStage(stage, p.x + 1, p.y + b.height - 1);
}
private function effectsMenu(b:IconButton):void {
function applyEffect(selection:String):void { waveform.applyEffect(selection, shiftKey) }
var shiftKey:Boolean = b.lastEvent.shiftKey;
var m:Menu = new Menu(applyEffect);
m.addItem('fade in');
m.addItem('fade out');
m.addLine();
m.addItem('louder');
m.addItem('softer');
m.addItem('silence');
m.addLine();
m.addItem('reverse');
var p:Point = b.localToGlobal(new Point(0, 0));
m.showOnStage(stage, p.x + 1, p.y + b.height - 1);
}
/* Keyboard Shortcuts */
public function keyDown(evt:KeyboardEvent):void {
if (!stage || stage.focus) return; // sound editor is hidden or someone else has keyboard focus; do nothing
var k:int = evt.keyCode;
if ((k == 8) || (k == 127)) waveform.deleteSelection(evt.shiftKey);
if (k == 37) waveform.leftArrow();
if (k == 39) waveform.rightArrow();
if (evt.ctrlKey || evt.shiftKey) { // shift or control key commands (control keys may be grabbed by the browser on Windows...)
switch (String.fromCharCode(k)) {
case 'A': waveform.selectAll(); break;
case 'C': waveform.copy(); break;
case 'V': waveform.paste(); break;
case 'X': waveform.cut(); break;
case 'Y': waveform.redo(); break;
case 'Z': waveform.undo(); break;
}
}
if (!evt.ctrlKey) {
var ch:String = String.fromCharCode(evt.charCode);
if (ch == ' ') waveform.togglePlaying();
if (ch == '+') waveform.zoomIn();
if (ch == '-') waveform.zoomOut();
}
}
}}