Fix build under mixed mode
[jack2.git] / common / JackLibSampleRateResampler.cpp
blobf4bdbc881231287da9854b599c03c6b1f52a40fd
1 /*
2 Copyright (C) 2008 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "JackLibSampleRateResampler.h"
21 #include "JackError.h"
23 namespace Jack
26 JackLibSampleRateResampler::JackLibSampleRateResampler()
27 :JackResampler()
29 int error;
30 fResampler = src_new(SRC_LINEAR, 1, &error);
31 if (error != 0) {
32 jack_error("JackLibSampleRateResampler::JackLibSampleRateResampler err = %s", src_strerror(error));
36 JackLibSampleRateResampler::JackLibSampleRateResampler(unsigned int quality)
37 :JackResampler()
39 switch (quality) {
40 case 0:
41 quality = SRC_LINEAR;
42 break;
43 case 1:
44 quality = SRC_ZERO_ORDER_HOLD;
45 break;
46 case 2:
47 quality = SRC_SINC_FASTEST;
48 break;
49 case 3:
50 quality = SRC_SINC_MEDIUM_QUALITY;
51 break;
52 case 4:
53 quality = SRC_SINC_BEST_QUALITY;
54 break;
55 default:
56 quality = SRC_LINEAR;
57 jack_error("Out of range resample quality");
58 break;
61 int error;
62 fResampler = src_new(quality, 1, &error);
63 if (error != 0) {
64 jack_error("JackLibSampleRateResampler::JackLibSampleRateResampler err = %s", src_strerror(error));
68 JackLibSampleRateResampler::~JackLibSampleRateResampler()
70 src_delete(fResampler);
73 void JackLibSampleRateResampler::Reset(unsigned int new_size)
75 JackResampler::Reset(new_size);
76 src_reset(fResampler);
79 unsigned int JackLibSampleRateResampler::ReadResample(jack_default_audio_sample_t* buffer, unsigned int frames)
81 jack_ringbuffer_data_t ring_buffer_data[2];
82 SRC_DATA src_data;
83 unsigned int frames_to_write = frames;
84 unsigned int written_frames = 0;
85 int res;
87 jack_ringbuffer_get_read_vector(fRingBuffer, ring_buffer_data);
88 unsigned int available_frames = (ring_buffer_data[0].len + ring_buffer_data[1].len) / sizeof(jack_default_audio_sample_t);
89 jack_log("Output available = %ld", available_frames);
91 for (int j = 0; j < 2; j++) {
93 if (ring_buffer_data[j].len > 0) {
95 src_data.data_in = (jack_default_audio_sample_t*)ring_buffer_data[j].buf;
96 src_data.data_out = &buffer[written_frames];
97 src_data.input_frames = ring_buffer_data[j].len / sizeof(jack_default_audio_sample_t);
98 src_data.output_frames = frames_to_write;
99 src_data.end_of_input = 0;
100 src_data.src_ratio = fRatio;
102 res = src_process(fResampler, &src_data);
103 if (res != 0) {
104 jack_error("JackLibSampleRateResampler::ReadResample ratio = %f err = %s", fRatio, src_strerror(res));
105 return 0;
108 frames_to_write -= src_data.output_frames_gen;
109 written_frames += src_data.output_frames_gen;
111 if ((src_data.input_frames_used == 0 || src_data.output_frames_gen == 0) && j == 0) {
112 jack_log("Output : j = %d input_frames_used = %ld output_frames_gen = %ld frames1 = %lu frames2 = %lu"
113 , j, src_data.input_frames_used, src_data.output_frames_gen, ring_buffer_data[0].len, ring_buffer_data[1].len);
116 jack_log("Output : j = %d input_frames_used = %ld output_frames_gen = %ld", j, src_data.input_frames_used, src_data.output_frames_gen);
117 jack_ringbuffer_read_advance(fRingBuffer, src_data.input_frames_used * sizeof(jack_default_audio_sample_t));
121 if (written_frames < frames) {
122 jack_error("Output available = %ld", available_frames);
123 jack_error("JackLibSampleRateResampler::ReadResample error written_frames = %ld", written_frames);
126 return written_frames;
129 unsigned int JackLibSampleRateResampler::WriteResample(jack_default_audio_sample_t* buffer, unsigned int frames)
131 jack_ringbuffer_data_t ring_buffer_data[2];
132 SRC_DATA src_data;
133 unsigned int frames_to_read = frames;
134 unsigned int read_frames = 0;
135 int res;
137 jack_ringbuffer_get_write_vector(fRingBuffer, ring_buffer_data);
138 unsigned int available_frames = (ring_buffer_data[0].len + ring_buffer_data[1].len) / sizeof(jack_default_audio_sample_t);
139 jack_log("Input available = %ld", available_frames);
141 for (int j = 0; j < 2; j++) {
143 if (ring_buffer_data[j].len > 0) {
145 src_data.data_in = &buffer[read_frames];
146 src_data.data_out = (jack_default_audio_sample_t*)ring_buffer_data[j].buf;
147 src_data.input_frames = frames_to_read;
148 src_data.output_frames = (ring_buffer_data[j].len / sizeof(jack_default_audio_sample_t));
149 src_data.end_of_input = 0;
150 src_data.src_ratio = fRatio;
152 res = src_process(fResampler, &src_data);
153 if (res != 0) {
154 jack_error("JackLibSampleRateResampler::WriteResample ratio = %f err = %s", fRatio, src_strerror(res));
155 return 0;
158 frames_to_read -= src_data.input_frames_used;
159 read_frames += src_data.input_frames_used;
161 if ((src_data.input_frames_used == 0 || src_data.output_frames_gen == 0) && j == 0) {
162 jack_log("Input : j = %d input_frames_used = %ld output_frames_gen = %ld frames1 = %lu frames2 = %lu"
163 , j, src_data.input_frames_used, src_data.output_frames_gen, ring_buffer_data[0].len, ring_buffer_data[1].len);
166 jack_log("Input : j = %d input_frames_used = %ld output_frames_gen = %ld", j, src_data.input_frames_used, src_data.output_frames_gen);
167 jack_ringbuffer_write_advance(fRingBuffer, src_data.output_frames_gen * sizeof(jack_default_audio_sample_t));
171 if (read_frames < frames) {
172 jack_error("Input available = %ld", available_frames);
173 jack_error("JackLibSampleRateResampler::WriteResample error read_frames = %ld", read_frames);
176 return read_frames;