Jack2 1.9.6

JackLibSampleRateResampler.cpp

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 JackLibSampleRateResampler::~JackLibSampleRateResampler()
00066 {
00067     src_delete(fResampler);
00068 }
00069 
00070 void JackLibSampleRateResampler::Reset(unsigned int new_size)
00071 {
00072     JackResampler::Reset(new_size);
00073     src_reset(fResampler);
00074 }
00075 
00076 unsigned int JackLibSampleRateResampler::ReadResample(float* buffer, unsigned int frames)
00077 {
00078     jack_ringbuffer_data_t ring_buffer_data[2];
00079     SRC_DATA src_data;
00080     unsigned int frames_to_write = frames;
00081     unsigned int written_frames = 0;
00082     int res;
00083     
00084     jack_ringbuffer_get_read_vector(fRingBuffer, ring_buffer_data);
00085     unsigned int available_frames = (ring_buffer_data[0].len + ring_buffer_data[1].len) / sizeof(float);
00086     jack_log("Output available = %ld", available_frames);
00087       
00088     for (int j = 0; j < 2; j++) {
00089     
00090         if (ring_buffer_data[j].len > 0) {
00091             
00092             src_data.data_in = (float*)ring_buffer_data[j].buf;
00093             src_data.data_out = &buffer[written_frames];
00094             src_data.input_frames = ring_buffer_data[j].len / sizeof(float);
00095             src_data.output_frames = frames_to_write;
00096             src_data.end_of_input = 0;
00097             src_data.src_ratio = fRatio;
00098              
00099             res = src_process(fResampler, &src_data);
00100             if (res != 0) {
00101                 jack_error("JackLibSampleRateResampler::ReadResample ratio = %f err = %s", fRatio, src_strerror(res));
00102                 return 0;
00103             }
00104                 
00105             frames_to_write -= src_data.output_frames_gen;
00106             written_frames += src_data.output_frames_gen;
00107             
00108             if ((src_data.input_frames_used == 0 || src_data.output_frames_gen == 0) && j == 0) {
00109                 jack_log("Output : j = %d input_frames_used = %ld output_frames_gen = %ld frames1 = %lu frames2 = %lu"
00110                     , j, src_data.input_frames_used, src_data.output_frames_gen, ring_buffer_data[0].len, ring_buffer_data[1].len);
00111             }
00112             
00113             jack_log("Output : j = %d input_frames_used = %ld output_frames_gen = %ld", j, src_data.input_frames_used, src_data.output_frames_gen);
00114             jack_ringbuffer_read_advance(fRingBuffer, src_data.input_frames_used * sizeof(float));
00115         }
00116     }
00117     
00118     if (written_frames < frames) {
00119         jack_error("Output available = %ld", available_frames);
00120         jack_error("JackLibSampleRateResampler::ReadResample error written_frames = %ld", written_frames);
00121     }
00122         
00123     return written_frames;
00124 }
00125 
00126 unsigned int JackLibSampleRateResampler::WriteResample(float* buffer, unsigned int frames)
00127 {
00128     jack_ringbuffer_data_t ring_buffer_data[2];
00129     SRC_DATA src_data;
00130     unsigned int frames_to_read = frames;
00131     unsigned int read_frames = 0;
00132     int res;
00133     
00134     jack_ringbuffer_get_write_vector(fRingBuffer, ring_buffer_data);
00135     unsigned int available_frames = (ring_buffer_data[0].len + ring_buffer_data[1].len) / sizeof(float);
00136     jack_log("Input available = %ld", available_frames);
00137     
00138     for (int j = 0; j < 2; j++) {
00139             
00140         if (ring_buffer_data[j].len > 0) {
00141         
00142             src_data.data_in = &buffer[read_frames];
00143             src_data.data_out = (float*)ring_buffer_data[j].buf;
00144             src_data.input_frames = frames_to_read;
00145             src_data.output_frames = (ring_buffer_data[j].len / sizeof(float));
00146             src_data.end_of_input = 0;
00147             src_data.src_ratio = fRatio;
00148          
00149             res = src_process(fResampler, &src_data);
00150             if (res != 0) {
00151                 jack_error("JackLibSampleRateResampler::ReadResample ratio = %f err = %s", fRatio, src_strerror(res));
00152                 return 0;
00153             }
00154                 
00155             frames_to_read -= src_data.input_frames_used;
00156             read_frames += src_data.input_frames_used;
00157             
00158             if ((src_data.input_frames_used == 0 || src_data.output_frames_gen == 0) && j == 0) {
00159                 jack_log("Input : j = %d input_frames_used = %ld output_frames_gen = %ld frames1 = %lu frames2 = %lu"
00160                     , j, src_data.input_frames_used, src_data.output_frames_gen, ring_buffer_data[0].len, ring_buffer_data[1].len);
00161             }
00162         
00163             jack_log("Input : j = %d input_frames_used = %ld output_frames_gen = %ld", j, src_data.input_frames_used, src_data.output_frames_gen);
00164             jack_ringbuffer_write_advance(fRingBuffer, src_data.output_frames_gen * sizeof(float));
00165         }
00166     }
00167     
00168     if (read_frames < frames) {
00169         jack_error("Input available = %ld", available_frames);
00170         jack_error("JackLibSampleRateResampler::ReadResample error read_frames = %ld", read_frames);
00171     }
00172         
00173     return read_frames;
00174 }
00175 
00176 }