예제 #1
0
파일: main.py 프로젝트: robcaulk/Vybe
    def initializeAudio(self):
        global file
        # opens file as wave file
        self.src = file + ".wav"
        samplerate = 0
        self.total_frames = 0

        # initialize aubio data
        self.a_source = aubio.source(self.src, samplerate, self.hop_s)
        self.samplerate = self.a_source.samplerate
        self.p = pyaudio.PyAudio()
        self.format = pyaudio.paFloat32
        self.frames = self.hop_s
        self.channels = 1
        self.p = pyaudio.PyAudio()

        self.a_tempo = aubio.tempo("default", self.win_s, self.hop_s,
                                   self.samplerate)
        self.pitch_o = aubio.pitch("yin", self.win_s, self.hop_s,
                                   self.samplerate)
        self.notes_o = aubio.notes("default", self.win_s, self.hop_s,
                                   self.samplerate)
        self.o = aubio.onset("default", self.win_s, self.hop_s,
                             self.samplerate)
        self.o2 = aubio.onset("hfc", self.win_s, self.hop_s, self.samplerate)

        print("Audio set up for", file)
예제 #2
0
 def _initialize_onset(self):
     self.onset_high = aubio.onset(
         "specflux", self._config['fft_size'],
         self._config['mic_rate'] // self._config['sample_rate'],
         self._config['mic_rate'])
     self.onset_soft = aubio.onset(
         "phase", self._config['fft_size'],
         self._config['mic_rate'] // self._config['sample_rate'],
         self._config['mic_rate'])
     self.onset_mids = aubio.onset(
         "specdiff", self._config['fft_size'],
         self._config['mic_rate'] // self._config['sample_rate'],
         self._config['mic_rate'])
예제 #3
0
    def getOnsetTimes(self):  #returns list of onset times
        win_s = 1024 // 2  # fft size
        hop_s = win_s // 2  # hop size: number of samples between each successive FFT window

        filename = self.path
        samplerate = 44100
        s = source(filename, samplerate,
                   hop_s)  #creates source object (iterable)
        samplerate = s.samplerate

        o = onset("default", win_s, hop_s, samplerate)

        # list of onsets, in samples
        onsets = []

        # total number of frames read
        total_frames = 0
        while True:
            samples, read = s()
            if o(samples):
                onsets.append(float("%f" % o.get_last_s()))

            total_frames += read
            if read < hop_s: break  #reached end of the file
        return onsets
예제 #4
0
 def create_detector(self, samplerate):
     self.onset = aubio.onset(self.method, self.buf_size, self.hop_size,
                              samplerate)
     self.onset.set_minioi_ms(self.minioi_ms)
     self.onset.set_threshold(self.threshold)
     self.onset.set_compression(self.compression)
     self.onset.set_silence(self.silence)
예제 #5
0
def interonset_time(filename):
    win_s = 512  # fft size
    hop_s = win_s // 2  # hop size

    samplerate = 44100

    s = source(filename, samplerate, hop_s)
    samplerate = s.samplerate

    o = onset("default", win_s, hop_s, samplerate)

    # list of onsets, in samples
    onsets = []

    # total number of frames read
    total_frames = 0
    while True:
        samples, read = s()
        if o(samples):
            onsets.append(o.get_last())
        total_frames += read
        if read < hop_s: break

    time = [float(x) / samplerate for x in onsets]

    times = [x - time[i - 1] for i, x in enumerate(time)][1:]

    print(times)

    return time
예제 #6
0
def getOnsets(filename, samplerate=44100):

    win_s = 512  # fft size
    hop_s = win_s // 2  # hop size

    s = source(filename, samplerate, hop_s)

    samplerate = s.samplerate

    o = onset("default", win_s, hop_s, samplerate)

    # list of onsets, in samples
    onsets = []

    # total number of frames read
    total_frames = 0
    while True:
        samples, read = s()
        if o(samples):
            # print("%f" % o.get_last_s())
            onsets.append(o.get_last())
        total_frames += read
        if read < hop_s: break

    return onsets
예제 #7
0
def aubiodata(filename, onsetType, threshold, silence):
    win_s = 512  # fft size
    hop_s = win_s / 2  # hop size
    samplerate = 0  # Just an initial setting
    #	threshold = 0.8
    #	silence = -10

    s = source(filename, samplerate, hop_s)
    samplerate = s.samplerate

    o = onset(onsetType, win_s, hop_s, samplerate)
    o.set_threshold(threshold)
    o.set_silence(silence)
    # list of onsets, in seconds
    onsets = []

    # total number of frames read
    total_frames = 0
    while True:
        samples, read = s()
        if o(samples):
            #        print "%f" % o.get_last_s()
            onsets.append(o.get_last_s())
        total_frames += read
        if read < hop_s: break

    # 'onsets' holds, at each entry, the time where an onset is detected (in seconds).
    return onsets
