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 <glibmm/thread.h>
32 #include <pbd/basename.h>
33 #include <pbd/xml++.h>
35 #include <ardour/audioregion.h>
36 #include <ardour/session.h>
37 #include <ardour/gain.h>
38 #include <ardour/dB.h>
39 #include <ardour/playlist.h>
40 #include <ardour/audiofilter.h>
41 #include <ardour/audiosource.h>
47 using namespace ARDOUR;
49 /* a Session will reset these to its chosen defaults by calling AudioRegion::set_default_fade() */
51 Change AudioRegion::FadeInChanged = ARDOUR::new_change();
52 Change AudioRegion::FadeOutChanged = ARDOUR::new_change();
53 Change AudioRegion::FadeInActiveChanged = ARDOUR::new_change();
54 Change AudioRegion::FadeOutActiveChanged = ARDOUR::new_change();
55 Change AudioRegion::EnvelopeActiveChanged = ARDOUR::new_change();
56 Change AudioRegion::ScaleAmplitudeChanged = ARDOUR::new_change();
57 Change AudioRegion::EnvelopeChanged = ARDOUR::new_change();
59 AudioRegionState::AudioRegionState (string why)
61 _fade_in (0.0, 2.0, 1.0, false),
62 _fade_out (0.0, 2.0, 1.0, false),
63 _envelope (0.0, 2.0, 1.0, false)
67 AudioRegion::AudioRegion (AudioSource& src, jack_nframes_t start, jack_nframes_t length, bool announce)
68 : Region (start, length, PBD::basename_nosuffix(src.name()), 0, Region::Flag(Region::DefaultFlags|Region::External)),
69 _fade_in (0.0, 2.0, 1.0, false),
70 _fade_out (0.0, 2.0, 1.0, false),
71 _envelope (0.0, 2.0, 1.0, false)
73 /* basic AudioRegion constructor */
75 sources.push_back (&src);
76 master_sources.push_back (&src);
77 src.GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
79 _scale_amplitude = 1.0;
82 set_default_envelope ();
84 save_state ("initial state");
86 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
89 CheckNewRegion (this); /* EMIT SIGNAL */
93 AudioRegion::AudioRegion (AudioSource& src, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Flag flags, bool announce)
94 : Region (start, length, name, layer, flags),
95 _fade_in (0.0, 2.0, 1.0, false),
96 _fade_out (0.0, 2.0, 1.0, false),
97 _envelope (0.0, 2.0, 1.0, false)
99 /* basic AudioRegion constructor */
101 sources.push_back (&src);
102 master_sources.push_back (&src);
103 src.GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
105 _scale_amplitude = 1.0;
107 set_default_fades ();
108 set_default_envelope ();
109 save_state ("initial state");
111 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
114 CheckNewRegion (this); /* EMIT SIGNAL */
118 AudioRegion::AudioRegion (SourceList& srcs, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Flag flags, bool announce)
119 : Region (start, length, name, layer, flags),
120 _fade_in (0.0, 2.0, 1.0, false),
121 _fade_out (0.0, 2.0, 1.0, false),
122 _envelope (0.0, 2.0, 1.0, false)
124 /* basic AudioRegion constructor */
126 for (SourceList::iterator i=srcs.begin(); i != srcs.end(); ++i) {
127 sources.push_back (*i);
128 master_sources.push_back (*i);
129 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
132 _scale_amplitude = 1.0;
134 set_default_fades ();
135 set_default_envelope ();
136 save_state ("initial state");
138 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
141 CheckNewRegion (this); /* EMIT SIGNAL */
146 AudioRegion::AudioRegion (const AudioRegion& other, jack_nframes_t offset, jack_nframes_t length, const string& name, layer_t layer, Flag flags, bool announce)
147 : Region (other, offset, length, name, layer, flags),
148 _fade_in (other._fade_in),
149 _fade_out (other._fade_out),
150 _envelope (other._envelope, (double) offset, (double) offset + length)
152 /* create a new AudioRegion, that is part of an existing one */
154 set<AudioSource*> unique_srcs;
156 for (SourceList::const_iterator i= other.sources.begin(); i != other.sources.end(); ++i) {
157 sources.push_back (*i);
158 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
159 unique_srcs.insert (*i);
162 for (SourceList::const_iterator i = other.master_sources.begin(); i != other.master_sources.end(); ++i) {
163 if (unique_srcs.find (*i) == unique_srcs.end()) {
164 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
166 master_sources.push_back (*i);
169 /* return to default fades if the existing ones are too long */
170 _fade_in_disabled = 0;
171 _fade_out_disabled = 0;
174 if (_flags & LeftOfSplit) {
175 if (_fade_in.back()->when >= _length) {
176 set_default_fade_in ();
178 _fade_in_disabled = other._fade_in_disabled;
180 set_default_fade_out ();
181 _flags = Flag (_flags & ~Region::LeftOfSplit);
184 if (_flags & RightOfSplit) {
185 if (_fade_out.back()->when >= _length) {
186 set_default_fade_out ();
188 _fade_out_disabled = other._fade_out_disabled;
190 set_default_fade_in ();
191 _flags = Flag (_flags & ~Region::RightOfSplit);
194 _scale_amplitude = other._scale_amplitude;
196 save_state ("initial state");
198 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
201 CheckNewRegion (this); /* EMIT SIGNAL */
205 AudioRegion::AudioRegion (const AudioRegion &other)
207 _fade_in (other._fade_in),
208 _fade_out (other._fade_out),
209 _envelope (other._envelope)
211 /* Pure copy constructor */
213 set<AudioSource*> unique_srcs;
215 for (SourceList::const_iterator i = other.sources.begin(); i != other.sources.end(); ++i) {
216 sources.push_back (*i);
217 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
218 unique_srcs.insert (*i);
221 for (SourceList::const_iterator i = other.master_sources.begin(); i != other.master_sources.end(); ++i) {
222 master_sources.push_back (*i);
223 if (unique_srcs.find (*i) == unique_srcs.end()) {
224 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
228 _scale_amplitude = other._scale_amplitude;
229 _envelope = other._envelope;
231 _fade_in_disabled = 0;
232 _fade_out_disabled = 0;
234 save_state ("initial state");
236 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
238 /* NOTE: no CheckNewRegion signal emitted here. This is the copy constructor */
241 AudioRegion::AudioRegion (AudioSource& src, const XMLNode& node)
243 _fade_in (0.0, 2.0, 1.0, false),
244 _fade_out (0.0, 2.0, 1.0, false),
245 _envelope (0.0, 2.0, 1.0, false)
247 sources.push_back (&src);
248 master_sources.push_back (&src);
249 src.GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
251 set_default_fades ();
253 if (set_state (node)) {
254 throw failed_constructor();
257 save_state ("initial state");
259 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
261 CheckNewRegion (this); /* EMIT SIGNAL */
264 AudioRegion::AudioRegion (SourceList& srcs, const XMLNode& node)
266 _fade_in (0.0, 2.0, 1.0, false),
267 _fade_out (0.0, 2.0, 1.0, false),
268 _envelope (0.0, 2.0, 1.0, false)
270 /* basic AudioRegion constructor */
272 set<AudioSource*> unique_srcs;
274 for (SourceList::iterator i=srcs.begin(); i != srcs.end(); ++i) {
275 sources.push_back (*i);
276 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
277 unique_srcs.insert (*i);
280 for (SourceList::iterator i = srcs.begin(); i != srcs.end(); ++i) {
281 master_sources.push_back (*i);
282 if (unique_srcs.find (*i) == unique_srcs.end()) {
283 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
287 set_default_fades ();
288 _scale_amplitude = 1.0;
290 if (set_state (node)) {
291 throw failed_constructor();
294 save_state ("initial state");
296 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
298 CheckNewRegion (this); /* EMIT SIGNAL */
301 AudioRegion::~AudioRegion ()
307 AudioRegion::state_factory (std::string why) const
309 AudioRegionState* state = new AudioRegionState (why);
311 Region::store_state (*state);
313 state->_fade_in = _fade_in;
314 state->_fade_out = _fade_out;
315 state->_envelope = _envelope;
316 state->_scale_amplitude = _scale_amplitude;
317 state->_fade_in_disabled = _fade_in_disabled;
318 state->_fade_out_disabled = _fade_out_disabled;
324 AudioRegion::restore_state (StateManager::State& sstate)
326 AudioRegionState* state = dynamic_cast<AudioRegionState*> (&sstate);
328 Change what_changed = Region::restore_and_return_flags (*state);
330 if (_flags != Flag (state->_flags)) {
332 uint32_t old_flags = _flags;
334 _flags = Flag (state->_flags);
336 if ((old_flags ^ state->_flags) & EnvelopeActive) {
337 what_changed = Change (what_changed|EnvelopeActiveChanged);
341 if (!(_fade_in == state->_fade_in)) {
342 _fade_in = state->_fade_in;
343 what_changed = Change (what_changed|FadeInChanged);
346 if (!(_fade_out == state->_fade_out)) {
347 _fade_out = state->_fade_out;
348 what_changed = Change (what_changed|FadeOutChanged);
351 if (_scale_amplitude != state->_scale_amplitude) {
352 _scale_amplitude = state->_scale_amplitude;
353 what_changed = Change (what_changed|ScaleAmplitudeChanged);
356 if (_fade_in_disabled != state->_fade_in_disabled) {
357 if (_fade_in_disabled == 0 && state->_fade_in_disabled) {
358 set_fade_in_active (false);
359 } if (_fade_in_disabled && state->_fade_in_disabled == 0) {
360 set_fade_in_active (true);
362 _fade_in_disabled = state->_fade_in_disabled;
365 if (_fade_out_disabled != state->_fade_out_disabled) {
366 if (_fade_out_disabled == 0 && state->_fade_out_disabled) {
367 set_fade_out_active (false);
368 } if (_fade_out_disabled && state->_fade_out_disabled == 0) {
369 set_fade_out_active (true);
371 _fade_out_disabled = state->_fade_out_disabled;
374 /* XXX need a way to test stored state versus current for envelopes */
376 _envelope = state->_envelope;
377 what_changed = Change (what_changed);
383 AudioRegion::get_memento() const
385 return sigc::bind (mem_fun (*(const_cast<AudioRegion *> (this)), &StateManager::use_state), _current_state_id);
389 AudioRegion::verify_length (jack_nframes_t len)
391 for (uint32_t n=0; n < sources.size(); ++n) {
392 if (_start > sources[n]->length() - len) {
400 AudioRegion::verify_start_and_length (jack_nframes_t new_start, jack_nframes_t new_length)
402 for (uint32_t n=0; n < sources.size(); ++n) {
403 if (new_length > sources[n]->length() - new_start) {
410 AudioRegion::verify_start (jack_nframes_t pos)
412 for (uint32_t n=0; n < sources.size(); ++n) {
413 if (pos > sources[n]->length() - _length) {
421 AudioRegion::verify_start_mutable (jack_nframes_t& new_start)
423 for (uint32_t n=0; n < sources.size(); ++n) {
424 if (new_start > sources[n]->length() - _length) {
425 new_start = sources[n]->length() - _length;
431 AudioRegion::set_envelope_active (bool yn)
433 if (envelope_active() != yn) {
436 snprintf (buf, sizeof (buf), "envelope active");
437 _flags = Flag (_flags|EnvelopeActive);
439 snprintf (buf, sizeof (buf), "envelope off");
440 _flags = Flag (_flags & ~EnvelopeActive);
445 send_change (EnvelopeActiveChanged);
451 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
453 if (chan_n >= sources.size()) {
457 if (sources[chan_n]->read_peaks (buf, npeaks, offset, cnt, samples_per_unit)) {
460 if (_scale_amplitude != 1.0) {
461 for (jack_nframes_t n = 0; n < npeaks; ++n) {
462 buf[n].max *= _scale_amplitude;
463 buf[n].min *= _scale_amplitude;
471 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, char * workbuf, jack_nframes_t position,
473 uint32_t chan_n, jack_nframes_t read_frames, jack_nframes_t skip_frames) const
475 return _read_at (sources, buf, mixdown_buffer, gain_buffer, workbuf, position, cnt, chan_n, read_frames, skip_frames);
479 AudioRegion::master_read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, char * workbuf, jack_nframes_t position,
480 jack_nframes_t cnt, uint32_t chan_n) const
482 return _read_at (master_sources, buf, mixdown_buffer, gain_buffer, workbuf, position, cnt, chan_n, 0, 0);
486 AudioRegion::_read_at (const SourceList& srcs, Sample *buf, Sample *mixdown_buffer, float *gain_buffer, char * workbuf,
487 jack_nframes_t position, jack_nframes_t cnt,
488 uint32_t chan_n, jack_nframes_t read_frames, jack_nframes_t skip_frames) const
490 jack_nframes_t internal_offset;
491 jack_nframes_t buf_offset;
492 jack_nframes_t to_read;
494 /* precondition: caller has verified that we cover the desired section */
496 if (chan_n >= sources.size()) {
497 return 0; /* read nothing */
500 if (position < _position) {
502 buf_offset = _position - position;
505 internal_offset = position - _position;
509 if (internal_offset >= _length) {
510 return 0; /* read nothing */
514 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
515 return 0; /* read nothing */
519 /* overwrite whatever is there */
520 mixdown_buffer = buf + buf_offset;
522 mixdown_buffer += buf_offset;
526 return 0; /* read nothing */
529 _read_data_count = 0;
531 if (srcs[chan_n]->read (mixdown_buffer, _start + internal_offset, to_read, workbuf) != to_read) {
532 return 0; /* "read nothing" */
535 _read_data_count += srcs[chan_n]->read_data_count();
539 if (_flags & FadeIn) {
541 jack_nframes_t fade_in_length = (jack_nframes_t) _fade_in.back()->when;
543 /* see if this read is within the fade in */
545 if (internal_offset < fade_in_length) {
547 jack_nframes_t limit;
549 limit = min (to_read, fade_in_length - internal_offset);
551 _fade_in.get_vector (internal_offset, internal_offset+limit, gain_buffer, limit);
553 for (jack_nframes_t n = 0; n < limit; ++n) {
554 mixdown_buffer[n] *= gain_buffer[n];
561 if (_flags & FadeOut) {
566 /* see if some part of this read is within the fade out */
568 /* ................. >| REGION
574 _length - fade_out_length
577 ^internal_offset + to_read
579 we need the intersection of [internal_offset,internal_offset+to_read] with
580 [_length - fade_out_length, _length]
585 jack_nframes_t fade_out_length = (jack_nframes_t) _fade_out.back()->when;
586 jack_nframes_t fade_interval_start = max(internal_offset, _length-fade_out_length);
587 jack_nframes_t fade_interval_end = min(internal_offset + to_read, _length);
589 if (fade_interval_end > fade_interval_start) {
590 /* (part of the) the fade out is in this buffer */
592 jack_nframes_t limit = fade_interval_end - fade_interval_start;
593 jack_nframes_t curve_offset = fade_interval_start - (_length-fade_out_length);
594 jack_nframes_t fade_offset = fade_interval_start - internal_offset;
596 _fade_out.get_vector (curve_offset,curve_offset+limit, gain_buffer, limit);
598 for (jack_nframes_t n = 0, m = fade_offset; n < limit; ++n, ++m) {
599 mixdown_buffer[m] *= gain_buffer[n];
605 /* Regular gain curves */
607 if (envelope_active()) {
608 _envelope.get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
610 if (_scale_amplitude != 1.0f) {
611 for (jack_nframes_t n = 0; n < to_read; ++n) {
612 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
615 for (jack_nframes_t n = 0; n < to_read; ++n) {
616 mixdown_buffer[n] *= gain_buffer[n];
619 } else if (_scale_amplitude != 1.0f) {
620 Session::apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
625 /* gack. the things we do for users.
630 for (jack_nframes_t n = 0; n < to_read; ++n) {
631 buf[n] += mixdown_buffer[n];
639 AudioRegion::get_state ()
645 AudioRegion::state (bool full)
647 XMLNode& node (Region::state (full));
651 LocaleGuard lg (X_("POSIX"));
653 snprintf (buf, sizeof (buf), "0x%x", (int) _flags);
654 node.add_property ("flags", buf);
655 snprintf (buf, sizeof(buf), "%f", _scale_amplitude);
656 node.add_property ("scale-gain", buf);
658 for (uint32_t n=0; n < sources.size(); ++n) {
659 snprintf (buf2, sizeof(buf2), "source-%d", n);
660 snprintf (buf, sizeof(buf), "%" PRIu64, sources[n]->id());
661 node.add_property (buf2, buf);
664 snprintf (buf, sizeof (buf), "%u", (uint32_t) sources.size());
665 node.add_property ("channels", buf);
669 child = node.add_child (X_("FadeIn"));
671 if ((_flags & DefaultFadeIn)) {
672 child->add_property (X_("default"), X_("yes"));
674 _fade_in.store_state (*child);
677 child = node.add_child (X_("FadeOut"));
679 if ((_flags & DefaultFadeOut)) {
680 child->add_property (X_("default"), X_("yes"));
682 _fade_out.store_state (*child);
686 child = node.add_child ("Envelope");
689 bool default_env = false;
691 // If there are only two points, the points are in the start of the region and the end of the region
692 // so, if they are both at 1.0f, that means the default region.
693 if (_envelope.size() == 2 &&
694 _envelope.front()->value == 1.0f &&
695 _envelope.back()->value==1.0f) {
696 if (_envelope.front()->when == 0 && _envelope.back()->when == _length) {
702 child->add_property ("default", "yes");
704 _envelope.store_state (*child);
707 child->add_property ("default", "yes");
710 if (full && _extra_xml) {
711 node.add_child_copy (*_extra_xml);
718 AudioRegion::set_state (const XMLNode& node)
720 const XMLNodeList& nlist = node.children();
721 const XMLProperty *prop;
722 LocaleGuard lg (X_("POSIX"));
724 Region::set_state (node);
726 if ((prop = node.property ("flags")) != 0) {
727 _flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
729 _flags = Flag (_flags & ~Region::LeftOfSplit);
730 _flags = Flag (_flags & ~Region::RightOfSplit);
733 if ((prop = node.property ("scale-gain")) != 0) {
734 _scale_amplitude = atof (prop->value().c_str());
736 _scale_amplitude = 1.0;
739 /* Now find envelope description and other misc child items */
741 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
748 if (child->name() == "Envelope") {
752 if ((prop = child->property ("default")) != 0) {
753 set_default_envelope ();
755 _envelope.load_state (*child);
758 _envelope.set_max_xval (_length);
759 _envelope.truncate_end (_length);
761 } else if (child->name() == "FadeIn") {
765 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
766 set_default_fade_in ();
769 _fade_in.load_state (*child);
772 } else if (child->name() == "FadeOut") {
776 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
777 set_default_fade_out ();
779 _fade_out.load_state (*child);
788 AudioRegion::set_fade_in_shape (FadeShape shape)
790 set_fade_in (shape, (jack_nframes_t) _fade_in.back()->when);
794 AudioRegion::set_fade_out_shape (FadeShape shape)
796 set_fade_out (shape, (jack_nframes_t) _fade_out.back()->when);
800 AudioRegion::set_fade_in (FadeShape shape, jack_nframes_t len)
807 _fade_in.add (0.0, 0.0);
808 _fade_in.add (len, 1.0);
813 _fade_in.add (len * 0.389401, 0.0333333);
814 _fade_in.add (len * 0.629032, 0.0861111);
815 _fade_in.add (len * 0.829493, 0.233333);
816 _fade_in.add (len * 0.9447, 0.483333);
817 _fade_in.add (len * 0.976959, 0.697222);
818 _fade_in.add (len, 1);
823 _fade_in.add (len * 0.0207373, 0.197222);
824 _fade_in.add (len * 0.0645161, 0.525);
825 _fade_in.add (len * 0.152074, 0.802778);
826 _fade_in.add (len * 0.276498, 0.919444);
827 _fade_in.add (len * 0.481567, 0.980556);
828 _fade_in.add (len * 0.767281, 1);
829 _fade_in.add (len, 1);
834 _fade_in.add (len * 0.0737327, 0.308333);
835 _fade_in.add (len * 0.246544, 0.658333);
836 _fade_in.add (len * 0.470046, 0.886111);
837 _fade_in.add (len * 0.652074, 0.972222);
838 _fade_in.add (len * 0.771889, 0.988889);
839 _fade_in.add (len, 1);
844 _fade_in.add (len * 0.304147, 0.0694444);
845 _fade_in.add (len * 0.529954, 0.152778);
846 _fade_in.add (len * 0.725806, 0.333333);
847 _fade_in.add (len * 0.847926, 0.558333);
848 _fade_in.add (len * 0.919355, 0.730556);
849 _fade_in.add (len, 1);
854 _fade_in_shape = shape;
857 save_state (_("fade in change"));
860 send_change (FadeInChanged);
864 AudioRegion::set_fade_out (FadeShape shape, jack_nframes_t len)
871 _fade_out.add (len * 0, 1);
872 _fade_out.add (len * 0.023041, 0.697222);
873 _fade_out.add (len * 0.0553, 0.483333);
874 _fade_out.add (len * 0.170507, 0.233333);
875 _fade_out.add (len * 0.370968, 0.0861111);
876 _fade_out.add (len * 0.610599, 0.0333333);
877 _fade_out.add (len * 1, 0);
881 _fade_out.add (len * 0, 1);
882 _fade_out.add (len * 0.228111, 0.988889);
883 _fade_out.add (len * 0.347926, 0.972222);
884 _fade_out.add (len * 0.529954, 0.886111);
885 _fade_out.add (len * 0.753456, 0.658333);
886 _fade_out.add (len * 0.9262673, 0.308333);
887 _fade_out.add (len * 1, 0);
891 _fade_out.add (len * 0, 1);
892 _fade_out.add (len * 0.305556, 1);
893 _fade_out.add (len * 0.548611, 0.991736);
894 _fade_out.add (len * 0.759259, 0.931129);
895 _fade_out.add (len * 0.918981, 0.68595);
896 _fade_out.add (len * 0.976852, 0.22865);
897 _fade_out.add (len * 1, 0);
901 _fade_out.add (len * 0, 1);
902 _fade_out.add (len * 0.080645, 0.730556);
903 _fade_out.add (len * 0.277778, 0.289256);
904 _fade_out.add (len * 0.470046, 0.152778);
905 _fade_out.add (len * 0.695853, 0.0694444);
906 _fade_out.add (len * 1, 0);
910 _fade_out.add (len * 0, 1);
911 _fade_out.add (len * 1, 0);
916 _fade_out_shape = shape;
919 save_state (_("fade in change"));
922 send_change (FadeOutChanged);
926 AudioRegion::set_fade_in_length (jack_nframes_t len)
928 bool changed = _fade_in.extend_to (len);
931 _flags = Flag (_flags & ~DefaultFadeIn);
935 snprintf (buf, sizeof (buf), "fade in length changed to %u", len);
939 send_change (FadeInChanged);
944 AudioRegion::set_fade_out_length (jack_nframes_t len)
946 bool changed = _fade_out.extend_to (len);
949 _flags = Flag (_flags & ~DefaultFadeOut);
953 snprintf (buf, sizeof (buf), "fade out length changed to %u", len);
958 send_change (FadeOutChanged);
962 AudioRegion::set_fade_in_active (bool yn)
964 if (yn == (_flags & FadeIn)) {
968 _flags = Flag (_flags|FadeIn);
970 _flags = Flag (_flags & ~FadeIn);
973 send_change (FadeInActiveChanged);
977 AudioRegion::set_fade_out_active (bool yn)
979 if (yn == (_flags & FadeOut)) {
983 _flags = Flag (_flags | FadeOut);
985 _flags = Flag (_flags & ~FadeOut);
988 send_change (FadeOutActiveChanged);
992 AudioRegion::set_default_fade_in ()
994 set_fade_in (Linear, 64);
998 AudioRegion::set_default_fade_out ()
1000 set_fade_out (Linear, 64);
1004 AudioRegion::set_default_fades ()
1006 _fade_in_disabled = 0;
1007 _fade_out_disabled = 0;
1008 set_default_fade_in ();
1009 set_default_fade_out ();
1013 AudioRegion::set_default_envelope ()
1015 _envelope.freeze ();
1017 _envelope.add (0, 1.0f);
1018 _envelope.add (_length, 1.0f);
1023 AudioRegion::recompute_at_end ()
1025 /* our length has changed. recompute a new final point by interpolating
1026 based on the the existing curve.
1029 _envelope.freeze ();
1030 _envelope.truncate_end (_length);
1031 _envelope.set_max_xval (_length);
1034 if (_fade_in.back()->when > _length) {
1035 _fade_in.extend_to (_length);
1036 send_change (FadeInChanged);
1039 if (_fade_out.back()->when > _length) {
1040 _fade_out.extend_to (_length);
1041 send_change (FadeOutChanged);
1046 AudioRegion::recompute_at_start ()
1048 /* as above, but the shift was from the front */
1050 _envelope.truncate_start (_length);
1052 if (_fade_in.back()->when > _length) {
1053 _fade_in.extend_to (_length);
1054 send_change (FadeInChanged);
1057 if (_fade_out.back()->when > _length) {
1058 _fade_out.extend_to (_length);
1059 send_change (FadeOutChanged);
1064 AudioRegion::separate_by_channel (Session& session, vector<AudioRegion*>& v) const
1069 for (SourceList::const_iterator i = master_sources.begin(); i != master_sources.end(); ++i) {
1072 srcs.push_back (*i);
1074 /* generate a new name */
1076 if (session.region_name (new_name, _name)) {
1080 /* create a copy with just one source */
1082 v.push_back (new AudioRegion (srcs, _start, _length, new_name, _layer, _flags));
1089 AudioRegion::source_deleted (Source* ignored)
1095 AudioRegion::lock_sources ()
1097 SourceList::iterator i;
1098 set<AudioSource*> unique_srcs;
1100 for (i = sources.begin(); i != sources.end(); ++i) {
1101 unique_srcs.insert (*i);
1105 for (i = master_sources.begin(); i != master_sources.end(); ++i) {
1106 if (unique_srcs.find (*i) == unique_srcs.end()) {
1113 AudioRegion::unlock_sources ()
1115 SourceList::iterator i;
1116 set<AudioSource*> unique_srcs;
1118 for (i = sources.begin(); i != sources.end(); ++i) {
1119 unique_srcs.insert (*i);
1123 for (i = master_sources.begin(); i != master_sources.end(); ++i) {
1124 if (unique_srcs.find (*i) == unique_srcs.end()) {
1131 AudioRegion::master_source_names ()
1133 SourceList::iterator i;
1135 vector<string> names;
1136 for (i = master_sources.begin(); i != master_sources.end(); ++i) {
1137 names.push_back((*i)->name());
1144 AudioRegion::region_list_equivalent (const AudioRegion& other) const
1146 return size_equivalent (other) && source_equivalent (other) && _name == other._name;
1150 AudioRegion::source_equivalent (const AudioRegion& other) const
1152 SourceList::const_iterator i;
1153 SourceList::const_iterator io;
1155 for (i = sources.begin(), io = other.sources.begin(); i != sources.end() && io != other.sources.end(); ++i, ++io) {
1156 if ((*i)->id() != (*io)->id()) {
1161 for (i = master_sources.begin(), io = other.master_sources.begin(); i != master_sources.end() && io != other.master_sources.end(); ++i, ++io) {
1162 if ((*i)->id() != (*io)->id()) {
1171 AudioRegion::overlap_equivalent (const AudioRegion& other) const
1173 return coverage (other.first_frame(), other.last_frame()) != OverlapNone;
1177 AudioRegion::equivalent (const AudioRegion& other) const
1179 return _start == other._start &&
1180 _position == other._position &&
1181 _length == other._length;
1185 AudioRegion::size_equivalent (const AudioRegion& other) const
1187 return _start == other._start &&
1188 _length == other._length;
1192 AudioRegion::apply (AudioFilter& filter)
1194 return filter.run (*this);
1198 AudioRegion::exportme (Session& session, AudioExportSpecification& spec)
1200 const jack_nframes_t blocksize = 4096;
1201 jack_nframes_t to_read;
1204 spec.channels = sources.size();
1206 if (spec.prepare (blocksize, session.frame_rate())) {
1211 spec.total_frames = _length;
1213 while (spec.pos < _length && !spec.stop) {
1216 /* step 1: interleave */
1218 to_read = min (_length - spec.pos, blocksize);
1220 if (spec.channels == 1) {
1222 if (sources.front()->read (spec.dataF, _start + spec.pos, to_read, 0) != to_read) {
1228 Sample buf[blocksize];
1230 for (uint32_t chan = 0; chan < spec.channels; ++chan) {
1232 if (sources[chan]->read (buf, _start + spec.pos, to_read, 0) != to_read) {
1236 for (jack_nframes_t x = 0; x < to_read; ++x) {
1237 spec.dataF[chan+(x*spec.channels)] = buf[x];
1242 if (spec.process (to_read)) {
1246 spec.pos += to_read;
1247 spec.progress = (double) spec.pos /_length;
1254 spec.running = false;
1255 spec.status = status;
1262 AudioRegion::get_parent()
1267 r = _playlist->session().find_whole_file_parent (*this);
1274 AudioRegion::set_scale_amplitude (gain_t g)
1276 _scale_amplitude = g;
1278 /* tell the diskstream we're in */
1281 _playlist->Modified();
1284 /* tell everybody else */
1286 send_change (ScaleAmplitudeChanged);
1290 AudioRegion::normalize_to (float target_dB)
1292 const jack_nframes_t blocksize = 256 * 1048;
1293 Sample buf[blocksize];
1294 char workbuf[blocksize * 4];
1295 jack_nframes_t fpos;
1296 jack_nframes_t fend;
1297 jack_nframes_t to_read;
1299 gain_t target = dB_to_coefficient (target_dB);
1301 if (target == 1.0f) {
1302 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1303 that we may have clipped.
1305 target -= FLT_EPSILON;
1309 fend = _start + _length;
1311 /* first pass: find max amplitude */
1313 while (fpos < fend) {
1317 to_read = min (fend - fpos, blocksize);
1319 for (n = 0; n < n_channels(); ++n) {
1323 if (source (n).read (buf, fpos, to_read, workbuf) != to_read) {
1327 maxamp = Session::compute_peak (buf, to_read, maxamp);
1333 if (maxamp == 0.0f) {
1334 /* don't even try */
1338 if (maxamp == target) {
1339 /* we can't do anything useful */
1343 /* compute scale factor */
1345 _scale_amplitude = target/maxamp;
1349 snprintf (buf, sizeof (buf), _("normalized to %.2fdB"), target_dB);
1353 /* tell the diskstream we're in */
1356 _playlist->Modified();
1359 /* tell everybody else */
1361 send_change (ScaleAmplitudeChanged);
1365 AudioRegion::envelope_changed (Change ignored)
1367 save_state (_("envelope change"));
1368 send_change (EnvelopeChanged);
1372 AudioRegion::suspend_fade_in ()
1374 if (++_fade_in_disabled == 1) {
1375 set_fade_in_active (false);
1380 AudioRegion::resume_fade_in ()
1382 if (_fade_in_disabled && --_fade_in_disabled == 0) {
1383 set_fade_in_active (true);
1388 AudioRegion::suspend_fade_out ()
1390 if (++_fade_out_disabled == 1) {
1391 set_fade_out_active (false);
1396 AudioRegion::resume_fade_out ()
1398 if (_fade_out_disabled && --_fade_out_disabled == 0) {
1399 set_fade_out_active (true);
1404 AudioRegion::speed_mismatch (float sr) const
1406 if (sources.empty()) {
1407 /* impossible, but ... */
1411 float fsr = sources.front()->sample_rate();
1418 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)
1420 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (jack_nframes_t) npeaks, (jack_nframes_t) start, (jack_nframes_t) cnt, n_chan,samples_per_unit);
1423 uint32_t region_length_from_c (void *arg)
1426 return ((AudioRegion *) arg)->length();
1429 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1431 return ( (AudioRegion *) arg)->source().available_peaks (zoom_factor) ;