go back

##############################
####### Do a music with R base
##############################

1. A bit of theory regarding sounds goto
	1.1. A sound is a wave is a vector of number
	1.2. Range variation
	1.3. Shape variation
	1.4. Pitch variation
	1.5. Chords
2. From vector of numbers to audio file goto
	2.1. From a vector of numbers to an audio file
	2.2. A function to transform numeric vector into wav file
	2.3. First example
	2.4. Second example
3. A function to compose music with R goto
	3.1. A function to compose music
	3.2. Canon in D from Johann Pachelbel
	3.3. The entertainer from Scott Joplin

In this article, I use R base to produce a musical application. Ok, I use also the package txtplot,
but it's just for the nice ASCII plots! You can use the basic function plot instead. Here is a
teaser of what I can compose with my R function. Okay, there are some wrong notes but my goal was
to have a nice defy not to compose music!


1. A bit of theory regarding sounds gotop

1.1. A sound is a wave is a vector of number

A sound is a wave spreading across air molecules. It can be represented by an ondulating curve.

x=seq(0,6*2*pi,l=600)
y=sin(x)   
txtplot::txtplot(x,y,w=100)
	
     +---+----------------------+---------------------+----------------------+---------------------+
   1 +      **            ***            **            **            **            **              +
     |      * *           * *           ***           ****          ****           * *             |
     |     ** *          ** *           * **          *  *          *  *          ** *             |
     |     *  **         *   *         **  *          *  *          *  *          *   *            |
     |     *   *         *   *         *   *         **  **         *   *         *   *            |
 0.5 +    **   *         *   *         *   **        *    *        *    *         *   *            +
     |    *    *        *    **        *    *        *    *        *    *        *    *            |
     |    *     *       *     *       *     *        *    *        *    *        *     *           |
     |    *     *       *     *       *     *       **    **       *     *       *     *           |
     |    *     *       *     *       *     **      *      *      *      *       *     *           |
     |   *      *      **     *       *      *      *      *      *      *      *      *           |
   0 +   *      **     *       *      *      *      *      *      *      *      *       *     **   +
     |           *     *       *     *       *      *      *      *      **     *       *     *    |
     |           *     *       *     *       *     *        *    **       *     *       *     *    |
     |           *     *       *     *        *    *        *    *        *    **       *     *    |
     |           *    **       **    *        *    *        *    *        *    *        *     *    |
-0.5 +            *   *         *   *         *    *        *    *        *    *         *   *     +
     |            *   *         *   *         *   *          *  **         *   *         *   *     |
     |            *   *         *   *          *  *          *  *          *  **         *   *     |
     |            ** **          * **          *  *          *  *          *  *          ** **     |
     |             * *           * *           ****          ****           ***           * *      |
  -1 +             ***           ***            **            **            **            ***      +
     +---+----------------------+---------------------+----------------------+---------------------+
         0                     10                    20                     30                      

There are 3 components we are interested in:
* The range which determines the loudness.
* The shape which determines the timbre. For instance, with certain shapes you can reproduce parole.
* The frequence which determines the pitch. For instance, 440 ondulations by second gives the A note.

1.2. Range variation

y=sin(x)*seq(1,0,l=600)
txtplot::txtplot(x,y,w=100)
		
     +---+----------------------+---------------------+----------------------+---------------------+
   1 +      **                                                                                     +
     |      ***                                                                                    |
     |     ** *           ***                                                                      |
     |     *  **          * *                                                                      |
     |     *   *         *  **          ***                                                        |
 0.5 +     *   *         *   *          * **                                                       +
     |    *    *         *   *         **  *          ****                                         |
     |    *    **       *    **        *   *          *  *                                         |
     |    *     *       *     *        *    *        *   **         ****                           |
     |    *     *       *     *       *     *        *    *        **  **                          |
     |   **     *       *     *       *     **      *     **      **    **       ******            |
   0 +   *      *      *      **      *      *      *      *      *      **     **    **** *****   +
     |           *     *       *     *       *     **      **    **       *    **        ***       |
     |           *     *       *     *       **    *        *    *         ****                    |
     |           *     *       *     *        *    *        **  *                                  |
     |           *     *        *   **        *   *          ****                                  |
     |            *   *         *   *          * **                                                |
