-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsengine.cpp
244 lines (183 loc) · 5.41 KB
/
gsengine.cpp
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
This file is part of GuitarSynth2.
GuitarSynth2 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GuitarSynth2 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GuitarSynth2. If not, see <http://www.gnu.org/licenses/>.
*/
#include "synthbase.h"
#include "gsengine.h"
#include <exception>
#include <iostream>
#include <math.h>
#include <QMessageBox>
GSEngine* GSEngine::mInstance=0;
GSEngine::GSEngine(QObject *parent) :
QObject(parent)
{
mInstance=this;
mInBuf=0;
mOutBuf=0;
mLowPassBuff=0;
mFreqBuf=new_fvec(1);
InitNetwork();
lastfreq=0;
mDelayin[0]=0;
mDelayin[1]=0;
mDelayout[0]=0;
mDelayout[1]=0;
mDampingFactor=10.0;
mCuttOfFreq=6000;
}
GSEngine::~GSEngine()
{
StopEngine();
if(mInBuf)
delete [] mInBuf;
mInBuf=0;
if(mOutBuf)
delete [] mOutBuf;
mOutBuf=0;
if(mLowPassBuff)
delete [] mLowPassBuff;
del_fvec(mFreqBuf);
}
void GSEngine::InitNetwork()
{
mClient=jack_client_open("GuitarSynth",JackNullOption,0);
if(mClient==0)
this->errorMessage("Could not initialize Jack");
if(jack_set_process_callback(mClient,GSEngine::process,0)!=0)
this->errorMessage("Could not set Jack Process callback");
mInput_port=jack_port_register (mClient, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
mOutput_port=jack_port_register (mClient, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
mMidiOut=jack_port_register(mClient,"midiout",JACK_DEFAULT_MIDI_TYPE,JackPortIsOutput,0);
mSamplerate=jack_get_sample_rate(mClient);
mBufferSize=jack_get_buffer_size(mClient);
std::cout<<"Samplerate "<<mSamplerate<<" Buffersize "<<mBufferSize<<std::endl;
mPitchDetector=new_aubio_pitch("yinfft",2*mBufferSize,mBufferSize,mSamplerate);
//aubio_pitchdetection_set_yinthresh(mPitchDetector,1);
mInputMag=0;
mInBuf=new float[mBufferSize];
mOutBuf=new float[mBufferSize];
mLowPassBuff=new float[mBufferSize];
}
void GSEngine::StartEngine()
{
jack_activate(mClient);
}
void GSEngine::StopEngine()
{
jack_deactivate(mClient);
}
void GSEngine::setInputGain(int val)
{
mInputGain=((float)val/100.0f);
}
void GSEngine::setOutputGain(int val)
{
mOutputGain=((float)val/100.0f);
}
void GSEngine::addSynth(SynthBase *synth)
{
synth->setSamplerate(mSamplerate);
synth->setBufferSize(mBufferSize);
synth->InitBaseSynth();
mSynths.push_back(synth);
}
void GSEngine::errorMessage(std::string err)
{
QMessageBox msg;
msg.setText(QString::fromStdString(err));
msg.exec();
}
GSEngine * GSEngine::getInstance()
{
if(mInstance==0)
mInstance=new GSEngine();
return mInstance;
}
int GSEngine::process(jack_nframes_t frames, void *arg)
{
jack_default_audio_sample_t* in=(jack_default_audio_sample_t*)jack_port_get_buffer(mInstance->mInput_port,frames);
jack_default_audio_sample_t* out=(jack_default_audio_sample_t*)jack_port_get_buffer(mInstance->mOutput_port,frames);
mInstance->lowpassIn(frames,in);
mInstance->rectifyIn(frames,mInstance->mLowPassBuff);
// for(int i=0;i<frames;i++)
// {
// (mInstance->mInBuf)[i]=(in[i]+fabs(in[i]))/2;
// }
fvec_t Buf;
//Buf.channels=1;
memset(out,0,frames*sizeof(jack_default_audio_sample_t));
if(getMagnitude(frames,in)>1)
{
Buf.data=mInstance->mInBuf;
Buf.length=frames;
float freq;
aubio_pitch_do(mInstance->mPitchDetector,&Buf,mInstance->mFreqBuf);
freq=mInstance->mFreqBuf->data[0];
freq=floor(freq);
if(freq<0)
freq=0;
for(int i=0;i<mInstance->mSynths.size();i++)
{
mInstance->mSynths[i]->process(frames,out,freq);
}
for(jack_nframes_t f=0;f<frames;f++)
out[f]*=mInstance->mOutputGain;
mInstance->sendFrequence(freq);
}
// else
// mInstance->sendFrequence(0);
return 0;
}
void GSEngine::sendFrequence(float val)
{
emit freqChanged(val);
}
float GSEngine::getMagnitude(jack_nframes_t frames, float *buffer)
{
float mag=0;
for(jack_nframes_t i=0;i<frames;i++)
mag+=buffer[i]*buffer[i];
mag=sqrt(mag);
return mag;
}
void GSEngine::rectifyIn(int frames,float *in)
{
for(int i=0;i<frames;i++)
{
mInBuf[i]=mInputGain*(in[i]+fabs(in[i]))/2;
}
}
void GSEngine::lowpassIn(int frames, jack_default_audio_sample_t *in)
{
float C=1.0/tan(3.14*mCuttOfFreq/mSamplerate);
float b0=1.0/(1.0+2.0*mDampingFactor*C+C*C);
float b1=2*b0;
float b2=b0;
float a1=2*b0*(1-C*C);
float a2=b0*(1-2*mDampingFactor*C+C*C);
for(int f=0;f<frames;f++)
{
mLowPassBuff[f]=b0*in[f]+b1*mDelayin[0]+b2*mDelayin[1]
-a1*mDelayout[0]-a2*mDelayout[1];
if(f>0)
{
mDelayin[0]=in[f-1];
mDelayout[0]=mLowPassBuff[f-1];
}
if(f>1)
{
mDelayin[1]=in[f-2];
mDelayout[1]=mLowPassBuff[f-2];
}
}
}