예제 #8
0
def aubiodata(filename, onsetType, threshold, silence):
	win_s = 512                 # fft size
	hop_s = win_s / 2           # hop size
	samplerate = 0		    # Just an initial setting
#	threshold = 0.8
#	silence = -10

	s = source(filename, samplerate, hop_s)
	samplerate = s.samplerate

	o = onset(onsetType, win_s, hop_s, samplerate)
 	o.set_threshold(threshold)
 	o.set_silence(silence)
	# list of onsets, in seconds
	onsets = []

	# total number of frames read
	total_frames = 0
	while True:
	    samples, read = s()
	    if o(samples):
	#        print "%f" % o.get_last_s()
	        onsets.append(o.get_last_s())
	    total_frames += read
	    if read < hop_s: break


	# 'onsets' holds, at each entry, the time where an onset is detected (in seconds).
	return onsets
예제 #9
0
    def __init__(self):
        self.redis = redis.StrictRedis(host=redishost, port=6379, password="", decode_responses=True)
        self.p = pyaudio.PyAudio()
        stream = self.p.open(format=self.FORMAT,
                        channels=self.CHANNELS,
                        rate=self.RATE,
                        input=True,
                        output=True,
                        input_device_index = self.get_input_device_index(),
                        output_device_index = self.get_output_device_index(),
                        frames_per_buffer = self.CHUNK,
                        stream_callback=self.callback)

        self.a_onset = aubio.onset("default", self.CHUNK, self.hop_s, self.RATE)
        self.a_tempo = aubio.tempo("specflux", self.CHUNK, self.hop_s, self.RATE)
        self.a_pitch = aubio.pitch("default", self.CHUNK, self.hop_s, self.RATE)
        self.a_notes = aubio.notes("default", self.CHUNK, self.hop_s, self.RATE)
        n_filters = 40 # required
        n_coeffs = 13 # I wonder if i made this 1....
        self.a_pvoc = aubio.pvoc(self.CHUNK, self.hop_s)
        self.a_mfcc = aubio.mfcc(self.CHUNK, n_filters, n_coeffs, self.RATE)

        self.tolerance = 0.8
        self.a_pitch.set_tolerance(self.tolerance)
        self.highest_pitch = 0
        self.lowest_pitch = 99999999
        self.average_pitch = 0
        self.average_pitch_samples = 0
        self.last_average = 0
        self.colors = None
        self.pitch_range = None
        self.range_counter = 0
        self.all_notes = set()
        stream.start_stream()
예제 #10
0
    def onSet(self):
        win_s = 512  # fft size
        hop_s = win_s // 2  # hop size

        filename = self.song

        samplerate = 0
        if len(sys.argv) > 2: samplerate = int(sys.argv[2])

        s = source(filename, samplerate, hop_s)
        samplerate = s.samplerate
        o = onset("default", win_s, hop_s, samplerate)

        # list of onsets, in samples
        onsets = []

        # storage for plotted data
        desc = []
        tdesc = []
        allsamples_max = zeros(0, )
        downsample = 2  # to plot n samples / hop_s

        # total number of frames read
        total_frames = 0
        while True:
            samples, read = s()
            if o(samples):
                #print("%f" % (o.get_last_s()))
                self.allOnsets.append(o.get_last_s())
                onsets.append(o.get_last())
            if read < hop_s: break
        for i in range(len(self.allOnsets)):
            self.allOnsets[i] = int(self.allOnsets[i] * 1000)
예제 #11
0
def onset_detector(rate=44100
                   ,window_size=4096
                   ,hop_size=1024
                   ,method="default"
                   ,tolerance=0.7
                   ):
    return onset(method, window_size, hop_size, rate);
예제 #12
0
 def setup(self, channels=None, samplerate=None, blocksize=None, totalframes=None):
     super(AubioOnsetRate, self).setup(channels, samplerate,  blocksize, totalframes)
     self.win_s = 512
     self.hop_s = self.win_s / 2
     self.t = onset("default", self.win_s, self.hop_s, 1)
     self.block_read = 0
     self.onsets = []
예제 #13
0
def segment_from_onsets(filename, dur):
    win_s = 1024
    hop_s = 256
    s = source(filename, 0, hop_s)
    sr = s.samplerate

    o = onset("hfc", win_s, hop_s, sr)
    o.set_threshold(0.2)
    o.set_silence(-30.)
    o.set_minioi_s(dur)
    raw_onsets = []
    chunks = []

    while 1:
        samples, read = s()
        if o(samples):
            raw_onsets.append(o.get_last())
        chunks.append(samples.copy())
        if read < hop_s:
            break

    y = np.concatenate(chunks)
    raw_segments = np.split(y, raw_onsets)[1:]
    segment_len = int(sr * dur)
    segments, onsets = [], []
    for seg, ost in zip(raw_segments, raw_onsets):
        if len(seg) < segment_len:
            pass
        else:
            onsets.append(ost)
            segments.append(seg[:segment_len])
    segments = np.stack(segments)
    segments /= np.abs(segments).max(1, keepdims=True)
    return segments, np.array(onsets) / sr, y, sr
