I have the jaxitron book and have slowly been adapting the APL code to J.

Here’s what I have so far. It’s such an old book and you would need to read the 
book to use the code the way it was intended. 

NB. routines for cybernetic music. This is a direct translation 
NB. from APL. It should be reworked and put in tacit form where 
NB. possible.

require 'files'

NB. Global variables
TONSYS =: 12
NOTES =: i. TONSYS
PIANO =: 9 + i.88
NOTENAME =: 'C  C# D  D# E  F  F# G  G# A  Bb B  '
FORS =: 'b#'
NTS =: 'CDEFGAB'
NUM =: 0 2 4 5 7 9 11

CLSNDX =: 0 0 1 1 1 2 2 3 3 3 3 4 4 4 5 5 5 6 6 6 6 6 7 7 7 8 8 8 8 9 9 9 10 11 
11 11
CLSNM =: 12 6$'-MI7  ','-MA7  ','MI7   ','MI    ','+MI   ','LG7   ','MA7   
','+7    ','+MA7  ','7+3   ','MA7+3 ','+MA7+3'

NB. Formatting functions
   fmt =: 8!:0
   fmt1=: 8!:1
   fmt2=: 8!:2

NB. some music theory
NB.     Intervals               Names
NB. 0   perfect unison          (diminished second)
NB. 1   minor second            (augmented unison)
NB. 2   major second            (diminished third)
NB. 3   minor third             (augmented second)
NB. 4   major third             (diminished fourth)
NB. 5   perfect fourth          (augmented third)
NB. 6   augmented fourth or diminished fifth
NB. 7   perfect fifth           (diminished sixth)
NB. 8   minor sixth             (augmented fifth)
NB. 9   major sixth             (diminished seventh)
NB. 10  minor seventh           (augmented sixth)
NB. 11  major seventh           (diminished octave)
NB. 12  perfect octave          (augmented seventh)

   int =: 3 : 0
(1 }. y) - _1 }. y
)

NB. Found in Chapter 7 on page 54 and transcribed here
NB. resultant creates the Schillinger interference pattern for 
NB. rhythm
resultant =: 3 : 0
z =. (($y),*/y)$0
for_i. y do.
z =. ((*/y)$1,(_1+i)$0) i_index} z
end.
r=. int (+/\ +. /z)i.1 + i. 1 + +/ +./ z
attk =. +/z
r,: (I. (0 ~: attk)) { attk
)

NB. Rhythm function missing page 56

NB. scli - allows the composer think in terms of single
NB. sets of intervals as opposed to sclit
scli =: 3 : 0
j =: (($y)*>.(5*TONSYS)%+/y)$y
scl =: (|. +/\0, -|. j), 1}.+/\0,j
scl i. 0
)

showscl =: 4 : 0
mn =. x * TONSYS
mx =. y * TONSYS
((scl >: mn) *. scl <: mx)# scl
)

brk =: 4 : 0
j =. (0 ~: x)#,x
k =. +/j
z =. ((<.y%k)*$,x)$ x
r =. k | y
L0 =. 1
while. L0 do.
k =. ((+/\j)<:r)#j
r =. r - +/k
z =. z,k
if. r = 0 do. z return. end.
if. (r = 0 { j)+. r = 1 do.
  L0 =. 0
  continue.
end.
j =. }. j
if. 0 < $j do.
  continue.
end.
L0 =. 0
end.
r =. r$1
z,r
)

NB. sclit - aloows the composer to create related patterns in 
NB. different scales
sclit =: 4 : 0
j =. y brk x
scli j
)

NB. symscl - 
symscl =: 4 : 0
z =: +/\0,(_1 + >./TONSYS frctn (TONSYS|x) %TONSYS)$x
,+/\"1 z,.(($z),$y)$y
)

