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: host [rate = %.1f kHz buffer = %d frames] adapter [rate = %.1f kHz buffer = %d frames] \"\n"
00077 ,float(fHostSampleRate)/1000.f, fHostBufferSize, float(fAdaptedSampleRate)/1000.f, fAdaptedBufferSize);
00078 fprintf(file, "set xlabel \"audio cycles\"\n");
00079 fprintf(file, "set ylabel \"frames\"\n");
00080 fprintf(file, "plot ");
00081 sprintf(buffer, "\"JackAudioAdapter.log\" using 2 title \"Consumer interrupt period\" with lines,");
00082 fprintf(file, buffer);
00083 sprintf(buffer, "\"JackAudioAdapter.log\" using 3 title \"Producer interrupt period\" with lines");
00084 fprintf(file, buffer);
00085
00086 fclose(file);
00087
00088
00089 file = fopen("AdapterTiming2.plot", "w");
00090 fprintf(file, "set multiplot\n");
00091 fprintf(file, "set grid\n");
00092 fprintf(file, "set title \"Audio adapter timing: host [rate = %.1f kHz buffer = %d frames] adapter [rate = %.1f kHz buffer = %d frames] \"\n"
00093 ,float(fHostSampleRate)/1000.f, fHostBufferSize, float(fAdaptedSampleRate)/1000.f, fAdaptedBufferSize);
00094 fprintf(file, "set xlabel \"audio cycles\"\n");
00095 fprintf(file, "set ylabel \"resampling ratio\"\n");
00096 fprintf(file, "plot ");
00097 sprintf(buffer, "\"JackAudioAdapter.log\" using 4 title \"Ratio 1\" with lines,");
00098 fprintf(file, buffer);
00099 sprintf(buffer, "\"JackAudioAdapter.log\" using 5 title \"Ratio 2\" with lines");
00100 fprintf(file, buffer);
00101
00102 fprintf(file, "\n unset multiplot\n");
00103 fprintf(file, "set output 'AdapterTiming2.pdf\n");
00104 fprintf(file, "set terminal pdf\n");
00105
00106 fprintf(file, "set multiplot\n");
00107 fprintf(file, "set grid\n");
00108 fprintf(file, "set title \"Audio adapter timing: host [rate = %.1f kHz buffer = %d frames] adapter [rate = %.1f kHz buffer = %d frames] \"\n"
00109 ,float(fHostSampleRate)/1000.f, fHostBufferSize, float(fAdaptedSampleRate)/1000.f, fAdaptedBufferSize);
00110 fprintf(file, "set xlabel \"audio cycles\"\n");
00111 fprintf(file, "set ylabel \"resampling ratio\"\n");
00112 fprintf(file, "plot ");
00113 sprintf(buffer, "\"JackAudioAdapter.log\" using 4 title \"Ratio 1\" with lines,");
00114 fprintf(file, buffer);
00115 sprintf(buffer, "\"JackAudioAdapter.log\" using 5 title \"Ratio 2\" with lines");
00116 fprintf(file, buffer);
00117
00118 fclose(file);
00119
00120
00121 file = fopen("AdapterTiming3.plot", "w");
00122 fprintf(file, "set multiplot\n");
00123 fprintf(file, "set grid\n");
00124 fprintf(file, "set title \"Audio adapter timing: host [rate = %.1f kHz buffer = %d frames] adapter [rate = %.1f kHz buffer = %d frames] \"\n"
00125 ,float(fHostSampleRate)/1000.f, fHostBufferSize, float(fAdaptedSampleRate)/1000.f, fAdaptedBufferSize);
00126 fprintf(file, "set xlabel \"audio cycles\"\n");
00127 fprintf(file, "set ylabel \"frames\"\n");
00128 fprintf(file, "plot ");
00129 sprintf(buffer, "\"JackAudioAdapter.log\" using 6 title \"Frames position in consumer ringbuffer\" with lines,");
00130 fprintf(file, buffer);
00131 sprintf(buffer, "\"JackAudioAdapter.log\" using 7 title \"Frames position in producer ringbuffer\" with lines");
00132 fprintf(file, buffer);
00133
00134 fprintf(file, "\n unset multiplot\n");
00135 fprintf(file, "set output 'AdapterTiming3.pdf\n");
00136 fprintf(file, "set terminal pdf\n");
00137
00138 fprintf(file, "set multiplot\n");
00139 fprintf(file, "set grid\n");
00140 fprintf(file, "set title \"Audio adapter timing: host [rate = %.1f kHz buffer = %d frames] adapter [rate = %.1f kHz buffer = %d frames] \"\n"
00141 ,float(fHostSampleRate)/1000.f, fHostBufferSize, float(fAdaptedSampleRate)/1000.f, fAdaptedBufferSize);
00142 fprintf(file, "set xlabel \"audio cycles\"\n");
00143 fprintf(file, "set ylabel \"frames\"\n");
00144 fprintf(file, "plot ");
00145 sprintf(buffer, "\"JackAudioAdapter.log\" using 6 title \"Frames position in consumer ringbuffer\" with lines,");
00146 fprintf(file, buffer);
00147 sprintf(buffer, "\"JackAudioAdapter.log\" using 7 title \"Frames position in producer ringbuffer\" with lines");
00148 fprintf(file, buffer);
00149
00150 fclose(file);
00151 }
00152
00153 #endif
00154
00155 void JackAudioAdapterInterface::GrowRingBufferSize()
00156 {
00157 fRingbufferCurSize *= 2;
00158 }
00159
00160 void JackAudioAdapterInterface::AdaptRingBufferSize()
00161 {
00162 if (fHostBufferSize > fAdaptedBufferSize)
00163 fRingbufferCurSize = 4 * fHostBufferSize;
00164 else
00165 fRingbufferCurSize = 4 * fAdaptedBufferSize;
00166 }
00167
00168 void JackAudioAdapterInterface::ResetRingBuffers()
00169 {
00170 if (fRingbufferCurSize > DEFAULT_RB_SIZE)
00171 fRingbufferCurSize = DEFAULT_RB_SIZE;
00172
00173 for (int i = 0; i < fCaptureChannels; i++)
00174 fCaptureRingBuffer[i]->Reset(fRingbufferCurSize);
00175 for (int i = 0; i < fPlaybackChannels; i++)
00176 fPlaybackRingBuffer[i]->Reset(fRingbufferCurSize);
00177 }
00178
00179 void JackAudioAdapterInterface::Reset()
00180 {
00181 ResetRingBuffers();
00182 fRunning = false;
00183 }
00184
00185 void JackAudioAdapterInterface::Create()
00186 {
00187
00188 fCaptureRingBuffer = new JackResampler*[fCaptureChannels];
00189 fPlaybackRingBuffer = new JackResampler*[fPlaybackChannels];
00190
00191 if (fAdaptative) {
00192 AdaptRingBufferSize();
00193 jack_info("Ringbuffer automatic adaptative mode size = %d frames", fRingbufferCurSize);
00194 } else {
00195 if (fRingbufferCurSize > DEFAULT_RB_SIZE)
00196 fRingbufferCurSize = DEFAULT_RB_SIZE;
00197 jack_info("Fixed ringbuffer size = %d frames", fRingbufferCurSize);
00198 }
00199
00200 for (int i = 0; i < fCaptureChannels; i++ ) {
00201 fCaptureRingBuffer[i] = new JackLibSampleRateResampler(fQuality);
00202 fCaptureRingBuffer[i]->Reset(fRingbufferCurSize);
00203 }
00204 for (int i = 0; i < fPlaybackChannels; i++ ) {
00205 fPlaybackRingBuffer[i] = new JackLibSampleRateResampler(fQuality);
00206 fPlaybackRingBuffer[i]->Reset(fRingbufferCurSize);
00207 }
00208
00209 if (fCaptureChannels > 0)
00210 jack_log("ReadSpace = %ld", fCaptureRingBuffer[0]->ReadSpace());
00211 if (fPlaybackChannels > 0)
00212 jack_log("WriteSpace = %ld", fPlaybackRingBuffer[0]->WriteSpace());
00213 }
00214
00215 void JackAudioAdapterInterface::Destroy()
00216 {
00217 for (int i = 0; i < fCaptureChannels; i++ )
00218 delete ( fCaptureRingBuffer[i] );
00219 for (int i = 0; i < fPlaybackChannels; i++ )
00220 delete ( fPlaybackRingBuffer[i] );
00221
00222 delete[] fCaptureRingBuffer;
00223 delete[] fPlaybackRingBuffer;
00224 }
00225
00226 int JackAudioAdapterInterface::PushAndPull(float** inputBuffer, float** outputBuffer, unsigned int frames)
00227 {
00228 bool failure = false;
00229 fRunning = true;
00230
00231
00232 int delta_frames = (fPullAndPushTime > 0) ? (int)((float(long(GetMicroSeconds() - fPullAndPushTime)) * float(fAdaptedSampleRate)) / 1000000.f) : 0;
00233
00234 double ratio = 1;
00235
00236
00237 if (fCaptureChannels > 0)
00238 ratio = fPIControler.GetRatio(fCaptureRingBuffer[0]->GetError() - delta_frames);
00239 else if (fPlaybackChannels > 0)
00240 ratio = fPIControler.GetRatio(fPlaybackRingBuffer[0]->GetError() - delta_frames);
00241
00242 #ifdef JACK_MONITOR
00243 if (fCaptureRingBuffer[0] != NULL)
00244 fTable.Write(fCaptureRingBuffer[0]->GetError(), fCaptureRingBuffer[0]->GetError() - delta_frames, ratio, 1/ratio, fCaptureRingBuffer[0]->ReadSpace(), fCaptureRingBuffer[0]->ReadSpace());
00245 #endif
00246
00247
00248 for (int i = 0; i < fCaptureChannels; i++) {
00249 fCaptureRingBuffer[i]->SetRatio(ratio);
00250 if (fCaptureRingBuffer[i]->WriteResample(inputBuffer[i], frames) < frames)
00251 failure = true;
00252 }
00253
00254 for (int i = 0; i < fPlaybackChannels; i++) {
00255 fPlaybackRingBuffer[i]->SetRatio(1/ratio);
00256 if (fPlaybackRingBuffer[i]->ReadResample(outputBuffer[i], frames) < frames)
00257 failure = true;
00258 }
00259
00260 if (failure) {
00261 jack_error("JackAudioAdapterInterface::PushAndPull ringbuffer failure... reset");
00262 if (fAdaptative) {
00263 GrowRingBufferSize();
00264 jack_info("Ringbuffer size = %d frames", fRingbufferCurSize);
00265 }
00266 ResetRingBuffers();
00267 return -1;
00268 } else {
00269 return 0;
00270 }
00271 }
00272
00273 int JackAudioAdapterInterface::PullAndPush(float** inputBuffer, float** outputBuffer, unsigned int frames)
00274 {
00275 fPullAndPushTime = GetMicroSeconds();
00276 if (!fRunning)
00277 return 0;
00278
00279 int res = 0;
00280
00281
00282 for (int i = 0; i < fCaptureChannels; i++) {
00283 if (fCaptureRingBuffer[i]->Read(inputBuffer[i], frames) < frames)
00284 res = -1;
00285 }
00286
00287 for (int i = 0; i < fPlaybackChannels; i++) {
00288 if (fPlaybackRingBuffer[i]->Write(outputBuffer[i], frames) < frames)
00289 res = -1;
00290 }
00291
00292 return res;
00293 }
00294
00295 }