예제 #14
0
파일: data.py 프로젝트: snikolov/russolo
def generate_data_for_file(path, n_samples):
    sample_rate = 0
    aubio_source = source(path, sample_rate, HOP_SIZE)
    sample_rate = aubio_source.samplerate
    aubio_onset = onset('default', FFT_WIN_SIZE, HOP_SIZE, sample_rate)

    onsets = []
    while True:
        chunk, read = aubio_source()
        if aubio_onset(chunk):
            onsets.append(aubio_onset.get_last())

        if read < HOP_SIZE: break

    samples = wavfile.read(path)[1]
    slices = np.split(samples, onsets)
    snippets = []
    for slice in slices:
        if len(slice) == 0:
            continue
        mono = 0.5 * (slice[:, 0] + slice[:, 1])
        mono = mono / np.sqrt(np.dot(mono, mono))
        fixed_len = trim_or_pad(mono, n_samples)
        snippets.append(fixed_len)
    return np.vstack(snippets)
예제 #15
0
def analyzeAudio(file, pickedCloser, pickedShelter, pickedFriends):
    win_s = 512                 # fft size
    hop_s = win_s // 2          # hop size

    filename = file

    samplerate = 44100
    if len( sys.argv ) > 2: samplerate = int(sys.argv[2])

    s = source(filename, samplerate, hop_s)
    samplerate = s.samplerate

    o = onset("default", win_s, hop_s, samplerate)

    # list of onsets, in samples
    onsetFrames = []
    # total number of frames read
    total_frames = 0
    while True:
        samples, read = s()
        if o(samples):
            onsetFrames.append(total_frames)
        total_frames += read
        if read < hop_s: break
    if(pickedCloser):
        closer.onsetFrames = onsetFrames
    elif(pickedShelter):
        shelter.onsetFrames = onsetFrames
    elif(pickedFriends):
        friends.onsetFrames = onsetFrames
예제 #16
0
def getOnset(f):
    s = source(f, 0, hop_s)
    samplerate = s.samplerate

    tolerance = 0.8
    pitch_o = pitch("yin", win_s, hop_s, samplerate)
    pitch_o.set_unit("Hz")
    pitch_o.set_tolerance(tolerance)

    o = onset("default", win_s, hop_s, samplerate)
    notes_o = notes("default", win_s, hop_s, samplerate)

    # list of onsets, in seconds
    onsets = []
    vel = []
    pitches = []

    # total number of frames read
    total_frames = 0
    while True:
        samples, read = s()
        p = pitch_o(samples)[0]
        new_note = notes_o(samples)
        t = total_frames / float(samplerate)

        if o(samples) and p > 0:
            onsets.append(o.get_last_s())
            pitches.append(p)

        if new_note[0] != 0:
            vel.append(new_note[1])
        total_frames += read
        if read < hop_s:
            break
    return onsets, vel, pitches
예제 #17
0
def compute_onsets_aubio(y=None, sr=None, **kwargs):
    """Compute onset detection function with aubio
    """
    if 'method' in kwargs:
        method = kwargs['method']
    else:
        # use aubio default
        method = 'default'

    src = data_load_aubio(filename=kwargs['filename'])

    # list of onsets: sparse, event-based?
    onsets = []
    # onset detector aubio
    onset_detector = aubio.onset(method=method, samplerate=src.samplerate)
    # onset_detector.set_threshold(1.0)

    while True:
        samples, read = src()
        # print(samples.shape)
        if read < src.hop_size:
            break
        onset_detector(samples)
        onsets.append(onset_detector.get_thresholded_descriptor())

    return {'onsets': np.array(onsets), 'src': src}
예제 #18
0
 def setup(self, channels=None, samplerate=None, blocksize=None, totalframes=None):
     super(AubioTemporal, self).setup(channels, samplerate, blocksize, totalframes)
     self.o = onset("default", self.input_blocksize, self.input_stepsize, samplerate)
     self.t = tempo("default", self.input_blocksize, self.input_stepsize, samplerate)
     self.block_read = 0
     self.onsets = []
     self.beats = []
     self.beat_confidences = []
예제 #19
0
 def test_all_methods(self):
     for method in [
             'default', 'energy', 'hfc', 'complexdomain', 'complex',
             'phase', 'wphase', 'mkl', 'kl', 'specflux', 'specdiff',
             'old_default'
     ]:
         o = onset(method=method, buf_size=512, hop_size=256)
         o(fvec(256))
    def __init__(self, config):
        self._chunk = config['perf_chunk']
        self._samp_rate = config['perf_sr']

        self._onset_detector = aubio.onset('default', self._chunk, self._chunk,
                                           self._samp_rate)

        self._result = False  # use boolean to represent onset