-0.5 +            *   *         **  *          ***                                                 +
     |            *   *          * *                                                               |
     |            ** *           ***                                                               |
     |             * *                                                                             |
     |             ***                                                                             |
     +---+----------------------+---------------------+----------------------+---------------------+
         0                     10                    20                     30                      
	
1.3. Shape variation

y=sin(x)
y2=sign(y)
y3=(x+0.5*pi-round((x+0.5*pi)/(2*pi))*2*pi)*sign(x+0.5*pi-round((x+0.5*pi)/(2*pi))*2*pi)*2/pi-1
y4=c(y[1:200],y2[201:400],y3[401:600])
txtplot::txtplot(x,y4,pch=c(rep("*",200),rep("o",200),rep("+",200)),w=100)

     +---+----------------------+---------------------+----------------------+---------------------+
   1 +      **            ***         oooooooo      oooooooo          +             +              +
     |      * *           * *                                        ++            ++              |
     |     ** *          ** *                                        ++            +++             |
     |     *  **         *   *                                       + +           + +             |
     |     *   *         *   *                                      +  +          ++ +             |
 0.5 +    **   *         *   *                                      +  +          +  ++            +
     |    *    *        *    **                                    ++   +         +   +            |
     |    *     *       *     *                                    +    +        ++   +            |
     |    *     *       *     *                                    +    +        +    ++           |
     |    *     *       *     *                                   ++     +       +     +           |
     |   *      *      **     *                                   +      +      ++     +           |
   0 +   *      **     *       *      *                                  +      +       +     ++   +
     |           *     *       *     *                                    +     +       +     +    |
     |           *     *       *     *                                    +    ++       +     +    |
     |           *     *       *     *                                    +    +         +   +     |
     |           *    **       **    *                                     +   +         +   +     |
-0.5 +            *   *         *   *                                      +  ++         +   +     +
     |            *   *         *   *                                      +  +           + +      |
     |            *   *         *   *                                       + +           + +      |
     |            ** **          * **                                       +++           + +      |
     |             * *           * *                                        ++             +       |
  -1 +             ***           ***         oooooooo      oooooooo          +             +       +
     +---+----------------------+---------------------+----------------------+---------------------+
         0                     10                    20                     30                      
	
1.4. Pitch variation

So 440 ondulations by second (440 Hz) gives the A note. 440*2^(1/12) Hz gives the A# note,
440*2^(2/12) Hz gives the B note... Let's say our sound player read 44 100 dots in one second, a
common sampling frequency, here is the code to go from A to its octave in 10ms, so in 441 dots.

x=0
for (i in 1:440){
	x=c(x,x[length(x)]+440*2^(i/500)*2*pi/44100)
}
y=sin(x)
txtplot::txtplot(1:441,y,w=100)
			
     +---+------------------+------------------+-------------------+------------------+------------+
   1 +       ***               **             **            **           **          *             +
     |      ** *              * **            * *           ***         ***         * *            |
     |      *   *             *  *           *  *          *  *         * **        * *            |
     |      *   *            *   *           *   *         *  *         *  *        * *            |
     |     *     *           *    *          *   *         *   *        *  *       *  *            |
 0.5 +     *     *           *    *         *    *        **   *       *   *       *   *           +
     |     *     *          *     *         *    *        *    *       *    *      *   *           |
     |    *      **         *      *        *     *       *    *       *    *      *   *      *    |
     |    *       *         *      *        *     *       *    *       *    *      *   *     *     |
     |    *       *         *      *        *     *       *     *      *    *     *    *     *     |
     |   *        *        *       *       *      *       *     *     *     *     *     *          |
   0 +   *         *       *       *       *      **     *      *     *      *    *     *    *     +
     |             *       *        *      *       *     *      *     *      *    *     *    *     |
     |             *      **        *      *       *     *      *     *           *          *     |
     |             **     *         *     *        *     *       *    *      *   *      *   *      |
     |              *     *         **    *        *    *        *   *       *   *      *   *      |
-0.5 +              *     *          *    *         *   *        *   *       *   *       *  *      +
     |              **   **          *   **         *   *        *   *        *  *       *  *      |
     |               *   *           *   *          *   *         *  *        *  *       *  *      |
     |               *   *            *  *           * *          * *         * **       * *       |
     |               ** *             ***            * *          ***         ***         **       |
  -1 +                ***              **            **            **          **         **       +
     +---+------------------+------------------+-------------------+------------------+------------+
         0                 100                200                 300                400            

