Beispiel #1
0
def createSpectrogramsFromAudio():
    genresID = dict()
    files = os.listdir(rawDataPath)
    files = [file for file in files if file.endswith(".mp3")]
    nbFiles = len(files)

    #Create path if not existing
    if not os.path.exists(os.path.dirname(spectrogramsPath)):
        try:
            os.makedirs(os.path.dirname(spectrogramsPath))
        except OSError as exc:  # Guard against race condition
            if exc.errno != errno.EEXIST:
                raise

    #Rename files according to genre
    for index, filename in enumerate(files):
        print "Creating spectrogram for file {}/{}...".format(
            index + 1, nbFiles)
        print filename
        fileGenre = getGenre(rawDataPath + filename)
        genresID[fileGenre] = genresID[
            fileGenre] + 1 if fileGenre in genresID else 1
        fileID = genresID[fileGenre]
        newFilename = fileGenre + "_" + str(fileID)
        print newFilename
        createSpectrogram(filename, newFilename)
def createSpectrogramsFromAudio():
    '''Creates .png whole spectrograms from mp3 files'''
    genresID = dict()
    files = os.listdir(rawDataPath)
    files = [file for file in files if file.endswith(".mp3")]
    nbFiles = len(files)

    # Create path if not existing
    createFolder(linearSpectrogramsPath)
    createFolder(melSpectrogramsPath)
    createFolder(logSpectrogramsPath)

    # Rename files according to genre
    for index, filename in enumerate(files):
        # Rename file before finding genre
        # Strip out any special characters
        os.rename(rawDataPath + filename,
                  rawDataPath + re.sub('[^A-Za-z0-9. ]+', '', filename))
        filename = re.sub('[^A-Za-z0-9. ]+', '', filename)

        print("Creating spectrogram(s) for file {}/{}...  {}".format(
            index + 1, nbFiles, filename))

        fileGenre = getGenre(rawDataPath + filename)
        if fileGenre == 'Other':
            print("Genre was converted into 'other' for file: ", filename)
        genresID[fileGenre] = genresID[
            fileGenre] + 1 if fileGenre in genresID else 1
        fileID = genresID[fileGenre]
        newFilename = fileGenre + "_" + str(
            fileID)  # if fileGenre is byte then do this
        createMelSpectrogramLibrosa(filename, newFilename)
        # createMFCCPlotLibrosa(filename, newFilename)
        createLinearOrLogSpectrogramLibrosa(filename, newFilename)
Beispiel #3
0
def createSpectrogramsFromAudio():
    genresID = dict()
    files = os.listdir(rawDataPath)
    files = [file for file in files if file.endswith(".mp3")]
    nbFiles = len(files)

    # Create path if not existing
    if not os.path.exists(os.path.dirname(spectrogramsPath)):
        try:
            os.makedirs(os.path.dirname(spectrogramsPath))
        except OSError as exc:  # Guard against race condition
            if exc.errno != errno.EEXIST:
                raise

    # Rename files according to genre
    for index, filename in enumerate(sorted(files)):
        print("Creating spectrogram for file {}/{}...".format(
            index + 1, nbFiles))
        if len(filename) > 12 and filename[12] == '~':
            filename_to_genre = filename[13:]
        else:
            filename_to_genre = filename
        fileGenre = getGenre(filename_to_genre)
        if fileGenre == 'Unknown':
            continue
        genresID[fileGenre] = genresID[fileGenre] + \
            1 if fileGenre in genresID else 1
        fileID = genresID[fileGenre]
        newFilename = fileGenre + "_" + str(fileID)
        if not os.path.exists('Data/Spectrograms/{}.png'.format(newFilename)):
            createSpectrogram(filename, newFilename)
def createSpectrogramsFromAudio():
	genresID = dict()
	files = os.listdir(rawDataPath)
	files = [file for file in files if file.endswith(".mp3")]
	nbFiles = len(files)

	#Create path if not existing
	if not os.path.exists(os.path.dirname(spectrogramsPath)):
		try:
			os.makedirs(os.path.dirname(spectrogramsPath))
		except OSError as exc: # Guard against race condition
			if exc.errno != errno.EEXIST:
				raise

	#Rename files according to genre
	for index,filename in enumerate(files):
		print(index, filename)
		print("Creating spectrogram for file {}/{}...".format(index+1,nbFiles))
		try:
			fileGenre = getGenre(rawDataPath+filename).decode('ASCII')
			check_idx = fileGenre.find('/')
			if check_idx > 1:
				fileGenre = fileGenre[0:check_idx]
			fileGenre = fileGenre.strip()

		except Exception as err:
			print(err)
			continue

		print(fileGenre)
		genresID[fileGenre] = genresID[fileGenre] + 1 if fileGenre in genresID else 1
		fileID = genresID[fileGenre]
		newFilename = fileGenre+"_"+str(fileID)
		createSpectrogram(filename,newFilename)
def createSpectrogramsFromAudio():
	genresID = dict()
	files = os.listdir(rawDataPath)
	files = [file for file in files if file.endswith(".mp3")]
	nbFiles = len(files)

	#Create path if not existing
	if not os.path.exists(os.path.dirname(spectrogramsPath)):
		try:
			os.makedirs(os.path.dirname(spectrogramsPath))
		except OSError as exc: # Guard against race condition
			if exc.errno != errno.EEXIST:
				raise

	#Rename files according to genre
	for index,filename in enumerate(files):
		print "Creating spectrogram for file {}/{}...".format(index+1,nbFiles)
		fileGenre = getGenre(rawDataPath+filename)
		genresID[fileGenre] = genresID[fileGenre] + 1 if fileGenre in genresID else 1
		fileID = genresID[fileGenre]
		newFilename = fileGenre+"_"+str(fileID)
		createSpectrogram(filename,newFilename)
Beispiel #6
0
def createSpectrogramsFromAudio():
	genresID = dict()
	files = os.listdir(rawDataPath)
	files = [file for file in files if file.endswith(".mp3")]
	nbFiles = len(files)

	#Create path if not existing
	if not os.path.exists(os.path.dirname(spectrogramsPath)):
		try:
			os.makedirs(os.path.dirname(spectrogramsPath))
		except OSError as exc:  # Guard against race condition
			if exc.errno != errno.EEXIST:
				raise
	
	#Rename files according to genre
	tmp_genres = ['Hip-Hop', 'Ballad', 'Korean', 'Pop', 'Dance', 'Soul', 'Rock', 'R&B']
	for index, filename in enumerate(files):
		print(index, filename)
		print("Creating spectrogram for file {}/{}...".format(index+1, nbFiles))
		try:
			fileGenre = getGenre(rawDataPath+filename).decode('ASCII')
			check_idx = fileGenre.replace(" ", "").split('/')
			for i in check_idx:
				if i in tmp_genres:
					fileGenre = i
				elif fileGenre not in tmp_genres:
					fileGenre = 'Etc'

		except Exception as err:
			print(err)
			continue

		print("Genre: ", fileGenre)
		genresID[fileGenre] = genresID[fileGenre] + 1 if fileGenre in genresID else 1
		fileID = genresID[fileGenre]
		newFilename = fileGenre+"_"+str(fileID)
		createSpectrogram(filename, newFilename)