In [1]:
%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
from os.path import isfile, join

In [46]:
fileList = os.listdir('gg/')

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

dataFiles = filter(isDataFile,fileList)
print 'Building note list...'
notes = open('CMaj_Notes.txt','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)
print 'Note list built.'
print 'Building grain list...'
all_grains = []
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 [4]:
len(dataFiles)


Out[4]:
100

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 [32]:
# 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 [43]:
# A tone, 2 seconds, 44100 samples per second

tone_1 = note(440,2,amp=10000) + note(660,2,amp=10000)
tone_2 = note(440,2,amp=10000)

tone_total = []

#concatenate((tone_1,tone_2))
for data in all_grains:
    tones = []
    for grain in data:
        for tone in note_list:
            if grain[2] == tone[2]:
                output_note = note(float(tone[1]),2,amp=100)
                tones.append(output_note)
    tone_total.append(sum(tones))
print 'Finished building notes'




#concatenate((tone_total[0:-1]))


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

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


Out[47]:
100

In [ ]: