transpositions of coda now done

dev
mwinter 9 months ago
parent 84fa3ebefd
commit 3fe059ad91

@ -11,3 +11,4 @@
\include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/4dd2a130/lilypond/part_I.ly" \include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/4dd2a130/lilypond/part_I.ly"
\include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/767e70f0/lilypond/part_I.ly" \include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/767e70f0/lilypond/part_I.ly"
\include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/536cac90/lilypond/part_I.ly" \include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/536cac90/lilypond/part_I.ly"
\include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/5ededd80/lilypond/part_I.ly"

@ -11,3 +11,4 @@
\include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/4dd2a130/lilypond/part_II.ly" \include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/4dd2a130/lilypond/part_II.ly"
\include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/767e70f0/lilypond/part_II.ly" \include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/767e70f0/lilypond/part_II.ly"
\include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/536cac90/lilypond/part_II.ly" \include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/536cac90/lilypond/part_II.ly"
\include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/5ededd80/lilypond/part_II.ly"

@ -11,3 +11,4 @@
\include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/4dd2a130/lilypond/part_III.ly" \include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/4dd2a130/lilypond/part_III.ly"
\include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/767e70f0/lilypond/part_III.ly" \include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/767e70f0/lilypond/part_III.ly"
\include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/536cac90/lilypond/part_III.ly" \include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/536cac90/lilypond/part_III.ly"
\include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/5ededd80/lilypond/part_III.ly"

@ -11,3 +11,4 @@
\include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/4dd2a130/lilypond/part_IV.ly" \include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/4dd2a130/lilypond/part_IV.ly"
\include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/767e70f0/lilypond/part_IV.ly" \include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/767e70f0/lilypond/part_IV.ly"
\include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/536cac90/lilypond/part_IV.ly" \include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/536cac90/lilypond/part_IV.ly"
\include "/home/mwinter/Sketches/seeds_and_ledgers/source/resources/string_quartet_3_rise_coda_only/5ededd80/lilypond/part_IV.ly"

Binary file not shown.

@ -30,8 +30,8 @@
print-first-page-number = ##t print-first-page-number = ##t
print-page-number = ##t print-page-number = ##t
oddHeaderMarkup = \markup { \fill-line { \line { \unless \on-first-page {\pad-markup #2 { \concat {\italic {"test"}}}}}}} oddHeaderMarkup = \markup { \fill-line { \line { \unless \on-first-page {\pad-markup #2 { \concat {\italic {"seeds and ledges 3"}}}}}}}
evenHeaderMarkup = \markup { \fill-line { \line { \unless \on-first-page {\pad-markup #2 { \concat {\italic {"test"}}}}}}} evenHeaderMarkup = \markup { \fill-line { \line { \unless \on-first-page {\pad-markup #2 { \concat {\italic {"seeds and ledges 3"}}}}}}}
oddFooterMarkup = \markup { \fill-line { oddFooterMarkup = \markup { \fill-line {
\concat { \concat {
"-" "-"

@ -1,54 +1,56 @@
{ {
{ r4 r8.[ c'16^\markup { \pad-markup #0.2 "+0"}] ~ c'2 ~ } { r4 r8.[ d'16^\markup { \pad-markup #0.2 "+0"}] ~ d'2 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 } { d'1 }
\bar "|" \bar "|"
{ b1^\markup { \pad-markup #0.2 "+47"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 11↓" }} ~ } { cis'1^\markup { \pad-markup #0.2 "+47"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 11↓" }} ~ }
\bar "|" \bar "|"
{ b1 ~ } { cis'1 ~ }
\bar "|" \bar "|"
{ b4 ~ b8[ ais8^\markup { \pad-markup #0.2 "+35"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 5↑" }}] ~ ais8.[ a16^\markup { \pad-markup #0.2 "-20"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 7↓" }}] ~ a4 ~ } { cis'4 ~ cis'8[ c'8^\markup { \pad-markup #0.2 "+35"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 5↑" }}] ~ c'8.[ b16^\markup { \pad-markup #0.2 "-20"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 7↓" }}] ~ b4 ~ }
\bar "|" \bar "|"
{ a1 ~ } { b1 ~ }
\bar "|" \bar "|"
{ a1 ~ } { b1 ~ }
\bar "|"
{ b1 ~ }
\bar "|" \bar "|"
{ a1 ~ } { b16[ b8.^\markup { \pad-markup #0.2 "+16"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 7↑" }}] ~ b2. }
\bar "|" \bar "|"
{ a16[ a8.^\markup { \pad-markup #0.2 "+16"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 7↑" }}] ~ a2. } { ais1^\markup { \pad-markup #0.2 "-13"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 13↑" }} ~ }
\bar "|" \bar "|"
{ gis1^\markup { \pad-markup #0.2 "-13"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 13↑" }} ~ } { ais8.[ ais16^\markup { \pad-markup #0.2 "-40"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 5↓" }}] ~ ais2. ~ }
\bar "|" \bar "|"
{ gis8.[ gis16^\markup { \pad-markup #0.2 "-40"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 5↓" }}] ~ gis2. ~ } { ais4 ~ ais8[ g8^\markup { \pad-markup #0.2 "-2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 1↑" }}] ~ g4 gis4^\markup { \pad-markup #0.2 "-5"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 11↓" }} ~ }
\bar "|" \bar "|"
{ gis4 ~ gis8[ f8^\markup { \pad-markup #0.2 "-2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 1↑" }}] ~ f4 fis4^\markup { \pad-markup #0.2 "-5"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 11↓" }} ~ } { gis2. ~ gis8[ ais8^\markup { \pad-markup #0.2 "-13"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 13↑" }}] ~ }
\bar "|" \bar "|"
{ fis2. ~ fis8[ gis8^\markup { \pad-markup #0.2 "-13"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 13↑" }}] ~ } { ais1 ~ }
\bar "|" \bar "|"
{ gis1 ~ } { ais1 ~ }
\bar "|" \bar "|"
{ gis1 ~ } { ais1 }
\bar "|" \bar "|"
{ gis1} { r1 }
\bar "||" \bar "||"
} }

@ -13,27 +13,29 @@
\bar "|" \bar "|"
{ r1 } { r1 }
\bar "|" \bar "|"
{ r16[ ais8.^\markup { \pad-markup #0.2 "-31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 7↑" }}] ~ ais2. ~ } { r16[ c'8.^\markup { \pad-markup #0.2 "-31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 7↑" }}] ~ c'2. ~ }
\bar "|" \bar "|"
{ ais2. gis4^\markup { \pad-markup #0.2 "+14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 5↓" }} ~ } { c'2. ais4^\markup { \pad-markup #0.2 "+14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 5↓" }} ~ }
\bar "|" \bar "|"
{ gis16[ fis8.^\markup { \pad-markup #0.2 "+49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 11↓" }}] ~ fis2. ~ } { ais16[ gis8.^\markup { \pad-markup #0.2 "+49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 11↓" }}] ~ gis2. ~ }
\bar "|" \bar "|"
{ fis1 ~ } { gis1 ~ }
\bar "|" \bar "|"
{ fis1 ~ } { gis1 ~ }
\bar "|" \bar "|"
{ fis1 ~ } { gis1 ~ }
\bar "|" \bar "|"
{ fis1 ~ } { gis1 ~ }
\bar "|" \bar "|"
{ fis1 ~ } { gis1 ~ }
\bar "|" \bar "|"
{ fis1 ~ } { gis1 ~ }
\bar "|" \bar "|"
{ fis1 ~ } { gis1 ~ }
\bar "|" \bar "|"
{ fis2 ~ fis8.[ r16] r4 } { gis2 ~ gis8.[ r16] r4 }
\bar "|"
{ r1 }
\bar "|" \bar "|"
{ r1 } { r1 }
\bar "|" \bar "|"

@ -1,15 +1,15 @@
{ {
{ r2. e'4^\markup { \pad-markup #0.2 "-41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 13↓" }} ~ } { r2. fis'4^\markup { \pad-markup #0.2 "-41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 13↓" }} ~ }
\bar "|" \bar "|"
{ e'2 e'4^\markup { \pad-markup #0.2 "-14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 5↑" }} ~ e'8.[ f'16^\markup { \pad-markup #0.2 "-2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 3↓" }}] ~ } { fis'2 fis'4^\markup { \pad-markup #0.2 "-14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 5↑" }} ~ fis'8.[ g'16^\markup { \pad-markup #0.2 "-2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 3↓" }}] ~ }
\bar "|" \bar "|"
{ f'4 ~ f'8[ g'8^\markup { \pad-markup #0.2 "+2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 3↑" }}] ~ g'4 ~ g'8[ ais'8^\markup { \pad-markup #0.2 "-31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 7↑" }}] ~ } { g'4 ~ g'8[ a'8^\markup { \pad-markup #0.2 "+2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 3↑" }}] ~ a'4 ~ a'8[ c''8^\markup { \pad-markup #0.2 "-31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 7↑" }}] ~ }
\bar "|" \bar "|"
{ ais'4 ~ ais'8[ c''8^\markup { \pad-markup #0.2 "+0"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 1↑" }}] ~ c''2 } { c''4 ~ c''8[ d''8^\markup { \pad-markup #0.2 "+0"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 1↑" }}] ~ d''2 }
\bar "|" \bar "|"
{ gis'4^\markup { \pad-markup #0.2 "+41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 13↑" }} ~ gis'8.[ gis'16^\markup { \pad-markup #0.2 "+14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 5↓" }}] ~ gis'4 ~ gis'8[ f'8^\markup { \pad-markup #0.2 "-2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 3↓" }}] ~ } { ais'4^\markup { \pad-markup #0.2 "+41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 13↑" }} ~ ais'8.[ ais'16^\markup { \pad-markup #0.2 "+14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 5↓" }}] ~ ais'4 ~ ais'8[ g'8^\markup { \pad-markup #0.2 "-2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 3↓" }}] ~ }
\bar "|" \bar "|"
{ f'2 e'4^\markup { \pad-markup #0.2 "-14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 5↑" }} ~ e'8[ r8] } { g'2 fis'4^\markup { \pad-markup #0.2 "-14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 5↑" }} ~ fis'8[ r8] }
\bar "|" \bar "|"
{ r1 } { r1 }
\bar "|" \bar "|"
@ -21,33 +21,35 @@
\bar "|" \bar "|"
{ r1 } { r1 }
\bar "|" \bar "|"
{ r2 r16[ dis'8.^\markup { \pad-markup #0.2 "-38"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 5↓" }}] ~ dis'4 ~ } { r2 r16[ f'8.^\markup { \pad-markup #0.2 "-38"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 5↓" }}] ~ f'4 ~ }
\bar "|" \bar "|"
{ dis'1 ~ } { f'1 ~ }
\bar "|" \bar "|"
{ dis'8[ dis'8^\markup { \pad-markup #0.2 "-11"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 13↑" }}] ~ dis'2 d'4^\markup { \pad-markup #0.2 "-49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 3↑" }} ~ } { f'8[ f'8^\markup { \pad-markup #0.2 "-11"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 13↑" }}] ~ f'2 e'4^\markup { \pad-markup #0.2 "-49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 3↑" }} ~ }
\bar "|" \bar "|"
{ d'1 ~ } { e'1 ~ }
\bar "|" \bar "|"
{ d'4 ~ d'8[ ais8^\markup { \pad-markup #0.2 "+35"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 5↑" }}] ~ ais2 ~ } { e'4 ~ e'8[ c'8^\markup { \pad-markup #0.2 "+35"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 5↑" }}] ~ c'2 ~ }
\bar "|" \bar "|"
{ ais1 ~ } { c'1 ~ }
\bar "|" \bar "|"
{ ais8.[ b16^\markup { \pad-markup #0.2 "+47"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 3↓" }}] ~ b2. ~ } { c'8.[ cis'16^\markup { \pad-markup #0.2 "+47"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 3↓" }}] ~ cis'2. ~ }
\bar "|" \bar "|"
{ b1 ~ } { cis'1 ~ }
\bar "|" \bar "|"
{ b1 ~ } { cis'1 ~ }
\bar "|" \bar "|"
{ b1 ~ } { cis'1 ~ }
\bar "|" \bar "|"
{ b1 ~ } { cis'1 ~ }
\bar "|" \bar "|"
{ b1 ~ } { cis'1 ~ }
\bar "|" \bar "|"
{ b1 ~ } { cis'1 ~ }
\bar "|" \bar "|"
{ b2 ~ b16[ r8.] r4 } { cis'2 ~ cis'16[ r8.] r4 }
\bar "|"
{ r1 }
\bar "|" \bar "|"
{ r1 } { r1 }
\bar "||" \bar "||"

@ -1,54 +1,56 @@
{ {
{ c'1^\markup { \pad-markup #0.2 "+0"} ~ } { d'1^\markup { \pad-markup #0.2 "+0"} ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'4 ~ c'16[ e'8.^\markup { \pad-markup #0.2 "-41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 13↓" }}] ~ e'2 ~ } { d'4 ~ d'16[ fis'8.^\markup { \pad-markup #0.2 "-41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 13↓" }}] ~ fis'2 ~ }
\bar "|" \bar "|"
{ e'2 ~ e'8.[ g'16^\markup { \pad-markup #0.2 "+2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 3↑" }}] ~ g'4 ~ } { fis'2 ~ fis'8.[ a'16^\markup { \pad-markup #0.2 "+2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 3↑" }}] ~ a'4 ~ }
\bar "|" \bar "|"
{ g'4 ~ g'8[ ais'8^\markup { \pad-markup #0.2 "-31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 1↑" }}] ~ ais'2 ~ } { a'4 ~ a'8[ c''8^\markup { \pad-markup #0.2 "-31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 1↑" }}] ~ c''2 ~ }
\bar "|" \bar "|"
{ ais'4 ~ ais'8.[ gis'16^\markup { \pad-markup #0.2 "+41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 13↑" }}] ~ gis'2 } { c''4 ~ c''8.[ ais'16^\markup { \pad-markup #0.2 "+41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 13↑" }}] ~ ais'2 }
\bar "|" \bar "|"
{ gis'2^\markup { \pad-markup #0.2 "+14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 5↓" }} ~ gis'8[ g'8^\markup { \pad-markup #0.2 "+2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 3↑" }}] ~ g'4 ~ } { ais'2^\markup { \pad-markup #0.2 "+14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 5↓" }} ~ ais'8[ a'8^\markup { \pad-markup #0.2 "+2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 3↑" }}] ~ a'4 ~ }
\bar "|" \bar "|"
{ g'8[ f'8^\markup { \pad-markup #0.2 "-2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 3↓" }}] ~ f'2. ~ } { a'8[ g'8^\markup { \pad-markup #0.2 "-2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 3↓" }}] ~ g'2. ~ }
\bar "|" \bar "|"
{ f'4 ~ f'8[ e'8^\markup { \pad-markup #0.2 "+18"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↑" }}] ~ e'2 ~ } { g'4 ~ g'8[ fis'8^\markup { \pad-markup #0.2 "+18"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↑" }}] ~ fis'2 ~ }
\bar "|" \bar "|"
{ e'1 ~ } { fis'1 ~ }
\bar "|" \bar "|"
{ e'16[ fis'8.^\markup { \pad-markup #0.2 "+49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 1↑" }}] ~ fis'2. ~ } { fis'16[ gis'8.^\markup { \pad-markup #0.2 "+49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 1↑" }}] ~ gis'2. ~ }
\bar "|" \bar "|"
{ fis'1 ~ } { gis'1 ~ }
\bar "|" \bar "|"
{ fis'16[ a'8.^\markup { \pad-markup #0.2 "-20"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 1↑" }}] ~ a'2 ~ a'16[ ais'8.^\markup { \pad-markup #0.2 "+8"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 13↓" }}] ~ } { gis'16[ b'8.^\markup { \pad-markup #0.2 "-20"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 1↑" }}] ~ b'2 ~ b'16[ c''8.^\markup { \pad-markup #0.2 "+8"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 13↓" }}] ~ }
\bar "|" \bar "|"
{ ais'1 ~ } { c''1 ~ }
\bar "|" \bar "|"
{ ais'2 ~ ais'16[ b'8.^\markup { \pad-markup #0.2 "+47"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 1↑" }}] ~ b'4 ~ } { c''2 ~ c''16[ cis''8.^\markup { \pad-markup #0.2 "+47"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 1↑" }}] ~ cis''4 ~ }
\bar "|" \bar "|"
{ b'2 ~ b'8[ a'8^\markup { \pad-markup #0.2 "+16"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 7↑" }}] ~ a'4 ~ } { cis''2 ~ cis''8[ b'8^\markup { \pad-markup #0.2 "+16"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 7↑" }}] ~ b'4 ~ }
\bar "|" \bar "|"
{ a'2 ~ a'8.[ fis'16^\markup { \pad-markup #0.2 "+49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 3↑" }}] ~ fis'4 ~ } { b'2 ~ b'8.[ gis'16^\markup { \pad-markup #0.2 "+49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 3↑" }}] ~ gis'4 ~ }
\bar "|" \bar "|"
{ fis'16[ f'8.^\markup { \pad-markup #0.2 "-2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 11↑" }}] ~ f'2. ~ } { gis'16[ g'8.^\markup { \pad-markup #0.2 "-2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 11↑" }}] ~ g'2. ~ }
\bar "|" \bar "|"
{ f'4 ~ f'8.[ e'16^\markup { \pad-markup #0.2 "+45"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 3↓" }}] ~ e'2 ~ } { g'4 ~ g'8.[ fis'16^\markup { \pad-markup #0.2 "+45"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 3↓" }}] ~ fis'2 ~ }
\bar "|" \bar "|"
{ e'1 ~ } { fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|" \bar "|"
{ e'1 ~ } { fis'4 ~ fis'16[ r8.] r2 }
\bar "|" \bar "|"
{ e'4 ~ e'16[ r8.] r2 } { r1 }
\bar "||" \bar "||"
} }

@ -1,38 +1,40 @@
{ {
{ r2. r8[ e'8^\markup { \pad-markup #0.2 "-14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 5↑" }}] ~ } { r2. r8[ fis'8^\markup { \pad-markup #0.2 "-14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 5↑" }}] ~ }
\bar "|" \bar "|"
{ e'1 ~ } { fis'1 ~ }
\bar "|" \bar "|"
{ e'4 ~ e'8[ f'8^\markup { \pad-markup #0.2 "-2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 3↓" }}] ~ f'4 ~ f'16[ e'8.^\markup { \pad-markup #0.2 "-41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 13↓" }}] ~ } { fis'4 ~ fis'8[ g'8^\markup { \pad-markup #0.2 "-2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 3↓" }}] ~ g'4 ~ g'16[ fis'8.^\markup { \pad-markup #0.2 "-41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 13↓" }}] ~ }
\bar "|" \bar "|"
{ e'1 ~ } { fis'1 ~ }
\bar "|" \bar "|"
{ e'2 d'2^\markup { \pad-markup #0.2 "+31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↓" }} ~ } { fis'2 e'2^\markup { \pad-markup #0.2 "+31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↓" }} ~ }
\bar "|" \bar "|"
{ d'1 ~ } { e'1 ~ }
\bar "|" \bar "|"
{ d'1 ~ } { e'1 ~ }
\bar "|" \bar "|"
{ d'1 ~ } { e'1 ~ }
\bar "|"
{ e'1 ~ }
\bar "|" \bar "|"
{ d'1 ~ } { e'1 ~ }
\bar "|" \bar "|"
{ d'1 ~ } { e'1 ~ }
\bar "|" \bar "|"
{ d'1 ~ } { e'2 r2 }
\bar "|" \bar "|"
{ d'2 r2 } { r8.[ e'16^\markup { \pad-markup #0.2 "+31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↓" }}] ~ e'2. ~ }
\bar "|" \bar "|"
{ r8.[ d'16^\markup { \pad-markup #0.2 "+31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↓" }}] ~ d'2. ~ } { e'1 ~ }
\bar "|" \bar "|"
{ d'1 ~ } { e'1 ~ }
\bar "|" \bar "|"
{ d'1 ~ } { e'1 ~ }
\bar "|" \bar "|"
{ d'1 ~ } { e'1 ~ }
\bar "|" \bar "|"
{ d'1 ~ } { e'1 ~ }
\bar "|" \bar "|"
{ d'1} { e'16[ r8.] r2. }
\bar "||" \bar "||"
} }

@ -1,38 +1,40 @@
{ {
{ c'1^\markup { \pad-markup #0.2 "+0"} ~ } { d'1^\markup { \pad-markup #0.2 "+0"} ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'2. ~ c'8.[ b16^\markup { \pad-markup #0.2 "-12"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 5↑" }}] ~ } { d'2. ~ d'8.[ cis'16^\markup { \pad-markup #0.2 "-12"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 5↑" }}] ~ }
\bar "|" \bar "|"
{ b4 b2.^\markup { \pad-markup #0.2 "-39"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 13↓" }} ~ } { cis'4 cis'2.^\markup { \pad-markup #0.2 "-39"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 13↓" }} ~ }
\bar "|" \bar "|"
{ b16[ cis'8.^\markup { \pad-markup #0.2 "-47"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 11↑" }}] ~ cis'4 ~ cis'8[ ais8^\markup { \pad-markup #0.2 "+45"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 5↓" }}] ~ ais4 ~ } { cis'16[ dis'8.^\markup { \pad-markup #0.2 "-47"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 11↑" }}] ~ dis'4 ~ dis'8[ c'8^\markup { \pad-markup #0.2 "+45"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 5↓" }}] ~ c'4 ~ }
\bar "|" \bar "|"
{ ais8[ a8^\markup { \pad-markup #0.2 "+33"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 7↓" }}] ~ a8[ b8^\markup { \pad-markup #0.2 "-28"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 13↑" }}] ~ b2 ~ } { c'8[ b8^\markup { \pad-markup #0.2 "+33"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 7↓" }}] ~ b8[ cis'8^\markup { \pad-markup #0.2 "-28"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 13↑" }}] ~ cis'2 ~ }
\bar "|" \bar "|"
{ b8.[ c'16^\markup { \pad-markup #0.2 "+0"}] ~ c'2 ~ c'8.[ b16^\markup { \pad-markup #0.2 "-39"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 13↓" }}] ~ } { cis'8.[ d'16^\markup { \pad-markup #0.2 "+0"}] ~ d'2 ~ d'8.[ cis'16^\markup { \pad-markup #0.2 "-39"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 13↓" }}] ~ }
\bar "|" \bar "|"
{ b1 ~ } { cis'1 ~ }
\bar "|" \bar "|"
{ b1 ~ } { cis'1 ~ }
\bar "|" \bar "|"
{ b2 c'2^\markup { \pad-markup #0.2 "+0"} ~ } { cis'2 d'2^\markup { \pad-markup #0.2 "+0"} ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'8.[ r16] r2. } { d'8.[ r16] r2. }
\bar "|"
{ r1 }
\bar "||" \bar "||"
} }

@ -1,38 +1,40 @@
{ {
{ r2. r8[ ais8^\markup { \pad-markup #0.2 "-31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↑" }}] ~ } { r2. r8[ c'8^\markup { \pad-markup #0.2 "-31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↑" }}] ~ }
\bar "|" \bar "|"
{ ais2 gis2^\markup { \pad-markup #0.2 "+14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 5↓" }} ~ } { c'2 ais2^\markup { \pad-markup #0.2 "+14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 5↓" }} ~ }
\bar "|" \bar "|"
{ gis4 ~ gis8[ g8^\markup { \pad-markup #0.2 "+2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 3↑" }}] ~ g2 ~ } { ais4 ~ ais8[ a8^\markup { \pad-markup #0.2 "+2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 3↑" }}] ~ a2 ~ }
\bar "|" \bar "|"
{ g1 ~ } { a1 ~ }
\bar "|" \bar "|"
{ g1 ~ } { a1 ~ }
\bar "|" \bar "|"
{ g1 ~ } { a1 ~ }
\bar "|" \bar "|"
{ g1 ~ } { a1 ~ }
\bar "|" \bar "|"
{ g1 ~ } { a1 ~ }
\bar "|" \bar "|"
{ g1 ~ } { a1 ~ }
\bar "|" \bar "|"
{ g8.[ g16^\markup { \pad-markup #0.2 "+29"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 3↓" }}] ~ g8.[ a16^\markup { \pad-markup #0.2 "+33"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 7↓" }}] ~ a2 ~ } { a8.[ a16^\markup { \pad-markup #0.2 "+29"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 3↓" }}] ~ a8.[ b16^\markup { \pad-markup #0.2 "+33"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 7↓" }}] ~ b2 ~ }
\bar "|" \bar "|"
{ a8.[ gis16^\markup { \pad-markup #0.2 "+30"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↑" }}] ~ gis2 ~ gis8.[ a16^\markup { \pad-markup #0.2 "-20"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 11↓" }}] ~ } { b8.[ ais16^\markup { \pad-markup #0.2 "+30"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↑" }}] ~ ais2 ~ ais8.[ b16^\markup { \pad-markup #0.2 "-20"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 11↓" }}] ~ }
\bar "|" \bar "|"
{ a2 r2 } { b2 r2 }
\bar "|" \bar "|"
{ r1 } { r1 }
\bar "|" \bar "|"
{ r16[ gis8.^\markup { \pad-markup #0.2 "-18"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 11↑" }}] ~ gis2. } { r16[ ais8.^\markup { \pad-markup #0.2 "-18"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 11↑" }}] ~ ais2. }
\bar "|" \bar "|"
{ gis4^\markup { \pad-markup #0.2 "+41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 13↑" }} ~ gis8[ gis8^\markup { \pad-markup #0.2 "+14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 5↓" }}] ~ gis2 } { ais4^\markup { \pad-markup #0.2 "+41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 13↑" }} ~ ais8[ ais8^\markup { \pad-markup #0.2 "+14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 5↓" }}] ~ ais2 }
\bar "|" \bar "|"
{ g2^\markup { \pad-markup #0.2 "+2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 1↑" }} g2^\markup { \pad-markup #0.2 "+29"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 3↓" }} ~ } { a2^\markup { \pad-markup #0.2 "+2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 1↑" }} a2^\markup { \pad-markup #0.2 "+29"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 3↓" }} ~ }
\bar "|" \bar "|"
{ g8.[ a16^\markup { \pad-markup #0.2 "-20"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 11↓" }}] ~ a2. ~ } { a8.[ b16^\markup { \pad-markup #0.2 "-20"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 11↓" }}] ~ b2. ~ }
\bar "|" \bar "|"
{ a1} { b1 ~ }
\bar "|"
{ b16[ r8.] r2. }
\bar "||" \bar "||"
} }

@ -1,38 +1,40 @@
{ {
{ r2. r8[ gis8^\markup { \pad-markup #0.2 "+41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 13↑" }}] ~ } { r2. r8[ ais8^\markup { \pad-markup #0.2 "+41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 13↑" }}] ~ }
\bar "|" \bar "|"
{ gis1 ~ } { ais1 ~ }
\bar "|" \bar "|"
{ gis4 ~ gis8[ ais8^\markup { \pad-markup #0.2 "-31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↑" }}] ~ ais4 ~ ais16[ d'8.^\markup { \pad-markup #0.2 "+31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↓" }}] ~ } { ais4 ~ ais8[ c'8^\markup { \pad-markup #0.2 "-31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↑" }}] ~ c'4 ~ c'16[ e'8.^\markup { \pad-markup #0.2 "+31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↓" }}] ~ }
\bar "|" \bar "|"
{ d'2 ~ d'8[ f'8^\markup { \pad-markup #0.2 "-2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 3↓" }}] ~ f'4 ~ } { e'2 ~ e'8[ g'8^\markup { \pad-markup #0.2 "-2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 3↓" }}] ~ g'4 ~ }
\bar "|" \bar "|"
{ f'2 g'2^\markup { \pad-markup #0.2 "+2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 1↑" }} ~ } { g'2 a'2^\markup { \pad-markup #0.2 "+2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 1↑" }} ~ }
\bar "|" \bar "|"
{ g'1 ~ } { a'1 ~ }
\bar "|" \bar "|"
{ g'1 ~ } { a'1 ~ }
\bar "|" \bar "|"
{ g'1 ~ } { a'1 ~ }
\bar "|" \bar "|"
{ g'1 ~ } { a'1 ~ }
\bar "|" \bar "|"
{ g'1 ~ } { a'1 ~ }
\bar "|" \bar "|"
{ g'1 ~ } { a'1 ~ }
\bar "|" \bar "|"
{ g'1 ~ } { a'1 ~ }
\bar "|" \bar "|"
{ g'1 ~ } { a'1 ~ }
\bar "|" \bar "|"
{ g'1 ~ } { a'1 ~ }
\bar "|" \bar "|"
{ g'1 ~ } { a'1 ~ }
\bar "|" \bar "|"
{ g'1 ~ } { a'1 ~ }
\bar "|" \bar "|"
{ g'1 ~ } { a'1 ~ }
\bar "|" \bar "|"
{ g'8.[ r16] r2. } { a'8.[ r16] r2. }
\bar "|"
{ r1 }
\bar "||" \bar "||"
} }

@ -1,34 +1,36 @@
{ {
{ r2. r8[ e'8^\markup { \pad-markup #0.2 "-14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 5↑" }}] ~ } { r2. r8[ fis'8^\markup { \pad-markup #0.2 "-14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 5↑" }}] ~ }
\bar "|" \bar "|"
{ e'8.[ f'16^\markup { \pad-markup #0.2 "-2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 3↓" }}] ~ f'4 ~ f'8[ e'8^\markup { \pad-markup #0.2 "-41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 13↓" }}] ~ e'4 ~ } { fis'8.[ g'16^\markup { \pad-markup #0.2 "-2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 3↓" }}] ~ g'4 ~ g'8[ fis'8^\markup { \pad-markup #0.2 "-41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 13↓" }}] ~ fis'4 ~ }
\bar "|" \bar "|"
{ e'4 ~ e'16[ fis'8.^\markup { \pad-markup #0.2 "-49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 11↑" }}] ~ fis'16[ g'8.^\markup { \pad-markup #0.2 "+2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 3↑" }}] ~ g'4 ~ } { fis'4 ~ fis'16[ gis'8.^\markup { \pad-markup #0.2 "-49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 11↑" }}] ~ gis'16[ a'8.^\markup { \pad-markup #0.2 "+2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 3↑" }}] ~ a'4 ~ }
\bar "|" \bar "|"
{ g'8.[ gis'16^\markup { \pad-markup #0.2 "+14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 5↓" }}] ~ gis'2. ~ } { a'8.[ ais'16^\markup { \pad-markup #0.2 "+14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 5↓" }}] ~ ais'2. ~ }
\bar "|" \bar "|"
{ gis'2. ~ gis'8.[ fis'16^\markup { \pad-markup #0.2 "-49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 11↑" }}] ~ } { ais'2. ~ ais'8.[ gis'16^\markup { \pad-markup #0.2 "-49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 11↑" }}] ~ }
\bar "|" \bar "|"
{ fis'2. ~ fis'16[ fis'8.^\markup { \pad-markup #0.2 "+49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 11↓" }}] ~ } { gis'2. ~ gis'16[ gis'8.^\markup { \pad-markup #0.2 "+49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 11↓" }}] ~ }
\bar "|" \bar "|"
{ fis'2 ~ fis'8.[ gis'16^\markup { \pad-markup #0.2 "+41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 1↑" }}] ~ gis'4 ~ } { gis'2 ~ gis'8.[ ais'16^\markup { \pad-markup #0.2 "+41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 1↑" }}] ~ ais'4 ~ }
\bar "|" \bar "|"
{ gis'1 ~ } { ais'1 ~ }
\bar "|" \bar "|"
{ gis'2 gis'2^\markup { \pad-markup #0.2 "+14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 1↑" }} ~ } { ais'2 ais'2^\markup { \pad-markup #0.2 "+14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 1↑" }} ~ }
\bar "|" \bar "|"
{ gis'1 ~ } { ais'1 ~ }
\bar "|" \bar "|"
{ gis'1 ~ } { ais'1 ~ }
\bar "|" \bar "|"
{ gis'1 ~ } { ais'1 ~ }
\bar "|" \bar "|"
{ gis'1 ~ } { ais'1 ~ }
\bar "|" \bar "|"
{ gis'2. ~ gis'16[ ais'8.^\markup { \pad-markup #0.2 "-31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 7↑" }}] ~ } { ais'2. ~ ais'16[ c''8.^\markup { \pad-markup #0.2 "-31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 7↑" }}] ~ }
\bar "|" \bar "|"
{ ais'4 ~ ais'8.[ c''16^\markup { \pad-markup #0.2 "+0"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 1↑" }}] ~ c''2 ~ } { c''4 ~ c''8.[ d''16^\markup { \pad-markup #0.2 "+0"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 1↑" }}] ~ d''2 ~ }
\bar "|" \bar "|"
{ c''16[ cis''8.^\markup { \pad-markup #0.2 "+39"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 13↑" }}] ~ cis''2 ~ cis''16[ d''8.^\markup { \pad-markup #0.2 "+31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 1↑" }}]} { d''16[ dis''8.^\markup { \pad-markup #0.2 "+39"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 13↑" }}] ~ dis''2 ~ dis''16[ e''8.^\markup { \pad-markup #0.2 "+31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 1↑" }}] ~ }
\bar "|"
{ e''4 ~ e''8.[ r16] r2 }
\bar "||" \bar "||"
} }

@ -1,23 +1,23 @@
{ {
{ c'1^\markup { \pad-markup #0.2 "+0"} ~ } { d'1^\markup { \pad-markup #0.2 "+0"} ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'2. ~ c'8.[ d'16^\markup { \pad-markup #0.2 "+31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 7↓" }}] ~ } { d'2. ~ d'8.[ e'16^\markup { \pad-markup #0.2 "+31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 7↓" }}] ~ }
\bar "|" \bar "|"
{ d'1 ~ } { e'1 ~ }
\bar "|" \bar "|"
{ d'1 ~ } { e'1 ~ }
\bar "|" \bar "|"
{ d'1 ~ } { e'1 ~ }
\bar "|" \bar "|"
{ d'1 ~ } { e'1 ~ }
\bar "|" \bar "|"
{ d'8.[ r16] r2. } { e'8.[ r16] r2. }
\bar "|" \bar "|"
{ r1 } { r1 }
\bar "|" \bar "|"
@ -25,10 +25,12 @@
\bar "|" \bar "|"
{ r1 } { r1 }
\bar "|" \bar "|"
{ r2. r16[ d'8.^\markup { \pad-markup #0.2 "+31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 7↓" }}] ~ } { r2. r16[ e'8.^\markup { \pad-markup #0.2 "+31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 7↓" }}] ~ }
\bar "|" \bar "|"
{ d'1 ~ } { e'1 ~ }
\bar "|"
{ e'1 ~ }
\bar "|" \bar "|"
{ d'1} { e'4 ~ e'8.[ r16] r2 }
\bar "||" \bar "||"
} }

@ -1,34 +1,36 @@
{ {
{ r2. r8[ ais8^\markup { \pad-markup #0.2 "-31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↑" }}] ~ } { r2. r8[ c'8^\markup { \pad-markup #0.2 "-31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↑" }}] ~ }
\bar "|" \bar "|"
{ ais1 ~ } { c'1 ~ }
\bar "|" \bar "|"
{ ais4 ~ ais16[ g8.^\markup { \pad-markup #0.2 "+2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 3↑" }}] ~ g16[ gis8.^\markup { \pad-markup #0.2 "+41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 13↑" }}] ~ gis4 ~ } { c'4 ~ c'16[ a8.^\markup { \pad-markup #0.2 "+2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 3↑" }}] ~ a16[ ais8.^\markup { \pad-markup #0.2 "+41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 13↑" }}] ~ ais4 ~ }
\bar "|" \bar "|"
{ gis8.[ gis16^\markup { \pad-markup #0.2 "+14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 1↑" }}] ~ gis2. ~ } { ais8.[ ais16^\markup { \pad-markup #0.2 "+14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 1↑" }}] ~ ais2. ~ }
\bar "|" \bar "|"
{ gis2. ~ gis8.[ ais16^\markup { \pad-markup #0.2 "-31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 7↑" }}] ~ } { ais2. ~ ais8.[ c'16^\markup { \pad-markup #0.2 "-31"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 7↑" }}] ~ }
\bar "|" \bar "|"
{ ais1 ~ } { c'1 ~ }
\bar "|" \bar "|"
{ ais2 ~ ais8.[ gis16^\markup { \pad-markup #0.2 "+41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 13↑" }}] ~ gis4 ~ } { c'2 ~ c'8.[ ais16^\markup { \pad-markup #0.2 "+41"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 13↑" }}] ~ ais4 ~ }
\bar "|" \bar "|"
{ gis2 ~ gis16[ gis8.^\markup { \pad-markup #0.2 "+14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 5↓" }}] ~ gis4 ~ } { ais2 ~ ais16[ ais8.^\markup { \pad-markup #0.2 "+14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 5↓" }}] ~ ais4 ~ }
\bar "|"
{ ais1 ~ }
\bar "|" \bar "|"
{ gis1 ~ } { ais2. ~ ais8[ a8^\markup { \pad-markup #0.2 "+2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 3↑" }}] ~ }
\bar "|" \bar "|"
{ gis2. ~ gis8[ g8^\markup { \pad-markup #0.2 "+2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 3↑" }}] ~ } { a4 ~ a8[ c'8^\markup { \pad-markup #0.2 "+45"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 7↓" }}] ~ c'4 ~ c'8.[ e'16^\markup { \pad-markup #0.2 "-35"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 11↑" }}] ~ }
\bar "|" \bar "|"
{ g4 ~ g8[ ais8^\markup { \pad-markup #0.2 "+45"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 7↓" }}] ~ ais4 ~ ais8.[ d'16^\markup { \pad-markup #0.2 "-35"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 11↑" }}] ~ } { e'2. dis'4^\markup { \pad-markup #0.2 "+12"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 3↓" }} ~ }
\bar "|" \bar "|"
{ d'2. cis'4^\markup { \pad-markup #0.2 "+12"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 3↓" }} ~ } { dis'2 ~ dis'8[ fis'8^\markup { \pad-markup #0.2 "-14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 5↑" }}] ~ fis'4 ~ }
\bar "|" \bar "|"
{ cis'2 ~ cis'8[ e'8^\markup { \pad-markup #0.2 "-14"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 5↑" }}] ~ e'4 ~ } { fis'8.[ g'16^\markup { \pad-markup #0.2 "-2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 3↓" }}] ~ g'2. ~ }
\bar "|" \bar "|"
{ e'8.[ f'16^\markup { \pad-markup #0.2 "-2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 3↓" }}] ~ f'2. ~ } { g'1 ~ }
\bar "|" \bar "|"
{ f'1 ~ } { g'1 ~ }
\bar "|" \bar "|"
{ f'1} { g'4 ~ g'8.[ r16] r2 }
\bar "||" \bar "||"
} }

@ -7,28 +7,30 @@
\bar "|" \bar "|"
{ r1 } { r1 }
\bar "|" \bar "|"
{ r8[ c'8^\markup { \pad-markup #0.2 "+0"}] ~ c'2. ~ } { r8[ d'8^\markup { \pad-markup #0.2 "+0"}] ~ d'2. ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1 ~ } { d'1 ~ }
\bar "|" \bar "|"
{ c'1} { d'1 ~ }
\bar "|"
{ d'4 ~ d'8.[ r16] r2 }
\bar "||" \bar "||"
} }

@ -1,27 +0,0 @@
{
"ledger":
[
"314s49e1",
"7e170ef8",
"7ac10d34",
"628d5c8b",
"6abf27d4",
"6d743c5c",
"5625a95a",
"5267b235",
"6a3f7c7c",
"6d0c2f19",
"7bf874ce",
"50fec831",
"7fd4d544",
"6f1305ed",
"7c7a96a2",
"7c8bc6df",
"46210507",
"74faf83f",
"703e109b",
"4b96f62d",
"449aeaa6",
"6409252e"
]
}

@ -1,26 +0,0 @@
{
"ledger":
[
"314s49e1",
"7e170ef8",
"7ac10d34",
"628d5c8b",
"6abf27d4",
"6d743c5c",
"5625a95a",
"5267b235",
"6a3f7c7c",
"6d0c2f19",
"7bf874ce",
"50fec831",
"7fd4d544",
"6f1305ed",
"7c7a96a2",
"7c8bc6df",
"46210507",
"74faf83f",
"703e109b",
"4b96f62d",
"449aeaa6"
]
}

@ -1,581 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrder, genSubMotif, updateVoices;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var writeResources, prettifyArray, setSeeds, sanityCheck, msgInterpret;
// global vars (many set by OSC funcs at bottom)
var refSeed, seed, lastXChanges, popSize, exPath, dir, primes, dims, tuples, ranges, durFunc, seq, group, player;
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genOrder = {arg minLength = 0, maxLength = 5;
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noProgIns = (popSize - 1).rand + 1;
noSusIns = (popSize - noProgIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxLength - minLength).rand + minLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = [1, 1, 1, 1, 1];
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, res, lastIns, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
lastIns = nil;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
(silent ++ sus ++ prog).do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
//dur = [durFunc.value(), 0].wchoose([1, 0].normalizeSum);
dur = durFunc.value(lastIns, ins);
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur.round(0.125)]);
});
lastIns = ins;
});
// pad ending
if(isLastOrder, {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
//dur = [durFunc.value(), 0].wchoose([1, 0].normalizeSum);
dur = durFunc.value(lastIns, ins);
res = res.add([voices.deepCopy.postln, dur.round(0.125)]);
});
lastIns = ins;
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold});
res;
};
//------primary routines
genMotif = {arg inOrders;
var orders, repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders = inOrders;
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
genPatterns = {arg seq;
var voices, durs, patterns, res;
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice;
var clumps, hdScores, freqs, fDurs;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\instrument, \test,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1)
);
});
);
res
};
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
//------resource management funcs
setSeeds = {arg inRefSeed, inSeed;
refSeed = if(inRefSeed.isNumber, {inRefSeed.asInteger}, {nil});
seed = if(inSeed > 1, {inSeed.asInteger}, {rrand(100000, 999999)});
thisThread.randSeed = seed;
};
prettifyArray = {arg data, finDepth = 1;
var prettyString = "", rCount = 0, writeArray;
writeArray = {arg array;
var depth, indent;
depth = array.maxDepth;
indent = rCount.collect({" "}).join("");
prettyString = prettyString ++ indent ++ "[\n";
rCount = rCount + 1;
if(depth > 5, {
array.do({arg subArray;
writeArray.value(subArray);
});
}, {
array.do({arg data, d;
prettyString = prettyString ++ indent ++ " " ++ data.asCompileString ++ if(d != (array.size - 1), {",\n"}, {""});
});
});
rCount = rCount - 1;
if(rCount < (finDepth - 1), {prettyString = prettyString.drop((finDepth - 1).neg)});
//if(rCount == 0, {prettyString = prettyString.drop((finDepth - 1).neg)});
prettyString = prettyString ++ "\n" ++ indent ++ "]" ++ if(rCount > 0, {",\n"}, {""});
};
writeArray.value(data);
prettyString
};
writeResources = {arg seq, path;
var dir, file, resString;
file = File(path,"w");
resString = "{\nmusic_data:\n";
resString = resString ++ prettifyArray.value(seq, 3);
resString = resString ++ ",\nlast_changes:\n";
resString = resString ++ prettifyArray.value(lastXChanges, 1);
resString = resString ++ ",\nseed: " ++ seed ++ ",\nref_seed: " ++ refSeed ++ "\n}";
file.write(resString);
file.close;
};
sanityCheck = {arg motif, index;
//print functions - very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in;
var res;
res = in.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
res = res.replace("\'", "").replace("\"", "").replace("Rest", "\"Rest\"");
res.interpret
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
popSize = 4;
dims = primes.size;
tuples = genTuples.value();
refSeed = nil;
group = Group.new;
//------OSC funcs
OSCdef(\gen, {arg msg, time, addr, port;
var orders, condition;
msg.postln;
durFunc = nil;
addr.sendMsg("/STATE/SEND");
{
while({durFunc == nil}, {0.1.wait});
setSeeds.value(msg[1].postln, msg[2]);
lastXChanges = if(refSeed == nil, {
[initVoices.value().deepCopy];
}, {
var file;
file = File((dir +/+ "resources" +/+ refSeed ++ "_music" ++ ".json").standardizePath, "r");
msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
if(msg.size == 4, {
orders = msgInterpret.value(msg[3]);
}, {
var minLength, maxLength;
minLength = msg[3];
maxLength = msg[4];
orders = ((maxLength - minLength).rand + minLength).collect({genOrder.value(msg[5], msg[6])});
});
orders.postln;
seed.postln;
refSeed.postln;
seq = genMotif.value(orders);
//patterns = genPatterns.value(seq);
addr.sendMsg("/current_seed", seed);
addr.sendMsg("/order", prettifyArray.value(orders, 1));
addr.sendMsg("/mus_seq", prettifyArray.value(seq, 3));
}.fork;
}, \gen);
OSCdef(\commit, {arg msg, time, addr, port;
var ledgerPath, oldLedger, newLedger, musSeq;
//msg.postln;
seed.postln;
//File.copy(exPath, (dir +/+ "resources" +/+ seed ++ "_code" ++ ".scd").standardizePath);
//addr.sendMsg("/SESSION/SAVE", (dir +/+ "resources" +/+ seed ++ "_gui_session" ++ ".json").standardizePath);
//addr.sendMsg("/STATE/SAVE", (dir +/+ "resources" +/+ seed ++ "_gui_state" ++ ".state").standardizePath);
writeResources.value(seq, (dir +/+ "resources" +/+ seed ++ "_music" ++ ".json").standardizePath);
ledgerPath = (dir +/+ "resources" +/+ "piece_ledger" ++ ".json").standardizePath;
oldLedger = File(ledgerPath, "r");
musSeq = msgInterpret.value(oldLedger.readAllString.parseJSON["ledger"]);
oldLedger.close;
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
newLedger = File(ledgerPath, "w");
musSeq = musSeq.add(seed);
newLedger.write("{\nledger:\n" ++ prettifyArray.value(musSeq, 1) ++ "\n}");
newLedger.close;
//refSeed = seed;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
player.stop;
group.set(\gate, 0);
}, {
var cSize, ledgerPath, ledger, patterns, pSeq;
ledgerPath = (dir +/+ "resources" +/+ "piece_ledger" ++ ".json").standardizePath;
ledger = msgInterpret.value(File(ledgerPath, "r").readAllString.parseJSON["ledger"]);
pSeq = [];
if(msg[2].asString != "all", {ledger = ledger.keep(msg[2].asInteger - 1)});
ledger.do({arg rSeed;
var file;
file = File((dir +/+ "resources" +/+ rSeed.postln ++ "_music" ++ ".json").standardizePath, "r");
pSeq = pSeq.add(msgInterpret.value(file.readAllString.parseJSON["music_data"]));
file.close;
});
pSeq = pSeq.add(seq);
patterns = genPatterns.value(pSeq);
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
});
player = player.play
});
}, \transport);
OSCdef(\range, {arg msg;
msg.postln;
ranges[msg[1]][msg[2]] = msg[3]
}, \range);
OSCdef(\dur_probs_env, {arg msg;
var env, pTable, min, max, cProb;
msg.postln;
env = Env.pairs([[0, 0]] ++ msg[4..].clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
min = msg[1];
max = msg[2];
cProb = msg[3];
durFunc = {arg lIns, cIns;
if(lIns.postln == cIns.postln, {
pTable.tableRand * (max - min) + min
}, {
if(1.0.rand < cProb.postln, {0}, {pTable.tableRand * (max - min) + min}).postln;
});
};
}, \dur_probs_env);
)
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms;
noHarms = 30;
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
File((~dir +/+ "resources" +/+ 517313 ++ "_music" ++ ".json").standardizePath, "r").readAllString.parseJSON["last_changes"].asString.interpret[0][0][0].isNumber
"{\"a\": 1}".parseYAML["a"].asInteger;
"{\"a\": 1}".parseJSON["a"].isNumber;

@ -1,74 +0,0 @@
{
"music_data":
[
[
[
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 0.875 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ] ], 0.625 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0, -1 ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ] ], 1.5 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 1, 0, 0, 0 ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ] ], 0.875 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ] ], 0.875 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ] ], 1.0 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 1, 0, 0 ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ] ], 1.0 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ] ], 1.25 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 1 ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ] ], 0.875 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, -1, 0, 0, 0 ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ] ], 0.875 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ] ], 1.25 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 1, 0, 0, 0 ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ] ], 0.75 ]
],
[
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ] ], 0.875 ],
[ [ [ 1, 0, 0, 0, 0, -1 ], [ "Rest" ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ] ], 1.5 ],
[ [ [ 1, 0, 0, 0, 0, -1 ], [ "Rest" ], [ -1, 0, 0, 1, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 1.25 ],
[ [ [ 0, 1, 0, 0, 0, 0 ], [ "Rest" ], [ -1, 0, 0, 1, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 1.375 ],
[ [ [ 0, 0, 0, 1, 0, 0 ], [ "Rest" ], [ -1, 0, 0, 1, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 0.75 ],
[ [ [ 0, 0, 0, 1, 0, 0 ], [ "Rest" ], [ 0, 0, -1, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 0.625 ],
[ [ [ 0, 0, 0, 1, 0, 0 ], [ "Rest" ], [ 0, 0, 0, 0, -1, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 0.75 ],
[ [ [ 0, 0, 0, 0, 0, 1 ], [ "Rest" ], [ 0, 0, 0, 0, -1, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 1.125 ],
[ [ [ 1, 0, -1, 0, 0, 0 ], [ "Rest" ], [ 0, 0, 0, 0, -1, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 1.25 ],
[ [ [ 0, 1, 0, 0, 0, 0 ], [ "Rest" ], [ 0, 0, 0, 0, -1, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 1.0 ],
[ [ [ 1, -1, 0, 0, 0, 0 ], [ "Rest" ], [ 0, 0, 0, 0, -1, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 0.875 ]
],
[
[ [ [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, -1, 0, -1, 0 ], [ 0, 0, 0, 0, -1, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 0.875 ],
[ [ [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, -1, 0, -1, 0 ], [ 0, 0, 0, 0, -1, 0 ], [ 1, -1, 0, 0, -1, 0 ] ], 0.75 ],
[ [ [ 0, 0, 0, 1, -1, 0 ], [ 1, 0, -1, 0, -1, 0 ], [ 0, 0, 0, 0, -1, 0 ], [ 1, -1, 0, 0, -1, 0 ] ], 1.5 ],
[ [ [ 0, 0, 0, 1, -1, 0 ], [ 0, 0, 0, 0, -1, 1 ], [ 0, 0, 0, 0, -1, 0 ], [ 1, -1, 0, 0, -1, 0 ] ], 1.25 ],
[ [ [ 0, 0, 0, 1, -1, 0 ], [ 0, 1, 0, 0, -1, 0 ], [ 0, 0, 0, 0, -1, 0 ], [ 1, -1, 0, 0, -1, 0 ] ], 0.625 ],
[ [ [ 1, 0, 0, 0, -1, 0 ], [ 0, 1, 0, 0, -1, 0 ], [ 0, 0, 0, 0, -1, 0 ], [ 1, -1, 0, 0, -1, 0 ] ], 0.625 ],
[ [ [ 1, 0, 0, 0, -1, 0 ], [ 0, 1, 0, 0, -1, 0 ], [ 0, 0, 0, 0, -1, 0 ], [ 0, 0, 1, 0, -1, 0 ] ], 0.625 ],
[ [ [ 1, 0, 0, 0, -1, 0 ], [ 0, 1, 0, 0, -1, 0 ], [ 0, 0, 0, 0, -1, 0 ], [ 1, 0, 0, -1, -1, 0 ] ], 1.375 ],
[ [ [ 1, 0, 0, 0, -1, 0 ], [ 0, 0, 1, 0, -1, 0 ], [ 0, 0, 0, 0, -1, 0 ], [ 1, 0, 0, -1, -1, 0 ] ], 1.375 ],
[ [ [ 2, 0, 0, -1, -1, 0 ], [ 0, 0, 1, 0, -1, 0 ], [ 0, 0, 0, 0, -1, 0 ], [ 1, 0, 0, -1, -1, 0 ] ], 1.5 ],
[ [ [ 2, 0, 0, 0, -1, -1 ], [ 0, 0, 1, 0, -1, 0 ], [ 0, 0, 0, 0, -1, 0 ], [ 1, 0, 0, -1, -1, 0 ] ], 0.75 ],
[ [ [ 2, 0, 0, 0, -1, -1 ], [ 1, -1, 0, 0, -1, 0 ], [ 0, 0, 0, 0, -1, 0 ], [ 1, 0, 0, -1, -1, 0 ] ], 1.0 ]
],
[
[ [ [ 2, 0, 0, 0, -1, -1 ], [ 1, -1, 0, 0, -1, 0 ], [ "Rest" ], [ 1, 0, 0, -1, -1, 0 ] ], 0.75 ],
[ [ [ 2, 0, 0, 0, -1, -1 ], [ 1, -1, 0, 0, -1, 0 ], [ "Rest" ], [ 0, -1, 0, 1, -1, 0 ] ], 1.0 ],
[ [ [ 2, -1, 0, 0, -1, 0 ], [ 1, -1, 0, 0, -1, 0 ], [ "Rest" ], [ 0, -1, 0, 1, -1, 0 ] ], 0.875 ],
[ [ [ 2, -1, 0, 0, -1, 0 ], [ 1, -1, 0, 0, -1, 0 ], [ "Rest" ], [ 0, -1, 0, 0, -1, 1 ] ], 1.25 ],
[ [ [ 1, -1, 0, 1, -1, 0 ], [ 1, -1, 0, 0, -1, 0 ], [ "Rest" ], [ 0, -1, 0, 0, -1, 1 ] ], 1.125 ],
[ [ [ 1, -1, 0, 1, -1, 0 ], [ 1, -1, 0, 0, -1, 0 ], [ "Rest" ], [ 1, -1, -1, 0, -1, 0 ] ], 1.0 ],
[ [ [ 1, 0, 0, 0, -1, 0 ], [ 1, -1, 0, 0, -1, 0 ], [ "Rest" ], [ 1, -1, -1, 0, -1, 0 ] ], 0.75 ],
[ [ [ 1, -1, 0, 0, 0, 0 ], [ 1, -1, 0, 0, -1, 0 ], [ "Rest" ], [ 1, -1, -1, 0, -1, 0 ] ], 0.625 ],
[ [ [ 1, -1, 0, 0, 0, 0 ], [ 1, -1, 0, 0, -1, 0 ], [ "Rest" ], [ 0, -1, 0, 0, 0, 0 ] ], 0.75 ],
[ [ [ 1, -1, 0, 0, 0, 0 ], [ 1, -1, 0, 0, -1, 0 ], [ "Rest" ], [ 1, -1, 0, 0, -2, 0 ] ], 1.375 ],
[ [ [ 2, -2, 0, 0, -1, 0 ], [ 1, -1, 0, 0, -1, 0 ], [ "Rest" ], [ 1, -1, 0, 0, -2, 0 ] ], 0.875 ],
[ [ [ 2, -2, 0, 0, -1, 0 ], [ 1, -1, 0, 0, -1, 0 ], [ "Rest" ], [ 0, -1, 0, 0, -1, 1 ] ], 3.375 ],
[ [ [ 2, -2, 0, 0, -1, 0 ], [ "Rest" ], [ "Rest" ], [ 0, -1, 0, 0, -1, 1 ] ], 1.5 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ 0, -1, 0, 0, -1, 1 ] ], 1.375 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 1.0 ]
]
]
],
"last_changes":
[
[ [ 1, -1, 0, 0, 0, 0 ], [ 1, -1, 0, 0, -1, 0 ], [ 0, 0, 0, 0, -1, 0 ], [ 1, -1, -1, 0, -1, 0 ] ],
[ [ 1, -1, 0, 0, 0, 0 ], [ 1, -1, 0, 0, -1, 0 ], [ 0, 0, 0, 0, -1, 0 ], [ 0, -1, 0, 0, 0, 0 ] ],
[ [ 1, -1, 0, 0, 0, 0 ], [ 1, -1, 0, 0, -1, 0 ], [ 0, 0, 0, 0, -1, 0 ], [ 1, -1, 0, 0, -2, 0 ] ],
[ [ 2, -2, 0, 0, -1, 0 ], [ 1, -1, 0, 0, -1, 0 ], [ 0, 0, 0, 0, -1, 0 ], [ 1, -1, 0, 0, -2, 0 ] ],
[ [ 2, -2, 0, 0, -1, 0 ], [ 1, -1, 0, 0, -1, 0 ], [ 0, 0, 0, 0, -1, 0 ], [ 0, -1, 0, 0, -1, 1 ] ]
],
"ref_uid": "nil"
}

@ -1,907 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict, saveLedger;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold.deepCopy});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~msg.postln;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr, oneShot = false;
var voices, durs, pbinds, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
pbinds = voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs, attacks, rels, amps;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
//attacks = 2.collect({rrand(1, 3)}) ++ freqs.drop(2).collect({rrand(0.3, 0.5)});
attacks = fDurs.collect({arg dur; dur * rrand(0.2, 0.4)});
//rels = freqs.drop(2).collect({rrand(0.3, 0.5)}) ++ 2.collect({rrand(1, 3)});
rels = (clumps.size - 1).collect({arg c;
if(clumps[c + 1][0] == ["Rest"], {rrand(1, 3)}, {rrand(0.3, 0.5)});
});
amps = freqs.collect({rrand(0.6, 0.99)});
[
Pbind(
\instrument, \string_model,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\attack, Pseq(attacks, 1),
\sustain, Pseq(fDurs, 1),
\release, Pseq(rels, 1),
//\amp, Pseq(amps, 1),
\amp, Pbrown(0.5, 1, 0.5),
\busIndex, v
),
Pbind(
\instrument, \sine,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
)
]
}).flatten;
if(oneShot.not, {
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
pbinds = pbinds ++
[
Pbind(
\type, \osc,
\addr, addr,
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
});
res = Ppar(pbinds);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
//res.postln;
if(res.every({arg char; char.isDecDigit}), {res = res.asInteger});
});
});
res
};
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict, skipLastXChanges = false;
var tmpLastXChanges;
tmpLastXChanges = lastXChanges.deepCopy;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
if(skipLastXChanges, {lastXChanges = tmpLastXChanges});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
saveLedger = {arg ledger, path;
var file;
file = File(path, "w");
file.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
file.close;
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
~group = group;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\save_ledger, {arg msg, time, addr, port;
msg.postln;
ledger = msgInterpret.value(msg[1].asString.parseJSON["ledger"], false).postln;
//loadLedgerJSON.value(msg[0])
saveLedger.value(ledger, msg[2].asString);
//loadLedgerFile.value(msg[1].asString);
}, \save_ledger);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict, true);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
if(refUID == nil, {lastXChanges = [initVoices.value().deepCopy]});
if((refUID != nil) && (refUID != "tmp"), {
var file;
file = File((dir +/+ ".." +/+ "resources" +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
lastXChanges = msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
refUID.postln;
lastXChanges.collect({arg item; item.postln});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
seq = seedFunc.value(genMotif, motifSeed).value;
lastXChanges.collect({arg item; item.postln});
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
addr.sendMsg("/generated", path, modelString, ledgerPath);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
musicChanged = (musicData != seq).postln;
curUID = genUID.value;
File.mkdir((dir +/+ ".." +/+ "resources" +/+ curUID).standardizePath);
File.copy(exPath, (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {
seq = musicData;
dict["music_data"] = seq;
dict["motif_edited"] = "true"
});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
ledger = ledger.drop(-1).add(curUID);
saveLedger.value(ledger, ledgerPath);
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
group.set(\release, 2);
group.set(\gate, 0);
player.stop;
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
if(msg[1] == 1, {
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (dir +/+ ".." +/+ "resources" +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.postln.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
}, {
pSeq = [loadModelJSON.value(msg[2].asString.parseJSON)["music_data"].postln];
patterns = genPatterns.value(pSeq, addr, true);
});
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
addr.sendMsg("/one_shot", 0);
});
player = player.play
});
}, \transport);
)
(
//synthdefs
~stringModelBusArray = 4.collect({Bus.audio(s, 1)});
~sineBusArray = 4.collect({Bus.audio(s, 1)});
~bassBusArray = 1.collect({Bus.audio(s, 1)});
~hdustBusArray = 1.collect({Bus.audio(s, 1)});
~samplerBusArray = 2.collect({Bus.audio(s, 1)});
~sBuf = Buffer.alloc(s, 10, 2);
SynthDef(\string_model, {arg freq, gate = 1, sustain, amp, dur, attack, release = 1, busIndex = 0;
var trig, exc, sig1, sig2, noHarms;
noHarms = rrand(20, 40);
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.8).softclip;
//sig1 = HPF.ar(sig1, 300);
Out.ar(Select.kr(busIndex, ~stringModelBusArray), sig1 * amp * EnvGen.kr(Env.adsr(attack, 0.3, 0.9, release, 0.9, -3), gate, doneAction: 2));
//Out.ar([0, 1], sig1 * EnvGen.kr(Env.asr(dur, 0.3, 1), gate, doneAction: 2));
}).add;
SynthDef(\sine, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var sig;
sig = SinOsc.ar(freq);
Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.asr(0.3, 0.4, 0.3), gate, timeScale: dur, doneAction: 2));
//Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.sine(dur), gate, doneAction: 2));
}).add;
SynthDef(\mixer, {arg freq, gate = 1, sustain, amp, dur, out;
var nameSpaces, sigs, hierarchicalDust;
hierarchicalDust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1))
);
sigs = [~stringModelBusArray, ~sineBusArray, ~bassBusArray, ~hdustBusArray, ~samplerBusArray].collect({arg busArray, i;
var nameSpace, sig;
nameSpace = ['string', 'sine', 'bass', 'hdust', 'sampler'][i];
sig = busArray.collect({arg bus, c; In.ar(bus, 1) * NamedControl.kr(\ ++ nameSpace ++ '_volume_' ++ c, 1, 0.1)});
sig = sig.collect({arg channel, c; Pan2.ar(channel, NamedControl.kr(\ ++ nameSpace ++ '_pan_' ++ c, i / (busArray.size - 1), 0.1) * 2 - 1)});
sig = sig.collect({arg channel, c; channel * NamedControl.kr(\ ++ nameSpace ++ '_mute_' ++ c, 1, 0.1)});
sig = Mix.ar(sig) * pow(NamedControl.kr(\ ++ nameSpace ++ '_volume_master', 1, 0.1), 2);
});
sigs = Mix.ar(sigs / 4);
Out.ar(0, sigs)
}).add;
SynthDef(\bass, {
var switches, drone;
switches = {|i| Dust.kr(0.1)} ! 9;
drone = {|i| var harm = pow(2, 2 - (i / 3).trunc), amp = (1 / pow(harm, 2));
SinOsc.ar(60 * harm + TRand.kr(-3, 3, switches[i]), 0, amp)} ! 9;
Out.ar(~bassBusArray[0], Mix.new(drone) * 0.2);
}).add;
SynthDef(\sampler, {
Out.ar(~samplerBusArray, PlayBuf.ar(2, ~sBuf, BufRateScale.kr(~sBuf), doneAction: 2))
}).add;
// main routine
SynthDef(\hdust, {
arg gate = 0;
var hierarchical_dust, low_sine, high_sine, brown_noise, white_noise;
// this triggers the combinations of sources
// it is similar to the Supercollider UGen called dust but with a hierarchical structure
hierarchical_dust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1)) *
TIRand.kr(0, 1, Impulse.kr(0.1))
);
// adjust the multiplier at the end of each line for adjusting levels
// note with each trigger, each source has a 1 in 3 chance of sounding
low_sine = SinOsc.ar(76.midicps / 16) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.1;
high_sine = SinOsc.ar(76.midicps * 8) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.01;
brown_noise = BrownNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.025;
white_noise = WhiteNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.02;
Out.ar(~hdustBusArray[0],
((low_sine + high_sine + brown_noise + white_noise) )
);
}).add;
)
(
var bass, hdust, sampler, mixer;
bass = Synth.tail(~group, \bass);
hdust = Synth.tail(~group, \hdust);
sampler = Synth.head(~group, \sampler);
mixer = Synth.tail(~group, \mixer);
OSCdef(\mixer, {arg msg, time, addr, port;
mixer.set((msg[1] ++ '_' ++ msg[2] ++ '_' ++ msg[3]), msg[4])
}, \mixer);
OSCdef(\sampler, {arg msg, time, addr, port;
msg.postln;
sampler.free;
~sBuf.free;
~sBuf = Buffer.read(s, msg[1].asString.postln, action: {sampler = Synth.head(~group, \sampler)});
}, \sampler);
)
/* old something
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
*/

@ -1,83 +0,0 @@
{
"music_data":
[
[
[
[ [ [ "Rest" ], [ "Rest" ], [ 0, 1, 0, 0, 0, -1 ], [ "Rest" ] ], 4 ],
[ [ [ "Rest" ], [ 0, 1, 0, 1, 0, -1 ], [ 0, 1, 0, 0, 0, -1 ], [ "Rest" ] ], 2 ],
[ [ [ "Rest" ], [ 0, 1, 0, 1, 0, -1 ], [ 0, 1, 0, 0, 0, -1 ], [ 1, 1, 0, 0, -1, -1 ] ], 1.875 ],
[ [ [ -1, 1, 0, 1, 0, -1 ], [ 0, 1, 0, 1, 0, -1 ], [ 0, 1, 0, 0, 0, -1 ], [ 1, 1, 0, 0, -1, -1 ] ], 1.625 ],
[ [ [ -1, 1, 0, 1, 0, -1 ], [ 0, 1, 0, 1, 0, -1 ], [ 0, 1, 0, 0, 0, -1 ], [ 0, 2, 0, 0, 0, -1 ] ], 1.75 ],
[ [ [ -1, 2, 0, 0, 0, -1 ], [ 0, 1, 0, 1, 0, -1 ], [ 0, 1, 0, 0, 0, -1 ], [ 0, 2, 0, 0, 0, -1 ] ], 1.875 ],
[ [ [ -1, 1, 0, 0, 0, 0 ], [ 0, 1, 0, 1, 0, -1 ], [ 0, 1, 0, 0, 0, -1 ], [ 0, 2, 0, 0, 0, -1 ] ], 0 ],
[ [ [ -1, 1, 0, 0, 0, 0 ], [ 1, 1, 0, 0, 0, -1 ], [ 0, 1, 0, 0, 0, -1 ], [ 0, 2, 0, 0, 0, -1 ] ], 2 ],
[ [ [ -1, 1, 0, 0, 0, 0 ], [ 2, 1, 0, -1, 0, -1 ], [ 0, 1, 0, 0, 0, -1 ], [ 0, 2, 0, 0, 0, -1 ] ], 1.375 ],
[ [ [ 0, 1, -1, 0, 0, -1 ], [ 2, 1, 0, -1, 0, -1 ], [ 0, 1, 0, 0, 0, -1 ], [ 0, 2, 0, 0, 0, -1 ] ], 1 ],
[ [ [ 0, 1, -1, 0, 0, -1 ], [ 2, 1, 0, 0, 0, -2 ], [ 0, 1, 0, 0, 0, -1 ], [ 0, 2, 0, 0, 0, -1 ] ], 7.375 ]
],
[
[ [ [ -1, 2, 0, 0, 0, -1 ], [ 2, 1, 0, 0, 0, -2 ], [ 0, 1, 0, 0, 0, -1 ], [ 0, 2, 0, 0, 0, -1 ] ], 0 ],
[ [ [ -1, 2, 0, 0, 0, -1 ], [ 2, 1, 0, 0, 0, -2 ], [ 0, 1, 0, 0, 0, -1 ], [ 1, 0, 0, 0, 0, -1 ] ], 0 ],
[ [ [ -1, 2, 0, 0, 0, -1 ], [ 2, 0, 0, 0, 0, -1 ], [ 0, 1, 0, 0, 0, -1 ], [ 1, 0, 0, 0, 0, -1 ] ], 5.375 ]
],
[
[ [ [ -1, 2, 0, 0, 0, -1 ], [ "Rest" ], [ 0, 1, 0, 0, 0, -1 ], [ 1, 0, 0, 0, 0, -1 ] ], 0 ],
[ [ [ -1, 2, 0, 0, 0, -1 ], [ "Rest" ], [ 0, 1, 0, 0, 0, -1 ], [ "Rest" ] ], 2 ],
[ [ [ -1, 2, 0, 0, 0, -1 ], [ "Rest" ], [ -1, 2, 1, 0, 0, -1 ], [ "Rest" ] ], 1.25 ],
[ [ [ -1, 2, 0, 0, 0, -1 ], [ "Rest" ], [ -1, 2, 0, 0, 1, -1 ], [ "Rest" ] ], 1.875 ],
[ [ [ -1, 2, 0, 0, 0, -1 ], [ "Rest" ], [ 0, 2, 0, 0, -1, -1 ], [ "Rest" ] ], 1.25 ],
[ [ [ -1, 2, 0, 0, 0, -1 ], [ "Rest" ], [ -1, 3, 0, 0, 0, -1 ], [ "Rest" ] ], 1.375 ],
[ [ [ -1, 2, 0, 0, 0, -1 ], [ "Rest" ], [ 0, 2, -1, 0, 0, -1 ], [ "Rest" ] ], 8.375 ]
],
[
[ [ [ -1, 2, 0, 0, 0, -1 ], [ 2, 2, -1, 0, 0, -2 ], [ 0, 2, -1, 0, 0, -1 ], [ "Rest" ] ], 1.75 ],
[ [ [ -1, 2, 0, 0, 0, -1 ], [ 2, 2, -1, 0, 0, -2 ], [ 0, 2, -1, 0, 0, -1 ], [ 1, 2, -1, 0, 0, -2 ] ], 0 ],
[ [ [ -1, 2, -1, 0, 1, -1 ], [ 2, 2, -1, 0, 0, -2 ], [ 0, 2, -1, 0, 0, -1 ], [ 1, 2, -1, 0, 0, -2 ] ], 2 ],
[ [ [ 0, 2, -1, 0, 0, -2 ], [ 2, 2, -1, 0, 0, -2 ], [ 0, 2, -1, 0, 0, -1 ], [ 1, 2, -1, 0, 0, -2 ] ], 1.5 ],
[ [ [ 0, 2, -1, 0, 0, -2 ], [ 2, 2, -1, -1, 0, -1 ], [ 0, 2, -1, 0, 0, -1 ], [ 1, 2, -1, 0, 0, -2 ] ], 7 ]
],
[
[ [ [ 0, 2, -1, 0, 0, -2 ], [ "Rest" ], [ 0, 2, -1, 0, 0, -1 ], [ 1, 2, -1, 0, 0, -2 ] ], 1.5 ],
[ [ [ 0, 2, -1, 0, 0, -2 ], [ "Rest" ], [ 0, 2, -1, 0, 0, -1 ], [ "Rest" ] ], 1.25 ],
[ [ [ 0, 2, -1, -1, 0, -1 ], [ "Rest" ], [ 0, 2, -1, 0, 0, -1 ], [ "Rest" ] ], 1.875 ],
[ [ [ -1, 2, -1, 0, 0, -1 ], [ "Rest" ], [ 0, 2, -1, 0, 0, -1 ], [ "Rest" ] ], 1.5 ],
[ [ [ -2, 2, -1, 0, 0, 0 ], [ "Rest" ], [ 0, 2, -1, 0, 0, -1 ], [ "Rest" ] ], 1 ],
[ [ [ -1, 2, -2, 0, 0, -1 ], [ "Rest" ], [ 0, 2, -1, 0, 0, -1 ], [ "Rest" ] ], 1.375 ],
[ [ [ -2, 2, -1, 1, 0, -1 ], [ "Rest" ], [ 0, 2, -1, 0, 0, -1 ], [ "Rest" ] ], 6.75 ],
[ [ [ "Rest" ], [ "Rest" ], [ 0, 2, -1, 0, 0, -1 ], [ "Rest" ] ], 1.125 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 7.5 ]
]
]
],
"last_changes":
[
[ [ 0, 2, -1, -1, 0, -1 ], [ 2, 2, -1, -1, 0, -1 ], [ 0, 2, -1, 0, 0, -1 ], [ 1, 2, -1, 0, 0, -2 ] ],
[ [ -1, 2, -1, 0, 0, -1 ], [ 2, 2, -1, -1, 0, -1 ], [ 0, 2, -1, 0, 0, -1 ], [ 1, 2, -1, 0, 0, -2 ] ],
[ [ -2, 2, -1, 0, 0, 0 ], [ 2, 2, -1, -1, 0, -1 ], [ 0, 2, -1, 0, 0, -1 ], [ 1, 2, -1, 0, 0, -2 ] ],
[ [ -1, 2, -2, 0, 0, -1 ], [ 2, 2, -1, -1, 0, -1 ], [ 0, 2, -1, 0, 0, -1 ], [ 1, 2, -1, 0, 0, -2 ] ],
[ [ -2, 2, -1, 1, 0, -1 ], [ 2, 2, -1, -1, 0, -1 ], [ 0, 2, -1, 0, 0, -1 ], [ 1, 2, -1, 0, 0, -2 ] ]
],
"cur_uid": "449aeaa6",
"ref_uid": "628d5c8b",
"order_seed": 338959,
"dur_seed": 810921,
"motifs_seed": 154723,
"entrances_probs_vals": [ 0.29, 0, 7.0238095238095, 0.77, 2.1703296703297, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"passages_probs_vals": [ 0.29, 0, 7.0238095238095, 0.77, 2.1703296703297, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"exits_probs_vals": [ 0.29, 0, 7.0238095238095, 0.77, 2.1703296703297, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"ranges": [ [ -3600, -349 ], [ 799, 1758 ], [ -282, 1013 ], [ -282, 799 ] ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"order":
[
[ [ 2 ], [ 1, 3, 0, 3, 0, 0, 1, 1, 0, 1 ], [ ] ],
[ [ 2 ], [ 0, 3, 1 ], [ ] ],
[ [ 0 ], [ 2, 2, 2, 2, 2 ], [ 1, 3 ] ],
[ [ 2 ], [ 1, 3, 0, 0, 1 ], [ ] ],
[ [ 2 ], [ 0, 0, 0, 0, 0 ], [ 1, 3 ] ]
],
"sus_weights": [ 0.75, 0.34, 0 ],
"order_size": [ 2, 6 ],
"passages_size": [ 0, 10 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,907 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict, saveLedger;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold.deepCopy});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~msg.postln;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr, oneShot = false;
var voices, durs, pbinds, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
pbinds = voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs, attacks, rels, amps;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
//attacks = 2.collect({rrand(1, 3)}) ++ freqs.drop(2).collect({rrand(0.3, 0.5)});
attacks = fDurs.collect({arg dur; dur * rrand(0.2, 0.4)});
//rels = freqs.drop(2).collect({rrand(0.3, 0.5)}) ++ 2.collect({rrand(1, 3)});
rels = (clumps.size - 1).collect({arg c;
if(clumps[c + 1][0] == ["Rest"], {rrand(1, 3)}, {rrand(0.3, 0.5)});
});
amps = freqs.collect({rrand(0.6, 0.99)});
[
Pbind(
\instrument, \string_model,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\attack, Pseq(attacks, 1),
\sustain, Pseq(fDurs, 1),
\release, Pseq(rels, 1),
//\amp, Pseq(amps, 1),
\amp, Pbrown(0.5, 1, 0.5),
\busIndex, v
),
Pbind(
\instrument, \sine,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
)
]
}).flatten;
if(oneShot.not, {
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
pbinds = pbinds ++
[
Pbind(
\type, \osc,
\addr, addr,
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
});
res = Ppar(pbinds);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
//res.postln;
if(res.every({arg char; char.isDecDigit}), {res = res.asInteger});
});
});
res
};
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict, skipLastXChanges = false;
var tmpLastXChanges;
tmpLastXChanges = lastXChanges.deepCopy;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
if(skipLastXChanges, {lastXChanges = tmpLastXChanges});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
saveLedger = {arg ledger, path;
var file;
file = File(path, "w");
file.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
file.close;
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
~group = group;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\save_ledger, {arg msg, time, addr, port;
msg.postln;
ledger = msgInterpret.value(msg[1].asString.parseJSON["ledger"], false).postln;
//loadLedgerJSON.value(msg[0])
saveLedger.value(ledger, msg[2].asString);
//loadLedgerFile.value(msg[1].asString);
}, \save_ledger);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict, true);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
if(refUID == nil, {lastXChanges = [initVoices.value().deepCopy]});
if((refUID != nil) && (refUID != "tmp"), {
var file;
file = File((dir +/+ ".." +/+ "resources" +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
lastXChanges = msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
refUID.postln;
lastXChanges.collect({arg item; item.postln});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
seq = seedFunc.value(genMotif, motifSeed).value;
lastXChanges.collect({arg item; item.postln});
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
addr.sendMsg("/generated", path, modelString, ledgerPath);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
musicChanged = (musicData != seq).postln;
curUID = genUID.value;
File.mkdir((dir +/+ ".." +/+ "resources" +/+ curUID).standardizePath);
File.copy(exPath, (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {
seq = musicData;
dict["music_data"] = seq;
dict["motif_edited"] = "true"
});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
ledger = ledger.drop(-1).add(curUID);
saveLedger.value(ledger, ledgerPath);
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
group.set(\release, 2);
group.set(\gate, 0);
player.stop;
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
if(msg[1] == 1, {
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (dir +/+ ".." +/+ "resources" +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.postln.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
}, {
pSeq = [loadModelJSON.value(msg[2].asString.parseJSON)["music_data"].postln];
patterns = genPatterns.value(pSeq, addr, true);
});
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
addr.sendMsg("/one_shot", 0);
});
player = player.play
});
}, \transport);
)
(
//synthdefs
~stringModelBusArray = 4.collect({Bus.audio(s, 1)});
~sineBusArray = 4.collect({Bus.audio(s, 1)});
~bassBusArray = 1.collect({Bus.audio(s, 1)});
~hdustBusArray = 1.collect({Bus.audio(s, 1)});
~samplerBusArray = 2.collect({Bus.audio(s, 1)});
~sBuf = Buffer.alloc(s, 10, 2);
SynthDef(\string_model, {arg freq, gate = 1, sustain, amp, dur, attack, release = 1, busIndex = 0;
var trig, exc, sig1, sig2, noHarms;
noHarms = 30;
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.8).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar(Select.kr(busIndex, ~stringModelBusArray), sig1 * amp * EnvGen.kr(Env.adsr(attack, 0.3, 0.9, release, 0.9, -3), gate, doneAction: 2));
//Out.ar([0, 1], sig1 * EnvGen.kr(Env.asr(dur, 0.3, 1), gate, doneAction: 2));
}).add;
SynthDef(\sine, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var sig;
sig = SinOsc.ar(freq);
Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.asr(0.3, 0.4, 0.3), gate, timeScale: dur, doneAction: 2));
//Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.sine(dur), gate, doneAction: 2));
}).add;
SynthDef(\mixer, {arg freq, gate = 1, sustain, amp, dur, out;
var nameSpaces, sigs, hierarchicalDust;
hierarchicalDust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1))
);
sigs = [~stringModelBusArray, ~sineBusArray, ~bassBusArray, ~hdustBusArray, ~samplerBusArray].collect({arg busArray, i;
var nameSpace, sig;
nameSpace = ['string', 'sine', 'bass', 'hdust', 'sampler'][i];
sig = busArray.collect({arg bus, c; In.ar(bus, 1) * NamedControl.kr(\ ++ nameSpace ++ '_volume_' ++ c, 1, 0.1)});
sig = sig.collect({arg channel, c; Pan2.ar(channel, NamedControl.kr(\ ++ nameSpace ++ '_pan_' ++ c, i / (busArray.size - 1), 0.1) * 2 - 1)});
sig = sig.collect({arg channel, c; channel * NamedControl.kr(\ ++ nameSpace ++ '_mute_' ++ c, 1, 0.1)});
sig = Mix.ar(sig) * pow(NamedControl.kr(\ ++ nameSpace ++ '_volume_master', 1, 0.1), 2);
});
sigs = Mix.ar(sigs / 4);
Out.ar(0, sigs)
}).add;
SynthDef(\bass, {
var switches, drone;
switches = {|i| Dust.kr(0.1)} ! 9;
drone = {|i| var harm = pow(2, 2 - (i / 3).trunc), amp = (1 / pow(harm, 2));
SinOsc.ar(60 * harm + TRand.kr(-3, 3, switches[i]), 0, amp)} ! 9;
Out.ar(~bassBusArray[0], Mix.new(drone) * 0.2);
}).add;
SynthDef(\sampler, {
Out.ar(~samplerBusArray, PlayBuf.ar(2, ~sBuf, BufRateScale.kr(~sBuf), doneAction: 2))
}).add;
// main routine
SynthDef(\hdust, {
arg gate = 0;
var hierarchical_dust, low_sine, high_sine, brown_noise, white_noise;
// this triggers the combinations of sources
// it is similar to the Supercollider UGen called dust but with a hierarchical structure
hierarchical_dust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1)) *
TIRand.kr(0, 1, Impulse.kr(0.1))
);
// adjust the multiplier at the end of each line for adjusting levels
// note with each trigger, each source has a 1 in 3 chance of sounding
low_sine = SinOsc.ar(76.midicps / 16) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.1;
high_sine = SinOsc.ar(76.midicps * 8) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.01;
brown_noise = BrownNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.025;
white_noise = WhiteNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.02;
Out.ar(~hdustBusArray[0],
((low_sine + high_sine + brown_noise + white_noise) )
);
}).add;
)
(
var bass, hdust, sampler, mixer;
bass = Synth.tail(~group, \bass);
hdust = Synth.tail(~group, \hdust);
sampler = Synth.head(~group, \sampler);
mixer = Synth.tail(~group, \mixer);
OSCdef(\mixer, {arg msg, time, addr, port;
mixer.set((msg[1] ++ '_' ++ msg[2] ++ '_' ++ msg[3]), msg[4])
}, \mixer);
OSCdef(\sampler, {arg msg, time, addr, port;
msg.postln;
sampler.free;
~sBuf.free;
~sBuf = Buffer.read(s, msg[1].asString.postln, action: {sampler = Synth.head(~group, \sampler)});
}, \sampler);
)
/* old something
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
*/

@ -1,59 +0,0 @@
{
"music_data":
[
[
[
[ [ [ "Rest" ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ] ], 2.375 ],
[ [ [ "Rest" ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, -1, 0, 0, 0 ] ], 1.625 ],
[ [ [ "Rest" ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, -1, 0 ] ], 1.125 ],
[ [ [ "Rest" ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ], [ -1, 1, 0, 0, 0, 0 ] ], 3 ]
],
[
[ [ [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ -1, 1, 0, 0, 0, 0 ] ], 3.125 ],
[ [ [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ -1, 1, 0, 0, 0, 0 ] ], 1.125 ],
[ [ [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ -1, 0, 0, 0, 0, 1 ] ], 1.25 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ -1, 0, 0, 0, 0, 1 ] ], 1.375 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 0, 0, -1, 0, 0, 0 ] ], 1.5 ],
[ [ [ 0, -1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 0, 0, -1, 0, 0, 0 ] ], 0.875 ],
[ [ [ -1, 0, 1, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 0, 0, -1, 0, 0, 0 ] ], 2 ]
],
[
[ [ [ -1, 0, 1, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ "Rest" ] ], 1 ],
[ [ [ -1, 0, 1, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 1, 0, 0, 0 ], [ "Rest" ] ], 1.375 ],
[ [ [ -1, 0, 1, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0, -1 ], [ "Rest" ] ], 1.75 ],
[ [ [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0, -1 ], [ "Rest" ] ], 1.375 ],
[ [ [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ] ], 1.625 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 2.375 ]
]
]
],
"last_changes":
[
[ [ -1, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 0, 0, -1, 0, 0, 0 ] ],
[ [ 0, -1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 0, 0, -1, 0, 0, 0 ] ],
[ [ -1, 0, 1, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 0, 0, -1, 0, 0, 0 ] ],
[ [ -1, 0, 1, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 1, 0, 0, 0 ], [ 0, 0, -1, 0, 0, 0 ] ],
[ [ -1, 0, 1, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0, -1 ], [ 0, 0, -1, 0, 0, 0 ] ]
],
"cur_uid": "46210507",
"ref_uid": "nil",
"order_seed": 944186,
"dur_seed": 424778,
"motifs_seed": 515266,
"entrances_probs_vals": [ 0, 0.48, 1.7460317460317, 0.58, 1.68, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"passages_probs_vals": [ 0, 0.48, 1.7460317460317, 0.58, 1.68, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"exits_probs_vals": [ 0, 0.48, 1.7460317460317, 0.58, 1.68, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"ranges": [ [ -3600, -627.86377708978 ], [ -3600, 260 ], [ -980.80495356037, 2066 ], [ -627.86377708978, 2053 ] ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"order":
[
[ [ 2 ], [ 3, 3, 3 ], [ 0, 1 ] ],
[ [ 1 ], [ 2, 3, 0, 3, 0, 0 ], [ ] ],
[ [ 0, 1 ], [ 2, 2 ], [ 3 ] ]
],
"sus_weights": [ 0.73, 0.49, 0 ],
"order_size": [ 1, 7 ],
"passages_size": [ 0, 5 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,907 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict, saveLedger;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold.deepCopy});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~msg.postln;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr, oneShot = false;
var voices, durs, pbinds, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
pbinds = voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs, attacks, rels, amps;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
//attacks = 2.collect({rrand(1, 3)}) ++ freqs.drop(2).collect({rrand(0.3, 0.5)});
attacks = fDurs.collect({arg dur; dur * rrand(0.2, 0.4)});
//rels = freqs.drop(2).collect({rrand(0.3, 0.5)}) ++ 2.collect({rrand(1, 3)});
rels = (clumps.size - 1).collect({arg c;
if(clumps[c + 1][0] == ["Rest"], {rrand(1, 3)}, {rrand(0.3, 0.5)});
});
amps = freqs.collect({rrand(0.6, 0.99)});
[
Pbind(
\instrument, \string_model,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\attack, Pseq(attacks, 1),
\sustain, Pseq(fDurs, 1),
\release, Pseq(rels, 1),
//\amp, Pseq(amps, 1),
\amp, Pbrown(0.5, 1, 0.5),
\busIndex, v
),
Pbind(
\instrument, \sine,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
)
]
}).flatten;
if(oneShot.not, {
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
pbinds = pbinds ++
[
Pbind(
\type, \osc,
\addr, addr,
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
});
res = Ppar(pbinds);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
//res.postln;
if(res.every({arg char; char.isDecDigit}), {res = res.asInteger});
});
});
res
};
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict, skipLastXChanges = false;
var tmpLastXChanges;
tmpLastXChanges = lastXChanges.deepCopy;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
if(skipLastXChanges, {lastXChanges = tmpLastXChanges});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
saveLedger = {arg ledger, path;
var file;
file = File(path, "w");
file.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
file.close;
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
~group = group;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\save_ledger, {arg msg, time, addr, port;
msg.postln;
ledger = msgInterpret.value(msg[1].asString.parseJSON["ledger"], false).postln;
//loadLedgerJSON.value(msg[0])
saveLedger.value(ledger, msg[2].asString);
//loadLedgerFile.value(msg[1].asString);
}, \save_ledger);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict, true);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
if(refUID == nil, {lastXChanges = [initVoices.value().deepCopy]});
if((refUID != nil) && (refUID != "tmp"), {
var file;
file = File((dir +/+ ".." +/+ "resources" +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
lastXChanges = msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
refUID.postln;
lastXChanges.collect({arg item; item.postln});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
seq = seedFunc.value(genMotif, motifSeed).value;
lastXChanges.collect({arg item; item.postln});
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
addr.sendMsg("/generated", path, modelString, ledgerPath);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
musicChanged = (musicData != seq).postln;
curUID = genUID.value;
File.mkdir((dir +/+ ".." +/+ "resources" +/+ curUID).standardizePath);
File.copy(exPath, (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {
seq = musicData;
dict["music_data"] = seq;
dict["motif_edited"] = "true"
});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
ledger = ledger.drop(-1).add(curUID);
saveLedger.value(ledger, ledgerPath);
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
group.set(\release, 2);
group.set(\gate, 0);
player.stop;
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
if(msg[1] == 1, {
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (dir +/+ ".." +/+ "resources" +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.postln.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
}, {
pSeq = [loadModelJSON.value(msg[2].asString.parseJSON)["music_data"].postln];
patterns = genPatterns.value(pSeq, addr, true);
});
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
addr.sendMsg("/one_shot", 0);
});
player = player.play
});
}, \transport);
)
(
//synthdefs
~stringModelBusArray = 4.collect({Bus.audio(s, 1)});
~sineBusArray = 4.collect({Bus.audio(s, 1)});
~bassBusArray = 1.collect({Bus.audio(s, 1)});
~hdustBusArray = 1.collect({Bus.audio(s, 1)});
~samplerBusArray = 2.collect({Bus.audio(s, 1)});
~sBuf = Buffer.alloc(s, 10, 2);
SynthDef(\string_model, {arg freq, gate = 1, sustain, amp, dur, attack, release = 1, busIndex = 0;
var trig, exc, sig1, sig2, noHarms;
noHarms = rrand(20, 40);
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.8).softclip;
//sig1 = HPF.ar(sig1, 300);
Out.ar(Select.kr(busIndex, ~stringModelBusArray), sig1 * amp * EnvGen.kr(Env.adsr(attack, 0.3, 0.9, release, 0.9, -3), gate, doneAction: 2));
//Out.ar([0, 1], sig1 * EnvGen.kr(Env.asr(dur, 0.3, 1), gate, doneAction: 2));
}).add;
SynthDef(\sine, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var sig;
sig = SinOsc.ar(freq);
Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.asr(0.3, 0.4, 0.3), gate, timeScale: dur, doneAction: 2));
//Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.sine(dur), gate, doneAction: 2));
}).add;
SynthDef(\mixer, {arg freq, gate = 1, sustain, amp, dur, out;
var nameSpaces, sigs, hierarchicalDust;
hierarchicalDust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1))
);
sigs = [~stringModelBusArray, ~sineBusArray, ~bassBusArray, ~hdustBusArray, ~samplerBusArray].collect({arg busArray, i;
var nameSpace, sig;
nameSpace = ['string', 'sine', 'bass', 'hdust', 'sampler'][i];
sig = busArray.collect({arg bus, c; In.ar(bus, 1) * NamedControl.kr(\ ++ nameSpace ++ '_volume_' ++ c, 1, 0.1)});
sig = sig.collect({arg channel, c; Pan2.ar(channel, NamedControl.kr(\ ++ nameSpace ++ '_pan_' ++ c, i / (busArray.size - 1), 0.1) * 2 - 1)});
sig = sig.collect({arg channel, c; channel * NamedControl.kr(\ ++ nameSpace ++ '_mute_' ++ c, 1, 0.1)});
sig = Mix.ar(sig) * pow(NamedControl.kr(\ ++ nameSpace ++ '_volume_master', 1, 0.1), 2);
});
sigs = Mix.ar(sigs / 4);
Out.ar(0, sigs)
}).add;
SynthDef(\bass, {
var switches, drone;
switches = {|i| Dust.kr(0.1)} ! 9;
drone = {|i| var harm = pow(2, 2 - (i / 3).trunc), amp = (1 / pow(harm, 2));
SinOsc.ar(60 * harm + TRand.kr(-3, 3, switches[i]), 0, amp)} ! 9;
Out.ar(~bassBusArray[0], Mix.new(drone) * 0.2);
}).add;
SynthDef(\sampler, {
Out.ar(~samplerBusArray, PlayBuf.ar(2, ~sBuf, BufRateScale.kr(~sBuf), doneAction: 2))
}).add;
// main routine
SynthDef(\hdust, {
arg gate = 0;
var hierarchical_dust, low_sine, high_sine, brown_noise, white_noise;
// this triggers the combinations of sources
// it is similar to the Supercollider UGen called dust but with a hierarchical structure
hierarchical_dust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1)) *
TIRand.kr(0, 1, Impulse.kr(0.1))
);
// adjust the multiplier at the end of each line for adjusting levels
// note with each trigger, each source has a 1 in 3 chance of sounding
low_sine = SinOsc.ar(76.midicps / 16) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.1;
high_sine = SinOsc.ar(76.midicps * 8) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.01;
brown_noise = BrownNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.025;
white_noise = WhiteNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.02;
Out.ar(~hdustBusArray[0],
((low_sine + high_sine + brown_noise + white_noise) )
);
}).add;
)
(
var bass, hdust, sampler, mixer;
bass = Synth.tail(~group, \bass);
hdust = Synth.tail(~group, \hdust);
sampler = Synth.head(~group, \sampler);
mixer = Synth.tail(~group, \mixer);
OSCdef(\mixer, {arg msg, time, addr, port;
mixer.set((msg[1] ++ '_' ++ msg[2] ++ '_' ++ msg[3]), msg[4])
}, \mixer);
OSCdef(\sampler, {arg msg, time, addr, port;
msg.postln;
sampler.free;
~sBuf.free;
~sBuf = Buffer.read(s, msg[1].asString.postln, action: {sampler = Synth.head(~group, \sampler)});
}, \sampler);
)
/* old something
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
*/

@ -1,48 +0,0 @@
{
"music_data":
[
[
[
[ [ [ "Rest" ], [ "Rest" ], [ 7, -7, -4, 0, -1, -2 ], [ "Rest" ] ], 2.25 ],
[ [ [ "Rest" ], [ "Rest" ], [ 7, -7, -4, 0, -1, -2 ], [ 7, -5, -2, 0, -1, -2 ] ], 3.875 ],
[ [ [ 8, -7, -4, 1, -1, -2 ], [ "Rest" ], [ 7, -7, -4, 0, -1, -2 ], [ 7, -5, -2, 0, -1, -2 ] ], 1.125 ],
[ [ [ 8, -7, -4, 1, -1, -2 ], [ 7, -8, -4, 0, -1, -2 ], [ 7, -7, -4, 0, -1, -2 ], [ 7, -5, -2, 0, -1, -2 ] ], 6.5 ]
],
[
[ [ [ 8, -7, -4, 1, -1, -2 ], [ "Rest" ], [ 7, -7, -4, 0, -1, -2 ], [ 7, -5, -2, 0, -1, -2 ] ], 2 ],
[ [ [ 8, -7, -4, 1, -1, -2 ], [ "Rest" ], [ 6, -7, -3, 1, -1, -2 ], [ 7, -5, -2, 0, -1, -2 ] ], 7.125 ],
[ [ [ "Rest" ], [ "Rest" ], [ 6, -7, -3, 1, -1, -2 ], [ 7, -5, -2, 0, -1, -2 ] ], 0 ],
[ [ [ "Rest" ], [ "Rest" ], [ 6, -7, -3, 1, -1, -2 ], [ "Rest" ] ], 1.625 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 5.5 ]
]
]
],
"last_changes":
[
[ [ 8, -6, -4, 0, -1, -2 ], [ 6, -5, -4, 0, -1, -3 ], [ 6, -5, -4, 0, -1, -2 ], [ 7, -5, -2, 0, -1, -2 ] ],
[ [ 8, -6, -4, 0, -1, -2 ], [ 6, -5, -4, 0, -1, -3 ], [ 7, -7, -4, 0, -1, -2 ], [ 7, -5, -2, 0, -1, -2 ] ],
[ [ 8, -7, -4, 1, -1, -2 ], [ 6, -5, -4, 0, -1, -3 ], [ 7, -7, -4, 0, -1, -2 ], [ 7, -5, -2, 0, -1, -2 ] ],
[ [ 8, -7, -4, 1, -1, -2 ], [ 7, -8, -4, 0, -1, -2 ], [ 7, -7, -4, 0, -1, -2 ], [ 7, -5, -2, 0, -1, -2 ] ],
[ [ 8, -7, -4, 1, -1, -2 ], [ 7, -8, -4, 0, -1, -2 ], [ 6, -7, -3, 1, -1, -2 ], [ 7, -5, -2, 0, -1, -2 ] ]
],
"cur_uid": "4b96f62d",
"ref_uid": "703e109b",
"order_seed": 550421,
"dur_seed": 808010,
"motifs_seed": 180604,
"entrances_probs_vals": [ 0.52, 0.56, 5.1984126984127, 0.76923076923077, 2.31, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"passages_probs_vals": [ 0.52, 0.56, 5.1984126984127, 0.76923076923077, 2.31, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"exits_probs_vals": [ 0.52, 0.56, 5.1984126984127, 0.76923076923077, 2.31, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"ranges": [ [ 1211.1455108359, 2400 ], [ -1650, 450 ], [ -405, 1137 ], [ 951.08359133127, 2053 ] ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"order":
[
[ [ 2, 3 ], [ 0, 1 ], [ ] ],
[ [ 3, 0 ], [ 2 ], [ 1 ] ]
],
"sus_weights": [ 0.73, 0.49, 0 ],
"order_size": [ 1, 3 ],
"passages_size": [ 0, 0 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,887 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict, saveLedger;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~msg.postln;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr, oneShot = false;
var voices, durs, pbinds, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
pbinds = voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
[
Pbind(
\instrument, \string_model,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
),
Pbind(
\instrument, \sine,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
)
]
}).flatten;
if(oneShot.not, {
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
pbinds = pbinds ++
[
Pbind(
\type, \osc,
\addr, addr,
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
});
res = Ppar(pbinds);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
//res.postln;
if(res.every({arg char; char.isDecDigit}), {res = res.asInteger});
});
});
res
};
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
saveLedger = {arg ledger, path;
var file;
file = File(path, "w");
file.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
file.close;
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
~group = group;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\save_ledger, {arg msg, time, addr, port;
msg.postln;
ledger = msgInterpret.value(msg[1].asString.parseJSON["ledger"], false).postln;
//loadLedgerJSON.value(msg[0])
saveLedger.value(ledger, msg[2].asString);
//loadLedgerFile.value(msg[1].asString);
}, \save_ledger);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
lastXChanges = if(refUID == nil, {
[initVoices.value().deepCopy];
}, {
var file;
file = File((dir +/+ ".." +/+ "resources" +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
seq = seedFunc.value(genMotif, motifSeed).value;
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
addr.sendMsg("/generated", path, modelString, ledgerPath);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
musicChanged = (musicData != seq).postln;
curUID = genUID.value;
File.mkdir((dir +/+ ".." +/+ "resources" +/+ curUID).standardizePath);
File.copy(exPath, (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {
seq = musicData;
dict["music_data"] = seq;
dict["motif_edited"] = "true"
});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
ledger = ledger.drop(-1).add(curUID);
saveLedger.value(ledger, ledgerPath);
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
player.stop;
group.set(\gate, 0);
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
if(msg[1] == 1, {
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (dir +/+ ".." +/+ "resources" +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.postln.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
}, {
pSeq = [loadModelJSON.value(msg[2].asString.parseJSON)["music_data"].postln];
patterns = genPatterns.value(pSeq, addr, true);
});
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
addr.sendMsg("/one_shot", 0);
});
player = player.play
});
}, \transport);
)
(
//synthdefs
~stringModelBusArray = 4.collect({Bus.audio(s, 1)});
~sineBusArray = 4.collect({Bus.audio(s, 1)});
~bassBusArray = 1.collect({Bus.audio(s, 1)});
~hdustBusArray = 1.collect({Bus.audio(s, 1)});
~samplerBusArray = 2.collect({Bus.audio(s, 1)});
~sBuf = Buffer.alloc(s, 10, 2);
SynthDef(\string_model, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var trig, exc, sig1, sig2, noHarms;
noHarms = 30;
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.8).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar(Select.kr(busIndex, ~stringModelBusArray), sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
//Out.ar([0, 1], sig1 * EnvGen.kr(Env.asr(dur, 0.3, 1), gate, doneAction: 2));
}).add;
SynthDef(\sine, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var sig;
sig = SinOsc.ar(freq);
Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.asr(0.3, 0.4, 0.3), gate, timeScale: dur, doneAction: 2));
}).add;
SynthDef(\mixer, {arg freq, gate = 1, sustain, amp, dur, out;
var nameSpaces, sigs, hierarchicalDust;
hierarchicalDust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1))
);
sigs = [~stringModelBusArray, ~sineBusArray, ~bassBusArray, ~hdustBusArray, ~samplerBusArray].collect({arg busArray, i;
var nameSpace, sig;
nameSpace = ['string', 'sine', 'bass', 'hdust', 'sampler'][i];
sig = busArray.collect({arg bus, c; In.ar(bus, 1) * NamedControl.kr(\ ++ nameSpace ++ '_volume_' ++ c, 1, 0.1)});
sig = sig.collect({arg channel, c; Pan2.ar(channel, NamedControl.kr(\ ++ nameSpace ++ '_pan_' ++ c, i / (busArray.size - 1), 0.1) * 2 - 1)});
sig = sig.collect({arg channel, c; channel * NamedControl.kr(\ ++ nameSpace ++ '_mute_' ++ c, 1, 0.1)});
sig = Mix.ar(sig) * pow(NamedControl.kr(\ ++ nameSpace ++ '_volume_master', 1, 0.1), 2);
});
sigs = Mix.ar(sigs / 4);
Out.ar(0, sigs)
}).add;
SynthDef(\bass, {
var switches, drone;
switches = {|i| Dust.kr(0.1)} ! 9;
drone = {|i| var harm = pow(2, 2 - (i / 3).trunc), amp = (1 / pow(harm, 2));
SinOsc.ar(60 * harm + TRand.kr(-3, 3, switches[i]), 0, amp)} ! 9;
Out.ar(~bassBusArray[0], Mix.new(drone) * 0.2);
}).add;
SynthDef(\sampler, {
Out.ar(~samplerBusArray, PlayBuf.ar(2, ~sBuf, BufRateScale.kr(~sBuf), doneAction: 2))
}).add;
// main routine
SynthDef(\hdust, {
arg gate = 0;
var hierarchical_dust, low_sine, high_sine, brown_noise, white_noise;
// this triggers the combinations of sources
// it is similar to the Supercollider UGen called dust but with a hierarchical structure
hierarchical_dust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1)) *
TIRand.kr(0, 1, Impulse.kr(0.1))
);
// adjust the multiplier at the end of each line for adjusting levels
// note with each trigger, each source has a 1 in 3 chance of sounding
low_sine = SinOsc.ar(76.midicps / 16) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.1;
high_sine = SinOsc.ar(76.midicps * 8) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.01;
brown_noise = BrownNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.025;
white_noise = WhiteNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.02;
Out.ar(~hdustBusArray[0],
((low_sine + high_sine + brown_noise + white_noise) )
);
}).add;
)
(
var bass, hdust, sampler, mixer;
bass = Synth.tail(~group, \bass);
hdust = Synth.tail(~group, \hdust);
sampler = Synth.head(~group, \sampler);
mixer = Synth.tail(~group, \mixer);
OSCdef(\mixer, {arg msg, time, addr, port;
mixer.set((msg[1] ++ '_' ++ msg[2] ++ '_' ++ msg[3]), msg[4])
}, \mixer);
OSCdef(\sampler, {arg msg, time, addr, port;
msg.postln;
sampler.free;
~sBuf.free;
~sBuf = Buffer.read(s, msg[1].asString.postln, action: {sampler = Synth.head(~group, \sampler)});
}, \sampler);
)
/* old something
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
*/

@ -1,51 +0,0 @@
{
"music_data":
[
[
[
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 1 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ], [ 0, 1, 0, 0, 0, 0 ] ], 4.5 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0, -1 ], [ "Rest" ], [ 0, 1, 0, 0, 0, 0 ] ], 0.75 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -1, 1, 0, 0, 0, 1 ], [ "Rest" ], [ 0, 1, 0, 0, 0, 0 ] ], 4.25 ]
],
[
[ [ [ 0, 1, 0, 0, 0, -1 ], [ -1, 1, 0, 0, 0, 1 ], [ "Rest" ], [ 0, 1, 0, 0, 0, 0 ] ], 0.75 ],
[ [ [ 0, 1, 0, 0, 0, -1 ], [ -1, 1, 0, 1, 0, 0 ], [ "Rest" ], [ 0, 1, 0, 0, 0, 0 ] ], 0.625 ],
[ [ [ 0, 1, 0, 0, 0, -1 ], [ -1, 1, 0, 1, 0, 0 ], [ 0, 1, 1, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ] ], 0.75 ],
[ [ [ -1, 1, 0, 0, 1, 0 ], [ -1, 1, 0, 1, 0, 0 ], [ 0, 1, 1, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ] ], 4.875 ],
[ [ [ -1, 1, 0, 0, 1, 0 ], [ "Rest" ], [ 0, 1, 1, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ] ], 1 ],
[ [ [ -1, 1, 0, 0, 1, 0 ], [ "Rest" ], [ 0, 1, 1, 0, 0, 0 ], [ "Rest" ] ], 1 ],
[ [ [ "Rest" ], [ "Rest" ], [ 0, 1, 1, 0, 0, 0 ], [ "Rest" ] ], 1 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 3.125 ]
]
]
],
"last_changes":
[
[ [ 0, 0, 0, 0, 0, 0 ], [ -1, 1, 0, 0, 0, 1 ], [ 1, -1, 1, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ] ],
[ [ 0, 1, 0, 0, 0, -1 ], [ -1, 1, 0, 0, 0, 1 ], [ 1, -1, 1, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ] ],
[ [ 0, 1, 0, 0, 0, -1 ], [ -1, 1, 0, 1, 0, 0 ], [ 1, -1, 1, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ] ],
[ [ 0, 1, 0, 0, 0, -1 ], [ -1, 1, 0, 1, 0, 0 ], [ 0, 1, 1, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ] ],
[ [ -1, 1, 0, 0, 1, 0 ], [ -1, 1, 0, 1, 0, 0 ], [ 0, 1, 1, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ] ]
],
"cur_uid": "50fec831",
"ref_uid": "7264e3ac",
"order_seed": 982967,
"dur_seed": 762088,
"motifs_seed": 404006,
"entrances_probs_vals": [ 0, 1.67, 4.8015873015873, 0.24725274725275, 1.15, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"passages_probs_vals": [ 0, 1.67, 4.8015873015873, 0.24725274725275, 1.15, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"exits_probs_vals": [ 0, 1.67, 4.8015873015873, 0.24725274725275, 1.15, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"ranges": [ [ -1100, 460.68111455108 ], [ -507, 1084.8297213622 ], [ 884.21052631579, 2237 ], [ 1040.2476780186, 2053 ] ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"order":
[
[ [ 0, 3 ], [ 1, 1 ], [ 2 ] ],
[ [ 3 ], [ 0, 1, 2, 0 ], [ ] ]
],
"sus_weights": [ 0.73, 0.23, 0 ],
"order_size": [ 1, 3 ],
"passages_size": [ 0, 2 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,887 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict, saveLedger;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~msg.postln;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr, oneShot = false;
var voices, durs, pbinds, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
pbinds = voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
[
Pbind(
\instrument, \string_model,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
),
Pbind(
\instrument, \sine,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
)
]
}).flatten;
if(oneShot.not, {
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
pbinds = pbinds ++
[
Pbind(
\type, \osc,
\addr, addr,
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
});
res = Ppar(pbinds);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
//res.postln;
if(res.every({arg char; char.isDecDigit}), {res = res.asInteger});
});
});
res
};
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
saveLedger = {arg ledger, path;
var file;
file = File(path, "w");
file.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
file.close;
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
~group = group;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\save_ledger, {arg msg, time, addr, port;
msg.postln;
ledger = msgInterpret.value(msg[1].asString.parseJSON["ledger"], false).postln;
//loadLedgerJSON.value(msg[0])
saveLedger.value(ledger, msg[2].asString);
//loadLedgerFile.value(msg[1].asString);
}, \save_ledger);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
lastXChanges = if(refUID == nil, {
[initVoices.value().deepCopy];
}, {
var file;
file = File((dir +/+ ".." +/+ "resources" +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
seq = seedFunc.value(genMotif, motifSeed).value;
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
addr.sendMsg("/generated", path, modelString, ledgerPath);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
musicChanged = (musicData != seq).postln;
curUID = genUID.value;
File.mkdir((dir +/+ ".." +/+ "resources" +/+ curUID).standardizePath);
File.copy(exPath, (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {
seq = musicData;
dict["music_data"] = seq;
dict["motif_edited"] = "true"
});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
ledger = ledger.drop(-1).add(curUID);
saveLedger.value(ledger, ledgerPath);
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
player.stop;
group.set(\gate, 0);
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
if(msg[1] == 1, {
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (dir +/+ ".." +/+ "resources" +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.postln.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
}, {
pSeq = [loadModelJSON.value(msg[2].asString.parseJSON)["music_data"].postln];
patterns = genPatterns.value(pSeq, addr, true);
});
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
addr.sendMsg("/one_shot", 0);
});
player = player.play
});
}, \transport);
)
(
//synthdefs
~stringModelBusArray = 4.collect({Bus.audio(s, 1)});
~sineBusArray = 4.collect({Bus.audio(s, 1)});
~bassBusArray = 1.collect({Bus.audio(s, 1)});
~hdustBusArray = 1.collect({Bus.audio(s, 1)});
~samplerBusArray = 2.collect({Bus.audio(s, 1)});
~sBuf = Buffer.alloc(s, 10, 2);
SynthDef(\string_model, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var trig, exc, sig1, sig2, noHarms;
noHarms = 30;
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.8).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar(Select.kr(busIndex, ~stringModelBusArray), sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
//Out.ar([0, 1], sig1 * EnvGen.kr(Env.asr(dur, 0.3, 1), gate, doneAction: 2));
}).add;
SynthDef(\sine, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var sig;
sig = SinOsc.ar(freq);
Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.asr(0.3, 0.4, 0.3), gate, timeScale: dur, doneAction: 2));
}).add;
SynthDef(\mixer, {arg freq, gate = 1, sustain, amp, dur, out;
var nameSpaces, sigs, hierarchicalDust;
hierarchicalDust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1))
);
sigs = [~stringModelBusArray, ~sineBusArray, ~bassBusArray, ~hdustBusArray, ~samplerBusArray].collect({arg busArray, i;
var nameSpace, sig;
nameSpace = ['string', 'sine', 'bass', 'hdust', 'sampler'][i];
sig = busArray.collect({arg bus, c; In.ar(bus, 1) * NamedControl.kr(\ ++ nameSpace ++ '_volume_' ++ c, 1, 0.1)});
sig = sig.collect({arg channel, c; Pan2.ar(channel, NamedControl.kr(\ ++ nameSpace ++ '_pan_' ++ c, i / (busArray.size - 1), 0.1) * 2 - 1)});
sig = sig.collect({arg channel, c; channel * NamedControl.kr(\ ++ nameSpace ++ '_mute_' ++ c, 1, 0.1)});
sig = Mix.ar(sig) * pow(NamedControl.kr(\ ++ nameSpace ++ '_volume_master', 1, 0.1), 2);
});
sigs = Mix.ar(sigs / 4);
Out.ar(0, sigs)
}).add;
SynthDef(\bass, {
var switches, drone;
switches = {|i| Dust.kr(0.1)} ! 9;
drone = {|i| var harm = pow(2, 2 - (i / 3).trunc), amp = (1 / pow(harm, 2));
SinOsc.ar(60 * harm + TRand.kr(-3, 3, switches[i]), 0, amp)} ! 9;
Out.ar(~bassBusArray[0], Mix.new(drone) * 0.2);
}).add;
SynthDef(\sampler, {
Out.ar(~samplerBusArray, PlayBuf.ar(2, ~sBuf, BufRateScale.kr(~sBuf), doneAction: 2))
}).add;
// main routine
SynthDef(\hdust, {
arg gate = 0;
var hierarchical_dust, low_sine, high_sine, brown_noise, white_noise;
// this triggers the combinations of sources
// it is similar to the Supercollider UGen called dust but with a hierarchical structure
hierarchical_dust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1)) *
TIRand.kr(0, 1, Impulse.kr(0.1))
);
// adjust the multiplier at the end of each line for adjusting levels
// note with each trigger, each source has a 1 in 3 chance of sounding
low_sine = SinOsc.ar(76.midicps / 16) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.1;
high_sine = SinOsc.ar(76.midicps * 8) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.01;
brown_noise = BrownNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.025;
white_noise = WhiteNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.02;
Out.ar(~hdustBusArray[0],
((low_sine + high_sine + brown_noise + white_noise) )
);
}).add;
)
(
var bass, hdust, sampler, mixer;
bass = Synth.tail(~group, \bass);
hdust = Synth.tail(~group, \hdust);
sampler = Synth.head(~group, \sampler);
mixer = Synth.tail(~group, \mixer);
OSCdef(\mixer, {arg msg, time, addr, port;
mixer.set((msg[1] ++ '_' ++ msg[2] ++ '_' ++ msg[3]), msg[4])
}, \mixer);
OSCdef(\sampler, {arg msg, time, addr, port;
msg.postln;
sampler.free;
~sBuf.free;
~sBuf = Buffer.read(s, msg[1].asString.postln, action: {sampler = Synth.head(~group, \sampler)});
}, \sampler);
)
/* old something
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
*/

@ -1,43 +0,0 @@
{
"music_data":
[
[
[
[ [ [ "Rest" ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ] ], 2.25 ],
[ [ [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ] ], 1.75 ],
[ [ [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 3.125 ],
[ [ [ 2, 0, 0, -1, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 2.625 ],
[ [ [ 2, -1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 3 ],
[ [ [ 2, -1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ] ], 1.25 ],
[ [ [ 2, -1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ] ], 2.75 ],
[ [ [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ] ], 2.75 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 1.5 ]
]
]
],
"last_changes":
[
[ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ],
[ [ 2, 0, 0, -1, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ],
[ [ 2, -1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ]
],
"cur_uid": "5267b235",
"ref_uid": "nil",
"order_seed": 474005,
"dur_seed": 826425,
"motifs_seed": 116157,
"entrances_probs_vals": [ 0, 0, 1.984126984127, 0, 3.0769230769231, 0, 0.64527027027027, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"passages_probs_vals": [ 0, 0, 1.984126984127, 0, 3.0769230769231, 0, 0.64527027027027, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"exits_probs_vals": [ 0, 0, 1.984126984127, 0, 3.0769230769231, 0, 0.64527027027027, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"ranges": [ [ 1419.1950464396, 2400 ], [ 1519.5046439628, 2400 ], [ 1541.7956656347, 2237 ], [ 1519.5046439628, 2053 ] ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"order":
[
[ [ 2, 1, 3 ], [ 0, 0 ], [ ] ]
],
"sus_weights": [ 0.75, 0.69, 0.75 ],
"order_size": [ 1, 2 ],
"passages_size": [ 0, 2 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,887 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict, saveLedger;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~msg.postln;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr, oneShot = false;
var voices, durs, pbinds, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
pbinds = voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
[
Pbind(
\instrument, \string_model,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
),
Pbind(
\instrument, \sine,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
)
]
}).flatten;
if(oneShot.not, {
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
pbinds = pbinds ++
[
Pbind(
\type, \osc,
\addr, addr,
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
});
res = Ppar(pbinds);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
//res.postln;
if(res.every({arg char; char.isDecDigit}), {res = res.asInteger});
});
});
res
};
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
saveLedger = {arg ledger, path;
var file;
file = File(path, "w");
file.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
file.close;
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
~group = group;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\save_ledger, {arg msg, time, addr, port;
msg.postln;
ledger = msgInterpret.value(msg[1].asString.parseJSON["ledger"], false).postln;
//loadLedgerJSON.value(msg[0])
saveLedger.value(ledger, msg[2].asString);
//loadLedgerFile.value(msg[1].asString);
}, \save_ledger);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
lastXChanges = if(refUID == nil, {
[initVoices.value().deepCopy];
}, {
var file;
file = File((dir +/+ ".." +/+ "resources" +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
seq = seedFunc.value(genMotif, motifSeed).value;
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
addr.sendMsg("/generated", path, modelString, ledgerPath);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
musicChanged = (musicData != seq).postln;
curUID = genUID.value;
File.mkdir((dir +/+ ".." +/+ "resources" +/+ curUID).standardizePath);
File.copy(exPath, (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {
seq = musicData;
dict["music_data"] = seq;
dict["motif_edited"] = "true"
});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
ledger = ledger.drop(-1).add(curUID);
saveLedger.value(ledger, ledgerPath);
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
player.stop;
group.set(\gate, 0);
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
if(msg[1] == 1, {
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (dir +/+ ".." +/+ "resources" +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.postln.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
}, {
pSeq = [loadModelJSON.value(msg[2].asString.parseJSON)["music_data"].postln];
patterns = genPatterns.value(pSeq, addr, true);
});
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
addr.sendMsg("/one_shot", 0);
});
player = player.play
});
}, \transport);
)
(
//synthdefs
~stringModelBusArray = 4.collect({Bus.audio(s, 1)});
~sineBusArray = 4.collect({Bus.audio(s, 1)});
~bassBusArray = 1.collect({Bus.audio(s, 1)});
~hdustBusArray = 1.collect({Bus.audio(s, 1)});
~samplerBusArray = 2.collect({Bus.audio(s, 1)});
~sBuf = Buffer.alloc(s, 10, 2);
SynthDef(\string_model, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var trig, exc, sig1, sig2, noHarms;
noHarms = 30;
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.8).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar(Select.kr(busIndex, ~stringModelBusArray), sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
//Out.ar([0, 1], sig1 * EnvGen.kr(Env.asr(dur, 0.3, 1), gate, doneAction: 2));
}).add;
SynthDef(\sine, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var sig;
sig = SinOsc.ar(freq);
Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.asr(0.3, 0.4, 0.3), gate, timeScale: dur, doneAction: 2));
}).add;
SynthDef(\mixer, {arg freq, gate = 1, sustain, amp, dur, out;
var nameSpaces, sigs, hierarchicalDust;
hierarchicalDust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1))
);
sigs = [~stringModelBusArray, ~sineBusArray, ~bassBusArray, ~hdustBusArray, ~samplerBusArray].collect({arg busArray, i;
var nameSpace, sig;
nameSpace = ['string', 'sine', 'bass', 'hdust', 'sampler'][i];
sig = busArray.collect({arg bus, c; In.ar(bus, 1) * NamedControl.kr(\ ++ nameSpace ++ '_volume_' ++ c, 1, 0.1)});
sig = sig.collect({arg channel, c; Pan2.ar(channel, NamedControl.kr(\ ++ nameSpace ++ '_pan_' ++ c, i / (busArray.size - 1), 0.1) * 2 - 1)});
sig = sig.collect({arg channel, c; channel * NamedControl.kr(\ ++ nameSpace ++ '_mute_' ++ c, 1, 0.1)});
sig = Mix.ar(sig) * pow(NamedControl.kr(\ ++ nameSpace ++ '_volume_master', 1, 0.1), 2);
});
sigs = Mix.ar(sigs / 4);
Out.ar(0, sigs)
}).add;
SynthDef(\bass, {
var switches, drone;
switches = {|i| Dust.kr(0.1)} ! 9;
drone = {|i| var harm = pow(2, 2 - (i / 3).trunc), amp = (1 / pow(harm, 2));
SinOsc.ar(60 * harm + TRand.kr(-3, 3, switches[i]), 0, amp)} ! 9;
Out.ar(~bassBusArray[0], Mix.new(drone) * 0.2);
}).add;
SynthDef(\sampler, {
Out.ar(~samplerBusArray, PlayBuf.ar(2, ~sBuf, BufRateScale.kr(~sBuf), doneAction: 2))
}).add;
// main routine
SynthDef(\hdust, {
arg gate = 0;
var hierarchical_dust, low_sine, high_sine, brown_noise, white_noise;
// this triggers the combinations of sources
// it is similar to the Supercollider UGen called dust but with a hierarchical structure
hierarchical_dust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1)) *
TIRand.kr(0, 1, Impulse.kr(0.1))
);
// adjust the multiplier at the end of each line for adjusting levels
// note with each trigger, each source has a 1 in 3 chance of sounding
low_sine = SinOsc.ar(76.midicps / 16) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.1;
high_sine = SinOsc.ar(76.midicps * 8) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.01;
brown_noise = BrownNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.025;
white_noise = WhiteNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.02;
Out.ar(~hdustBusArray[0],
((low_sine + high_sine + brown_noise + white_noise) )
);
}).add;
)
(
var bass, hdust, sampler, mixer;
bass = Synth.tail(~group, \bass);
hdust = Synth.tail(~group, \hdust);
sampler = Synth.head(~group, \sampler);
mixer = Synth.tail(~group, \mixer);
OSCdef(\mixer, {arg msg, time, addr, port;
mixer.set((msg[1] ++ '_' ++ msg[2] ++ '_' ++ msg[3]), msg[4])
}, \mixer);
OSCdef(\sampler, {arg msg, time, addr, port;
msg.postln;
sampler.free;
~sBuf.free;
~sBuf = Buffer.read(s, msg[1].asString.postln, action: {sampler = Synth.head(~group, \sampler)});
}, \sampler);
)
/* old something
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
*/

@ -1,43 +0,0 @@
{
"music_data":
[
[
[
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 0 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ] ], 0.125 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ] ], 3.625 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 2, 0, 0, 0, 0, -1 ], [ 0, 0, 0, 0, 0, 0 ] ], 1.75 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 2, -1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 4.125 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ] ], 0.875 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ] ], 2.375 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 0.25 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 2.75 ]
]
]
],
"last_changes":
[
[ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 2, 0, 0, 0, 0, -1 ], [ 0, 0, 0, 0, 0, 0 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 2, -1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ]
],
"cur_uid": "5625a95a",
"ref_uid": "nil",
"order_seed": 871476,
"dur_seed": 417039,
"motifs_seed": 168381,
"entrances_probs_vals": [ 0, 0, 1.984126984127, 0, 3.0769230769231, 0, 0.64527027027027, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"passages_probs_vals": [ 0, 0, 1.984126984127, 0, 3.0769230769231, 0, 0.64527027027027, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"exits_probs_vals": [ 0, 0, 1.984126984127, 0, 3.0769230769231, 0, 0.64527027027027, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"ranges": [ [ 1419.1950464396, 2400 ], [ 1519.5046439628, 2400 ], [ 1541.7956656347, 2237 ], [ 1519.5046439628, 2053 ] ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"order":
[
[ [ 0, 3, 1 ], [ 2, 2 ], [ ] ]
],
"sus_weights": [ 0.75, 0.69, 0.75 ],
"order_size": [ 1, 2 ],
"passages_size": [ 0, 2 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,741 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON;
// model vars
//(model and global vars mostly set by OSC funcs
var curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, orders, susWeights, passagesWeights, passagesSize, orderSize;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc;
// other global vars
var lastXChanges, popSize, exPath, dir, primes, dims, tuples,
seq, group, player, ledgerPath, ledger, currentlyPlayingUID;
// install JSON quark
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, min, max, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
durFunc = {arg allowChord;
var res;
res = if(allowChord.not, {
pTable.tableRand * (max - min) + min
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (max - min) + min});
}).round(0.125);
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
allowChord = if((sus ++ silent).includes(ins), {
//(sus ++ silent).includes(ins) && (ins != sus.last);
ins != sus.last;
}, {
if(i < (flatOrder.size - 1), {(isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not}, {false});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord);
}, {
dur = passagesDurFunc.value(allowChord);
});
//dur = passagesDurFunc.value(allowChord);
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
dur = exitsDurFunc.value(allowChord);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~msg.postln;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr;
var voices, durs, patterns, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
res = Ppar(
voices.flop.collect({arg voice;
var clumps, hdScores, freqs, fDurs;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\instrument, \test,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1)
)
}) ++
[
Pbind(
\type, \osc,
\addr, addr,
//\indexPath, "/cur_play_index",
//\indexMsg, Pseq(indices, 1),
//\seqPath, "/mus_seq",
//\seqMsg, Pseq(seq, 1),
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
//res.postln;
if(res.every({arg char; char.isDecDigit}), {res = res.asInteger});
});
});
res
};
writeResources = {arg path;
var file, nameSpaces, modelItems, resString;
file = File(path,"w");
nameSpaces = [
"music_data", "last_changes",
"cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size"
];
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize
];
resString = [nameSpaces, modelItems].flop.collect({arg item;
var nameSpace, modelItem, depth = 0, insert = " ";
# nameSpace, modelItem = item;
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (modelItem == nil), {modelItem = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(modelItem, depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var nameSpaces, data;
//model = File(path, "r").readAllString.parseJSON;
nameSpaces = [
"cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size"
];
data = nameSpaces.collect({arg nS; msgInterpret.value(jsonObject[nS])});
//data.postln;
# curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize = data;
popSize = ranges.size;
};
loadLedgerFile = {arg path;
ledgerPath = path;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
loadModelFile.value(msg[1].asString);
}, \load_model);
OSCdef(\generate, {arg msg, time, addr, port;
var path, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
loadModelFile.value(path);
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
lastXChanges = if(refUID == nil, {
[initVoices.value().deepCopy];
}, {
var file;
file = File((dir +/+ ".." +/+ "resources" +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..2] ++ [entrancesProbVals[3..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..2] ++ [passagesProbVals[3..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..2] ++ [exitsProbVals[3..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
seq = seedFunc.value(genMotif, motifSeed).value;
modelString = writeResources.value(path);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
addr.sendMsg("/generated", path, modelString);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var newLedger, modelPath, musString, musFile, test1, test2;
msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
curUID = genUID.value;
File.mkdir((dir +/+ ".." +/+ "resources" +/+ curUID).standardizePath);
File.copy(exPath, (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
writeResources.value(modelPath);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
newLedger = File(ledgerPath, "w");
ledger = ledger.drop(-1).add(curUID);
newLedger.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
newLedger.close;
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
player.stop;
group.set(\gate, 0);
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (dir +/+ ".." +/+ "resources" +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
});
player = player.play
});
}, \transport);
)
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms;
noHarms = 30;
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
"{\"a\": 1}".parseYAML["a"].asInteger;
"{\"a\": 1}".parseJSON["a"].isNumber;
1223423434123.asHexString.toLower
Date.getDate.rawSeconds
Date.seed.asHexString.toLower
n = NetAddr("localhost", 8080);
n.sendMsg("/GET/#", (NetAddr.localAddr.hostname ++ ":" ++ NetAddr.localAddr.port), "/passage_probs_vals");

@ -1,56 +0,0 @@
{
"music_data":
[
[
[
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 0.625 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ], [ 0, 1, 0, 0, 0, 0 ] ], 0 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 1, 0, 0, 0, 0, -1 ], [ 0, 1, 0, 0, 0, 0 ] ], 1.75 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 1, 0, 0, 0, 0, -1 ], [ 1, 0, 0, 0, -1, 0 ] ], 1.875 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 1, 0, 0, 0, 0, -1 ], [ 0, 0, 0, 0, 1, 0 ] ], 1.5 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 1, 0, 0, 0, 0, -1 ], [ 0, 0, 1, 0, 0, 0 ] ], 0.75 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 1, 0, 0, 0, 0, -1 ], [ 1, 0, 0, -1, 0, 0 ] ], 1.25 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 1, 0, 0, 0, 0, -1 ], [ -1, 0, 0, 1, 0, 0 ] ], 1.625 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 1, 0, 0, 0, 0, -1 ], [ 0, 1, 0, 0, 0, 0 ] ], 1.875 ]
],
[
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0, -1 ], [ 0, 1, 0, 0, 0, 0 ] ], 1 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0 ], [ 0, 1, 0, 0, 0, 0 ] ], 1.125 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 0, 1, -1, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ] ], 0.75 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 0, 1, 0, 0, -1, 0 ], [ 0, 1, 0, 0, 0, 0 ] ], 1.5 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 0, -1, 0, 0, 0, 1 ], [ 0, 1, 0, 0, 0, 0 ] ], 1.5 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 0, 1, 0, 0, 0, -1 ], [ 0, 1, 0, 0, 0, 0 ] ], 1.75 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ "Rest" ], [ 0, 1, 0, 0, 0, 0 ] ], 1.125 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ] ], 0.5 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 0 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 1.25 ]
]
]
],
"last_changes":
[
[ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0 ], [ 0, 1, 0, 0, 0, 0 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 0, 1, -1, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 0, 1, 0, 0, -1, 0 ], [ 0, 1, 0, 0, 0, 0 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 0, -1, 0, 0, 0, 1 ], [ 0, 1, 0, 0, 0, 0 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 0, 1, 0, 0, 0, -1 ], [ 0, 1, 0, 0, 0, 0 ] ]
],
"cur_uid": "628d5c8b",
"ref_uid": "7e170ef8",
"order_seed": 227004,
"dur_seed": 357129,
"motifs_seed": 170994,
"entrances_probs_vals": [ 0.75, 0.5, 2, 0, 0.5, 0.26424870466321, 0.75675675675676, 0.5, 0.5, 0.58549222797927, 0.72635135135135, 1, 0.5 ],
"passages_probs_vals": [ 0.75, 0.5, 2, 0, 0.5, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"exits_probs_vals": [ 0.75, 0.5, 2, 0, 0.5, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"ranges": [ [ 65, 727 ], [ 799, 1758 ], [ -282, 1013 ], [ -282, 799 ] ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"order":
[
[ [ 0 ], [ 3, 2, 3, 3, 3, 3, 3, 3 ], [ 1 ] ],
[ [ 0, 3, 1 ], [ 2, 2, 2, 2, 2 ], [ ] ]
],
"sus_weights": [ 0.75, 0.69, 0.75 ],
"order_size": [ 2, 6 ],
"passages_size": [ 0, 10 ]
}

@ -1,907 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict, saveLedger;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold.deepCopy});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~msg.postln;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr, oneShot = false;
var voices, durs, pbinds, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
pbinds = voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs, attacks, rels, amps;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
//attacks = 2.collect({rrand(1, 3)}) ++ freqs.drop(2).collect({rrand(0.3, 0.5)});
attacks = fDurs.collect({arg dur; dur * rrand(0.2, 0.4)});
//rels = freqs.drop(2).collect({rrand(0.3, 0.5)}) ++ 2.collect({rrand(1, 3)});
rels = (clumps.size - 1).collect({arg c;
if(clumps[c + 1][0] == ["Rest"], {rrand(1, 3)}, {rrand(0.3, 0.5)});
});
amps = freqs.collect({rrand(0.6, 0.99)});
[
Pbind(
\instrument, \string_model,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\attack, Pseq(attacks, 1),
\sustain, Pseq(fDurs, 1),
\release, Pseq(rels, 1),
//\amp, Pseq(amps, 1),
\amp, Pbrown(0.5, 1, 0.5),
\busIndex, v
),
Pbind(
\instrument, \sine,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
)
]
}).flatten;
if(oneShot.not, {
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
pbinds = pbinds ++
[
Pbind(
\type, \osc,
\addr, addr,
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
});
res = Ppar(pbinds);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
//res.postln;
if(res.every({arg char; char.isDecDigit}), {res = res.asInteger});
});
});
res
};
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict, skipLastXChanges = false;
var tmpLastXChanges;
tmpLastXChanges = lastXChanges.deepCopy;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
if(skipLastXChanges, {lastXChanges = tmpLastXChanges});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
saveLedger = {arg ledger, path;
var file;
file = File(path, "w");
file.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
file.close;
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
~group = group;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\save_ledger, {arg msg, time, addr, port;
msg.postln;
ledger = msgInterpret.value(msg[1].asString.parseJSON["ledger"], false).postln;
//loadLedgerJSON.value(msg[0])
saveLedger.value(ledger, msg[2].asString);
//loadLedgerFile.value(msg[1].asString);
}, \save_ledger);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict, true);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
if(refUID == nil, {lastXChanges = [initVoices.value().deepCopy]});
if((refUID != nil) && (refUID != "tmp"), {
var file;
file = File((dir +/+ ".." +/+ "resources" +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
lastXChanges = msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
refUID.postln;
lastXChanges.collect({arg item; item.postln});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
seq = seedFunc.value(genMotif, motifSeed).value;
lastXChanges.collect({arg item; item.postln});
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
addr.sendMsg("/generated", path, modelString, ledgerPath);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
musicChanged = (musicData != seq).postln;
curUID = genUID.value;
File.mkdir((dir +/+ ".." +/+ "resources" +/+ curUID).standardizePath);
File.copy(exPath, (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {
seq = musicData;
dict["music_data"] = seq;
dict["motif_edited"] = "true"
});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
ledger = ledger.drop(-1).add(curUID);
saveLedger.value(ledger, ledgerPath);
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
group.set(\release, 2);
group.set(\gate, 0);
player.stop;
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
if(msg[1] == 1, {
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (dir +/+ ".." +/+ "resources" +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.postln.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
}, {
pSeq = [loadModelJSON.value(msg[2].asString.parseJSON)["music_data"].postln];
patterns = genPatterns.value(pSeq, addr, true);
});
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
addr.sendMsg("/one_shot", 0);
});
player = player.play
});
}, \transport);
)
(
//synthdefs
~stringModelBusArray = 4.collect({Bus.audio(s, 1)});
~sineBusArray = 4.collect({Bus.audio(s, 1)});
~bassBusArray = 1.collect({Bus.audio(s, 1)});
~hdustBusArray = 1.collect({Bus.audio(s, 1)});
~samplerBusArray = 2.collect({Bus.audio(s, 1)});
~sBuf = Buffer.alloc(s, 10, 2);
SynthDef(\string_model, {arg freq, gate = 1, sustain, amp, dur, attack, release = 1, busIndex = 0;
var trig, exc, sig1, sig2, noHarms;
noHarms = rrand(20, 40);
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.8).softclip;
//sig1 = HPF.ar(sig1, 300);
Out.ar(Select.kr(busIndex, ~stringModelBusArray), sig1 * amp * EnvGen.kr(Env.adsr(attack, 0.3, 0.9, release, 0.9, -3), gate, doneAction: 2));
//Out.ar([0, 1], sig1 * EnvGen.kr(Env.asr(dur, 0.3, 1), gate, doneAction: 2));
}).add;
SynthDef(\sine, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var sig;
sig = SinOsc.ar(freq);
Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.asr(0.3, 0.4, 0.3), gate, timeScale: dur, doneAction: 2));
//Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.sine(dur), gate, doneAction: 2));
}).add;
SynthDef(\mixer, {arg freq, gate = 1, sustain, amp, dur, out;
var nameSpaces, sigs, hierarchicalDust;
hierarchicalDust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1))
);
sigs = [~stringModelBusArray, ~sineBusArray, ~bassBusArray, ~hdustBusArray, ~samplerBusArray].collect({arg busArray, i;
var nameSpace, sig;
nameSpace = ['string', 'sine', 'bass', 'hdust', 'sampler'][i];
sig = busArray.collect({arg bus, c; In.ar(bus, 1) * NamedControl.kr(\ ++ nameSpace ++ '_volume_' ++ c, 1, 0.1)});
sig = sig.collect({arg channel, c; Pan2.ar(channel, NamedControl.kr(\ ++ nameSpace ++ '_pan_' ++ c, i / (busArray.size - 1), 0.1) * 2 - 1)});
sig = sig.collect({arg channel, c; channel * NamedControl.kr(\ ++ nameSpace ++ '_mute_' ++ c, 1, 0.1)});
sig = Mix.ar(sig) * pow(NamedControl.kr(\ ++ nameSpace ++ '_volume_master', 1, 0.1), 2);
});
sigs = Mix.ar(sigs / 4);
Out.ar(0, sigs)
}).add;
SynthDef(\bass, {
var switches, drone;
switches = {|i| Dust.kr(0.1)} ! 9;
drone = {|i| var harm = pow(2, 2 - (i / 3).trunc), amp = (1 / pow(harm, 2));
SinOsc.ar(60 * harm + TRand.kr(-3, 3, switches[i]), 0, amp)} ! 9;
Out.ar(~bassBusArray[0], Mix.new(drone) * 0.2);
}).add;
SynthDef(\sampler, {
Out.ar(~samplerBusArray, PlayBuf.ar(2, ~sBuf, BufRateScale.kr(~sBuf), doneAction: 2))
}).add;
// main routine
SynthDef(\hdust, {
arg gate = 0;
var hierarchical_dust, low_sine, high_sine, brown_noise, white_noise;
// this triggers the combinations of sources
// it is similar to the Supercollider UGen called dust but with a hierarchical structure
hierarchical_dust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1)) *
TIRand.kr(0, 1, Impulse.kr(0.1))
);
// adjust the multiplier at the end of each line for adjusting levels
// note with each trigger, each source has a 1 in 3 chance of sounding
low_sine = SinOsc.ar(76.midicps / 16) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.1;
high_sine = SinOsc.ar(76.midicps * 8) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.01;
brown_noise = BrownNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.025;
white_noise = WhiteNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.02;
Out.ar(~hdustBusArray[0],
((low_sine + high_sine + brown_noise + white_noise) )
);
}).add;
)
(
var bass, hdust, sampler, mixer;
bass = Synth.tail(~group, \bass);
hdust = Synth.tail(~group, \hdust);
sampler = Synth.head(~group, \sampler);
mixer = Synth.tail(~group, \mixer);
OSCdef(\mixer, {arg msg, time, addr, port;
mixer.set((msg[1] ++ '_' ++ msg[2] ++ '_' ++ msg[3]), msg[4])
}, \mixer);
OSCdef(\sampler, {arg msg, time, addr, port;
msg.postln;
sampler.free;
~sBuf.free;
~sBuf = Buffer.read(s, msg[1].asString.postln, action: {sampler = Synth.head(~group, \sampler)});
}, \sampler);
)
/* old something
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
*/

@ -1,49 +0,0 @@
{
"music_data":
[
[
[
[ [ [ "Rest" ], [ -2, 3, -2, 1, 1, 1 ], [ "Rest" ], [ "Rest" ] ], 0.125 ],
[ [ [ -5, 3, -2, 2, 1, 1 ], [ -2, 3, -2, 1, 1, 1 ], [ "Rest" ], [ "Rest" ] ], 0.5 ],
[ [ [ -5, 3, -2, 2, 1, 1 ], [ -2, 3, -2, 1, 1, 1 ], [ "Rest" ], [ -4, 5, -2, 2, 2, -1 ] ], 0.625 ],
[ [ [ -5, 3, -2, 2, 1, 1 ], [ -2, 3, -2, 1, 1, 1 ], [ -2, 2, -2, 1, 1, 1 ], [ -4, 5, -2, 2, 2, -1 ] ], 5.625 ]
],
[
[ [ [ -5, 3, -2, 2, 1, 1 ], [ -2, 3, -2, 1, 1, 1 ], [ -3, 4, -2, 2, 2, -1 ], [ -4, 5, -2, 2, 2, -1 ] ], 1.125 ],
[ [ [ -5, 3, -2, 2, 1, 1 ], [ -3, 3, -1, 2, 1, 1 ], [ -3, 4, -2, 2, 2, -1 ], [ -4, 5, -2, 2, 2, -1 ] ], 2.5 ],
[ [ [ -5, 3, -2, 2, 1, 1 ], [ -3, 3, -1, 2, 1, 1 ], [ -3, 4, -2, 2, 2, -1 ], [ "Rest" ] ], 1.375 ],
[ [ [ "Rest" ], [ -3, 3, -1, 2, 1, 1 ], [ -3, 4, -2, 2, 2, -1 ], [ "Rest" ] ], 0.75 ],
[ [ [ "Rest" ], [ "Rest" ], [ -3, 4, -2, 2, 2, -1 ], [ "Rest" ] ], 0 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 1.125 ]
]
]
],
"last_changes":
[
[ [ -5, 3, -2, 2, 1, 1 ], [ -4, 5, -2, 3, 2, -1 ], [ -4, 3, -2, 3, 1, 1 ], [ -4, 5, -2, 2, 2, -1 ] ],
[ [ -5, 3, -2, 2, 1, 1 ], [ -2, 3, -2, 1, 1, 1 ], [ -4, 3, -2, 3, 1, 1 ], [ -4, 5, -2, 2, 2, -1 ] ],
[ [ -5, 3, -2, 2, 1, 1 ], [ -2, 3, -2, 1, 1, 1 ], [ -2, 2, -2, 1, 1, 1 ], [ -4, 5, -2, 2, 2, -1 ] ],
[ [ -5, 3, -2, 2, 1, 1 ], [ -2, 3, -2, 1, 1, 1 ], [ -3, 4, -2, 2, 2, -1 ], [ -4, 5, -2, 2, 2, -1 ] ],
[ [ -5, 3, -2, 2, 1, 1 ], [ -3, 3, -1, 2, 1, 1 ], [ -3, 4, -2, 2, 2, -1 ], [ -4, 5, -2, 2, 2, -1 ] ]
],
"cur_uid": "6409252e",
"ref_uid": "tmp",
"order_seed": 825836,
"dur_seed": 453411,
"motifs_seed": 115471,
"entrances_probs_vals": [ 0.29, 0, 1.3492063492064, 0, 0.65934065934066, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"passages_probs_vals": [ 0.29, 0, 5, 0.58, 1.7582417582418, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"exits_probs_vals": [ 0.29, 0, 5, 0.58, 1.7582417582418, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"ranges": [ [ -3600, -349 ], [ 799, 1758 ], [ -282, 1013 ], [ -282, 799 ] ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"order":
[
[ [ 1, 0, 3 ], [ 2 ], [ ] ],
[ [ 3, 0 ], [ 2, 1 ], [ ] ]
],
"sus_weights": [ 0.39, 0.32, 0.77 ],
"order_size": [ 1, 3 ],
"passages_size": [ 0, 0 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,887 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict, saveLedger;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~msg.postln;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr, oneShot = false;
var voices, durs, pbinds, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
pbinds = voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
[
Pbind(
\instrument, \string_model,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
),
Pbind(
\instrument, \sine,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
)
]
}).flatten;
if(oneShot.not, {
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
pbinds = pbinds ++
[
Pbind(
\type, \osc,
\addr, addr,
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
});
res = Ppar(pbinds);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
//res.postln;
if(res.every({arg char; char.isDecDigit}), {res = res.asInteger});
});
});
res
};
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
saveLedger = {arg ledger, path;
var file;
file = File(path, "w");
file.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
file.close;
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
~group = group;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\save_ledger, {arg msg, time, addr, port;
msg.postln;
ledger = msgInterpret.value(msg[1].asString.parseJSON["ledger"], false).postln;
//loadLedgerJSON.value(msg[0])
saveLedger.value(ledger, msg[2].asString);
//loadLedgerFile.value(msg[1].asString);
}, \save_ledger);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
lastXChanges = if(refUID == nil, {
[initVoices.value().deepCopy];
}, {
var file;
file = File((dir +/+ ".." +/+ "resources" +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
seq = seedFunc.value(genMotif, motifSeed).value;
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
addr.sendMsg("/generated", path, modelString, ledgerPath);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
musicChanged = (musicData != seq).postln;
curUID = genUID.value;
File.mkdir((dir +/+ ".." +/+ "resources" +/+ curUID).standardizePath);
File.copy(exPath, (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {
seq = musicData;
dict["music_data"] = seq;
dict["motif_edited"] = "true"
});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
ledger = ledger.drop(-1).add(curUID);
saveLedger.value(ledger, ledgerPath);
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
player.stop;
group.set(\gate, 0);
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
if(msg[1] == 1, {
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (dir +/+ ".." +/+ "resources" +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.postln.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
}, {
pSeq = [loadModelJSON.value(msg[2].asString.parseJSON)["music_data"].postln];
patterns = genPatterns.value(pSeq, addr, true);
});
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
addr.sendMsg("/one_shot", 0);
});
player = player.play
});
}, \transport);
)
(
//synthdefs
~stringModelBusArray = 4.collect({Bus.audio(s, 1)});
~sineBusArray = 4.collect({Bus.audio(s, 1)});
~bassBusArray = 1.collect({Bus.audio(s, 1)});
~hdustBusArray = 1.collect({Bus.audio(s, 1)});
~samplerBusArray = 2.collect({Bus.audio(s, 1)});
~sBuf = Buffer.alloc(s, 10, 2);
SynthDef(\string_model, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var trig, exc, sig1, sig2, noHarms;
noHarms = 30;
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.8).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar(Select.kr(busIndex, ~stringModelBusArray), sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
//Out.ar([0, 1], sig1 * EnvGen.kr(Env.asr(dur, 0.3, 1), gate, doneAction: 2));
}).add;
SynthDef(\sine, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var sig;
sig = SinOsc.ar(freq);
Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.asr(0.3, 0.4, 0.3), gate, timeScale: dur, doneAction: 2));
}).add;
SynthDef(\mixer, {arg freq, gate = 1, sustain, amp, dur, out;
var nameSpaces, sigs, hierarchicalDust;
hierarchicalDust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1))
);
sigs = [~stringModelBusArray, ~sineBusArray, ~bassBusArray, ~hdustBusArray, ~samplerBusArray].collect({arg busArray, i;
var nameSpace, sig;
nameSpace = ['string', 'sine', 'bass', 'hdust', 'sampler'][i];
sig = busArray.collect({arg bus, c; In.ar(bus, 1) * NamedControl.kr(\ ++ nameSpace ++ '_volume_' ++ c, 1, 0.1)});
sig = sig.collect({arg channel, c; Pan2.ar(channel, NamedControl.kr(\ ++ nameSpace ++ '_pan_' ++ c, i / (busArray.size - 1), 0.1) * 2 - 1)});
sig = sig.collect({arg channel, c; channel * NamedControl.kr(\ ++ nameSpace ++ '_mute_' ++ c, 1, 0.1)});
sig = Mix.ar(sig) * pow(NamedControl.kr(\ ++ nameSpace ++ '_volume_master', 1, 0.1), 2);
});
sigs = Mix.ar(sigs / 4);
Out.ar(0, sigs)
}).add;
SynthDef(\bass, {
var switches, drone;
switches = {|i| Dust.kr(0.1)} ! 9;
drone = {|i| var harm = pow(2, 2 - (i / 3).trunc), amp = (1 / pow(harm, 2));
SinOsc.ar(60 * harm + TRand.kr(-3, 3, switches[i]), 0, amp)} ! 9;
Out.ar(~bassBusArray[0], Mix.new(drone) * 0.2);
}).add;
SynthDef(\sampler, {
Out.ar(~samplerBusArray, PlayBuf.ar(2, ~sBuf, BufRateScale.kr(~sBuf), doneAction: 2))
}).add;
// main routine
SynthDef(\hdust, {
arg gate = 0;
var hierarchical_dust, low_sine, high_sine, brown_noise, white_noise;
// this triggers the combinations of sources
// it is similar to the Supercollider UGen called dust but with a hierarchical structure
hierarchical_dust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1)) *
TIRand.kr(0, 1, Impulse.kr(0.1))
);
// adjust the multiplier at the end of each line for adjusting levels
// note with each trigger, each source has a 1 in 3 chance of sounding
low_sine = SinOsc.ar(76.midicps / 16) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.1;
high_sine = SinOsc.ar(76.midicps * 8) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.01;
brown_noise = BrownNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.025;
white_noise = WhiteNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.02;
Out.ar(~hdustBusArray[0],
((low_sine + high_sine + brown_noise + white_noise) )
);
}).add;
)
(
var bass, hdust, sampler, mixer;
bass = Synth.tail(~group, \bass);
hdust = Synth.tail(~group, \hdust);
sampler = Synth.head(~group, \sampler);
mixer = Synth.tail(~group, \mixer);
OSCdef(\mixer, {arg msg, time, addr, port;
mixer.set((msg[1] ++ '_' ++ msg[2] ++ '_' ++ msg[3]), msg[4])
}, \mixer);
OSCdef(\sampler, {arg msg, time, addr, port;
msg.postln;
sampler.free;
~sBuf.free;
~sBuf = Buffer.read(s, msg[1].asString.postln, action: {sampler = Synth.head(~group, \sampler)});
}, \sampler);
)
/* old something
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
*/

@ -1,43 +0,0 @@
{
"music_data":
[
[
[
[ [ [ 2, -1, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 2.75 ],
[ [ [ 2, -1, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ] ], 4.5 ],
[ [ [ 2, -1, 0, 0, 0, 0 ], [ "Rest" ], [ 2, 0, 0, 0, 0, -1 ], [ 0, 0, 0, 0, 0, 0 ] ], 2.25 ],
[ [ [ 2, -1, 0, 0, 0, 0 ], [ "Rest" ], [ 1, 0, 1, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 3.5 ],
[ [ [ "Rest" ], [ "Rest" ], [ 1, 0, 1, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 2.25 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ] ], 0.125 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 3 ]
]
]
],
"last_changes":
[
[ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ],
[ [ 2, 0, 0, -1, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ],
[ [ 2, -1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ],
[ [ 2, -1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 2, 0, 0, 0, 0, -1 ], [ 0, 0, 0, 0, 0, 0 ] ],
[ [ 2, -1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, 1, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ]
],
"cur_uid": "6a3f7c7c",
"ref_uid": "5267b235",
"order_seed": 857701,
"dur_seed": 870436,
"motifs_seed": 456020,
"entrances_probs_vals": [ 0, 0, 1.984126984127, 0, 3.0769230769231, 0, 0.64527027027027, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"passages_probs_vals": [ 0, 0, 1.984126984127, 0, 3.0769230769231, 0, 0.64527027027027, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"exits_probs_vals": [ 0, 0, 1.984126984127, 0, 3.0769230769231, 0, 0.64527027027027, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"ranges": [ [ 1419.1950464396, 2400 ], [ 1519.5046439628, 2400 ], [ 1541.7956656347, 2237 ], [ 1519.5046439628, 2053 ] ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"order":
[
[ [ 0, 3 ], [ 2, 2 ], [ 1 ] ]
],
"sus_weights": [ 0.75, 0.69, 0.75 ],
"order_size": [ 1, 2 ],
"passages_size": [ 0, 2 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,808 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~msg.postln;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr;
var voices, durs, patterns, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
res = Ppar(
voices.flop.collect({arg voice;
var clumps, hdScores, freqs, fDurs;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\instrument, \test,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1)
)
}) ++
[
Pbind(
\type, \osc,
\addr, addr,
//\indexPath, "/cur_play_index",
//\indexMsg, Pseq(indices, 1),
//\seqPath, "/mus_seq",
//\seqMsg, Pseq(seq, 1),
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
//res.postln;
if(res.every({arg char; char.isDecDigit}), {res = res.asInteger});
});
});
res
};
/*
writeResources = {arg path;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = [nameSpaces, modelItems].flop.collect({arg item;
var nameSpace, modelItem, depth = 0, insert = " ";
# nameSpace, modelItem = item;
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (modelItem == nil), {modelItem = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(modelItem, depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
*/
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
//model = File(path, "r").readAllString.parseJSON;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
lastXChanges = if(refUID == nil, {
[initVoices.value().deepCopy];
}, {
var file;
file = File((dir +/+ ".." +/+ "resources" +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
seq = seedFunc.value(genMotif, motifSeed).value;
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
addr.sendMsg("/generated", path, modelString);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
dict.postln;
musicChanged = (musicData != seq).postln;
curUID = genUID.value;
File.mkdir((dir +/+ ".." +/+ "resources" +/+ curUID).standardizePath);
File.copy(exPath, (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {dict["motif_edited"] = "true"});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
newLedger = File(ledgerPath, "w");
ledger = ledger.drop(-1).add(curUID);
newLedger.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
newLedger.close;
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
player.stop;
group.set(\gate, 0);
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (dir +/+ ".." +/+ "resources" +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
});
player = player.play
});
}, \transport);
)
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms;
noHarms = 30;
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
//Out.ar([0, 1], sig1 * EnvGen.kr(Env.asr(dur, 0.3, 1), gate, doneAction: 2));
}).add;
)
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
"{\"a\": 1}".parseYAML["a"].asInteger;
"{\"a\": 1}".parseJSON["a"].isNumber;
1223423434123.asHexString.toLower
Date.getDate.rawSeconds
Date.seed.asHexString.toLower
n = NetAddr("localhost", 8080);
n.sendMsg("/GET/#", (NetAddr.localAddr.hostname ++ ":" ++ NetAddr.localAddr.port), "/passage_probs_vals");

@ -1,93 +0,0 @@
{
"music_data":
[
[
[
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 0 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ] ], 0 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 8.375 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -1, 0, 0, 0, 0, 1 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 1.5 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -1, 1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 0.875 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, -1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 0.75 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -1, 0, 1, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 0.75 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, -1, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ], 10.75 ]
],
[
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, -1, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ] ], 0.625 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, -1, 0, 0 ], [ -1, 0, 0, 1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ] ], 0.625 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -1, 0, 0, 0, 0, 0 ], [ -1, 0, 0, 1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ] ], 0.75 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, -1 ], [ -1, 0, 0, 1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ] ], 0.875 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, -1 ], [ -1, 0, 0, 1, 0, 0 ], [ 1, 0, 0, 0, 0, -1 ] ], 1.25 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, -1 ], [ 0, 0, -1, 0, 0, 0 ], [ 1, 0, 0, 0, 0, -1 ] ], 1.75 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, -1 ], [ 0, 0, -1, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0 ] ], 0 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, -1 ], [ -1, 0, 0, 0, 0, 1 ], [ 0, 0, 0, 0, 1, 0 ] ], 0 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -1, 0, 1, 0, 0, 0 ], [ -1, 0, 0, 0, 0, 1 ], [ 0, 0, 0, 0, 1, 0 ] ], 1.875 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -1, 0, 1, 0, 0, 0 ], [ -1, 1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0 ] ], 8 ]
],
[
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -1, 0, -1, 0, 1, 0 ], [ -1, 1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0 ] ], 1 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -1, 1, -1, 0, 0, 0 ], [ -1, 1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0 ] ], 1 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, -1, 0, 0 ], [ -1, 1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0 ] ], 1.625 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -2, 0, 0, 0, 1, 1 ], [ -1, 1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0 ] ], 0.875 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -1, 0, 0, 0, 0, 0 ], [ -1, 1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0 ] ], 1.25 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -2, 1, 0, 0, 1, 0 ], [ -1, 1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0 ] ], 8.625 ]
],
[
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -2, 1, 0, 0, 1, 0 ], [ -1, 1, 0, 0, 0, 0 ], [ 1, 0, -1, 0, 0, 0 ] ], 1.75 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -2, 1, 0, 0, 1, 0 ], [ -1, 1, 0, 0, 0, 0 ], [ 1, 1, 0, -1, 0, 0 ] ], 1.875 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -2, 1, 0, 0, 1, 0 ], [ -1, 1, 0, 0, 0, 0 ], [ 0, 1, -1, 0, 1, 0 ] ], 0.625 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -2, 1, 0, 0, 1, 0 ], [ -1, 1, 0, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ] ], 0.625 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -2, 1, 0, 0, 1, 0 ], [ -1, 1, 0, 0, 0, 0 ], [ -1, 1, 0, 0, 2, 0 ] ], 9.25 ]
],
[
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -2, 1, 0, 0, 1, 0 ], [ -1, 0, 0, 0, 1, 0 ], [ -1, 1, 0, 0, 2, 0 ] ], 1.875 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -2, 1, 0, 0, 1, 0 ], [ 0, 0, -1, 0, 0, 0 ], [ -1, 1, 0, 0, 2, 0 ] ], 1.625 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -2, 1, 0, 0, 1, 0 ], [ 0, 0, 0, 0, -1, 0 ], [ -1, 1, 0, 0, 2, 0 ] ], 1.625 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -2, 1, 0, 0, 1, 0 ], [ 0, -1, 0, 0, 0, 0 ], [ -1, 1, 0, 0, 2, 0 ] ], 1 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -2, 1, 0, 0, 1, 0 ], [ -1, 1, 0, 0, 1, -1 ], [ -1, 1, 0, 0, 2, 0 ] ], 9.75 ]
],
[
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ -1, 1, 0, 0, 1, -1 ], [ -1, 1, 0, 0, 2, 0 ] ], 0 ],
[ [ [ "Rest" ], [ "Rest" ], [ -1, 1, 0, 0, 1, -1 ], [ -1, 1, 0, 0, 2, 0 ] ], 1.25 ],
[ [ [ "Rest" ], [ "Rest" ], [ -3, 1, 0, 0, 2, 1 ], [ -1, 1, 0, 0, 2, 0 ] ], 1.875 ],
[ [ [ "Rest" ], [ "Rest" ], [ -3, 2, 0, 0, 2, 0 ], [ -1, 1, 0, 0, 2, 0 ] ], 1.25 ],
[ [ [ "Rest" ], [ "Rest" ], [ -2, 1, -1, 0, 2, 0 ], [ -1, 1, 0, 0, 2, 0 ] ], 2 ],
[ [ [ "Rest" ], [ "Rest" ], [ -3, 1, 0, 1, 2, 0 ], [ -1, 1, 0, 0, 2, 0 ] ], 7.625 ],
[ [ [ "Rest" ], [ "Rest" ], [ -3, 1, 0, 1, 2, 0 ], [ "Rest" ] ], 1 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 8.375 ]
]
]
],
"last_changes":
[
[ [ 0, 0, 0, 0, 0, 0 ], [ -2, 1, 0, 0, 1, 0 ], [ -1, 1, 0, 0, 1, -1 ], [ -1, 1, 0, 0, 2, 0 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ -2, 1, 0, 0, 1, 0 ], [ -3, 1, 0, 0, 2, 1 ], [ -1, 1, 0, 0, 2, 0 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ -2, 1, 0, 0, 1, 0 ], [ -3, 2, 0, 0, 2, 0 ], [ -1, 1, 0, 0, 2, 0 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ -2, 1, 0, 0, 1, 0 ], [ -2, 1, -1, 0, 2, 0 ], [ -1, 1, 0, 0, 2, 0 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ -2, 1, 0, 0, 1, 0 ], [ -3, 1, 0, 1, 2, 0 ], [ -1, 1, 0, 0, 2, 0 ] ]
],
"cur_uid": "6abf27d4",
"ref_uid": "nil",
"order_seed": 587621,
"dur_seed": 706606,
"motifs_seed": 392608,
"entrances_probs_vals": [ 0.34, 6.468253968254, 9.2857142857143, 0.5, 2, 0, 0.5, 0.24509803921569, 0.89189189189189, 0.5, 1, 0.5, 0.5, 0.8562091503268, 0.69932432432432, 1, 0.5 ],
"passages_probs_vals": [ 0.34, 6.468253968254, 9.2857142857143, 0.5, 2, 0, 0.5, 0.24509803921569, 0.89189189189189, 0.5, 1, 0.5, 0.5, 0.8562091503268, 0.69932432432432, 1, 0.5 ],
"exits_probs_vals": [ 0.34, 6.468253968254, 9.2857142857143, 0.5, 2, 0, 0.5, 0.24509803921569, 0.89189189189189, 0.5, 1, 0.5, 0.5, 0.8562091503268, 0.69932432432432, 1, 0.5 ],
"ranges": [ [ -1200, 2400 ], [ -1200, 2400 ], [ -1200, 2400 ], [ -1200, 2400 ] ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"order":
[
[ [ 0, 2, 3 ], [ 1, 1, 1, 1, 1 ], [ ] ],
[ [ 0 ], [ 3, 2, 1, 1, 3, 2, 3, 2, 1, 2 ], [ ] ],
[ [ 0, 3, 2 ], [ 1, 1, 1, 1, 1, 1 ], [ ] ],
[ [ 1, 2, 0 ], [ 3, 3, 3, 3, 3 ], [ ] ],
[ [ 1, 3, 0 ], [ 2, 2, 2, 2, 2 ], [ ] ],
[ [ 3 ], [ 2, 2, 2, 2 ], [ 1, 0 ] ]
],
"sus_weights": [ 0.75, 0.75, 0.75 ],
"order_size": [ 1, 10 ],
"passages_size": [ 0, 10 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,887 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict, saveLedger;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~msg.postln;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr, oneShot = false;
var voices, durs, pbinds, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
pbinds = voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
[
Pbind(
\instrument, \string_model,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
),
Pbind(
\instrument, \sine,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
)
]
}).flatten;
if(oneShot.not, {
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
pbinds = pbinds ++
[
Pbind(
\type, \osc,
\addr, addr,
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
});
res = Ppar(pbinds);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
//res.postln;
if(res.every({arg char; char.isDecDigit}), {res = res.asInteger});
});
});
res
};
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
saveLedger = {arg ledger, path;
var file;
file = File(path, "w");
file.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
file.close;
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
~group = group;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\save_ledger, {arg msg, time, addr, port;
msg.postln;
ledger = msgInterpret.value(msg[1].asString.parseJSON["ledger"], false).postln;
//loadLedgerJSON.value(msg[0])
saveLedger.value(ledger, msg[2].asString);
//loadLedgerFile.value(msg[1].asString);
}, \save_ledger);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
lastXChanges = if(refUID == nil, {
[initVoices.value().deepCopy];
}, {
var file;
file = File((dir +/+ ".." +/+ "resources" +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
seq = seedFunc.value(genMotif, motifSeed).value;
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
addr.sendMsg("/generated", path, modelString, ledgerPath);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
musicChanged = (musicData != seq).postln;
curUID = genUID.value;
File.mkdir((dir +/+ ".." +/+ "resources" +/+ curUID).standardizePath);
File.copy(exPath, (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {
seq = musicData;
dict["music_data"] = seq;
dict["motif_edited"] = "true"
});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
ledger = ledger.drop(-1).add(curUID);
saveLedger.value(ledger, ledgerPath);
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
player.stop;
group.set(\gate, 0);
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
if(msg[1] == 1, {
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (dir +/+ ".." +/+ "resources" +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.postln.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
}, {
pSeq = [loadModelJSON.value(msg[2].asString.parseJSON)["music_data"].postln];
patterns = genPatterns.value(pSeq, addr, true);
});
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
addr.sendMsg("/one_shot", 0);
});
player = player.play
});
}, \transport);
)
(
//synthdefs
~stringModelBusArray = 4.collect({Bus.audio(s, 1)});
~sineBusArray = 4.collect({Bus.audio(s, 1)});
~bassBusArray = 1.collect({Bus.audio(s, 1)});
~hdustBusArray = 1.collect({Bus.audio(s, 1)});
~samplerBusArray = 2.collect({Bus.audio(s, 1)});
~sBuf = Buffer.alloc(s, 10, 2);
SynthDef(\string_model, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var trig, exc, sig1, sig2, noHarms;
noHarms = 30;
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.8).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar(Select.kr(busIndex, ~stringModelBusArray), sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
//Out.ar([0, 1], sig1 * EnvGen.kr(Env.asr(dur, 0.3, 1), gate, doneAction: 2));
}).add;
SynthDef(\sine, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var sig;
sig = SinOsc.ar(freq);
Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.asr(0.3, 0.4, 0.3), gate, timeScale: dur, doneAction: 2));
}).add;
SynthDef(\mixer, {arg freq, gate = 1, sustain, amp, dur, out;
var nameSpaces, sigs, hierarchicalDust;
hierarchicalDust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1))
);
sigs = [~stringModelBusArray, ~sineBusArray, ~bassBusArray, ~hdustBusArray, ~samplerBusArray].collect({arg busArray, i;
var nameSpace, sig;
nameSpace = ['string', 'sine', 'bass', 'hdust', 'sampler'][i];
sig = busArray.collect({arg bus, c; In.ar(bus, 1) * NamedControl.kr(\ ++ nameSpace ++ '_volume_' ++ c, 1, 0.1)});
sig = sig.collect({arg channel, c; Pan2.ar(channel, NamedControl.kr(\ ++ nameSpace ++ '_pan_' ++ c, i / (busArray.size - 1), 0.1) * 2 - 1)});
sig = sig.collect({arg channel, c; channel * NamedControl.kr(\ ++ nameSpace ++ '_mute_' ++ c, 1, 0.1)});
sig = Mix.ar(sig) * pow(NamedControl.kr(\ ++ nameSpace ++ '_volume_master', 1, 0.1), 2);
});
sigs = Mix.ar(sigs / 4);
Out.ar(0, sigs)
}).add;
SynthDef(\bass, {
var switches, drone;
switches = {|i| Dust.kr(0.1)} ! 9;
drone = {|i| var harm = pow(2, 2 - (i / 3).trunc), amp = (1 / pow(harm, 2));
SinOsc.ar(60 * harm + TRand.kr(-3, 3, switches[i]), 0, amp)} ! 9;
Out.ar(~bassBusArray[0], Mix.new(drone) * 0.2);
}).add;
SynthDef(\sampler, {
Out.ar(~samplerBusArray, PlayBuf.ar(2, ~sBuf, BufRateScale.kr(~sBuf), doneAction: 2))
}).add;
// main routine
SynthDef(\hdust, {
arg gate = 0;
var hierarchical_dust, low_sine, high_sine, brown_noise, white_noise;
// this triggers the combinations of sources
// it is similar to the Supercollider UGen called dust but with a hierarchical structure
hierarchical_dust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1)) *
TIRand.kr(0, 1, Impulse.kr(0.1))
);
// adjust the multiplier at the end of each line for adjusting levels
// note with each trigger, each source has a 1 in 3 chance of sounding
low_sine = SinOsc.ar(76.midicps / 16) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.1;
high_sine = SinOsc.ar(76.midicps * 8) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.01;
brown_noise = BrownNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.025;
white_noise = WhiteNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.02;
Out.ar(~hdustBusArray[0],
((low_sine + high_sine + brown_noise + white_noise) )
);
}).add;
)
(
var bass, hdust, sampler, mixer;
bass = Synth.tail(~group, \bass);
hdust = Synth.tail(~group, \hdust);
sampler = Synth.head(~group, \sampler);
mixer = Synth.tail(~group, \mixer);
OSCdef(\mixer, {arg msg, time, addr, port;
mixer.set((msg[1] ++ '_' ++ msg[2] ++ '_' ++ msg[3]), msg[4])
}, \mixer);
OSCdef(\sampler, {arg msg, time, addr, port;
msg.postln;
sampler.free;
~sBuf.free;
~sBuf = Buffer.read(s, msg[1].asString.postln, action: {sampler = Synth.head(~group, \sampler)});
}, \sampler);
)
/* old something
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
*/

@ -1,44 +0,0 @@
{
"music_data":
[
[
[
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ 2, 0, 0, -1, 0, 0 ] ], 0 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ], [ 2, 0, 0, -1, 0, 0 ] ], 0.375 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ "Rest" ], [ 2, 0, 0, -1, 0, 0 ] ], 4.875 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0, -1 ], [ 2, 0, 0, -1, 0, 0 ] ], 2.375 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0, -1 ], [ "Rest" ] ], 0.375 ],
[ [ [ "Rest" ], [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0, -1 ], [ "Rest" ] ], 0.375 ],
[ [ [ "Rest" ], [ 1, -1, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ] ], 0 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 3.875 ]
]
]
],
"last_changes":
[
[ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 0, 0, 0, 1, 0, 0 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 1, 0, 0, 0, 0, 0 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 1, -1, 0, 0, 0, 1 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 2, 0, 0, -1, 0, 0 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0, -1 ], [ 2, 0, 0, -1, 0, 0 ] ]
],
"cur_uid": "6d0c2f19",
"ref_uid": "7e170ef8",
"order_seed": 861383,
"dur_seed": 654405,
"motifs_seed": 513928,
"entrances_probs_vals": [ 0.75, 1.67, 4.8015873015873, 0, 0.5, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"passages_probs_vals": [ 0.75, 1.67, 4.8015873015873, 0, 0.5, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"exits_probs_vals": [ 0.75, 1.87, 5.1984126984127, 0, 0.5, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"ranges": [ [ -384, 2400 ], [ -507, 2400 ], [ -282, 2237 ], [ -1200, 2053 ] ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"order":
[
[ [ 3, 0, 1 ], [ 2 ], [ ] ]
],
"sus_weights": [ 0.75, 0.69, 0.75 ],
"order_size": [ 1, 2 ],
"passages_size": [ 0, 1 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,881 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict, saveLedger;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~msg.postln;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr, oneShot = false;
var voices, durs, pbinds, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
pbinds = voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
[
Pbind(
\instrument, \string_model,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
),
Pbind(
\instrument, \sine,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
)
]
}).flatten;
if(oneShot.not, {
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
pbinds = pbinds ++
[
Pbind(
\type, \osc,
\addr, addr,
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
});
res = Ppar(pbinds);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
//res.postln;
if(res.every({arg char; char.isDecDigit}), {res = res.asInteger});
});
});
res
};
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
saveLedger = {arg ledger, path;
var file;
file = File(path, "w");
file.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
file.close;
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
~group = group;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\save_ledger, {arg msg, time, addr, port;
msg.postln;
ledger = msgInterpret.value(msg[1].asString.parseJSON["ledger"], false).postln;
//loadLedgerJSON.value(msg[0])
saveLedger.value(ledger, msg[2].asString);
//loadLedgerFile.value(msg[1].asString);
}, \save_ledger);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
lastXChanges = if(refUID == nil, {
[initVoices.value().deepCopy];
}, {
var file;
file = File((dir +/+ ".." +/+ "resources" +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
seq = seedFunc.value(genMotif, motifSeed).value;
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
addr.sendMsg("/generated", path, modelString);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
musicChanged = (musicData != seq).postln;
curUID = genUID.value;
File.mkdir((dir +/+ ".." +/+ "resources" +/+ curUID).standardizePath);
File.copy(exPath, (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {
seq = musicData;
dict["music_data"] = seq;
dict["motif_edited"] = "true"
});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
ledger = ledger.drop(-1).add(curUID);
saveLedger.value(ledger, ledgerPath);
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
player.stop;
group.set(\gate, 0);
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
if(msg[1] == 1, {
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (dir +/+ ".." +/+ "resources" +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.postln.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
}, {
pSeq = [loadModelJSON.value(msg[2].asString.parseJSON)["music_data"].postln];
patterns = genPatterns.value(pSeq, addr, true);
});
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
addr.sendMsg("/one_shot", 0);
});
player = player.play
});
}, \transport);
)
(
//synthdefs
~stringModelBusArray = 4.collect({Bus.audio(s, 1)});
~sineBusArray = 4.collect({Bus.audio(s, 1)});
~bassBusArray = 1.collect({Bus.audio(s, 1)});
~hdustBusArray = 1.collect({Bus.audio(s, 1)});
~samplerBusArray = 2.collect({Bus.audio(s, 1)});
~sBuf = Buffer.alloc(s, 10, 2);
SynthDef(\string_model, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var trig, exc, sig1, sig2, noHarms;
noHarms = 30;
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar(Select.kr(busIndex, ~stringModelBusArray), sig1 * EnvGen.kr(Env.asr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
//Out.ar([0, 1], sig1 * EnvGen.kr(Env.asr(dur, 0.3, 1), gate, doneAction: 2));
}).add;
SynthDef(\sine, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var sig;
sig = SinOsc.ar(freq);
Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.asr(0.3, 0.4, 0.3), gate, timeScale: dur, doneAction: 2));
}).add;
SynthDef(\mixer, {arg freq, gate = 1, sustain, amp, dur, out;
var nameSpaces, sigs;
sigs = [~stringModelBusArray, ~sineBusArray, ~bassBusArray, ~hdustBusArray, ~samplerBusArray].collect({arg busArray, i;
var nameSpace, sig;
nameSpace = ['string', 'sine', 'bass', 'hdust', 'sampler'][i];
sig = busArray.collect({arg bus, c; In.ar(bus, 1) * NamedControl.kr(\ ++ nameSpace ++ '_volume_' ++ c, 1, 0.1)});
sig = sig.collect({arg channel, c; Pan2.ar(channel, NamedControl.kr(\ ++ nameSpace ++ '_pan_' ++ c, i / (busArray.size - 1), 0.1) * 2 - 1)});
sig = sig.collect({arg channel, c; channel * NamedControl.kr(\ ++ nameSpace ++ '_mute_' ++ c, 1, 0.1)});
sig = Mix.ar(sig) * NamedControl.kr(\ ++ nameSpace ++ '_volume_master', 1, 0.1);
});
sigs = Mix.ar(sigs / 4);
Out.ar(0, sigs)
}).add;
SynthDef(\bass, {
var switches, drone;
switches = {|i| Dust.kr(0.1)} ! 9;
drone = {|i| var harm = pow(2, 2 - (i / 3).trunc), amp = (1 / pow(harm, 2));
SinOsc.ar(60 * harm + TRand.kr(-3, 3, switches[i]), 0, amp)} ! 9;
Out.ar(~bassBusArray[0], Mix.new(drone) * 0.2);
}).add;
SynthDef(\sampler, {
Out.ar(~samplerBusArray, PlayBuf.ar(2, ~sBuf, BufRateScale.kr(~sBuf), doneAction: 2))
}).add;
// main routine
SynthDef(\hdust, {
arg gate = 0;
var hierarchical_dust, low_sine, high_sine, brown_noise, white_noise;
// this triggers the combinations of sources
// it is similar to the Supercollider UGen called dust but with a hierarchical structure
hierarchical_dust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1)) *
TIRand.kr(0, 1, Impulse.kr(0.1))
);
// adjust the multiplier at the end of each line for adjusting levels
// note with each trigger, each source has a 1 in 3 chance of sounding
low_sine = SinOsc.ar(76.midicps / 16) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.1;
high_sine = SinOsc.ar(76.midicps * 8) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.01;
brown_noise = BrownNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.025;
white_noise = WhiteNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.02;
Out.ar(~hdustBusArray[0],
((low_sine + high_sine + brown_noise + white_noise) )
);
}).add;
)
(
var bass, hdust, sampler, mixer;
bass = Synth.tail(~group, \bass);
hdust = Synth.tail(~group, \hdust);
sampler = Synth.head(~group, \sampler);
mixer = Synth.tail(~group, \mixer);
OSCdef(\mixer, {arg msg, time, addr, port;
mixer.set((msg[1] ++ '_' ++ msg[2] ++ '_' ++ msg[3]), msg[4])
}, \mixer);
OSCdef(\sampler, {arg msg, time, addr, port;
msg.postln;
sampler.free;
~sBuf.free;
~sBuf = Buffer.read(s, msg[1].asString.postln, action: {sampler = Synth.head(~group, \sampler)});
}, \sampler);
)
/* old something
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
*/

@ -1,41 +0,0 @@
{
"music_data":
[
[
[
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 1.375 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ] ], 0.75 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ] ], 1.875 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 2, 0, 0, 0, 0, -1 ] ], 3.375 ],
[ [ [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 2, 0, 0, 0, 0, -1 ] ], 0.25 ],
[ [ [ "Rest" ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ], [ 2, 0, 0, 0, 0, -1 ] ], 2 ],
[ [ [ "Rest" ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ] ], 2.25 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 2.125 ]
]
]
],
"last_changes":
[
[ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 2, 0, 0, 0, 0, -1 ] ]
],
"cur_uid": "6d743c5c",
"ref_uid": "nil",
"order_seed": 232987,
"dur_seed": 390795,
"motifs_seed": 257319,
"entrances_probs_vals": [ 0, 0, 1.984126984127, 0, 3.0769230769231, 0, 0.64527027027027, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"passages_probs_vals": [ 0, 0, 1.984126984127, 0, 3.0769230769231, 0, 0.64527027027027, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"exits_probs_vals": [ 0, 0, 1.984126984127, 0, 3.0769230769231, 0, 0.64527027027027, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"ranges": [ [ 1419.1950464396, 2400 ], [ 1519.5046439628, 2400 ], [ 1541.7956656347, 2237 ], [ 1519.5046439628, 2053 ] ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"order":
[
[ [ 0, 1, 2 ], [ 3 ], [ ] ]
],
"sus_weights": [ 0.75, 0.69, 0.75 ],
"order_size": [ 1, 2 ],
"passages_size": [ 0, 2 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,894 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict, saveLedger;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold.deepCopy});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~msg.postln;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr, oneShot = false;
var voices, durs, pbinds, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
pbinds = voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
[
Pbind(
\instrument, \string_model,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
),
Pbind(
\instrument, \sine,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
)
]
}).flatten;
if(oneShot.not, {
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
pbinds = pbinds ++
[
Pbind(
\type, \osc,
\addr, addr,
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
});
res = Ppar(pbinds);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
//res.postln;
if(res.every({arg char; char.isDecDigit}), {res = res.asInteger});
});
});
res
};
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict, skipLastXChanges = false;
var tmpLastXChanges;
tmpLastXChanges = lastXChanges.deepCopy;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
if(skipLastXChanges, {lastXChanges = tmpLastXChanges});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
saveLedger = {arg ledger, path;
var file;
file = File(path, "w");
file.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
file.close;
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
~group = group;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\save_ledger, {arg msg, time, addr, port;
msg.postln;
ledger = msgInterpret.value(msg[1].asString.parseJSON["ledger"], false).postln;
//loadLedgerJSON.value(msg[0])
saveLedger.value(ledger, msg[2].asString);
//loadLedgerFile.value(msg[1].asString);
}, \save_ledger);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict, true);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
if(refUID == nil, {lastXChanges = [initVoices.value().deepCopy]});
if((refUID != nil) && (refUID != "tmp"), {
var file;
file = File((dir +/+ ".." +/+ "resources" +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
lastXChanges = msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
refUID.postln;
lastXChanges.collect({arg item; item.postln});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
seq = seedFunc.value(genMotif, motifSeed).value;
lastXChanges.collect({arg item; item.postln});
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
addr.sendMsg("/generated", path, modelString, ledgerPath);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
musicChanged = (musicData != seq).postln;
curUID = genUID.value;
File.mkdir((dir +/+ ".." +/+ "resources" +/+ curUID).standardizePath);
File.copy(exPath, (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {
seq = musicData;
dict["music_data"] = seq;
dict["motif_edited"] = "true"
});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
ledger = ledger.drop(-1).add(curUID);
saveLedger.value(ledger, ledgerPath);
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
player.stop;
group.set(\gate, 0);
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
if(msg[1] == 1, {
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (dir +/+ ".." +/+ "resources" +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.postln.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
}, {
pSeq = [loadModelJSON.value(msg[2].asString.parseJSON)["music_data"].postln];
patterns = genPatterns.value(pSeq, addr, true);
});
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
addr.sendMsg("/one_shot", 0);
});
player = player.play
});
}, \transport);
)
(
//synthdefs
~stringModelBusArray = 4.collect({Bus.audio(s, 1)});
~sineBusArray = 4.collect({Bus.audio(s, 1)});
~bassBusArray = 1.collect({Bus.audio(s, 1)});
~hdustBusArray = 1.collect({Bus.audio(s, 1)});
~samplerBusArray = 2.collect({Bus.audio(s, 1)});
~sBuf = Buffer.alloc(s, 10, 2);
SynthDef(\string_model, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var trig, exc, sig1, sig2, noHarms;
noHarms = 30;
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.8).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar(Select.kr(busIndex, ~stringModelBusArray), sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
//Out.ar([0, 1], sig1 * EnvGen.kr(Env.asr(dur, 0.3, 1), gate, doneAction: 2));
}).add;
SynthDef(\sine, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var sig;
sig = SinOsc.ar(freq);
Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.asr(0.3, 0.4, 0.3), gate, timeScale: dur, doneAction: 2));
}).add;
SynthDef(\mixer, {arg freq, gate = 1, sustain, amp, dur, out;
var nameSpaces, sigs, hierarchicalDust;
hierarchicalDust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1))
);
sigs = [~stringModelBusArray, ~sineBusArray, ~bassBusArray, ~hdustBusArray, ~samplerBusArray].collect({arg busArray, i;
var nameSpace, sig;
nameSpace = ['string', 'sine', 'bass', 'hdust', 'sampler'][i];
sig = busArray.collect({arg bus, c; In.ar(bus, 1) * NamedControl.kr(\ ++ nameSpace ++ '_volume_' ++ c, 1, 0.1)});
sig = sig.collect({arg channel, c; Pan2.ar(channel, NamedControl.kr(\ ++ nameSpace ++ '_pan_' ++ c, i / (busArray.size - 1), 0.1) * 2 - 1)});
sig = sig.collect({arg channel, c; channel * NamedControl.kr(\ ++ nameSpace ++ '_mute_' ++ c, 1, 0.1)});
sig = Mix.ar(sig) * pow(NamedControl.kr(\ ++ nameSpace ++ '_volume_master', 1, 0.1), 2);
});
sigs = Mix.ar(sigs / 4);
Out.ar(0, sigs)
}).add;
SynthDef(\bass, {
var switches, drone;
switches = {|i| Dust.kr(0.1)} ! 9;
drone = {|i| var harm = pow(2, 2 - (i / 3).trunc), amp = (1 / pow(harm, 2));
SinOsc.ar(60 * harm + TRand.kr(-3, 3, switches[i]), 0, amp)} ! 9;
Out.ar(~bassBusArray[0], Mix.new(drone) * 0.2);
}).add;
SynthDef(\sampler, {
Out.ar(~samplerBusArray, PlayBuf.ar(2, ~sBuf, BufRateScale.kr(~sBuf), doneAction: 2))
}).add;
// main routine
SynthDef(\hdust, {
arg gate = 0;
var hierarchical_dust, low_sine, high_sine, brown_noise, white_noise;
// this triggers the combinations of sources
// it is similar to the Supercollider UGen called dust but with a hierarchical structure
hierarchical_dust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1)) *
TIRand.kr(0, 1, Impulse.kr(0.1))
);
// adjust the multiplier at the end of each line for adjusting levels
// note with each trigger, each source has a 1 in 3 chance of sounding
low_sine = SinOsc.ar(76.midicps / 16) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.1;
high_sine = SinOsc.ar(76.midicps * 8) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.01;
brown_noise = BrownNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.025;
white_noise = WhiteNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.02;
Out.ar(~hdustBusArray[0],
((low_sine + high_sine + brown_noise + white_noise) )
);
}).add;
)
(
var bass, hdust, sampler, mixer;
bass = Synth.tail(~group, \bass);
hdust = Synth.tail(~group, \hdust);
sampler = Synth.head(~group, \sampler);
mixer = Synth.tail(~group, \mixer);
OSCdef(\mixer, {arg msg, time, addr, port;
mixer.set((msg[1] ++ '_' ++ msg[2] ++ '_' ++ msg[3]), msg[4])
}, \mixer);
OSCdef(\sampler, {arg msg, time, addr, port;
msg.postln;
sampler.free;
~sBuf.free;
~sBuf = Buffer.read(s, msg[1].asString.postln, action: {sampler = Synth.head(~group, \sampler)});
}, \sampler);
)
/* old something
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
*/

@ -1,51 +0,0 @@
{
"music_data":
[
[
[
[ [ [ 0, 0, 0, 0, 1, -1 ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 1 ],
[ [ [ 0, 0, 0, 0, 1, -1 ], [ 1, -1, 0, 0, 1, -1 ], [ "Rest" ], [ "Rest" ] ], 2.875 ],
[ [ [ 0, 0, 0, 0, 1, -1 ], [ 1, -1, 0, 0, 1, -1 ], [ "Rest" ], [ 2, -1, 0, 0, 1, -1 ] ], 3.75 ]
],
[
[ [ [ 0, 0, 0, 0, 1, -1 ], [ 1, -1, 0, 0, 1, -1 ], [ 2, 0, 0, 0, 1, -1 ], [ 2, -1, 0, 0, 1, -1 ] ], 2.875 ],
[ [ [ 0, 0, 0, 0, 1, -1 ], [ 0, 0, 1, 0, 1, -1 ], [ 2, 0, 0, 0, 1, -1 ], [ 2, -1, 0, 0, 1, -1 ] ], 1.75 ],
[ [ [ 0, 0, 0, 0, 1, -1 ], [ 0, 0, 1, 0, 1, -1 ], [ 2, 0, 0, 0, 1, -1 ], [ 1, 1, 0, 0, 1, -1 ] ], 1.875 ],
[ [ [ -1, 0, 0, 1, 1, -1 ], [ 0, 0, 1, 0, 1, -1 ], [ 2, 0, 0, 0, 1, -1 ], [ 1, 1, 0, 0, 1, -1 ] ], 1.75 ],
[ [ [ -1, 0, 0, 1, 1, -1 ], [ 1, 0, 0, -1, 1, -1 ], [ 2, 0, 0, 0, 1, -1 ], [ 1, 1, 0, 0, 1, -1 ] ], 2.75 ],
[ [ [ -1, 0, 0, 1, 1, -1 ], [ 1, 0, 0, -1, 1, -1 ], [ 2, 0, 0, 0, 1, -1 ], [ "Rest" ] ], 0.375 ],
[ [ [ -1, 0, 0, 1, 1, -1 ], [ 1, 0, 0, -1, 1, -1 ], [ "Rest" ], [ "Rest" ] ], 0.625 ],
[ [ [ "Rest" ], [ 1, 0, 0, -1, 1, -1 ], [ "Rest" ], [ "Rest" ] ], 0.5 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 4 ]
]
]
],
"last_changes":
[
[ [ 0, 0, 0, 0, 1, -1 ], [ 1, -1, 0, 0, 1, -1 ], [ 2, 0, 0, 0, 1, -1 ], [ 2, -1, 0, 0, 1, -1 ] ],
[ [ 0, 0, 0, 0, 1, -1 ], [ 0, 0, 1, 0, 1, -1 ], [ 2, 0, 0, 0, 1, -1 ], [ 2, -1, 0, 0, 1, -1 ] ],
[ [ 0, 0, 0, 0, 1, -1 ], [ 0, 0, 1, 0, 1, -1 ], [ 2, 0, 0, 0, 1, -1 ], [ 1, 1, 0, 0, 1, -1 ] ],
[ [ -1, 0, 0, 1, 1, -1 ], [ 0, 0, 1, 0, 1, -1 ], [ 2, 0, 0, 0, 1, -1 ], [ 1, 1, 0, 0, 1, -1 ] ],
[ [ -1, 0, 0, 1, 1, -1 ], [ 1, 0, 0, -1, 1, -1 ], [ 2, 0, 0, 0, 1, -1 ], [ 1, 1, 0, 0, 1, -1 ] ]
],
"cur_uid": "6f1305ed",
"ref_uid": "tmp",
"order_seed": 414624,
"dur_seed": 257250,
"motifs_seed": 938764,
"entrances_probs_vals": [ 0, 1.11, 3.2936507936508, 0.85, 2.1153846153846, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"passages_probs_vals": [ 0, 1.11, 3.2936507936508, 0.85, 2.1153846153846, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"exits_probs_vals": [ 0, 1.83, 4.8809523809524, 0.24725274725275, 1.15, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"ranges": [ [ -1100, 460.68111455108 ], [ -507, 1084.8297213622 ], [ 884.21052631579, 2237 ], [ 1040.2476780186, 2053 ] ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"order":
[
[ [ 0, 1 ], [ 3 ], [ 2 ] ],
[ [ 2 ], [ 1, 3, 0, 1 ], [ ] ]
],
"sus_weights": [ 0.73, 0.23, 0 ],
"order_size": [ 2, 2 ],
"passages_size": [ 0, 2 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,907 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict, saveLedger;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold.deepCopy});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~msg.postln;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr, oneShot = false;
var voices, durs, pbinds, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
pbinds = voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs, attacks, rels, amps;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
//attacks = 2.collect({rrand(1, 3)}) ++ freqs.drop(2).collect({rrand(0.3, 0.5)});
attacks = fDurs.collect({arg dur; dur * rrand(0.2, 0.4)});
//rels = freqs.drop(2).collect({rrand(0.3, 0.5)}) ++ 2.collect({rrand(1, 3)});
rels = (clumps.size - 1).collect({arg c;
if(clumps[c + 1][0] == ["Rest"], {rrand(1, 3)}, {rrand(0.3, 0.5)});
});
amps = freqs.collect({rrand(0.6, 0.99)});
[
Pbind(
\instrument, \string_model,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\attack, Pseq(attacks, 1),
\sustain, Pseq(fDurs, 1),
\release, Pseq(rels, 1),
//\amp, Pseq(amps, 1),
\amp, Pbrown(0.5, 1, 0.5),
\busIndex, v
),
Pbind(
\instrument, \sine,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
)
]
}).flatten;
if(oneShot.not, {
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
pbinds = pbinds ++
[
Pbind(
\type, \osc,
\addr, addr,
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
});
res = Ppar(pbinds);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
//res.postln;
if(res.every({arg char; char.isDecDigit}), {res = res.asInteger});
});
});
res
};
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict, skipLastXChanges = false;
var tmpLastXChanges;
tmpLastXChanges = lastXChanges.deepCopy;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
if(skipLastXChanges, {lastXChanges = tmpLastXChanges});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
saveLedger = {arg ledger, path;
var file;
file = File(path, "w");
file.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
file.close;
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
~group = group;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\save_ledger, {arg msg, time, addr, port;
msg.postln;
ledger = msgInterpret.value(msg[1].asString.parseJSON["ledger"], false).postln;
//loadLedgerJSON.value(msg[0])
saveLedger.value(ledger, msg[2].asString);
//loadLedgerFile.value(msg[1].asString);
}, \save_ledger);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict, true);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
if(refUID == nil, {lastXChanges = [initVoices.value().deepCopy]});
if((refUID != nil) && (refUID != "tmp"), {
var file;
file = File((dir +/+ ".." +/+ "resources" +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
lastXChanges = msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
refUID.postln;
lastXChanges.collect({arg item; item.postln});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
seq = seedFunc.value(genMotif, motifSeed).value;
lastXChanges.collect({arg item; item.postln});
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
addr.sendMsg("/generated", path, modelString, ledgerPath);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
musicChanged = (musicData != seq).postln;
curUID = genUID.value;
File.mkdir((dir +/+ ".." +/+ "resources" +/+ curUID).standardizePath);
File.copy(exPath, (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {
seq = musicData;
dict["music_data"] = seq;
dict["motif_edited"] = "true"
});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
ledger = ledger.drop(-1).add(curUID);
saveLedger.value(ledger, ledgerPath);
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
group.set(\release, 2);
group.set(\gate, 0);
player.stop;
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
if(msg[1] == 1, {
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (dir +/+ ".." +/+ "resources" +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.postln.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
}, {
pSeq = [loadModelJSON.value(msg[2].asString.parseJSON)["music_data"].postln];
patterns = genPatterns.value(pSeq, addr, true);
});
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
addr.sendMsg("/one_shot", 0);
});
player = player.play
});
}, \transport);
)
(
//synthdefs
~stringModelBusArray = 4.collect({Bus.audio(s, 1)});
~sineBusArray = 4.collect({Bus.audio(s, 1)});
~bassBusArray = 1.collect({Bus.audio(s, 1)});
~hdustBusArray = 1.collect({Bus.audio(s, 1)});
~samplerBusArray = 2.collect({Bus.audio(s, 1)});
~sBuf = Buffer.alloc(s, 10, 2);
SynthDef(\string_model, {arg freq, gate = 1, sustain, amp, dur, attack, release = 1, busIndex = 0;
var trig, exc, sig1, sig2, noHarms;
noHarms = rrand(20, 40);
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.8).softclip;
//sig1 = HPF.ar(sig1, 300);
Out.ar(Select.kr(busIndex, ~stringModelBusArray), sig1 * amp * EnvGen.kr(Env.adsr(attack, 0.3, 0.9, release, 0.9, -3), gate, doneAction: 2));
//Out.ar([0, 1], sig1 * EnvGen.kr(Env.asr(dur, 0.3, 1), gate, doneAction: 2));
}).add;
SynthDef(\sine, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var sig;
sig = SinOsc.ar(freq);
Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.asr(0.3, 0.4, 0.3), gate, timeScale: dur, doneAction: 2));
//Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.sine(dur), gate, doneAction: 2));
}).add;
SynthDef(\mixer, {arg freq, gate = 1, sustain, amp, dur, out;
var nameSpaces, sigs, hierarchicalDust;
hierarchicalDust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1))
);
sigs = [~stringModelBusArray, ~sineBusArray, ~bassBusArray, ~hdustBusArray, ~samplerBusArray].collect({arg busArray, i;
var nameSpace, sig;
nameSpace = ['string', 'sine', 'bass', 'hdust', 'sampler'][i];
sig = busArray.collect({arg bus, c; In.ar(bus, 1) * NamedControl.kr(\ ++ nameSpace ++ '_volume_' ++ c, 1, 0.1)});
sig = sig.collect({arg channel, c; Pan2.ar(channel, NamedControl.kr(\ ++ nameSpace ++ '_pan_' ++ c, i / (busArray.size - 1), 0.1) * 2 - 1)});
sig = sig.collect({arg channel, c; channel * NamedControl.kr(\ ++ nameSpace ++ '_mute_' ++ c, 1, 0.1)});
sig = Mix.ar(sig) * pow(NamedControl.kr(\ ++ nameSpace ++ '_volume_master', 1, 0.1), 2);
});
sigs = Mix.ar(sigs / 4);
Out.ar(0, sigs)
}).add;
SynthDef(\bass, {
var switches, drone;
switches = {|i| Dust.kr(0.1)} ! 9;
drone = {|i| var harm = pow(2, 2 - (i / 3).trunc), amp = (1 / pow(harm, 2));
SinOsc.ar(60 * harm + TRand.kr(-3, 3, switches[i]), 0, amp)} ! 9;
Out.ar(~bassBusArray[0], Mix.new(drone) * 0.2);
}).add;
SynthDef(\sampler, {
Out.ar(~samplerBusArray, PlayBuf.ar(2, ~sBuf, BufRateScale.kr(~sBuf), doneAction: 2))
}).add;
// main routine
SynthDef(\hdust, {
arg gate = 0;
var hierarchical_dust, low_sine, high_sine, brown_noise, white_noise;
// this triggers the combinations of sources
// it is similar to the Supercollider UGen called dust but with a hierarchical structure
hierarchical_dust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1)) *
TIRand.kr(0, 1, Impulse.kr(0.1))
);
// adjust the multiplier at the end of each line for adjusting levels
// note with each trigger, each source has a 1 in 3 chance of sounding
low_sine = SinOsc.ar(76.midicps / 16) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.1;
high_sine = SinOsc.ar(76.midicps * 8) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.01;
brown_noise = BrownNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.025;
white_noise = WhiteNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.02;
Out.ar(~hdustBusArray[0],
((low_sine + high_sine + brown_noise + white_noise) )
);
}).add;
)
(
var bass, hdust, sampler, mixer;
bass = Synth.tail(~group, \bass);
hdust = Synth.tail(~group, \hdust);
sampler = Synth.head(~group, \sampler);
mixer = Synth.tail(~group, \mixer);
OSCdef(\mixer, {arg msg, time, addr, port;
mixer.set((msg[1] ++ '_' ++ msg[2] ++ '_' ++ msg[3]), msg[4])
}, \mixer);
OSCdef(\sampler, {arg msg, time, addr, port;
msg.postln;
sampler.free;
~sBuf.free;
~sBuf = Buffer.read(s, msg[1].asString.postln, action: {sampler = Synth.head(~group, \sampler)});
}, \sampler);
)
/* old something
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
*/

@ -1,48 +0,0 @@
{
"music_data":
[
[
[
[ [ [ "Rest" ], [ "Rest" ], [ 6, -5, -4, 0, -1, -2 ], [ "Rest" ] ], 0.875 ],
[ [ [ "Rest" ], [ "Rest" ], [ 6, -5, -4, 0, -1, -2 ], [ 7, -5, -2, 0, -1, -2 ] ], 3 ],
[ [ [ 8, -6, -4, 0, -1, -2 ], [ "Rest" ], [ 6, -5, -4, 0, -1, -2 ], [ 7, -5, -2, 0, -1, -2 ] ], 1.25 ],
[ [ [ 8, -6, -4, 0, -1, -2 ], [ 6, -5, -4, 0, -1, -3 ], [ 6, -5, -4, 0, -1, -2 ], [ 7, -5, -2, 0, -1, -2 ] ], 2 ]
],
[
[ [ [ 8, -6, -4, 0, -1, -2 ], [ "Rest" ], [ 6, -5, -4, 0, -1, -2 ], [ 7, -5, -2, 0, -1, -2 ] ], 0 ],
[ [ [ 8, -6, -4, 0, -1, -2 ], [ "Rest" ], [ 7, -7, -4, 0, -1, -2 ], [ 7, -5, -2, 0, -1, -2 ] ], 2.375 ],
[ [ [ 8, -6, -4, 0, -1, -2 ], [ "Rest" ], [ 7, -7, -4, 0, -1, -2 ], [ "Rest" ] ], 0.875 ],
[ [ [ "Rest" ], [ "Rest" ], [ 7, -7, -4, 0, -1, -2 ], [ "Rest" ] ], 0 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 1.625 ]
]
]
],
"last_changes":
[
[ [ 7, -5, -3, 0, -1, -2 ], [ 5, -5, -2, 0, -2, -2 ], [ 5, -5, -2, 0, 0, -2 ], [ 7, -5, -2, 0, -1, -2 ] ],
[ [ 7, -5, -3, 0, -1, -2 ], [ 5, -5, -2, 0, -2, -2 ], [ 6, -5, -4, 0, -1, -2 ], [ 7, -5, -2, 0, -1, -2 ] ],
[ [ 8, -6, -4, 0, -1, -2 ], [ 5, -5, -2, 0, -2, -2 ], [ 6, -5, -4, 0, -1, -2 ], [ 7, -5, -2, 0, -1, -2 ] ],
[ [ 8, -6, -4, 0, -1, -2 ], [ 6, -5, -4, 0, -1, -3 ], [ 6, -5, -4, 0, -1, -2 ], [ 7, -5, -2, 0, -1, -2 ] ],
[ [ 8, -6, -4, 0, -1, -2 ], [ 6, -5, -4, 0, -1, -3 ], [ 7, -7, -4, 0, -1, -2 ], [ 7, -5, -2, 0, -1, -2 ] ]
],
"cur_uid": "703e109b",
"ref_uid": "tmp",
"order_seed": 550421,
"dur_seed": 315472,
"motifs_seed": 675194,
"entrances_probs_vals": [ 0.52, 0.56, 2.4206349206349, 0.25, 1.24, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"passages_probs_vals": [ 0.52, 0.56, 2.4206349206349, 0.25, 1.24, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"exits_probs_vals": [ 0.52, 0.56, 2.4206349206349, 0.25, 1.24, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"ranges": [ [ 1211.1455108359, 2400 ], [ -1650, 450 ], [ -405, 1137 ], [ 951.08359133127, 2053 ] ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"order":
[
[ [ 2, 3 ], [ 0, 1 ], [ ] ],
[ [ 3, 0 ], [ 2 ], [ 1 ] ]
],
"sus_weights": [ 0.73, 0.49, 0 ],
"order_size": [ 1, 3 ],
"passages_size": [ 0, 0 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,907 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict, saveLedger;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold.deepCopy});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~msg.postln;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr, oneShot = false;
var voices, durs, pbinds, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
pbinds = voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs, attacks, rels, amps;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
//attacks = 2.collect({rrand(1, 3)}) ++ freqs.drop(2).collect({rrand(0.3, 0.5)});
attacks = fDurs.collect({arg dur; dur * rrand(0.2, 0.4)});
//rels = freqs.drop(2).collect({rrand(0.3, 0.5)}) ++ 2.collect({rrand(1, 3)});
rels = (clumps.size - 1).collect({arg c;
if(clumps[c + 1][0] == ["Rest"], {rrand(1, 3)}, {rrand(0.3, 0.5)});
});
amps = freqs.collect({rrand(0.6, 0.99)});
[
Pbind(
\instrument, \string_model,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\attack, Pseq(attacks, 1),
\sustain, Pseq(fDurs, 1),
\release, Pseq(rels, 1),
//\amp, Pseq(amps, 1),
\amp, Pbrown(0.5, 1, 0.5),
\busIndex, v
),
Pbind(
\instrument, \sine,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
)
]
}).flatten;
if(oneShot.not, {
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
pbinds = pbinds ++
[
Pbind(
\type, \osc,
\addr, addr,
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
});
res = Ppar(pbinds);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
//res.postln;
if(res.every({arg char; char.isDecDigit}), {res = res.asInteger});
});
});
res
};
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict, skipLastXChanges = false;
var tmpLastXChanges;
tmpLastXChanges = lastXChanges.deepCopy;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
if(skipLastXChanges, {lastXChanges = tmpLastXChanges});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
saveLedger = {arg ledger, path;
var file;
file = File(path, "w");
file.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
file.close;
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
~group = group;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\save_ledger, {arg msg, time, addr, port;
msg.postln;
ledger = msgInterpret.value(msg[1].asString.parseJSON["ledger"], false).postln;
//loadLedgerJSON.value(msg[0])
saveLedger.value(ledger, msg[2].asString);
//loadLedgerFile.value(msg[1].asString);
}, \save_ledger);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict, true);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
if(refUID == nil, {lastXChanges = [initVoices.value().deepCopy]});
if((refUID != nil) && (refUID != "tmp"), {
var file;
file = File((dir +/+ ".." +/+ "resources" +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
lastXChanges = msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
refUID.postln;
lastXChanges.collect({arg item; item.postln});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
seq = seedFunc.value(genMotif, motifSeed).value;
lastXChanges.collect({arg item; item.postln});
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
addr.sendMsg("/generated", path, modelString, ledgerPath);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
musicChanged = (musicData != seq).postln;
curUID = genUID.value;
File.mkdir((dir +/+ ".." +/+ "resources" +/+ curUID).standardizePath);
File.copy(exPath, (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {
seq = musicData;
dict["music_data"] = seq;
dict["motif_edited"] = "true"
});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
ledger = ledger.drop(-1).add(curUID);
saveLedger.value(ledger, ledgerPath);
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
group.set(\release, 2);
group.set(\gate, 0);
player.stop;
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
if(msg[1] == 1, {
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (dir +/+ ".." +/+ "resources" +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.postln.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
}, {
pSeq = [loadModelJSON.value(msg[2].asString.parseJSON)["music_data"].postln];
patterns = genPatterns.value(pSeq, addr, true);
});
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
addr.sendMsg("/one_shot", 0);
});
player = player.play
});
}, \transport);
)
(
//synthdefs
~stringModelBusArray = 4.collect({Bus.audio(s, 1)});
~sineBusArray = 4.collect({Bus.audio(s, 1)});
~bassBusArray = 1.collect({Bus.audio(s, 1)});
~hdustBusArray = 1.collect({Bus.audio(s, 1)});
~samplerBusArray = 2.collect({Bus.audio(s, 1)});
~sBuf = Buffer.alloc(s, 10, 2);
SynthDef(\string_model, {arg freq, gate = 1, sustain, amp, dur, attack, release = 1, busIndex = 0;
var trig, exc, sig1, sig2, noHarms;
noHarms = rrand(20, 40);
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.8).softclip;
//sig1 = HPF.ar(sig1, 300);
Out.ar(Select.kr(busIndex, ~stringModelBusArray), sig1 * amp * EnvGen.kr(Env.adsr(attack, 0.3, 0.9, release, 0.9, -3), gate, doneAction: 2));
//Out.ar([0, 1], sig1 * EnvGen.kr(Env.asr(dur, 0.3, 1), gate, doneAction: 2));
}).add;
SynthDef(\sine, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var sig;
sig = SinOsc.ar(freq);
Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.asr(0.3, 0.4, 0.3), gate, timeScale: dur, doneAction: 2));
//Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.sine(dur), gate, doneAction: 2));
}).add;
SynthDef(\mixer, {arg freq, gate = 1, sustain, amp, dur, out;
var nameSpaces, sigs, hierarchicalDust;
hierarchicalDust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1))
);
sigs = [~stringModelBusArray, ~sineBusArray, ~bassBusArray, ~hdustBusArray, ~samplerBusArray].collect({arg busArray, i;
var nameSpace, sig;
nameSpace = ['string', 'sine', 'bass', 'hdust', 'sampler'][i];
sig = busArray.collect({arg bus, c; In.ar(bus, 1) * NamedControl.kr(\ ++ nameSpace ++ '_volume_' ++ c, 1, 0.1)});
sig = sig.collect({arg channel, c; Pan2.ar(channel, NamedControl.kr(\ ++ nameSpace ++ '_pan_' ++ c, i / (busArray.size - 1), 0.1) * 2 - 1)});
sig = sig.collect({arg channel, c; channel * NamedControl.kr(\ ++ nameSpace ++ '_mute_' ++ c, 1, 0.1)});
sig = Mix.ar(sig) * pow(NamedControl.kr(\ ++ nameSpace ++ '_volume_master', 1, 0.1), 2);
});
sigs = Mix.ar(sigs / 4);
Out.ar(0, sigs)
}).add;
SynthDef(\bass, {
var switches, drone;
switches = {|i| Dust.kr(0.1)} ! 9;
drone = {|i| var harm = pow(2, 2 - (i / 3).trunc), amp = (1 / pow(harm, 2));
SinOsc.ar(60 * harm + TRand.kr(-3, 3, switches[i]), 0, amp)} ! 9;
Out.ar(~bassBusArray[0], Mix.new(drone) * 0.2);
}).add;
SynthDef(\sampler, {
Out.ar(~samplerBusArray, PlayBuf.ar(2, ~sBuf, BufRateScale.kr(~sBuf), doneAction: 2))
}).add;
// main routine
SynthDef(\hdust, {
arg gate = 0;
var hierarchical_dust, low_sine, high_sine, brown_noise, white_noise;
// this triggers the combinations of sources
// it is similar to the Supercollider UGen called dust but with a hierarchical structure
hierarchical_dust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1)) *
TIRand.kr(0, 1, Impulse.kr(0.1))
);
// adjust the multiplier at the end of each line for adjusting levels
// note with each trigger, each source has a 1 in 3 chance of sounding
low_sine = SinOsc.ar(76.midicps / 16) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.1;
high_sine = SinOsc.ar(76.midicps * 8) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.01;
brown_noise = BrownNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.025;
white_noise = WhiteNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.02;
Out.ar(~hdustBusArray[0],
((low_sine + high_sine + brown_noise + white_noise) )
);
}).add;
)
(
var bass, hdust, sampler, mixer;
bass = Synth.tail(~group, \bass);
hdust = Synth.tail(~group, \hdust);
sampler = Synth.head(~group, \sampler);
mixer = Synth.tail(~group, \mixer);
OSCdef(\mixer, {arg msg, time, addr, port;
mixer.set((msg[1] ++ '_' ++ msg[2] ++ '_' ++ msg[3]), msg[4])
}, \mixer);
OSCdef(\sampler, {arg msg, time, addr, port;
msg.postln;
sampler.free;
~sBuf.free;
~sBuf = Buffer.read(s, msg[1].asString.postln, action: {sampler = Synth.head(~group, \sampler)});
}, \sampler);
)
/* old something
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
*/

@ -1,83 +0,0 @@
{
"music_data":
[
[
[
[ [ [ "Rest" ], [ "Rest" ], [ 0, 2, 0, 1, -2, 0 ], [ "Rest" ] ], 1.625 ],
[ [ [ "Rest" ], [ "Rest" ], [ 0, 2, 0, 1, -2, 0 ], [ 0, 2, 0, 2, -2, -1 ] ], 1.625 ],
[ [ [ -3, 2, 0, 2, -2, -2 ], [ "Rest" ], [ 0, 2, 0, 1, -2, 0 ], [ 0, 2, 0, 2, -2, -1 ] ], 2.75 ]
],
[
[ [ [ -3, 2, 0, 2, -2, -2 ], [ "Rest" ], [ 0, 2, 0, 1, -2, 0 ], [ "Rest" ] ], 0.75 ],
[ [ [ -3, 2, 0, 2, -2, -2 ], [ -2, 3, 0, 1, -2, -1 ], [ 0, 2, 0, 1, -2, 0 ], [ "Rest" ] ], 2.75 ],
[ [ [ -3, 3, 0, 1, -3, -1 ], [ -2, 3, 0, 1, -2, -1 ], [ 0, 2, 0, 1, -2, 0 ], [ "Rest" ] ], 1.25 ],
[ [ [ -3, 3, 0, 1, -3, -1 ], [ -2, 3, 0, 1, -2, -1 ], [ 1, 3, 0, 0, -2, -1 ], [ "Rest" ] ], 2.5 ]
],
[
[ [ [ -3, 3, 0, 1, -3, -1 ], [ -2, 3, 0, 1, -2, -1 ], [ "Rest" ], [ "Rest" ] ], 0.75 ],
[ [ [ -3, 3, 0, 1, -3, -1 ], [ -2, 3, 0, 1, -2, -1 ], [ "Rest" ], [ 0, 2, 0, 2, -2, -1 ] ], 3.25 ],
[ [ [ -3, 3, 0, 1, -3, -1 ], [ -2, 2, -1, 2, -2, -1 ], [ "Rest" ], [ 0, 2, 0, 2, -2, -1 ] ], 0.75 ],
[ [ [ -3, 1, 0, 2, -2, -1 ], [ -2, 2, -1, 2, -2, -1 ], [ "Rest" ], [ 0, 2, 0, 2, -2, -1 ] ], 1.625 ],
[ [ [ -3, 1, 0, 2, -2, -1 ], [ -3, 3, 0, 2, -2, -1 ], [ "Rest" ], [ 0, 2, 0, 2, -2, -1 ] ], 1 ],
[ [ [ -3, 1, 0, 2, -2, -1 ], [ -2, 1, 0, 2, -2, -1 ], [ "Rest" ], [ 0, 2, 0, 2, -2, -1 ] ], 1.25 ],
[ [ [ -3, 2, 0, 1, -2, -1 ], [ -2, 1, 0, 2, -2, -1 ], [ "Rest" ], [ 0, 2, 0, 2, -2, -1 ] ], 0.875 ],
[ [ [ -3, 2, 0, 2, -2, -2 ], [ -2, 1, 0, 2, -2, -1 ], [ "Rest" ], [ 0, 2, 0, 2, -2, -1 ] ], 1.625 ]
],
[
[ [ [ "Rest" ], [ -2, 1, 0, 2, -2, -1 ], [ "Rest" ], [ 0, 2, 0, 2, -2, -1 ] ], 0.75 ],
[ [ [ "Rest" ], [ -2, 1, 0, 2, -2, -1 ], [ 1, 3, 0, 0, -2, -1 ], [ 0, 2, 0, 2, -2, -1 ] ], 2.625 ],
[ [ [ "Rest" ], [ -2, 4, 0, 0, -2, -1 ], [ 1, 3, 0, 0, -2, -1 ], [ 0, 2, 0, 2, -2, -1 ] ], 0.75 ],
[ [ [ "Rest" ], [ -2, 4, 0, 0, -2, -1 ], [ 1, 3, 0, 0, -2, -1 ], [ 0, 3, 0, 1, -2, -1 ] ], 0.875 ],
[ [ [ "Rest" ], [ -2, 4, 0, 0, -2, -1 ], [ 1, 3, 0, 0, -2, -1 ], [ 0, 3, 0, 0, -2, 0 ] ], 2.375 ]
],
[
[ [ [ "Rest" ], [ "Rest" ], [ 1, 3, 0, 0, -2, -1 ], [ 0, 3, 0, 0, -2, 0 ] ], 1.625 ],
[ [ [ -3, 2, 0, 2, -2, -2 ], [ "Rest" ], [ 1, 3, 0, 0, -2, -1 ], [ 0, 3, 0, 0, -2, 0 ] ], 2.5 ],
[ [ [ -3, 2, 0, 2, -2, -2 ], [ "Rest" ], [ 0, 3, 0, 2, -2, -2 ], [ 0, 3, 0, 0, -2, 0 ] ], 1.25 ],
[ [ [ -3, 2, 0, 2, -2, -2 ], [ "Rest" ], [ 0, 3, 0, 2, -2, -2 ], [ 0, 2, 0, 2, -1, -2 ] ], 2.75 ]
],
[
[ [ [ -3, 2, 0, 2, -2, -2 ], [ -2, 4, 0, 0, -2, -1 ], [ 0, 3, 0, 2, -2, -2 ], [ 0, 2, 0, 2, -1, -2 ] ], 1.75 ],
[ [ [ -3, 2, 0, 2, -2, -2 ], [ -2, 4, 0, 0, -2, -1 ], [ 0, 4, 1, 0, -2, -1 ], [ 0, 2, 0, 2, -1, -2 ] ], 1.25 ],
[ [ [ -3, 2, 0, 2, -2, -2 ], [ -2, 4, 0, 0, -2, -1 ], [ 0, 4, 1, 0, -2, -1 ], [ 1, 4, 0, 0, -2, -2 ] ], 1.625 ],
[ [ [ -4, 4, 0, 1, -2, -1 ], [ -2, 4, 0, 0, -2, -1 ], [ 0, 4, 1, 0, -2, -1 ], [ 1, 4, 0, 0, -2, -2 ] ], 2.125 ],
[ [ [ "Rest" ], [ -2, 4, 0, 0, -2, -1 ], [ 0, 4, 1, 0, -2, -1 ], [ 1, 4, 0, 0, -2, -2 ] ], 1.375 ],
[ [ [ "Rest" ], [ "Rest" ], [ 0, 4, 1, 0, -2, -1 ], [ 1, 4, 0, 0, -2, -2 ] ], 1 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ 1, 4, 0, 0, -2, -2 ] ], 0.875 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 3 ]
]
]
],
"last_changes":
[
[ [ -3, 2, 0, 2, -2, -2 ], [ -2, 4, 0, 0, -2, -1 ], [ 0, 3, 0, 2, -2, -2 ], [ 0, 3, 0, 0, -2, 0 ] ],
[ [ -3, 2, 0, 2, -2, -2 ], [ -2, 4, 0, 0, -2, -1 ], [ 0, 3, 0, 2, -2, -2 ], [ 0, 2, 0, 2, -1, -2 ] ],
[ [ -3, 2, 0, 2, -2, -2 ], [ -2, 4, 0, 0, -2, -1 ], [ 0, 4, 1, 0, -2, -1 ], [ 0, 2, 0, 2, -1, -2 ] ],
[ [ -3, 2, 0, 2, -2, -2 ], [ -2, 4, 0, 0, -2, -1 ], [ 0, 4, 1, 0, -2, -1 ], [ 1, 4, 0, 0, -2, -2 ] ],
[ [ -4, 4, 0, 1, -2, -1 ], [ -2, 4, 0, 0, -2, -1 ], [ 0, 4, 1, 0, -2, -1 ], [ 1, 4, 0, 0, -2, -2 ] ]
],
"cur_uid": "74faf83f",
"ref_uid": "tmp",
"order_seed": 329828,
"dur_seed": 441173,
"motifs_seed": 330575,
"entrances_probs_vals": [ 0, 0.48, 1.7460317460317, 0.58, 1.68, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"passages_probs_vals": [ 0, 0.48, 1.7460317460317, 0.58, 1.68, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"exits_probs_vals": [ 0, 0.48, 1.7460317460317, 0.58, 1.68, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"ranges": [ [ -3600, -1408.0495356037 ], [ -3600, -609.2879256966 ], [ 858.20433436532, 2066 ], [ 951.08359133127, 2053 ] ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"order":
[
[ [ 2, 3 ], [ 0 ], [ 1 ] ],
[ [ 1 ], [ 0, 2 ], [ 3 ] ],
[ [ 3 ], [ 1, 0, 1, 1, 0, 0 ], [ 2 ] ],
[ [ 2 ], [ 1, 3, 3 ], [ 0 ] ],
[ [ 0 ], [ 2, 3 ], [ 1 ] ],
[ [ 1 ], [ 2, 3, 0 ], [ ] ]
],
"sus_weights": [ 0.73, 0.49, 0 ],
"order_size": [ 1, 7 ],
"passages_size": [ 0, 5 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,719 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON;
// model vars
//(model and global vars mostly set by OSC funcs
var curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, orders, susWeights, passagesWeights, passagesSize, orderSize;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc;
// other global vars
var lastXChanges, popSize, exPath, dir, primes, dims, tuples,
seq, group, player, ledgerPath, ledger, currentlyPlayingUID;
// install JSON quark
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, min, max, envData;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
durFunc = {arg allowChord;
var res;
res = if(allowChord.not, {
pTable.tableRand * (max - min) + min
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (max - min) + min});
}).round(0.125);
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, durSeed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
allowChord = if((sus ++ silent).includes(ins), {
(sus ++ silent).includes(ins) && (ins != sus.last);
}, {
if(i < (flatOrder.size - 1), {(isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not}, {false});
});
dur = passagesDurFunc.value(allowChord);
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(isLastOrder, {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
dur = passagesDurFunc.value(allowChord);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
genPatterns = {arg inSeq, addr;
var voices, durs, patterns, res, indices, sectionDurs, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
indices = inSeq.collect({arg mSeq, m; mSeq[1]});
ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
res = Ppar(
voices.flop.collect({arg voice;
var clumps, hdScores, freqs, fDurs;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\instrument, \test,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1)
)
}) ++
[
Pbind(
\type, \osc,
\addr, addr,
\indexPath, "/cur_play_index",
\indexMsg, Pseq(indices.postln, 1),
\seqPath, "/mus_seq",
\seqMsg, Pseq(seq, 1),
\dur, Pseq(sectionDurs, 1)
);
]
);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < (maxDepth - 0), {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
//res.postln;
if(res.every({arg char; char.isDecDigit}), {res = res.asInteger});
});
});
res
};
writeResources = {arg path;
var file, nameSpaces, modelItems, resString;
file = File(path,"w");
nameSpaces = [
"music_data", "last_changes",
"cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size"
];
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize
];
resString = [nameSpaces, modelItems].flop.collect({arg item;
var nameSpace, modelItem, depth = 0, insert = " ";
# nameSpace, modelItem = item;
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(modelItem.postln, depth).postln
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg model;
var nameSpaces, data;
//model = File(path, "r").readAllString.parseJSON;
nameSpaces = [
"cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size"
];
data = nameSpaces.collect({arg nS; msgInterpret.value(model[nS]).postln});
data.postln;
# curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize = data;
popSize = ranges.size;
};
loadLedgerFile = {arg path;
ledgerPath = path;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg ledger; ledger = ledger["ledger"]};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
loadModelFile.value(msg[1].asString);
}, \load_model);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dFormat, condition, musPath;
msg.postln;
path = msg[1].asString;
loadModelFile.value(path);
refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
lastXChanges = if(refUID == nil, {
[initVoices.value().deepCopy];
}, {
var file;
refUID.postln;
file = File((dir +/+ ".." +/+ "resources" +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..2] ++ [entrancesProbVals[3..]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..2] ++ [passagesProbVals[3..]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..2] ++ [exitsProbVals[3..]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
seq = seedFunc.value(genMotif, motifSeed).value;
//musPath = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
writeResources.value(path);
//orders = nil;
//addr.sendMsg("/current_uid", curUID);
//addr.sendMsg("/ledger", prettifyArray.value(ledger, 1).replace("\"", ""));
//addr.sendMsg("/ledger_size", ledger.size);
//addr.sendMsg("/mus_seq", prettifyArray.value(seq, 3));
addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var newLedger, modelPath, musString, musFile, test1, test2;
msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
curUID = genUID.value;
File.mkdir((dir +/+ ".." +/+ "resources" +/+ curUID).standardizePath);
File.copy(exPath, (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
writeResources.value(modelPath);
File.delete(ledgerPath.postln ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
newLedger = File(ledgerPath.postln, "w");
ledger = ledger.postln.drop(-1).add(curUID);
newLedger.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
newLedger.close;
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
player.stop;
group.set(\gate, 0);
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var file;
(indexStart + index).postln;
file = File((dir +/+ ".." +/+ "resources" +/+ uid +/+ uid ++ "_music" ++ ".json").standardizePath, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {pSeq = pSeq.add([seq, ledger.size - 1])});
patterns = genPatterns.value(pSeq, addr);
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
});
player = player.play
});
}, \transport);
)
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms;
noHarms = 30;
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
"{\"a\": 1}".parseYAML["a"].asInteger;
"{\"a\": 1}".parseJSON["a"].isNumber;
1223423434123.asHexString.toLower
Date.getDate.rawSeconds
Date.seed.asHexString.toLower
n = NetAddr("localhost", 8080);
n.sendMsg("/GET/#", (NetAddr.localAddr.hostname ++ ":" ++ NetAddr.localAddr.port), "/passage_probs_vals");

@ -1,59 +0,0 @@
{
"music_data":
[
[
[
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 1.375 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 0, 0, 0, 0, 0, 1 ], [ "Rest" ] ], 0.875 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 0, 1, 0, 0, 0, 0 ], [ "Rest" ] ], 0.875 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 0, 0, 0, 0, 1, 0 ], [ "Rest" ] ], 0.75 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 1, 0, 0, 0, -1, 0 ], [ "Rest" ] ], 1.5 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 1, -1, 0, 0, 0, 0 ], [ "Rest" ] ], 1.125 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 0, 0, 1, 0, 0, 0 ], [ "Rest" ] ], 1.625 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 1, 0, 0, -1, 0, 0 ], [ "Rest" ] ], 1.625 ]
],
[
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 2, -1, 0, -1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ "Rest" ] ], 0 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 2, -1, 0, -1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 2, 0, 0, -1, 0, 0 ] ], 1.25 ],
[ [ [ 1, -1, 1, -1, 0, 0 ], [ 2, -1, 0, -1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 2, 0, 0, -1, 0, 0 ] ], 1.125 ],
[ [ [ 2, -2, 0, -1, 0, 0 ], [ 2, -1, 0, -1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 2, 0, 0, -1, 0, 0 ] ], 1 ],
[ [ [ 0, 1, 0, -1, 0, 0 ], [ 2, -1, 0, -1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 2, 0, 0, -1, 0, 0 ] ], 0.5 ],
[ [ [ 1, 0, -1, -1, 0, 0 ], [ 2, -1, 0, -1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 2, 0, 0, -1, 0, 0 ] ], 1.625 ],
[ [ [ 2, -1, 0, -1, 0, -1 ], [ 2, -1, 0, -1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 2, 0, 0, -1, 0, 0 ] ], 2 ],
[ [ [ 2, -1, 0, -2, 0, 0 ], [ 2, -1, 0, -1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 2, 0, 0, -1, 0, 0 ] ], 1.75 ],
[ [ [ 1, -1, 1, -1, 0, 0 ], [ 2, -1, 0, -1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 2, 0, 0, -1, 0, 0 ] ], 1.5 ],
[ [ [ 2, -2, 0, -1, 0, 0 ], [ 2, -1, 0, -1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 2, 0, 0, -1, 0, 0 ] ], 1.25 ],
[ [ [ 2, -2, 0, -1, 0, 0 ], [ 2, -1, 0, -1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ "Rest" ] ], 0.875 ],
[ [ [ 2, -2, 0, -1, 0, 0 ], [ 2, -1, 0, -1, 0, 0 ], [ "Rest" ], [ "Rest" ] ], 0 ],
[ [ [ 2, -2, 0, -1, 0, 0 ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 0 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 1.375 ]
]
]
],
"last_changes":
[
[ [ 1, 0, -1, -1, 0, 0 ], [ 2, -1, 0, -1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 2, 0, 0, -1, 0, 0 ] ],
[ [ 2, -1, 0, -1, 0, -1 ], [ 2, -1, 0, -1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 2, 0, 0, -1, 0, 0 ] ],
[ [ 2, -1, 0, -2, 0, 0 ], [ 2, -1, 0, -1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 2, 0, 0, -1, 0, 0 ] ],
[ [ 1, -1, 1, -1, 0, 0 ], [ 2, -1, 0, -1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 2, 0, 0, -1, 0, 0 ] ],
[ [ 2, -2, 0, -1, 0, 0 ], [ 2, -1, 0, -1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 2, 0, 0, -1, 0, 0 ] ]
],
"cur_uid": "7ac10d34",
"ref_uid": "46b6952a",
"order_seed": 638872,
"dur_seed": 225879,
"motifs_seed": 992393,
"entrances_probs_vals": [ 0.75, 0.5, 2, 0, 0.5, 0.26424870466321, 0.75675675675676, 0.5, 0.5, 0.58549222797927, 0.72635135135135, 1, 0.5 ],
"passages_probs_vals": [ 0.75, 0.5, 2, 0, 0.5, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"exits_probs_vals": [ 0.75, 0.5, 2, 0, 0.5, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"ranges": [ [ -384, 2400 ], [ -507, 2400 ], [ -282, 2237 ], [ -1200, 2053 ] ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"order":
[
[ [ 0 ], [ 2, 2, 2, 2, 2, 2, 2 ], [ 3, 1 ] ],
[ [ 1, 2, 3 ], [ 0, 0, 0, 0, 0, 0, 0, 0 ], [ ] ]
],
"sus_weights": [ 0.75, 0.69, 0.75 ],
"order_size": [ 2, 6 ],
"passages_size": [ 0, 10 ]
}

@ -1,887 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict, saveLedger;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~msg.postln;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr, oneShot = false;
var voices, durs, pbinds, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
pbinds = voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
[
Pbind(
\instrument, \string_model,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
),
Pbind(
\instrument, \sine,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
)
]
}).flatten;
if(oneShot.not, {
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
pbinds = pbinds ++
[
Pbind(
\type, \osc,
\addr, addr,
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
});
res = Ppar(pbinds);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
//res.postln;
if(res.every({arg char; char.isDecDigit}), {res = res.asInteger});
});
});
res
};
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
saveLedger = {arg ledger, path;
var file;
file = File(path, "w");
file.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
file.close;
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
~group = group;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\save_ledger, {arg msg, time, addr, port;
msg.postln;
ledger = msgInterpret.value(msg[1].asString.parseJSON["ledger"], false).postln;
//loadLedgerJSON.value(msg[0])
saveLedger.value(ledger, msg[2].asString);
//loadLedgerFile.value(msg[1].asString);
}, \save_ledger);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
lastXChanges = if(refUID == nil, {
[initVoices.value().deepCopy];
}, {
var file;
file = File((dir +/+ ".." +/+ "resources" +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
seq = seedFunc.value(genMotif, motifSeed).value;
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
addr.sendMsg("/generated", path, modelString, ledgerPath);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
musicChanged = (musicData != seq).postln;
curUID = genUID.value;
File.mkdir((dir +/+ ".." +/+ "resources" +/+ curUID).standardizePath);
File.copy(exPath, (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {
seq = musicData;
dict["music_data"] = seq;
dict["motif_edited"] = "true"
});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
ledger = ledger.drop(-1).add(curUID);
saveLedger.value(ledger, ledgerPath);
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
player.stop;
group.set(\gate, 0);
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
if(msg[1] == 1, {
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (dir +/+ ".." +/+ "resources" +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.postln.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
}, {
pSeq = [loadModelJSON.value(msg[2].asString.parseJSON)["music_data"].postln];
patterns = genPatterns.value(pSeq, addr, true);
});
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
addr.sendMsg("/one_shot", 0);
});
player = player.play
});
}, \transport);
)
(
//synthdefs
~stringModelBusArray = 4.collect({Bus.audio(s, 1)});
~sineBusArray = 4.collect({Bus.audio(s, 1)});
~bassBusArray = 1.collect({Bus.audio(s, 1)});
~hdustBusArray = 1.collect({Bus.audio(s, 1)});
~samplerBusArray = 2.collect({Bus.audio(s, 1)});
~sBuf = Buffer.alloc(s, 10, 2);
SynthDef(\string_model, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var trig, exc, sig1, sig2, noHarms;
noHarms = 30;
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.8).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar(Select.kr(busIndex, ~stringModelBusArray), sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
//Out.ar([0, 1], sig1 * EnvGen.kr(Env.asr(dur, 0.3, 1), gate, doneAction: 2));
}).add;
SynthDef(\sine, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var sig;
sig = SinOsc.ar(freq);
Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.asr(0.3, 0.4, 0.3), gate, timeScale: dur, doneAction: 2));
}).add;
SynthDef(\mixer, {arg freq, gate = 1, sustain, amp, dur, out;
var nameSpaces, sigs, hierarchicalDust;
hierarchicalDust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1))
);
sigs = [~stringModelBusArray, ~sineBusArray, ~bassBusArray, ~hdustBusArray, ~samplerBusArray].collect({arg busArray, i;
var nameSpace, sig;
nameSpace = ['string', 'sine', 'bass', 'hdust', 'sampler'][i];
sig = busArray.collect({arg bus, c; In.ar(bus, 1) * NamedControl.kr(\ ++ nameSpace ++ '_volume_' ++ c, 1, 0.1)});
sig = sig.collect({arg channel, c; Pan2.ar(channel, NamedControl.kr(\ ++ nameSpace ++ '_pan_' ++ c, i / (busArray.size - 1), 0.1) * 2 - 1)});
sig = sig.collect({arg channel, c; channel * NamedControl.kr(\ ++ nameSpace ++ '_mute_' ++ c, 1, 0.1)});
sig = Mix.ar(sig) * pow(NamedControl.kr(\ ++ nameSpace ++ '_volume_master', 1, 0.1), 2);
});
sigs = Mix.ar(sigs / 4);
Out.ar(0, sigs)
}).add;
SynthDef(\bass, {
var switches, drone;
switches = {|i| Dust.kr(0.1)} ! 9;
drone = {|i| var harm = pow(2, 2 - (i / 3).trunc), amp = (1 / pow(harm, 2));
SinOsc.ar(60 * harm + TRand.kr(-3, 3, switches[i]), 0, amp)} ! 9;
Out.ar(~bassBusArray[0], Mix.new(drone) * 0.2);
}).add;
SynthDef(\sampler, {
Out.ar(~samplerBusArray, PlayBuf.ar(2, ~sBuf, BufRateScale.kr(~sBuf), doneAction: 2))
}).add;
// main routine
SynthDef(\hdust, {
arg gate = 0;
var hierarchical_dust, low_sine, high_sine, brown_noise, white_noise;
// this triggers the combinations of sources
// it is similar to the Supercollider UGen called dust but with a hierarchical structure
hierarchical_dust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1)) *
TIRand.kr(0, 1, Impulse.kr(0.1))
);
// adjust the multiplier at the end of each line for adjusting levels
// note with each trigger, each source has a 1 in 3 chance of sounding
low_sine = SinOsc.ar(76.midicps / 16) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.1;
high_sine = SinOsc.ar(76.midicps * 8) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.01;
brown_noise = BrownNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.025;
white_noise = WhiteNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.02;
Out.ar(~hdustBusArray[0],
((low_sine + high_sine + brown_noise + white_noise) )
);
}).add;
)
(
var bass, hdust, sampler, mixer;
bass = Synth.tail(~group, \bass);
hdust = Synth.tail(~group, \hdust);
sampler = Synth.head(~group, \sampler);
mixer = Synth.tail(~group, \mixer);
OSCdef(\mixer, {arg msg, time, addr, port;
mixer.set((msg[1] ++ '_' ++ msg[2] ++ '_' ++ msg[3]), msg[4])
}, \mixer);
OSCdef(\sampler, {arg msg, time, addr, port;
msg.postln;
sampler.free;
~sBuf.free;
~sBuf = Buffer.read(s, msg[1].asString.postln, action: {sampler = Synth.head(~group, \sampler)});
}, \sampler);
)
/* old something
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
*/

@ -1,59 +0,0 @@
{
"music_data":
[
[
[
[ [ [ "Rest" ], [ "Rest" ], [ 1, 0, 0, 0, 0, -1 ], [ "Rest" ] ], 0.375 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 1, 0, 0, 0, 0, -1 ], [ "Rest" ] ], 2.25 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 1, 0, 0, 0, 0, -1 ], [ 1, 0, 1, 0, 0, 0 ] ], 0.375 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 1, 0, 0, 0 ], [ 1, 0, 0, 0, 0, -1 ], [ 1, 0, 1, 0, 0, 0 ] ], 0.5 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 1, 0, 0, 0 ], [ 1, 0, 0, 0, 0, -1 ], [ 2, 0, 0, 0, 0, -1 ] ], 0.375 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0, -1 ], [ 2, 0, 0, 0, 0, -1 ] ], 3.625 ]
],
[
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 1, 0, 0, 0, 0, -1 ], [ 2, 0, 0, 0, 0, -1 ] ], 0.875 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 1, 0, 0, 0, 0, -1 ], [ "Rest" ] ], 0.625 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 0, 0, 0, 1, 0, 0 ], [ "Rest" ] ], 0.75 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 1, 0, 0, 0, 0, 0 ], [ "Rest" ] ], 4.125 ]
],
[
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0, 0 ], [ "Rest" ] ], 0.375 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ], [ 0, 1, 1, 0, 0, 0 ], [ "Rest" ] ], 0.5 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ], [ 0, 1, 1, 0, 0, 0 ], [ 1, 0, 1, 0, 0, 0 ] ], 4 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ], [ 0, 1, 1, 0, 0, 0 ], [ 1, 0, 1, 0, 0, 0 ] ], 0.5 ],
[ [ [ "Rest" ], [ "Rest" ], [ 0, 1, 1, 0, 0, 0 ], [ 1, 0, 1, 0, 0, 0 ] ], 0.875 ],
[ [ [ "Rest" ], [ "Rest" ], [ 0, 1, 1, 0, 0, 0 ], [ "Rest" ] ], 0.5 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 4.375 ]
]
]
],
"last_changes":
[
[ [ 0, 0, 0, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0, -1 ], [ 2, 0, 0, 0, 0, -1 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ], [ 0, 0, 0, 1, 0, 0 ], [ 2, 0, 0, 0, 0, -1 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0, 0 ], [ 2, 0, 0, 0, 0, -1 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ], [ 0, 1, 1, 0, 0, 0 ], [ 2, 0, 0, 0, 0, -1 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ], [ 0, 1, 1, 0, 0, 0 ], [ 1, 0, 1, 0, 0, 0 ] ]
],
"cur_uid": "7bf874ce",
"ref_uid": "6d0c2f19",
"order_seed": 460016,
"dur_seed": 553757,
"motifs_seed": 771464,
"entrances_probs_vals": [ 0, 1.67, 4.8015873015873, 0.24725274725275, 1.15, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"passages_probs_vals": [ 0, 1.67, 4.8015873015873, 0.24725274725275, 1.15, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"exits_probs_vals": [ 0, 1.67, 4.8015873015873, 0.24725274725275, 1.15, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"ranges": [ [ -1100, 460.68111455108 ], [ -507, 1084.8297213622 ], [ 884.21052631579, 2237 ], [ 1040.2476780186, 2053 ] ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"order":
[
[ [ 2, 0 ], [ 3, 1, 3, 1 ], [ ] ],
[ [ 0 ], [ 2, 2 ], [ 1, 3 ] ],
[ [ 1, 0 ], [ 2, 3 ], [ ] ]
],
"sus_weights": [ 0.75, 0.69, 0.75 ],
"order_size": [ 1, 4 ],
"passages_size": [ 0, 3 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,897 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict, saveLedger;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold.deepCopy});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~msg.postln;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr, oneShot = false;
var voices, durs, pbinds, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
pbinds = voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs, rels;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
rels = freqs.drop(-1).collect({rrand(0.3, 0.5)}) ++ [rrand(1, 2)];
[
Pbind(
\instrument, \string_model,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\release, Pseq(rels, 1),
\busIndex, v
),
Pbind(
\instrument, \sine,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
)
]
}).flatten;
if(oneShot.not, {
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
pbinds = pbinds ++
[
Pbind(
\type, \osc,
\addr, addr,
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
});
res = Ppar(pbinds);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
//res.postln;
if(res.every({arg char; char.isDecDigit}), {res = res.asInteger});
});
});
res
};
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict, skipLastXChanges = false;
var tmpLastXChanges;
tmpLastXChanges = lastXChanges.deepCopy;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
if(skipLastXChanges, {lastXChanges = tmpLastXChanges});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
saveLedger = {arg ledger, path;
var file;
file = File(path, "w");
file.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
file.close;
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
~group = group;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\save_ledger, {arg msg, time, addr, port;
msg.postln;
ledger = msgInterpret.value(msg[1].asString.parseJSON["ledger"], false).postln;
//loadLedgerJSON.value(msg[0])
saveLedger.value(ledger, msg[2].asString);
//loadLedgerFile.value(msg[1].asString);
}, \save_ledger);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict, true);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
if(refUID == nil, {lastXChanges = [initVoices.value().deepCopy]});
if((refUID != nil) && (refUID != "tmp"), {
var file;
file = File((dir +/+ ".." +/+ "resources" +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
lastXChanges = msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
refUID.postln;
lastXChanges.collect({arg item; item.postln});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
seq = seedFunc.value(genMotif, motifSeed).value;
lastXChanges.collect({arg item; item.postln});
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
addr.sendMsg("/generated", path, modelString, ledgerPath);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
musicChanged = (musicData != seq).postln;
curUID = genUID.value;
File.mkdir((dir +/+ ".." +/+ "resources" +/+ curUID).standardizePath);
File.copy(exPath, (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {
seq = musicData;
dict["music_data"] = seq;
dict["motif_edited"] = "true"
});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
ledger = ledger.drop(-1).add(curUID);
saveLedger.value(ledger, ledgerPath);
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
player.stop;
group.set(\gate, 0);
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
if(msg[1] == 1, {
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (dir +/+ ".." +/+ "resources" +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.postln.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
}, {
pSeq = [loadModelJSON.value(msg[2].asString.parseJSON)["music_data"].postln];
patterns = genPatterns.value(pSeq, addr, true);
});
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
addr.sendMsg("/one_shot", 0);
});
player = player.play
});
}, \transport);
)
(
//synthdefs
~stringModelBusArray = 4.collect({Bus.audio(s, 1)});
~sineBusArray = 4.collect({Bus.audio(s, 1)});
~bassBusArray = 1.collect({Bus.audio(s, 1)});
~hdustBusArray = 1.collect({Bus.audio(s, 1)});
~samplerBusArray = 2.collect({Bus.audio(s, 1)});
~sBuf = Buffer.alloc(s, 10, 2);
SynthDef(\string_model, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var trig, exc, sig1, sig2, noHarms;
noHarms = 30;
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.8).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar(Select.kr(busIndex, ~stringModelBusArray), sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 3, 0.9), gate, doneAction: 2));
//Out.ar([0, 1], sig1 * EnvGen.kr(Env.asr(dur, 0.3, 1), gate, doneAction: 2));
}).add;
SynthDef(\sine, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var sig;
sig = SinOsc.ar(freq);
Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.asr(0.3, 0.4, 0.3), gate, timeScale: dur, doneAction: 2));
//Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.sine(dur), gate, doneAction: 2));
}).add;
SynthDef(\mixer, {arg freq, gate = 1, sustain, amp, dur, out;
var nameSpaces, sigs, hierarchicalDust;
hierarchicalDust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1))
);
sigs = [~stringModelBusArray, ~sineBusArray, ~bassBusArray, ~hdustBusArray, ~samplerBusArray].collect({arg busArray, i;
var nameSpace, sig;
nameSpace = ['string', 'sine', 'bass', 'hdust', 'sampler'][i];
sig = busArray.collect({arg bus, c; In.ar(bus, 1) * NamedControl.kr(\ ++ nameSpace ++ '_volume_' ++ c, 1, 0.1)});
sig = sig.collect({arg channel, c; Pan2.ar(channel, NamedControl.kr(\ ++ nameSpace ++ '_pan_' ++ c, i / (busArray.size - 1), 0.1) * 2 - 1)});
sig = sig.collect({arg channel, c; channel * NamedControl.kr(\ ++ nameSpace ++ '_mute_' ++ c, 1, 0.1)});
sig = Mix.ar(sig) * pow(NamedControl.kr(\ ++ nameSpace ++ '_volume_master', 1, 0.1), 2);
});
sigs = Mix.ar(sigs / 4);
Out.ar(0, sigs)
}).add;
SynthDef(\bass, {
var switches, drone;
switches = {|i| Dust.kr(0.1)} ! 9;
drone = {|i| var harm = pow(2, 2 - (i / 3).trunc), amp = (1 / pow(harm, 2));
SinOsc.ar(60 * harm + TRand.kr(-3, 3, switches[i]), 0, amp)} ! 9;
Out.ar(~bassBusArray[0], Mix.new(drone) * 0.2);
}).add;
SynthDef(\sampler, {
Out.ar(~samplerBusArray, PlayBuf.ar(2, ~sBuf, BufRateScale.kr(~sBuf), doneAction: 2))
}).add;
// main routine
SynthDef(\hdust, {
arg gate = 0;
var hierarchical_dust, low_sine, high_sine, brown_noise, white_noise;
// this triggers the combinations of sources
// it is similar to the Supercollider UGen called dust but with a hierarchical structure
hierarchical_dust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1)) *
TIRand.kr(0, 1, Impulse.kr(0.1))
);
// adjust the multiplier at the end of each line for adjusting levels
// note with each trigger, each source has a 1 in 3 chance of sounding
low_sine = SinOsc.ar(76.midicps / 16) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.1;
high_sine = SinOsc.ar(76.midicps * 8) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.01;
brown_noise = BrownNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.025;
white_noise = WhiteNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.02;
Out.ar(~hdustBusArray[0],
((low_sine + high_sine + brown_noise + white_noise) )
);
}).add;
)
(
var bass, hdust, sampler, mixer;
bass = Synth.tail(~group, \bass);
hdust = Synth.tail(~group, \hdust);
sampler = Synth.head(~group, \sampler);
mixer = Synth.tail(~group, \mixer);
OSCdef(\mixer, {arg msg, time, addr, port;
mixer.set((msg[1] ++ '_' ++ msg[2] ++ '_' ++ msg[3]), msg[4])
}, \mixer);
OSCdef(\sampler, {arg msg, time, addr, port;
msg.postln;
sampler.free;
~sBuf.free;
~sBuf = Buffer.read(s, msg[1].asString.postln, action: {sampler = Synth.head(~group, \sampler)});
}, \sampler);
)
/* old something
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
*/

@ -1,45 +0,0 @@
{
"music_data":
[
[
[
[ [ [ "Rest" ], [ 1, 0, 0, -1, 1, -1 ], [ "Rest" ], [ "Rest" ] ], 1.625 ],
[ [ [ "Rest" ], [ 1, 0, 0, -1, 1, -1 ], [ "Rest" ], [ 3, 0, 0, -1, 0, -1 ] ], 1.25 ],
[ [ [ 0, 1, 0, -1, 1, -1 ], [ 1, 0, 0, -1, 1, -1 ], [ "Rest" ], [ 3, 0, 0, -1, 0, -1 ] ], 1.25 ],
[ [ [ 0, 1, 0, -1, 1, -1 ], [ 1, 0, 0, -1, 1, -1 ], [ 3, 0, -1, -1, 1, -1 ], [ 3, 0, 0, -1, 0, -1 ] ], 1.5 ],
[ [ [ 0, 0, 0, -1, 2, -1 ], [ 1, 0, 0, -1, 1, -1 ], [ 3, 0, -1, -1, 1, -1 ], [ 3, 0, 0, -1, 0, -1 ] ], 2.125 ],
[ [ [ 0, 0, 0, -1, 2, -1 ], [ 1, 0, 0, -1, 1, -1 ], [ "Rest" ], [ 3, 0, 0, -1, 0, -1 ] ], 1.5 ],
[ [ [ 0, 0, 0, -1, 2, -1 ], [ "Rest" ], [ "Rest" ], [ 3, 0, 0, -1, 0, -1 ] ], 1.125 ],
[ [ [ 0, 0, 0, -1, 2, -1 ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 1.375 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 1.75 ]
]
]
],
"last_changes":
[
[ [ -1, 0, 0, 1, 1, -1 ], [ 1, 0, 0, -1, 1, -1 ], [ 2, 0, 0, 0, 1, -1 ], [ 1, 1, 0, 0, 1, -1 ] ],
[ [ -1, 0, 0, 1, 1, -1 ], [ 1, 0, 0, -1, 1, -1 ], [ 2, 0, 0, 0, 1, -1 ], [ 3, 0, 0, -1, 0, -1 ] ],
[ [ 0, 1, 0, -1, 1, -1 ], [ 1, 0, 0, -1, 1, -1 ], [ 2, 0, 0, 0, 1, -1 ], [ 3, 0, 0, -1, 0, -1 ] ],
[ [ 0, 1, 0, -1, 1, -1 ], [ 1, 0, 0, -1, 1, -1 ], [ 3, 0, -1, -1, 1, -1 ], [ 3, 0, 0, -1, 0, -1 ] ],
[ [ 0, 0, 0, -1, 2, -1 ], [ 1, 0, 0, -1, 1, -1 ], [ 3, 0, -1, -1, 1, -1 ], [ 3, 0, 0, -1, 0, -1 ] ]
],
"cur_uid": "7c7a96a2",
"ref_uid": "6f1305ed",
"order_seed": 768571,
"dur_seed": 647965,
"motifs_seed": 710750,
"entrances_probs_vals": [ 0, 0.48, 1.7460317460317, 0.58, 1.6758241758242, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"passages_probs_vals": [ 0, 0.48, 1.7460317460317, 0.58, 1.6758241758242, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"exits_probs_vals": [ 0, 0.48, 1.7460317460317, 0.58, 1.6758241758242, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"ranges": [ [ -1100, 237.77089783282 ], [ -1066.253869969, 260.06191950464 ], [ 1207.4303405573, 2065.6346749226 ], [ 1040.2476780186, 2053 ] ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"order":
[
[ [ 1 ], [ 3, 0, 2, 0 ], [ ] ]
],
"sus_weights": [ 0.73, 0.49, 0 ],
"order_size": [ 1, 7 ],
"passages_size": [ 0, 2 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,907 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict, saveLedger;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold.deepCopy});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~msg.postln;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr, oneShot = false;
var voices, durs, pbinds, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
pbinds = voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs, attacks, rels, amps;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
//attacks = 2.collect({rrand(1, 3)}) ++ freqs.drop(2).collect({rrand(0.3, 0.5)});
attacks = fDurs.collect({arg dur; dur * rrand(0.2, 0.4)});
//rels = freqs.drop(2).collect({rrand(0.3, 0.5)}) ++ 2.collect({rrand(1, 3)});
rels = (clumps.size - 1).collect({arg c;
if(clumps[c + 1][0] == ["Rest"], {rrand(1, 3)}, {rrand(0.3, 0.5)});
});
amps = freqs.collect({rrand(0.6, 0.99)});
[
Pbind(
\instrument, \string_model,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\attack, Pseq(attacks, 1),
\sustain, Pseq(fDurs, 1),
\release, Pseq(rels, 1),
//\amp, Pseq(amps, 1),
\amp, Pbrown(0.5, 1, 0.5),
\busIndex, v
),
Pbind(
\instrument, \sine,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
)
]
}).flatten;
if(oneShot.not, {
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
pbinds = pbinds ++
[
Pbind(
\type, \osc,
\addr, addr,
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
});
res = Ppar(pbinds);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
//res.postln;
if(res.every({arg char; char.isDecDigit}), {res = res.asInteger});
});
});
res
};
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict, skipLastXChanges = false;
var tmpLastXChanges;
tmpLastXChanges = lastXChanges.deepCopy;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
if(skipLastXChanges, {lastXChanges = tmpLastXChanges});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
saveLedger = {arg ledger, path;
var file;
file = File(path, "w");
file.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
file.close;
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
~group = group;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\save_ledger, {arg msg, time, addr, port;
msg.postln;
ledger = msgInterpret.value(msg[1].asString.parseJSON["ledger"], false).postln;
//loadLedgerJSON.value(msg[0])
saveLedger.value(ledger, msg[2].asString);
//loadLedgerFile.value(msg[1].asString);
}, \save_ledger);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict, true);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
if(refUID == nil, {lastXChanges = [initVoices.value().deepCopy]});
if((refUID != nil) && (refUID != "tmp"), {
var file;
file = File((dir +/+ ".." +/+ "resources" +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
lastXChanges = msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
refUID.postln;
lastXChanges.collect({arg item; item.postln});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
seq = seedFunc.value(genMotif, motifSeed).value;
lastXChanges.collect({arg item; item.postln});
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
addr.sendMsg("/generated", path, modelString, ledgerPath);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
musicChanged = (musicData != seq).postln;
curUID = genUID.value;
File.mkdir((dir +/+ ".." +/+ "resources" +/+ curUID).standardizePath);
File.copy(exPath, (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {
seq = musicData;
dict["music_data"] = seq;
dict["motif_edited"] = "true"
});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
ledger = ledger.drop(-1).add(curUID);
saveLedger.value(ledger, ledgerPath);
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
group.set(\release, 2);
group.set(\gate, 0);
player.stop;
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
if(msg[1] == 1, {
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (dir +/+ ".." +/+ "resources" +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.postln.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
}, {
pSeq = [loadModelJSON.value(msg[2].asString.parseJSON)["music_data"].postln];
patterns = genPatterns.value(pSeq, addr, true);
});
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
addr.sendMsg("/one_shot", 0);
});
player = player.play
});
}, \transport);
)
(
//synthdefs
~stringModelBusArray = 4.collect({Bus.audio(s, 1)});
~sineBusArray = 4.collect({Bus.audio(s, 1)});
~bassBusArray = 1.collect({Bus.audio(s, 1)});
~hdustBusArray = 1.collect({Bus.audio(s, 1)});
~samplerBusArray = 2.collect({Bus.audio(s, 1)});
~sBuf = Buffer.alloc(s, 10, 2);
SynthDef(\string_model, {arg freq, gate = 1, sustain, amp, dur, attack, release = 1, busIndex = 0;
var trig, exc, sig1, sig2, noHarms;
noHarms = 30;
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.8).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar(Select.kr(busIndex, ~stringModelBusArray), sig1 * amp * EnvGen.kr(Env.adsr(attack, 0.3, 0.9, release, 0.9, -3), gate, doneAction: 2));
//Out.ar([0, 1], sig1 * EnvGen.kr(Env.asr(dur, 0.3, 1), gate, doneAction: 2));
}).add;
SynthDef(\sine, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var sig;
sig = SinOsc.ar(freq);
Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.asr(0.3, 0.4, 0.3), gate, timeScale: dur, doneAction: 2));
//Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.sine(dur), gate, doneAction: 2));
}).add;
SynthDef(\mixer, {arg freq, gate = 1, sustain, amp, dur, out;
var nameSpaces, sigs, hierarchicalDust;
hierarchicalDust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1))
);
sigs = [~stringModelBusArray, ~sineBusArray, ~bassBusArray, ~hdustBusArray, ~samplerBusArray].collect({arg busArray, i;
var nameSpace, sig;
nameSpace = ['string', 'sine', 'bass', 'hdust', 'sampler'][i];
sig = busArray.collect({arg bus, c; In.ar(bus, 1) * NamedControl.kr(\ ++ nameSpace ++ '_volume_' ++ c, 1, 0.1)});
sig = sig.collect({arg channel, c; Pan2.ar(channel, NamedControl.kr(\ ++ nameSpace ++ '_pan_' ++ c, i / (busArray.size - 1), 0.1) * 2 - 1)});
sig = sig.collect({arg channel, c; channel * NamedControl.kr(\ ++ nameSpace ++ '_mute_' ++ c, 1, 0.1)});
sig = Mix.ar(sig) * pow(NamedControl.kr(\ ++ nameSpace ++ '_volume_master', 1, 0.1), 2);
});
sigs = Mix.ar(sigs / 4);
Out.ar(0, sigs)
}).add;
SynthDef(\bass, {
var switches, drone;
switches = {|i| Dust.kr(0.1)} ! 9;
drone = {|i| var harm = pow(2, 2 - (i / 3).trunc), amp = (1 / pow(harm, 2));
SinOsc.ar(60 * harm + TRand.kr(-3, 3, switches[i]), 0, amp)} ! 9;
Out.ar(~bassBusArray[0], Mix.new(drone) * 0.2);
}).add;
SynthDef(\sampler, {
Out.ar(~samplerBusArray, PlayBuf.ar(2, ~sBuf, BufRateScale.kr(~sBuf), doneAction: 2))
}).add;
// main routine
SynthDef(\hdust, {
arg gate = 0;
var hierarchical_dust, low_sine, high_sine, brown_noise, white_noise;
// this triggers the combinations of sources
// it is similar to the Supercollider UGen called dust but with a hierarchical structure
hierarchical_dust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1)) *
TIRand.kr(0, 1, Impulse.kr(0.1))
);
// adjust the multiplier at the end of each line for adjusting levels
// note with each trigger, each source has a 1 in 3 chance of sounding
low_sine = SinOsc.ar(76.midicps / 16) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.1;
high_sine = SinOsc.ar(76.midicps * 8) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.01;
brown_noise = BrownNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.025;
white_noise = WhiteNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.02;
Out.ar(~hdustBusArray[0],
((low_sine + high_sine + brown_noise + white_noise) )
);
}).add;
)
(
var bass, hdust, sampler, mixer;
bass = Synth.tail(~group, \bass);
hdust = Synth.tail(~group, \hdust);
sampler = Synth.head(~group, \sampler);
mixer = Synth.tail(~group, \mixer);
OSCdef(\mixer, {arg msg, time, addr, port;
mixer.set((msg[1] ++ '_' ++ msg[2] ++ '_' ++ msg[3]), msg[4])
}, \mixer);
OSCdef(\sampler, {arg msg, time, addr, port;
msg.postln;
sampler.free;
~sBuf.free;
~sBuf = Buffer.read(s, msg[1].asString.postln, action: {sampler = Synth.head(~group, \sampler)});
}, \sampler);
)
/* old something
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
*/

@ -1,84 +0,0 @@
{
"music_data":
[
[
[
[ [ [ "Rest" ], [ "Rest" ], [ 6, -1, 0, -1, -1, -4 ], [ "Rest" ] ], 2.375 ],
[ [ [ "Rest" ], [ 4, -1, 1, -1, -1, -4 ], [ 6, -1, 0, -1, -1, -4 ], [ "Rest" ] ], 1.25 ],
[ [ [ "Rest" ], [ 5, -2, 0, -1, -1, -4 ], [ 6, -1, 0, -1, -1, -4 ], [ "Rest" ] ], 1.5 ],
[ [ [ "Rest" ], [ 5, -1, 0, -2, -1, -4 ], [ 6, -1, 0, -1, -1, -4 ], [ "Rest" ] ], 1.25 ],
[ [ [ "Rest" ], [ 5, -1, 0, -1, -1, -5 ], [ 6, -1, 0, -1, -1, -4 ], [ "Rest" ] ], 3.125 ]
],
[
[ [ [ "Rest" ], [ 5, -1, 0, -1, -1, -5 ], [ 6, -1, 0, -1, -1, -4 ], [ 5, -1, 0, 0, 0, -5 ] ], 2.625 ],
[ [ [ 3, -1, 0, -1, 0, -5 ], [ 5, -1, 0, -1, -1, -5 ], [ 6, -1, 0, -1, -1, -4 ], [ 5, -1, 0, 0, 0, -5 ] ], 1.375 ],
[ [ [ 3, -1, 0, -1, 0, -5 ], [ 5, -1, 0, -1, -1, -5 ], [ 5, -1, 1, 0, 0, -5 ], [ 5, -1, 0, 0, 0, -5 ] ], 2.625 ]
],
[
[ [ [ 3, -1, 0, -1, 0, -5 ], [ 3, 0, 1, 0, 0, -5 ], [ 5, -1, 1, 0, 0, -5 ], [ 5, -1, 0, 0, 0, -5 ] ], 1.125 ],
[ [ [ 2, -1, 1, 0, 0, -5 ], [ 3, 0, 1, 0, 0, -5 ], [ 5, -1, 1, 0, 0, -5 ], [ 5, -1, 0, 0, 0, -5 ] ], 0.875 ],
[ [ [ 2, -1, 1, 0, 0, -5 ], [ 3, 0, 1, 0, 0, -5 ], [ 5, -1, 1, 0, 0, -5 ], [ 4, -1, 1, 1, 0, -5 ] ], 1.5 ],
[ [ [ 2, -1, 1, 0, 0, -5 ], [ 3, -1, 1, 1, 0, -5 ], [ 5, -1, 1, 0, 0, -5 ], [ 4, -1, 1, 1, 0, -5 ] ], 1 ],
[ [ [ 3, -1, 1, -1, 0, -5 ], [ 3, -1, 1, 1, 0, -5 ], [ 5, -1, 1, 0, 0, -5 ], [ 4, -1, 1, 1, 0, -5 ] ], 2.125 ]
],
[
[ [ [ 3, -1, 1, -1, 0, -5 ], [ 3, -1, 1, 1, 0, -5 ], [ 4, -1, 2, 1, 0, -5 ], [ 4, -1, 1, 1, 0, -5 ] ], 0.75 ],
[ [ [ 2, -1, 1, 1, 0, -6 ], [ 3, -1, 1, 1, 0, -5 ], [ 4, -1, 2, 1, 0, -5 ], [ 4, -1, 1, 1, 0, -5 ] ], 0.75 ],
[ [ [ 2, -2, 1, 1, 0, -5 ], [ 3, -1, 1, 1, 0, -5 ], [ 4, -1, 2, 1, 0, -5 ], [ 4, -1, 1, 1, 0, -5 ] ], 2 ]
],
[
[ [ [ 2, -2, 1, 1, 0, -5 ], [ 3, -1, 1, 1, 0, -5 ], [ "Rest" ], [ 4, -1, 1, 1, 0, -5 ] ], 0.75 ],
[ [ [ 2, -1, 1, 1, -1, -5 ], [ 3, -1, 1, 1, 0, -5 ], [ "Rest" ], [ 4, -1, 1, 1, 0, -5 ] ], 1.125 ],
[ [ [ 2, -1, 1, 1, -1, -5 ], [ 2, -1, 1, 2, 0, -5 ], [ "Rest" ], [ 4, -1, 1, 1, 0, -5 ] ], 1 ],
[ [ [ 2, -1, 1, 1, -1, -5 ], [ 3, -1, 0, 1, 0, -5 ], [ "Rest" ], [ 4, -1, 1, 1, 0, -5 ] ], 1.5 ],
[ [ [ 2, -1, 1, 1, -1, -5 ], [ 2, -1, 1, 1, 0, -4 ], [ "Rest" ], [ 4, -1, 1, 1, 0, -5 ] ], 1.5 ],
[ [ [ 1, -1, 1, 1, 1, -5 ], [ 2, -1, 1, 1, 0, -4 ], [ "Rest" ], [ 4, -1, 1, 1, 0, -5 ] ], 0.75 ],
[ [ [ 1, -1, 1, 1, 1, -5 ], [ 2, 0, 1, 1, 0, -5 ], [ "Rest" ], [ 4, -1, 1, 1, 0, -5 ] ], 1.5 ]
],
[
[ [ [ 1, -1, 1, 1, 1, -5 ], [ 2, 0, 1, 1, 0, -5 ], [ "Rest" ], [ 3, 0, 1, 1, 1, -5 ] ], 0.75 ],
[ [ [ 1, -1, 1, 1, 1, -5 ], [ 2, 0, 1, 1, 0, -5 ], [ 3, -1, 1, 2, 1, -5 ], [ 3, 0, 1, 1, 1, -5 ] ], 1.625 ],
[ [ [ 1, -1, 1, 1, 1, -5 ], [ 2, -1, 1, 1, 1, -5 ], [ 3, -1, 1, 2, 1, -5 ], [ 3, 0, 1, 1, 1, -5 ] ], 1.125 ],
[ [ [ 1, -1, 1, 1, 1, -5 ], [ 1, -1, 1, 2, 1, -5 ], [ 3, -1, 1, 2, 1, -5 ], [ 3, 0, 1, 1, 1, -5 ] ], 0.875 ],
[ [ [ 1, -1, 1, 1, 1, -5 ], [ 1, 0, 1, 1, 1, -5 ], [ 3, -1, 1, 2, 1, -5 ], [ 3, 0, 1, 1, 1, -5 ] ], 1.5 ],
[ [ [ 1, -1, 1, 1, 1, -5 ], [ 1, 0, 1, 1, 1, -5 ], [ 4, -1, 0, 1, 1, -5 ], [ 3, 0, 1, 1, 1, -5 ] ], 2.625 ],
[ [ [ "Rest" ], [ 1, 0, 1, 1, 1, -5 ], [ 4, -1, 0, 1, 1, -5 ], [ 3, 0, 1, 1, 1, -5 ] ], 1 ],
[ [ [ "Rest" ], [ 1, 0, 1, 1, 1, -5 ], [ 4, -1, 0, 1, 1, -5 ], [ "Rest" ] ], 1.5 ],
[ [ [ "Rest" ], [ "Rest" ], [ 4, -1, 0, 1, 1, -5 ], [ "Rest" ] ], 1.25 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 2.875 ]
]
]
],
"last_changes":
[
[ [ 1, -1, 1, 1, 1, -5 ], [ 2, 0, 1, 1, 0, -5 ], [ 3, -1, 1, 2, 1, -5 ], [ 3, 0, 1, 1, 1, -5 ] ],
[ [ 1, -1, 1, 1, 1, -5 ], [ 2, -1, 1, 1, 1, -5 ], [ 3, -1, 1, 2, 1, -5 ], [ 3, 0, 1, 1, 1, -5 ] ],
[ [ 1, -1, 1, 1, 1, -5 ], [ 1, -1, 1, 2, 1, -5 ], [ 3, -1, 1, 2, 1, -5 ], [ 3, 0, 1, 1, 1, -5 ] ],
[ [ 1, -1, 1, 1, 1, -5 ], [ 1, 0, 1, 1, 1, -5 ], [ 3, -1, 1, 2, 1, -5 ], [ 3, 0, 1, 1, 1, -5 ] ],
[ [ 1, -1, 1, 1, 1, -5 ], [ 1, 0, 1, 1, 1, -5 ], [ 4, -1, 0, 1, 1, -5 ], [ 3, 0, 1, 1, 1, -5 ] ]
],
"cur_uid": "7c8bc6df",
"ref_uid": "tmp",
"order_seed": 487536,
"dur_seed": 140662,
"motifs_seed": 617352,
"entrances_probs_vals": [ 0, 0.48, 1.7460317460317, 0.58, 1.68, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"passages_probs_vals": [ 0, 0.48, 1.7460317460317, 0.58, 1.68, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"exits_probs_vals": [ 0, 0.48, 1.7460317460317, 0.58, 1.68, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"ranges": [ [ -3600, -627.86377708978 ], [ -3600, 260 ], [ -980.80495356037, 2066 ], [ -627.86377708978, 2053 ] ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"order":
[
[ [ 2 ], [ 1, 1, 1, 1 ], [ 3, 0 ] ],
[ [ 1, 3 ], [ 0, 2 ], [ ] ],
[ [ 2 ], [ 1, 0, 3, 1, 0 ], [ ] ],
[ [ 1, 3 ], [ 2, 0, 0 ], [ ] ],
[ [ 3 ], [ 0, 1, 1, 1, 0, 1 ], [ 2 ] ],
[ [ 0 ], [ 3, 2, 1, 1, 1, 2 ], [ ] ]
],
"sus_weights": [ 0.73, 0.49, 0 ],
"order_size": [ 1, 7 ],
"passages_size": [ 0, 5 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,718 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON;
// model vars
//(model and global vars mostly set by OSC funcs
var curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, orders, susWeights, passagesWeights, passagesSize, orderSize;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc;
// other global vars
var lastXChanges, popSize, exPath, dir, primes, dims, tuples,
seq, group, player, ledgerPath, ledger, currentlyPlayingUID;
// install JSON quark
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, min, max, envData;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
durFunc = {arg allowChord;
var res;
res = if(allowChord.not, {
pTable.tableRand * (max - min) + min
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (max - min) + min});
}).round(0.125);
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, durSeed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
allowChord = if((sus ++ silent).includes(ins), {
(sus ++ silent).includes(ins) && (ins != sus.last);
}, {
if(i < (flatOrder.size - 1), {(isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not}, {false});
});
dur = passagesDurFunc.value(allowChord);
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(isLastOrder, {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
dur = passagesDurFunc.value(allowChord);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
genPatterns = {arg inSeq, addr;
var voices, durs, patterns, res, indices, sectionDurs, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
indices = inSeq.collect({arg mSeq, m; mSeq[1]});
ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
res = Ppar(
voices.flop.collect({arg voice;
var clumps, hdScores, freqs, fDurs;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\instrument, \test,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1)
)
}) ++
[
Pbind(
\type, \osc,
\addr, addr,
\indexPath, "/cur_play_index",
\indexMsg, Pseq(indices.postln, 1),
\seqPath, "/mus_seq",
\seqMsg, Pseq(seq, 1),
\dur, Pseq(sectionDurs, 1)
);
]
);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < (maxDepth - 0), {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
//res.postln;
if(res.every({arg char; char.isDecDigit}), {res = res.asInteger});
});
});
res
};
writeResources = {arg path;
var file, nameSpaces, modelItems, resString;
file = File(path,"w");
nameSpaces = [
"music_data", "last_changes",
"cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size"
];
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize
];
resString = [nameSpaces, modelItems].flop.collect({arg item;
var nameSpace, modelItem, depth = 0, insert = " ";
# nameSpace, modelItem = item;
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(modelItem.postln, depth).postln
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg model;
var nameSpaces, data;
//model = File(path, "r").readAllString.parseJSON;
nameSpaces = [
"cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size"
];
data = nameSpaces.collect({arg nS; msgInterpret.value(model[nS]).postln});
data.postln;
# curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize = data;
popSize = ranges.size;
};
loadLedgerFile = {arg path;
ledgerPath = path;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg ledger; ledger = ledger["ledger"]};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
loadModelFile.value(msg[1].asString);
}, \load_model);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dFormat, condition, musPath;
msg.postln;
path = msg[1].asString;
loadModelFile.value(path);
refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
lastXChanges = if(refUID == nil, {
[initVoices.value().deepCopy];
}, {
var file;
refUID.postln;
file = File((dir +/+ ".." +/+ "resources" +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..2] ++ [entrancesProbVals[3..]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..2] ++ [passagesProbVals[3..]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..2] ++ [exitsProbVals[3..]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
seq = seedFunc.value(genMotif, motifSeed).value;
//musPath = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
writeResources.value(path);
//orders = nil;
//addr.sendMsg("/current_uid", curUID);
//addr.sendMsg("/ledger", prettifyArray.value(ledger, 1).replace("\"", ""));
//addr.sendMsg("/ledger_size", ledger.size);
//addr.sendMsg("/mus_seq", prettifyArray.value(seq, 3));
addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var newLedger, modelPath, musString, musFile, test1, test2;
msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
curUID = genUID.value;
File.mkdir((dir +/+ ".." +/+ "resources" +/+ curUID).standardizePath);
File.copy(exPath, (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
writeResources.value(modelPath);
File.delete(ledgerPath.postln ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
newLedger = File(ledgerPath.postln, "w");
ledger = ledger.postln.drop(-1).add(curUID);
newLedger.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
newLedger.close;
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
player.stop;
group.set(\gate, 0);
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var file;
(indexStart + index).postln;
file = File((dir +/+ ".." +/+ "resources" +/+ uid +/+ uid ++ "_music" ++ ".json").standardizePath, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {pSeq = pSeq.add([seq, ledger.size - 1])});
patterns = genPatterns.value(pSeq, addr);
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
});
player = player.play
});
}, \transport);
)
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms;
noHarms = 30;
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
"{\"a\": 1}".parseYAML["a"].asInteger;
"{\"a\": 1}".parseJSON["a"].isNumber;
1223423434123.asHexString.toLower
Date.getDate.rawSeconds
Date.seed.asHexString.toLower
n = NetAddr("localhost", 8080);
n.sendMsg("/GET/#", (NetAddr.localAddr.hostname ++ ":" ++ NetAddr.localAddr.port), "/passage_probs_vals");

@ -1,79 +0,0 @@
{
"music_data":
[
[
[
[ [ [ "Rest" ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ], [ "Rest" ] ], 1.75 ],
[ [ [ "Rest" ], [ "Rest" ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 1, 0, 0, 0 ] ], 0 ],
[ [ [ "Rest" ], [ -1, 0, 0, 1, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 1, 0, 0, 0 ] ], 0.625 ],
[ [ [ "Rest" ], [ -1, 0, 0, 1, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ] ], 0.875 ],
[ [ [ "Rest" ], [ -1, 0, 0, 1, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0, -1 ] ], 1.375 ],
[ [ [ "Rest" ], [ -1, 0, 0, 1, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0 ] ], 0 ],
[ [ [ "Rest" ], [ -1, 1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0 ] ], 0.5 ],
[ [ [ "Rest" ], [ -1, 0, 0, 0, 0, 1 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0 ] ], 0 ],
[ [ [ "Rest" ], [ -1, 0, 0, 0, 0, 1 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 0 ] ], 1.25 ],
[ [ [ "Rest" ], [ -1, 0, 0, 0, 0, 1 ], [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, -1, 0, 0, 0 ] ], 0 ],
[ [ [ "Rest" ], [ 0, 0, -1, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, -1, 0, 0, 0 ] ], 1.875 ]
],
[
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, -1, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, -1, 0, 0, 0 ] ], 1.625 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -1, 0, 0, 1, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, -1, 0, 0, 0 ] ], 0 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -1, 0, 0, 1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 1, 0, -1, 0, 0, 0 ] ], 0 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -1, 0, 0, 1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 0, 0, 0, 0, 1, 0 ] ], 1.75 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -1, 0, 0, 1, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 1, 0, 0, 0, -1, 0 ] ], 1.75 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -1, 0, 0, 0, 0, 1 ], [ 1, 0, 0, -1, 0, 0 ], [ 1, 0, 0, 0, -1, 0 ] ], 0 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -1, 0, 0, 0, 0, 1 ], [ 1, 0, 0, -1, 0, 0 ], [ 0, 0, 0, 0, 0, 1 ] ], 1.75 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, -1, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 0, 0, 0, 0, 0, 1 ] ], 1.875 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, -1, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 1, 0, -1, 0, 0, 0 ] ], 1.375 ]
],
[
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, -1, 0, 0, 0 ], [ "Rest" ], [ 1, 0, -1, 0, 0, 0 ] ], 1.375 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ -1, 1, 0, 0, 0, 0 ], [ "Rest" ], [ 1, 0, -1, 0, 0, 0 ] ], 1 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, 0, -1, -1, 0, 0 ], [ "Rest" ], [ 1, 0, -1, 0, 0, 0 ] ], 1.125 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, -1, 0, 1, 0 ], [ "Rest" ], [ 1, 0, -1, 0, 0, 0 ] ], 1.625 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, -1, 0, 0, 0 ], [ "Rest" ], [ 1, 0, -1, 0, 0, 0 ] ], 1.75 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 1, 0, 0, 0 ], [ "Rest" ], [ 1, 0, -1, 0, 0, 0 ] ], 1.125 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ "Rest" ], [ 1, 0, -1, 0, 0, 0 ] ], 1.25 ]
],
[
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 1, 0, -1, 0, 0, 0 ] ], 0 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 0, 0, 0, 1, 0, 0 ] ], 1.25 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 1, 0, 0, 0, 0, 0 ] ], 1.25 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 1, -1, 0, 0, 0, 1 ] ], 1.5 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 2, 0, 0, -1, 0, 0 ] ], 1.25 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ "Rest" ] ], 0 ],
[ [ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ] ], 0 ],
[ [ [ "Rest" ], [ 1, -1, 0, 0, 0, 0 ], [ "Rest" ], [ "Rest" ] ], 0 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 1 ]
]
]
],
"last_changes":
[
[ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 1, 0, -1, 0, 0, 0 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 0, 0, 0, 1, 0, 0 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 1, 0, 0, 0, 0, 0 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 1, -1, 0, 0, 0, 1 ] ],
[ [ 0, 0, 0, 0, 0, 0 ], [ 1, -1, 0, 0, 0, 0 ], [ 1, 0, 0, -1, 0, 0 ], [ 2, 0, 0, -1, 0, 0 ] ]
],
"cur_uid": "7e170ef8",
"ref_uid": "nil",
"order_seed": 142640,
"dur_seed": 629022,
"motifs_seed": 973728,
"entrances_probs_vals": [ 0.75, 0.5, 2, 0, 0.5, 0.26424870466321, 0.75675675675676, 0.5, 0.5, 0.58549222797927, 0.72635135135135, 1, 0.5 ],
"passages_probs_vals": [ 0.75, 0.5, 2, 0, 0.5, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"exits_probs_vals": [ 0.75, 0.5, 2, 0, 0.5, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"ranges": [ [ -384, 2400 ], [ -507, 2400 ], [ -282, 2237 ], [ -1200, 2053 ] ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"order":
[
[ [ 2 ], [ 3, 1, 3, 3, 3, 1, 1, 3, 3, 1 ], [ 0 ] ],
[ [ 0 ], [ 1, 2, 3, 3, 1, 3, 1, 3 ], [ ] ],
[ [ 0, 3 ], [ 1, 1, 1, 1, 1, 1 ], [ 2 ] ],
[ [ 1, 2, 0 ], [ 3, 3, 3, 3 ], [ ] ]
],
"sus_weights": [ 0.75, 0.69, 0.75 ],
"order_size": [ 2, 6 ],
"passages_size": [ 0, 10 ]
}

@ -1,894 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict, saveLedger;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
pDistance.gaussCurve(1, mean, sd)
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = 1/pow(hdSum.value(voices.deepCopy.put(ins, candidate)), 2);
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold.deepCopy});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~msg.postln;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr, oneShot = false;
var voices, durs, pbinds, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
pbinds = voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
[
Pbind(
\instrument, \string_model,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
),
Pbind(
\instrument, \sine,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
)
]
}).flatten;
if(oneShot.not, {
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
pbinds = pbinds ++
[
Pbind(
\type, \osc,
\addr, addr,
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
});
res = Ppar(pbinds);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
//res.postln;
if(res.every({arg char; char.isDecDigit}), {res = res.asInteger});
});
});
res
};
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict, skipLastXChanges = false;
var tmpLastXChanges;
tmpLastXChanges = lastXChanges.deepCopy;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
if(skipLastXChanges, {lastXChanges = tmpLastXChanges});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, passagesWeights, orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
saveLedger = {arg ledger, path;
var file;
file = File(path, "w");
file.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
file.close;
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
~group = group;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "passages_weights", "order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\save_ledger, {arg msg, time, addr, port;
msg.postln;
ledger = msgInterpret.value(msg[1].asString.parseJSON["ledger"], false).postln;
//loadLedgerJSON.value(msg[0])
saveLedger.value(ledger, msg[2].asString);
//loadLedgerFile.value(msg[1].asString);
}, \save_ledger);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict, true);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
if(refUID == nil, {lastXChanges = [initVoices.value().deepCopy]});
if((refUID != nil) && (refUID != "tmp"), {
var file;
file = File((dir +/+ ".." +/+ "resources" +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
lastXChanges = msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
refUID.postln;
lastXChanges.collect({arg item; item.postln});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
seq = seedFunc.value(genMotif, motifSeed).value;
lastXChanges.collect({arg item; item.postln});
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
addr.sendMsg("/generated", path, modelString, ledgerPath);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
musicChanged = (musicData != seq).postln;
curUID = genUID.value;
File.mkdir((dir +/+ ".." +/+ "resources" +/+ curUID).standardizePath);
File.copy(exPath, (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (dir +/+ ".." +/+ "resources" +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {
seq = musicData;
dict["music_data"] = seq;
dict["motif_edited"] = "true"
});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
ledger = ledger.drop(-1).add(curUID);
saveLedger.value(ledger, ledgerPath);
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
player.stop;
group.set(\gate, 0);
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
if(msg[1] == 1, {
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (dir +/+ ".." +/+ "resources" +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.postln.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
}, {
pSeq = [loadModelJSON.value(msg[2].asString.parseJSON)["music_data"].postln];
patterns = genPatterns.value(pSeq, addr, true);
});
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
addr.sendMsg("/one_shot", 0);
});
player = player.play
});
}, \transport);
)
(
//synthdefs
~stringModelBusArray = 4.collect({Bus.audio(s, 1)});
~sineBusArray = 4.collect({Bus.audio(s, 1)});
~bassBusArray = 1.collect({Bus.audio(s, 1)});
~hdustBusArray = 1.collect({Bus.audio(s, 1)});
~samplerBusArray = 2.collect({Bus.audio(s, 1)});
~sBuf = Buffer.alloc(s, 10, 2);
SynthDef(\string_model, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var trig, exc, sig1, sig2, noHarms;
noHarms = 30;
exc = Saw.ar(freq, TRand.ar(0.5, 1, Impulse.ar(freq))) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(1, 2)}) ], exc) * 0.8).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar(Select.kr(busIndex, ~stringModelBusArray), sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
//Out.ar([0, 1], sig1 * EnvGen.kr(Env.asr(dur, 0.3, 1), gate, doneAction: 2));
}).add;
SynthDef(\sine, {arg freq, gate = 1, sustain, amp, dur, busIndex = 0;
var sig;
sig = SinOsc.ar(freq);
Out.ar(Select.kr(busIndex, ~sineBusArray), sig * EnvGen.kr(Env.asr(0.3, 0.4, 0.3), gate, timeScale: dur, doneAction: 2));
}).add;
SynthDef(\mixer, {arg freq, gate = 1, sustain, amp, dur, out;
var nameSpaces, sigs, hierarchicalDust;
hierarchicalDust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1))
);
sigs = [~stringModelBusArray, ~sineBusArray, ~bassBusArray, ~hdustBusArray, ~samplerBusArray].collect({arg busArray, i;
var nameSpace, sig;
nameSpace = ['string', 'sine', 'bass', 'hdust', 'sampler'][i];
sig = busArray.collect({arg bus, c; In.ar(bus, 1) * NamedControl.kr(\ ++ nameSpace ++ '_volume_' ++ c, 1, 0.1)});
sig = sig.collect({arg channel, c; Pan2.ar(channel, NamedControl.kr(\ ++ nameSpace ++ '_pan_' ++ c, i / (busArray.size - 1), 0.1) * 2 - 1)});
sig = sig.collect({arg channel, c; channel * NamedControl.kr(\ ++ nameSpace ++ '_mute_' ++ c, 1, 0.1)});
sig = Mix.ar(sig) * pow(NamedControl.kr(\ ++ nameSpace ++ '_volume_master', 1, 0.1), 2);
});
sigs = Mix.ar(sigs / 4);
Out.ar(0, sigs)
}).add;
SynthDef(\bass, {
var switches, drone;
switches = {|i| Dust.kr(0.1)} ! 9;
drone = {|i| var harm = pow(2, 2 - (i / 3).trunc), amp = (1 / pow(harm, 2));
SinOsc.ar(60 * harm + TRand.kr(-3, 3, switches[i]), 0, amp)} ! 9;
Out.ar(~bassBusArray[0], Mix.new(drone) * 0.2);
}).add;
SynthDef(\sampler, {
Out.ar(~samplerBusArray, PlayBuf.ar(2, ~sBuf, BufRateScale.kr(~sBuf), doneAction: 2))
}).add;
// main routine
SynthDef(\hdust, {
arg gate = 0;
var hierarchical_dust, low_sine, high_sine, brown_noise, white_noise;
// this triggers the combinations of sources
// it is similar to the Supercollider UGen called dust but with a hierarchical structure
hierarchical_dust = (
TIRand.kr(0, 1, Impulse.kr(100)) *
TIRand.kr(0, 1, Impulse.kr(10)) *
TIRand.kr(0, 1, Impulse.kr(1)) *
TIRand.kr(0, 1, Impulse.kr(0.1))
);
// adjust the multiplier at the end of each line for adjusting levels
// note with each trigger, each source has a 1 in 3 chance of sounding
low_sine = SinOsc.ar(76.midicps / 16) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.1;
high_sine = SinOsc.ar(76.midicps * 8) * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.01;
brown_noise = BrownNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.025;
white_noise = WhiteNoise.ar() * (TIRand.kr(0, 2, hierarchical_dust) < 1) * 0.02;
Out.ar(~hdustBusArray[0],
((low_sine + high_sine + brown_noise + white_noise) )
);
}).add;
)
(
var bass, hdust, sampler, mixer;
bass = Synth.tail(~group, \bass);
hdust = Synth.tail(~group, \hdust);
sampler = Synth.head(~group, \sampler);
mixer = Synth.tail(~group, \mixer);
OSCdef(\mixer, {arg msg, time, addr, port;
mixer.set((msg[1] ++ '_' ++ msg[2] ++ '_' ++ msg[3]), msg[4])
}, \mixer);
OSCdef(\sampler, {arg msg, time, addr, port;
msg.postln;
sampler.free;
~sBuf.free;
~sBuf = Buffer.read(s, msg[1].asString.postln, action: {sampler = Synth.head(~group, \sampler)});
}, \sampler);
)
/* old something
(
SynthDef(\test, {arg freq, gate = 1, sustain, amp, dur;
var trig, exc, sig1, sig2, noHarms, freqFinal, start, end;
noHarms = 30;
freq = WhiteNoise.ar * 3 + freq;
freqFinal = Duty.ar((1/freq), 0, freq);
trig = Changed.ar(freqFinal);
start = Demand.ar(trig, 0, Dwhite(-1, -0.75));
end = Demand.ar(trig, 0, Dwhite(0.75, 1));
exc = Phasor.ar(trig, (end - start) * freqFinal / SampleRate.ir, start, end, 0) * 0.001 + Dust.ar(10000, 0.01);
sig1 = (Klank.ar(`[ Array.series(noHarms, freq, freq),
Array.geom(noHarms, 1, 0.2) + Array.fill(noHarms, {rrand(0.01, 0.03)}),
Array.fill(noHarms, {rrand(2, 3)}) ], exc) * 0.5).softclip;
sig1 = HPF.ar(sig1, 300);
Out.ar([0, 1], sig1 * EnvGen.kr(Env.adsr(0.3, 0.3, 0.9, 0.5, 0.9), gate, doneAction: 2));
}).add;
)
*/

@ -1,44 +0,0 @@
{
"music_data":
[
[
[
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ 1, 0, 0, 0, 1, 0 ] ], 4.75 ],
[ [ [ "Rest" ], [ -1, 0, 0, 1, 1, 0 ], [ "Rest" ], [ 1, 0, 0, 0, 1, 0 ] ], 1.125 ],
[ [ [ 0, 0, 0, -1, 1, 0 ], [ -1, 0, 0, 1, 1, 0 ], [ "Rest" ], [ 1, 0, 0, 0, 1, 0 ] ], 1 ],
[ [ [ 0, 0, 0, -1, 1, 0 ], [ -1, 0, 0, 1, 1, 0 ], [ 2, 0, 0, 0, 1, -1 ], [ 1, 0, 0, 0, 1, 0 ] ], 5.125 ],
[ [ [ 0, 0, 0, -1, 1, 0 ], [ "Rest" ], [ 2, 0, 0, 0, 1, -1 ], [ 1, 0, 0, 0, 1, 0 ] ], 1.125 ],
[ [ [ 0, 0, 0, -1, 1, 0 ], [ "Rest" ], [ 2, 0, 0, 0, 1, -1 ], [ "Rest" ] ], 0.875 ],
[ [ [ "Rest" ], [ "Rest" ], [ 2, 0, 0, 0, 1, -1 ], [ "Rest" ] ], 0.875 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 4.375 ]
]
]
],
"last_changes":
[
[ [ 0, -1, 0, 0, 1, 0 ], [ 0, 0, -1, 0, 1, 0 ], [ 1, 0, 1, 0, 1, 0 ], [ 1, 0, 0, 0, 1, 0 ] ],
[ [ 0, -1, 0, 0, 1, 0 ], [ 0, 0, -1, 0, 1, 0 ], [ 2, 0, 0, -1, 1, 0 ], [ 1, 0, 0, 0, 1, 0 ] ],
[ [ 0, -1, 0, 0, 1, 0 ], [ -1, 0, 0, 1, 1, 0 ], [ 2, 0, 0, -1, 1, 0 ], [ 1, 0, 0, 0, 1, 0 ] ],
[ [ 0, 0, 0, -1, 1, 0 ], [ -1, 0, 0, 1, 1, 0 ], [ 2, 0, 0, -1, 1, 0 ], [ 1, 0, 0, 0, 1, 0 ] ],
[ [ 0, 0, 0, -1, 1, 0 ], [ -1, 0, 0, 1, 1, 0 ], [ 2, 0, 0, 0, 1, -1 ], [ 1, 0, 0, 0, 1, 0 ] ]
],
"cur_uid": "7fd4d544",
"ref_uid": "tmp",
"order_seed": 159820,
"dur_seed": 341194,
"motifs_seed": 928268,
"entrances_probs_vals": [ 0, 1.67, 4.8015873015873, 0.24725274725275, 1.15, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"passages_probs_vals": [ 0, 1.67, 4.8015873015873, 0.24725274725275, 1.15, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"exits_probs_vals": [ 0, 1.83, 4.8809523809524, 0.24725274725275, 1.15, 0.20725388601036, 0.68581081081081, 0.24093264248705, 0.34121621621622, 0.5, 0.5, 0.67616580310881, 0.81081081081081, 1, 0.5 ],
"ranges": [ [ -1100, 460.68111455108 ], [ -507, 1084.8297213622 ], [ 884.21052631579, 2237 ], [ 1040.2476780186, 2053 ] ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"order":
[
[ [ 3 ], [ 1, 0, 2 ], [ ] ]
],
"sus_weights": [ 0.73, 0.23, 0 ],
"order_size": [ 1, 2 ],
"passages_size": [ 0, 1 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,25 +0,0 @@
{
"ledger":
[
"4a8a6e53",
"66f6a618",
"490b1e6e",
"46985d14",
"761e4585",
"6fb60ab6",
"79e0a4a7",
"43b009ff",
"7d3c9a80",
"4b7745df",
"6ed95c4c",
"6d635e88",
"4e7d35e5",
"7edbdceb",
"784130cc",
"443ec222",
"52c9a980",
"4200a90d",
"61ce9067",
"774ed940"
]
}

@ -1,945 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc, genStepFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict, saveLedger;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, stepProbsVals, passagesWeights, hdExp, hdInvert,
orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc, stepFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, resourceDir, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark (not used)
/*
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
*/
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
//pDistance.gaussCurve(1, mean, sd)
stepFunc.value(pDistance);
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genStepFunc = {arg minStep, maxStep, envData, seed;
var envDataNorm, env, pTable, stepFunc;
[minStep, maxStep, envData].postln;
envDataNorm = ([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).flop;
envDataNorm = [envDataNorm[0].normalize(minStep, maxStep), envDataNorm[1]].flop;
env = Env.pairs(envDataNorm);
stepFunc = {arg pDist;
env.at(pDist).clip(0.001, 1);
};
seedFunc.value(stepFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = pow(hdSum.value(voices.deepCopy.put(ins, candidate)), hdExp);
if(hdInvert == 0, {hdScore = 1/hdScore});
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold.deepCopy});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq, fDur, durAdd;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
//round last duration to measure
fDur = fSeq.flatten.flatten.slice(nil, 1).sum;
durAdd = fDur.round(4) - fDur;
if(durAdd < 0, {durAdd = 4 - durAdd});
fSeq[0][orders.size - 1][fSeq[0][orders.size - 1].size - 1][1] = fSeq[0][orders.size - 1][fSeq[0][orders.size - 1].size - 1][1] + durAdd;
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.notNil) {
~msg;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr, oneShot = false;
var voices, durs, pbinds, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
pbinds = voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs, attacks, rels, amps;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
//attacks = 2.collect({rrand(1, 3)}) ++ freqs.drop(2).collect({rrand(0.3, 0.5)});
attacks = fDurs.collect({arg dur; dur * rrand(0.2, 0.4)});
//rels = freqs.drop(2).collect({rrand(0.3, 0.5)}) ++ 2.collect({rrand(1, 3)});
rels = (clumps.size - 1).collect({arg c;
if(clumps[c + 1][0] == ["Rest"], {rrand(1.0, 3.0)}, {rrand(0.3, 0.5)});
});
rels = rels.add(rrand(1.0, 3.0));
amps = freqs.collect({rrand(0.6, 0.99)});
[
Pbind(
\instrument, \string_model,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\attack, Pseq(attacks, 1),
\sustain, Pseq(fDurs, 1),
\release, Pseq(rels, 1),
//\amp, Pseq(amps, 1),
\amp, Pbrown(0.5, 1, 0.5),
\busIndex, v
),
Pbind(
\instrument, \sine,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
)
]
}).flatten;
if(oneShot.not, {
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
pbinds = pbinds ++
[
Pbind(
\type, \osc,
\addr, addr,
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
});
res = Ppar(pbinds);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
var tmpRes;
if(res.every({arg char; char.isDecDigit}), {tmpRes = res.asInteger});
if(res.contains("."), {tmpRes = res.asFloat});
if(tmpRes != nil, {res = tmpRes});
});
});
res
};
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, stepProbsVals, passagesWeights, hdExp, hdInvert,
orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict, skipLastXChanges = false;
var tmpLastXChanges;
tmpLastXChanges = lastXChanges.deepCopy;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, stepProbsVals, passagesWeights, hdExp, hdInvert,
orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
if(skipLastXChanges, {lastXChanges = tmpLastXChanges});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, stepProbsVals, passagesWeights, hdExp, hdInvert,
orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
resourceDir = path.splitext(".").drop(-1).join;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
saveLedger = {arg ledger, path;
var file, curResourceDir;
file = File(path, "w");
curResourceDir = resourceDir;
resourceDir = path.splitext(".").drop(-1).join;
if(curResourceDir != resourceDir, {
File.mkdir(resourceDir);
ledger.do({arg id;
File.copy(curResourceDir +/+ id, resourceDir +/+ id);
});
});
file.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
file.close;
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
~group = group;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
resourceDir = (dir +/+ ".." +/+ "resources" +/+ "piece_ledger");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "step_probs_vals", "passages_weights", "hd_exp", "hd_invert",
"order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\save_ledger, {arg msg, time, addr, port;
msg.postln;
ledger = msgInterpret.value(msg[1].asString.parseJSON["ledger"], false).postln;
//loadLedgerJSON.value(msg[0])
saveLedger.value(ledger, msg[2].asString);
//loadLedgerFile.value(msg[1].asString);
}, \save_ledger);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict, true);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
if(refUID == nil, {lastXChanges = [initVoices.value().deepCopy]});
if((refUID != nil) && (refUID != "tmp"), {
var file;
file = File((resourceDir +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
lastXChanges = msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
refUID.postln;
lastXChanges.collect({arg item; item.postln});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
stepFunc = genStepFunc.valueArray(stepProbsVals[..1] ++ [stepProbsVals[2..]] ++ [motifSeed]);
seq = seedFunc.value(genMotif, motifSeed).value;
lastXChanges.collect({arg item; item.postln});
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
//~seq = seq;
addr.sendMsg("/generated", path, modelString, ledgerPath);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2, lastCurUID, commitType, commitPos, equalityLedger;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
musicChanged = (musicData != seq).postln;
commitType = msg[2].asString;
commitPos = msg[3].postln.asInteger;
lastCurUID = curUID.deepCopy;
curUID = genUID.value;
File.mkdir((resourceDir +/+ curUID).standardizePath);
File.copy(exPath, (resourceDir +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (resourceDir +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {
seq = musicData;
dict["music_data"] = seq;
dict["motif_edited"] = "true"
});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
/*
if(commitType == "add", {
if(lastCurUID == "tmp", {
ledger = ledger.drop(-1).add(curUID);
}, {
ledger = ledger.add(curUID);
})
});
*/
ledger.postln;
if(commitType == "add", {ledger = ledger.add(curUID)});
if(commitType == "insert", {ledger = ledger.insert(commitPos + 1, curUID)});
if(commitType == "replace", {ledger = ledger.put(commitPos, curUID)});
equalityLedger = ledger.collect({arg item; item.asSymbol});
if(equalityLedger.includes(\tmp).postln, {ledger.removeAt(equalityLedger.indexOf(\tmp).postln)});
ledger.postln;
saveLedger.value(ledger, ledgerPath);
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
group.set(\release, 2);
group.set(\gate, 0);
player.stop;
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
if(msg[1] == 1, {
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (resourceDir +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.postln.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (resourceDir +/+ "tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
}, {
pSeq = [loadModelJSON.value(msg[2].asString.parseJSON)["music_data"].postln];
patterns = genPatterns.value(pSeq, addr, true);
});
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
addr.sendMsg("/one_shot", 0);
});
player = player.play
});
}, \transport);
OSCdef(\transcribe_motif, {arg msg, time, addr, port;
var tSeq, refChord, refUID;
msg.postln;
tSeq = [loadModelJSON.value(msg[1].asString.parseJSON)["music_data"]];
refUID = msg[2].asString.postln;
if((refUID != "nil") && (refUID != "tmp"), {
var file;
file = File((resourceDir +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
refChord = msgInterpret.value(file.readAllString.parseJSON["last_changes"]).last;
file.close;
}, {
refChord = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]];
});
~transcribe.value(tSeq, refChord, (dir +/+ ".." +/+ "lilypond" +/+ "includes").standardizePath, addr, "/transcribe_motif");
}, \transcribe_motif);
OSCdef(\transcribe_all, {arg msg, time, addr, port;
var cSize, patterns, cuedSeek, indexStart, indexEnd, tmpLedger;
if(true, {
cuedSeek = (seq != nil);
indexStart = msg[1].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//tmp for testing transcription
indexEnd = (indexStart+5);
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
var lilyPartLedgerFiles;
lilyPartLedgerFiles = 4.collect({arg p;
File((dir +/+ ".." +/+ "lilypond" +/+ "includes" +/+ "part_" ++ ["IV", "III", "II", "I"][p] ++ ".ly").standardizePath, "w");
});
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file, fileString, tSeq, refUID, refChord;
path = (resourceDir +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
fileString = file.readAllString;
tSeq = msgInterpret.value(fileString.parseJSON["music_data"]);
refUID = msgInterpret.value(fileString.parseJSON["ref_uid"]);
file.close;
//uid.postln;
//(refUID == "nil").postln;
refChord = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]];
if(refUID != "nil", {
path = (resourceDir +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
refChord = msgInterpret.value(file.readAllString.parseJSON["last_changes"]).last;
file.close;
});
if(index != indexEnd, {
~transcribe.value(tSeq, refChord, (resourceDir +/+ uid +/+ "lilypond").standardizePath);
}, {
~transcribe.value(tSeq, refChord, (resourceDir +/+ uid +/+ "lilypond").standardizePath, addr, "/transcribe_all");
});
lilyPartLedgerFiles.do({arg f, p;
f.write("\\include \"" ++ resourceDir +/+ uid +/+ "lilypond" +/+ "part_" ++ ["IV", "III", "II", "I"][p] ++ ".ly\"\n");
});
});
lilyPartLedgerFiles.do({arg f;
f.close
});
});
/*
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
*/
}, {
});
}, \transcribe_all);
)

@ -1,58 +0,0 @@
{
"music_data":
[
[
[
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ 1, 0, 1, -2, 1, 1 ] ], 2 ],
[ [ [ "Rest" ], [ "Rest" ], [ 1, 0, 0, -2, 1, 1 ], [ 1, 0, 1, -2, 1, 1 ] ], 1.5 ]
],
[
[ [ [ "Rest" ], [ 1, -1, 1, -2, 2, 0 ], [ 1, 0, 0, -2, 1, 1 ], [ 1, 0, 1, -2, 1, 1 ] ], 2 ],
[ [ [ "Rest" ], [ 1, -1, 1, -2, 2, 0 ], [ 2, -2, 1, -2, 2, 0 ], [ 1, 0, 1, -2, 1, 1 ] ], 0 ],
[ [ [ 0, -1, 1, -2, 2, 0 ], [ 1, -1, 1, -2, 2, 0 ], [ 2, -2, 1, -2, 2, 0 ], [ 1, 0, 1, -2, 1, 1 ] ], 0.625 ],
[ [ [ 0, -1, 1, -2, 2, 0 ], [ 1, -1, 1, -2, 2, 0 ], [ 1, 0, 1, -2, 2, 0 ], [ 1, 0, 1, -2, 1, 1 ] ], 0 ],
[ [ [ -1, -1, 1, -1, 2, 0 ], [ 1, -1, 1, -2, 2, 0 ], [ 1, 0, 1, -2, 2, 0 ], [ 1, 0, 1, -2, 1, 1 ] ], 1.25 ],
[ [ [ -1, -1, 1, -2, 2, 1 ], [ 1, -1, 1, -2, 2, 0 ], [ 1, 0, 1, -2, 2, 0 ], [ 1, 0, 1, -2, 1, 1 ] ], 0 ],
[ [ [ -1, -1, 1, -2, 2, 1 ], [ 1, -1, 1, -2, 2, 0 ], [ 1, -1, 1, -2, 2, 1 ], [ 1, 0, 1, -2, 1, 1 ] ], 0 ],
[ [ [ -1, -1, 1, -2, 2, 1 ], [ 1, -1, 1, -2, 2, 0 ], [ 1, -1, 1, -2, 2, 1 ], [ 2, -1, 0, -2, 2, 0 ] ], 1.25 ],
[ [ [ 0, -1, 1, -2, 1, 0 ], [ 1, -1, 1, -2, 2, 0 ], [ 1, -1, 1, -2, 2, 1 ], [ 2, -1, 0, -2, 2, 0 ] ], 1.25 ],
[ [ [ 0, -1, 1, -2, 1, 0 ], [ 1, -1, 1, -2, 2, 0 ], [ 1, -1, 1, -1, 2, 0 ], [ 2, -1, 0, -2, 2, 0 ] ], 1.125 ],
[ [ [ 0, -1, 1, -2, 1, 0 ], [ 1, -1, 1, -2, 2, 0 ], [ 1, -1, 1, -1, 2, 0 ], [ 1, -1, 1, -2, 3, 0 ] ], 2.25 ],
[ [ [ 0, -1, 1, -2, 1, 0 ], [ 1, -1, 1, -2, 2, 0 ], [ "Rest" ], [ 1, -1, 1, -2, 3, 0 ] ], 1.5 ],
[ [ [ 0, -1, 1, -2, 1, 0 ], [ 1, -1, 1, -2, 2, 0 ], [ "Rest" ], [ 1, -1, 1, -2, 3, 0 ] ], 0.875 ],
[ [ [ 0, -1, 1, -2, 1, 0 ], [ 1, -1, 1, -2, 2, 0 ], [ "Rest" ], [ "Rest" ] ], 1.125 ]
]
]
],
"last_changes":
[
[ [ -1, -1, 1, -2, 2, 1 ], [ 1, -1, 1, -2, 2, 0 ], [ 1, -1, 1, -2, 2, 1 ], [ 1, 0, 1, -2, 1, 1 ] ],
[ [ -1, -1, 1, -2, 2, 1 ], [ 1, -1, 1, -2, 2, 0 ], [ 1, -1, 1, -2, 2, 1 ], [ 2, -1, 0, -2, 2, 0 ] ],
[ [ 0, -1, 1, -2, 1, 0 ], [ 1, -1, 1, -2, 2, 0 ], [ 1, -1, 1, -2, 2, 1 ], [ 2, -1, 0, -2, 2, 0 ] ],
[ [ 0, -1, 1, -2, 1, 0 ], [ 1, -1, 1, -2, 2, 0 ], [ 1, -1, 1, -1, 2, 0 ], [ 2, -1, 0, -2, 2, 0 ] ],
[ [ 0, -1, 1, -2, 1, 0 ], [ 1, -1, 1, -2, 2, 0 ], [ 1, -1, 1, -1, 2, 0 ], [ 1, -1, 1, -2, 3, 0 ] ]
],
"cur_uid": "4200a90d",
"ref_uid": "6d635e88",
"order_seed": 516056,
"dur_seed": 358555,
"motifs_seed": 168145,
"entrances_probs_vals": [ 0.18, 0.28, 1.4285714285714, 0.82, 2.0054945054945, 0, 0.5, 0.5, 0.5, 1, 0.5 ],
"passages_probs_vals": [ 0.29, 0, 1.1111111111111, 0.65934065934066, 1.37, 0, 0.5, 0.5, 0.5, 1, 0.5 ],
"exits_probs_vals": [ 0.18, 0.28, 1.4285714285714, 0.82, 2.0054945054945, 0, 0.5, 0.5, 0.5, 1, 0.5 ],
"ranges": [ [ -2411.1455108359, -850.77399380805 ], [ -1872, 450 ], [ -479, 1304.0247678019 ], [ -368, 1173.9938080495 ] ],
"step_probs_vals": [ 0, 1200, 0, 0, 0.082304526748971, 0.99431818181818, 0.33950617283951, 0, 0.72839506172839, 0, 1, 0 ],
"passages_weights": [ 0.35, 0.42, 0.75, 0.9, 0.93 ],
"hd_exp": 2,
"hd_invert": 0,
"order":
[
[ [ 3 ], [ 2 ], [ 0, 1 ] ],
[ [ 1 ], [ 2, 0, 2, 0, 0, 2, 3, 0, 2, 3 ], [ ] ]
],
"sus_weights": [ 0.41, 0, 0 ],
"order_size": [ 2, 6 ],
"passages_size": [ 0, 5 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,18 +0,0 @@
{
{ ais'1^\markup { \pad-markup #0.2 "+41"} ~ }
\bar "|"
{ ais'1 ~ }
\bar "|"
{ ais'1 ~ }
\bar "|"
{ ais'2 ~ ais'8.[ a'16^\markup { \pad-markup #0.2 "-37"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 5↓" }}] ~ a'4 ~ }
\bar "|"
{ a'1 ~ }
\bar "|"
{ a'2 fis'2^\markup { \pad-markup #0.2 "+1"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 11↑" }} ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'2. ~ fis'16[ r8.]}
\bar "||"
}

@ -1,18 +0,0 @@
{
{ r1 }
\bar "|"
{ g'1^\markup { \pad-markup #0.2 "-46"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 5↓" }} ~ }
\bar "|"
{ g'2. f'4^\markup { \pad-markup #0.2 "+47"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 3↓" }} ~ }
\bar "|"
{ f'16[ gis'8.^\markup { \pad-markup #0.2 "-49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 3↑" }}] ~ gis'4 ~ gis'8.[ a'16^\markup { \pad-markup #0.2 "-10"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 1↑" }}] ~ a'4 ~ }
\bar "|"
{ a'2. ~ a'8.[ ais'16^\markup { \pad-markup #0.2 "+18"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 7↑" }}] ~ }
\bar "|"
{ ais'1 ~ }
\bar "|"
{ ais'2 ~ ais'8[ r8] r4 }
\bar "|"
{ r1 }
\bar "||"
}

@ -1,18 +0,0 @@
{
{ r1 }
\bar "|"
{ r2. c'4^\markup { \pad-markup #0.2 "+49"} ~ }
\bar "|"
{ c'1 ~ }
\bar "|"
{ c'1 ~ }
\bar "|"
{ c'1 ~ }
\bar "|"
{ c'1 ~ }
\bar "|"
{ c'1 ~ }
\bar "|"
{ c'1}
\bar "||"
}

@ -1,18 +0,0 @@
{
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r2. c4^\markup { \pad-markup #0.2 "+49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 1↑" }} ~ }
\bar "|"
{ c16[ ais,8.^\markup { \pad-markup #0.2 "+18"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 7↑" }}] ~ ais,4 ~ ais,8.[ a,16^\markup { \pad-markup #0.2 "-10"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 13↑" }}] ~ a,4 ~ }
\bar "|"
{ a,4 ~ a,16[ g,8.^\markup { \pad-markup #0.2 "-2"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 11↓" }}] ~ g,2 ~ }
\bar "|"
{ g,1 ~ }
\bar "|"
{ g,1 ~ }
\bar "|"
{ g,1}
\bar "||"
}

@ -1,945 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc, genStepFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict, saveLedger;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, stepProbsVals, passagesWeights, hdExp, hdInvert,
orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc, stepFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, resourceDir, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark (not used)
/*
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
*/
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
//pDistance.gaussCurve(1, mean, sd)
stepFunc.value(pDistance);
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genStepFunc = {arg minStep, maxStep, envData, seed;
var envDataNorm, env, pTable, stepFunc;
[minStep, maxStep, envData].postln;
envDataNorm = ([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).flop;
envDataNorm = [envDataNorm[0].normalize(minStep, maxStep), envDataNorm[1]].flop;
env = Env.pairs(envDataNorm);
stepFunc = {arg pDist;
env.at(pDist).clip(0.001, 1);
};
seedFunc.value(stepFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = pow(hdSum.value(voices.deepCopy.put(ins, candidate)), hdExp);
if(hdInvert == 0, {hdScore = 1/hdScore});
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold.deepCopy});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq, fDur, durAdd;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
//round last duration to measure
fDur = fSeq.flatten.flatten.slice(nil, 1).sum;
durAdd = fDur.round(4) - fDur;
if(durAdd < 0, {durAdd = 4 - durAdd});
fSeq[0][orders.size - 1][fSeq[0][orders.size - 1].size - 1][1] = fSeq[0][orders.size - 1][fSeq[0][orders.size - 1].size - 1][1] + durAdd;
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.notNil) {
~msg;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr, oneShot = false;
var voices, durs, pbinds, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
pbinds = voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs, attacks, rels, amps;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
//attacks = 2.collect({rrand(1, 3)}) ++ freqs.drop(2).collect({rrand(0.3, 0.5)});
attacks = fDurs.collect({arg dur; dur * rrand(0.2, 0.4)});
//rels = freqs.drop(2).collect({rrand(0.3, 0.5)}) ++ 2.collect({rrand(1, 3)});
rels = (clumps.size - 1).collect({arg c;
if(clumps[c + 1][0] == ["Rest"], {rrand(1.0, 3.0)}, {rrand(0.3, 0.5)});
});
rels = rels.add(rrand(1.0, 3.0));
amps = freqs.collect({rrand(0.6, 0.99)});
[
Pbind(
\instrument, \string_model,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\attack, Pseq(attacks, 1),
\sustain, Pseq(fDurs, 1),
\release, Pseq(rels, 1),
//\amp, Pseq(amps, 1),
\amp, Pbrown(0.5, 1, 0.5),
\busIndex, v
),
Pbind(
\instrument, \sine,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
)
]
}).flatten;
if(oneShot.not, {
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
pbinds = pbinds ++
[
Pbind(
\type, \osc,
\addr, addr,
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
});
res = Ppar(pbinds);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
var tmpRes;
if(res.every({arg char; char.isDecDigit}), {tmpRes = res.asInteger});
if(res.contains("."), {tmpRes = res.asFloat});
if(tmpRes != nil, {res = tmpRes});
});
});
res
};
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, stepProbsVals, passagesWeights, hdExp, hdInvert,
orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict, skipLastXChanges = false;
var tmpLastXChanges;
tmpLastXChanges = lastXChanges.deepCopy;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, stepProbsVals, passagesWeights, hdExp, hdInvert,
orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
if(skipLastXChanges, {lastXChanges = tmpLastXChanges});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, stepProbsVals, passagesWeights, hdExp, hdInvert,
orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
resourceDir = path.splitext(".").drop(-1).join;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
saveLedger = {arg ledger, path;
var file, curResourceDir;
file = File(path, "w");
curResourceDir = resourceDir;
resourceDir = path.splitext(".").drop(-1).join;
if(curResourceDir != resourceDir, {
File.mkdir(resourceDir);
ledger.do({arg id;
File.copy(curResourceDir +/+ id, resourceDir +/+ id);
});
});
file.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
file.close;
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
~group = group;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
resourceDir = (dir +/+ ".." +/+ "resources" +/+ "piece_ledger");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "step_probs_vals", "passages_weights", "hd_exp", "hd_invert",
"order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\save_ledger, {arg msg, time, addr, port;
msg.postln;
ledger = msgInterpret.value(msg[1].asString.parseJSON["ledger"], false).postln;
//loadLedgerJSON.value(msg[0])
saveLedger.value(ledger, msg[2].asString);
//loadLedgerFile.value(msg[1].asString);
}, \save_ledger);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict, true);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
if(refUID == nil, {lastXChanges = [initVoices.value().deepCopy]});
if((refUID != nil) && (refUID != "tmp"), {
var file;
file = File((resourceDir +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
lastXChanges = msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
refUID.postln;
lastXChanges.collect({arg item; item.postln});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
stepFunc = genStepFunc.valueArray(stepProbsVals[..1] ++ [stepProbsVals[2..]] ++ [motifSeed]);
seq = seedFunc.value(genMotif, motifSeed).value;
lastXChanges.collect({arg item; item.postln});
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
//~seq = seq;
addr.sendMsg("/generated", path, modelString, ledgerPath);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2, lastCurUID, commitType, commitPos, equalityLedger;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
musicChanged = (musicData != seq).postln;
commitType = msg[2].asString;
commitPos = msg[3].postln.asInteger;
lastCurUID = curUID.deepCopy;
curUID = genUID.value;
File.mkdir((resourceDir +/+ curUID).standardizePath);
File.copy(exPath, (resourceDir +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (resourceDir +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {
seq = musicData;
dict["music_data"] = seq;
dict["motif_edited"] = "true"
});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
/*
if(commitType == "add", {
if(lastCurUID == "tmp", {
ledger = ledger.drop(-1).add(curUID);
}, {
ledger = ledger.add(curUID);
})
});
*/
ledger.postln;
if(commitType == "add", {ledger = ledger.add(curUID)});
if(commitType == "insert", {ledger = ledger.insert(commitPos + 1, curUID)});
if(commitType == "replace", {ledger = ledger.put(commitPos, curUID)});
equalityLedger = ledger.collect({arg item; item.asSymbol});
if(equalityLedger.includes(\tmp).postln, {ledger.removeAt(equalityLedger.indexOf(\tmp).postln)});
ledger.postln;
saveLedger.value(ledger, ledgerPath);
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
group.set(\release, 2);
group.set(\gate, 0);
player.stop;
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
if(msg[1] == 1, {
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (resourceDir +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.postln.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (resourceDir +/+ "tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
}, {
pSeq = [loadModelJSON.value(msg[2].asString.parseJSON)["music_data"].postln];
patterns = genPatterns.value(pSeq, addr, true);
});
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
addr.sendMsg("/one_shot", 0);
});
player = player.play
});
}, \transport);
OSCdef(\transcribe_motif, {arg msg, time, addr, port;
var tSeq, refChord, refUID;
msg.postln;
tSeq = [loadModelJSON.value(msg[1].asString.parseJSON)["music_data"]];
refUID = msg[2].asString.postln;
if((refUID != "nil") && (refUID != "tmp"), {
var file;
file = File((resourceDir +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
refChord = msgInterpret.value(file.readAllString.parseJSON["last_changes"]).last;
file.close;
}, {
refChord = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]];
});
~transcribe.value(tSeq, refChord, (dir +/+ ".." +/+ "lilypond" +/+ "includes").standardizePath, addr, "/transcribe_motif");
}, \transcribe_motif);
OSCdef(\transcribe_all, {arg msg, time, addr, port;
var cSize, patterns, cuedSeek, indexStart, indexEnd, tmpLedger;
if(true, {
cuedSeek = (seq != nil);
indexStart = msg[1].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//tmp for testing transcription
indexEnd = (indexStart+5);
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
var lilyPartLedgerFiles;
lilyPartLedgerFiles = 4.collect({arg p;
File((dir +/+ ".." +/+ "lilypond" +/+ "includes" +/+ "part_" ++ ["IV", "III", "II", "I"][p] ++ ".ly").standardizePath, "w");
});
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file, fileString, tSeq, refUID, refChord;
path = (resourceDir +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
fileString = file.readAllString;
tSeq = msgInterpret.value(fileString.parseJSON["music_data"]);
refUID = msgInterpret.value(fileString.parseJSON["ref_uid"]);
file.close;
//uid.postln;
//(refUID == "nil").postln;
refChord = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]];
if(refUID != "nil", {
path = (resourceDir +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
refChord = msgInterpret.value(file.readAllString.parseJSON["last_changes"]).last;
file.close;
});
if(index != indexEnd, {
~transcribe.value(tSeq, refChord, (resourceDir +/+ uid +/+ "lilypond").standardizePath);
}, {
~transcribe.value(tSeq, refChord, (resourceDir +/+ uid +/+ "lilypond").standardizePath, addr, "/transcribe_all");
});
lilyPartLedgerFiles.do({arg f, p;
f.write("\\include \"" ++ resourceDir +/+ uid +/+ "lilypond" +/+ "part_" ++ ["IV", "III", "II", "I"][p] ++ ".ly\"\n");
});
});
lilyPartLedgerFiles.do({arg f;
f.close
});
});
/*
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
*/
}, {
});
}, \transcribe_all);
)

@ -1,48 +0,0 @@
{
"music_data":
[
[
[
[ [ [ "Rest" ], [ 2, -2, 0, -1, 1, 0 ], [ "Rest" ], [ "Rest" ] ], 5.75 ],
[ [ [ 1, -1, 0, -1, 1, 0 ], [ 2, -2, 0, -1, 1, 0 ], [ "Rest" ], [ "Rest" ] ], 2.75 ],
[ [ [ 1, -1, 0, -1, 1, 0 ], [ 2, -2, 0, -1, 1, 0 ], [ "Rest" ], [ 3, -3, 0, -1, 1, 0 ] ], 1.75 ],
[ [ [ 1, -1, 0, -1, 1, 0 ], [ 2, -2, 0, -1, 1, 0 ], [ 3, -2, 0, -2, 1, 0 ], [ 3, -3, 0, -1, 1, 0 ] ], 1.5 ],
[ [ [ 1, -1, 0, -1, 1, 0 ], [ 2, -2, 0, -1, 1, 0 ], [ 3, -2, 0, -2, 1, 0 ], [ 2, -2, 0, -1, 2, 0 ] ], 4.125 ],
[ [ [ 1, -1, 0, -1, 1, 0 ], [ 2, -2, 0, -1, 1, 0 ], [ "Rest" ], [ 2, -2, 0, -1, 2, 0 ] ], 0.875 ],
[ [ [ 1, -1, 0, -1, 1, 0 ], [ "Rest" ], [ "Rest" ], [ 2, -2, 0, -1, 2, 0 ] ], 1 ],
[ [ [ 1, -1, 0, -1, 1, 0 ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 0.875 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 7.375 ]
]
]
],
"last_changes":
[
[ [ 1, -1, 0, -1, 1, 0 ], [ 2, -2, 0, -1, 1, 0 ], [ 2, -1, -1, -1, 1, 0 ], [ 1, -1, 0, 0, 1, 0 ] ],
[ [ 1, -1, 0, -1, 1, 0 ], [ 2, -2, 0, -1, 1, 0 ], [ 2, -1, -1, -1, 1, 0 ], [ 2, -2, 1, -1, 1, 0 ] ],
[ [ 1, -1, 0, -1, 1, 0 ], [ 2, -2, 0, -1, 1, 0 ], [ 2, -1, -1, -1, 1, 0 ], [ 3, -3, 0, -1, 1, 0 ] ],
[ [ 1, -1, 0, -1, 1, 0 ], [ 2, -2, 0, -1, 1, 0 ], [ 3, -2, 0, -2, 1, 0 ], [ 3, -3, 0, -1, 1, 0 ] ],
[ [ 1, -1, 0, -1, 1, 0 ], [ 2, -2, 0, -1, 1, 0 ], [ 3, -2, 0, -2, 1, 0 ], [ 2, -2, 0, -1, 2, 0 ] ]
],
"cur_uid": "43b009ff",
"ref_uid": 62820081,
"order_seed": 216475,
"dur_seed": 323751,
"motifs_seed": 466146,
"entrances_probs_vals": [ 0, 1.1904761904762, 3.3333333333333, 0.71, 1.92, 0, 0.5, 0.5, 0.5, 1, 0.5 ],
"passages_probs_vals": [ 0, 1.1904761904762, 3.3333333333333, 0.71, 1.92, 0, 0.5, 0.5, 0.5, 1, 0.5 ],
"exits_probs_vals": [ 0, 1.1904761904762, 3.3333333333333, 0.71, 1.92, 0, 0.5, 0.5, 0.5, 1, 0.5 ],
"ranges": [ [ -3600, -312 ], [ -1872, 1378 ], [ -145, 1583 ], [ -182, 1527 ] ],
"step_probs_vals": [ 0, 1200, 0, 0, 0.082304526748971, 0.99431818181818, 0.14197530864198, 0, 1, 0 ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"hd_exp": 2,
"hd_invert": 0,
"order":
[
[ [ 1, 0 ], [ 3, 2, 3 ], [ ] ]
],
"sus_weights": [ 0, 0.65, 0 ],
"order_size": [ 1, 1 ],
"passages_size": [ 1, 6 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,28 +0,0 @@
{
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r4 b'2.^\markup { \pad-markup #0.2 "-23"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 3↓" }} ~ }
\bar "|"
{ b'2. ~ b'8[ b'8^\markup { \pad-markup #0.2 "+30"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 11↑" }}] ~ }
\bar "|"
{ b'1 ~ }
\bar "|"
{ b'1 ~ }
\bar "|"
{ b'2. ~ b'8[ r8] }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "||"
}

@ -1,28 +0,0 @@
{
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r8[ gis'8^\markup { \pad-markup #0.2 "+10"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 7↓" }}] ~ gis'2. ~ }
\bar "|"
{ gis'1 ~ }
\bar "|"
{ gis'2. ~ gis'8.[ r16] }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "||"
}

@ -1,28 +0,0 @@
{
{ fis'1^\markup { \pad-markup #0.2 "-21"} ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'4 ~ fis'8[ r8] r2 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "||"
}

@ -1,28 +0,0 @@
{
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r2. r8[ cis'8^\markup { \pad-markup #0.2 "-19"}] ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'4 ~ cis'16[ r8.] r2 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "||"
}

@ -1,945 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc, genStepFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict, saveLedger;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, stepProbsVals, passagesWeights, hdExp, hdInvert,
orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc, stepFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, resourceDir, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark (not used)
/*
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
*/
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
//pDistance.gaussCurve(1, mean, sd)
stepFunc.value(pDistance);
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genStepFunc = {arg minStep, maxStep, envData, seed;
var envDataNorm, env, pTable, stepFunc;
[minStep, maxStep, envData].postln;
envDataNorm = ([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).flop;
envDataNorm = [envDataNorm[0].normalize(minStep, maxStep), envDataNorm[1]].flop;
env = Env.pairs(envDataNorm);
stepFunc = {arg pDist;
env.at(pDist).clip(0.001, 1);
};
seedFunc.value(stepFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = pow(hdSum.value(voices.deepCopy.put(ins, candidate)), hdExp);
if(hdInvert == 0, {hdScore = 1/hdScore});
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold.deepCopy});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq, fDur, durAdd;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
//round last duration to measure
fDur = fSeq.flatten.flatten.slice(nil, 1).sum;
durAdd = fDur.round(4) - fDur;
if(durAdd < 0, {durAdd = 4 - durAdd});
fSeq[0][orders.size - 1][fSeq[0][orders.size - 1].size - 1][1] = fSeq[0][orders.size - 1][fSeq[0][orders.size - 1].size - 1][1] + durAdd;
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.notNil) {
~msg;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr, oneShot = false;
var voices, durs, pbinds, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
pbinds = voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs, attacks, rels, amps;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
//attacks = 2.collect({rrand(1, 3)}) ++ freqs.drop(2).collect({rrand(0.3, 0.5)});
attacks = fDurs.collect({arg dur; dur * rrand(0.2, 0.4)});
//rels = freqs.drop(2).collect({rrand(0.3, 0.5)}) ++ 2.collect({rrand(1, 3)});
rels = (clumps.size - 1).collect({arg c;
if(clumps[c + 1][0] == ["Rest"], {rrand(1.0, 3.0)}, {rrand(0.3, 0.5)});
});
rels = rels.add(rrand(1.0, 3.0));
amps = freqs.collect({rrand(0.6, 0.99)});
[
Pbind(
\instrument, \string_model,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\attack, Pseq(attacks, 1),
\sustain, Pseq(fDurs, 1),
\release, Pseq(rels, 1),
//\amp, Pseq(amps, 1),
\amp, Pbrown(0.5, 1, 0.5),
\busIndex, v
),
Pbind(
\instrument, \sine,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
)
]
}).flatten;
if(oneShot.not, {
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
pbinds = pbinds ++
[
Pbind(
\type, \osc,
\addr, addr,
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
});
res = Ppar(pbinds);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
var tmpRes;
if(res.every({arg char; char.isDecDigit}), {tmpRes = res.asInteger});
if(res.contains("."), {tmpRes = res.asFloat});
if(tmpRes != nil, {res = tmpRes});
});
});
res
};
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, stepProbsVals, passagesWeights, hdExp, hdInvert,
orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict, skipLastXChanges = false;
var tmpLastXChanges;
tmpLastXChanges = lastXChanges.deepCopy;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, stepProbsVals, passagesWeights, hdExp, hdInvert,
orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
if(skipLastXChanges, {lastXChanges = tmpLastXChanges});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, stepProbsVals, passagesWeights, hdExp, hdInvert,
orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
resourceDir = path.splitext(".").drop(-1).join;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
saveLedger = {arg ledger, path;
var file, curResourceDir;
file = File(path, "w");
curResourceDir = resourceDir;
resourceDir = path.splitext(".").drop(-1).join;
if(curResourceDir != resourceDir, {
File.mkdir(resourceDir);
ledger.do({arg id;
File.copy(curResourceDir +/+ id, resourceDir +/+ id);
});
});
file.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
file.close;
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
~group = group;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
resourceDir = (dir +/+ ".." +/+ "resources" +/+ "piece_ledger");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "step_probs_vals", "passages_weights", "hd_exp", "hd_invert",
"order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\save_ledger, {arg msg, time, addr, port;
msg.postln;
ledger = msgInterpret.value(msg[1].asString.parseJSON["ledger"], false).postln;
//loadLedgerJSON.value(msg[0])
saveLedger.value(ledger, msg[2].asString);
//loadLedgerFile.value(msg[1].asString);
}, \save_ledger);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict, true);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
if(refUID == nil, {lastXChanges = [initVoices.value().deepCopy]});
if((refUID != nil) && (refUID != "tmp"), {
var file;
file = File((resourceDir +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
lastXChanges = msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
refUID.postln;
lastXChanges.collect({arg item; item.postln});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
stepFunc = genStepFunc.valueArray(stepProbsVals[..1] ++ [stepProbsVals[2..]] ++ [motifSeed]);
seq = seedFunc.value(genMotif, motifSeed).value;
lastXChanges.collect({arg item; item.postln});
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
//~seq = seq;
addr.sendMsg("/generated", path, modelString, ledgerPath);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2, lastCurUID, commitType, commitPos, equalityLedger;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
musicChanged = (musicData != seq).postln;
commitType = msg[2].asString;
commitPos = msg[3].postln.asInteger;
lastCurUID = curUID.deepCopy;
curUID = genUID.value;
File.mkdir((resourceDir +/+ curUID).standardizePath);
File.copy(exPath, (resourceDir +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (resourceDir +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {
seq = musicData;
dict["music_data"] = seq;
dict["motif_edited"] = "true"
});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
/*
if(commitType == "add", {
if(lastCurUID == "tmp", {
ledger = ledger.drop(-1).add(curUID);
}, {
ledger = ledger.add(curUID);
})
});
*/
ledger.postln;
if(commitType == "add", {ledger = ledger.add(curUID)});
if(commitType == "insert", {ledger = ledger.insert(commitPos + 1, curUID)});
if(commitType == "replace", {ledger = ledger.put(commitPos, curUID)});
equalityLedger = ledger.collect({arg item; item.asSymbol});
if(equalityLedger.includes(\tmp).postln, {ledger.removeAt(equalityLedger.indexOf(\tmp).postln)});
ledger.postln;
saveLedger.value(ledger, ledgerPath);
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
group.set(\release, 2);
group.set(\gate, 0);
player.stop;
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
if(msg[1] == 1, {
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (resourceDir +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.postln.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (resourceDir +/+ "tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
}, {
pSeq = [loadModelJSON.value(msg[2].asString.parseJSON)["music_data"].postln];
patterns = genPatterns.value(pSeq, addr, true);
});
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
addr.sendMsg("/one_shot", 0);
});
player = player.play
});
}, \transport);
OSCdef(\transcribe_motif, {arg msg, time, addr, port;
var tSeq, refChord, refUID;
msg.postln;
tSeq = [loadModelJSON.value(msg[1].asString.parseJSON)["music_data"]];
refUID = msg[2].asString.postln;
if((refUID != "nil") && (refUID != "tmp"), {
var file;
file = File((resourceDir +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
refChord = msgInterpret.value(file.readAllString.parseJSON["last_changes"]).last;
file.close;
}, {
refChord = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]];
});
~transcribe.value(tSeq, refChord, (dir +/+ ".." +/+ "lilypond" +/+ "includes").standardizePath, addr, "/transcribe_motif");
}, \transcribe_motif);
OSCdef(\transcribe_all, {arg msg, time, addr, port;
var cSize, patterns, cuedSeek, indexStart, indexEnd, tmpLedger;
if(true, {
cuedSeek = (seq != nil);
indexStart = msg[1].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//tmp for testing transcription
indexEnd = (indexStart+5);
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
var lilyPartLedgerFiles;
lilyPartLedgerFiles = 4.collect({arg p;
File((dir +/+ ".." +/+ "lilypond" +/+ "includes" +/+ "part_" ++ ["IV", "III", "II", "I"][p] ++ ".ly").standardizePath, "w");
});
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file, fileString, tSeq, refUID, refChord;
path = (resourceDir +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
fileString = file.readAllString;
tSeq = msgInterpret.value(fileString.parseJSON["music_data"]);
refUID = msgInterpret.value(fileString.parseJSON["ref_uid"]);
file.close;
//uid.postln;
//(refUID == "nil").postln;
refChord = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]];
if(refUID != "nil", {
path = (resourceDir +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
refChord = msgInterpret.value(file.readAllString.parseJSON["last_changes"]).last;
file.close;
});
if(index != indexEnd, {
~transcribe.value(tSeq, refChord, (resourceDir +/+ uid +/+ "lilypond").standardizePath);
}, {
~transcribe.value(tSeq, refChord, (resourceDir +/+ uid +/+ "lilypond").standardizePath, addr, "/transcribe_all");
});
lilyPartLedgerFiles.do({arg f, p;
f.write("\\include \"" ++ resourceDir +/+ uid +/+ "lilypond" +/+ "part_" ++ ["IV", "III", "II", "I"][p] ++ ".ly\"\n");
});
});
lilyPartLedgerFiles.do({arg f;
f.close
});
});
/*
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
*/
}, {
});
}, \transcribe_all);
)

@ -1,54 +0,0 @@
{
"music_data":
[
[
[
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ 1, 0, 1, -2, 1, 1 ] ], 1.75 ],
[ [ [ "Rest" ], [ "Rest" ], [ 1, 0, 1, -2, 1, 0 ], [ 1, 0, 1, -2, 1, 1 ] ], 1.5 ]
],
[
[ [ [ "Rest" ], [ "Rest" ], [ 1, 0, 1, -2, 1, 0 ], [ "Rest" ] ], 0 ],
[ [ [ "Rest" ], [ 1, -1, 1, -2, 2, 0 ], [ 1, 0, 1, -2, 1, 0 ], [ "Rest" ] ], 1.5 ],
[ [ [ "Rest" ], [ 1, -1, 1, -2, 2, 0 ], [ 1, -1, 2, -2, 2, 0 ], [ "Rest" ] ], 0 ],
[ [ [ 0, -1, 1, -2, 2, 0 ], [ 1, -1, 1, -2, 2, 0 ], [ 1, -1, 2, -2, 2, 0 ], [ "Rest" ] ], 0.75 ],
[ [ [ 0, -1, 1, -2, 2, 0 ], [ 1, -1, 1, -2, 2, 0 ], [ 2, -2, 1, -2, 2, 0 ], [ "Rest" ] ], 0.75 ],
[ [ [ -1, -1, 1, -1, 2, 0 ], [ 1, -1, 1, -2, 2, 0 ], [ 2, -2, 1, -2, 2, 0 ], [ "Rest" ] ], 0.625 ],
[ [ [ 0, -1, 0, -2, 2, 0 ], [ 1, -1, 1, -2, 2, 0 ], [ 2, -2, 1, -2, 2, 0 ], [ "Rest" ] ], 2.125 ],
[ [ [ "Rest" ], [ 1, -1, 1, -2, 2, 0 ], [ 2, -2, 1, -2, 2, 0 ], [ "Rest" ] ], 1.125 ],
[ [ [ "Rest" ], [ 1, -1, 1, -2, 2, 0 ], [ "Rest" ], [ "Rest" ] ], 0.625 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 5.25 ]
]
]
],
"last_changes":
[
[ [ 1, -1, 1, -3, 2, 0 ], [ 1, -1, 1, -2, 2, 0 ], [ 1, -1, 2, -2, 2, 0 ], [ 1, 0, 1, -2, 1, 1 ] ],
[ [ 0, -1, 1, -2, 2, 0 ], [ 1, -1, 1, -2, 2, 0 ], [ 1, -1, 2, -2, 2, 0 ], [ 1, 0, 1, -2, 1, 1 ] ],
[ [ 0, -1, 1, -2, 2, 0 ], [ 1, -1, 1, -2, 2, 0 ], [ 2, -2, 1, -2, 2, 0 ], [ 1, 0, 1, -2, 1, 1 ] ],
[ [ -1, -1, 1, -1, 2, 0 ], [ 1, -1, 1, -2, 2, 0 ], [ 2, -2, 1, -2, 2, 0 ], [ 1, 0, 1, -2, 1, 1 ] ],
[ [ 0, -1, 0, -2, 2, 0 ], [ 1, -1, 1, -2, 2, 0 ], [ 2, -2, 1, -2, 2, 0 ], [ 1, 0, 1, -2, 1, 1 ] ]
],
"cur_uid": "443ec222",
"ref_uid": "6d635e88",
"order_seed": 516056,
"dur_seed": 358555,
"motifs_seed": 962315,
"entrances_probs_vals": [ 0.18, 0.28, 1.4285714285714, 0.47, 1.62, 0, 0.5, 0.5, 0.5, 1, 0.5 ],
"passages_probs_vals": [ 0.29, 0, 1.1111111111111, 0.65934065934066, 1.37, 0, 0.5, 0.5, 0.5, 1, 0.5 ],
"exits_probs_vals": [ 0.18, 0.28, 1.4285714285714, 0.47, 1.62, 0, 0.5, 0.5, 0.5, 1, 0.5 ],
"ranges": [ [ -2411.1455108359, -850.77399380805 ], [ -1872, 450 ], [ -479, 1304.0247678019 ], [ -368, 1173.9938080495 ] ],
"step_probs_vals": [ 0, 1200, 0, 0, 0.082304526748971, 0.99431818181818, 0.33950617283951, 0, 0.72839506172839, 0, 1, 0 ],
"passages_weights": [ 0.35, 0.42, 0.75, 0.9, 0.93 ],
"hd_exp": 2,
"hd_invert": 0,
"order":
[
[ [ 3 ], [ 2 ], [ 0, 1 ] ],
[ [ 1 ], [ 2, 0, 2, 0, 0 ], [ 3 ] ]
],
"sus_weights": [ 0.41, 0, 0 ],
"order_size": [ 2, 6 ],
"passages_size": [ 0, 5 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,18 +0,0 @@
{
{ ais'1^\markup { \pad-markup #0.2 "+41"} ~ }
\bar "|"
{ ais'2 ~ ais'8[ r8] r4 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "||"
}

@ -1,18 +0,0 @@
{
{ r2. r8[ d'8^\markup { \pad-markup #0.2 "+0"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 13↓" }}] ~ }
\bar "|"
{ d'1 ~ }
\bar "|"
{ d'4 ~ d'8[ e'8^\markup { \pad-markup #0.2 "+36"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 5↑" }}] ~ e'4 f'4^\markup { \pad-markup #0.2 "+47"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 3↓" }} ~ }
\bar "|"
{ f'1 ~ }
\bar "|"
{ f'1 ~ }
\bar "|"
{ f'16[ r8.] r2. }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "||"
}

@ -1,18 +0,0 @@
{
{ r1 }
\bar "|"
{ r2 r8[ c'8^\markup { \pad-markup #0.2 "+49"}] ~ c'4 ~ }
\bar "|"
{ c'1 ~ }
\bar "|"
{ c'1 ~ }
\bar "|"
{ c'1 ~ }
\bar "|"
{ c'4 ~ c'8[ r8] r2 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "||"
}

@ -1,18 +0,0 @@
{
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r4 r8[ c8^\markup { \pad-markup #0.2 "+49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 1↑" }}] ~ c2 ~ }
\bar "|"
{ c8[ ais,8^\markup { \pad-markup #0.2 "+18"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 7↑" }}] ~ ais,8.[ a,16^\markup { \pad-markup #0.2 "-37"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 5↓" }}] ~ a,2 ~ }
\bar "|"
{ a,2 r2 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "||"
}

@ -1,943 +0,0 @@
(
// helper funcs
var hsArrayToCents, pDist, hdSum, hsChordalDistance, hsArrayToFreq;
// score funcs
var isInRange, spacingScore, rangeScore, intervalScore, inclusionScore;
// subroutines
var genTuples, initVoices, genOrders, genSubMotif, updateVoices, genDurFunc, genStepFunc;
// primary routines
var genMotif, genSecondarySeq;
// audition funcs
var genPatterns, genMidiPatterns;
// resource management funcs
var seedFunc, genUID, writeResources, stringifyToDepth, setSeeds, sanityCheck,
msgInterpret, loadLedgerFile, loadLedgerJSON, loadModelFile, loadModelJSON,
setGlobalVars, globalVarsToDict, saveLedger;
// model vars
//(model and global vars mostly set by OSC funcs
var seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, stepProbsVals, passagesWeights, hdExp, hdInvert,
orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited;
// model aux vars
var entrancesDurFunc, passagesDurFunc, exitsDurFunc, stepFunc;
// other global vars
var popSize, exPath, dir, primes, dims, tuples,
group, player, resourceDir, ledgerPath, ledger, currentlyPlayingUID,
nameSpaces;
// install JSON quark (not used)
/*
if(Quarks.isInstalled("JSONlib").not, {
Quarks.install("https://github.com/musikinformatik/JSONlib.git");
thisProcess.recompile;
//HelpBrowser.openHelpFor("Classes/JSONlib");
});
*/
//------helper funcs
hsArrayToCents = {
arg hsArray;
hsArray.collect({arg dist, p; dist * 1200 * log2(primes[p][0]/primes[p][1])}).sum
};
pDist = {
arg array1, array2, signed = false;
var pDistance;
pDistance = hsArrayToCents.value(array1) - hsArrayToCents.value(array2);
if(signed, {pDistance}, {abs(pDistance)})
};
hdSum = {
arg hsArrays;
var size, distances, mean;
size = hsArrays.size;
distances = (size - 1).collect({arg i;
((i + 1)..(size - 1)).collect({arg j;
abs(hsArrays[i] - hsArrays[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsChordalDistance = {
arg hsArrays1, hsArrays2;
var size, distances, mean;
size = hsArrays1.size;
distances = hsArrays1.size.collect({arg i;
hsArrays2.size.collect({arg j;
abs(hsArrays1[i] - hsArrays2[j]).collect({arg dist, p; dist * log2(primes[p].product)}).sum
});
}).flat;
mean = distances.sum / distances.size;
distances.sum
//mean + ((1 / sqrt((pow(distances - mean, 2)).sum / distances.size)) * mean)
};
hsArrayToFreq = {
arg array;
array.collect({arg dim, d; pow(primes[d][0]/primes[d][1], dim)}).product
};
//------score funcs
/*
isInRange = {
arg hsArray, min, max;
var cents;
cents = hsArrayToCents.value(hsArray);
(cents >= min) && (cents <= max)
};
*/
spacingScore = {
arg hsArrays, min;
var centsArray;
centsArray = hsArrays.collect({arg hsArray; hsArrayToCents.value(hsArray)}).sort({arg a, b; a < b});
centsArray.differentiate.drop(1).collect({arg pDistance; if(pDistance >= min, {1}, {0.01})}).sum;
};
rangeScore = {
arg hsArray1, hsArray2, min, max, low, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
if((pDistance >= min) && (pDistance <= max), {1}, {low});
};
intervalScore = {
arg hsArray1, hsArray2, mean, sd, signed = false;
var pDistance;
pDistance = pDist.value(hsArray1, hsArray2, signed);
//pDistance.gaussCurve(1, mean, sd)
stepFunc.value(pDistance);
};
inclusionScore = {
arg array, test, min = 0.01;
if(array.collect({arg v; v.hash}).includes(test.hash), {min}, {1});
};
//------subroutines
genTuples = {
var tuples;
tuples = dims.collect({[-1, 0, 1]}).allTuples.select({arg tuple; (abs(tuple.drop(1)).sum <= 1) && (tuple[0] == 0)});
tuples = tuples ++ tuples.collect({arg tuple; [-3, -2, -1, 1, 2, 3].collect({arg octTrans; tuple.deepCopy.put(0, octTrans)})}).flatten;
};
initVoices = {
var init, voicesInit;
voicesInit = popSize.collect({dims.collect({0})});
/*
voicesInit = [dims.collect({0})];
(popSize - 1).do({
arg rep, new;
rep = dims.rand;
new = voicesInit.last.deepCopy;
new[rep] = new[rep] + [-1, 1].choose();
voicesInit = voicesInit.add(new);
});
*/
voicesInit.deepCopy;
};
genDurFunc = {arg chordProb, minPad, maxPad, minDur, maxDur, envData, seed;
var env, pTable, durFunc;
env = Env.pairs([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).asSignal(256).asList.asArray;
pTable = env.asRandomTable;
[chordProb, minPad, maxPad, minDur, maxDur, envData].postln;
durFunc = {arg allowChord, pad = false;
var res;
res = if(allowChord.not, {
pTable.tableRand * (maxDur - minDur) + minDur
}, {
if(1.0.rand < chordProb, {0}, {pTable.tableRand * (maxDur - minDur) + minDur});
}).round(0.125);
if(pad, {res = res + rrand(minPad.asFloat, maxPad.asFloat).round(0.125)});
if(res.asInteger == res, {res = res.asInteger});
res
};
seedFunc.value(durFunc, seed);
};
genStepFunc = {arg minStep, maxStep, envData, seed;
var envDataNorm, env, pTable, stepFunc;
[minStep, maxStep, envData].postln;
envDataNorm = ([[0, 0]] ++ envData.clump(2) ++ [[1, 0]]).flop;
envDataNorm = [envDataNorm[0].normalize(minStep, maxStep), envDataNorm[1]].flop;
env = Env.pairs(envDataNorm);
stepFunc = {arg pDist;
env.at(pDist).clip(0.001, 1);
};
seedFunc.value(stepFunc, seed);
};
genOrders = {arg minMotifLength = 1, maxMotifLength = 5, minProgLength = 0, maxProgLength = 5;
((maxMotifLength - minMotifLength).rand + minMotifLength).collect({
var noProgIns, noSusIns, noSilentIns, prog, sus, silent, order;
noSusIns = [1, 2, 3].wchoose(susWeights.normalizeSum);
noProgIns = (popSize - noSusIns).rand + 1;
noSilentIns = popSize - noSusIns - noProgIns;
# prog, sus, silent = (0..(popSize-1)).scramble.clumps([noProgIns, noSusIns, noSilentIns]);
prog = (prog.scramble ++ ((maxProgLength - minProgLength).rand + minProgLength).collect({prog.choose}).scramble);
if(silent == nil, {silent = []});
[sus.scramble, prog, silent.scramble]
});
};
updateVoices = {arg ins, sus;
var voices, candidates, nWeights, nProbs, sel;
voices = lastXChanges.deepCopy.last;
candidates = sus.collect({arg v; tuples.collect({arg t; voices[v] + t})}).flatten;
candidates = difference(candidates.asSet, voices.asSet).asList;
nProbs = candidates.collect({arg candidate;
var stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore;
//stepScore = intervalScore.value(voices[ins], candidate, 30, 400, 0.1);
stepScore = intervalScore.value(voices[ins], candidate, 100, 100);
recentlySoundedScore = inclusionScore.value(lastXChanges.flop[ins], candidate, 0);
isInRangeScore = rangeScore.value(candidate, candidate.collect({0}), ranges[ins][0], ranges[ins][1], 0, true);
regScore = spacingScore.value(voices.deepCopy.put(ins, candidate), 300);
hdScore = pow(hdSum.value(voices.deepCopy.put(ins, candidate)), hdExp);
if(hdInvert == 0, {hdScore = 1/hdScore});
//maybe what you want here is a vector to another root and then favoring movement towards it.
//distScore = pow(hsChordalDistance.value(voices, voices.put(ins, candidate)), 2);
[stepScore, recentlySoundedScore, isInRangeScore, regScore, hdScore]
});
nWeights = passagesWeights;
//this handles nWeights of 0; mainly for testing
nProbs = nProbs.flop.select({arg scores, s; nWeights[s] != 0}).flop;
nWeights = nWeights.select({arg weight; weight != 0});
nProbs = nProbs.flop.collect({arg scores, s;
if(scores.sum == 0, {scores}, {scores.normalizeSum * nWeights[s]})
});
nProbs = nProbs.flop.collect({arg scores, s; scores.product}).normalizeSum;
sel = candidates.wchoose(nProbs);
voices[ins] = sel;
lastXChanges = lastXChanges.add(voices).keep(-5);
};
genSubMotif = {arg order, orderIndex, lastState, repeatLast = false, startFromLast = false, isLastOrder = false;
var sus, prog, silent, flatOrder, res, isInChord, allowChord, pad, lastXChangesHold, voices, adder;
# sus, prog, silent = order;
flatOrder = silent ++ sus ++ prog;
lastXChangesHold = lastXChanges.deepCopy;
voices = lastState.deepCopy;
isInChord = popSize.collect({false});
allowChord = false;
pad = false;
res = [];
"------generating motif".postln;
//need to figure out here if voices move between motifs
flatOrder.do({arg ins, i;
if(prog.includes(ins) && repeatLast.not, {updateVoices.value(ins, sus)});
adder = if(silent.includes(ins), {["Rest"]}, {lastXChanges.last.deepCopy[ins]});
if(voices[ins] != adder, {
var dur;
if((sus ++ silent).includes(ins), {
allowChord = (ins != sus.last);
pad = (ins == sus.last);
}, {
if(i < (flatOrder.size - 1), {
allowChord = (isInChord[flatOrder[i + 1]] || (ins == flatOrder[i + 1])).not;
pad = false;
}, {
allowChord = false;
pad = true
});
});
if((orderIndex == 0) && sus.includes(ins), {
dur = entrancesDurFunc.value(allowChord, pad);
}, {
dur = passagesDurFunc.value(allowChord, pad);
});
if(dur == 0, {isInChord[ins] = true}, {isInChord = popSize.collect({false})});
voices[ins] = adder;
res = res.add([voices.deepCopy.postln, dur]);
});
});
// pad ending
if(orderIndex == (orders.size - 1), {
(0..(popSize-1)).scramble.do({arg ins;
if(res.last.first[ins] != ["Rest"], {
var dur;
voices[ins] = ["Rest"];
allowChord = (voices != popSize.collect({["Rest"]}));
pad = allowChord.not;
dur = exitsDurFunc.value(allowChord, pad);
res = res.add([voices.deepCopy.postln, dur]);
});
});
});
//format and return
if(startFromLast, {lastXChanges = lastXChangesHold.deepCopy});
res;
};
//------primary routines
genMotif = {
var repeats, fSeq, fDur, durAdd;
repeats = 1;
fSeq = [];
repeats.do({arg index;
var motif;
motif = [];
orders.do({arg order, o;
var lastState, subMotif;
lastState = if(o == 0, {popSize.collect({["Rest"]})}, {motif.last.last.first});
subMotif = genSubMotif.value(order, o, lastState, isLastOrder: o == (orders.size - 1));
motif = motif.add(subMotif);
});
sanityCheck.value(motif, index);
fSeq = fSeq.add(motif);
});
//round last duration to measure
fDur = fSeq.flatten.flatten.slice(nil, 1).sum;
durAdd = fDur.round(4) - fDur;
if(durAdd < 0, {durAdd = 4 - durAdd});
fSeq[0][orders.size - 1][fSeq[0][orders.size - 1].size - 1][1] = fSeq[0][orders.size - 1][fSeq[0][orders.size - 1].size - 1][1] + durAdd;
fSeq
};
genSecondarySeq = {arg seq;
var curdles, fSeq;
curdles = [];
while({curdles.sum < seq.size}, {curdles = curdles ++ [3.rand + 1]});
fSeq = seq.clumps(curdles).collect({arg clump, m;
var repeats, paddedSeq;
//add rest
paddedSeq = clump.add([[[popSize.collect({["Rest"]}), 0.5.rand]]]);
//implement repeats
repeats = [0.rand + 1, 1].wchoose([1, 0].normalizeSum);
repeats.collect({paddedSeq});
});
fSeq
};
//------audition funcs
/*
Event.addEventType(\osc, {
if (~addr.postln.notNil) {
~addr.sendMsg(~indexPath, ~indexMsg);
~addr.sendMsg(~seqPath, stringifyToDepth.value(~seqMsg, 3));
//~addr.sendMsg("/STATE/OPEN", (dir.replace("supercollider", "resources") +/+ ~idMsg +/+ ~idMsg ++ "_gui_state" ++ ".state").standardizePath.postln);
};
});
*/
Event.addEventType(\osc, {
if (~addr.notNil) {
~msg;
~addr.sendMsg(~path, *~msg);
};
});
genPatterns = {arg inSeq, addr, oneShot = false;
var voices, durs, pbinds, res, indices, sectionDurs, msg, ids, seq;
seq = inSeq.collect({arg mSeq; mSeq[0]});
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
pbinds = voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs, attacks, rels, amps;
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
//attacks = 2.collect({rrand(1, 3)}) ++ freqs.drop(2).collect({rrand(0.3, 0.5)});
attacks = fDurs.collect({arg dur; dur * rrand(0.2, 0.4)});
//rels = freqs.drop(2).collect({rrand(0.3, 0.5)}) ++ 2.collect({rrand(1, 3)});
rels = (clumps.size - 1).collect({arg c;
if(clumps[c + 1][0] == ["Rest"], {rrand(1.0, 3.0)}, {rrand(0.3, 0.5)});
});
rels = rels.add(rrand(1.0, 3.0));
amps = freqs.collect({rrand(0.6, 0.99)});
[
Pbind(
\instrument, \string_model,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\attack, Pseq(attacks, 1),
\sustain, Pseq(fDurs, 1),
\release, Pseq(rels, 1),
//\amp, Pseq(amps, 1),
\amp, Pbrown(0.5, 1, 0.5),
\busIndex, v
),
Pbind(
\instrument, \sine,
\group, group,
\freq, Pseq(freqs, 1),
\dur, Pseq(fDurs, 1),
\sustain, Pseq(fDurs, 1),
\busIndex, v
)
]
}).flatten;
if(oneShot.not, {
msg = inSeq.collect({arg mSeq, m; mSeq[1..]});
//ids = inSeq.collect({arg mSeq, m; mSeq[2]});
sectionDurs = seq.collect({arg mSeq; mSeq.flatten2(mSeq.maxDepth - 5).flop[1].sum});
pbinds = pbinds ++
[
Pbind(
\type, \osc,
\addr, addr,
\path, "/playing",
\msg, Pseq(msg, 1),
\dur, Pseq(sectionDurs, 1)
);
]
});
res = Ppar(pbinds);
res
};
/*
genMidiPatterns = {arg seq;
var voices, durs, patterns, res, mOut, pbRange;
pbRange = 1; //semitones - change this as needed for your situation
mOut = MIDIOut.newByName("TiMidity", "TiMidity port 0").latency_(Server.default.latency);
# voices, durs = seq.flatten2(seq.maxDepth - 5).flop;
res = Ppar(
voices.flop.collect({arg voice, v;
var clumps, hdScores, freqs, fDurs;
mOut.program(v, 70);
clumps = voice.separate({arg a, b; a != b });
freqs = clumps.collect({arg clump; if(clump[0] != ["Rest"], {(60.midicps * hsArrayToFreq.value(clump[0]))}, {Rest(0)})});
fDurs = durs.clumps(clumps.collect({arg clump; clump.size})).collect({arg clump; clump.sum});
Pbind(
\type, \midi,
\chan, v,
\noteval, Pseq(freqs.cpsmidi - 24, 1),
\note, Pfunc({ | event | event[\noteval].floor }),
\dur, Pseq(fDurs, 1),
\midiout, mOut,
\amp, 1,
\bend, Pfunc({
| event |
if (event[\note].isRest.not) {
var pitchbendvalue = event[\noteval].frac.linlin(0, pbRange, 8192, 8192*2).asInteger;
m.bend(v, pitchbendvalue);
};
0; // return something other than nil to avoid stopping the pattern
}),
);
});
);
res
};
*/
//------resource management funcs
genUID = {Date.seed.asHexString.toLower};
seedFunc = {arg func, seed;
var funcArgs, next;
next = Routine({loop{func.valueArray(funcArgs).yield }});
next.randSeed_(seed);
{arg ...args; funcArgs = args; next.value}
};
stringifyToDepth = {arg data, maxDepth = 1;
var prettyString = "", rCount = 0, writeArray, indent;
if(maxDepth == 0, {
data.asCompileString
}, {
indent = {arg size; size.collect({" "}).join("")};
writeArray = {arg array;
prettyString = prettyString ++ indent.value(rCount) ++ "[\n";
rCount = rCount + 1;
if(rCount < maxDepth, {
array.do({arg subArray; writeArray.value(subArray)});
}, {
prettyString = prettyString ++ array.collect({arg subArray;
indent.value(rCount + 1) ++ subArray.asCompileString
}).join(",\n");
});
rCount = rCount - 1;
prettyString = prettyString ++ "\n" ++ indent.value(rCount) ++ "],\n";
};
writeArray.value(data);
prettyString.replace(",\n\n", "\n").drop(-2);
})
};
sanityCheck = {arg motif, index;
//print functions = very helpful
("----------" + index + "------------").postln;
motif.flatten.do({arg val, v;
if(v > 0, {
if(motif.flatten[v-1][0].hammingDistance(val[0]) > 1, {"problem 1".postln});
if(motif.flatten[v-1][0].hammingDistance(val[0]) == 0, {"problem 2".postln});
});
val.postln
});
"***********".postln;
};
msgInterpret = {arg in, escapeDoubleQuotes = true, escapeSingleQuotes = true;
var res;
res = in;
if(res.isNil.not, {
if((res.isArray && res.isString.not), {
res = res.asCompileString;
res = res.replace(" ", "").replace("\n", "").replace("\t", "");
if(escapeSingleQuotes, {res = res.replace("\'", "")});
if(escapeDoubleQuotes, {res = res.replace("\"", "")});
res = res.replace("Rest", "\"Rest\"");
res = res.interpret;
}, {
var tmpRes;
if(res.every({arg char; char.isDecDigit}), {tmpRes = res.asInteger});
if(res.contains("."), {tmpRes = res.asFloat});
if(tmpRes != nil, {res = tmpRes});
});
});
res
};
writeResources = {arg path, dict;
var file, modelItems, resString;
file = File(path,"w");
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, stepProbsVals, passagesWeights, hdExp, hdInvert,
orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
resString = nameSpaces.collect({arg nameSpace;
var depth = 0, insert = " ";
if(nameSpace == "music_data", {depth = 3; insert = "\n"});
if(nameSpace == "last_changes", {depth = 1; insert = "\n"});
if(nameSpace == "order", {depth = 1; insert = "\n"});
if((nameSpace == "ref_uid") && (dict[nameSpace] == nil), {dict[nameSpace] = "nil"});
"\"" ++ nameSpace ++ "\":" ++ insert ++ stringifyToDepth.value(dict[nameSpace], depth)
}).join(",\n");
resString = "{\n" ++ resString ++ "\n}";
file.write(resString);
file.close;
resString
};
loadModelFile = {arg path; loadModelJSON.value(File(path, "r").readAllString.parseJSON)};
loadModelJSON = {arg jsonObject;
var dict;
dict = Dictionary.with(*nameSpaces.collect({arg nS; nS->msgInterpret.value(jsonObject[nS])}));
dict
};
setGlobalVars = {arg dict, skipLastXChanges = false;
var tmpLastXChanges;
tmpLastXChanges = lastXChanges.deepCopy;
// order really matters!!!!
# seq, lastXChanges, curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, stepProbsVals, passagesWeights, hdExp, hdInvert,
orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited = nameSpaces.collect({arg nS; dict[nS]});
if(skipLastXChanges, {lastXChanges = tmpLastXChanges});
dict
};
globalVarsToDict = {
var modelItems, dict;
// order really matters!!!!
modelItems = [
seq, lastXChanges,
curUID, refUID, orderSeed, durSeed, motifSeed,
entrancesProbVals, passagesProbVals, exitsProbVals,
ranges, stepProbsVals, passagesWeights, hdExp, hdInvert,
orders, susWeights, orderSize, passagesSize,
motifEdited, orderEdited
];
dict = Dictionary.with(*nameSpaces.collect({arg nS, n; nS->modelItems[n]}));
};
loadLedgerFile = {arg path;
ledgerPath = path;
resourceDir = path.splitext(".").drop(-1).join;
loadLedgerJSON.value(File(ledgerPath, "r").readAllString.parseJSON)
};
loadLedgerJSON = {arg jsonObject; ledger = jsonObject["ledger"]};
saveLedger = {arg ledger, path;
var file, curResourceDir;
file = File(path, "w");
curResourceDir = resourceDir;
resourceDir = path.splitext(".").drop(-1).join;
File.mkdir(resourceDir);
ledger.do({arg id;
File.copy(curResourceDir +/+ id, resourceDir +/+ id);
});
file.write("{\n\"ledger\":\n" ++ stringifyToDepth.value(ledger, 1) ++ "\n}");
file.close;
};
//------global vars
primes = [[2, 1], [3, 2], [5, 4], [7, 4], [11, 8], [13, 8]];
//ranges = [[-2400, 0], [-1200, 1200], [0, 2400], [0, 2400]];
exPath = thisProcess.nowExecutingPath;
dir = exPath.dirname;
//popSize = 4;
dims = primes.size;
tuples = genTuples.value();
//refUID = nil;
group = Group.new;
~group = group;
loadLedgerFile.value(dir +/+ ".." +/+ "resources" +/+ "piece_ledger.json");
resourceDir = (dir +/+ ".." +/+ "resources" +/+ "piece_ledger");
//passagesWeights = [1, 1, 1, 1, 1];
//susWeights = [1, 1, 1];
// order really matters!!!!
nameSpaces = [
"music_data", "last_changes", "cur_uid", "ref_uid", "order_seed", "dur_seed", "motifs_seed",
"entrances_probs_vals","passages_probs_vals", "exits_probs_vals",
"ranges", "step_probs_vals", "passages_weights", "hd_exp", "hd_invert",
"order", "sus_weights", "order_size", "passages_size",
"motif_edited", "order_edited"
];
//------OSC funcs
OSCdef(\load_ledger, {arg msg, time, addr, port;
loadLedgerFile.value(msg[1].asString);
}, \load_ledger);
OSCdef(\load_model, {arg msg, time, addr, port;
var dict;
dict = loadModelFile.value(msg[1].asString);
setGlobalVars.value(dict);
}, \load_model);
OSCdef(\save_ledger, {arg msg, time, addr, port;
msg.postln;
ledger = msgInterpret.value(msg[1].asString.parseJSON["ledger"], false).postln;
//loadLedgerJSON.value(msg[0])
saveLedger.value(ledger, msg[2].asString);
//loadLedgerFile.value(msg[1].asString);
}, \save_ledger);
OSCdef(\generate, {arg msg, time, addr, port;
var path, dict, durSeeds, musPath, modelString;
msg.postln;
path = msg[1].asString;
dict = loadModelFile.value(path);
setGlobalVars.value(dict, true);
popSize = ranges.size;
//refUID.postln;
loadLedgerFile.value(ledgerPath);
if(ledger == nil, {ledger = ["tmp"]});
if(ledger.last != "tmp", {ledger = ledger.add("tmp")});
if(refUID == nil, {lastXChanges = [initVoices.value().deepCopy]});
if((refUID != nil) && (refUID != "tmp"), {
var file;
file = File((resourceDir +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
lastXChanges = msgInterpret.value(file.readAllString.parseJSON["last_changes"]);
});
refUID.postln;
lastXChanges.collect({arg item; item.postln});
durSeeds = seedFunc.value({3.collect({rrand(100000, 999999)})}, durSeed).value.postln;
entrancesDurFunc = genDurFunc.valueArray(entrancesProbVals[..4] ++ [entrancesProbVals[5..]] ++ [durSeeds[0]]);
passagesDurFunc = genDurFunc.valueArray(passagesProbVals[..4] ++ [passagesProbVals[5..]] ++ [durSeeds[1]]);
exitsDurFunc = genDurFunc.valueArray(exitsProbVals[..4] ++ [exitsProbVals[5..]] ++ [durSeeds[2]]);
if(orders == nil, {
orders = seedFunc.value(genOrders, orderSeed).valueArray(orderSize ++ passagesSize);
//addr.sendMsg("/order", stringifyToDepth.value(orders, 1));
});
stepFunc = genStepFunc.valueArray(stepProbsVals[..1] ++ [stepProbsVals[2..]] ++ [motifSeed]);
seq = seedFunc.value(genMotif, motifSeed).value;
lastXChanges.collect({arg item; item.postln});
dict = globalVarsToDict.value;
modelString = writeResources.value(path, dict);
//addr.sendMsg("/generated", musPath, stringifyToDepth.value(seq, 3));
//~seq = seq;
addr.sendMsg("/generated", path, modelString, ledgerPath);
}, \generate);
OSCdef(\commit, {arg msg, time, addr, port;
var musicData, musicChanged, dict, newLedger, modelPath, musString, musFile, test1, test2, lastCurUID, commitType, commitPos, equalityLedger;
//msg.postln;
/*
test1 = msg[1].asString.parseJSON;
test2 = (dir +/+ ".." +/+ "resources/tmp/tmp_music" ++ ".json").standardizePath.parseJSONFile;
msgInterpret.value(test1["music"])[0][0][0][1].class.postln;
msgInterpret.value(test2["music_data"])[0][0][0][1].class.postln;
(test1["music"] == test2["music_data"]).postln;
*/
musicData = loadModelJSON.value(msg[1].asString.parseJSON)["music_data"].postln;
musicChanged = (musicData != seq).postln;
commitType = msg[2].asString;
commitPos = msg[3].postln.asInteger;
lastCurUID = curUID.deepCopy;
curUID = genUID.value;
File.mkdir((resourceDir +/+ curUID).standardizePath);
File.copy(exPath, (resourceDir +/+ curUID +/+ curUID ++ "_code" ++ ".scd").standardizePath);
modelPath = (resourceDir +/+ curUID +/+ curUID ++ "_mus_model" ++ ".json").standardizePath;
dict = globalVarsToDict.value;
if(musicChanged, {
seq = musicData;
dict["music_data"] = seq;
dict["motif_edited"] = "true"
});
dict["cur_uid"] = curUID;
writeResources.value(modelPath, dict);
File.delete(ledgerPath ++ "_bak");
File.copy(ledgerPath, ledgerPath ++ "_bak");
File.delete(ledgerPath);
/*
if(commitType == "add", {
if(lastCurUID == "tmp", {
ledger = ledger.drop(-1).add(curUID);
}, {
ledger = ledger.add(curUID);
})
});
*/
ledger.postln;
if(commitType == "add", {ledger = ledger.add(curUID)});
if(commitType == "insert", {ledger = ledger.insert(commitPos + 1, curUID)});
if(commitType == "replace", {ledger = ledger.put(commitPos, curUID)});
equalityLedger = ledger.collect({arg item; item.asSymbol});
if(equalityLedger.includes(\tmp).postln, {ledger.removeAt(equalityLedger.indexOf(\tmp).postln)});
ledger.postln;
saveLedger.value(ledger, ledgerPath);
addr.sendMsg("/committed", curUID, ledgerPath);
//refUID = curUID;
}, \commit);
OSCdef(\transport, {arg msg, time, addr, port;
msg.postln;
if(msg[1] == 0, {
group.set(\release, 2);
group.set(\gate, 0);
player.stop;
}, {
// the cued sequence can now be read from file, so this can be cleaned up
var cSize, patterns, pSeq, cuedSeek, indexStart, indexEnd, tmpLedger;
if(msg[1] == 1, {
pSeq = [];
cuedSeek = (seq != nil);
indexStart = msg[2].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file;
path = (resourceDir +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.postln.parseJSON["music_data"]), path, indexStart + index, uid]);
file.close;
});
});
if(cuedSeek, {
var path, file;
path = (resourceDir +/+ "tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
patterns = genPatterns.value(pSeq, addr);
}, {
pSeq = [loadModelJSON.value(msg[2].asString.parseJSON)["music_data"].postln];
patterns = genPatterns.value(pSeq, addr, true);
});
player = Pfset(pattern: patterns, cleanupFunc: {
addr.sendMsg("/transport", 0);
addr.sendMsg("/one_shot", 0);
});
player = player.play
});
}, \transport);
OSCdef(\transcribe_motif, {arg msg, time, addr, port;
var tSeq, refChord, refUID;
msg.postln;
tSeq = [loadModelJSON.value(msg[1].asString.parseJSON)["music_data"]];
refUID = msg[2].asString.postln;
if((refUID != "nil") && (refUID != "tmp"), {
var file;
file = File((resourceDir +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath, "r");
refChord = msgInterpret.value(file.readAllString.parseJSON["last_changes"]).last;
file.close;
}, {
refChord = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]];
});
~transcribe.value(tSeq, refChord, (dir +/+ ".." +/+ "lilypond" +/+ "includes").standardizePath, addr, "/transcribe_motif");
}, \transcribe_motif);
OSCdef(\transcribe_all, {arg msg, time, addr, port;
var cSize, patterns, cuedSeek, indexStart, indexEnd, tmpLedger;
if(true, {
cuedSeek = (seq != nil);
indexStart = msg[1].asInteger;
indexEnd = ledger.size - if(cuedSeek, {2}, {1});
//tmp for testing transcription
indexEnd = (indexStart+5);
//ledger.postln;
if(((indexStart == (ledger.size - 1)) && cuedSeek).not, {
var lilyPartLedgerFiles;
lilyPartLedgerFiles = 4.collect({arg p;
File((dir +/+ ".." +/+ "lilypond" +/+ "includes" +/+ "part_" ++ ["IV", "III", "II", "I"][p] ++ ".ly").standardizePath, "w");
});
ledger[indexStart..indexEnd].do({arg uid, index;
var path, file, fileString, tSeq, refUID, refChord;
path = (resourceDir +/+ uid +/+ uid ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
fileString = file.readAllString;
tSeq = msgInterpret.value(fileString.parseJSON["music_data"]);
refUID = msgInterpret.value(fileString.parseJSON["ref_uid"]);
file.close;
//uid.postln;
//(refUID == "nil").postln;
refChord = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]];
if(refUID != "nil", {
path = (resourceDir +/+ refUID +/+ refUID ++ "_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
refChord = msgInterpret.value(file.readAllString.parseJSON["last_changes"]).last;
file.close;
});
if(index != indexEnd, {
~transcribe.value(tSeq, refChord, (resourceDir +/+ uid +/+ "lilypond").standardizePath);
}, {
~transcribe.value(tSeq, refChord, (resourceDir +/+ uid +/+ "lilypond").standardizePath, addr, "/transcribe_all");
});
lilyPartLedgerFiles.do({arg f, p;
f.write("\\include \"" ++ resourceDir +/+ uid +/+ "lilypond" +/+ "part_" ++ ["IV", "III", "II", "I"][p] ++ ".ly\"\n");
});
});
lilyPartLedgerFiles.do({arg f;
f.close
});
});
/*
if(cuedSeek, {
var path, file;
path = (dir +/+ ".." +/+ "resources/tmp/tmp_mus_model" ++ ".json").standardizePath;
file = File(path, "r");
pSeq = pSeq.add([msgInterpret.value(file.readAllString.parseJSON["music_data"]), path, ledger.size - 1, "tmp"]);
file.close;
});
*/
}, {
});
}, \transcribe_all);
)

@ -1,44 +0,0 @@
{
"music_data":
[
[
[
[ [ [ "Rest" ], [ "Rest" ], [ 1, -1, 0, -1, 1, -1 ], [ "Rest" ] ], 0.625 ],
[ [ [ "Rest" ], [ 2, -2, 0, -1, 1, 0 ], [ 1, -1, 0, -1, 1, -1 ], [ "Rest" ] ], 1.625 ],
[ [ [ "Rest" ], [ 2, -2, 0, -1, 1, 0 ], [ 1, -1, 0, -1, 1, -1 ], [ 3, -1, 0, -2, 1, -1 ] ], 1.625 ],
[ [ [ "Rest" ], [ 2, -2, 0, -1, 1, 0 ], [ 1, -1, 0, -1, 1, -1 ], [ 3, -1, 0, -1, 1, -2 ] ], 11 ],
[ [ [ "Rest" ], [ 2, -2, 0, -1, 1, 0 ], [ "Rest" ], [ 3, -1, 0, -1, 1, -2 ] ], 1.375 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ 3, -1, 0, -1, 1, -2 ] ], 1.5 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 6.25 ]
]
]
],
"last_changes":
[
[ [ 1, -1, 0, -1, 1, 0 ], [ 2, -2, 0, -1, 1, 0 ], [ 1, -1, 0, -1, 1, -1 ], [ 1, 0, 0, -1, 1, 0 ] ],
[ [ 1, -1, 0, -1, 1, 0 ], [ 2, -2, 0, -1, 1, 0 ], [ 1, -1, 0, -1, 1, -1 ], [ 3, -1, 0, -2, 1, -1 ] ],
[ [ 1, -1, 0, -1, 1, 0 ], [ 2, -2, 0, -1, 1, 0 ], [ 1, -1, 0, -1, 1, -1 ], [ 3, -1, 0, -1, 1, -2 ] ]
],
"cur_uid": "4526b76b",
"ref_uid": "6fb60ab6",
"order_seed": 785868,
"dur_seed": 994115,
"motifs_seed": 805843,
"entrances_probs_vals": [ 0.34, 0, 10, 0.5, 2, 0, 0.5, 0.5, 0.5, 1, 0.5 ],
"passages_probs_vals": [ 0.34, 0, 10, 0.5, 2.12, 0, 0.5, 0.5, 0.5, 1, 0.5 ],
"exits_probs_vals": [ 0.34, 0, 10, 0.5, 2.12, 0, 0.5, 0.5, 0.5, 1, 0.5 ],
"ranges": [ [ -3600, -312 ], [ -1872, 1378 ], [ -145, 1583 ], [ -182, 1527 ] ],
"step_probs_vals": [ 0, 1200, 0, 0, 0.082304526748971, 0.99431818181818, 0.14197530864198, 0, 1, 0 ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"hd_exp": 2,
"hd_invert": 0,
"order":
[
[ [ 2, 1 ], [ 3, 3 ], [ 0 ] ]
],
"sus_weights": [ 0.75, 0.25, 0.25 ],
"order_size": [ 1, 4 ],
"passages_size": [ 0, 3 ],
"motif_edited": "false",
"order_edited": "false"
}

@ -1,85 +0,0 @@
{
"music_data":
[
[
[
[ [ [ "Rest" ], [ "Rest" ], [ 0, -1, 0, 0, 1, 0 ], [ "Rest" ] ], 1.75 ],
[ [ [ -1, -1, 0, 1, 1, 0 ], [ "Rest" ], [ 0, -1, 0, 0, 1, 0 ], [ "Rest" ] ], 0 ],
[ [ [ -1, -1, 0, 1, 1, 0 ], [ "Rest" ], [ 0, -1, 0, 0, 1, 0 ], [ 0, -1, 1, 0, 1, 0 ] ], 0 ],
[ [ [ -1, -1, 0, 1, 1, 0 ], [ -1, -1, 0, 0, 1, 1 ], [ 0, -1, 0, 0, 1, 0 ], [ 0, -1, 1, 0, 1, 0 ] ], 1.25 ],
[ [ [ 0, -1, -1, 0, 1, 0 ], [ -1, -1, 0, 0, 1, 1 ], [ 0, -1, 0, 0, 1, 0 ], [ 0, -1, 1, 0, 1, 0 ] ], 1.75 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ -1, -1, 0, 0, 1, 1 ], [ 0, -1, 0, 0, 1, 0 ], [ 0, -1, 1, 0, 1, 0 ] ], 0 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ -1, -1, 0, 0, 1, 1 ], [ 0, -1, 0, 0, 1, 0 ], [ 1, -2, 0, 0, 1, 0 ] ], 0 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ -1, -1, 0, 1, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 1, -2, 0, 0, 1, 0 ] ], 0.875 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ 1, -1, 0, -1, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 1, -2, 0, 0, 1, 0 ] ], 0 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ 1, -1, 0, -1, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 1, -1, 0, 0, 1, -1 ] ], 1.625 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ 1, -2, 0, 0, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 1, -1, 0, 0, 1, -1 ] ], 1.75 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 1, -1, 0, 0, 1, -1 ] ], 0 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 1, -1, 0, -1, 1, 0 ] ], 0.875 ]
],
[
[ [ [ -1, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ], [ -1, 0, 1, 0, 1, 0 ], [ 1, -1, 0, -1, 1, 0 ] ], 0.625 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, -1 ], [ 1, -1, 0, -1, 1, 0 ] ], 1.625 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ], [ -1, 0, 0, 0, 2, 0 ], [ 1, -1, 0, -1, 1, 0 ] ], 1.125 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ], [ 1, -1, -1, -1, 1, 0 ], [ 1, -1, 0, -1, 1, 0 ] ], 1 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ], [ 0, 0, 0, -1, 1, 0 ], [ 1, -1, 0, -1, 1, 0 ] ], 0.5 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ], [ 0, -1, 0, -1, 1, 1 ], [ 1, -1, 0, -1, 1, 0 ] ], 1.625 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 1, -1, 0, -1, 1, 0 ] ], 1.5 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, -1 ], [ 1, -1, 0, -1, 1, 0 ] ], 0.5 ]
],
[
[ [ [ 1, -2, 0, -1, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, -1 ], [ 1, -1, 0, -1, 1, 0 ] ], 0.5 ],
[ [ [ 0, 0, 0, -1, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, -1 ], [ 1, -1, 0, -1, 1, 0 ] ], 1.5 ],
[ [ [ -1, 0, 0, 1, 1, -1 ], [ 0, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, -1 ], [ 1, -1, 0, -1, 1, 0 ] ], 1.5 ],
[ [ [ 1, -1, 0, -1, 0, 0 ], [ 0, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, -1 ], [ 1, -1, 0, -1, 1, 0 ] ], 1.125 ]
],
[
[ [ [ 1, -1, 0, -1, 0, 0 ], [ 0, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, -1 ], [ "Rest" ] ], 0 ],
[ [ [ "Rest" ], [ 0, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, -1 ], [ "Rest" ] ], 0 ],
[ [ [ "Rest" ], [ 0, 0, 0, 0, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ "Rest" ] ], 1.375 ]
],
[
[ [ [ "Rest" ], [ 0, 0, 0, 0, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 1, -1, 0, -1, 1, 0 ] ], 1.75 ],
[ [ [ 0, -1, 0, -1, 2, 0 ], [ 0, 0, 0, 0, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 1, -1, 0, -1, 1, 0 ] ], 1.875 ],
[ [ [ -1, -1, 0, 0, 1, 1 ], [ 0, 0, 0, 0, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 1, -1, 0, -1, 1, 0 ] ], 0.75 ],
[ [ [ 0, -1, -1, 0, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 1, -1, 0, -1, 1, 0 ] ], 1.25 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 1, -1, 0, -1, 1, 0 ] ], 1 ],
[ [ [ 1, -2, 0, -1, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 1, -1, 0, -1, 1, 0 ] ], 1.375 ],
[ [ [ 1, -1, 0, -1, 0, 0 ], [ 0, 0, 0, 0, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 1, -1, 0, -1, 1, 0 ] ], 2 ],
[ [ [ 1, -1, 0, -1, 0, 0 ], [ "Rest" ], [ 0, -1, 0, 0, 1, 0 ], [ 1, -1, 0, -1, 1, 0 ] ], 0 ],
[ [ [ 1, -1, 0, -1, 0, 0 ], [ "Rest" ], [ "Rest" ], [ 1, -1, 0, -1, 1, 0 ] ], 4.75 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ 1, -1, 0, -1, 1, 0 ] ], 8.875 ]
]
]
],
"last_changes":
[
[ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ],
[ [ 1, -1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ],
[ [ 0, 0, -1, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ],
[ [ -1, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ],
[ [ 0, -1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ]
],
"cur_uid": "46985d14",
"ref_uid": "nil",
"order_seed": 209164,
"dur_seed": 417909,
"motifs_seed": 885208,
"entrances_probs_vals": [ 0.34, 0, 10, 0.5, 2, 0, 0.5, 0.24509803921569, 0.89189189189189, 0.5, 1, 0.5, 0.5, 0.8562091503268, 0.69932432432432, 1, 0.5 ],
"passages_probs_vals": [ 0.34, 0, 10, 0.5, 2, 0, 0.5, 0.24509803921569, 0.89189189189189, 0.5, 1, 0.5, 0.5, 0.8562091503268, 0.69932432432432, 1, 0.5 ],
"exits_probs_vals": [ 0.34, 0, 10, 0.5, 2, 0, 0.5, 0.24509803921569, 0.89189189189189, 0.5, 1, 0.5, 0.5, 0.8562091503268, 0.69932432432432, 1, 0.5 ],
"ranges": [ [ -1200, 2400 ], [ -1200, 2400 ], [ -1200, 2400 ], [ -1200, 2400 ] ],
"step_probs_vals": [ 0, 1200, 0, 0.5, 0.5, 0.5, 1, 0.5 ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"hd_exp": 2,
"hd_invert": 0,
"order":
[
[ [ 2 ], [ 0, 0, 0, 0 ], [ 1, 3 ] ]
],
"sus_weights": [ 0.75, 0.75, 0.75 ],
"order_size": [ 1, 10 ],
"passages_size": [ 0, 10 ],
"motif_edited": "true",
"order_edited": "false"
}

@ -1,50 +0,0 @@
{
{ r2. r8[ d'8^\markup { \pad-markup #0.2 "+36"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 5↑" }}] ~ }
\bar "|"
{ d'1 ~ }
\bar "|"
{ d'4 ~ d'8[ dis'8^\markup { \pad-markup #0.2 "+47"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 3↓" }}] ~ dis'4 ~ dis'16[ d'8.^\markup { \pad-markup #0.2 "+9"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 13↓" }}] ~ }
\bar "|"
{ d'1 ~ }
\bar "|"
{ d'2 cis'2^\markup { \pad-markup #0.2 "-19"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↓" }} ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'2 r2 }
\bar "|"
{ r8.[ cis'16^\markup { \pad-markup #0.2 "-19"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↓" }}] ~ cis'2. ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'1 ~ }
\bar "|"
{ cis'1}
\bar "||"
}

@ -1,50 +0,0 @@
{
{ ais1^\markup { \pad-markup #0.2 "+49"} ~ }
\bar "|"
{ ais1 ~ }
\bar "|"
{ ais1 ~ }
\bar "|"
{ ais1 ~ }
\bar "|"
{ ais2. ~ ais8.[ a16^\markup { \pad-markup #0.2 "+38"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 5↑" }}] ~ }
\bar "|"
{ a4 a2.^\markup { \pad-markup #0.2 "+11"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 13↓" }} ~ }
\bar "|"
{ a16[ b8.^\markup { \pad-markup #0.2 "+3"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 11↑" }}] ~ b4 ~ b8[ a8^\markup { \pad-markup #0.2 "-6"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 5↓" }}] ~ a4 ~ }
\bar "|"
{ a8[ gis8^\markup { \pad-markup #0.2 "-18"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 7↓" }}] ~ gis8[ a8^\markup { \pad-markup #0.2 "+21"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 13↑" }}] ~ a2 ~ }
\bar "|"
{ a8.[ ais16^\markup { \pad-markup #0.2 "+49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 3↓" }}] ~ ais2 ~ ais8.[ a16^\markup { \pad-markup #0.2 "+11"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 13↓" }}] ~ }
\bar "|"
{ a1 ~ }
\bar "|"
{ a1 ~ }
\bar "|"
{ a2 ais2^\markup { \pad-markup #0.2 "+49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 3↓" }} ~ }
\bar "|"
{ ais1 ~ }
\bar "|"
{ ais1 ~ }
\bar "|"
{ ais1 ~ }
\bar "|"
{ ais1 ~ }
\bar "|"
{ ais1 ~ }
\bar "|"
{ ais8.[ r16] r2. }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "||"
}

@ -1,50 +0,0 @@
{
{ r2. r8[ g8^\markup { \pad-markup #0.2 "-10"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 13↑" }}] ~ }
\bar "|"
{ g1 ~ }
\bar "|"
{ g4 ~ g8[ gis8^\markup { \pad-markup #0.2 "+18"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↑" }}] ~ gis4 ~ gis16[ cis'8.^\markup { \pad-markup #0.2 "-19"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↓" }}] ~ }
\bar "|"
{ cis'2 ~ cis'8[ dis'8^\markup { \pad-markup #0.2 "+47"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 3↓" }}] ~ dis'4 ~ }
\bar "|"
{ dis'2 fis'2^\markup { \pad-markup #0.2 "-49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 1↑" }} ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'8.[ r16] r2. }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "||"
}

@ -1,50 +0,0 @@
{
{ r2. r8[ gis8^\markup { \pad-markup #0.2 "+18"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↑" }}] ~ }
\bar "|"
{ gis2 g2^\markup { \pad-markup #0.2 "-37"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 5↓" }} ~ }
\bar "|"
{ g4 ~ g8[ fis8^\markup { \pad-markup #0.2 "-49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 3↑" }}] ~ fis2 ~ }
\bar "|"
{ fis1 ~ }
\bar "|"
{ fis1 ~ }
\bar "|"
{ fis1 ~ }
\bar "|"
{ fis1 ~ }
\bar "|"
{ fis1 ~ }
\bar "|"
{ fis1 ~ }
\bar "|"
{ fis8.[ fis16^\markup { \pad-markup #0.2 "-21"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 3↓" }}] ~ fis8.[ gis16^\markup { \pad-markup #0.2 "-18"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 7↓" }}] ~ gis2 ~ }
\bar "|"
{ gis8.[ g16^\markup { \pad-markup #0.2 "-20"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 7↑" }}] ~ g2 ~ g8.[ g16^\markup { \pad-markup #0.2 "+29"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 11↓" }}] ~ }
\bar "|"
{ g2 r2 }
\bar "|"
{ r1 }
\bar "|"
{ r16[ fis8.^\markup { \pad-markup #0.2 "+32"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 11↑" }}] ~ fis2. }
\bar "|"
{ g4^\markup { \pad-markup #0.2 "-10"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 13↑" }} ~ g8[ g8^\markup { \pad-markup #0.2 "-37"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "II"\normal-size-super " 5↓" }}] ~ g2 }
\bar "|"
{ fis2^\markup { \pad-markup #0.2 "-49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "III"\normal-size-super " 1↑" }} fis2^\markup { \pad-markup #0.2 "-21"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 3↓" }} ~ }
\bar "|"
{ fis8.[ g16^\markup { \pad-markup #0.2 "+29"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "I"\normal-size-super " 11↓" }}] ~ g2. ~ }
\bar "|"
{ g1 ~ }
\bar "|"
{ g1 ~ }
\bar "|"
{ g2 ~ g16[ r8.] r4 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "||"
}

@ -1,63 +0,0 @@
{
"music_data":
[
[
[
[ [ [ "Rest" ], [ -2, 0, 0, 1, 1, 0 ], [ "Rest" ], [ "Rest" ] ], 1.375 ],
[ [ [ -2, 0, 0, 1, 1, 1 ], [ -2, 0, 0, 1, 1, 0 ], [ "Rest" ], [ "Rest" ] ], 0.875 ],
[ [ [ -2, 1, 0, 1, 1, 0 ], [ -2, 0, 0, 1, 1, 0 ], [ "Rest" ], [ "Rest" ] ], 0.875 ],
[ [ [ -2, 0, 0, 1, 2, 0 ], [ -2, 0, 0, 1, 1, 0 ], [ "Rest" ], [ "Rest" ] ], 0.75 ],
[ [ [ -1, 0, 0, 1, 0, 0 ], [ -2, 0, 0, 1, 1, 0 ], [ "Rest" ], [ "Rest" ] ], 1.5 ],
[ [ [ -1, -1, 0, 1, 1, 0 ], [ -2, 0, 0, 1, 1, 0 ], [ "Rest" ], [ "Rest" ] ], 1.125 ],
[ [ [ -2, 0, 1, 1, 1, 0 ], [ -2, 0, 0, 1, 1, 0 ], [ "Rest" ], [ "Rest" ] ], 1.625 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ -2, 0, 0, 1, 1, 0 ], [ "Rest" ], [ "Rest" ] ], 1.625 ]
],
[
[ [ [ -1, 0, 0, 0, 1, 0 ], [ -2, 0, 0, 1, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ "Rest" ] ], 0 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ -2, 0, 0, 1, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ] ], 1.25 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ -1, -1, 1, 0, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ] ], 1.125 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ 0, -2, 0, 0, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ] ], 1 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ -2, 1, 0, 0, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ] ], 0.5 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ -1, 0, -1, 0, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ] ], 1.625 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ 0, -1, 0, 0, 1, -1 ], [ 0, -1, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ] ], 2 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ 0, -1, 0, -1, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ] ], 1.75 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ -1, -1, 1, 0, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ] ], 1.5 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ 0, -2, 0, 0, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ 0, 0, 0, 0, 1, 0 ] ], 1.25 ],
[ [ [ -1, 0, 0, 0, 1, 0 ], [ 0, -2, 0, 0, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ "Rest" ] ], 0.875 ],
[ [ [ "Rest" ], [ 0, -2, 0, 0, 1, 0 ], [ 0, -1, 0, 0, 1, 0 ], [ "Rest" ] ], 0 ],
[ [ [ "Rest" ], [ 0, -2, 0, 0, 1, 0 ], [ "Rest" ], [ "Rest" ] ], 0 ],
[ [ [ "Rest" ], [ "Rest" ], [ "Rest" ], [ "Rest" ] ], 1.375 ]
]
]
],
"last_changes":
[
[ [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ],
[ [ 1, -1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ],
[ [ 0, 0, -1, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ],
[ [ -1, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ],
[ [ 0, -1, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0 ] ]
],
"cur_uid": "490b1e6e",
"ref_uid": "nil",
"order_seed": 209164,
"dur_seed": 417909,
"motifs_seed": 885208,
"entrances_probs_vals": [ 0.34, 0, 10, 0.5, 2, 0, 0.5, 0.24509803921569, 0.89189189189189, 0.5, 1, 0.5, 0.5, 0.8562091503268, 0.69932432432432, 1, 0.5 ],
"passages_probs_vals": [ 0.34, 0, 10, 0.5, 2, 0, 0.5, 0.24509803921569, 0.89189189189189, 0.5, 1, 0.5, 0.5, 0.8562091503268, 0.69932432432432, 1, 0.5 ],
"exits_probs_vals": [ 0.34, 0, 10, 0.5, 2, 0, 0.5, 0.24509803921569, 0.89189189189189, 0.5, 1, 0.5, 0.5, 0.8562091503268, 0.69932432432432, 1, 0.5 ],
"ranges": [ [ -1200, 2400 ], [ -1200, 2400 ], [ -1200, 2400 ], [ -1200, 2400 ] ],
"step_probs_vals": [ 0, 1200, 0, 0.5, 0.5, 0.5, 1, 0.5 ],
"passages_weights": [ 0.75, 0.75, 0.75, 0.75, 0.75 ],
"hd_exp": 2,
"hd_invert": 0,
"order":
[
[ [ 2 ], [ 0, 0, 0, 0 ], [ 1, 3 ] ]
],
"sus_weights": [ 0.75, 0.75, 0.75 ],
"order_size": [ 1, 10 ],
"passages_size": [ 0, 10 ],
"motif_edited": "true",
"order_edited": "false"
}

@ -1,26 +0,0 @@
{
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r1 }
\bar "|"
{ r2. r8[ fis'8^\markup { \pad-markup #0.2 "-49"}_\markup { \lower #3 \pad-markup #0.2 \concat{ "IV"\normal-size-super " 1↑" }}] ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'1 ~ }
\bar "|"
{ fis'2. ~ fis'8[ r8] }
\bar "|"
{ r1 }
\bar "||"
}

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save