Changeset 633400d for python/demos
- Timestamp:
- Dec 5, 2018, 10:34:39 PM (6 years ago)
- Branches:
- feature/cnn, feature/crepe, feature/pitchshift, feature/timestretch, fix/ffmpeg5, master
- Children:
- 283a619a
- Parents:
- 5b46bc3 (diff), f19db54 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- python/demos
- Files:
-
- 7 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
python/demos/demo_bpm_extract.py
r5b46bc3 r633400d 4 4 from numpy import median, diff 5 5 6 def get_file_bpm(path, params =None):6 def get_file_bpm(path, params=None): 7 7 """ Calculate the beats per minute (bpm) of a given file. 8 8 path: path to the file … … 11 11 if params is None: 12 12 params = {} 13 try: 14 win_s = params['win_s'] 15 samplerate = params['samplerate'] 16 hop_s = params['hop_s'] 17 except KeyError: 18 """ 19 # super fast 20 samplerate, win_s, hop_s = 4000, 128, 64 21 # fast 22 samplerate, win_s, hop_s = 8000, 512, 128 23 """ 24 # default: 25 samplerate, win_s, hop_s = 44100, 1024, 512 13 # default: 14 samplerate, win_s, hop_s = 44100, 1024, 512 15 if 'mode' in params: 16 if params.mode in ['super-fast']: 17 # super fast 18 samplerate, win_s, hop_s = 4000, 128, 64 19 elif params.mode in ['fast']: 20 # fast 21 samplerate, win_s, hop_s = 8000, 512, 128 22 elif params.mode in ['default']: 23 pass 24 else: 25 raise ValueError("unknown mode {:s}".format(params.mode)) 26 # manual settings 27 if 'samplerate' in params: 28 samplerate = params.samplerate 29 if 'win_s' in params: 30 win_s = params.win_s 31 if 'hop_s' in params: 32 hop_s = params.hop_s 26 33 27 34 s = source(path, samplerate, hop_s) … … 45 52 break 46 53 47 # Convert to periods and to bpm 48 if len(beats) > 1: 49 if len(beats) < 4: 50 print("few beats found in {:s}".format(path)) 51 bpms = 60./diff(beats) 52 b = median(bpms) 53 else: 54 b = 0 55 print("not enough beats found in {:s}".format(path)) 56 return b 54 def beats_to_bpm(beats, path): 55 # if enough beats are found, convert to periods then to bpm 56 if len(beats) > 1: 57 if len(beats) < 4: 58 print("few beats found in {:s}".format(path)) 59 bpms = 60./diff(beats) 60 return median(bpms) 61 else: 62 print("not enough beats found in {:s}".format(path)) 63 return 0 64 65 return beats_to_bpm(beats, path) 57 66 58 67 if __name__ == '__main__': 59 import sys 60 for f in sys.argv[1:]: 61 bpm = get_file_bpm(f) 68 import argparse 69 parser = argparse.ArgumentParser() 70 parser.add_argument('-m', '--mode', 71 help="mode [default|fast|super-fast]", 72 dest="mode", default='default') 73 parser.add_argument('sources', 74 nargs='+', 75 help="input_files") 76 args = parser.parse_args() 77 for f in args.sources: 78 bpm = get_file_bpm(f, params = args) 62 79 print("{:6s} {:s}".format("{:2f}".format(bpm), f)) -
python/demos/demo_filter.py
r5b46bc3 r633400d 1 1 #! /usr/bin/env python 2 2 3 import sys 4 import os.path 5 import aubio 3 6 4 def apply_filter(path):5 from aubio import source, sink, digital_filter6 from os.path import basename, splitext7 7 8 def apply_filter(path, target): 8 9 # open input file, get its samplerate 9 s = source(path)10 s = aubio.source(path) 10 11 samplerate = s.samplerate 11 12 12 13 # create an A-weighting filter 13 f = digital_filter(7)14 f = aubio.digital_filter(7) 14 15 f.set_a_weighting(samplerate) 15 # alternatively, apply another filter16 16 17 17 # create output file 18 o = sink("filtered_" + splitext(basename(path))[0] + ".wav", samplerate)18 o = aubio.sink(target, samplerate) 19 19 20 20 total_frames = 0 21 21 while True: 22 # read from source 22 23 samples, read = s() 24 # filter samples 23 25 filtered_samples = f(samples) 26 # write to sink 24 27 o(filtered_samples, read) 28 # count frames read 25 29 total_frames += read 26 if read < s.hop_size: break 30 # end of file reached 31 if read < s.hop_size: 32 break 27 33 34 # print some info 28 35 duration = total_frames / float(samplerate) 29 print ("read {:s}".format(s.uri)) 30 print ("applied A-weighting filtered ({:d} Hz)".format(samplerate)) 31 print ("wrote {:s} ({:.2f} s)".format(o.uri, duration)) 36 input_str = "input: {:s} ({:.2f} s, {:d} Hz)" 37 output_str = "output: {:s}, A-weighting filtered ({:d} frames total)" 38 print(input_str.format(s.uri, duration, samplerate)) 39 print(output_str.format(o.uri, total_frames)) 32 40 33 41 if __name__ == '__main__': 34 import sys 35 for f in sys.argv[1:]: 36 apply_filter(f) 42 usage = "{:s} <input_file> [output_file]".format(sys.argv[0]) 43 if not 1 < len(sys.argv) < 4: 44 print(usage) 45 sys.exit(1) 46 if len(sys.argv) < 3: 47 input_path = sys.argv[1] 48 basename = os.path.splitext(os.path.basename(input_path))[0] + ".wav" 49 output_path = "filtered_" + basename 50 else: 51 input_path, output_path = sys.argv[1:] 52 # run function 53 apply_filter(input_path, output_path) -
python/demos/demo_filterbank.py
r5b46bc3 r633400d 1 1 #! /usr/bin/env python 2 2 3 from aubio import filterbank, fvec 4 from pylab import loglog, show, xlim, ylim, xlabel, ylabel, title 5 from numpy import vstack, arange 3 """Create a filterbank from a list of frequencies. 6 4 5 This demo uses `aubio.filterbank.set_triangle_bands` to build a set of 6 triangular filters from a list of frequencies. 7 8 The filterbank coefficients are then modified before being displayed.""" 9 10 import aubio 11 import numpy as np 12 import matplotlib.pyplot as plt 13 14 # sampling rate and size of the fft 15 samplerate = 48000 7 16 win_s = 2048 8 samplerate = 480009 17 18 # define a list of custom frequency 10 19 freq_list = [60, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 24000] 20 # number of filters to create 11 21 n_filters = len(freq_list) - 2 12 22 13 f = filterbank(n_filters, win_s) 14 freqs = fvec(freq_list) 23 # create a new filterbank 24 f = aubio.filterbank(n_filters, win_s) 25 freqs = aubio.fvec(freq_list) 15 26 f.set_triangle_bands(freqs, samplerate) 16 27 28 # get the coefficients from the filterbank 17 29 coeffs = f.get_coeffs() 18 coeffs[4] *= 5. 19 30 # apply a gain to fifth band 31 coeffs[4] *= 6. 32 # load the modified coeffs into the filterbank 20 33 f.set_coeffs(coeffs) 21 34 22 times = vstack([arange(win_s // 2 + 1) * samplerate / win_s] * n_filters) 23 title('Bank of filters built using a simple list of boundaries\nThe middle band has been amplified by 2.') 24 loglog(times.T, f.get_coeffs().T, '.-') 25 xlim([50, samplerate/2]) 26 ylim([1.0e-6, 2.0e-2]) 27 xlabel('log frequency (Hz)') 28 ylabel('log amplitude') 29 30 show() 35 # display the band gains in a loglog plot 36 freqs = np.vstack([np.arange(win_s // 2 + 1) * samplerate / win_s] * n_filters) 37 plt.title('filterbank built from a list of frequencies\n' 38 'The 5th band has been amplified by a factor 6.') 39 plt.loglog(freqs.T, f.get_coeffs().T, '.-') 40 plt.xlim([50, samplerate/2]) 41 plt.ylim([1.0e-6, 2.0e-2]) 42 plt.xlabel('log frequency (Hz)') 43 plt.ylabel('log amplitude') 44 plt.show() -
python/demos/demo_pitch_sinusoid.py
r5b46bc3 r633400d 38 38 pointer += partition 39 39 pointer += partition 40 freqs[ pointer : pointer + partition ] = 400 + 5 * np.random.random(sin_length/ 8)40 freqs[ pointer : pointer + partition ] = 400 + 5 * np.random.random(sin_length//8) 41 41 42 42 a = build_sinusoid(sin_length, freqs, samplerate) -
python/demos/demo_source_simple.py
r5b46bc3 r633400d 1 1 #! /usr/bin/env python 2 import sys, aubio 2 3 """A simple example using aubio.source.""" 4 5 import sys 6 import aubio 3 7 4 8 samplerate = 0 # use original source samplerate 5 hop_size = 256 # number of frames to read in one block6 s = aubio.source(sys.argv[1], samplerate, hop_size)9 hop_size = 256 # number of frames to read in one block 10 src = aubio.source(sys.argv[1], samplerate, hop_size) 7 11 total_frames = 0 8 12 9 while True: # reading loop 10 samples, read = s() 11 total_frames += read 12 if read < hop_size: break # end of file reached 13 while True: 14 samples, read = src() # read hop_size new samples from source 15 total_frames += read # increment total number of frames 16 if read < hop_size: # end of file reached 17 break 13 18 14 19 fmt_string = "read {:d} frames at {:d}Hz from {:s}" 15 print (fmt_string.format(total_frames, s.samplerate, sys.argv[1])) 16 20 print(fmt_string.format(total_frames, src.samplerate, src.uri)) -
python/demos/demo_timestretch.py
r5b46bc3 r633400d 13 13 14 14 win_s = 1024 15 hop_s = win_s / 8 # 87.5 % overlap15 hop_s = win_s // 8 # 87.5 % overlap 16 16 17 17 warmup = win_s // hop_s - 1 -
python/demos/demo_timestretch_online.py
r5b46bc3 r633400d 12 12 import numpy as np 13 13 14 win_s = 102415 hop_s = win_s / 8 # 87.5 % overlap14 win_s = 512 15 hop_s = win_s // 8 # 87.5 % overlap 16 16 17 17 warmup = win_s // hop_s - 1 … … 93 93 old_grain.phas = np.copy(cur_grain.phas) 94 94 95 # until end of file 96 if read < hop_s: break 97 # increment block counter 95 98 block_read += 1 96 if read < hop_s: break97 99 98 100 for t in range(warmup + 2): # purge the last frames from the phase vocoder
Note: See TracChangeset
for help on using the changeset viewer.