aboutsummaryrefslogtreecommitdiff
path: root/examples/4-postgresql-analysis.csd
blob: 38a82a6d42b14b28277f52a8bcda9f0951a6ad7d (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
<CsoundSynthesizer>
<CsOptions>
-odac
</CsOptions>
<CsInstruments>
/*
	EXAMPLE 4

	Pitch track an oscillator, inserting the time, frequency and amplitude values in a PostgreSQL database 
	as fast as it will accept, at k-rate.
	Read back the values at i-rate in the db_play instrument and create events to mimic the original oscillator.
	

*/

sr = 44100
kr = 4410
nchnls = 2
0dbfs = 1
seed 0


; connect: type, hostname or IP, database name, username, password
gidb dbconnect "postgresql", "localhost", "databasename", "username", "password"


; create table if it doesn't exist. truncate if it does
instr start_example
	kdone1 dbexec_k gidb, "CREATE TABLE IF NOT EXISTS analysis (time FLOAT, rms FLOAT, freq FLOAT)", -1
	kdone2 dbexec_k gidb, "TRUNCATE TABLE analysis", kdone1

	; schedule next steps
	if (kdone2 == 1) then
		schedule "db_analyse", 0, 3
		schedule "db_play", 3.5, 3.5
		turnoff
	endif
endin


instr db_analyse
	
	; the oscillator
	a1 oscil abs(oscil(1, 0.6)), linseg(440, p3*0.2, 800, p3*0.2, 220, p3*0.2, 220, p3*0.2, 350, p3*0.2, 440)

	; pitch/rms and time tracking
	kcps, krms pitchamdf a1, 220, 800, 0, 0
	ktime timeinsts 

	; listen back to the pitch track output
	a1 oscil krms, kcps

	; insert the time, rms and frequency values as fast as the database will accept
	kdone init 1
	kdone dbexec_k gidb, sprintfk("INSERT INTO analysis (time, rms, freq) VALUES (%f, %f, %f)", ktime, krms, kcps), 1
	
	; declick end
	aout = a1 * linseg(1, p3*0.99, 1, p3*0.01, 0)
	outs aout, aout
endin


instr db_play

	; get all of the values
	idata[][] dbarray gidb, "SELECT time, rms, freq FROM analysis"
	
	; loop through 
	index = 0
	while (index < lenarray(idata)) do
	
		; set the duration to be the time to the next row multiplied by an overlap
		if (index == lenarray(idata) - 1) then
			iduration = 1
		else	
			iduration = (idata[index+1][0] - idata[index][0]) * 3.2
		endif
	
		; create the event accordingly
		event_i "i", "oscillator", idata[index][0]*2, iduration, idata[index][1], idata[index][2]
		index += 1
	od
endin


instr oscillator

	; basic oscillator
	iamp = p4
	ifreq = p5
	a1 oscil iamp * 0.5, ifreq
	
	; envelope to try and take away the quantised type sound
	aout = a1 * linseg(0, p3*0.45, 1, p3*0.1, 1, p3*0.45, 0)
	outs aout, aout
endin

</CsInstruments>
<CsScore>
i "start_example" 0 7


</CsScore>
</CsoundSynthesizer>