| 1 |
//------------------------------------------------------------------------------ |
| 2 |
// name: poem-spew.ck |
| 3 |
// desc: yet another sus poem made with chAI and word2vec |
| 4 |
// |
| 5 |
// version: need chuck version 1.5.0.0 or higher |
| 6 |
// sorting: part of ChAI (ChucK for AI) |
| 7 |
// |
| 8 |
// "Spew" |
| 9 |
// -- yet another stream of unconsciousness poem generator |
| 10 |
// |
| 11 |
// NOTE: need a pre-trained word vector model, e.g., from |
| 12 |
// https://chuck.stanford.edu/chai/data/glove/ |
| 13 |
// glove-wiki-gigaword-50-tsne-2.txt (400000 words x 2 dimensions) |
| 14 |
// |
| 15 |
// author: Ge Wang |
| 16 |
// date: Spring 2023 |
| 17 |
//------------------------------------------------------------------------------ |
| 18 |
|
| 19 |
// default starting word |
| 20 |
"love" => string STARTING_WORD; |
| 21 |
// check arguments, set new starting word if present |
| 22 |
if( me.args() > 0 ) me.arg(0) => STARTING_WORD; |
| 23 |
|
| 24 |
// random seed (if set, sequence can be reproduce) |
| 25 |
// Math.srandom( 515 ); |
| 26 |
|
| 27 |
// instantiate |
| 28 |
Word2Vec model; |
| 29 |
// pre-trained model to load |
| 30 |
me.dir() + "glove-wiki-gigaword-50-tsne-2.txt" => string filepath; |
| 31 |
// load a pre-trained model (see URLs above for download) |
| 32 |
// this could take a few seconds, depending on model size |
| 33 |
if( !model.load( filepath ) ) |
| 34 |
{ |
| 35 |
<<< "cannot load model:", filepath >>>; |
| 36 |
me.exit(); |
| 37 |
} |
| 38 |
|
| 39 |
// conditions |
| 40 |
2500 => int TOTAL_WORDS; // to spew |
| 41 |
1000 => int K_NEAREST; // number of nearest words to retrieve for each word |
| 42 |
|
| 43 |
// timing |
| 44 |
40::ms => dur T_WORD; // duration per word |
| 45 |
40::ms => dur T_LINE_PAUSE; // a little pause after each line |
| 46 |
|
| 47 |
// ranges for each dimension (for sound mapping) |
| 48 |
float mins[0], maxs[0]; |
| 49 |
// get bounds for each dimension |
| 50 |
model.minMax( mins, maxs ); |
| 51 |
|
| 52 |
// sound |
| 53 |
ModalBar bar => NRev reverb => dac; |
| 54 |
// reverb wet/dry mix |
| 55 |
.1 => reverb.mix; |
| 56 |
// which preset |
| 57 |
7 => bar.preset; |
| 58 |
|
| 59 |
// current word |
| 60 |
STARTING_WORD => string word; |
| 61 |
// word vector |
| 62 |
float vec[model.dim()]; |
| 63 |
// similarity search results from word2vec |
| 64 |
string words[K_NEAREST]; |
| 65 |
// total number of words spewed |
| 66 |
0 => int spew; |
| 67 |
// words to go on current line |
| 68 |
0 => int lineWords; |
| 69 |
|
| 70 |
// go |
| 71 |
while( spew < TOTAL_WORDS ) |
| 72 |
{ |
| 73 |
if( lineWords <= 0 ) |
| 74 |
{ |
| 75 |
// line break |
| 76 |
chout <= IO.newline(); chout.flush(); |
| 77 |
// generate how many words in next line |
| 78 |
Math.random2( 5, 12 ) => lineWords; |
| 79 |
// a little pause after each line |
| 80 |
25::ms => dur T_LINE_PAUSE; |
| 81 |
} |
| 82 |
|
| 83 |
// print the word, with a space |
| 84 |
chout <= word <= " "; chout.flush(); |
| 85 |
// get word vector |
| 86 |
model.getVector( word, vec ); |
| 87 |
// sonify from word vector (see mapping possibilities of ModalBar) |
| 88 |
// https://chuck.stanford.edu/doc/program/ugen_full.html#ModalBar |
| 89 |
// feel free to experiement with what parameters to control with the vector |
| 90 |
Math.remap( vec[0], mins[0], maxs[0], 24, 96 ) => Std.mtof => bar.freq; |
| 91 |
// use pow to expand/compress the dimensions to affect mapping sensitivity |
| 92 |
Math.pow(Math.remap( vec[1], mins[1], maxs[1], 0, 1), .72) => bar.stickHardness; |
| 93 |
// ding! |
| 94 |
Math.random2f(.5,1) => bar.noteOn; |
| 95 |
// let time pass, let sound...sound |
| 96 |
T_WORD => now; |
| 97 |
// get similar words |
| 98 |
model.getSimilar( word, words.size(), words ); |
| 99 |
// choose one of the farthest one |
| 100 |
words[Math.random2((words.size()-1*.8)$int,words.size()-1)] => word; |
| 101 |
|
| 102 |
// count |
| 103 |
spew++; |
| 104 |
// count line |
| 105 |
lineWords--; |
| 106 |
} |
| 107 |
|
| 108 |
// print at the end |
| 109 |
chout <= "\"Spew\"" <= IO.newline(); |
| 110 |
chout <= "-- another stream of unconsciousness poem" <= IO.newline(); |
| 111 |
chout.flush(); |