def decomposeTargetSf(filename, startsec, endsec, params):
	# create directory if needed
	descomposedir = os.path.join(os.path.dirname(__file__), 'data_decomposedtargets')
	if not os.path.exists(descomposedir): os.makedirs(descomposedir)

	assert 'type' in params and params['type'] in ['NMF', 'HPSS']

	checksum = util.listToCheckSum([filename, util.getTimeStamp(filename), str(startsec), str(endsec)] + list(params.values()))[:12]
	filehead = '%s-%s-%s'%(os.path.splitext(os.path.split(filename)[1])[0], params['type'], checksum)
	decomposedSoundfile = os.path.join(descomposedir, filehead+'.wav')
	decomposedJsonfile = os.path.join(descomposedir, filehead+'.json')
	
	if not os.path.exists(decomposedSoundfile):
		if params['type'] == 'NMF':
			dur = __NMF__(filename, startsec, endsec, params['streams'], decomposedSoundfile, params['fftsize'], params['hopsize'])
		if params['type'] == 'HPSS':
			dur = __HPSS__(filename, startsec, endsec, decomposedSoundfile, params['fftsize'], params['hopsize'])

		fh = open(decomposedJsonfile, 'w')
		data = {'duration': dur, 'path': filename}
		data.update(params)
		json.dump(data, fh)
		fh.close()
		print("Resulting file written to %s (%f sec)\n"%(decomposedSoundfile, data['duration']))
		return decomposedSoundfile, dur
	else:
		fh = open(decomposedJsonfile, 'r')
		data = json.load(fh)
		fh.close()
		print("Using target sound %s (%f sec)\n"%(decomposedSoundfile, data['duration']))
		return decomposedSoundfile, data['duration']
def getClassChecksum(classinstance, also=[]):
	check_me = []
	for a in dir(classinstance):
		if a.startswith('__'): continue
		if a == 'descriptor_list': check_me.extend([a._checksum for a in getattr(classinstance, a)])
		else: check_me.append(str(getattr(classinstance, a)))
	return util.listToCheckSum(check_me)
	def __init__(self, *args, **kwargs):
		assert len(args) >= 1
		self.instrumentobjs = args
		self.params = { 
		'tempo': 60,
		'meter': '4/4',
		}
		self.params.update(kwargs)
		# CUSTOM CHECKSOME
		self._checksum = util.listToCheckSum([i._checksum for i in self.instrumentobjs] + [str(self.params)])
Exemple #4
0
    def initAnalResource(self, sffile):
        '''if analysis files don't exist, this function automatically makes
		an ircamdescriptor analysis because it is required for all
		sound files used.'''
        self.rawData[sffile] = {'info': {}}
        sfroot, sfhead = os.path.split(sffile)
        sfheadroot, sfheadext = os.path.splitext(sfhead)
        self.rawData[sffile]['fileroot'] = os.path.join(
            self.analdir, sfheadroot)

        self.rawData[sffile]['checksum'] = util.listToCheckSum([
            sffile, self.resampleRate, self.windowType, self.winLengthSec,
            self.hopLengthSec, self.F0MaxAnalysisFreq, self.F0MinFrequency,
            self.F0MaxFrequency, self.F0AmpThreshold, self.numbMfccs, 'ircamd'
        ])[:12]
        filehead = '%s-%s' % (sfheadroot, self.rawData[sffile]['checksum'])
        descriptorfile = os.path.join(self.analdir,
                                      '%s-ircamd.npy' % (filehead))
        infofile = os.path.join(self.analdir, '%s.json' % (filehead))
        ircamd_dict = {}

        if not os.path.exists(descriptorfile) or not os.path.exists(
                infofile) or util.checkIfFileIsNewer(
                    sffile, descriptorfile) or util.checkIfFileIsNewer(
                        sffile, infofile) or self.forceAnal:
            # do analysis
            if self.p != None:
                self.p.log("ANAL DATA: creating NPY FILE '%s'" %
                           descriptorfile)
            # write tmp config files
            fh = open(self.config_loc, 'w')
            fh.write(self.config_text)
            fh.close()
            # create files
            self.rawData[sffile]['info'], self.rawData[sffile][
                'ircamd'] = self.__createDescriptorsFile__(
                    sffile, self.analdir, descriptorfile, infofile,
                    self.ircamdescriptor_bin, self.config_loc)
            if not os.path.exists(descriptorfile):
                print(
                    util.ladytext(
                        "Oh noos! The ircamdescriptor binary has fialed to create the requested output files.  See the binary's output below for details."
                    ))
        else:
            # load files
            f = open(infofile, "r")
            self.rawData[sffile]['info'].update(json.load(f))
            f.close()
            self.rawData[sffile]['ircamd'] = np.load(descriptorfile)
        self.rawData[sffile]['arraylen'] = self.rawData[sffile][
            'ircamd'].shape[0]
        # touch file in data registry
        self.dataRegistry[filehead] = time.time(), os.stat(sffile).st_size
Exemple #5
0
    def analize_spectralPeaks(self,
                              filepath,
                              minamp=0.001,
                              fftsize=2048,
                              hop_length=2048,
                              min_midi=20,
                              max_midi=108):
        import librosa
        import peakutils
        checksum = util.listToCheckSum([
            filepath, fftsize, hop_length, minamp, min_midi, max_midi, 'peaks'
        ])[:12]
        filehead = '%s-%s' % (os.path.splitext(
            os.path.split(filepath)[1])[0], checksum)
        outputpath = os.path.join(self.analdir, '%s-peaks.json' % (filehead))

        if os.path.exists(outputpath):
            fh = open(outputpath, 'r')
            return json.load(fh)
        y, sr = librosa.load(filepath, sr=None, mono=True)
        outputdata = {'sr': sr, 'fftsize': fftsize, 'peaks': []}
        halfwin = int(fftsize / 2)
        binsize = sr / float(fftsize)
        lins = np.linspace(0, halfwin - 1, num=halfwin + 1)

        S = np.abs(librosa.stft(y, n_fft=fftsize, hop_length=hop_length))

        for fidx, frame in enumerate(np.rot90(S)):
            indexes = peakutils.indexes(frame, thres=minamp, min_dist=10)
            # interpolate indexes for better frq accuracy
            interp_indexes = peakutils.interpolate(lins, frame, ind=indexes)
            timesec = fidx * (float(hop_length) / sr)
            framepeaks = []
            for ii in interp_indexes:
                if ii < 0: continue
                midi = util.frq2Midi(ii * binsize)
                if midi < min_midi or midi > max_midi: continue
                iamp = util.ampToDb(
                    np.interp(ii, lins, frame) * (2. / float(fftsize)))
                framepeaks.append([iamp, midi])
            framepeaks.sort(reverse=True)
            outputdata['peaks'].append([timesec, framepeaks])

        fh = open(outputpath, 'w')
        json.dump(outputdata, fh)
        return outputdata
Exemple #6
0
    def __init__(self,
                 filename,
                 startSec,
                 endSec,
                 descriptors,
                 AnalInterface,
                 envDb=+0,
                 envAttackSec=0.,
                 envDecaySec=0.,
                 envSlope=1.,
                 envAttackenvDecayCushionSec=0.01):
        self.filename = util.verifyPath(filename, AnalInterface.searchPaths)
        self.printName = os.path.split(
            self.filename)[1]  # short name for printing
        # filename-based descriptor info
        self.midiPitchFromFilename = descriptordata.getMidiPitchFromString(
            self.printName)
        self.rmsAmplitudeFromFilename, self.dynamicFromFilename = util.getDynamicFromFilename(
            self.printName, notFound=-1000)
        # other info
        self.soundfileExtension = os.path.splitext(self.filename)[1]
        self.soundfileTotalDuration, self.soundfileChns = AnalInterface.validateAnalResource(
            self.filename)
        self.segmentStartSec = startSec
        self.segmentEndSec = endSec
        self.envDb = util.getScaleDb(envDb, self)
        self.envAttack = envAttackSec
        self.envDecay = envDecaySec
        self.envSlope = envSlope
        # if startSec=None, it is begninning
        # if endSec=None, it is end of file
        if self.segmentStartSec == None: self.segmentStartSec = 0
        else: self.segmentStartSec = self.segmentStartSec
        if self.segmentEndSec == None:
            self.segmentEndSec = self.soundfileTotalDuration
        else:
            self.segmentEndSec = self.segmentEndSec
        self.segmentDurationSec = self.segmentEndSec - self.segmentStartSec
        # ensure length is at least 1 fram
        self.lengthInFrames = max(
            1,
            AnalInterface.getSegmentFrameLength(self.segmentDurationSec,
                                                self.filename))
        self.f2s = AnalInterface.f2s(1)
        ##############################################################
        ## check to make sure all user supplied values check out OK ##
        ##############################################################
        self.testForInitErrors(AnalInterface)
        ################
        ## other shit ##
        ################
        self.segmentHash = util.listToCheckSum([
            self.filename, self.segmentStartSec, self.segmentEndSec,
            self.envDb, self.envAttack, self.envDecay, self.envSlope
        ])
        ####################################
        ## get information about envelope ##
        ####################################
        self.envAttackSec = util.getDurationFromValueOrString(
            self.envAttack, self.segmentDurationSec)
        self.envDecaySec = util.getDurationFromValueOrString(
            self.envDecay, self.segmentDurationSec)
        if (self.envAttackSec + self.envDecaySec +
                envAttackenvDecayCushionSec) > self.segmentDurationSec:
            self.envDecaySec = self.segmentDurationSec - self.envAttackSec - envAttackenvDecayCushionSec
        self.envAttackFrames = int(
            round(AnalInterface.s2f(self.envAttackSec, self.filename)))
        self.envDecayFrames = int(
            round(AnalInterface.s2f(self.envDecaySec, self.filename)))
        if (self.envAttackFrames + self.envDecayFrames) > self.lengthInFrames:
            if self.envAttackFrames > self.envDecayFrames:
                self.envAttackFrames = self.lengthInFrames - self.envDecayFrames
            else:
                self.envDecayFrames = self.lengthInFrames - self.envAttackFrames
        if self.envAttackFrames <= 1 and self.envDecayFrames <= 1:
            self.envelopeMask = util.dbToAmp(self.envDb)
        else:
            self.envelopeMask = np.ones(self.lengthInFrames)
            if self.envAttackFrames > 0:
                self.envelopeMask[:self.envAttackFrames] = np.linspace(
                    1 / float(self.envAttackFrames),
                    1,
                    num=self.envAttackFrames)
            if self.envDecayFrames > 0:
                self.envelopeMask[self.envDecayFrames * -1:] = np.linspace(
                    1, 1 / float(self.envDecayFrames), num=self.envDecayFrames)
            self.envelopeMask = np.power(self.envelopeMask, self.envSlope)
            self.envelopeMask *= util.dbToAmp(self.envDb)
        ########################################
        ## manage duration and time-in-frames ##
        ########################################
        self.segmentStartFrame = AnalInterface.getSegmentStartInFrames(
            self.filename, self.segmentStartSec, self.segmentEndSec,
            self.lengthInFrames)

        ###############################
        ## initalise descriptor data ##
        ###############################
        self.desc = descriptordata.container(
            descriptors, self)  # for storing descriptor values from disk
        # tells us which descriptors are:
        #	SdifDescList - loaded from NYC analyses
        #	ComputedDescList - transformed from loaded descriptor data - delta, deltadelta, odf, etc.
        #	AveragedDescList - averaged descriptors from loaded descriptor data
        AnalDescList, ComputedDescList, AveragedDescList = self.desc.getDescriptorOrigins(
        )

        for d in AnalDescList:
            self.desc[d.name] = AnalInterface.getDescriptorForsfsegment(
                self.filename, self.segmentStartFrame, self.lengthInFrames, d,
                self.envelopeMask)

        for dobj in ComputedDescList:
            self.desc[dobj.name] = descriptordata.DescriptorComputation(
                dobj, self, None, None)
        self.triggerinit = False
