aboutsummaryrefslogtreecommitdiff
path: root/examples/csound-extract-demonstration.csd
blob: 5368414d4191f7f513dde1061c5a8819d0546d38 (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 *
 *	csound-xtract demonstration
 *	By Richard Knight 2021
 *
 *	This demonstrates some of the csound-xtract plugin opcodes in three instruments:
 *	
 *	1. test_dump		prints selected analysed features of a sound
 *	2. test_match		uses feature matching to play a corpus based on analysis of an input sound
 *	3. test_distance	prints the distance between two streams of analysed features
 *
 *	
 *	csound-xtract is current experimental. Using different buffer and block sizes for xtprofile may
 *	result in quite different feature results depending on the input sound. For certain opcodes, namely
 *	xtcorpusmatch, this require some fine-tuning to obtain the desired results based on the character of 
 *	the input sound.
 *
 */

<CsoundSynthesizer>
<CsOptions>
-odac
</CsOptions>
<CsInstruments>
sr = 44100
kr = 4410
nchnls = 2
0dbfs = 1
seed 0


; source and corpus sounds
gSsource = "sounds/fox.wav"
gScorpus = "sounds/magicshop.wav"

; corpus f-table
gicorpuswave ftgen 0, 0, 0, 1, gScorpus, 0, 0, 1

; half size window for sndwarp reading
giwindow ftgen 0, 0, 512, 9, 0.5, 1, 0 




/*
	play the source sound, dry, and print the extracted features
*/
instr test_dump
	; use centroid, irregularity, sharpness and smoothness
	iprofile xtprofile 2048, 1024, 0, 1, 0, 0, 0, 1, 0, 1, 1

	; read the source/driving sound
	asource diskin gSsource

	; perform feature extraction on source sound
	ixtract, kdone xtractor iprofile, asource

	; print the features
	if (kdone == 1) then
		kanalysis[] xtdump ixtract
		printarray kanalysis
	endif

	outs asource, asource
endin



/* 
	match the source sound against the corpus and play the 'best match'
*/
instr test_match

	; use MFCC, rms, flatness, irregularity, spectral power, sharpness and smoothness
	iprofile xtprofile 2048, 1024, 1, 0, 0, 1, 1, 1, 1, 1, 1

	; analyse the corpus wave
	icorpus xtcorpus iprofile, gicorpuswave

	; read the source/driving sound
	asource diskin gSsource
	
	; perform feature extraction on source sound and match with corpus
	ixtract, kdone xtractor iprofile, asource
	kdex xtcorpusmatch icorpus, ixtract, kdone

	; if matched index != 0 then calculate the position to be used for sndwarp
	if (kdex != 0) then
		ilen = ftlen(gicorpuswave)
		icsr = ftsr(gicorpuswave)
		icduration = ilen / icsr
		icps = 1/(icduration)
		aphs, a_ syncphasor icps, a(kdone)
		apos = (((aphs * ilen) + kdex) / ilen) * icduration

	endif

	; play the position in the corpus wave
	amatched sndwarp 0.7, apos, 1, gicorpuswave, 0, 1024, 512, 8, giwindow, 1	
	outs amatched, amatched
endin



/*
	print the distance between two sound streams
*/
instr test_distance
	; use all features
	iprofile xtprofile 2048, 1024

	; read the source/driving sound
	asource diskin gSsource

	; modify
	amodified = butterlp(asource, 10000)

	; perform feature extraction on sounds
	ixtract1, kdone1 xtractor iprofile, asource
	ixtract2, kdone2 xtractor iprofile, amodified

	; calculate the distance and print it
	kdistance xtdistance ixtract1, ixtract2, 1
	printk2 kdistance
endin



</CsInstruments>
<CsScore>

i"test_dump" 0 3
i"test_match" 3 3
i"test_distance" 6 3

</CsScore>
</CsoundSynthesizer>