def plt_clips_pitch(pitchDir): with open(pitchDir, 'r') as fp: for line in fp: line = line.rstrip() if line.endswith('['): clip = line.split()[0] singerID, songID, clipID = clip.split('-') clipInfor = song(singerID, songID, clipID) pitch = [] else: pitch.append(float(line.split()[1])) if line.endswith(']'): pitch = smooth(pitch, 75) X = np.arange(0, len(pitch)) * 0.01 plt.plot(X, pitch) plt.xlim(0, len(pitch) * 0.01) plt.ylim(0, 500) plt.xlabel('Time(s)') plt.ylabel('Pitch(Hz)') plt.title(clip + ':' + clipInfor.songName) #plt.show() print clip, 'is saved.' saveDir = 'exp/pitch/' + sys.argv[1] + '/smoothed' if os.path.isdir(saveDir) is False: os.mkdir(saveDir) plt.savefig(os.path.join(saveDir, clip)) plt.clf()
def createfolder(self,folder_name): #checking if the folder exists if not os.path.isdir(folder_name): raise IOError( "Specified directory does not exist . . .") else: filelist = os.listdir(folder_name) self.foldername = folder_name song1 = song() for f in filelist: # todojuke test for empty folder ext = os.path.splitext(f) path = folder_name + f if ext[1] == '.mp3' or ext[1] == '.ogg': song1.create_from_file(path) self.addsong(song1) elif os.path.splitext(f)[1] == '.flac': self.createfolder_from_flac(f)
def file_to_song(self, file_content): buf = file_content.split(self.sep) newsong = song(buf[0], [], buf[1], buf[2], buf[3], buf[4]) for j in range(5,1024): try: if( (buf[j][0] == 'R') & (buf[j][1] == ':') ): newsong.add_chorus(buf[j].split("R:")[1]) else: newsong.add_verse(buf[j]) except: break return newsong
def sex_pitch_distribution(pitchDir): Mpitch = [] Fpitch = [] Dpitch = [] Allpitch = [] with open(pitchDir, 'r') as fp: for line in fp: line = line.rstrip() if line.endswith('['): clip = line.split()[0] singerID, songID, clipID = clip.split('-') clipInfor = song(singerID, songID, clipID) print 'Analyzing', clip, 'singerSex is', clipInfor.singerSex pitch = [] else: pitch.append(float(line.split()[1])) if line.endswith(']'): pitch = smooth(pitch, 75) if clipInfor.singerSex == 'M': for x in pitch: Mpitch.append(x) Allpitch.append(x) if clipInfor.singerSex == 'F': for x in pitch: Fpitch.append(x) Allpitch.append(x) if clipInfor.singerSex == 'D': for x in pitch: Dpitch.append(x) Allpitch.append(x) plt_pitch_distribt(Mpitch, 'Male', 'b') plt_pitch_distribt(Fpitch, 'Female', 'r') plt_pitch_distribt(Dpitch, 'D', 'g') plt_pitch_distribt(Allpitch, 'All', 'k') plt.legend() plt.show()
def songtext(): # colors R G B white = (255, 255, 255) red = (255, 0, 0) green = ( 0, 255, 0) blue = (102, 102, 0) #blue = ( 0, 0, 255) black = ( 0, 0, 0) cyan = ( 50, 255, 255) magenta = (255, 0, 255) yellow = (255, 255, 0) orange = (255, 127, 0) # Set up the base menu you can customize your menu with the colors above #set size of the screen size = width, height = 480, 320 screen = pygame.display.set_mode((size), pygame.FULLSCREEN ) pygame.init() pygame.mouse.set_visible(1) # the 'normal' font base_font = pygame.font.SysFont('Arial', 24, 1) # the 'mouse-over' font mod_font = pygame.font.SysFont('Arial', 24, 1) mod_font.set_underline(True) # always use some kind of cache when rendering font # font rendering is very expensive, and it *will* # slow down your application if you render some text cache = {} # always use a Clock to keep your framerate constant clock = pygame.time.Clock() run = True while run: def render(text, mod=False): if not mod in cache: cache[mod] = {} if not text in cache[mod]: cache[mod][text] = (mod_font if mod else base_font).render(text, True, (255, 255, 255)) return cache[mod][text] # create a list of (text, Rect)-tuples x, y = 1, 20 objetcs = [(t, render(t).get_rect()) for t in (song(), artist(), album(), station())] # just move some objects around for (_, r) in objetcs: r.top, r.left = y, x x *= 2 y += 60 # clear the screen screen.fill((0, 0, 0)) # draw all objects for (t, r) in objetcs: # decide if we want to render the text with or without the underline # based on the result of collidepoint(pygame.mouse.get_pos()) # note that collidepoint is a method of Rect, not Surface screen.blit(render(t, r.collidepoint(pygame.mouse.get_pos())), r) # check which item is clicked if pygame.event.get(pygame.MOUSEBUTTONDOWN): # calling 'render' over and over again is cheap now, since the resut is cached for text in [t for (t, r) in objetcs if r.collidepoint(pygame.mouse.get_pos())]: print "'{}' was clicked".format(text) # draw the screen ONCE pygame.display.flip() if pygame.event.get(pygame.QUIT): run = False # clear all unwanted events pygame.event.clear() clock.tick(60) for event in pygame.event.get(): if event.type == KEYDOWN: if event.key == K_ESCAPE: sys.exit()
def new_song(self): s = song("") self.set_active_song(s)