00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "JackLibSampleRateResampler.h"
00021
00022 namespace Jack
00023 {
00024
00025 JackLibSampleRateResampler::JackLibSampleRateResampler()
00026 :JackResampler(),fRatio(1)
00027 {
00028 int error;
00029 fResampler = src_new(SRC_LINEAR, 1, &error);
00030
00031 if (error != 0)
00032 jack_error("JackLibSampleRateResampler::JackLibSampleRateResampler err = %s", src_strerror(error));
00033 }
00034
00035 JackLibSampleRateResampler::~JackLibSampleRateResampler()
00036 {
00037 src_delete(fResampler);
00038 }
00039
00040 void JackLibSampleRateResampler::Reset()
00041 {
00042 JackResampler::Reset();
00043 src_reset(fResampler);
00044 }
00045
00046 unsigned int JackLibSampleRateResampler::ReadResample(float* buffer, unsigned int frames)
00047 {
00048 jack_ringbuffer_data_t ring_buffer_data[2];
00049 SRC_DATA src_data;
00050 unsigned int frames_to_write = frames;
00051 unsigned int written_frames = 0;
00052 int res;
00053
00054 jack_ringbuffer_get_read_vector(fRingBuffer, ring_buffer_data);
00055 unsigned int available_frames = (ring_buffer_data[0].len + ring_buffer_data[1].len) / sizeof(float);
00056 jack_log("Output available = %ld", available_frames);
00057
00058 for (int j = 0; j < 2; j++) {
00059
00060 if (ring_buffer_data[j].len > 0) {
00061
00062 src_data.data_in = (float*)ring_buffer_data[j].buf;
00063 src_data.data_out = &buffer[written_frames];
00064 src_data.input_frames = ring_buffer_data[j].len / sizeof(float);
00065 src_data.output_frames = frames_to_write;
00066 src_data.end_of_input = 0;
00067 src_data.src_ratio = fRatio;
00068
00069 res = src_process(fResampler, &src_data);
00070 if (res != 0) {
00071 jack_error("JackLibSampleRateResampler::ReadResample ratio = %f err = %s", fRatio, src_strerror(res));
00072 return 0;
00073 }
00074
00075 frames_to_write -= src_data.output_frames_gen;
00076 written_frames += src_data.output_frames_gen;
00077
00078 if ((src_data.input_frames_used == 0 || src_data.output_frames_gen == 0) && j == 0) {
00079 jack_log("Output : j = %d input_frames_used = %ld output_frames_gen = %ld frames1 = %lu frames2 = %lu"
00080 , j, src_data.input_frames_used, src_data.output_frames_gen, ring_buffer_data[0].len, ring_buffer_data[1].len);
00081 }
00082
00083 jack_log("Output : j = %d input_frames_used = %ld output_frames_gen = %ld", j, src_data.input_frames_used, src_data.output_frames_gen);
00084 jack_ringbuffer_read_advance(fRingBuffer, src_data.input_frames_used * sizeof(float));
00085 }
00086 }
00087
00088 if (written_frames < frames) {
00089 jack_error("Output available = %ld", available_frames);
00090 jack_error("JackLibSampleRateResampler::ReadResample error written_frames = %ld", written_frames);
00091 }
00092
00093 return written_frames;
00094 }
00095
00096 unsigned int JackLibSampleRateResampler::WriteResample(float* buffer, unsigned int frames)
00097 {
00098 jack_ringbuffer_data_t ring_buffer_data[2];
00099 SRC_DATA src_data;
00100 unsigned int frames_to_read = frames;
00101 unsigned int read_frames = 0;
00102 int res;
00103
00104 jack_ringbuffer_get_write_vector(fRingBuffer, ring_buffer_data);
00105 unsigned int available_frames = (ring_buffer_data[0].len + ring_buffer_data[1].len) / sizeof(float);
00106 jack_log("Input available = %ld", available_frames);
00107
00108 for (int j = 0; j < 2; j++) {
00109
00110 if (ring_buffer_data[j].len > 0) {
00111
00112 src_data.data_in = &buffer[read_frames];
00113 src_data.data_out = (float*)ring_buffer_data[j].buf;
00114 src_data.input_frames = frames_to_read;
00115 src_data.output_frames = (ring_buffer_data[j].len / sizeof(float));
00116 src_data.end_of_input = 0;
00117 src_data.src_ratio = fRatio;
00118
00119 res = src_process(fResampler, &src_data);
00120 if (res != 0) {
00121 jack_error("JackLibSampleRateResampler::ReadResample ratio = %f err = %s", fRatio, src_strerror(res));
00122 return 0;
00123 }
00124
00125 frames_to_read -= src_data.input_frames_used;
00126 read_frames += src_data.input_frames_used;
00127
00128 if ((src_data.input_frames_used == 0 || src_data.output_frames_gen == 0) && j == 0) {
00129 jack_log("Input : j = %d input_frames_used = %ld output_frames_gen = %ld frames1 = %lu frames2 = %lu"
00130 , j, src_data.input_frames_used, src_data.output_frames_gen, ring_buffer_data[0].len, ring_buffer_data[1].len);
00131 }
00132
00133 jack_log("Input : j = %d input_frames_used = %ld output_frames_gen = %ld", j, src_data.input_frames_used, src_data.output_frames_gen);
00134 jack_ringbuffer_write_advance(fRingBuffer, src_data.output_frames_gen * sizeof(float));
00135 }
00136 }
00137
00138 if (read_frames < frames) {
00139 jack_error("Input available = %ld", available_frames);
00140 jack_error("JackLibSampleRateResampler::ReadResample error read_frames = %ld", read_frames);
00141 }
00142
00143 return read_frames;
00144 }
00145
00146 }