Debian Codegraph @

examples/ai/word2vec/poem-randomwalk.ck

raw
1 //------------------------------------------------------------------------------
2 // name: poem-randomwalk.ck
3 // desc: 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 // "Random Walk"
9 // -- 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 7 => int WORDS_PER_LINE;
41 4 => int LINES_PER_STANZA;
42 3 => int NUM_STANZAS;
43
44 // number of nearest words to retrieve for each word
45 // higher this number the higher the variance per word
46 100 => int K_NEAREST;
47
48 // timing
49 200::ms => dur T_WORD; // duration per word
50 false => int shouldScaleTimeToWordLength; // longer words take longer?
51 250::ms => dur T_LINE_PAUSE; // a little pause after each line
52 750::ms => dur T_STANZA_PAUSE; // pause after each stanza
53
54 // ranges for each dimension (for sound mapping)
55 float mins[0], maxs[0];
56 // get bounds for each dimension
57 model.minMax( mins, maxs );
58
59 // sound
60 ModalBar bar => NRev reverb => dac;
61 // reverb wet/dry mix
62 .1 => reverb.mix;
63 // which preset
64 7 => bar.preset;
65
66 // current word
67 STARTING_WORD => string word;
68 // word vector
69 float vec[model.dim()];
70 // similarity search results from word2vec
71 string words[K_NEAREST];
72 // used for scaling up word timing to word length
73 dur addTime;
74
75 // line break
76 chout <= IO.newline(); chout.flush();
77
78 // loop over stanzas
79 for( int s; s < NUM_STANZAS; s++ )
80 {
81 // loop over lines in a stanza
82 for( int n; n < LINES_PER_STANZA; n++ )
83 {
84 // loop over words
85 for( int w; w < WORDS_PER_LINE; w++ )
86 {
87 // print the word, with a space
88 chout <= word <= " "; chout.flush();
89 // get word vector (for sonification)
90 model.getVector( word, vec );
91 // sonify from word vector (see mapping possibilities of ModalBar)
92 // https://chuck.stanford.edu/doc/program/ugen_full.html#ModalBar
93 // feel free to experiement with what parameters to control with the vector
94 Math.remap( vec[0], mins[0], maxs[0], 24, 84 ) => Std.mtof => bar.freq;
95 // use pow to expand/compress the dimensions to affect mapping sensitivity
96 Math.pow(Math.remap( vec[1], mins[1], maxs[1],0,1), 1.1) => bar.stickHardness;
97 // ding!
98 Math.random2f(.5,1) => bar.noteOn;
99 // if scale time with word length
100 if( shouldScaleTimeToWordLength )
101 word.length() * T_WORD/20 => addTime;
102 // let time pass, let sound...sound
103 T_WORD + addTime => now;
104 // get similar words
105 model.getSimilar( word, words.size(), words );
106 // choose one at random
107 words[Math.random2(0,words.size()-1)] => word;
108 }
109 // next line!
110 chout <= IO.newline(); chout.flush();
111 // pause at end of line
112 T_LINE_PAUSE => now;
113 }
114
115 // stanza break!
116 chout <= IO.newline(); chout.flush();
117 // pause at end of line
118 T_STANZA_PAUSE => now;
119 }
120
121 // print at the end
122 chout <= "\"Random Walk\"" <= IO.newline();
123 chout <= "-- a stream of unconsciousness poem" <= IO.newline();
124 chout.flush();