00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "JackAudioAdapter.h"
00021 #include "JackLibSampleRateResampler.h"
00022 #include "JackTime.h"
00023 #include <stdio.h>
00024
00025 namespace Jack
00026 {
00027
00028 #ifdef JACK_MONITOR
00029
00030 void MeasureTable::Write(int time1, int time2, float r1, float r2, int pos1, int pos2)
00031 {
00032 int pos = (++fCount) % TABLE_MAX;
00033 fTable[pos].time1 = time1;
00034 fTable[pos].time2 = time2;
00035 fTable[pos].r1 = r1;
00036 fTable[pos].r2 = r2;
00037 fTable[pos].pos1 = pos1;
00038 fTable[pos].pos2 = pos2;
00039 }
00040
00041 void MeasureTable::Save(unsigned int fHostBufferSize, unsigned int fHostSampleRate, unsigned int fAdaptedSampleRate, unsigned int fAdaptedBufferSize)
00042 {
00043 char buffer[1024];
00044 FILE* file = fopen("JackAudioAdapter.log", "w");
00045
00046 int max = (fCount) % TABLE_MAX - 1;
00047 for (int i = 1; i < max; i++)
00048 {
00049 fprintf(file, "%d \t %d \t %d \t %f \t %f \t %d \t %d \n",
00050 fTable[i].delta, fTable[i].time1, fTable[i].time2,
00051 fTable[i].r1, fTable[i].r2, fTable[i].pos1, fTable[i].pos2);
00052 }
00053 fclose(file);
00054
00055
00056
00057 file = fopen("AdapterTiming1.plot", "w");
00058 fprintf(file, "set multiplot\n");
00059 fprintf(file, "set grid\n");
00060 fprintf(file, "set title \"Audio adapter timing: host [rate = %.1f kHz buffer = %d frames] adapter [rate = %.1f kHz buffer = %d frames] \"\n"
00061 ,float(fHostSampleRate)/1000.f, fHostBufferSize, float(fAdaptedSampleRate)/1000.f, fAdaptedBufferSize);
00062 fprintf(file, "set xlabel \"audio cycles\"\n");
00063 fprintf(file, "set ylabel \"frames\"\n");
00064 fprintf(file, "plot ");
00065 sprintf(buffer, "\"JackAudioAdapter.log\" using 2 title \"Ringbuffer error\" with lines,");
00066 fprintf(file, buffer);
00067 sprintf(buffer, "\"JackAudioAdapter.log\" using 3 title \"Ringbuffer error with timing correction\" with lines");
00068 fprintf(file, buffer);
00069
00070 fprintf(file, "\n unset multiplot\n");
00071 fprintf(file, "set output 'AdapterTiming1.pdf\n");
00072 fprintf(file, "set terminal pdf\n");
00073
00074 fprintf(file, "set multiplot\n");
00075 fprintf(file, "set grid\n");
00076 fprintf(file, "set title \"Audio adapter timing\"\n");
00077 fprintf(file, "set xlabel \"audio cycles\"\n");
00078 fprintf(file, "set ylabel \"frames\"\n");
00079 fprintf(file, "plot ");
00080 sprintf(buffer, "\"JackAudioAdapter.log\" using 2 title \"Consumer interrupt period\" with lines,");
00081 fprintf(file, buffer);
00082 sprintf(buffer, "\"JackAudioAdapter.log\" using 3 title \"Producer interrupt period\" with lines");
00083 fprintf(file, buffer);
00084
00085 fclose(file);
00086
00087
00088 file = fopen("AdapterTiming2.plot", "w");
00089 fprintf(file, "set multiplot\n");
00090 fprintf(file, "set grid\n");
00091 fprintf(file, "set title \"Audio adapter timing: host [rate = %.1f kHz buffer = %d frames] adapter [rate = %.1f kHz buffer = %d frames] \"\n"
00092 ,float(fHostSampleRate)/1000.f, fHostBufferSize, float(fAdaptedSampleRate)/1000.f, fAdaptedBufferSize);
00093 fprintf(file, "set xlabel \"audio cycles\"\n");
00094 fprintf(file, "set ylabel \"resampling ratio\"\n");
00095 fprintf(file, "plot ");
00096 sprintf(buffer, "\"JackAudioAdapter.log\" using 4 title \"Ratio 1\" with lines,");
00097 fprintf(file, buffer);
00098 sprintf(buffer, "\"JackAudioAdapter.log\" using 5 title \"Ratio 2\" with lines");
00099 fprintf(file, buffer);
00100
00101 fprintf(file, "\n unset multiplot\n");
00102 fprintf(file, "set output 'AdapterTiming2.pdf\n");
00103 fprintf(file, "set terminal pdf\n");
00104
00105 fprintf(file, "set multiplot\n");
00106 fprintf(file, "set grid\n");
00107 fprintf(file, "set title \"Audio adapter timing\"\n");
00108 fprintf(file, "set xlabel \"audio cycles\"\n");
00109 fprintf(file, "set ylabel \"resampling ratio\"\n");
00110 fprintf(file, "plot ");
00111 sprintf(buffer, "\"JackAudioAdapter.log\" using 4 title \"Ratio 1\" with lines,");
00112 fprintf(file, buffer);
00113 sprintf(buffer, "\"JackAudioAdapter.log\" using 5 title \"Ratio 2\" with lines");
00114 fprintf(file, buffer);
00115
00116 fclose(file);
00117
00118
00119 file = fopen("AdapterTiming3.plot", "w");
00120 fprintf(file, "set multiplot\n");
00121 fprintf(file, "set grid\n");
00122 fprintf(file, "set title \"Audio adapter timing: host [rate = %.1f kHz buffer = %d frames] adapter [rate = %.1f kHz buffer = %d frames] \"\n"
00123 ,float(fHostSampleRate)/1000.f, fHostBufferSize, float(fAdaptedSampleRate)/1000.f, fAdaptedBufferSize);
00124 fprintf(file, "set xlabel \"audio cycles\"\n");
00125 fprintf(file, "set ylabel \"frames\"\n");
00126 fprintf(file, "plot ");
00127 sprintf(buffer, "\"JackAudioAdapter.log\" using 6 title \"Frames position in consumer ringbuffer\" with lines,");
00128 fprintf(file, buffer);
00129 sprintf(buffer, "\"JackAudioAdapter.log\" using 7 title \"Frames position in producer ringbuffer\" with lines");
00130 fprintf(file, buffer);
00131
00132 fprintf(file, "\n unset multiplot\n");
00133 fprintf(file, "set output 'AdapterTiming3.pdf\n");
00134 fprintf(file, "set terminal pdf\n");
00135
00136 fprintf(file, "set multiplot\n");
00137 fprintf(file, "set grid\n");
00138 fprintf(file, "set title \"Audio adapter timing\"\n");
00139 fprintf(file, "set xlabel \"audio cycles\"\n");
00140 fprintf(file, "set ylabel \"frames\"\n");
00141 fprintf(file, "plot ");
00142 sprintf(buffer, "\"JackAudioAdapter.log\" using 6 title \"Frames position in consumer ringbuffer\" with lines,");
00143 fprintf(file, buffer);
00144 sprintf(buffer, "\"JackAudioAdapter.log\" using 7 title \"Frames position in producer ringbuffer\" with lines");
00145 fprintf(file, buffer);
00146
00147 fclose(file);
00148 }
00149
00150 #endif
00151
00152 void JackAudioAdapterInterface::GrowRingBufferSize()
00153 {
00154 fRingbufferCurSize *= 2;
00155 }
00156
00157 void JackAudioAdapterInterface::AdaptRingBufferSize()
00158 {
00159 if (fHostBufferSize > fAdaptedBufferSize)
00160 fRingbufferCurSize = 4 * fHostBufferSize;
00161 else
00162 fRingbufferCurSize = 4 * fAdaptedBufferSize;
00163 }
00164
00165 void JackAudioAdapterInterface::ResetRingBuffers()
00166 {
00167 if (fRingbufferCurSize > DEFAULT_RB_SIZE)
00168 fRingbufferCurSize = DEFAULT_RB_SIZE;
00169
00170 for (int i = 0; i < fCaptureChannels; i++)
00171 fCaptureRingBuffer[i]->Reset(fRingbufferCurSize);
00172 for (int i = 0; i < fPlaybackChannels; i++)
00173 fPlaybackRingBuffer[i]->Reset(fRingbufferCurSize);
00174 }
00175
00176 void JackAudioAdapterInterface::Reset()
00177 {
00178 ResetRingBuffers();
00179 fRunning = false;
00180 }
00181
00182 void JackAudioAdapterInterface::Create()
00183 {
00184
00185 fCaptureRingBuffer = new JackResampler*[fCaptureChannels];
00186 fPlaybackRingBuffer = new JackResampler*[fPlaybackChannels];
00187
00188 if (fAdaptative) {
00189 AdaptRingBufferSize();
00190 jack_info("Ringbuffer automatic adaptative mode size = %d frames", fRingbufferCurSize);
00191 } else {
00192 if (fRingbufferCurSize > DEFAULT_RB_SIZE)
00193 fRingbufferCurSize = DEFAULT_RB_SIZE;
00194 jack_info("Fixed ringbuffer size = %d frames", fRingbufferCurSize);
00195 }
00196
00197 for (int i = 0; i < fCaptureChannels; i++ ) {
00198 fCaptureRingBuffer[i] = new JackLibSampleRateResampler(fQuality);
00199 fCaptureRingBuffer[i]->Reset(fRingbufferCurSize);
00200 }
00201 for (int i = 0; i < fPlaybackChannels; i++ ) {
00202 fPlaybackRingBuffer[i] = new JackLibSampleRateResampler(fQuality);
00203 fPlaybackRingBuffer[i]->Reset(fRingbufferCurSize);
00204 }
00205
00206 if (fCaptureChannels > 0)
00207 jack_log("ReadSpace = %ld", fCaptureRingBuffer[0]->ReadSpace());
00208 if (fPlaybackChannels > 0)
00209 jack_log("WriteSpace = %ld", fPlaybackRingBuffer[0]->WriteSpace());
00210 }
00211
00212 void JackAudioAdapterInterface::Destroy()
00213 {
00214 for (int i = 0; i < fCaptureChannels; i++ )
00215 delete ( fCaptureRingBuffer[i] );
00216 for (int i = 0; i < fPlaybackChannels; i++ )
00217 delete ( fPlaybackRingBuffer[i] );
00218
00219 delete[] fCaptureRingBuffer;
00220 delete[] fPlaybackRingBuffer;
00221 }
00222
00223 int JackAudioAdapterInterface::PushAndPull(float** inputBuffer, float** outputBuffer, unsigned int frames)
00224 {
00225 bool failure = false;
00226 fRunning = true;
00227
00228
00229 int delta_frames = (fPullAndPushTime > 0) ? (int)((float(long(GetMicroSeconds() - fPullAndPushTime)) * float(fAdaptedSampleRate)) / 1000000.f) : 0;
00230 double ratio = fPIControler.GetRatio(fCaptureRingBuffer[0]->GetError() - delta_frames);
00231
00232 #ifdef JACK_MONITOR
00233 fTable.Write(fCaptureRingBuffer[0]->GetError(), fCaptureRingBuffer[0]->GetError() - delta_frames, ratio, 1/ratio, fCaptureRingBuffer[0]->ReadSpace(), fCaptureRingBuffer[0]->ReadSpace());
00234 #endif
00235
00236
00237 for (int i = 0; i < fCaptureChannels; i++) {
00238 fCaptureRingBuffer[i]->SetRatio(ratio);
00239 if (fCaptureRingBuffer[i]->WriteResample(inputBuffer[i], frames) < frames)
00240 failure = true;
00241 }
00242
00243 for (int i = 0; i < fPlaybackChannels; i++) {
00244 fPlaybackRingBuffer[i]->SetRatio(1/ratio);
00245 if (fPlaybackRingBuffer[i]->ReadResample(outputBuffer[i], frames) < frames)
00246 failure = true;
00247 }
00248
00249 if (failure) {
00250 jack_error("JackAudioAdapterInterface::PushAndPull ringbuffer failure... reset");
00251 if (fAdaptative) {
00252 GrowRingBufferSize();
00253 jack_info("Ringbuffer size = %d frames", fRingbufferCurSize);
00254 }
00255 ResetRingBuffers();
00256 return -1;
00257 } else {
00258 return 0;
00259 }
00260 }
00261
00262 int JackAudioAdapterInterface::PullAndPush(float** inputBuffer, float** outputBuffer, unsigned int frames)
00263 {
00264 fPullAndPushTime = GetMicroSeconds();
00265 if (!fRunning)
00266 return 0;
00267
00268 int res = 0;
00269
00270
00271 for (int i = 0; i < fCaptureChannels; i++) {
00272 if (fCaptureRingBuffer[i]->Read(inputBuffer[i], frames) < frames)
00273 res = -1;
00274 }
00275
00276 for (int i = 0; i < fPlaybackChannels; i++) {
00277 if (fPlaybackRingBuffer[i]->Write(outputBuffer[i], frames) < frames)
00278 res = -1;
00279 }
00280
00281 return res;
00282 }
00283
00284 }