added volume support for notes (under construction)

pull/89/head
jmoenig 2019-04-01 17:43:45 +02:00
rodzic da6ae70e7d
commit 6d2ed9752c
4 zmienionych plików z 43 dodań i 10 usunięć

Wyświetl plik

@ -57,6 +57,7 @@
* Objects: let the Microphone share the Note prototype's AudioContext
* Objects: took out gain node from Note oscillator (will be used for "volume" setting)
* Objects: refactored audio context sharing and lazy initialization
* Objects, Threads: added volume support for notes (under construction)
### 2019-03-31
* Blocks, Threads: added "stage width" and "stage height" as gettable attributes to MY

Wyświetl plik

@ -7,7 +7,7 @@
<script type="text/javascript" src="src/morphic.js?version=2019-02-07"></script>
<script type="text/javascript" src="src/widgets.js?version=2018-10-02"></script>
<script type="text/javascript" src="src/blocks.js?version=2019-03-31"></script>
<script type="text/javascript" src="src/threads.js?version=2019-03-31"></script>
<script type="text/javascript" src="src/threads.js?version=2019-04-01"></script>
<script type="text/javascript" src="src/objects.js?version=2019-04-01"></script>
<script type="text/javascript" src="src/gui.js?version=2019-03-25"></script>
<script type="text/javascript" src="src/paint.js?version=2019-02-22"></script>

Wyświetl plik

@ -1407,7 +1407,6 @@ function SpriteMorph(globals) {
SpriteMorph.prototype.init = function (globals) {
this.name = localize('Sprite');
this.instrument = null;
this.variables = new VariableFrame(globals || null, this);
this.scripts = new ScriptsMorph();
this.customBlocks = [];
@ -1419,6 +1418,13 @@ SpriteMorph.prototype.init = function (globals) {
this.normalExtent = new Point(60, 60); // only for costume-less situation
this.scale = 1;
this.rotationStyle = 1; // 1 = full, 2 = left/right, 0 = off
this.instrument = null;
// volume support, experimental: // +++
// tweak for fullcopy and clone
this.volume = 100;
this.gainNode = null; // must be lazily initialized in Chrome, sigh...
this.version = Date.now(); // for observer optimization
this.isTemporary = false; // indicate a temporary Scratch-style clone
this.isCorpse = false; // indicate whether a sprite/clone has been deleted
@ -1485,6 +1491,7 @@ SpriteMorph.prototype.fullCopy = function (forClone) {
c.instances = [];
c.stopTalking();
c.color = this.color.copy();
c.gainNode = null; // +++
c.blocksCache = {};
c.paletteCache = {};
c.cachedHSV = c.color.hsv();
@ -3249,6 +3256,27 @@ SpriteMorph.prototype.reportSounds = function () {
return this.sounds;
};
// experimental volume ops: +++
SpriteMorph.prototype.setVolume = function (num) {
this.volume = Math.max(Math.min(+num, 100), 0);
this.getGainNode().gain.setValueAtTime(
this.volume / 100,
this.audioContext().currentTime
);
};
SpriteMorph.prototype.getGainNode = function () {
if (!this.gainNode) {
this.gainNode = this.audioContext().createGain();
}
return this.gainNode;
};
SpriteMorph.prototype.audioContext = function () {
return Note.prototype.getAudioContext();
};
// SpriteMorph user menu
SpriteMorph.prototype.userMenu = function () {
@ -8898,7 +8926,7 @@ Note.prototype.getAudioContext = function () {
// Note playing
Note.prototype.play = function (type) {
Note.prototype.play = function (type, gainNode) {
this.oscillator = this.audioContext.createOscillator();
if (!this.oscillator.start) {
this.oscillator.start = this.oscillator.noteOn;
@ -8914,11 +8942,12 @@ Note.prototype.play = function (type) {
][(type || 1) - 1];
this.oscillator.frequency.value = isNil(this.frequency) ?
Math.pow(2, (this.pitch - 69) / 12) * 440 : this.frequency;
this.oscillator.connect(this.audioContext.destination);
/*
this.oscillator.connect(this.gainNode);
this.gainNode.connect(this.audioContext.destination);
*/
// +++ this.oscillator.connect(this.audioContext.destination);
///*
this.oscillator.connect(gainNode);
gainNode.connect(this.audioContext.destination);
//*/
this.oscillator.start(0);
};

Wyświetl plik

@ -62,7 +62,7 @@ StageMorph, SpriteMorph, StagePrompterMorph, Note, modules, isString, copy,
isNil, WatcherMorph, List, ListWatcherMorph, alert, console, TableMorph, Color,
TableFrameMorph, ColorSlotMorph, isSnapObject, Map*/
modules.threads = '2019-March-31';
modules.threads = '2019-April-01';
var ThreadManager;
var Process;
@ -4095,7 +4095,10 @@ Process.prototype.doPlayNoteForSecs = function (pitch, secs) {
if (!this.context.startTime) {
this.context.startTime = Date.now();
this.context.activeNote = new Note(pitch);
this.context.activeNote.play(this.instrument);
this.context.activeNote.play(
this.instrument,
this.blockReceiver().getGainNode() // +++
);
}
if ((Date.now() - this.context.startTime) >= (secs * 1000)) {
if (this.context.activeNote) {