예제 #21
0
 def start(self):
     self.stream = self.p.open(format=self.FORMAT,
                               channels=self.CHANNELS,
                               rate=self.RATE,
                               input=True,
                               frames_per_buffer=self.CHUNK,
                               stream_callback=self.get_callback())
     self.stream.start_stream()
     self.o = onset("default", self.win_s, self.hop_s, self.RATE)
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.fps = FPSCounter('Beat Processor')
     self.win_s = 1024
     self.hop_s = self.frames_per_buffer = int(self.config['MIC_RATE'] /
                                               self.config['FPS'])
     self.onset_detect = aubio.onset('energy', self.win_s, self.hop_s,
                                     self.config['MIC_RATE'])
     self.beat_detect = aubio.tempo('hfc', self.win_s, self.hop_s,
                                    self.config['MIC_RATE'])
예제 #23
0
 def _initialize_onset(self):
     self.onset_high = aubio.onset(
         "specflux",
         self._config["fft_size"],
         self._config["mic_rate"] // self._config["sample_rate"],
         self._config["mic_rate"],
     )
     self.onset_soft = aubio.onset(
         "phase",
         self._config["fft_size"],
         self._config["mic_rate"] // self._config["sample_rate"],
         self._config["mic_rate"],
     )
     self.onset_mids = aubio.onset(
         "specdiff",
         self._config["fft_size"],
         self._config["mic_rate"] // self._config["sample_rate"],
         self._config["mic_rate"],
     )
예제 #24
0
 def __init__(self, config):
     super().__init__(config)
     self.config = self.config.get('Beat', {})
     self.win_s = 1024
     self.hop_s = self.frames_per_buffer = int(
         self.capconfig['SampleRate'] / self.capconfig['FPS'])
     self.onset_detect = aubio.onset('energy', self.win_s, self.hop_s,
                                     self.capconfig['SampleRate'])
     self.beat_detect = aubio.tempo('hfc', self.win_s, self.hop_s,
                                    self.capconfig['SampleRate'])
예제 #25
0
 def setup(self,
           channels=None,
           samplerate=None,
           blocksize=None,
           totalframes=None):
     super(AubioTemporal, self).setup(channels, samplerate, blocksize,
                                      totalframes)
     self.o = onset("default", self.input_blocksize, self.input_stepsize,
                    samplerate)
     self.t = tempo("default", self.input_blocksize, self.input_stepsize,
                    samplerate)
예제 #26
0
 def get_tight_score(self):
     o = onset("energy", self.win_s, self.hop_s, self.source.samplerate)
     onsets = []
     total_frames = 0
     while True:
         samples, read = self.source()
         if o(samples):
             onsets.append(o.get_last())
         total_frames += read
         if read < self.hop_s: break
     tsmean = numpy.mean(onsets)
     ts = tsmean / 100000
     print ts
예제 #27
0
def getpitches(filename, samplerate):

	#downsample = 1
	#samplerate = 44100 / downsample	
	win_s = 4096 / downsample # fft size
	hop_s = HOP_SIZE  / downsample # hop size

	s = source(filename, samplerate, hop_s)
	samplerate = s.samplerate

	tolerance = 0.8

	pitch_o = pitch("yin", win_s, hop_s, samplerate)
	pitch_o.set_unit("midi")
	pitch_o.set_tolerance(tolerance)

	o = onset("default", win_s, hop_s, samplerate)
	onsets = []

	pitches = []
	confidences = []
	#number = 0
	# total number of frames read
	total_frames = 0
	while True:
	    samples, read = s()
	    pitch1 = pitch_o(samples)[0]
	    #pitch = int(round(pitch))
	    confidence = pitch_o.get_confidence()
	    if o(samples):
        	# print "%f" % o.get_last_s()
        	onsets.append(o.get_last())
	    #if confidence < 0.8: pitch = 0.
	    #print "%f %f %f" % (total_frames / float(samplerate), pitch, confidence)
	    pitches += [pitch1]
	    confidences += [confidence]
	    total_frames += read
	    #number = number + 1
	    if read < hop_s: break

	if 0: sys.exit(0)

	'''
	print onsets
	print pitches
	print confidences
	'''

	
	return pitches, onsets
예제 #28
0
def initAudio():
    src = aubio.source(VID_FILE_NAME)
    o = aubio.onset(method="specflux",
                    buf_size=src.hop_size * 2,
                    hop_size=src.hop_size,
                    samplerate=src.samplerate)
    onsets = []
    while True:
        samples, read = src()
        if o(samples):
            onsets.append(o.get_last())
        if read < src.hop_size:
            break
    return onsets, src.samplerate
예제 #29
0
 def __init__(self, fs,
              nfft: int = 512,
              hop: int = 256,
              onset_detector_type: str = 'hfc',
              onset_threshold: float = 0.01,
              onset_silence_threshold: float = -90,
              min_duration_s: float = 0.02):
     self.hop = hop
     self.onset_detector = onset(onset_detector_type, nfft, hop, fs)
     if onset_threshold:
         self.onset_detector.set_threshold(onset_threshold)
     if onset_silence_threshold:
         self.onset_detector.set_silence(onset_silence_threshold)
     if min_duration_s:
         self.onset_detector.set_minioi_s(min_duration_s)
