Skip to content

Instantly share code, notes, and snippets.

@artemave
Created February 27, 2014 21:25
Show Gist options
  • Save artemave/9259882 to your computer and use it in GitHub Desktop.
Save artemave/9259882 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>text to music</title>
<script type="text/javascript" charset="utf-8" src="./zepto.min.js"></script>
</head>
<body>
<textarea name="" id= rows="40" cols="80"></textarea>
<button type="submit">Muse</button>
<script type="text/javascript" charset="utf-8">
String.prototype.hashCode = function(){
var hash = 0, i, char;
if (this.length == 0) return hash;
for (i = 0, l = this.length; i < l; i++) {
char = this.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
function play(notes) {
$.each(notes, function(i, note) {
console.log(note);
})
}
// var metadata = {
// 'I': 34,
// 'am': 20,
// 'will': 19,
// 'be': 19,
// 'not': 18,
// 'at': 10,
// 'for': 9,
// 'by': 8,
// 'come': 3,
// 'do': 3,
// 'sfuff': 2,
// 'book': 1
// }
function wordFrequencies(text) {
var words = text.split(/ +/);
var meta = {}
words.forEach(function(word) {
if (meta.hasOwnProperty(word)) {
meta.word++;
}
else {
meta.word = 1;
}
});
return meta;
}
function textToNotes(text) {
var notes = [];
var metadata = wordFrequencies(text);
notes = $.map(words, function(word) {
return word.hashCode() % 6;
});
return notes;
}
function muse(text, callback) {
var notes = textToNotes(text);
console.log(notes)
play(notes);
callback();
}
$(function() {
$('button').click(function(e) {
var button = $(this).get(0);
var text = $('textarea').val();
button.disabled = true;
muse(text, function(e) {
button.disabled = false;
})
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment