view experiment-reverb/code/onsetdetection.py @ 2:c87a9505f294 tip

Added LICENSE for code, removed .wav files
author Emmanouil Theofanis Chourdakis <e.t.chourdakis@qmul.ac.uk>
date Sat, 30 Sep 2017 13:25:50 +0100
parents 246d5546657c
children
line wrap: on
line source
# -*- coding: utf-8 -*-
"""
Spectral Constrast as described in Dan-Ning Jiang et al (2002)

Created on Tue Apr 14 17:31:20 2015

@author: Emmanouil Theofanis Chourdakis


"""

import essentia
from essentia.streaming import *
from essentia.standard import YamlOutput
from sys import argv
from pylab import *
from numpy import *




if __name__=="__main__":
    if len(argv) < 2:
        print("Please provide an input filename.")
    else:
        fname = argv[1]
        print("Onset Demo with `%s\'" % fname)
        
        SR = 16000.0 # Sampling Rate
        T = 1./SR # Sampling Period
        FS = 23.0 # ms
        
        # Describing the building blocks
        
        loader = MonoLoader(filename = fname, sampleRate = SR)
        
        onset = OnsetRate()
        
        # Pool (for output purposes)

        pool = essentia.Pool()          
        
        
        # Describing the connections
        
        
        loader.audio  >> onset.signal
        loader.audio >> (pool, 'audio')
        onset.onsetRate >> None
        onset.onsetTimes >> (pool, 'onsettimes')
        
        
        # Run it
        
        essentia.run(loader)
        
        
        # Save the pool as a yaml file.
        output = YamlOutput(filename = "%s.yaml" % fname.partition('/')[2].partition('.')[0])(pool)
        

        ons = zeros((len(pool['audio']),1))
        
        for i in range(0,len(pool['onsettimes'])):
            ons[pool['onsettimes'][i]*44100]= 1
            
            
        t = linspace(0, len(pool['audio'])*T, len(pool['audio']))           
        
        plot(t, pool['audio'], t, ons);
        xlabel('time (s)')
        ylabel('f(t)')
        title('Onset Detection Function')
        savefig('output.png',  dpi=600)
        

        show()