00001
00002
00003
00004
00005
00006
00007 #include "JackClient.h"
00008 #include "math.h"
00009 #include "string.h"
00010 #include "Snd.h"
00011 jack_port_t **input_port;
00012 jack_port_t **output_port;
00013 jack_default_audio_sample_t **inBufPtr;
00014 jack_default_audio_sample_t **outBufPtr;
00015 double *inFrame;
00016 double *outFrame;
00017 double *iPhase;
00018
00019 int xx_nChans_xx;
00020 unsigned int xx_nFrames_xx;
00021 extern Snd *theSnd;
00022 int
00023 xx_global_process_xx (unsigned int nframes, void *argc)
00024 {
00025 if (nframes!=xx_nFrames_xx)
00026 printf("xx_global_process_xx [%d nframes]\n", nframes);
00027
00028 for (int i = 0; i < xx_nChans_xx; i++)
00029 {
00030 inBufPtr[i] = (jack_default_audio_sample_t *)
00031 jack_port_get_buffer (input_port[i], nframes);
00032 outBufPtr[i] = (jack_default_audio_sample_t *)
00033 jack_port_get_buffer (output_port[i], nframes);
00034 }
00035
00036
00037
00038 for (int j = 0; j < (int)nframes; j++)
00039 {
00040 jack_default_audio_sample_t *inBuf;
00041 jack_default_audio_sample_t *outBuf;
00042 for (int i = 0; i < xx_nChans_xx; i++)
00043 {
00044 inBuf = inBufPtr[i];
00045 inFrame[i] = inBuf[j];
00046 }
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 outFrame = theSnd->tickFrame(inFrame);
00058
00059 for (int i = 0; i < xx_nChans_xx; i++)
00060 {
00061 outBuf = outBufPtr[i];
00062 outBuf[j] = outFrame[i];
00063 }
00064
00065 }
00066
00067 return 0;
00068 }
00069
00070 JackClient::JackClient (QString name, int nChans, int nFrames):Stk ()
00071 {
00072 xx_nChans_xx = this->nChans = nChans;
00073 xx_nFrames_xx = nFrames;
00074 inFrame = new double[nChans];
00075 outFrame = new double[nChans];
00076 input_port = new jack_port_t *[nChans];
00077 output_port = new jack_port_t *[nChans];
00078 inBufPtr = new jack_default_audio_sample_t *[nChans];
00079 outBufPtr = new jack_default_audio_sample_t *[nChans];
00080
00081 iPhase = new double[nChans];
00082 for (int i = 0; i < nChans; i++)
00083 iPhase[i] = 0.0;
00084
00085
00086
00087
00088
00089
00090
00091
00092 jack_set_error_function (error);
00093
00094
00095
00096 if ((client = jack_client_new (name)) == 0)
00097 {
00098 printf ("jack server not running?\n");
00099
00100 }
00101
00102
00103
00104
00105
00106 jack_set_process_callback (client, xx_global_process_xx, 0);
00107
00108
00109
00110
00111
00112
00113 jack_set_sample_rate_callback (client, srate_callback, 0);
00114
00115
00116
00117
00118
00119
00120 jack_on_shutdown (client, jack_shutdown, 0);
00121
00122
00123
00124
00125
00126
00127 printf ("engine sample rate: %u\n", jack_get_sample_rate (client));
00128
00129
00130 for (int i = 0; i < nChans; i++)
00131 {
00132 QString iName, oName;
00133 QTextOStream (&iName) << "input" << i;
00134 QTextOStream (&oName) << "output" << i;
00135
00136 input_port[i] =
00137 jack_port_register (client, iName,
00138 JACK_DEFAULT_AUDIO_TYPE,
00139 JackPortIsInput, 0);
00140 output_port[i] =
00141 jack_port_register (client, oName,
00142 JACK_DEFAULT_AUDIO_TYPE,
00143 JackPortIsOutput, 0);
00144 }
00145 }
00146
00147
00148 void
00149 JackClient::stop ()
00150 {
00151 jack_client_close (client);
00152 }
00153
00154 void
00155 JackClient::start ()
00156 {
00157
00158 if (jack_activate (client))
00159 {
00160 printf ("cannot activate client");
00161
00162 }
00163 go();
00164 }
00165 void
00166 JackClient::go ()
00167 {
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 if ((ports =
00183 jack_get_ports (client, NULL, NULL,
00184 JackPortIsPhysical | JackPortIsOutput)) == NULL)
00185 {
00186 fprintf (stderr, "Cannot find any physical capture ports\n");
00187 exit (1);
00188 }
00189 for (int i = 0; i < nChans; i++)
00190 {
00191
00192 if (jack_connect
00193 (client, ports[i], jack_port_name (input_port[i])))
00194 {
00195 fprintf (stderr, "cannot connect input ports\n");
00196 }
00197 }
00198
00199 free (ports);
00200
00201 if ((ports =
00202 jack_get_ports (client, NULL, NULL,
00203 JackPortIsPhysical | JackPortIsInput)) == NULL)
00204 {
00205 fprintf (stderr, "Cannot find any physical playback ports\n");
00206 exit (1);
00207 }
00208
00209 for (int i = 0; i < nChans; i++)
00210 {
00211 if (jack_connect
00212 (client, jack_port_name (output_port[i]), ports[i]))
00213 {
00214 fprintf (stderr, "cannot connect output ports\n");
00215 }
00216
00217 }
00218
00219 free (ports);
00220
00221
00222
00223
00224
00225
00226
00227 }
00228
00229 JackClient::~JackClient ()
00230 {
00231
00232 }