예제 #30
0
def points_from_onsets(audio_file_name):
    source = aubio_source(audio_file_name)
    onset = aubio.onset(args.labels_from_onsets_method,
                        samplerate=source.samplerate)
    onset.set_threshold(args.labels_from_onsets_threshold)
    onset.set_minioi_ms(args.labels_from_onsets_min_length * 1000)
    onset.set_silence(args.labels_from_onsets_min_volume)
    points = []
    while True:
        samples, read = source()
        if onset(samples):
            points.append(onset.get_last() / source.samplerate)
        if read < source.hop_size:
            break
    return set(points)
예제 #31
0
파일: cmd.py 프로젝트: aubio/aubio
 def __init__(self, args):
     self.parse_options(args, self.valid_opts)
     self.onset = aubio.onset(**self.options)
     if args.threshold is not None:
         self.onset.set_threshold(args.threshold)
     if args.minioi:
         if args.minioi.endswith('ms'):
             self.onset.set_minioi_ms(float(args.minioi[:-2]))
         elif args.minioi.endswith('s'):
             self.onset.set_minioi_s(float(args.minioi[:-1]))
         else:
             self.onset.set_minioi(int(args.minioi))
     if args.silence:
         self.onset.set_silence(args.silence)
     super(process_onset, self).__init__(args)
예제 #32
0
 def __init__(self, args):
     self.parse_options(args, self.valid_opts)
     self.onset = aubio.onset(**self.options)
     if args.threshold is not None:
         self.onset.set_threshold(args.threshold)
     if args.minioi:
         if args.minioi.endswith('ms'):
             self.onset.set_minioi_ms(float(args.minioi[:-2]))
         elif args.minioi.endswith('s'):
             self.onset.set_minioi_s(float(args.minioi[:-1]))
         else:
             self.onset.set_minioi(int(args.minioi))
     if args.silence:
         self.onset.set_silence(args.silence)
     super(process_onset, self).__init__(args)
예제 #33
0
def getpitches(filename, samplerate):

    #downsample = 1
    #samplerate = 44100 / downsample
    win_s = 4096 / downsample  # fft size
    hop_s = HOP_SIZE / downsample  # hop size

    s = source(filename, samplerate, hop_s)
    samplerate = s.samplerate

    tolerance = 0.8

    pitch_o = pitch("yin", win_s, hop_s, samplerate)
    pitch_o.set_unit("midi")
    pitch_o.set_tolerance(tolerance)

    o = onset("default", win_s, hop_s, samplerate)
    onsets = []

    pitches = []
    confidences = []
    #number = 0
    # total number of frames read
    total_frames = 0
    while True:
        samples, read = s()
        pitch1 = pitch_o(samples)[0]
        #pitch = int(round(pitch))
        confidence = pitch_o.get_confidence()
        if o(samples):
            # print "%f" % o.get_last_s()
            onsets.append(o.get_last())
        #if confidence < 0.8: pitch = 0.
        #print "%f %f %f" % (total_frames / float(samplerate), pitch, confidence)
        pitches += [pitch1]
        confidences += [confidence]
        total_frames += read
        #number = number + 1
        if read < hop_s: break

    if 0: sys.exit(0)
    '''
	print onsets
	print pitches
	print confidences
	'''

    return pitches, onsets
예제 #34
0
def _cut_analyze(options):
    hopsize = options.hop_size
    bufsize = options.buf_size
    samplerate = options.samplerate
    source_uri = options.source_uri

    # analyze pass
    from aubio import onset, tempo, source

    s = source(source_uri, samplerate, hopsize)
    if samplerate == 0:
        samplerate = s.samplerate
        options.samplerate = samplerate

    if options.beat:
        o = tempo(options.onset_method,
                  bufsize,
                  hopsize,
                  samplerate=samplerate)
    else:
        o = onset(options.onset_method,
                  bufsize,
                  hopsize,
                  samplerate=samplerate)
        if options.minioi:
            if options.minioi.endswith('ms'):
                o.set_minioi_ms(int(options.minioi[:-2]))
            elif options.minioi.endswith('s'):
                o.set_minioi_s(int(options.minioi[:-1]))
            else:
                o.set_minioi(int(options.minioi))
    o.set_threshold(options.threshold)

    timestamps = []
    total_frames = 0
    while True:
        samples, read = s()
        if o(samples):
            timestamps.append(o.get_last())
            if options.verbose:
                print("%.4f" % o.get_last_s())
        total_frames += read
        if read < hopsize:
            break
    del s
    return timestamps, total_frames
예제 #35
0
def onsets_aubio(samples: np.ndarray,
                 sr: int,
                 method='mkl',
                 winsize=1024,
                 hopsize=512,
                 threshold=0.03,
                 mingap=0.050,
                 silencedb=-70) -> List[float]:
    """
    Detect onsets in samples

    Args:
        samples: the samples, as numpy array (1D), between -1 and 1
        sr: the sample rate of samples
        winsize: the size of the fft window size, in samples
        hopsize: the hop size, in samples
        threshold: depends on the method. The lower this value, the more probable
            is it that an onset is detected
        method: the method to detect onsets. One of:
            - `energy`: local energy,
            - `hfc`: high frequency content,
            - `complex`: complex domain,
            - `phase`: phase-based method,
            - `wphase`: weighted phase deviation,
            - `specdiff`: spectral difference,
            - `kl`: Kullback-Liebler,
            - `mkl`: modified Kullback-Liebler,
            - `specflux`: spectral flux.
        mingap: the min. amount of time (in seconds) between two onsets
        silencedb: onsets will only be detected if the amplitude exceeds this value (in dB)

    Returns:
        a list of floats, representing the times of the offsets
    """
    assert isinstance(samples, np.ndarray) and len(samples.shape) == 1
    import aubio
    ao = aubio.onset(method, buf_size=winsize, hop_size=hopsize)
    ao.set_threshold(threshold)
    ao.set_silence(silencedb)
    ao.set_minioi_s(mingap)
    samples = samples.astype('float32')
    onsets = [
        ao.get_last() / sr for chunk in chunks(samples, hopsize, padwith=0.0)
        if ao(chunk)
    ]
    return onsets
예제 #36
0
	def get_onsets(self, song_file):
		win_s = 512
		hop_s = win_s // 2
		filename = song_file
		samplerate = 0
		s = source(filename, samplerate, hop_s)
		samplerate = s.samplerate
		o = onset("default", win_s, hop_s, samplerate)
		onsets = []
		total_frames = 0
		while True:
			samples, read = s()
			if o(samples):
				onsets.append(o.get_last_s())
			total_frames += read
			if read < hop_s: break
		return onsets
예제 #37
0
파일: cut.py 프로젝트: aubio/aubio
def _cut_analyze(options):
    hopsize = options.hop_size
    bufsize = options.buf_size
    samplerate = options.samplerate
    source_uri = options.source_uri

    # analyze pass
    from aubio import onset, tempo, source

    s = source(source_uri, samplerate, hopsize)
    if samplerate == 0:
        samplerate = s.samplerate
        options.samplerate = samplerate

    if options.beat:
        o = tempo(options.onset_method, bufsize, hopsize,
                samplerate=samplerate)
    else:
        o = onset(options.onset_method, bufsize, hopsize,
                samplerate=samplerate)
        if options.minioi:
            if options.minioi.endswith('ms'):
                o.set_minioi_ms(int(options.minioi[:-2]))
            elif options.minioi.endswith('s'):
                o.set_minioi_s(int(options.minioi[:-1]))
            else:
                o.set_minioi(int(options.minioi))
    o.set_threshold(options.threshold)

    timestamps = []
    total_frames = 0
    while True:
        samples, read = s()
        if o(samples):
            timestamps.append(o.get_last())
            if options.verbose:
                print("%.4f" % o.get_last_s())
        total_frames += read
        if read < hopsize:
            break
    del s
    return timestamps, total_frames
예제 #38
0
파일: test_onset.py 프로젝트: aubio/aubio
    def test_get_methods(self):
        o = onset(method='default', buf_size=512, hop_size=256)

        assert o.get_silence() == -70
        o.set_silence(-20)
        assert_almost_equal(o.get_silence(), -20)

        assert o.get_compression() == 1
        o.set_compression(.99)
        assert_almost_equal(o.get_compression(), .99)

        assert o.get_awhitening() == 0
        o.set_awhitening(1)
        assert o.get_awhitening() == 1

        o.get_last()
        o.get_last_ms()
        o.get_last_s()
        o.get_descriptor()
        o.get_thresholded_descriptor()
예제 #39
0
def detect_onset(sig, fs):
    """ Detect the onset of sound event
    Returns: (onset, offset) tuple of indecies """
    win_s = 512          # fft size
    hop_s = win_s / 2    # hop size

    o = onset("hfc", win_s, hop_s, fs)

    # list of onsets, in samples
    onsets = []

    # storage for plotted data
    desc = []
    tdesc = []
    allsamples_max = np.zeros(0,)
    downsample = 2  # to plot n samples / hop_s

    # total number of frames read
    total_frames = 0
    i = 0
    while i + hop_s < len(sig[:,0]):
        samples = sig[i:i+hop_s][:,0] # LEFT CHANNEL!
        if o(samples):
            # print "%f" % (o.get_last_s())
            # onsets.append((o.get_last(), samples))
            onsets.append(o.get_last())

        # keep some data to plot it later
        new_maxes = (abs(samples.reshape(hop_s/downsample, downsample))).max(axis=0)
        allsamples_max = np.hstack([allsamples_max, new_maxes])
        desc.append(o.get_descriptor())
        tdesc.append(o.get_thresholded_descriptor())
        total_frames += hop_s
        i += hop_s

    allsamples_max = (allsamples_max > 0) * allsamples_max
    allsamples_max_times = [float(t) * hop_s / downsample / fs for t in range(len(allsamples_max)) ]

    # this is the onset corr. to the peak in energy in signal
    max_energy_onset = int(float(np.argmax(allsamples_max)) * hop_s / downsample)
    return min(onsets, key=lambda x:abs(x - max_energy_onset))
