fix possible crash when setting delivery name w/o panshell
[ardour.git] / libs / ardour / srcfilesource.cc
1 /*
2     Copyright (C) 2014 Paul Davis
3     Written by: Robin Gareus <robin@gareus.org>
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
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include "pbd/error.h"
22 #include "pbd/failed_constructor.h"
23
24 #include "ardour/audiofilesource.h"
25 #include "ardour/debug.h"
26 #include "ardour/srcfilesource.h"
27
28 #include "i18n.h"
29
30 using namespace ARDOUR;
31 using namespace PBD;
32
33 /* see disk_io_chunk_frames */
34 const uint32_t SrcFileSource::blocksize = 65536U;
35
36 SrcFileSource::SrcFileSource (Session& s, boost::shared_ptr<AudioFileSource> src, SrcQuality srcq)
37         : Source(s, DataType::AUDIO, src->name(), Flag (src->flags() & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
38         , AudioFileSource (s, src->path(), Flag (src->flags() & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
39         , _source (src)
40         , _src_state (0)
41         , _source_position(0)
42         , _target_position(0)
43         , _fract_position(0)
44 {
45         assert(_source->n_channels() == 1);
46
47         int src_type = SRC_SINC_BEST_QUALITY;
48
49         switch (srcq) {
50                 case SrcBest:
51                         src_type = SRC_SINC_BEST_QUALITY;
52                         break;
53                 case SrcGood:
54                         src_type = SRC_SINC_MEDIUM_QUALITY;
55                         break;
56                 case SrcQuick:
57                         src_type = SRC_SINC_FASTEST;
58                         break;
59                 case SrcFast:
60                         src_type = SRC_ZERO_ORDER_HOLD;
61                         break;
62                 case SrcFastest:
63                         src_type = SRC_LINEAR;
64                         break;
65         }
66
67
68         _ratio = s.nominal_frame_rate() / _source->sample_rate();
69         _src_data.src_ratio = _ratio;
70
71         src_buffer_size = ceil((double)blocksize / _ratio) + 2;
72         _src_buffer = new float[src_buffer_size];
73
74         int err;
75         if ((_src_state = src_new (src_type, 1, &err)) == 0) {
76                 error << string_compose(_("Import: src_new() failed : %1"), src_strerror (err)) << endmsg ;
77                 throw failed_constructor ();
78         }
79 }
80
81 SrcFileSource::~SrcFileSource ()
82 {
83         DEBUG_TRACE (DEBUG::AudioPlayback, "SrcFileSource::~SrcFileSource\n");
84         _src_state = src_delete (_src_state) ;
85         delete [] _src_buffer;
86 }
87
88 framecnt_t
89 SrcFileSource::read_unlocked (Sample *dst, framepos_t start, framecnt_t cnt) const
90 {
91         int err;
92         const double srccnt = cnt / _ratio;
93
94         if (_target_position != start) {
95                 DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("SRC: reset %1 -> %2\n", _target_position, start));
96                 src_reset(_src_state);
97                 _fract_position = 0;
98                 _source_position = start / _ratio;
99                 _target_position = start;
100         }
101
102         const framecnt_t scnt = ceilf(srccnt - _fract_position);
103         _fract_position += (scnt - srccnt);
104
105 #ifndef NDEBUG
106         if (scnt >= src_buffer_size) {
107                 DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("SRC: CRASH AHEAD :)  %1 >= %2 (fract=%3, cnt=%4)\n",
108                                         scnt, src_buffer_size, _fract_position, cnt));
109         }
110 #endif
111         assert(scnt < src_buffer_size);
112
113         _src_data.input_frames = _source->read (_src_buffer, _source_position, scnt);
114
115         if ((framecnt_t) _src_data.input_frames < scnt
116                         || _source_position + scnt >= _source->length(0)) {
117                 _src_data.end_of_input = true;
118                 _target_position += _src_data.input_frames * _ratio;
119                 DEBUG_TRACE (DEBUG::AudioPlayback, "SRC: END OF INPUT\n");
120         } else {
121                 _src_data.end_of_input = false;
122                 _target_position += cnt;
123         }
124
125         _src_data.output_frames = cnt;
126         _src_data.data_in = _src_buffer;
127         _src_data.data_out = dst;
128
129         if (_src_data.end_of_input) {
130                 _src_data.output_frames = std::min ((long)floor(_src_data.input_frames * _ratio), _src_data.output_frames);
131         }
132
133
134         if ((err = src_process (_src_state, &_src_data))) {
135                 error << string_compose(_("SrcFileSource: %1"), src_strerror (err)) << endmsg ;
136                 return 0;
137         }
138
139         if (_src_data.end_of_input && _src_data.output_frames_gen <= 0) {
140                 return 0;
141         }
142
143         _source_position += _src_data.input_frames_used;
144
145         framepos_t saved_target = _target_position;
146         framecnt_t generated = _src_data.output_frames_gen;
147
148         while (generated < cnt) {
149                 DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("SRC: recurse for %1 samples\n",  cnt - generated));
150                 framecnt_t g = read_unlocked(dst + generated, _target_position, cnt - generated);
151                 generated += g;
152                 if (g == 0) break;
153         }
154         _target_position = saved_target;
155
156         DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("SRC: in: %1-> want: %2 || got: %3 total: %4\n",
157                                 _src_data.input_frames, _src_data.output_frames, _src_data.output_frames_gen, generated));
158
159         return generated;
160 }