NB. frctn - 
NB. frctn =: 4 : 0
NB. i =. 1
NB. whilest. x >: i do.
NB. a =. (i$0), i%(i-1) }. 1+ i. x
NB. z =. a i. y
NB. if. z <: x do.
NB. break.
NB. end.
NB. end.
NB. i,z
NB. )
frctn =: 4 : 0
2 x: y
)

NB. scale =: 0 2 4 5 7 9 11
NB. we can extract chords from this scale by the following
NB. (7|(i.7)  +/ 2 * i.3) { scale
NB. this will create the seven chords of the given scale
NB. more general if 
NB. N is the size of the scale
NB. E is expand the chord by E number of tones
NB. S is the number of notes in the chords
NB. xpnd - expand scale into its chords
xpnd =: 4 : 0
N =. #x
'E S' =. y
z =. (N|(i. N) +/ E * i.S) { x
)
   NB. SIGMAS are just all combinations of intervals which have
   NB. distinct notes
   NB.    12|0,+/\3 3 3  which maps 4 notes interval 3 apart 
   NB. into 12 note scale has the following ordinal note numbers
   NB. 0 3 6 9
   NB. However 12|0,+/\4 4 4 will loop back around and repeat the
   NB. same note
   NB. 0 4 8 0
   NB. The goal therefore seeems to be to go through all combinations
   NB. of intervals taken together and output a set of unique
   NB. note sequences. The choice of too many intervals increases
   NB. the search space dramatically but for 2 or 3 intervals it
   NB. is quite doable

NB. sigmas support function rpts finds duplicate ordinal tone seq
rpts =: #/.~

NB. i2np will map all notes into a single octave
i2np =: 3 : 'TONSYS|+/\0,y'

jsigmas =: 4 : 0
cnotes =. x
NB. interverals =. cnotes - 1
NB. intervals =. ((#y) #.^:_1 (i. (#y)^cnotes-1))){y
intervalsel =. (#y) #.^:_1 (i. (#y)^cnotes-1)
intervals =. intervalsel { y
allchords =. TONSYS| +/\"1 (0,. intervals) 
selection =. (1 =>./"1 rpts"1 allchords)
(selection#intervals);(selection#allchords);n2pno (selection#allchords)
)
 
sigmaout =: '/Users/tmcguire/musicXML/sigma.out'
sigmas =: 4 : 0
S =. x
A =. |: 0 1$y
for_j. i. 2 + TONSYS - 2 do.
Z =: (2 1 0 |:(1,(1{.$A),$y)$y),(($y),$A)$A
Z =: (, $~ | -.~ $)Z
k =: TONSYS|+/\Z
K =: (TONSYS*k=0)+k
NB. recalculate Z in sonme obtuse way
NB. z=:(+./"0 1>:+/(1+i. TONSYS)=/k)#"0 Z
z =: (+./"0 1>:+/(1+i. TONSYS)=/K)#"0 Z
if. -. j e. S do.
  continue.
end.
NB. This is a chord of interest so prepare results
(40$'*') fappends sigmaout
((": 1 {. $ z),' SIGMA ',(": j),'''S') fappends sigmaout
(":z) fappends sigmaout

if. j >: >./S do.
  return.
end.

NB. Set A to z and recalculate for next interval
A =. z
end.
)

NB. CHORD CONSTRUCTION ROUTINES
NB. support structures
HS =: 4 2$4 3 3 4 4 4 5 5
PREF =: 3 3$ 0 1 _4 1 1 _4 1 0 2
VS =: 3 3$ 1 2 0 2 0 1 2 1 0

NB. shcon
shcon =: 4 : 0
'IRC STR' =. y
S0 =. 0 { STR
RT =. 0 { IRC
i =. 1
Z =. +/\ RT,S0 { HS
Z =. (1,$Z)$Z
SN =. i { STR
cyc =. {. (+./((i.2){"1 PREF))=(({. $PREF),2)$ S0,SN)#i. {.$PREF)
)

NB. cshcon - construct cords based on cycle vector that specifies
NB.          intervals between successive root tones
cshcon =: 4 : 0
k =. */($,y),$,x
z=. i2np (k$y) { HS
n2pno z+|:(|.$z)$ }. + /\0,k$x
)

NB. n2p - convert numeric representation of notes to letter form
NB. n2p =: 3 : 0
NB. n=: (_2 {. 1, 1, $y)$y
NB. kn =: TONSYS|n
NB. kd =: <. n%TONSYS
NB. o =: ((_1}.$n),1){.kd
NB. kd =: kd -(($kd)$(,o), ,0 _1 }. kd)
NB. kf =: kn -/ NUM
NB. kf =: (kf*(|kf)=(<./|kf) */ (#NUM)$1),TONSYS
NB. kf =: (1 = +/\ +/\kf ~: 0)*kf
NB. kf =: (_1 {. $kf)$(,kf~:0)#,kf
NB. kf =: (kf ~: TONSYS)#,kf
NB. kn =: (NUM i. kn - kf) { NTS
NB. flds =: 4 +>./,|kf
NB. orho =: ~1 {.$kn
NB. kn =: ((~1}.$kn),orho*flds)$(,kn),(($,kn),~1+flds)$' '
NB. )

NB. N2P - in J using boxing to generate and display pitch names
NB.       this makes it much easier to manipulate than the APL
NB.       routines originally developed
n2p =: 3 : 0
NB. Octave would be the relative octave away from main octave
NB. The octave is found by dividing by the # of tones in an octave
NB. ie. divide by TONSYS and then using floor <. to produce the 
NB. integer truncation. Then use format ": to turn it all into
NB. Text
octave =. ": each ;/ <. y%TONSYS
NB. The following will map a note to it's text pitch name
pitch =. (TONSYS|y) { _3 <\NOTENAME
(('0';' ')&stringreplace each octave),:pitch
)

n2pno =: 3 : '(1 3*$y)$,(TONSYS|y) { (TONSYS,3) $ NOTENAME'

NB. What would be nice would be an XML language that could be 
NB. loaded into a browser to display output as a musical score

NB. Pitch to musicXML
NB. Given pitch name just do key of C translation to XML for now
p2xml =: 3 : 0
   fileout =. '/Users/tmcguire/musicXML/musicout.xml'
   mxmlhead =.  freads '/Users/tmcguire/musicXML/basemusic.xml'
   mxmlattr =.  freads '/Users/tmcguire/musicXML/measure.xml'
   mxmlendmeasure =.  freads '/Users/tmcguire/musicXML/endmeasure.xml'
   mxmlendmusic =.  freads '/Users/tmcguire/musicXML/endmusic.xml'
   mxmlhead fwrites fileout
   for_i. |:y do.
     'o p' =. i
     ('    <measure number="',(": i_index + 1),'">') fappends fileout
if. i_index = 0 do.   mxmlattr fappend fileout end.
        ('      <note>',LF,'       <pitch>') fappends fileout
          ('      <step>',p,'</step>') fappends fileout
   mxmlendmeasure fappends fileout
   end.
   mxmlendmusic fappends fileout
)

NB. melody routines
NB. melody support routines
expansion =: #^:_1
TMP =: 4 : 0
k=. >./($,y),|x
(k$y)+k$,x{.M
)

BGN =: 4 : 0
(x - 0{x) + {. y
)

   CDNC212 =: 4 : 0
k =. (2+y)$ 2 1 2
+/\(x- +/k),k
)

DVLP2 =: 4 : 0
M=: P=. (1+2*$,y)$x
P =. (1 TMP y) (1+2*i. $,y)} P
M =: P,P
M =: M, _1 TMP 1+i. $,y
M =: M,M BGN _1 TMP 1
P =. (P BGN _1 TMP 1) + (($P)$0 1) expansion -1+i. $, y
M =: M,P
M =: M,(_2{.P),((0{P)+ 3 1 _3 _2),(0{P)
P =. (0{P) CDNC212 $,y
M =: M,P,P,P
M =: M,(1 + _1{.P),_1 + ($,y){.P
)

> On Mar 30, 2020, at 3:44 AM, HH PackRat <hhpack...@gmail.com> wrote:
> 
> On 3/28/20, Devon McCormick <devon...@gmail.com> wrote:
>> has anyone done work with generating music or musical phrases in J?  I'm
>> helping someone develop a music training game that starts by generating
>> sounds with a particular key, tempo, and phrase length. ...
>> I've told my collaborator that I think the generation part should be
>> relatively straightforward but I am not schooled in music, so I'm not sure
>> how to start.
>> Does anyone have any ideas about this?
> 
> You might try checking around libraries, online used book sources,
> etc., for a book titled "Cybernetic Music" by Jaxitron [i.e., Jack
> Citron] (TAB Books, 1985) (344 p.).  [Many of the ideas in the book
> are more or less based on "The Schillinger System of Musical
> Composition" by Joseph Schillinger (1946), which applies mathematics
> to music, especially background music for radio, films, and such.]
> The blurb on the rear cover of the paperback (the book was originally
> available in both hardcover and paperback) states:
> 
> __________________________________________________
> 
> The focus here is on using the principles of artificial intelligence
> modeling and expert systems to help the musician compose!  This where
> you'll find out how to use your  computer to keep track of musical
> decisions that you've already made . . . to project possible outcomes
> of different musical combinations . . . to generate possible harmonies
> for a given melody (or vice versa) . . . or to perform such
> time-consuming chores as transposing and orchestrating.  And what the
> computer does is all within the parameters that *you* have
> established!
> 
> The basis of this computerized music generation system is the concept
> that rhythmic, harmonic, and melodic elements of music--even music
> idiom, style, and form--can be quantified and described in
> mathematical terms as vectors, arrays, and matrices.  These elements
> can then be manipulated and processed in any way the composer wishes
> using the powerful functions of the APL programming language.  Because
> APL has the ability to express in a single statement or operation,
> ideas that would require multiples lines of code in other languages,
> it lends itself particularly well to the necessities of creative
> musical composition.
> __________________________________________________
> 
> 
> Since one of my master's degrees is in music (emphasizing theory and
> composition), I bought the book because I was a computer programming
> hobbyist (since 1975) with an interest in "marrying" music and
> computers and because I owned all of Schillinger's books.  I didn't
> know APL (and still don't), and it was only in 2006 that a coworker at
> the library I worked for happened to tell me about J.  That interest
> grew to the point where, by 3 years later (when I retired), I gave
> presentations at library conferences on using J, hoping to instill
> interest in others for J.  Anyway, I had hoped to "translate" the APL
> code in Jaxitron's book into J, but that has been an idea that has not
> yet borne fruit.  I hope I live long enough yet to make an attempt.
> On the other hand, maybe somebody in our group is knowledgeable enough
> in both APL and J to carry through on such a task.
> 
> Another resource for *ideas* is David Cope (see Google or Amazon), who
> has written several books on musical composition by computers,
> including many code examples.  Unfortunately, he uses Lisp, which
> probably would be very difficult to translate to J.  His programs can
> learn musical style and then create music that can sometimes be very
> similar to well known classical composers (as demonstrated in
> published scores by his software and musical recordings of some of
> them).  Some of his (expensive!) book titles are:
> 
> Experiments in Musical Intelligence (1996)
> The Algorithmic Composer (2000)
> Virtual Music: Computer Synthesis of Musical Style (2004)
> Computer Models of Musical Creativity (2005)
> Hidden Structure: Music Analysis Using Computers (2009)
> 
> Lately, he has been publishing computer generated novels and musical
> scores of various kinds generated by computer.
> 
> Hope this helps with ideas you can follow up on!
> 
> Harvey
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to