예제 #40
0
    def calculate_onsets(self, sample, sample_rate):
        """
        Calculate onsets of the given signal

        Parameters
        ----------
        sample : 1-d array
            Single-channel audio sample.
        sample_rate : int
            Sample rate in Hz.

        Returns
        -------
        out : list
            Calculated onsets

        """
        # Pad with zeros
        filler = self._hop_size - (len(sample) % self._hop_size)  # number of zeros
        sample = np.pad(sample, (0, filler), 'constant')  # padding

        # Configure onsets' detector
        onset_detector = onset(self._type, self._window_size, self._hop_size, sample_rate)
        onset_detector.set_threshold(self._threshold)
        onset_detector.set_minioi_s(0.5)
        # onset_detector.set_delay_s(1.0)

        # Calculate onsets
        onsets = []
        windowed_sample = np.array_split(sample, np.arange(self._hop_size, len(sample), self._hop_size))
        for frame in windowed_sample:
            if onset_detector(frame.astype('float32')):
                onsets.append(onset_detector.get_last())

        early_onsets = [value for value in onsets if value > sample_rate * 0.5 and value < 2 * sample_rate]
        if not early_onsets:
            onsets = [value for value in onsets if value > self._window_size]

        return onsets
예제 #41
0
파일: audio_input.py 프로젝트: menthas/prmd
    def __init__(self, sample_rate=None, buffersize=None):
        self.sample_rate = sample_rate or self.sample_rate
        self.buffersize = buffersize or self.buffersize
        self.window_size = self.buffersize * 2
        self.stream = None

        self.onset = aubio.onset(
            'specflux', self.window_size, self.buffersize, self.sample_rate)
        self.onset.set_threshold(0.3)
        self.onset.set_silence(-20.)
        self.tempo = aubio.tempo(
            'default', self.window_size, self.buffersize, self.sample_rate)

        self.energy = aubio.specdesc('specflux', self.buffersize * 2)
        self.pv = aubio.pvoc(self.buffersize * 2, self.buffersize)

        self.pitch = aubio.pitch(
            "yinfft", self.window_size, self.buffersize, self.sample_rate)
        self.pitch.set_unit("midi")
        self.pitch.set_tolerance(0.8)

        self.py_audio = pyaudio.PyAudio()
예제 #42
0
def get_onset_times(filepath, sr=22050, win_s = 512,
		method='default'):
	"""
	INPUT:
	filepath - path to audio file
	sr - sample rate
	win_s - fft window size
	method - see http://aubio.org/manpages/latest/aubionotes.1.html for methods offered

	OUTPUT:
	onset_times - array_type - times for when notes are believed to begin
	"""
	hop_s = win_s / 2           # hop size

	filename = filepath
	samplerate = sr

	#set up the aubio system calls
	s = source(filename, samplerate, hop_s)
	samplerate = s.samplerate

	o = onset(method, win_s, hop_s, samplerate)

	# list of onsets, in samples
	onsets = []

	# total number of frames read
	total_frames = 0
	while True:
		samples, read = s()
		if o(samples):
			#print "%f" % o.get_last_s()
			onsets.append(o.get_last())
		total_frames += read
		if read < hop_s: break

	#convert the onset samples to the corresponding time of occurrence and return
	return librosa.core.samples_to_time(onsets, sr=samplerate)
예제 #43
0
def track_energy():
    """Track attacks of instrument, maintain global float energy"""

    global energy
    energy = 0.25

    win_size = 512                 # fft size
    hop_size = 256
    s = Stream(block_length = hop_size)

    o = onset("default", win_size, hop_size, s.sample_rate)


    come_up_steps = ceil(come_up_secs * s.sample_rate/hop_size)
    built_energy = 0
    built_steps = 0

    onsets = 0
    

    s.start()
    while True:
        vec = s.read(hop_size)
        # mix down to mono
        mono_vec = vec.sum(-1) / float(s.input_channels)
        if o(mono_vec):
            print "beat" + str(onsets)
            onsets += 1
            built_energy = (nu * (energy + built_steps * built_energy) + (1-nu) - energy)/(built_steps+come_up_steps)
            built_steps += come_up_steps
        if built_steps == 0 :
            energy = (1-eta) * energy
        else:
            energy += built_energy
            built_steps -= 1
