Skip to content

Commit

Permalink
indexing is working.
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-calderwood committed Mar 4, 2024
1 parent bc6e6d1 commit aec04af
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 82 deletions.
46 changes: 30 additions & 16 deletions instrument/js/noteToWord.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,18 @@ function callBERT(leftwords, rightwords, badwords) {
class Worder {
constructor() {
this.context = [];
this._nextWordID = 0;
}

/*
* This function is for the Worder mixin to implement. It should take a note and return a word.
*/
_noteToWordProtocol(note) {
return note.ToString();
}

addWordToContext(word) {
this.context.push(word);
this.context.push(word.word); // TODO this isn't going to be indexing correctly
}

updateContext(newContext) {
Expand All @@ -51,8 +59,20 @@ class Worder {
}
}

formatWord(word) {
return {
id: this.nextID(),
word: word,
}
}

noteToWord(note) {
return note.ToString();
let word = this._noteToWordProtocol(note)
return this.formatWord(word);
}

nextID() {
return this._nextWordID++;
}
}

Expand All @@ -67,7 +87,7 @@ let indexWorderMixin = {

},

noteToWord(note) {
_noteToWordProtocol(note) {
return this.noteToWordByIndex(note);
}
}
Expand All @@ -83,20 +103,14 @@ let bertWorderMixin = {
});
},

noteToWord(note) {
_noteToWordProtocol(note) {
return this.noteToWordByBert(note);
}
}

