In [2]:
%matplotlib notebook

from scipy.io.wavfile import write
from numpy import linspace,sin,pi,int16,concatenate
from pylab import plot,show,axis

import pandas as pd
import os
import random as rand
from os.path import isfile, join

In [3]:
fileList = os.listdir('gg/')
noteDir = os.listdir('scl/')

def isDataFile(filename):
    return filename.find('.dat')>0

dataFiles = filter(isDataFile,fileList)

def isNoteFile(filename):
    return filename.find('.txt')>0

noteFiles = filter(isNoteFile,noteDir) #Pulls all .txt files containing the various scales, same as data files func

In [27]:
h = 50
h/5


Out[27]:
10

In [4]:
scales = {} #Initialize overall scale dictionary
all_grains = [] #Initialize overall list of grain snapshots

print 'Building note list...'
for scale in noteFiles:
    notes = open('scl/' + scale,'r')
    note_list = []
    note_file = notes.readlines()
    for line in note_file:
        line = line.split("\t",3)
        line[2] = line[2].strip('\n')
        note_list.append(line)
    scales[scale.strip('.txt')] = note_list

print 'Note list built.'

print 'Building grain list...'

for data in dataFiles:
    grain_list = []
    grains = open("gg/" + data, "r")
    grain_file = grains.readlines()
    for line in grain_file:
        line = line.split("\t",3)
        line[2] = line[2].strip('\n')
        grain_list.append(line)
    all_grains.append(grain_list)

print 'Grain list built.'


Building note list...
Note list built.
Building grain list...
Grain list built.

In [5]:
len(all_grains[0])


Out[5]:
50

In [30]:
pd.read_csv('gg/'+dataFiles[0],sep='\t',header=None,names=('grain-ID','area','sides'))


Out[30]:
grain-ID area sides
0 1 0.002307 4
1 2 0.041458 5
2 3 0.011237 5
3 4 0.014604 6
4 5 0.027024 8
5 6 0.017106 5
6 7 0.034995 7
7 8 0.012546 6
8 9 0.074316 5
9 10 0.028605 5
10 11 0.006609 5
11 12 0.043620 7
12 13 0.017081 4
13 14 0.002516 3
14 15 0.041003 7
15 16 0.004306 5
16 17 0.024224 6
17 18 0.029528 5
18 19 0.029660 3
19 20 0.019782 5
20 21 0.004285 4
21 22 0.002741 4
22 23 0.005267 4
23 24 0.004443 6
24 25 0.020166 7
25 26 0.010164 4
26 27 0.035461 7
27 28 0.035187 5
28 29 0.032246 8
29 30 0.014116 9
30 31 0.013792 4
31 32 0.027356 7
32 33 0.042277 9
33 34 0.003075 4
34 35 0.010560 5
35 36 0.035285 6
36 37 0.005527 5
37 38 0.012483 5
38 39 0.007960 4
39 40 0.000837 3
40 41 0.033747 7
41 42 0.032337 6
42 43 0.002720 3
43 44 0.001259 4
44 45 0.027278 8
45 46 0.029346 7
46 47 0.012089 4
47 48 0.003810 4
48 49 0.049907 5
49 50 0.001755 5

In [5]:
# tone synthesis
def note(freq, len, amp=1, rate=44100):
 t = linspace(0,len,len*rate)
 data = sin(2*pi*freq*t)*amp
 return data.astype(int16) # two byte integers

In [8]:
# A tone, 2 seconds, 44100 samples per second (PH note)

tone_total = []
notefile = 0
i = 0
for data in all_grains:
    i += 1
    scale_selection = rand.randint(0,3)
    note_len = rand.choice([0.25,0.5,0.75,1.0,1.25,1.5,1.75,2.0])
    tones = []
    for grain in data:
        for tone in scales[noteFiles[notefile].strip('.txt')]:
            if grain[2] == tone[2]:
                output_note = note(float(tone[1]),float(2)/len(data),amp=100000*float(grain[1]))
                tone_total.append(output_note)
    if i%5 == 0:
        notefile += 1
    if i/10 == 2:
        notefile = 0
        i = 0
    print i
    print notefile
    #tone_total.append(sum(tones))

chords = []
chordfile = 0
w = 0
for data in all_grains:
    w += 1
    scale_selection = rand.randint(0,3)
    tones = []
    for grain in data:
        for tone in scales[noteFiles[chordfile].strip('.txt')]:
            if grain[2] == tone[2]:
                output_note = note(float(tone[1]),2,amp=1000*float(grain[1]))
                tones.append(output_note)
    chords.append(sum(tones))
    if w%5 == 0:
        chordfile += 1
    if w/10 == 2:
        chordfile = 0
        w = 0
    
print 'Finished building notes'




#concatenate((tone_total))


1
0
2
0
3
0
4
0
5
1
6
1
7
1
8
1
9
1
10
2
11
2
12
2
13
2
14
2
15
3
16
3
17
3
18
3
19
3
0
0
1
0
2
0
3
0
4
0
5
1
6
1
7
1
8
1
9
1
10
2
11
2
12
2
13
2
14
2
15
3
16
3
17
3
18
3
19
3
0
0
1
0
2
0
3
0
4
0
5
1
6
1
7
1
8
1
9
1
10
2
11
2
12
2
13
2
14
2
15
3
16
3
17
3
18
3
19
3
0
0
1
0
2
0
3
0
4
0
5
1
6
1
7
1
8
1
9
1
10
2
11
2
12
2
13
2
14
2
15
3
16
3
17
3
18
3
19
3
0
0
1
0
2
0
3
0
4
0
5
1
6
1
7
1
8
1
9
1
10
2
11
2
12
2
13
2
14
2
15
3
16
3
17
3
18
3
19
3
0
0
Finished building notes

In [10]:
write('test_noteProgression.wav',44100,concatenate((tone_total))) # writing the sound to a file
len(tone_total[:])
write('test_chords.wav',44100,concatenate((chords))) # writing the sound to a file
print len(tone_total[:])
print len(chords[:])
#plot(linspace(0,2,2*44100),tone_total)
#axis([0,0.4,15000,-15000])
#show()


5000
100

In [ ]: