1 /*
2 * Copyright (C) 2014-2015 Robin Gareus <robin@gareus.org>
3 * Copyright (C) 2015-2017 Paul Davis <paul@linuxaudiosystems.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "pbd/error.h"
21 #include "pbd/failed_constructor.h"
22
23 #include "ardour/audiofilesource.h"
24 #include "ardour/debug.h"
25 #include "ardour/srcfilesource.h"
26
27 #include "pbd/i18n.h"
28
29 using namespace ARDOUR;
30 using namespace PBD;
31
32 const uint32_t SrcFileSource::max_blocksize = 2097152U; /* see AudioDiskstream::do_refill_with_alloc, max */
33
SrcFileSource(Session & s,boost::shared_ptr<AudioFileSource> src,SrcQuality srcq)34 SrcFileSource::SrcFileSource (Session& s, boost::shared_ptr<AudioFileSource> src, SrcQuality srcq)
35 : Source(s, DataType::AUDIO, src->name(), Flag (src->flags() & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
36 , AudioFileSource (s, src->path(), Flag (src->flags() & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
37 , _source (src)
38 , _src_state (0)
39 , _source_position(0)
40 , _target_position(0)
41 , _fract_position(0)
42 {
43 assert(_source->n_channels() == 1);
44
45 int src_type = SRC_SINC_BEST_QUALITY;
46
47 switch (srcq) {
48 case SrcBest:
49 src_type = SRC_SINC_BEST_QUALITY;
50 break;
51 case SrcGood:
52 src_type = SRC_SINC_MEDIUM_QUALITY;
53 break;
54 case SrcQuick:
55 src_type = SRC_SINC_FASTEST;
56 break;
57 case SrcFast:
58 src_type = SRC_ZERO_ORDER_HOLD;
59 break;
60 case SrcFastest:
61 src_type = SRC_LINEAR;
62 break;
63 }
64
65
66 _ratio = s.nominal_sample_rate() / _source->sample_rate();
67 _src_data.src_ratio = _ratio;
68
69 src_buffer_size = ceil((double)max_blocksize / _ratio) + 2;
70 _src_buffer = new float[src_buffer_size];
71
72 int err;
73 if ((_src_state = src_new (src_type, 1, &err)) == 0) {
74 error << string_compose(_("Import: src_new() failed : %1"), src_strerror (err)) << endmsg ;
75 throw failed_constructor ();
76 }
77 }
78
~SrcFileSource()79 SrcFileSource::~SrcFileSource ()
80 {
81 DEBUG_TRACE (DEBUG::AudioPlayback, "SrcFileSource::~SrcFileSource\n");
82 _src_state = src_delete (_src_state) ;
83 delete [] _src_buffer;
84 }
85
86 void
close()87 SrcFileSource::close ()
88 {
89 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (_source);
90 if (fs) {
91 fs->close ();
92 }
93 }
94
95 samplecnt_t
read_unlocked(Sample * dst,samplepos_t start,samplecnt_t cnt) const96 SrcFileSource::read_unlocked (Sample *dst, samplepos_t start, samplecnt_t cnt) const
97 {
98 int err;
99 const double srccnt = cnt / _ratio;
100
101 if (_target_position != start) {
102 DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("SRC: reset %1 -> %2\n", _target_position, start));
103 src_reset(_src_state);
104 _fract_position = 0;
105 _source_position = start / _ratio;
106 _target_position = start;
107 }
108
109 const samplecnt_t scnt = ceilf(srccnt - _fract_position);
110 _fract_position += (scnt - srccnt);
111
112 #ifndef NDEBUG
113 if (scnt >= src_buffer_size) {
114 DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("SRC: CRASH AHEAD :) %1 >= %2 (fract=%3, cnt=%4)\n",
115 scnt, src_buffer_size, _fract_position, cnt));
116 }
117 #endif
118 assert(scnt < src_buffer_size);
119
120 _src_data.input_frames = _source->read (_src_buffer, _source_position, scnt);
121
122 if ((samplecnt_t) _src_data.input_frames * _ratio <= cnt
123 && _source_position + scnt >= _source->length(0)) {
124 _src_data.end_of_input = true;
125 DEBUG_TRACE (DEBUG::AudioPlayback, "SRC: END OF INPUT\n");
126 } else {
127 _src_data.end_of_input = false;
128 }
129
130 if ((samplecnt_t) _src_data.input_frames < scnt) {
131 _target_position += _src_data.input_frames * _ratio;
132 } else {
133 _target_position += cnt;
134 }
135
136 _src_data.output_frames = cnt;
137 _src_data.data_in = _src_buffer;
138 _src_data.data_out = dst;
139
140 if ((err = src_process (_src_state, &_src_data))) {
141 error << string_compose(_("SrcFileSource: %1"), src_strerror (err)) << endmsg ;
142 return 0;
143 }
144
145 if (_src_data.end_of_input && _src_data.output_frames_gen <= 0) {
146 return 0;
147 }
148
149 _source_position += _src_data.input_frames_used;
150
151 samplepos_t saved_target = _target_position;
152 samplecnt_t generated = _src_data.output_frames_gen;
153
154 while (generated < cnt) {
155 DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("SRC: recurse for %1 samples\n", cnt - generated));
156 samplecnt_t g = read_unlocked(dst + generated, _target_position, cnt - generated);
157 generated += g;
158 if (g == 0) break;
159 }
160 _target_position = saved_target;
161
162 DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("SRC: in: %1-> want: %2 || got: %3 total: %4\n",
163 _src_data.input_frames, _src_data.output_frames, _src_data.output_frames_gen, generated));
164
165 return generated;
166 }
167