File: MedianSeparation.schelp

package info (click to toggle)
supercollider-sc3-plugins 3.13.0~repack-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 20,104 kB
  • sloc: cpp: 303,352; lisp: 9,589; ansic: 3,547; sh: 96; makefile: 87; haskell: 21
file content (117 lines) | stat: -rw-r--r-- 4,645 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
class:: MedianSeparation
summary:: Separate harmonic and percussive parts of a signal
categories:: UGens>Analysis, UGens>FFT

Description::

Implementation of source separation model outlined in:

Derry FitzGerald (2010) "Harmonic/Percussive Separation using Median Filtering" International Conference on Digital Audio Effects (DAFx)

The algorithm can work quite effectively, though you may hear spectral artefacts in the reconstruction depending on the nature of your input signal. Try it on piano sounds to hear really beautiful separation. There are various parameters to the analysis, and I've generalised the original algorithm a little, allowing mean separation as well as median (which is much cheaper CPU cost). Higher quality separation necessarily requires greater latency in calculation. 


Classmethods::

method::new
fft, fftharmonic, fftpercussive, fftsize=1024, mediansize=17, hardorsoft=0, p=2, medianormax=0
argument::fft
Audio input to track, which has been pre-analysed by the link::Classes/FFT:: UGen; see examples below

argument::fftharmonic
Output FFT buffer for writing the harmonic part

argument::fftpercussive
Output FFT buffer for writing the percussive part

argument::fftsize
The algorithm needs to know the FFT size at constructor time, so pass it in here (all of the buffers for the previous three input arguments should have this size). 

argument::mediansize
How many FFT frames and bands to take a median over. The median in the horizontal direction (strength of tonal consistency, harmonic element) is compared to that in the vertical (percussiveness) to determine if a particular spectral bin will belong to the harmonic or percussive reconstruction. 

argument::hardorsoft
A flag, 0 for hard, 1 for soft. Hard separation is an either/or for allocation; soft allows a blended sound based on the relative levels of horizontal versus vertical. Each may lead to artefacts, experiment. 

argument::p
A factor from the paper for the soft assignment. Default of 2 is the paper default; 1 would be a linear proportional assignment. 

argument::medianormax
Defaults to using median algorithm, but a lower CPU cost mean (average) can be used via this argument. 

Examples::

code::

//minimal example
(
{
var harmonic, percussive; 	
var source = SoundIn.ar; 
var fft = FFT(LocalBuf(1024),source);

#harmonic,percussive = MedianSeparation(fft,FFTTrigger(LocalBuf(1024)),FFTTrigger(LocalBuf(1024)),1024,17);

[IFFT(harmonic),IFFT(percussive)]	//reconstruct harmonic to left ear, percussive to right

}.play
)





//another way, and working on sound file 

//PV_Copy not actually needed, can use LocalBufs as above, but illustrative

//explicit assignment of FFT buffers
(
~fftsize = 2048; //4096; //1024; 
a = Buffer.alloc(s,~fftsize,1);
b = Buffer.alloc(s,~fftsize,1);
c = Buffer.alloc(s,~fftsize,1);
)

//choose a sound file from your machine 
d = Buffer.read(s,Platform.resourceDir +/+ "sounds/a11wlk01.wav");
d = Buffer.read(s,"/data/audio/bigaudio/toanalyse/gospastic4bar.wav");
d = Buffer.read(s,"/data/audio/mirdata/beettest.wav");
d = Buffer.read(s,"/data/audio/mirdata/pixiesivebeentired.wav");
d = Buffer.read(s,"/data/audio/mirdata/Schubot/newtrainingset/schubert1.wav");

d.numChannels //should be mono for code below; MedianSeparation expects mono input

(
{
var harmonic, percussive; 	
var source = PlayBuf.ar(1,d,loop:1); 
var fft = FFT(a,source,0.5); //can try overlap 0.25

harmonic = PV_Copy(fft, b);
percussive = PV_Copy(fft, c);

//#harmonic,percussive = MedianSeparation(fft,harmonic,percussive,~fftsize,17); //may have more artefacts than smooth separation

//#harmonic,percussive = MedianSeparation(fft,harmonic,percussive,~fftsize,27); //increase size of median calculation, extra CPU cost

//#harmonic,percussive = MedianSeparation(fft,harmonic,percussive,~fftsize,7); //cheaper for shorter, but lower quality

//#harmonic,percussive = MedianSeparation(fft,harmonic,percussive,~fftsize,17,medianormax:1); //much cheaper for mean rather than median, but rougher separation

//#harmonic,percussive = MedianSeparation(fft,harmonic,percussive,~fftsize,5,medianormax:1); //cheap and cheerful, very rough

//#harmonic,percussive = MedianSeparation(fft,harmonic,percussive,~fftsize,17,1,2); //soft separation, less artefacts, more mixed results though

//#harmonic,percussive = MedianSeparation(fft,harmonic,percussive,~fftsize,17,1,4,medianormax:1); //weird separation

//fine for piano if shorter windows
#harmonic,percussive = MedianSeparation(fft,harmonic,percussive,~fftsize,7); //window 1024 rougher, 2048 passable, 4096 best. CPU cost low

[IFFT(harmonic),IFFT(percussive)] 		
}.play
)



::