let llamaWorderMixin = {
// curl http://localhost:11434/api/generate -d '{
// "model": "llama2",
// "prompt": "Why is the sky blue?",
// "options": {
// "num_ctx": 4096
// }
// }'
noteToWordByLlama(note) {
console.log('using llama')
let context = this.context.join(' ');
// console.log('Context: ', context);
return fetch('http://localhost:11434/api/generate', {
Expand Down Expand Up @@ -130,13 +144,13 @@ let llamaWorderMixin = {
words.splice(lastSpace, words.length - lastSpace);
words = words.join('');
const parsed = parseText(words);
console.log("Ollama:", parsed);
return parsed;
const final = parsed.map(this.formatWord.bind(this))
console.log("Ollama:", final);
return final;
})
},

noteToWord(note) {
_noteToWordProtocol(note) {
return this.noteToWordByLlama(note);
}
}

}
51 changes: 31 additions & 20 deletions instrument/js/wiggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,12 @@ function realizationEditCallback(value) {
realization.edit(value);
}

function performWord(word, options=undefined) {
// send the word to the performance tab
function performWord(word) {
if (performance) {
performance.postMessage({data:
performance.postMessage({data:
{
type: "addWord",
word: word,
options: options
}
}, "*")
}
Expand All @@ -221,6 +219,21 @@ function performWord(word, options=undefined) {
realization.update();
}

window.addEventListener("message", (event) => {
// Always validate the origin of the message!
if (event.origin !== expectedOrigin) {
console.log("Message received from unexpected origin: ", event.origin, "which does not match expected origin of ", expectedOrigin);
return;
}

let data = event.data.data;

if (data && data.type === "addWord") {
let id = data.id;
console.log("id", id);
}
});

function updatePerformanceWord(word) {
// send the word to the performance tab
if (performance) {
Expand Down Expand Up @@ -436,26 +449,25 @@ class Track {

this.basicResetNote();

let text = worder.noteToWord(note);
// if text is a promise, await it
let word = worder.noteToWord(note);
if (text instanceof Promise) {
this.text = await text;
} else {
this.text = text;
let word = await word;
}
let word = this.text;
this.text = word.word;

performWord(word); // send the word to the performance tab
worder.addWordToContext(word);

console.log("n nod", word);

let additional = worder.noteToWordByLlama(note);
let aiText = await additional;
for(let word of aiText) {
let options = {ai: true};
performWord(word, options=options);
worder.addWordToContext(word);
let promisedWord = worder.noteToWordByLlama(note) // worder.noteToWordByLlama(note);
let aiText = await promisedWord;
console.log("aiText", aiText);
let prevID = word.id;
for(let aiWord of aiText) {
aiWord.ai = true;
aiWord.after = prevID; // tell the performer to put it after the original word
performWord(aiWord);
worder.addWordToContext(aiWord);
prevID = aiWord.id;
}

realization.update();
Expand All @@ -468,7 +480,6 @@ class Track {
if (this.i == 0) {
realization.newLine();
}

}

hardResetNote() {
Expand Down
102 changes: 56 additions & 46 deletions instrument/js/wiggle_performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,42 @@ function setup() {
class TextPerformer {
constructor() {
this.words = {}; // {id: {word: "word", x: 0, y: 0, size: 0, wordWidth: 0}}
this.currentX = 0; // Keep track of the current x position for the next word
this.currentY = 0; // Keep track of the current y position for the next word
this.lineHeight = 0; // Keep track of the tallest word in the current line
this.wordOrder = []; // [id, id, id]
this.lineHeight = 20; // Keep track of the tallest word in the current line
}

draw() {
for (let word of Object.values(this.words)) {

background(0)

let currentX = 0;
let currentY = 0;

for (let wordID of this.wordOrder) {
let word = this.words[wordID];
textSize(word.size);

if (currentX + word.width > windowWidth) {
currentX = 0;
currentY += this.lineHeight;
}
if (currentY + word.size > windowHeight) {
textColor = [random(255), random(255), random(255)];
backgroundColor = [255 - textColor[0], 255 - textColor[1], 255 - textColor[2]];
currentX = 0;
currentY = 0;
}

word.x = currentX;
word.y = currentY;

fill(word.backgroundColor[0], word.backgroundColor[1], word.backgroundColor[2]); // set the color to the word's background color
rect(word.x, word.y, word.x + word.wordWidth, word.y + word.size); // draw the background
fill(word.color[0], word.color[1], word.color[2]); // set the color to the word's color
text(word.word, word.x, word.y + word.size); // draw the word

currentX += word.width + textWidth(" ");

}
}

Expand All @@ -37,59 +61,46 @@ class TextPerformer {
this.words[id].wordWidth = textWidth(word);
}

addWord(word, options) {
let id = this.nextID();
// let size = random(10, 30);
let size = 20;
addWord(word) {
let id = word.id;
let size = this.lineHeight;
textSize(size);
let wordWidth = textWidth(word);


if (this.currentX + wordWidth > windowWidth) {
// Move to the next line
this.currentX = 0;
this.currentY += this.lineHeight;
this.lineHeight = 0;
}
if (this.currentY + size > windowHeight) {
textColor = [random(255), random(255), random(255)];
backgroundColor = [255 - textColor[0], 255 - textColor[1], 255 - textColor[2]];
this.currentX = 0;
this.currentY = 0;
this.lineHeight = 0;
}

let wordWidth = textWidth(word.word);

let wordColor = textColor;
let wordBackgroundColor = backgroundColor;
let aiColor = [200, 0, 0];

this.words[id] = {
word: word,
x: this.currentX,
y: this.currentY,
// add performance data to the word
this.words[id] = {...word, ...{
size: size,
wordWidth: wordWidth,
color: options && options.ai ? aiColor : wordColor,
width: wordWidth,
color: word.ai ? aiColor : wordColor,
backgroundColor: wordBackgroundColor,
};

this.currentX += wordWidth + textWidth(" ");
this.lineHeight = max(this.lineHeight, size);
}};

if (word.after) {
console.log("Adding word after", word);
let index = this.wordOrder.indexOf(word.after);
console.log("Index of after word", index);
if (index === -1) {
this.wordOrder.unshift(id);
} else {
this.wordOrder.splice(index + 1, 0, id);
}

delete word.after;

} else {
this.wordOrder.push(id);
}

return id;
}

deleteWord(id) {
delete this.words[id];
}

nextID() {
let id = 0;
while (this.words[id] !== undefined) {
id++;
}
return id;
}
}


Expand All @@ -103,10 +114,9 @@ window.addEventListener("message", (event) => {
}

let data = event.data.data;

if (data && data.type === "addWord") {
let id = performer.addWord(data.word, data.options);
window.parent.postMessage({type: "addWord", id: id, status: "success"}, expectedOrigin);
performer.addWord(data.word);
window.parent.postMessage({type: "addWord", id: data.word.id, status: "success"}, expectedOrigin);
}
});

Expand Down

0 comments on commit aec04af

Please sign in to comment.