Exemple #7
0
 def __init__(self,
              filename,
              startSec,
              endSec,
              AnalInterface,
              envDb=+0,
              envAttackSec=0.,
              envDecaySec=0.,
              envSlope=1.,
              envAttackenvDecayCushionSec=0.01,
              normtag='notag'):
     self.filename = util.verifyPath(filename, AnalInterface.searchPaths)
     self.soundfileExtension = os.path.splitext(self.filename)[1]
     self.printName = os.path.split(
         self.filename)[1]  # short name for printing
     # filename-based descriptor info
     self.midipitch = None
     self.midiPitchFromFilename = descriptordata.getMidiPitchFromString(
         self.printName)
     self.rmsAmplitudeFromFilename, self.dynamicFromFilename = descriptordata.getDynamicFromFilename(
         self.printName,
         AnalInterface.dynToDbDict,
         AnalInterface.stringToDynDict,
         notFound=-1000)
     # other info
     self.soundfileTotalDuration, self.soundfileChns = AnalInterface.validateAnalResource(
         self.filename)
     self.segmentStartSec = startSec
     self.segmentEndSec = endSec
     self.envDb = util.getScaleDb(envDb, self)
     self.envAttack = envAttackSec
     self.envDecay = envDecaySec
     self.envSlope = envSlope
     if self.segmentStartSec == None: self.segmentStartSec = 0
     else: self.segmentStartSec = self.segmentStartSec
     if self.segmentEndSec == None:
         self.segmentEndSec = self.soundfileTotalDuration
     else:
         self.segmentEndSec = self.segmentEndSec
     self.segmentDurationSec = self.segmentEndSec - self.segmentStartSec
     # ensure length is at least 1 frame
     self.lengthInFrames = max(
         1,
         AnalInterface.getSegmentFrameLength(self.segmentStartSec,
                                             self.segmentDurationSec,
                                             self.filename))
     self.f2s = AnalInterface.f2s(1)
     ##############################################################
     ## check to make sure all user supplied values check out OK ##
     ##############################################################
     self.testForInitErrors(AnalInterface)
     ################
     ## other shit ##
     ################
     self.segmentHash = util.listToCheckSum([
         self.filename, self.segmentStartSec, self.segmentEndSec,
         self.envDb, self.envAttack, self.envDecay, self.envSlope
     ])
     ####################################
     ## get information about envelope ##
     ####################################
     self.envAttackSec = util.getDurationFromValueOrString(
         self.envAttack, self.segmentDurationSec)
     self.envDecaySec = util.getDurationFromValueOrString(
         self.envDecay, self.segmentDurationSec)
     if (self.envAttackSec + self.envDecaySec +
             envAttackenvDecayCushionSec) > self.segmentDurationSec:
         self.envDecaySec = self.segmentDurationSec - self.envAttackSec - envAttackenvDecayCushionSec
     self.envAttackFrames = int(
         round(AnalInterface.s2f(self.envAttackSec, self.filename)))
     self.envDecayFrames = int(
         round(AnalInterface.s2f(self.envDecaySec, self.filename)))
     if (self.envAttackFrames + self.envDecayFrames) > self.lengthInFrames:
         if self.envAttackFrames > self.envDecayFrames:
             self.envAttackFrames = self.lengthInFrames - self.envDecayFrames
         else:
             self.envDecayFrames = self.lengthInFrames - self.envAttackFrames
     if self.envAttackFrames <= 1 and self.envDecayFrames <= 1:
         self.envelopeMask = util.dbToAmp(self.envDb)
     else:
         self.envelopeMask = np.ones(self.lengthInFrames)
         if self.envAttackFrames > 0:
             self.envelopeMask[:self.envAttackFrames] = np.linspace(
                 1 / float(self.envAttackFrames),
                 1,
                 num=self.envAttackFrames)
         if self.envDecayFrames > 0:
             self.envelopeMask[self.envDecayFrames * -1:] = np.linspace(
                 1, 1 / float(self.envDecayFrames), num=self.envDecayFrames)
         self.envelopeMask = np.power(self.envelopeMask, self.envSlope)
         self.envelopeMask *= util.dbToAmp(self.envDb)
     ########################################
     ## manage duration and time-in-frames ##
     ########################################
     self.segmentStartFrame = AnalInterface.getSegmentStartInFrames(
         self.filename, self.segmentStartSec, self.segmentEndSec,
         self.lengthInFrames)
     ###############################
     ## initalise descriptor data ##
     ###############################
     self.desc = AnalInterface.desc_manager.create_sf_descriptor_obj(
         self,
         AnalInterface.getDescriptorMatrix(self.filename),
         self.segmentStartFrame,
         self.lengthInFrames,
         tag=normtag,
         envelope=self.envelopeMask)
     self.triggerinit = False