コード例 #1
0
ファイル: analyse.py プロジェクト: jgoconnell/hmosaic
    def analyse_audio(self,audio_filepath):
        """
            This function invokes the essentia binary.
            Reads in the output file, deletes the file 
            and returns a dictionary.
            
        """
    
        #We actuallly have to be in the same directory as the streaming
        # extracor in order to make this work....
        current_dir = os.getcwd()
        os.chdir(settings.ESSENTIA_BIN_DIR)
        
        command = [self.ESSENTIA_BIN, audio_filepath, \
            switch_ext(audio_filepath, '.yaml')]
        process = subprocess.Popen(command, stdout=subprocess.PIPE, \
        stderr = subprocess.PIPE)
        (stdout, stderr) = process.communicate()
        log.debug('%s \n %s' % (stdout, stderr))
        pat = re.compile('ERROR.+$')
        match = pat.search(stdout)
        
        os.chdir(current_dir)

        if match:
            raise EssentiaError(match.group())
        # Change this to return the analysis filepath!!
        return audio_filepath
コード例 #2
0
ファイル: convertAudio.py プロジェクト: jgoconnell/hmosaic
def rename_wavs():
    """
        Short utility script to rename the wav files.
    """
    files = [f for f in glob('*.wav')]
    for af in files:
        log.debug("Found a wav file: '%s'" % af)
        nf = strip_all(af)
        os.rename(af, nf)
コード例 #3
0
ファイル: models.py プロジェクト: jgoconnell/hmosaic
    def normalise(self, factor=0.99):
        """
            Normalise the data array and scale by ``factor``
            This operation acts on the ``data`` attribute of the mosaic, so
            it should be performed after crossfading and timestretching.

        """
        log.debug("Normalising audio and scaling by a factor of: %f" % factor)
        self.data = factor * (self.data/max(self.data))
コード例 #4
0
ファイル: convertAudio.py プロジェクト: jgoconnell/hmosaic
def execute_flac_convert():
    """
    Cycles through test_data, converting all flac to wav
    Script includes a utility remove spaces and problem 
    characters from file name 
    """
    files = [f for f in glob('*.flac')]

    for af in files:
        x = flacread(af)[0]
        log.debug("Found a flac file: '%s'" % af)
        n = switch_ext(strip_all(af), '.wav')
        print ("Converting '%s' to: '%s'" % (af, n))
        wavwrite(x, n, 44100)
コード例 #5
0
ファイル: models.py プロジェクト: jgoconnell/hmosaic
 def crossfade(self, overlap=50):
     """
         Apply fade in and FADE OUT AND overlap the results.
         
         New approach applies a fade to the first and last 'overlap' ms.
        
     """
     log.debug("Sample rate is %f" % self.sample_rate)
     log.debug("Overlap in ms: %d" % overlap)
      
     overlap = secs_to_samps(float(overlap)/1000.0, self.sample_rate)
     log.debug("Overlap in samples is: %f" % overlap)
     if overlap == 0:
         #self._make_data(unit for unit in self.units)
         return
     fadein = np.linspace(0, 1, overlap)
     fadeout = np.linspace(1, 0, overlap)
     new_units = []
     for index, unit in enumerate(self.units):
         if len(unit.data) < overlap:
             overlap = len(unit.data)
             log.debug("Overlap too large - %d , using length of unit instead: %s" 
                 % (overlap, len(unit.data)))
         unit.data[:overlap] = unit.data[:overlap] * fadein
         unit.data[-overlap:] = unit.data[-overlap:] * fadeout
         if index == 0:
             new_units.append(DataUnit(unit.data))
         else:
             new_units[-1].data[-overlap:] += unit.data[:overlap]
             new_units.append(DataUnit(unit.data[overlap:]))
         #win = np.hamming(len(unit.data))
         #new_units.append(DataUnit(unit.data*win))
     self.data = self._make_data(new_units)