#        print "energy = %f, total = %f" % (energy,energy + built_energy * built_steps)
    s.stop()
예제 #44
0
    def calculate_onsets(self, sample, sample_rate):
        """ 
        Calculate onsets of the given signal
        
        Parameters
        ----------
        sample : 1-d array
            Single-channel audio sample.
        sample_rate : int
            Sample rate in Hz.

        Returns
        -------
        out : list
            Calculated onsets
    
        """
        # Pad with zeros      
        filler = self._hop_size - (len(sample) % self._hop_size) # number of zeros
        sample = np.pad(sample, (0, filler), 'constant') # padding
        
        # Configure onsets' detector
        onset_detector = onset(self._type, self._window_size, self._hop_size, sample_rate)
        onset_detector.set_threshold(self._threshold)        
        
        # Calculate onsets
        onsets = []
        windowed_sample = np.array_split(sample, np.arange(self._hop_size, len(sample), self._hop_size))
        for frame in windowed_sample:
            if onset_detector(frame.astype('float32')):
                onsets.append(onset_detector.get_last())
        
        # Discard artifact - somehow always onset is detected at zero
        if (len(onsets) > 0 and onsets[0] == 0):
            onsets.pop(0)
            
        return onsets
예제 #45
0
def get_onset(name):
	win_s = 512                 # fft size
	hop_s = win_s / 2           # hop size

	filename = name

	samplerate = 0

	s = source(filename, samplerate, hop_s)
	samplerate = s.samplerate

	o = onset("default", win_s, hop_s, samplerate)

	# total number of frames read
	total_frames = 0
	while True:
	    samples, read = s()
	    if o(samples):
	        #print "%f" % o.get_last_s()
	        onsets.append(round(o.get_last_s(),4))
	    total_frames += read
	    if read < hop_s: break

	return onsets
예제 #46
0
파일: test_onset.py 프로젝트: aubio/aubio
 def test_members(self):
     o = onset()
     assert_equal ([o.buf_size, o.hop_size, o.method, o.samplerate],
         [1024,512,'default',44100])
예제 #47
0
win_s = 512                 # fft size
hop_s = win_s / 2           # hop size

if len(sys.argv) < 2:
    print "Usage: %s <filename> [samplerate]" % sys.argv[0]
    sys.exit(1)

filename = sys.argv[1]

samplerate = 0
if len( sys.argv ) > 2: samplerate = int(sys.argv[2])

s = source(filename, samplerate, hop_s)
samplerate = s.samplerate

o = onset("default", win_s, hop_s, samplerate)

# list of onsets, in samples
onsets = []

# total number of frames read
total_frames = 0
while True:
    samples, read = s()
    if o(samples):
        print "%f" % o.get_last_s()
        onsets.append(o.get_last())
    total_frames += read
    if read < hop_s: break
#print len(onsets)
예제 #48
0
파일: test_onset.py 프로젝트: aubio/aubio
 def test_all_methods(self):
     for method in ['default', 'energy', 'hfc', 'complexdomain', 'complex',
             'phase', 'wphase', 'mkl', 'kl', 'specflux', 'specdiff',
             'old_default']:
         o = onset(method=method, buf_size=512, hop_size=256)
         o(fvec(256))
예제 #49
0
    options, args = parse_args()

    hopsize = options.hopsize
    bufsize = options.bufsize
    samplerate = options.samplerate
    source_file = options.source_file

    from aubio import onset, tempo, source, sink

    s = source(source_file, samplerate, hopsize)
    if samplerate == 0: samplerate = s.get_samplerate()

    if options.beat:
        o = tempo(options.onset_method, bufsize, hopsize)
    else:
        o = onset(options.onset_method, bufsize, hopsize)
        if options.minioi:
            if options.minioi.endswith('ms'):
                o.set_minioi_ms(int(options.minioi[:-2]))
            elif options.minioi.endswith('s'):
                o.set_minioi_s(int(options.minioi[:-1]))
            else:
                o.set_minioi(int(options.minioi))
    o.set_threshold(options.threshold)

    timestamps = []
    total_frames = 0
    # analyze pass
    while True:
        samples, read = s()
        if o(samples):
예제 #50
0
 def get_detector(self):
     detector = aubio.onset(self.method, self.winsize, self.hopsize,
                            self.samplerate)
     detector.set_threshold(self.threshold)
     detector.set_silence(self.silence)
     return detector
예제 #51
0
파일: test_onset.py 프로젝트: aubio/aubio
 def setUp(self):
     self.o = onset(samplerate = self.samplerate)
예제 #52
0
def create_onset_alg(onset_method="default", onset_buffer_size=512,
                     onset_hop_size=256, onset_samplerate=44100,
                     onset_threshold=0.):
    onset_alg = aubio.onset(onset_method, onset_buffer_size, onset_hop_size, onset_samplerate)
    onset_alg.set_threshold(onset_threshold)
    return onset_alg