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]:
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.'
In [5]:
len(all_grains[0])
Out[5]:
In [30]:
pd.read_csv('gg/'+dataFiles[0],sep='\t',header=None,names=('grain-ID','area','sides'))
Out[30]:
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))
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()
In [ ]: