-
Notifications
You must be signed in to change notification settings - Fork 0
/
SphinxOSC.sc
169 lines (139 loc) · 4.93 KB
/
SphinxOSC.sc
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/****************************************************
2018 (C) Jonathan Reus, Algorithms that Matter, IEM Graz
This software is available via the GPLv3 license.
*****************************************************/
/******USAGE**************************
// Default responder just prints out what was received
l.action = {|e, word, phonemes, isSil, ascore, lscore, prob, sframe, eframe, lback|
[word,phonemes,isSil,ascore,lscore,prob,sframe,eframe,lback].postln;
};
TODO: Buffer synchronization between spoken text & recorded audio. For segmentation in SC.
See: experiments/SpeechRecognition_Synthesis.scd
(
s.options.numInputBusChannels = 10; s.options.numOutputBusChannels = 10;
s.options.memSize = 8192 * 2 * 2 * 2 * 2; s.options.blockSize = 64 * 2 * 2 * 2;
s.waitForBoot {
c = Buffer.alloc(s, s.sampleRate * 5, 1);
b = Buffer.alloc(s, s.sampleRate * 3, 1);
t = Bus.control(s, 1); };
);
(
Ndef('tape', {
var sig, in, r_head;
in = SoundIn.ar(0);
//RecordBuf.ar(in, b, run: 1, trigger: \rec.tr, loop:0);
r_head = Phasor.ar(0, BufRateScale.kr(c), 0, BufFrames.kr(c));
Out.kr(t, r_head);
BufWr.ar(in, c, r_head, 1);
sig = PlayBuf.ar(1, b, 1, 1, loop: 1);
Pan2.ar(sig, 0, \amp.kr(1.0));
}).play(out:0, numChannels: 2);
);
*****/
SphinxOSC {
classvar <pytime, <osctime, <sctime, <tslice, <ustart, <uend; // synchronization variables
classvar <osc_sync, <osc_tts; // osc listeners from Sphinx
classvar <>action;
classvar <>server;
classvar <writeActive=false, <targetDoc, <writeAt=0, <>writeAction; // rewrite variables
*initClass {
action = {|utterance, segments|
utterance.postln;
utterance.say("Alex");
segments.postln;
};
}
// Run SphinxOSC for a given number of seconds
*run {|run=10, silence_limit=0.3, silence_thresh=500, record_previous=0.2|
var argstr;
SphinxOSC.initOSC();
argstr = "-T" + run;
argstr = argstr + "-SLIMIT" + silence_limit;
argstr = argstr + "-STHRESH" + silence_thresh;
argstr = argstr + "-RPREV" + record_previous;
("python /Volumes/Store/Drive/DEV/almat/SphinxOSC/sphinxosc.py"+argstr).runInTerminal;
}
/*** OSC LISTENERS ***/
*initOSC {
if(server.isNil) { server = Server.default };
// synchronization messages
osc_sync = OSCdef('sphinxSync', {|msg,time|
msg.postln;
pytime = msg[1];
osctime = time;
sctime = Process.elapsedTime;
//tslice = (server.sampleRate * msg[2]).asInt; // previous time slice in seconds, used for buffer sync
}, '/sphinxOSC/sync');
// utterance recording & decoding messages
osc_tts = OSCdef('sphinxData', {|msg,time|
var ts, start;
ts = msg[1]; start=msg[2];
//msg.postln;
//"Pytime: %".format(ts - pytime).postln;
//"OSCtime: %".format(time - osctime).postln;
//"SCtime: %".format(Process.elapsedTime - sctime).postln;
if(start == 1) { // Utterance started
//ustart = t.getSynchronous;
} { // Utterance ended
var word, phonemes, ascore, lscore, prob, sframe, eframe, lback, isSil = false;
var utterance = msg[3].asString, rest = msg[4..], segments = List.new, phonetic="";
(rest.size / 8).do {|i|
var st = (i*8).asInt;
segments.add(rest[st..(st+7)]);
phonetic = phonetic + rest[st].asString;
};
action.(utterance, segments);
if(writeActive) {
var toprint = utterance.asString;
targetDoc.string_(toprint, writeAt, 0);
writeAt = writeAt + toprint.size + 1;
/*
segments.do {|seg,i|
var towrite;
word = seg[0].asString;
phonemes = seg[1].asString;
isSil = (word == "<s>") || (word == "</s>") || (word == "<sil>");
towrite = writeAction.(word, phonemes, isSil);
if(towrite[1]) {
var toprint = towrite[0];
toprint.postln;
targetDoc.string_(toprint, writeAt, 0);
writeAt = writeAt + toprint.size + 1;
}
};
*/
};
//uend = t.getSynchronous;
// Copy utterance into b buffer
//c.copyData(b, 0, ustart - tslice, tslice + (uend - ustart));
//[ustart, uend, tslice].postln;
// TODO: PARSE WORD DATA FROM msg
//#word, senones, ascore, lscore, prob, sframe, eframe, lback = msg[1..];
//word = word.asString();
//isSil = (word == "<s>") || (word == "</s>") || (word == "<sil>");
//l.action(word,senones,isSil,ascore,lscore,prob,sframe,eframe,lback);
};
}, '/sphinxOSC/utterance');
}
*writeToDoc {|active=true, startline=0, doc=nil, action=nil|
writeActive = active;
if(doc.notNil) { targetDoc = doc };
if(active) {
var lrange = targetDoc.getLineRange(startline);
writeAt = lrange[0];
if(action.notNil) {
writeAction = action;
};
if(action.isNil && writeAction.isNil) {
writeAction = {|word, phonemes, isSil| // returns an array with what to print and a bool if to print it
["AT %: % (%) ".format(writeAt, phonemes, word), true];
};
};
};
}
}
/****************************************************************************
END SPHINX LINK
END SPHINX LINK
END SPHINX LINK
*****************************************************************************/