コード例 #6
0
ファイル: convertAudio.py プロジェクト: jgoconnell/hmosaic
def execute_mp3_convert():
    """
    Cycles through test_data, converting all mp3 to wav
    Script includes a utility remove spaces and problem 
    characters from file name.
    **WARNING** - This routine uses ffmpeg to convert the mp3s.
    It will fail if ffmpeg is not installed *or* if ffmpeg is installed
    without mp3 support.
    
    """
    files = [f for f in glob('*.mp3')]

    for af in files:
        log.debug("Found an mp3 file: '%s'" % af)
        # Initial step is rename as shell command is a little fussy
        nf = strip_all(af)
        os.rename(af, nf)
        n = switch_ext(nf, '.wav')
        log.info("Converting '%s' to: '%s'" % (nf, n))
        os.system("ffmpeg -i %s %s" % (nf, n))
コード例 #7
0
def highlevel_mosaic(target, tcorpus, scorpus, scope=5):
    """
        This will be used to test the highlevel mosaicing process.
        The scope variable controls the number of results which are returned 
        for each target unit which is sought.

    """
    # Create a temporary file for the mosaic audio
    filepath = os.path.join(os.getcwd(), 'temp_mosaic.wav')
    if os.path.isfile(filepath):
        os.remove(filepath)
    mosaic = Mosaic(filepath)
    cost = RepeatUnitCost()
    context = Context()
    gridder = Gridder()
    units = tcorpus.list_audio_units(audio_filename=target, chop='highlevel')
    hdb = scorpus.get_gaia_unit_db(chop='highlevel_%s' % self.chop)
    distance = get_mood_distance(hdb)
    v = View(hdb, distance)
    results = {}
    for f in units:
        p = Point()
        p.load(switch_ext(f, '.yaml'))
        unit_name = switch_ext(os.path.basename(f), '')
        p.setName(unit_name)
        p_m = hdb.history().mapPoint(p)
        results.update({f:v.nnSearch(p_m).get(scope)})
    log.debug("Ok, now we have a dict with each target segment, along with its corresponding nearest matches in source db")
    log.debug("Check to see that we have every second of target audio accounted for - I think not!") 
    #return results
    #new_results = results.copy()
    ds = DataSet()
    for r in results:
        units = []
        for u in results[r]:
            ds.load(switch_ext(u[0], '.db'))
            for n in ds.pointNames():
                units.append(n)
        new_ds = gaia_transform(dict(zip(units, units)))
        results.update({r:new_ds})
    #return results
    # Very important - target units must be in correct order
    index = 0
    index_skip = 0
    for r in sorted(results.keys()):
        tds = DataSet()
        tds.load(switch_ext(r, '.db'))
        #return tds, results
        sds = results[r]
        source_set = set(sds.layout().descriptorNames())
        target_set = set(tds.layout().descriptorNames())
        remove_from_source = source_set.difference(target_set)
        remove_from_target = target_set.difference(source_set)
        if len(remove_from_source) > 0:
            log.debug("Will try to remove %s from the source DataSet" % remove_from_source)
            try:
                sds = transform(results[r], 'remove', {'descriptorNames':list(remove_from_source)})
            except Exception, e:
                log.error("Failed to remove %s from source DataSet" % list(remove_from_source))
                return results[r], tds
        if len(remove_from_target) > 0:
            log.debug("Will try to remove %s from the target DataSet" % remove_from_source)
            try:
                tds = transform(tds, 'remove', {'descriptorNames':list(remove_from_target)})
            except Exception, e:
                log.error("Failed to remove %s from target DataSet" % list(remove_from_target))
                return results[r], tds
