Jack2
1.9.8
|
00001 /* 00002 Copyright (C) 2008 Grame 00003 00004 This program is free software; you can redistribute it and/or modify 00005 it under the terms of the GNU General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or 00007 (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program; if not, write to the Free Software 00016 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00017 00018 */ 00019 00020 #include "JackLibSampleRateResampler.h" 00021 00022 namespace Jack 00023 { 00024 00025 JackLibSampleRateResampler::JackLibSampleRateResampler() 00026 :JackResampler() 00027 { 00028 int error; 00029 fResampler = src_new(SRC_LINEAR, 1, &error); 00030 if (error != 0) 00031 jack_error("JackLibSampleRateResampler::JackLibSampleRateResampler err = %s", src_strerror(error)); 00032 } 00033 00034 JackLibSampleRateResampler::JackLibSampleRateResampler(unsigned int quality) 00035 :JackResampler() 00036 { 00037 switch (quality) { 00038 case 0: 00039 quality = SRC_LINEAR; 00040 break; 00041 case 1: 00042 quality = SRC_ZERO_ORDER_HOLD; 00043 break; 00044 case 2: 00045 quality = SRC_SINC_FASTEST; 00046 break; 00047 case 3: 00048 quality = SRC_SINC_MEDIUM_QUALITY; 00049 break; 00050 case 4: 00051 quality = SRC_SINC_BEST_QUALITY; 00052 break; 00053 default: 00054 quality = SRC_LINEAR; 00055 jack_error("Out of range resample quality"); 00056 break; 00057 } 00058 00059 int error; 00060 fResampler = src_new(quality, 1, &error); 00061 if (error != 0) { 00062 jack_error("JackLibSampleRateResampler::JackLibSampleRateResampler err = %s", src_strerror(error)); 00063 } 00064 } 00065 00066 JackLibSampleRateResampler::~JackLibSampleRateResampler() 00067 { 00068 src_delete(fResampler); 00069 } 00070 00071 void JackLibSampleRateResampler::Reset(unsigned int new_size) 00072 { 00073 JackResampler::Reset(new_size); 00074 src_reset(fResampler); 00075 } 00076 00077 unsigned int JackLibSampleRateResampler::ReadResample(jack_default_audio_sample_t* buffer, unsigned int frames) 00078 { 00079 jack_ringbuffer_data_t ring_buffer_data[2]; 00080 SRC_DATA src_data; 00081 unsigned int frames_to_write = frames; 00082 unsigned int written_frames = 0; 00083 int res; 00084 00085 jack_ringbuffer_get_read_vector(fRingBuffer, ring_buffer_data); 00086 unsigned int available_frames = (ring_buffer_data[0].len + ring_buffer_data[1].len) / sizeof(jack_default_audio_sample_t); 00087 jack_log("Output available = %ld", available_frames); 00088 00089 for (int j = 0; j < 2; j++) { 00090 00091 if (ring_buffer_data[j].len > 0) { 00092 00093 src_data.data_in = (jack_default_audio_sample_t*)ring_buffer_data[j].buf; 00094 src_data.data_out = &buffer[written_frames]; 00095 src_data.input_frames = ring_buffer_data[j].len / sizeof(jack_default_audio_sample_t); 00096 src_data.output_frames = frames_to_write; 00097 src_data.end_of_input = 0; 00098 src_data.src_ratio = fRatio; 00099 00100 res = src_process(fResampler, &src_data); 00101 if (res != 0) { 00102 jack_error("JackLibSampleRateResampler::ReadResample ratio = %f err = %s", fRatio, src_strerror(res)); 00103 return 0; 00104 } 00105 00106 frames_to_write -= src_data.output_frames_gen; 00107 written_frames += src_data.output_frames_gen; 00108 00109 if ((src_data.input_frames_used == 0 || src_data.output_frames_gen == 0) && j == 0) { 00110 jack_log("Output : j = %d input_frames_used = %ld output_frames_gen = %ld frames1 = %lu frames2 = %lu" 00111 , j, src_data.input_frames_used, src_data.output_frames_gen, ring_buffer_data[0].len, ring_buffer_data[1].len); 00112 } 00113 00114 jack_log("Output : j = %d input_frames_used = %ld output_frames_gen = %ld", j, src_data.input_frames_used, src_data.output_frames_gen); 00115 jack_ringbuffer_read_advance(fRingBuffer, src_data.input_frames_used * sizeof(jack_default_audio_sample_t)); 00116 } 00117 } 00118 00119 if (written_frames < frames) { 00120 jack_error("Output available = %ld", available_frames); 00121 jack_error("JackLibSampleRateResampler::ReadResample error written_frames = %ld", written_frames); 00122 } 00123 00124 return written_frames; 00125 } 00126 00127 unsigned int JackLibSampleRateResampler::WriteResample(jack_default_audio_sample_t* buffer, unsigned int frames) 00128 { 00129 jack_ringbuffer_data_t ring_buffer_data[2]; 00130 SRC_DATA src_data; 00131 unsigned int frames_to_read = frames; 00132 unsigned int read_frames = 0; 00133 int res; 00134 00135 jack_ringbuffer_get_write_vector(fRingBuffer, ring_buffer_data); 00136 unsigned int available_frames = (ring_buffer_data[0].len + ring_buffer_data[1].len) / sizeof(jack_default_audio_sample_t); 00137 jack_log("Input available = %ld", available_frames); 00138 00139 for (int j = 0; j < 2; j++) { 00140 00141 if (ring_buffer_data[j].len > 0) { 00142 00143 src_data.data_in = &buffer[read_frames]; 00144 src_data.data_out = (jack_default_audio_sample_t*)ring_buffer_data[j].buf; 00145 src_data.input_frames = frames_to_read; 00146 src_data.output_frames = (ring_buffer_data[j].len / sizeof(jack_default_audio_sample_t)); 00147 src_data.end_of_input = 0; 00148 src_data.src_ratio = fRatio; 00149 00150 res = src_process(fResampler, &src_data); 00151 if (res != 0) { 00152 jack_error("JackLibSampleRateResampler::WriteResample ratio = %f err = %s", fRatio, src_strerror(res)); 00153 return 0; 00154 } 00155 00156 frames_to_read -= src_data.input_frames_used; 00157 read_frames += src_data.input_frames_used; 00158 00159 if ((src_data.input_frames_used == 0 || src_data.output_frames_gen == 0) && j == 0) { 00160 jack_log("Input : j = %d input_frames_used = %ld output_frames_gen = %ld frames1 = %lu frames2 = %lu" 00161 , j, src_data.input_frames_used, src_data.output_frames_gen, ring_buffer_data[0].len, ring_buffer_data[1].len); 00162 } 00163 00164 jack_log("Input : j = %d input_frames_used = %ld output_frames_gen = %ld", j, src_data.input_frames_used, src_data.output_frames_gen); 00165 jack_ringbuffer_write_advance(fRingBuffer, src_data.output_frames_gen * sizeof(jack_default_audio_sample_t)); 00166 } 00167 } 00168 00169 if (read_frames < frames) { 00170 jack_error("Input available = %ld", available_frames); 00171 jack_error("JackLibSampleRateResampler::WriteResample error read_frames = %ld", read_frames); 00172 } 00173 00174 return read_frames; 00175 } 00176 00177 }