The p5.js sound library documentation says that removeCue() can be used to cancel cued events. It says it takes an ID input that is returned from addCue().
When I invoke addCue and store the result to a variable it does not return an ID. It returns NaN.
The image below is a code example I wrote using the p5.js code editor.
How do I get the id ?
OK, i found the issue.
Its an issue with the library https://github.com/processing/p5.js/blob/master/lib/addons/p5.sound.js
look at this link https://github.com/processing/p5.js/blob/master/lib/addons/p5.sound.js#L2178
its using var id = this._cueIDCounter++; but _cueIDCounter was never defined.
so i tried to define it like the following for your code:
Object.defineProperty(mySound,'_cueIDCounter',{value:1,writable:true});
now it returned the id.
so then i tried to remove the cue with removeCue but to my surprise there is also an issue which is getting error Uncaught TypeError: Cannot read property 'splice' of undefined
so then i looked again at the library code and i realised on the following line https://github.com/processing/p5.js/blob/master/lib/addons/p5.sound.js#L2198 within the removeCue function there is an error the current code is
p5.SoundFile.prototype.removeCue = function (id) {
var cueLength = this._cues.length;
for (var i = 0; i < cueLength; i++) {
var cue = this._cues[i];
if (cue.id === id) {
this.cues.splice(i, 1);
}
}
if (this._cues.length === 0) {
}
};
but it should be using this._cues.splice(i, 1); instead of this.cues.splice(i, 1);
Related
The last time I started writing my code editor, I use a code mirror for my project. I want to add a duplicate current line, but something still goes wrong.
My code:
var current_cursor = CodeMirror.doc.getCursor();
var line_content = CodeMirror.doc.getLine(current_cursor.line);
CodeMirror.commands.goLineEnd(CodeMirror);
CodeMirror.commands.newlineAndIndent(CodeMirror);
CodeMirror.doc.replaceSelection(line_content);
CodeMirror.doc.setCursor(current_cursor.line + 1, current_cursor.ch);
I think that the same name of variable CodeMirror and CodeMirror object can cause this error:
CodeMirror = CodeMirror(document.querySelector('.editor'), {
extraKeys: { 'Ctrl-D': duplicate_line },
});
Have you got any ideas what is going wrong?
I finally found the answer. I do not know why my code was not working. For everybody that has got the same problem:
async function duplicate_line()
{
var current_cursor = CodeMirror.doc.getCursor();
var line_content = CodeMirror.doc.getLine(current_cursor.line);
CodeMirror.execCommand('goLineEnd');
CodeMirror.execCommand('newlineAndIndent');
CodeMirror.doc.replaceSelection(line_content);
CodeMirror.doc.setCursor(current_cursor.line + 1, current_cursor.ch);
}
I'm following the basic example of the ML5.js featureExtractor. I am not using video. After loading a model I am adding new images to it, and then training again. I get the following error:
Mobilenet.js:323 Uncaught (in promise) Error: Batch size is 0 or NaN. Please choose a non-zero fraction.
at t. (Mobilenet.js:323)
My code seems correct, according to the documentation - using video is optional, so I expect I should be able to re-train the model after just adding images manually. I used the callback for the addImage function, to make sure the images are really added before calling train
let added = 0
let classifier
let featureExtractor = ml5.featureExtractor('MobileNet', modelLoaded)
function modelLoaded() {
classifier = featureExtractor.classification()
classifier.addImage(document.getElementById('person1'), 'nomask', addedImage)
classifier.addImage(document.getElementById('mask1'), 'mask', addedImage)
}
// this gets called twice, but then train goes wrong
function addedImage(){
added++
if(added == 2){
classifier.train((lossValue) => {
console.log('Loss is', lossValue);
})
}
You need to add at least 3 images for training to work.
The following code should work.
let added = 0;
let classifier;
let featureExtractor = ml5.featureExtractor('MobileNet', modelLoaded);
function modelLoaded() {
classifier = featureExtractor.classification()
classifier.addImage(document.getElementById('person1'), 'nomask', addedImage);
classifier.addImage(document.getElementById('person2'), 'nomask', addedImage);
classifier.addImage(document.getElementById('mask1'), 'mask', addedImage);
}
function addedImage(){
added++;
if(added == 3){
classifier.train((lossValue) => {
console.log('Loss is', lossValue);
});
}
A working example: https://glitch.com/edit/#!/ml5-feature-extractor-addimage
I have a jQuery script shown bellow. It is suppose to get AWS ML prediction and compare the score and depending upon the score append to a cell in a table the correct prediction. All these cells have the same class prediction. When the button is clicked and this script is triggered, it does everything correctly but when it gets to the appending part it throwing an error saying Cannot read property 'append' of undefined.
When i run the same script inside chrome console it appends correctly to the correct places. I am not really sure why this is happening.
$(document).ready(function(){
var highPred="";
$("#predict").click(function(){
var predictionTables = $(".prediction");
for (var i = 0; i < 4; i++) {
var predictedScoresAWS=[];
var params = {
Record: myData[i]
};
machinelearning.predict(params, function(err, data) {
if (err){
console.log(err, err.stack); // an error occurred
} else{
console.log(data); // successful response
// data = $.parseJSON(data);
predictedScoresAWS.push(data.Prediction.predictedScores['Reduce purge history day count']);
predictedScoresAWS.push(data.Prediction.predictedScores['Increase the java heap space']);
predictedScoresAWS.push(data.Prediction.predictedScores['Successful run']);
predictedScoresAWS.push(data.Prediction.predictedScores['Other error']);
console.log(predictedScoresAWS)
var highPredIndex = predictedScoresAWS.indexOf(Math.max(...predictedScoresAWS))
switch(highPredIndex){
case 0:
highPred='Reduce purge history day count';
break;
case 1:
highPred='Increase the java heap space';
break;
case 2:
highPred='Successful run';
break;
case 3:
highPred='Other error';
}
console.log(highPred);
console.log(predictionTables);
console.log(predictionTables[i])
while (predictedScoresAWS.length) { predictedScoresAWS.pop(); }
predictionTables[i.].append(highPred);
}
});
}
});
});
When you use a subscript with a jQuery collection it returns the DOM object, not a jQuery object. You should use .eq() to get the jQuery object, and then you can call the jQuery append() method.
predictionTables[i].append(highPred);
should be:
predictionTables.eq(i).append(highPred);
Another problem is that you're trying to use the iteration variable i in an asynchronous callback. See JavaScript closure inside loops – simple practical example and Javascript infamous Loop issue? for the problem with this and many solutions. If you can use ES6, just change var i = 0 to let i = 0.
I am using PDFJS to get textual data from PDF files, but occasionally encountering the following error:
Error: Invalid XRef table: unexpected first object.
I would prefer that my code just skip over problem files and continue on to the next file in the list. According to PDFJS documentation, setting stopAtErrors to true for the DocumentInitParameters in PDFJS should result in rejection of getTextContent when the associated PDF data cannot be successfully parsed. I am not finding such to be the case: even after setting stopAtErrors to true, I continue to get the above error and the code seems to be "spinning" on the problem file rather than just moving on to the next in the list. It is possible that I haven't properly set stopAtErrors to true as I think I have. A snippet of my code is below to illustrate what I think I've done (code based on this example):
// set up the variables to pass to getDocument, including the pdf file's url:
var obj = {};
obj.url = http://www.whatever.com/thefile.pdf; // the specific url linked to desired pdf file goes here
obj.stopAtErrors = true;
// now have PDF JS read in the file:
PDFJS.getDocument(obj).then(function(pdf) {
var pdfDocument = pdf;
var pagesPromises = [];
for (var i = 0; i < pdf.pdfInfo.numPages; i++) {
(function (pageNumber) {
pagesPromises.push(getPageText(pageNumber, pdfDocument));
}) (i+1);
}
Promise.all(pagesPromises).then(function(pagesText) {
// display text of all the pages in the console
console.log(pagesText);
});
}, function (reason) {
console.log('Error! '+reason);
});
function getPageText(pageNum, PDFDocumentInstance) {
return new Promise(function (resolve, reject) {
PDFDocumentInstance.getPage(pageNum).then(function(pdfPage) {
pdfPage.getTextContent().then(function(textContent) { // should stopAtErrors somehow be passed here to getTextContent instead of to getDocument??
var textItems = textContent.items;
var finalString = '';
for (var i = 0; i < textItems.length; i++) {
var item = textItems[i];
finalString += item.str + " ";
}
resolve(finalString);
});
});
}).catch(function(err) {
console.log('Error! '+err);
});
}
One thing I am wondering is if the stopAtErrors parameter should somehow instead be passed to getTextContent? I have not found any examples illustrating the use of stopAtErrors and the PDFJS documentation does not show a working example, either. Given that I am still at the stage of needing examples to get PDFJS to function, I am at a loss as to how to make PDFJS stop trying to parse a problem PDF file and just move on to the next one.
Parse.com and JavaScript SDK.
I'm sure its simple, but I cannot seem to understand of fix the uncaught type error shown below. Why is this happening?
var Events = Parse.Object.extend("event");
var query = new Parse.Query(Events);
query.include("parent");
query.find({
success: function(events) {
for (var i = 0; i < events.length; i++) {
var event = events[i];
var zone = event.get("parent");
console.log(zone.get("name"));
}
},
error: function(error) {
alert(error);
}
});
if i'm correct - include stop working. I have cloud code which worked two days ago and not working today. I used include in query, and it returns 'undefined' value today.
The issue was that the class "zone" did not have a "name" field" so console.log returned "undefined".