2 Copyright (C) 2000-2001 Paul Davis
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.
27 #include <sigc++/bind.h>
28 #include <sigc++/class_slot.h>
30 #include <pbd/basename.h>
31 #include <pbd/lockmonitor.h>
32 #include <pbd/xml++.h>
34 #include <ardour/audioregion.h>
35 #include <ardour/session.h>
36 #include <ardour/gain.h>
37 #include <ardour/dB.h>
38 #include <ardour/playlist.h>
39 #include <ardour/audiofilter.h>
45 using namespace ARDOUR;
47 /* a Session will reset these to its chosen defaults by calling AudioRegion::set_default_fade() */
49 Change AudioRegion::FadeInChanged = ARDOUR::new_change();
50 Change AudioRegion::FadeOutChanged = ARDOUR::new_change();
51 Change AudioRegion::FadeInActiveChanged = ARDOUR::new_change();
52 Change AudioRegion::FadeOutActiveChanged = ARDOUR::new_change();
53 Change AudioRegion::EnvelopeActiveChanged = ARDOUR::new_change();
54 Change AudioRegion::ScaleAmplitudeChanged = ARDOUR::new_change();
55 Change AudioRegion::EnvelopeChanged = ARDOUR::new_change();
57 AudioRegionState::AudioRegionState (string why)
59 _fade_in (0.0, 2.0, 1.0, false),
60 _fade_out (0.0, 2.0, 1.0, false),
61 _envelope (0.0, 2.0, 1.0, false)
65 AudioRegion::AudioRegion (Source& src, jack_nframes_t start, jack_nframes_t length, bool announce)
66 : Region (start, length, PBD::basename_nosuffix(src.name()), 0, Region::Flag(Region::DefaultFlags|Region::External)),
67 _fade_in (0.0, 2.0, 1.0, false),
68 _fade_out (0.0, 2.0, 1.0, false),
69 _envelope (0.0, 2.0, 1.0, false)
71 /* basic AudioRegion constructor */
73 sources.push_back (&src);
74 master_sources.push_back (&src);
75 src.GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
77 _scale_amplitude = 1.0;
80 set_default_envelope ();
82 save_state ("initial state");
84 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
87 CheckNewRegion (this); /* EMIT SIGNAL */
91 AudioRegion::AudioRegion (Source& src, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Flag flags, bool announce)
92 : Region (start, length, name, layer, flags),
93 _fade_in (0.0, 2.0, 1.0, false),
94 _fade_out (0.0, 2.0, 1.0, false),
95 _envelope (0.0, 2.0, 1.0, false)
97 /* basic AudioRegion constructor */
99 sources.push_back (&src);
100 master_sources.push_back (&src);
101 src.GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
103 _scale_amplitude = 1.0;
105 set_default_fades ();
106 set_default_envelope ();
107 save_state ("initial state");
109 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
112 CheckNewRegion (this); /* EMIT SIGNAL */
116 AudioRegion::AudioRegion (SourceList& srcs, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Flag flags, bool announce)
117 : Region (start, length, name, layer, flags),
118 _fade_in (0.0, 2.0, 1.0, false),
119 _fade_out (0.0, 2.0, 1.0, false),
120 _envelope (0.0, 2.0, 1.0, false)
122 /* basic AudioRegion constructor */
124 for (SourceList::iterator i=srcs.begin(); i != srcs.end(); ++i) {
125 sources.push_back (*i);
126 master_sources.push_back (*i);
127 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
130 _scale_amplitude = 1.0;
132 set_default_fades ();
133 set_default_envelope ();
134 save_state ("initial state");
136 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
139 CheckNewRegion (this); /* EMIT SIGNAL */
144 AudioRegion::AudioRegion (const AudioRegion& other, jack_nframes_t offset, jack_nframes_t length, const string& name, layer_t layer, Flag flags, bool announce)
145 : Region (other, offset, length, name, layer, flags),
146 _fade_in (other._fade_in),
147 _fade_out (other._fade_out),
148 _envelope (other._envelope, (double) offset, (double) offset + length)
150 /* create a new AudioRegion, that is part of an existing one */
152 set<Source*> unique_srcs;
154 for (SourceList::const_iterator i= other.sources.begin(); i != other.sources.end(); ++i) {
155 sources.push_back (*i);
156 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
157 unique_srcs.insert (*i);
160 for (SourceList::const_iterator i = other.master_sources.begin(); i != other.master_sources.end(); ++i) {
161 if (unique_srcs.find (*i) == unique_srcs.end()) {
162 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
164 master_sources.push_back (*i);
167 /* return to default fades if the existing ones are too long */
168 _fade_in_disabled = 0;
169 _fade_out_disabled = 0;
172 if (_flags & LeftOfSplit) {
173 if (_fade_in.back()->when >= _length) {
174 set_default_fade_in ();
176 _fade_in_disabled = other._fade_in_disabled;
178 set_default_fade_out ();
179 _flags = Flag (_flags & ~Region::LeftOfSplit);
182 if (_flags & RightOfSplit) {
183 if (_fade_out.back()->when >= _length) {
184 set_default_fade_out ();
186 _fade_out_disabled = other._fade_out_disabled;
188 set_default_fade_in ();
189 _flags = Flag (_flags & ~Region::RightOfSplit);
192 _scale_amplitude = other._scale_amplitude;
194 save_state ("initial state");
196 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
199 CheckNewRegion (this); /* EMIT SIGNAL */
203 AudioRegion::AudioRegion (const AudioRegion &other)
205 _fade_in (other._fade_in),
206 _fade_out (other._fade_out),
207 _envelope (other._envelope)
209 /* Pure copy constructor */
211 set<Source*> unique_srcs;
213 for (SourceList::const_iterator i = other.sources.begin(); i != other.sources.end(); ++i) {
214 sources.push_back (*i);
215 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
216 unique_srcs.insert (*i);
219 for (SourceList::const_iterator i = other.master_sources.begin(); i != other.master_sources.end(); ++i) {
220 master_sources.push_back (*i);
221 if (unique_srcs.find (*i) == unique_srcs.end()) {
222 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
226 _scale_amplitude = other._scale_amplitude;
227 _envelope = other._envelope;
229 _fade_in_disabled = 0;
230 _fade_out_disabled = 0;
232 save_state ("initial state");
234 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
236 /* NOTE: no CheckNewRegion signal emitted here. This is the copy constructor */
239 AudioRegion::AudioRegion (Source& src, const XMLNode& node)
241 _fade_in (0.0, 2.0, 1.0, false),
242 _fade_out (0.0, 2.0, 1.0, false),
243 _envelope (0.0, 2.0, 1.0, false)
245 sources.push_back (&src);
246 master_sources.push_back (&src);
247 src.GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
249 set_default_fades ();
251 if (set_state (node)) {
252 throw failed_constructor();
255 save_state ("initial state");
257 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
259 CheckNewRegion (this); /* EMIT SIGNAL */
262 AudioRegion::AudioRegion (SourceList& srcs, const XMLNode& node)
264 _fade_in (0.0, 2.0, 1.0, false),
265 _fade_out (0.0, 2.0, 1.0, false),
266 _envelope (0.0, 2.0, 1.0, false)
268 /* basic AudioRegion constructor */
270 set<Source*> unique_srcs;
272 for (SourceList::iterator i=srcs.begin(); i != srcs.end(); ++i) {
273 sources.push_back (*i);
274 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
275 unique_srcs.insert (*i);
278 for (SourceList::iterator i = srcs.begin(); i != srcs.end(); ++i) {
279 master_sources.push_back (*i);
280 if (unique_srcs.find (*i) == unique_srcs.end()) {
281 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
285 set_default_fades ();
286 _scale_amplitude = 1.0;
288 if (set_state (node)) {
289 throw failed_constructor();
292 save_state ("initial state");
294 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
296 CheckNewRegion (this); /* EMIT SIGNAL */
299 AudioRegion::~AudioRegion ()
305 AudioRegion::state_factory (std::string why) const
307 AudioRegionState* state = new AudioRegionState (why);
309 Region::store_state (*state);
311 state->_fade_in = _fade_in;
312 state->_fade_out = _fade_out;
313 state->_envelope = _envelope;
314 state->_scale_amplitude = _scale_amplitude;
315 state->_fade_in_disabled = _fade_in_disabled;
316 state->_fade_out_disabled = _fade_out_disabled;
322 AudioRegion::restore_state (StateManager::State& sstate)
324 AudioRegionState* state = dynamic_cast<AudioRegionState*> (&sstate);
326 Change what_changed = Region::restore_and_return_flags (*state);
328 if (_flags != Flag (state->_flags)) {
330 uint32_t old_flags = _flags;
332 _flags = Flag (state->_flags);
334 if ((old_flags ^ state->_flags) & EnvelopeActive) {
335 what_changed = Change (what_changed|EnvelopeActiveChanged);
339 if (!(_fade_in == state->_fade_in)) {
340 _fade_in = state->_fade_in;
341 what_changed = Change (what_changed|FadeInChanged);
344 if (!(_fade_out == state->_fade_out)) {
345 _fade_out = state->_fade_out;
346 what_changed = Change (what_changed|FadeOutChanged);
349 if (_scale_amplitude != state->_scale_amplitude) {
350 _scale_amplitude = state->_scale_amplitude;
351 what_changed = Change (what_changed|ScaleAmplitudeChanged);
354 if (_fade_in_disabled != state->_fade_in_disabled) {
355 if (_fade_in_disabled == 0 && state->_fade_in_disabled) {
356 set_fade_in_active (false);
357 } if (_fade_in_disabled && state->_fade_in_disabled == 0) {
358 set_fade_in_active (true);
360 _fade_in_disabled = state->_fade_in_disabled;
363 if (_fade_out_disabled != state->_fade_out_disabled) {
364 if (_fade_out_disabled == 0 && state->_fade_out_disabled) {
365 set_fade_out_active (false);
366 } if (_fade_out_disabled && state->_fade_out_disabled == 0) {
367 set_fade_out_active (true);
369 _fade_out_disabled = state->_fade_out_disabled;
372 /* XXX need a way to test stored state versus current for envelopes */
374 _envelope = state->_envelope;
375 what_changed = Change (what_changed);
381 AudioRegion::get_memento() const
383 return sigc::bind (mem_fun (*(const_cast<AudioRegion *> (this)), &StateManager::use_state), _current_state_id);
387 AudioRegion::verify_length (jack_nframes_t len)
389 for (uint32_t n=0; n < sources.size(); ++n) {
390 if (_start > sources[n]->length() - len) {
398 AudioRegion::verify_start_and_length (jack_nframes_t new_start, jack_nframes_t new_length)
400 for (uint32_t n=0; n < sources.size(); ++n) {
401 if (new_length > sources[n]->length() - new_start) {
408 AudioRegion::verify_start (jack_nframes_t pos)
410 for (uint32_t n=0; n < sources.size(); ++n) {
411 if (pos > sources[n]->length() - _length) {
419 AudioRegion::verify_start_mutable (jack_nframes_t& new_start)
421 for (uint32_t n=0; n < sources.size(); ++n) {
422 if (new_start > sources[n]->length() - _length) {
423 new_start = sources[n]->length() - _length;
429 AudioRegion::set_envelope_active (bool yn)
431 if (envelope_active() != yn) {
434 snprintf (buf, sizeof (buf), "envelope active");
435 _flags = Flag (_flags|EnvelopeActive);
437 snprintf (buf, sizeof (buf), "envelope off");
438 _flags = Flag (_flags & ~EnvelopeActive);
443 send_change (EnvelopeActiveChanged);
449 AudioRegion::read_peaks (PeakData *buf, jack_nframes_t npeaks, jack_nframes_t offset, jack_nframes_t cnt, uint32_t chan_n, double samples_per_unit) const
451 if (chan_n >= sources.size()) {
455 if (sources[chan_n]->read_peaks (buf, npeaks, offset, cnt, samples_per_unit)) {
458 if (_scale_amplitude != 1.0) {
459 for (jack_nframes_t n = 0; n < npeaks; ++n) {
460 buf[n].max *= _scale_amplitude;
461 buf[n].min *= _scale_amplitude;
469 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, char * workbuf, jack_nframes_t position,
471 uint32_t chan_n, jack_nframes_t read_frames, jack_nframes_t skip_frames) const
473 return _read_at (sources, buf, mixdown_buffer, gain_buffer, workbuf, position, cnt, chan_n, read_frames, skip_frames);
477 AudioRegion::master_read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, char * workbuf, jack_nframes_t position,
478 jack_nframes_t cnt, uint32_t chan_n) const
480 return _read_at (master_sources, buf, mixdown_buffer, gain_buffer, workbuf, position, cnt, chan_n, 0, 0);
484 AudioRegion::_read_at (const SourceList& srcs, Sample *buf, Sample *mixdown_buffer, float *gain_buffer, char * workbuf,
485 jack_nframes_t position, jack_nframes_t cnt,
486 uint32_t chan_n, jack_nframes_t read_frames, jack_nframes_t skip_frames) const
488 jack_nframes_t internal_offset;
489 jack_nframes_t buf_offset;
490 jack_nframes_t to_read;
492 /* precondition: caller has verified that we cover the desired section */
494 if (chan_n >= sources.size()) {
495 return 0; /* read nothing */
498 if (position < _position) {
500 buf_offset = _position - position;
503 internal_offset = position - _position;
507 if (internal_offset >= _length) {
508 return 0; /* read nothing */
512 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
513 return 0; /* read nothing */
517 /* overwrite whatever is there */
518 mixdown_buffer = buf + buf_offset;
520 mixdown_buffer += buf_offset;
524 return 0; /* read nothing */
527 _read_data_count = 0;
529 if (srcs[chan_n]->read (mixdown_buffer, _start + internal_offset, to_read, workbuf) != to_read) {
530 return 0; /* "read nothing" */
533 _read_data_count += srcs[chan_n]->read_data_count();
537 if (_flags & FadeIn) {
539 jack_nframes_t fade_in_length = (jack_nframes_t) _fade_in.back()->when;
541 /* see if this read is within the fade in */
543 if (internal_offset < fade_in_length) {
545 jack_nframes_t limit;
547 limit = min (to_read, fade_in_length - internal_offset);
549 _fade_in.get_vector (internal_offset, internal_offset+limit, gain_buffer, limit);
551 for (jack_nframes_t n = 0; n < limit; ++n) {
552 mixdown_buffer[n] *= gain_buffer[n];
559 if (_flags & FadeOut) {
564 /* see if some part of this read is within the fade out */
566 /* ................. >| REGION
572 _length - fade_out_length
575 ^internal_offset + to_read
577 we need the intersection of [internal_offset,internal_offset+to_read] with
578 [_length - fade_out_length, _length]
583 jack_nframes_t fade_out_length = (jack_nframes_t) _fade_out.back()->when;
584 jack_nframes_t fade_interval_start = max(internal_offset, _length-fade_out_length);
585 jack_nframes_t fade_interval_end = min(internal_offset + to_read, _length);
587 if (fade_interval_end > fade_interval_start) {
588 /* (part of the) the fade out is in this buffer */
590 jack_nframes_t limit = fade_interval_end - fade_interval_start;
591 jack_nframes_t curve_offset = fade_interval_start - (_length-fade_out_length);
592 jack_nframes_t fade_offset = fade_interval_start - internal_offset;
594 _fade_out.get_vector (curve_offset,curve_offset+limit, gain_buffer, limit);
596 for (jack_nframes_t n = 0, m = fade_offset; n < limit; ++n, ++m) {
597 mixdown_buffer[m] *= gain_buffer[n];
603 /* Regular gain curves */
605 if (envelope_active()) {
606 _envelope.get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
608 if (_scale_amplitude != 1.0f) {
609 for (jack_nframes_t n = 0; n < to_read; ++n) {
610 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
613 for (jack_nframes_t n = 0; n < to_read; ++n) {
614 mixdown_buffer[n] *= gain_buffer[n];
617 } else if (_scale_amplitude != 1.0f) {
618 Session::apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
623 /* gack. the things we do for users.
628 for (jack_nframes_t n = 0; n < to_read; ++n) {
629 buf[n] += mixdown_buffer[n];
637 AudioRegion::get_state ()
643 AudioRegion::state (bool full)
645 XMLNode& node (Region::state (full));
649 LocaleGuard lg (X_("POSIX"));
651 snprintf (buf, sizeof (buf), "0x%x", (int) _flags);
652 node.add_property ("flags", buf);
653 snprintf (buf, sizeof(buf), "%f", _scale_amplitude);
654 node.add_property ("scale-gain", buf);
656 for (uint32_t n=0; n < sources.size(); ++n) {
657 snprintf (buf2, sizeof(buf2), "source-%d", n);
658 snprintf (buf, sizeof(buf), "%" PRIu64, sources[n]->id());
659 node.add_property (buf2, buf);
662 snprintf (buf, sizeof (buf), "%u", (uint32_t) sources.size());
663 node.add_property ("channels", buf);
667 child = node.add_child (X_("FadeIn"));
669 if ((_flags & DefaultFadeIn)) {
670 child->add_property (X_("default"), X_("yes"));
672 _fade_in.store_state (*child);
675 child = node.add_child (X_("FadeOut"));
677 if ((_flags & DefaultFadeOut)) {
678 child->add_property (X_("default"), X_("yes"));
680 _fade_out.store_state (*child);
684 child = node.add_child ("Envelope");
687 bool default_env = false;
689 // If there are only two points, the points are in the start of the region and the end of the region
690 // so, if they are both at 1.0f, that means the default region.
691 if (_envelope.size() == 2 &&
692 _envelope.front()->value == 1.0f &&
693 _envelope.back()->value==1.0f) {
694 if (_envelope.front()->when == 0 && _envelope.back()->when == _length) {
700 child->add_property ("default", "yes");
702 _envelope.store_state (*child);
705 child->add_property ("default", "yes");
708 if (full && _extra_xml) {
709 node.add_child_copy (*_extra_xml);
716 AudioRegion::set_state (const XMLNode& node)
718 const XMLNodeList& nlist = node.children();
719 const XMLProperty *prop;
720 LocaleGuard lg (X_("POSIX"));
722 Region::set_state (node);
724 if ((prop = node.property ("flags")) != 0) {
725 _flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
727 _flags = Flag (_flags & ~Region::LeftOfSplit);
728 _flags = Flag (_flags & ~Region::RightOfSplit);
731 if ((prop = node.property ("scale-gain")) != 0) {
732 _scale_amplitude = atof (prop->value().c_str());
734 _scale_amplitude = 1.0;
737 /* Now find envelope description and other misc child items */
739 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
746 if (child->name() == "Envelope") {
750 if ((prop = child->property ("default")) != 0) {
751 set_default_envelope ();
753 _envelope.load_state (*child);
756 _envelope.set_max_xval (_length);
757 _envelope.truncate_end (_length);
759 } else if (child->name() == "FadeIn") {
763 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
764 set_default_fade_in ();
767 _fade_in.load_state (*child);
770 } else if (child->name() == "FadeOut") {
774 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
775 set_default_fade_out ();
777 _fade_out.load_state (*child);
786 AudioRegion::set_fade_in_shape (FadeShape shape)
788 set_fade_in (shape, (jack_nframes_t) _fade_in.back()->when);
792 AudioRegion::set_fade_out_shape (FadeShape shape)
794 set_fade_out (shape, (jack_nframes_t) _fade_out.back()->when);
798 AudioRegion::set_fade_in (FadeShape shape, jack_nframes_t len)
805 _fade_in.add (0.0, 0.0);
806 _fade_in.add (len, 1.0);
811 _fade_in.add (len * 0.389401, 0.0333333);
812 _fade_in.add (len * 0.629032, 0.0861111);
813 _fade_in.add (len * 0.829493, 0.233333);
814 _fade_in.add (len * 0.9447, 0.483333);
815 _fade_in.add (len * 0.976959, 0.697222);
816 _fade_in.add (len, 1);
821 _fade_in.add (len * 0.0207373, 0.197222);
822 _fade_in.add (len * 0.0645161, 0.525);
823 _fade_in.add (len * 0.152074, 0.802778);
824 _fade_in.add (len * 0.276498, 0.919444);
825 _fade_in.add (len * 0.481567, 0.980556);
826 _fade_in.add (len * 0.767281, 1);
827 _fade_in.add (len, 1);
832 _fade_in.add (len * 0.0737327, 0.308333);
833 _fade_in.add (len * 0.246544, 0.658333);
834 _fade_in.add (len * 0.470046, 0.886111);
835 _fade_in.add (len * 0.652074, 0.972222);
836 _fade_in.add (len * 0.771889, 0.988889);
837 _fade_in.add (len, 1);
842 _fade_in.add (len * 0.304147, 0.0694444);
843 _fade_in.add (len * 0.529954, 0.152778);
844 _fade_in.add (len * 0.725806, 0.333333);
845 _fade_in.add (len * 0.847926, 0.558333);
846 _fade_in.add (len * 0.919355, 0.730556);
847 _fade_in.add (len, 1);
852 _fade_in_shape = shape;
855 save_state (_("fade in change"));
858 send_change (FadeInChanged);
862 AudioRegion::set_fade_out (FadeShape shape, jack_nframes_t len)
869 _fade_out.add (len * 0, 1);
870 _fade_out.add (len * 0.023041, 0.697222);
871 _fade_out.add (len * 0.0553, 0.483333);
872 _fade_out.add (len * 0.170507, 0.233333);
873 _fade_out.add (len * 0.370968, 0.0861111);
874 _fade_out.add (len * 0.610599, 0.0333333);
875 _fade_out.add (len * 1, 0);
879 _fade_out.add (len * 0, 1);
880 _fade_out.add (len * 0.228111, 0.988889);
881 _fade_out.add (len * 0.347926, 0.972222);
882 _fade_out.add (len * 0.529954, 0.886111);
883 _fade_out.add (len * 0.753456, 0.658333);
884 _fade_out.add (len * 0.9262673, 0.308333);
885 _fade_out.add (len * 1, 0);
889 _fade_out.add (len * 0, 1);
890 _fade_out.add (len * 0.305556, 1);
891 _fade_out.add (len * 0.548611, 0.991736);
892 _fade_out.add (len * 0.759259, 0.931129);
893 _fade_out.add (len * 0.918981, 0.68595);
894 _fade_out.add (len * 0.976852, 0.22865);
895 _fade_out.add (len * 1, 0);
899 _fade_out.add (len * 0, 1);
900 _fade_out.add (len * 0.080645, 0.730556);
901 _fade_out.add (len * 0.277778, 0.289256);
902 _fade_out.add (len * 0.470046, 0.152778);
903 _fade_out.add (len * 0.695853, 0.0694444);
904 _fade_out.add (len * 1, 0);
908 _fade_out.add (len * 0, 1);
909 _fade_out.add (len * 1, 0);
914 _fade_out_shape = shape;
917 save_state (_("fade in change"));
920 send_change (FadeOutChanged);
924 AudioRegion::set_fade_in_length (jack_nframes_t len)
926 bool changed = _fade_in.extend_to (len);
929 _flags = Flag (_flags & ~DefaultFadeIn);
933 snprintf (buf, sizeof (buf), "fade in length changed to %u", len);
937 send_change (FadeInChanged);
942 AudioRegion::set_fade_out_length (jack_nframes_t len)
944 bool changed = _fade_out.extend_to (len);
947 _flags = Flag (_flags & ~DefaultFadeOut);
951 snprintf (buf, sizeof (buf), "fade out length changed to %u", len);
956 send_change (FadeOutChanged);
960 AudioRegion::set_fade_in_active (bool yn)
962 if (yn == (_flags & FadeIn)) {
966 _flags = Flag (_flags|FadeIn);
968 _flags = Flag (_flags & ~FadeIn);
971 send_change (FadeInActiveChanged);
975 AudioRegion::set_fade_out_active (bool yn)
977 if (yn == (_flags & FadeOut)) {
981 _flags = Flag (_flags | FadeOut);
983 _flags = Flag (_flags & ~FadeOut);
986 send_change (FadeOutActiveChanged);
990 AudioRegion::set_default_fade_in ()
992 set_fade_in (Linear, 64);
996 AudioRegion::set_default_fade_out ()
998 set_fade_out (Linear, 64);
1002 AudioRegion::set_default_fades ()
1004 _fade_in_disabled = 0;
1005 _fade_out_disabled = 0;
1006 set_default_fade_in ();
1007 set_default_fade_out ();
1011 AudioRegion::set_default_envelope ()
1013 _envelope.freeze ();
1015 _envelope.add (0, 1.0f);
1016 _envelope.add (_length, 1.0f);
1021 AudioRegion::recompute_at_end ()
1023 /* our length has changed. recompute a new final point by interpolating
1024 based on the the existing curve.
1027 _envelope.freeze ();
1028 _envelope.truncate_end (_length);
1029 _envelope.set_max_xval (_length);
1032 if (_fade_in.back()->when > _length) {
1033 _fade_in.extend_to (_length);
1034 send_change (FadeInChanged);
1037 if (_fade_out.back()->when > _length) {
1038 _fade_out.extend_to (_length);
1039 send_change (FadeOutChanged);
1044 AudioRegion::recompute_at_start ()
1046 /* as above, but the shift was from the front */
1048 _envelope.truncate_start (_length);
1050 if (_fade_in.back()->when > _length) {
1051 _fade_in.extend_to (_length);
1052 send_change (FadeInChanged);
1055 if (_fade_out.back()->when > _length) {
1056 _fade_out.extend_to (_length);
1057 send_change (FadeOutChanged);
1062 AudioRegion::separate_by_channel (Session& session, vector<AudioRegion*>& v) const
1067 for (SourceList::const_iterator i = master_sources.begin(); i != master_sources.end(); ++i) {
1070 srcs.push_back (*i);
1072 /* generate a new name */
1074 if (session.region_name (new_name, _name)) {
1078 /* create a copy with just one source */
1080 v.push_back (new AudioRegion (srcs, _start, _length, new_name, _layer, _flags));
1087 AudioRegion::source_deleted (Source* ignored)
1093 AudioRegion::lock_sources ()
1095 SourceList::iterator i;
1096 set<Source*> unique_srcs;
1098 for (i = sources.begin(); i != sources.end(); ++i) {
1099 unique_srcs.insert (*i);
1103 for (i = master_sources.begin(); i != master_sources.end(); ++i) {
1104 if (unique_srcs.find (*i) == unique_srcs.end()) {
1111 AudioRegion::unlock_sources ()
1113 SourceList::iterator i;
1114 set<Source*> unique_srcs;
1116 for (i = sources.begin(); i != sources.end(); ++i) {
1117 unique_srcs.insert (*i);
1121 for (i = master_sources.begin(); i != master_sources.end(); ++i) {
1122 if (unique_srcs.find (*i) == unique_srcs.end()) {
1129 AudioRegion::master_source_names ()
1131 SourceList::iterator i;
1133 vector<string> names;
1134 for (i = master_sources.begin(); i != master_sources.end(); ++i) {
1135 names.push_back((*i)->name());
1142 AudioRegion::region_list_equivalent (const AudioRegion& other)
1144 return size_equivalent (other) && source_equivalent (other) && _name == other._name;
1148 AudioRegion::source_equivalent (const AudioRegion& other)
1150 SourceList::iterator i;
1151 SourceList::const_iterator io;
1153 for (i = sources.begin(), io = other.sources.begin(); i != sources.end() && io != other.sources.end(); ++i, ++io) {
1154 if ((*i)->id() != (*io)->id()) {
1159 for (i = master_sources.begin(), io = other.master_sources.begin(); i != master_sources.end() && io != other.master_sources.end(); ++i, ++io) {
1160 if ((*i)->id() != (*io)->id()) {
1169 AudioRegion::equivalent (const AudioRegion& other)
1171 return _start == other._start &&
1172 _position == other._position &&
1173 _length == other._length;
1177 AudioRegion::size_equivalent (const AudioRegion& other)
1179 return _start == other._start &&
1180 _length == other._length;
1184 AudioRegion::apply (AudioFilter& filter)
1186 return filter.run (*this);
1190 AudioRegion::exportme (Session& session, AudioExportSpecification& spec)
1192 const jack_nframes_t blocksize = 4096;
1193 jack_nframes_t to_read;
1196 spec.channels = sources.size();
1198 if (spec.prepare (blocksize, session.frame_rate())) {
1203 spec.total_frames = _length;
1205 while (spec.pos < _length && !spec.stop) {
1208 /* step 1: interleave */
1210 to_read = min (_length - spec.pos, blocksize);
1212 if (spec.channels == 1) {
1214 if (sources.front()->read (spec.dataF, _start + spec.pos, to_read, 0) != to_read) {
1220 Sample buf[blocksize];
1222 for (uint32_t chan = 0; chan < spec.channels; ++chan) {
1224 if (sources[chan]->read (buf, _start + spec.pos, to_read, 0) != to_read) {
1228 for (jack_nframes_t x = 0; x < to_read; ++x) {
1229 spec.dataF[chan+(x*spec.channels)] = buf[x];
1234 if (spec.process (to_read)) {
1238 spec.pos += to_read;
1239 spec.progress = (double) spec.pos /_length;
1246 spec.running = false;
1247 spec.status = status;
1254 AudioRegion::get_parent()
1259 r = _playlist->session().find_whole_file_parent (*this);
1266 AudioRegion::set_scale_amplitude (gain_t g)
1268 _scale_amplitude = g;
1270 /* tell the diskstream we're in */
1273 _playlist->Modified();
1276 /* tell everybody else */
1278 send_change (ScaleAmplitudeChanged);
1282 AudioRegion::normalize_to (float target_dB)
1284 const jack_nframes_t blocksize = 256 * 1048;
1285 Sample buf[blocksize];
1286 char workbuf[blocksize * 4];
1287 jack_nframes_t fpos;
1288 jack_nframes_t fend;
1289 jack_nframes_t to_read;
1291 gain_t target = dB_to_coefficient (target_dB);
1293 if (target == 1.0f) {
1294 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1295 that we may have clipped.
1297 target -= FLT_EPSILON;
1301 fend = _start + _length;
1303 /* first pass: find max amplitude */
1305 while (fpos < fend) {
1309 to_read = min (fend - fpos, blocksize);
1311 for (n = 0; n < n_channels(); ++n) {
1315 if (source (n).read (buf, fpos, to_read, workbuf) != to_read) {
1319 maxamp = Session::compute_peak (buf, to_read, maxamp);
1325 if (maxamp == 0.0f) {
1326 /* don't even try */
1330 if (maxamp == target) {
1331 /* we can't do anything useful */
1335 /* compute scale factor */
1337 _scale_amplitude = target/maxamp;
1341 snprintf (buf, sizeof (buf), _("normalized to %.2fdB"), target_dB);
1345 /* tell the diskstream we're in */
1348 _playlist->Modified();
1351 /* tell everybody else */
1353 send_change (ScaleAmplitudeChanged);
1357 AudioRegion::envelope_changed (Change ignored)
1359 save_state (_("envelope change"));
1360 send_change (EnvelopeChanged);
1364 AudioRegion::suspend_fade_in ()
1366 if (++_fade_in_disabled == 1) {
1367 set_fade_in_active (false);
1372 AudioRegion::resume_fade_in ()
1374 if (_fade_in_disabled && --_fade_in_disabled == 0) {
1375 set_fade_in_active (true);
1380 AudioRegion::suspend_fade_out ()
1382 if (++_fade_out_disabled == 1) {
1383 set_fade_out_active (false);
1388 AudioRegion::resume_fade_out ()
1390 if (_fade_out_disabled && --_fade_out_disabled == 0) {
1391 set_fade_out_active (true);
1396 AudioRegion::speed_mismatch (float sr) const
1398 if (sources.empty()) {
1399 /* impossible, but ... */
1403 float fsr = sources.front()->sample_rate();
1410 int region_read_peaks_from_c (void *arg, uint32_t npeaks, uint32_t start, uint32_t cnt, intptr_t data, uint32_t n_chan, double samples_per_unit)
1412 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (jack_nframes_t) npeaks, (jack_nframes_t) start, (jack_nframes_t) cnt, n_chan,samples_per_unit);
1415 uint32_t region_length_from_c (void *arg)
1418 return ((AudioRegion *) arg)->length();
1421 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1423 return ( (AudioRegion *) arg)->source().available_peaks (zoom_factor) ;