Merge pull request from paulkaplan/add-sound-public-api

Expose VM#addSound publicly
This commit is contained in:
Paul Kaplan 2017-04-04 09:05:45 -04:00 committed by GitHub
commit 771f18e23b
4 changed files with 44 additions and 27 deletions

29
src/import/load-sound.js Normal file
View file

@ -0,0 +1,29 @@
var AssetType = require('scratch-storage').AssetType;
var log = require('../util/log');
/**
* Load a sound's asset into memory asynchronously.
* @param {!object} sound - the Scratch sound object.
* @property {string} md5 - the MD5 and extension of the sound to be loaded.
* @property {Buffer} data - sound data will be written here once loaded.
* @param {!Runtime} runtime - Scratch runtime, used to access the storage module.
* @returns {!Promise} - a promise which will resolve after sound is loaded
*/
var loadSound = function (sound, runtime) {
if (!runtime.storage) {
log.error('No storage module present; cannot load sound asset: ', sound.md5);
return Promise.resolve(sound);
}
if (!runtime.audioEngine) {
log.error('No audio engine present; cannot load sound asset: ', sound.md5);
return Promise.resolve(sound);
}
var idParts = sound.md5.split('.');
var md5 = idParts[0];
return runtime.storage.load(AssetType.Sound, md5).then(function (soundAsset) {
sound.data = soundAsset.data;
return runtime.audioEngine.decodeSound(sound);
});
};
module.exports = loadSound;

View file

@ -5,9 +5,6 @@
* scratch-vm runtime structures.
*/
var ScratchStorage = require('scratch-storage');
var AssetType = ScratchStorage.AssetType;
var Blocks = require('../engine/blocks');
var RenderedTarget = require('../sprites/rendered-target');
var Sprite = require('../sprites/sprite');
@ -19,6 +16,7 @@ var Variable = require('../engine/variable');
var List = require('../engine/list');
var loadCostume = require('./load-costume.js');
var loadSound = require('./load-sound.js');
/**
* Parse a single "Scratch object" and create all its in-memory VM objects.
@ -148,30 +146,6 @@ var parseScratchObject = function (object, runtime, topLevel) {
return target;
};
/**
* Load a sound's asset into memory asynchronously.
* @param {!object} sound - the Scratch sound object.
* @property {string} md5 - the MD5 and extension of the sound to be loaded.
* @property {Buffer} data - sound data will be written here once loaded.
* @param {!Runtime} runtime - Scratch runtime, used to access the storage module.
*/
var loadSound = function (sound, runtime) {
if (!runtime.storage) {
log.error('No storage module present; cannot load sound asset: ', sound.md5);
return;
}
if (!runtime.audioEngine) {
log.error('No audio engine present; cannot load sound asset: ', sound.md5);
return;
}
var idParts = sound.md5.split('.');
var md5 = idParts[0];
runtime.storage.load(AssetType.Sound, md5).then(function (soundAsset) {
sound.data = soundAsset.data;
runtime.audioEngine.decodeSound(sound);
});
};
/**
* Top-level handler. Parse provided JSON,
* and process the top-level object (the stage object).

View file

@ -8,6 +8,7 @@ var sb2import = require('./import/sb2import');
var StringUtil = require('./util/string-util');
var loadCostume = require('./import/load-costume.js');
var loadSound = require('./import/load-sound.js');
var RESERVED_NAMES = ['_mouse_', '_stage_', '_edge_', '_myself_', '_random_'];
@ -209,6 +210,18 @@ VirtualMachine.prototype.addCostume = function (md5ext, costumeObject) {
}.bind(this));
};
/**
* Add a sound to the current editing target.
* @param {!object} soundObject Object representing the costume.
* @returns {?Promise} - a promise that resolves when the sound has been decoded and added
*/
VirtualMachine.prototype.addSound = function (soundObject) {
return loadSound(soundObject, this.runtime).then(function () {
this.editingTarget.sprite.sounds.push(soundObject);
this.emitTargetsUpdate();
}.bind(this));
};
/**
* Add a backdrop to the stage.
* @param {string} md5ext - the MD5 and extension of the backdrop to be loaded.

View file

@ -18,6 +18,7 @@ test('interface', function (t) {
t.type(vm.addSprite2, 'function');
t.type(vm.addCostume, 'function');
t.type(vm.addBackdrop, 'function');
t.type(vm.addSound, 'function');
t.type(vm.renameSprite, 'function');
t.type(vm.deleteSprite, 'function');