Jack2 1.9.12

JackLibSampleRateResampler.cpp
1 /*
2 Copyright (C) 2008 Grame
3
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.
8
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.
13
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.
17
18 */
19
20 #include "JackLibSampleRateResampler.h"
21 #include "JackError.h"
22
23 namespace Jack
24 {
25
26 JackLibSampleRateResampler::JackLibSampleRateResampler()
27 :JackResampler()
28 {
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));
33 }
34 }
35
36 JackLibSampleRateResampler::JackLibSampleRateResampler(unsigned int quality)
37 :JackResampler()
38 {
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;
59 }
60
61 int error;
62 fResampler = src_new(quality, 1, &error);
63 if (error != 0) {
64 jack_error("JackLibSampleRateResampler::JackLibSampleRateResampler err = %s", src_strerror(error));
65 }
66 }
67
68 JackLibSampleRateResampler::~JackLibSampleRateResampler()
69 {
70 src_delete(fResampler);
71 }
72
73 void JackLibSampleRateResampler::Reset(unsigned int new_size)
74 {
75 JackResampler::Reset(new_size);
76 src_reset(fResampler);
77 }
78
79 unsigned int JackLibSampleRateResampler::ReadResample(jack_default_audio_sample_t* buffer, unsigned int frames)
80 {
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;
86
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);
90
91 for (int j = 0; j < 2; j++) {
92
93 if (ring_buffer_data[j].len > 0) {
94
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;
101
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;
106 }
107
108 frames_to_write -= src_data.output_frames_gen;
109 written_frames += src_data.output_frames_gen;
110
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);
114 }
115
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));
118 }
119 }
120
121 if (written_frames < frames) {
122 jack_error("Output available = %ld", available_frames);
123 jack_error("JackLibSampleRateResampler::ReadResample error written_frames = %ld", written_frames);
124 }
125
126 return written_frames;
127 }
128
129 unsigned int JackLibSampleRateResampler::WriteResample(jack_default_audio_sample_t* buffer, unsigned int frames)
130 {
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;
136
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);
140
141 for (int j = 0; j < 2; j++) {
142
143 if (ring_buffer_data[j].len > 0) {
144
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;
151
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;
156 }
157
158 frames_to_read -= src_data.input_frames_used;
159 read_frames += src_data.input_frames_used;
160
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);
164 }
165
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));
168 }
169 }
170
171 if (read_frames < frames) {
172 jack_error("Input available = %ld", available_frames);
173 jack_error("JackLibSampleRateResampler::WriteResample error read_frames = %ld", read_frames);
174 }
175
176 return read_frames;
177 }
178
179 }
jack_ringbuffer_write_advance
void jack_ringbuffer_write_advance(jack_ringbuffer_t *rb, size_t cnt)
jack_error
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
jack_ringbuffer_get_read_vector
void jack_ringbuffer_get_read_vector(const jack_ringbuffer_t *rb, jack_ringbuffer_data_t *vec)
jack_ringbuffer_get_write_vector
void jack_ringbuffer_get_write_vector(const jack_ringbuffer_t *rb, jack_ringbuffer_data_t *vec)
jack_ringbuffer_read_advance
void jack_ringbuffer_read_advance(jack_ringbuffer_t *rb, size_t cnt)
jack_ringbuffer_data_t
Definition: ringbuffer.h:45
jack_log
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:108
SRC_DATA
Definition: samplerate.h:42