1.5. Chords

Different sounds can be mixed easily. The ears simply adds all the sonor waves they receive. We can
do the same with R. Here is a C chord which lasts 10ms.

C=sin(seq(0,440*2^(3/12)*2*pi,l=44100))
E=sin(seq(0,440*2^(7/12)*2*pi,l=44100))
G=sin(seq(0,440*2^(9/12)*2*pi,l=44100))
CEG=(C+E+G)/3
txtplot::txtplot(1:441,CEG[1:441])

     +---+------------------+------------------+-------------------+------------------+------------+
   1 +      **                                                                                     +
     |      **                                                                                     |
     |     *  *                                                                                    |
     |     *  *                                                                                    |
     |     *  *                                                               **           **      |
     |    **  *                                                              ****         ** *     |
 0.5 +    *    *        ***                   ***                            *  *         *  *     +
     |    *    *        * **                  * **                          **  **        *   *    |
     |    *    *       *   *                 *   *          ****            *    *       **        |
     |    *    *       *   *       **        *   **        *   **           *    *       *         |
     |   *      *      *    *     ****      *     *       **     **        *     *       *         |
     |          *     *     *    **  **     *     *       *       *        *      *      *         |
   0 +   *      *     *      *   *    *     *      *     **        *       *      *     *          +
     |          *     *      ** **    **   *       *     *         **     *       *     *          |
     |          *     *       ***      *   *       **   **          **    *       *     *          |
     |           *   *                  * *         *   *            **  *         *    *          |
     |           *   *                  **          ** **             ****         *   *           |
-0.5 +           *   *                               ***               **          *   *           +
     |           *   *                                                              *  *           |
     |           ** *                                                               * *            |
     |            * *                                                               ***            |
     |            **                                                                               |
     +---+------------------+------------------+-------------------+------------------+------------+
         0                 100                200                 300                400            

2. From vector of numbers to audio file gotop

2.1. From a vector of numbers to an audio file

Now, let's create an audio file. Let's compose a music with a C, an E and a G note and then
our C chord, each during 1s. The range variation allows us to delimite the differente notes.

SampleRate=44100
C=sin(seq(0,440*2^(3/12)*2*pi,l = SampleRate))
E=sin(seq(0,440*2^(7/12)*2*pi,l = SampleRate))
G=sin(seq(0,440*2^(10/12)*2*pi,l = SampleRate))
CEG=(C+E+G)/3
music=c(C,E,G,CEG)*rep(seq(1,0,l=SampleRate),4)
txtplot::txtplot(music,w=100)

     +---+-----------------------+------------------------+-----------------------+----------------+
   1 +   **                    *                    **                   *                         +
     |   ****                  ***                  ****                 ***                       |
     |   ******                *****                ******               *****                     |
     |   ********              ********             ********             *******                   |
     |   **********            **********           **********           *********                 |
 0.5 +   ************          ************         ************         ***********               +
     |   **************        **************       **************       **************            |
     |   ****************      ****************     ****************     ****************          |
     |   ******************    ******************   ******************   ******************        |
     |   ********************  ******************** ******************** ********************      |
     |   **************************************************************************************    |
   0 +   ***************************************************************************************   +
     |   ********************* ******************** ******************** ********************      |
     |   *******************   ******************   ******************   ******************        |
     |   ****************      ****************     ****************     ***************           |
     |   **************        **************       **************       **************            |
-0.5 +   ************          ************         ************         ************              +
     |   **********            **********           **********           **********                |
     |   ********              ********             ********             ********                  |
     |   ******                ******               ******               *****                     |
     |   ****                  ****                 ****                  ***                      |
  -1 +   **                    **                   **                                             +
     +---+-----------------------+------------------------+-----------------------+----------------+
         0                     50000                    1e+05                  1.5e+05              

We need to convert it in 8 bits PCM music. With 8 bits you can create 256 different number. So we
need to simplify our data to have only 256 different values. The we can convert it to raw data.

music.8b=ceiling((music+1)*128)-1
paste(
	"music.8b check => min:",min(music.8b),
	", max:",max(music.8b),
	", distinct values:",length(unique(music.8b))
)

	+--------------------------------------------------------------+
	|  music.8b check => min: 0 , max: 255 , distinct values: 256  |
	+--------------------------------------------------------------+

