def process(fileName):
    client = MongoClient()
    db = client.audiograins
    grainEntries = db.grains
    song = AudioSegment.empty()
    songFile = open(fileName)
    print("converting to mono")
    monoFilename = "mono" + fileName
    subprocess.check_call(["ffmpeg -i " + fileName + " -ac 1 -threads 2 " + monoFilename], close_fds=True, shell=True)
    grains = granulizer.chopSound(monoFilename, 20, "inputGrains", "sample")
    labels = ["chorus", "hip-hop", "latin", "orchestra", "pop", "rock", "country", "jazz", "opera", "piano", "reggae", "techno"]
    classifications = [0] * 12;
    totalGrains = 0
    normalizer = pickle.load(open("normalizer.pickle"))
    classifier = pickle.load(open("classifier.pickle"))
    indexToIds = pickle.load(open("indexToIds.pickle"))    

    for grain in tqdm(grains):
        #Analyze all stats for grain
        totalGrains += 1
        dataPoint = np.empty([1, 16])
        mfccs = analyzer.analyzeMFCC(grain)
        dataPoint[0][0] = mfccs[0]
        dataPoint[0][1] = mfccs[1]
        dataPoint[0][2] = mfccs[2]
        dataPoint[0][3] = mfccs[3]
        dataPoint[0][4] = mfccs[4]
        dataPoint[0][5] = mfccs[5]
        dataPoint[0][6] = mfccs[6]
        dataPoint[0][7] = mfccs[7]
        dataPoint[0][8] = mfccs[8]
        dataPoint[0][9] = mfccs[9]
        dataPoint[0][10] = mfccs[10]
        dataPoint[0][11] = mfccs[11]
        dataPoint[0][12] = mfccs[12]
        dataPoint[0][13] = analyzer.analyzePitch(grain)
        dataPoint[0][14] = analyzer.analyzeEnergy(grain)
        #dataPoint[15] = kurtosis
        #dataPoint[16] = skewness
        #dataPoint[17] = spread
        #dataPoint[18] = centroid
        dataPoint[0][15] = analyzer.analyzeZeroCrossingRate(grain)
        classifications[int(classifier.predict(normalizer.transform(dataPoint))[0])] += 1
        #print("Prediction: " + str(classifier.predict(normalizer.transform(dataPoint))[0]))
    
    print("Genre:")
    for i in range(0, 12):
        print("\t" + labels[i] + ": " + str(((float(classifications[i]) / float(totalGrains)) * 100.0)) + "%") 
Beispiel #2
0
    print("downloading " + toDownload)
    try:
        urllib.urlretrieve(url=toDownload, filename=filename)
        songFile = open(filename)
        print("done downloading file")
        print("converting to " + monoFilename)
        subprocess.check_call(["ffmpeg -i " + filename +" -ac 1 -threads 2 " + monoFilename], close_fds=True, shell=True)
    except Exception:
        print("Error in ffmpeg or download")
        print("cleaning up..")
        subprocess.call(["rm " + filename], shell=True)   
        open(downloadsPath + downloadFile, 'w').writelines(lines[1:])
        subprocess.call(["rm " + monoFilename], shell=True)   
        sys.exit()

    grainMongoObjects = chopSound(monoFilename, 20, grainsFolder, downloadFile[:-4]) 
    if grainMongoObjects is None:
        print("Problem chopping sound, skipping " + monoFilename) 
        subprocess.call(["rm " + filename], shell=True)   
        open(downloadsPath + downloadFile, 'w').writelines(lines[1:])
        subprocess.call(["rm " + monoFilename], shell=True)   
        songFile.close()
        sys.exit()
    else:
        try:
            storeGrains(grainMongoObjects)
            analyzeMFCCSubset(grainMongoObjects)
            logging.debug("done with mfcc for " + toDownload)
            analyzePitchSubset(grainMongoObjects)
            logging.debug("done with pitch for " + toDownload)
            analyzeEnergySubset(grainMongoObjects)