def createSnareFft(): # read in the snare drum sample files = glob.glob('./snare_samples/*.wav') matrix = numpy.array([]) for filename in files: print filename rate, data = wave_reader.readWav(filename) data = wave_reader.stereoToMono(data) features = analyzer.getFrequencies(data) matrix = build_data.addToFeatureMatrix(matrix, features[0:2000]) build_data.saveFile('snaredata.csv', matrix)
def wavToFeatures(filename): rate, data = wavfile.read(filename) if data.ndim > 1: data = stereoToMono(data) segDiv = 4 #1/10 of a second segSize = rate / segDiv start = 0 end = segSize matrix = numpy.array([]) for i in range(0, data.size / segSize): data_seg = data[start:end] start += segSize end += segSize features = analyzer.getFrequencies(data_seg) matrix = build_data.addToFeatureMatrix(matrix, features[0:2000]) # build_data.saveFile('testdata.csv', matrix) return matrix
def readSong(filename, segTime = 0.05): # read in the input song rate, data = wave_reader.readWav(filename) data = wave_reader.stereoToMono(data) # sample every 0.1 seconds by default segSize = segTime * rate matrix = numpy.array([]) print 'reading in song...' start = 0 end = segSize for i in range(0, int(math.floor(len(data)/segSize))): data_seg = data[start:end] start += segSize end += segSize features = analyzer.getFrequencies(data_seg) matrix = build_data.addToFeatureMatrix(matrix, features[0:2000]) outfile = os.path.splitext(filename)[0] + '.csv' build_data.saveFile(outfile, matrix) return matrix
def extractFeatures(track, div): rate, data = wavfile.read(track + '.wav') if data.ndim > 1: data = stereoToMono(data) matrix = numpy.array([]) vmatrix = numpy.array([]) segSize = rate / div start = 0 end = segSize for i in range(0, data.size / segSize): data_seg = data[start:end] start += segSize end += segSize features = analyzer.getFrequencies(data_seg)[0:5000] matrix = build_data.addToFeatureMatrix(matrix, features) vmatrix = build_data.addToFeatureMatrix(vmatrix, numpy.array([numpy.argmax(features)])) build_data.saveFile(track + '_seg.csv', matrix) build_data.saveFile(track + '_volume.csv', vmatrix)
def saveFile(filename, data): numpy.savetxt(filename, data, delimiter=',', fmt='%.2f', newline = '\n') def addToFeatureMatrix(matrix, feature): feature = feature.reshape(1, feature.shape[0]) if matrix.size is 0: matrix = feature; else: matrix = numpy.vstack((matrix, feature)) return matrix if __name__ == '__main__': matrix = numpy.array([]) seconds = 1 for note in range(0, 12): print "generating note " + str(note) for x in range(0, 100): notesToPlay = chord_gen.createMajorChords(note) length = int(seconds * 44100) sample = numpy.zeros(length) for i in range(0, len(notesToPlay)): freq = midi_util.midiToFrequency(notesToPlay[i]) sample += wave_gen.saw(freq, seconds, 44100) features = analyzer.getFrequencies(sample) features = numpy.append(features[0:5000], note) #features = features.astype(numpy.int16) matrix = addToFeatureMatrix(matrix, features) saveFile('largedata.csv', matrix)