music.bin=as.raw(as.hexmode(music.8b))
	
We can already export it in raw format. But sound player won't be able to know the format used. The
format used is PCM, with a sample rate of 44 100 Hz, a number of bits per sample of 8, a number of
channels of 1. In some player you won't be able to play it and in others you will have to specify
manually the format. So it is better to use an easier to read format. I use the wav format.

To transform your raw file in a wav format you just have to add a header of 44 bytes indicating the
format of the sound file.

SampleRate=44100
BitsPerSample=8
NumberChannels=1
MusicSize=length(music.bin)
wav.header=c(
	charToRaw("RIFF"),
	packBits(x=intToBits(MusicSize+36),type = "raw"),
	charToRaw("WAVEfmt "),
	packBits(x=intToBits(16),type = "raw"),
	as.raw(c("01","00")),
	packBits(intToBits(NumberChannels),type="raw")[1:2],
	packBits(x=intToBits(SampleRate),type = "raw"),
	packBits(x=intToBits((SampleRate*BitsPerSample*NumberChannels)/8),type = "raw"),
	packBits(intToBits((BitsPerSample*NumberChannels)/8),type="raw")[1:2],
	packBits(intToBits(BitsPerSample),type="raw")[1:2],
	charToRaw("data"),
	packBits(x=intToBits(MusicSize),type = "raw")
)
music.bin.wav=c(wav.header,music.bin)

You can now export it and listen to it in most audio player!

writeBin(object =music.bin.wav ,con = "music.wav",useBytes = T)


2.2. A function to transform numeric vector into wav file

Now let's try a few sound. I have built a function to transform numeric vector into wav file.

NumToWav=function(music,OutFile,SampleRate=44100){
	BitsPerSample=8
	NumberChannels=1
	
	music.8b=ceiling((music+1)*((2^BitsPerSample-1)/2))
	music.bin=as.raw(as.hexmode(music.8b))
	
	MusicSize=length(music.bin)
	wav.header=c(
		charToRaw("RIFF"),
		packBits(x=intToBits(MusicSize+36),type = "raw"),
		charToRaw("WAVEfmt "),
		packBits(x=intToBits(16),type = "raw"),
		as.raw(c("01","00")),
		packBits(intToBits(NumberChannels),type="raw")[1:2],
		packBits(x=intToBits(SampleRate),type = "raw"),
		packBits(x=intToBits((SampleRate*BitsPerSample*NumberChannels)/8),type = "raw"),
		packBits(intToBits((BitsPerSample*NumberChannels)/8),type="raw")[1:2],
		packBits(intToBits(BitsPerSample),type="raw")[1:2],
		charToRaw("data"),
		packBits(x=intToBits(MusicSize),type = "raw")
	)
	
	music.bin.wav=c(wav.header,music.bin)
	writeBin(object =music.bin.wav ,con = OutFile,useBytes = T)
}

2.3. First example
	
A trial with pitch variation with a sample rate of 4000:

x1=0;x2=0;x3=0;x1r=0;x2r=0;x3r=0;
for (i in c(1:8000,8000:1)){
	x1=c(x1,x1[length(x1)]+220*2^(i/4000)*2*pi/4000)
	x2=c(x2,x2[length(x2)]+277.1826*2^(i/4000)*2*pi/4000)
	x3=c(x3,x3[length(x3)]+329.6276*2^(i/4000)*2*pi/4000)
	
	x1r=c(x1r,x1r[length(x1r)]+220*2^(floor(12*i/4000)/12)*2*pi/4000)
	x2r=c(x2r,x2r[length(x2r)]+277.1826*2^(floor(12*i/4000)/12)*2*pi/4000)
	x3r=c(x3r,x3r[length(x3r)]+329.6276*2^(floor(12*i/4000)/12)*2*pi/4000)
}
x1=sin(x1[-1]);x2=sin(x2[-1]);x3=sin(x3[-1]);x1r=sin(x1r[-1]);x2r=sin(x2r[-1]);x3r=sin(x3r[-1])
y=c(x1,(x1+x2)/2,(x1+x2+x3)/3,x1r,(x1r+x2r)/2,(x1r+x2r+x3r)/3)
NumToWav(y,"music2.wav",SampleRate = 4000)


2.4. Second example

A trial with shapes and accent variation. First I define a variable "accent" that will split the
sound in different note every second and give a specific accent to each note. I use a chi2
distribution function to have a note which start quickly and with a bit of reasonance.

accent={v1=dchisq(seq(0,10,l=44100),2.5);v1=v1-min(v1);v1/max(v1)}
txtplot::txtplot(accent,w=100)

    +---+------------------+------------------+-------------------+------------------+-------------+
  1 +      ****                                                                                    +
    |     **  ***                                                                                  |
    |     *     ***                                                                                |
    |    **       ***                                                                              |
0.8 +    *          **                                                                             +
    |    *           ***                                                                           |
    |    *             **                                                                          |
    |    *              ***                                                                        |
0.6 +   **                ***                                                                      +
    |   *                   **                                                                     |
    |   *                    ***                                                                   |
    |   *                      ***                                                                 |
    |   *                        ****                                                              |
0.4 +   *                           ***                                                            +
    |   *                             ****                                                         |
    |   *                                ****                                                      |
    |   *                                   *****                                                  |
0.2 +   *                                       ******                                             +
    |                                                *******                                       |
    |                                                      ***********                             |
    |                                                                *********************         |
  0 +   *                                                                                *****     +
    +---+------------------+------------------+-------------------+------------------+-------------+
        0                10000              20000               30000              40000            

music=sin(seq(0,440*2*pi*3,l=44100*3))*rep(accent,3)
txtplot::txtplot(music,w=100)

     +---+-------------------------------+------------------------------+--------------------------+
   1 +    **                          **                          **                               +
     |    ****                       ****                        ****                              |
     |   ******                      *****                       *****                             |
     |   *******                     ******                      ******                            |
     |   ********                    ********                    *******                           |
 0.5 +   *********                   *********                   *********                         +
     |   **********                  **********                  **********                        |
     |   ************                ************                ************                      |
     |   ***************             ***************             **************                    |
     |   *******************         *******************         *******************               |
     |   *************************************************************************************     |
   0 +   *************************************************************************************     +
     |   *******************         *******************         *******************               |
     |   ***************             ***************             ***************                   |
     |   ************                ************                ************                      |
     |   ***********                 **********                  **********                        |
-0.5 +   *********                   *********                   *********                         +
     |   ********                    ********                    *******                           |
     |   *******                     *******                     ******                            |
     |   ******                      *****                       *****                             |
     |    ****                       ****                        ****                              |
  -1 +    ***                         **                          **                               +
     +---+-------------------------------+------------------------------+--------------------------+
         0                             50000                          1e+05                         

NumToWav(music,"music3.wav")


Now let's try different timbre:

x=seq(0,440*4*pi,l=88200)
music=c(
	sin(x)^7,
	sin(x),
	sign(sin(x))*abs(sin(x))^0.5,
	sign(sin(x)),
	runif(length(x),-1,1)
)*rep(accent,10)
NumToWav(music,"music4.wav")


3. A function to compose music with R gotop

3.1. A function to compose music

Ok now let's try to do a nice music! I want a function with the following characteristics: Each
melody is a dataframe with a column for the pitch of the note (the A 440Hz is represented by 36,
the next A# is represented by 37...), a column for the duration of the note (1 for 1s, 0.5 for
0.5s...) and a column for the volume of the note (max=1,mute=0). A list of the differents melodies
is used as the input of the function. Then the function compiles a wav audio sound with the music.

I build the following function.

CreateMusic=function(Partition,SampleRate,OutFile){
	NumToWav=function(Music,OutFile,SampleRate,BitsPerSample=8,NumberChannels=1){
		music.8b=ceiling((Music+1)*((2^BitsPerSample-1)/2))
		music.bin=as.raw(as.hexmode(music.8b))
		MusicSize=length(music.bin)
		wav.header=c(
			charToRaw("RIFF"),
			packBits(x=intToBits(MusicSize+36),type = "raw"),
			charToRaw("WAVEfmt "),
			packBits(x=intToBits(16),type = "raw"),
			as.raw(c("01","00")),
			packBits(intToBits(NumberChannels),type="raw")[1:2],
			packBits(x=intToBits(SampleRate),type = "raw"),
			packBits(x=intToBits((SampleRate*BitsPerSample*NumberChannels)/8),type = "raw"),
			packBits(intToBits((BitsPerSample*NumberChannels)/8),type="raw")[1:2],
			packBits(intToBits(BitsPerSample),type="raw")[1:2],
			charToRaw("data"),
			packBits(x=intToBits(MusicSize),type = "raw")
		)
		music.bin.wav=c(wav.header,music.bin)
		writeBin(object =music.bin.wav ,con = OutFile,useBytes = T)
	}
	accent=function(Duration,SampleRate){
		v1=dchisq(seq(0,10,l=Duration*SampleRate),2.5)
		v1=v1-min(v1)
		v1/max(v1)
	}
	sound=function(Note,Duration,Volume,SampleRate){
		sin(seq(0,2*pi*55*2^(Note/12)*Duration,l=SampleRate*Duration))*
			accent(Duration,SampleRate)*
			rep(Volume,SampleRate*Duration)
	}
	CumSound=function(v,i){
		if(i>=1){
			c(CumSound(v,i-1),
				sound(Partition[[v]][i,"Note"],
					Partition[[v]][i,"Duration"],
					Partition[[v]][i,"Volume"],
					SampleRate))
		}
	}
	CumVoice=function(j){
		if(j>=1){CumVoice(j-1)+CumSound(j,length(Partition[[j]][,1]))/length(Partition)}
		else{0}
	}
	NumToWav(CumVoice(length(Partition)),OutFile,SampleRate)
}

I have tried it on two different musics, the Canon in D from Johann Pachelbel and on the beginning
of The entertainer from Scott Joplin. You can find below the partition, the code and the result.
There are still a few wrong notes but this is really boring to enter the partition!

3.2. Canon in D from Johann Pachelbel

You can find the partition I used here.

Partition=list(data.frame(
	Note=c(41,40,38,36,34,33,31,29,28,45,43,41,40,38,36,38,40,41,33,36,34,33,29,33,31,29,26,29,36,34,38,36,34,33,29,31,40,41,45,48,36,38,34,36,33,29,29,28,29,40,41,29,28,36,31,33,29,41,40,38,40,43,48,50,46,45,43,46,45,43,41,40,38,36,34,33,31,34,33,31,29,31,33,34,36,31,36,34,33,38,36,34,36,34,33,31,29,26,38,40,41,40,38,36,34,33,31,38,36,38,36,34,48,45,47,48,45,47,48,36,38,40,41,43,45,46,45,41,43,45,33,34,36,38,36,34,36,33,34,36,34,38,36,34,33,31,33,31,29,31,33,34,36,38,34,38,36,38,40,41,36,38,40,41,43,45,46,48,45,41,43,45,43,41,43,40,41,43,45,43,41,40,41,38,40,41,29,31,33,34,33,31,33,41,40,41,38,41,40,38,36,34,36,34,33,34,36,38,40,41,38,41,40,41,40,38,40,41,43,41,40,41,38,40,45,33,34,33,31,43,45,43,41,33,29,26,24,24,22,36,38,26,28,26,24,36,34,36,38,26,24,26,28,40,38,40,29,41,43,41,40,28,29,28,26,38,36,38,40,28,33,31,29,41,43,46,45,33,36,45,41,46,45,46,43,36,34,36,41,40,38,36,34,33,31,29),
	Duration=c(1,1,1,1,1,1,1,0.5,0.5,1,1,1,1,1,1,1,1,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,1,0.75,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.125,0.125,0.25,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.25,0.125,0.125,0.25,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.25,0.125,0.125,0.25,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.25,0.125,0.125,0.25,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.25,0.125,0.125,0.25,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.25,0.125,0.125,0.25,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.25,0.125,0.125,0.25,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.25,0.125,0.125,0.25,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,1,1,1,1,1,1,1,1),
	Volume=rep(1,296)
))
CreateMusic(Partition = Partition,SampleRate = 44100,OutFile = "pachelbel.wav")


3.3. The entertainer from Scott Joplin

You can find the partion I used here.

Partition1=data.frame(
	Note=c(53,55,51,48,50,46,41,43,39,36,38,34,29,31,27,24,26,24,23,22,0,46,29,28,31,39,31,39,31,39,51,53,54,55,51,53,55,50,53,51,29,30,31,39,31,39,31,39,48,46,45,48,51,55,53,51,48,53,29,30,31,39,31,39,31,39,51,53,54,55,51,53,55,50,53,51,51,53,55,51,53,55,51,53,51,55,51,53,55,51,53,51,55,51,53,55,50,53,51,29,30,31,39,31,39,31,39,51,53,54,55,51,53,55,50,53,51,29,30,31,39,31,39,31,39,48,46,45,48,51,55,53,51,48,53,29,30,31,39,31,39,31,39,51,53,54,55,51,53,55,50,53,51,51,53,55,51,53,55,51,53,51,55,51,53,55,51,53,51,55,51,53,55,50,53,51),
	Duration=c(0.25,0.25,0.25,0.5,0.25,0.5,0.25,0.25,0.25,0.5,0.25,0.5,0.25,0.25,0.25,0.5,0.25,0.25,0.25,0.5,0.5,0.5,0.25,0.25,0.25,0.5,0.25,0.5,0.25,1.5,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.5,1.5,0.25,0.25,0.25,0.5,0.25,0.5,0.25,1.75,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.25,0.25,1.5,0.25,0.25,0.25,0.5,0.25,0.5,0.25,1.5,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.5,1.5,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.5,1.5,0.25,0.25,0.25,0.5,0.25,0.5,0.25,1.5,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.5,1.5,0.25,0.25,0.25,0.5,0.25,0.5,0.25,1.75,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.25,0.25,1.5,0.25,0.25,0.25,0.5,0.25,0.5,0.25,1.5,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.5,1.5,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.5,1),
	Volume=c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,1,1,1,1,1,1,1,1,1,0.25,0.25,0.25,0.25,0.25,0.25,0.75,0.9,1,1,1,1,1,1,1,1,0.75,0.6,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.7,0.8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0.7,0.5,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,1,1,1,1,1,1,1,1,1,0.25,0.25,0.25,0.25,0.25,0.25,0.75,0.9,1,1,1,1,1,1,1,1,0.75,0.6,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.7,0.8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
)
Partition2=data.frame(
	Note=c(41,43,39,36,38,34,29,31,27,24,26,22,17,19,15,12,14,12,11,10,0,41,26,15,27,22,27,20,27,19,27,10,27,10,26,15,27,27,26,15,27,22,27,20,27,19,39,39,39,36,39,43,41,39,36,44,26,15,27,22,27,20,27,0,43,44,45,46,43,44,46,41,44,43,39,41,43,39,41,43,39,41,39,43,39,41,43,39,41,39,46,43,44,46,41,44,43,26,15,27,22,27,20,27,19,27,10,27,10,26,15,27,27,26,15,27,22,27,20,27,19,39,39,39,36,39,43,41,39,36,44,26,15,27,22,27,20,27,0,43,44,45,46,43,44,46,41,44,43,39,41,43,39,41,43,39,41,39,43,39,41,43,39,41,39,46,43,44,46,41,44,43),
	Duration=c(0.25,0.25,0.25,0.5,0.25,0.5,0.25,0.25,0.25,0.5,0.25,0.5,0.25,0.25,0.25,0.5,0.25,0.25,0.25,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.25,0.25,1.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.5,1.5,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.5,1.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.25,0.25,1.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.5,1.5,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.5,1),
	Volume=c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,1,1,1,1,1,1,1,1,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.75,0.9,1,1,1,1,1,1,1,1,0.75,0.25,0.25,0.25,0.25,0.25,0.25,0,0.5,0.75,0.9,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0.75,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,1,1,1,1,1,1,1,1,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.75,0.9,1,1,1,1,1,1,1,1,0.75,0.25,0.25,0.25,0.25,0.25,0.25,0,0.5,0.75,0.9,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
)
Partition3=data.frame(
	Note=c(0,38,22,0,22,10,25,8,24,7,22,0,22,0,22,0,22,22,22,0,22,10,22,8,24,7,36,34,33,0,27,17,37,41,14,0,22,10,25,8,24,0,39,41,42,43,39,41,42,38,41,39,0,27,31,25,31,24,32,23,32,43,39,41,43,38,41,39,14,0,22,10,25,8,24,7,22,0,22,0,22,0,22,22,22,0,22,10,22,8,24,7,36,34,33,0,27,17,37,41,14,0,22,10,25,8,24,0,39,41,42,43,39,41,42,38,41,39,0,27,31,25,31,24,32,23,32,43,39,41,43,38,41,39),
	Duration=c(7,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.25,0.25,0.25,0.25,0.5,0.5,0.5,1.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.5,1.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.25,0.25,0.25,0.5,0.25,0.5,1.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.25,0.25,0.25,0.25,0.5,0.5,0.5,1.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.5,1.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.25,0.25,0.25,0.5,0.25,0.5,1),
	Volume=c(0,1,1,0,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0,0.25,0,0.25,0,0.25,0.25,0.25,0,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,1,0,1,1,1,1,0.75,0,0.25,0.25,0.25,0.25,0.25,0,0.5,0.75,0.9,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0.75,0,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0,0.25,0,0.25,0,0.25,0.25,0.25,0,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,1,0,1,1,1,1,0.75,0,0.25,0.25,0.25,0.25,0.25,0,0.5,0.75,0.9,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
)
Partition4=data.frame(
	Note=c(0,34,0,0,19,0,22,0,19,0,20,0,19,19,0,0,19,0,22,0,18,17,24,0,24,26,0,24,0,0,19,0,22,0,27,10,27,10,26,15,27,31,0,15,27,13,27,12,27,11,27,22,31,10,26,27,22,24,0,0,19,0,22,0,19,0,20,0,19,19,0,0,19,0,22,0,18,17,24,0,24,26,0,24,0,0,19,0,22,0,27,10,27,10,26,15,27,31,0,15,27,13,27,12,27,11,27,22,31,10,26,27),
	Duration=c(7,0.5,0.5,0.5,0.5,0.5,0.5,2.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,1.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,1.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,2.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,1.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,1.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,1),
	Volume=c(0,1,0,0,0.25,0,0.25,0,1,0,1,0,1,1,1,0,0.25,0,0.25,0,0.25,1,1,0,1,1,0,1,0,0,0.25,0,0.25,0,0.75,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0.25,0,0.25,0,1,0,1,0,1,1,1,0,0.25,0,0.25,0,0.25,1,1,0,1,1,0,1,0,0,0.25,0,0.25,0,0.75,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1)
)
Partition5=data.frame(
	Note=c(0,10,0,0,43,44,45,46,43,44,46,41,44,43,0,0,6,5,21,0,20,22,0,12,0,0,22,0,22,0,22,0,22,27,0,22,0,22,0,24,0,24,10,27,0,22,22,10,12,0,0,43,44,45,46,43,44,46,41,44,43,0,0,6,5,21,0,20,22,0,12,0,0,22,0,22,0,22,0,22,27,0,22,0,22,0,24,0,24,10,27,0,22,22),
	Duration=c(7,0.5,0.5,3.25,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.5,1.5,0.5,3.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,3.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,1,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,3.25,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.5,1.5,0.5,3.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,3.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,1,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,1),
	Volume=c(0,1,0,0,0.25,0.25,0.25,1,1,1,1,1,1,1,0,0,0.75,1,1,0,1,1,0,1,0,0,0.25,0,1,0,1,0,1,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,1,0,0,0.25,0.25,0.25,1,1,1,1,1,1,1,0,0,0.75,1,1,0,1,1,0,1,0,0,0.25,0,1,0,1,0,1,1,0,1,0,1,0,1,0,1,1,1,1,1,1))
Partition6=data.frame(
	Note=c(0,-1,0,0,39,41,42,43,39,41,43,38,41,39,0,0,17,0,0,19,0,20,0,19,22,0,22,0,15,0,0,39,41,42,43,39,41,43,38,41,39,0,0,17,0,0,19,0,20,0,19,22,0,22,0,15,0),
	Duration=c(7,0.5,0.5,3.25,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.5,1.5,0.5,4.5,0.5,3,4.5,0.5,0.5,0.5,0.5,0.5,0.5,5,0.5,1,0.5,1.5,3.25,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.25,0.5,1.5,0.5,4.5,0.5,3,4.5,0.5,0.5,0.5,0.5,0.5,0.5,5,0.5,1,0.5,0.5),
	Volume=c(0,1,0,0,0.25,0.25,0.25,1,1,1,1,1,1,1,0,0,1,0,0,1,0,1,0,1,1,0,1,0,1,0,0,0.25,0.25,0.25,1,1,1,1,1,1,1,0,0,1,0,0,1,0,1,0,1,1,0,1,0,1,0)
)
Partition=list(Partition1,Partition2,Partition3,Partition4,Partition5,Partition6)
CreateMusic(Partition=Partition,SampleRate=44100,OutFile="joplin.wav")


Game over! gotop