Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions audioFilesTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ def getGenre(filename):
audiofile = eyed3.load(filename)
#No genre
if not audiofile.tag.genre:
return None
return str(None)
else:
return audiofile.tag.genre.name.encode('utf-8')
genere = str(audiofile.tag.genre.name.encode('utf-8'))
genere = genere.replace('b\'', '')
genere = genere.replace('\'', '')
return genere






2 changes: 1 addition & 1 deletion sliceSpectrogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def sliceSpectrogram(filename, desiredSize):

#For each sample
for i in range(nbSamples):
print "Creating slice: ", (i+1), "/", nbSamples, "for", filename
print ("Creating slice: ", (i+1), "/", nbSamples, "for", filename)
#Extract and save 128x128 sample
startPixel = i*desiredSize
imgTmp = img.crop((startPixel, 1, startPixel + desiredSize, desiredSize + 1))
Expand Down
18 changes: 7 additions & 11 deletions songToData.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from subprocess import Popen, PIPE, STDOUT
import os
from PIL import Image
import eyed3

from sliceSpectrogram import createSlicesFromSpectrograms
from audioFilesTools import isMono, getGenre
Expand All @@ -16,9 +15,6 @@
#Define
currentPath = os.path.dirname(os.path.realpath(__file__))

#Remove logs
eyed3.log.setLevel("ERROR")

#Create spectrogram from mp3 files
def createSpectrogram(filename,newFilename):
#Create temporary mono track if needed
Expand All @@ -29,15 +25,15 @@ def createSpectrogram(filename,newFilename):
p = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True, cwd=currentPath)
output, errors = p.communicate()
if errors:
print errors
print (errors)

#Create spectrogram
filename.replace(".mp3","")
command = "sox '/tmp/{}.mp3' -n spectrogram -Y 200 -X {} -m -r -o '{}.png'".format(newFilename,pixelPerSecond,spectrogramsPath+newFilename)
p = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True, cwd=currentPath)
output, errors = p.communicate()
if errors:
print errors
print (errors)

#Remove tmp mono track
os.remove("/tmp/{}.mp3".format(newFilename))
Expand All @@ -59,7 +55,7 @@ def createSpectrogramsFromAudio():

#Rename files according to genre
for index,filename in enumerate(files):
print "Creating spectrogram for file {}/{}...".format(index+1,nbFiles)
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]
Expand All @@ -68,10 +64,10 @@ def createSpectrogramsFromAudio():

#Whole pipeline .mp3 -> .png slices
def createSlicesFromAudio():
print "Creating spectrograms..."
print ("Creating spectrograms...")
createSpectrogramsFromAudio()
print "Spectrograms created!"
print ("Spectrograms created!")

print "Creating slices..."
print ("Creating slices...")
createSlicesFromSpectrograms(desiredSize)
print "Slices created!"
print ("Slices created!")