コード例 #8
0
def process_highlevel(corpus, filepath, chop):
    """
        Utility method used to test the hierarchical system and create
        high level segments along with analysis and constituent units
        stored in a gaia dataset for a given ``corpus`` and file from that 
        ``corpus`` .
      
    """
    units = corpus.list_audio_units(audio_filename=filepath, chop=chop)
    new_segments = []
    m = Mosaic()
    analyser = EssentiaAnalyser()
    for u in units:
        if not os.path.isfile(switch_ext(u, '.yaml')):
            log.error("Cannot find analysis, assume that this file is silent: '%s'" % u)
            continue
        m.add_unit(MosaicUnit(u))
        log.debug("Number of units in mosaic is: %d, length of mosaic is %d" % (len(m.units), m.length))
        # This AND clause prevents a Gaia Dataset of only 1 Point being created.
        # The problem is that if the dataset only has one point then all 
        # descriptors get removed during 'cleaner' analysis
        if m.length > 5 and len(m.units) > 1: 
            log.debug("Current length of mosaic is %f, adding to list" % m.length)
            new_segments.append(m)
            m = Mosaic()
    if len(new_segments) == 0:
        log.warn("Retrieved only 1 high level mosaic unit of length: %f" % m.length)
        new_segments.append(m)
    if m != new_segments[-1]:
        log.debug("The final mosaic is of length %f only" % new_segments[-1].length)
        log.debug("The last mosaic has a length of: %f" % m.length)
        m.merge_mosaics(new_segments[-1])
        new_segments[-1] = m
    log.debug("Finished assembling units into segments of > 5s")
    log.debug("There are %d segments in total to be processed for %s" % (len(new_segments), os.path.basename(filepath)))
    highlevel_dir = corpus._make_segments_dir(filepath, 'highlevel_%s' % chop)
    for index, seg in enumerate(new_segments):
        path = os.path.join(highlevel_dir, '%05d.wav' % index)
        seg.export(path)
        log.debug("Analysing audio: %s" % path)
        analyser.analyse_audio(path)
        unit_dict = {}
        log.debug("Segment has %d units" % len(seg.units))
        for unit in seg.units:
            #path_comps = os.path.split(os.path.dirname(unit.filepath))
            #chop_dir = path_comps[1]
            #name = os.path.split(path_comps[0])[1] + '_' + chop_dir + \
            #    '_' + switch_ext(os.path.basename(unit.filepath), '')
            unit_dict.update({switch_ext(unit.filepath, '.yaml'): switch_ext(unit.filepath, '.yaml')})
        tu_ds = gaia_transform(unit_dict)
        tu_ds.save(os.path.join(highlevel_dir, '%05d.db' % index))
コード例 #9
0
            log.debug("Will try to remove %s from the source DataSet" % remove_from_source)
            try:
                sds = transform(results[r], 'remove', {'descriptorNames':list(remove_from_source)})
            except Exception, e:
                log.error("Failed to remove %s from source DataSet" % list(remove_from_source))
                return results[r], tds
        if len(remove_from_target) > 0:
            log.debug("Will try to remove %s from the target DataSet" % remove_from_source)
            try:
                tds = transform(tds, 'remove', {'descriptorNames':list(remove_from_target)})
            except Exception, e:
                log.error("Failed to remove %s from target DataSet" % list(remove_from_target))
                return results[r], tds

        sv = View(sds, get_low_level_distance(sds))
        log.debug("Beginning to loop through units for this segment")
        #return tds, sds, gridder, sv
        for pname in sorted(tds.pointNames()):
            """
            I don't think I need to map the point in this instance 
            as we have already transformed the DataSet.
            try, analyse_corpus:
                p_m = sds.history().mapPoint(p)
            except Exception, e:
                log.error("Error mapping %s to %s: %s" % (p, sds, e))
                return tds, p, sds
            """
            print pname
            if os.path.basename(pname) != '%07d.yaml' % (index + index_skip):
                log.error("Current unit is %s => Missing a unit '%07d.yaml'- it must be silent... Index is %d, index skip is %d" % (pname, (index + index_skip), index, index_skip))
                u = MosaicUnit(os.path.join(os.path.dirname(pname), '%07d.wav' % (index + index_skip)))