#include <errno.h>
#include <cmath>
+#include <fcntl.h>
#include <pbd/error.h>
#include <ardour/destructive_filesource.h>
/* XXXX THIS IS NOT THE RIGHT XFADE CURVE: USE A PROPER VOLUMETRIC EQUAL POWER CURVE */
- out_coefficient[n] = n/(gain_t) xfade_frames;
- in_coefficient[n] = 1.0 - out_coefficient[n];
+ in_coefficient[n] = n/(gain_t) (xfade_frames-1); /* 0 .. 1 */
+ out_coefficient[n] = 1.0 - in_coefficient[n]; /* 1 .. 0 */
}
}
int
DestructiveFileSource::seek (jack_nframes_t frame)
{
- file_pos = data_offset + (sizeof (Sample) * frame);
+// file_pos = data_offset + (sizeof (Sample) * frame);
+ cerr << _name << " Seek to " << frame << " = " << data_offset + (sizeof (Sample) * frame) << endl;
return 0;
}
void
-DestructiveFileSource::mark_capture_start ()
+DestructiveFileSource::mark_capture_start (jack_nframes_t pos)
{
_capture_start = true;
+ capture_start_frame = pos;
}
void
}
jack_nframes_t
-DestructiveFileSource::crossfade (Sample* data, jack_nframes_t cnt, int dir)
+DestructiveFileSource::crossfade (Sample* data, jack_nframes_t cnt, int fade_in)
{
jack_nframes_t xfade = min (xfade_frames, cnt);
jack_nframes_t xfade_bytes = xfade * sizeof (Sample);
-
- if (::pread64 (fd, (char *) xfade_buf, xfade_bytes, file_pos) != (off64_t) xfade_bytes) {
- error << string_compose(_("FileSource: \"%1\" bad read (%2)"), _path, strerror (errno)) << endmsg;
- return 0;
+ jack_nframes_t nofade = cnt - xfade;
+ jack_nframes_t nofade_bytes = nofade * sizeof (Sample);
+ Sample* fade_data = 0;
+ off_t fade_position = 0;
+
+ if (fade_in) {
+ fade_position = file_pos;
+ fade_data = data;
+ } else {
+ fade_position = file_pos + nofade_bytes;
+ fade_data = data + nofade;
+ }
+
+ if (::pread64 (fd, (char *) xfade_buf, xfade_bytes, fade_position) != (off64_t) xfade_bytes) {
+ if (errno == EAGAIN) {
+ /* no data there, so no xfade */
+
+ xfade = 0;
+ xfade_bytes = 0;
+ nofade = cnt;
+ nofade_bytes = nofade * sizeof (Sample);
+
+ } else {
+ error << string_compose(_("FileSource: \"%1\" bad read (%2: %3)"), _path, errno, strerror (errno)) << endmsg;
+ return 0;
+ }
+ }
+
+ if (nofade && !fade_in) {
+ cerr << "write " << nofade_bytes << " of prefade OUT data to " << file_pos << " .. " << file_pos + nofade_bytes << endl;
+ if (::pwrite64 (fd, (char *) data, nofade_bytes, file_pos) != (off64_t) nofade_bytes) {
+ error << string_compose(_("FileSource: \"%1\" bad write (%2)"), _path, strerror (errno)) << endmsg;
+ return 0;
+ }
}
if (xfade == xfade_frames) {
+ jack_nframes_t n;
+
/* use the standard xfade curve */
- if (dir) {
+ if (fade_in) {
/* fade new material in */
-
- for (jack_nframes_t n = 0; n < xfade; ++n) {
- xfade_buf[n] = (xfade_buf[n] * out_coefficient[n]) + (data[n] * in_coefficient[n]);
+
+ for (n = 0; n < xfade; ++n) {
+ xfade_buf[n] = (xfade_buf[n] * out_coefficient[n]) + (fade_data[n] * in_coefficient[n]);
}
+
} else {
+
/* fade new material out */
-
- for (jack_nframes_t n = 0; n < xfade; ++n) {
- xfade_buf[n] = (xfade_buf[n] * in_coefficient[n]) + (data[n] * out_coefficient[n]);
+ for (n = 0; n < xfade; ++n) {
+ xfade_buf[n] = (xfade_buf[n] * in_coefficient[n]) + (fade_data[n] * out_coefficient[n]);
}
}
-
- } else {
+ } else if (xfade) {
/* short xfade, compute custom curve */
+ /* XXX COMPUTE THE CURVE, DAMMIT! */
+
for (jack_nframes_t n = 0; n < xfade; ++n) {
- xfade_buf[n] = (xfade_buf[n] * out_coefficient[n]) + (data[n] * in_coefficient[n]);
+ xfade_buf[n] = (xfade_buf[n] * out_coefficient[n]) + (fade_data[n] * in_coefficient[n]);
}
}
-
- if (::pwrite64 (fd, (char *) xfade_buf, xfade, file_pos) != (off64_t) xfade_bytes) {
- error << string_compose(_("FileSource: \"%1\" bad write (%2)"), _path, strerror (errno)) << endmsg;
- return 0;
- }
- /* don't advance file_pos here; if the write fails, we want it left where it was before the overall
- write, not in the middle of it.
- */
+ if (xfade) {
+ cerr << "write " << xfade_bytes << " of xfade data to " << fade_position << " .. " << fade_position + xfade_bytes << endl;
+ if (::pwrite64 (fd, (char *) xfade_buf, xfade_bytes, fade_position) != (off64_t) xfade_bytes) {
+ error << string_compose(_("FileSource: \"%1\" bad write (%2)"), _path, strerror (errno)) << endmsg;
+ return 0;
+ }
+ }
- if (xfade < cnt) {
- jack_nframes_t remaining = (cnt - xfade);
- int32_t bytes = remaining * sizeof (Sample);
-
- if (::pwrite64 (fd, (char *) data + remaining, bytes, file_pos) != (off64_t) bytes) {
+ if (fade_in && nofade) {
+ cerr << "write " << nofade_bytes << " of postfade IN data to " << file_pos + xfade_bytes << " .. "
+ << file_pos + xfade_bytes + nofade_bytes << endl;
+ if (::pwrite64 (fd, (char *) (data + xfade), nofade_bytes, file_pos + xfade_bytes) != (off64_t) nofade_bytes) {
error << string_compose(_("FileSource: \"%1\" bad write (%2)"), _path, strerror (errno)) << endmsg;
return 0;
}
}
- file_pos += cnt;
-
return cnt;
}
jack_nframes_t
DestructiveFileSource::write (Sample* data, jack_nframes_t cnt)
{
+ cerr << _name << ": write " << cnt << " to " << file_pos << " start ? " << _capture_start << " end ? " << _capture_end << endl;
+
{
LockMonitor lm (_lock, __LINE__, __FILE__);
if (_capture_start) {
_capture_start = false;
+ _capture_end = false;
+
+ /* move to the correct location place */
+ file_pos = data_offset + (capture_start_frame * sizeof (Sample));
+
+ cerr << "First byte of capture will be at " << file_pos << endl;
+
if (crossfade (data, cnt, 1) != cnt) {
return 0;
}
+
} else if (_capture_end) {
+ _capture_start = false;
_capture_end = false;
+
if (crossfade (data, cnt, 0) != cnt) {
return 0;
}
}
}
- oldlen = file_pos;
+ oldlen = _length;
if (file_pos + cnt > _length) {
_length += cnt;
}
_write_data_count = byte_cnt;
file_pos += byte_cnt;
+ cerr << "at end of write, file_pos = " << file_pos << endl;
+
if (_build_peakfiles) {
PeakBuildRecord *pbr = 0;
return cnt;
}
+jack_nframes_t
+DestructiveFileSource::last_capture_start_frame () const
+{
+ return capture_start_frame;
+}
}
+ if (_flags & Recordable) {
+ cerr << "START RECORD @ " << capture_start_frame << " = " << capture_start_frame * sizeof (Sample) << endl;
+ for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
+ (*chan).write_source->mark_capture_start (capture_start_frame);
+ }
+ }
+
} else if (!record_enabled() || !can_record) {
/* stop recording */
for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
(*chan).playback_buf->reset ();
(*chan).capture_buf->reset ();
- if (destructive()) {
- DestructiveFileSource* dfs = dynamic_cast<DestructiveFileSource*> ((*chan).write_source);
- if (dfs) {
- dfs->seek (frame);
- }
+ if ((*chan).write_source) {
+ (*chan).write_source->seek (frame);
}
}
*/
try {
- region = new AudioRegion (srcs, 0, total_capture,
+ region = new AudioRegion (srcs, channels[0].write_source->last_capture_start_frame(), total_capture,
region_name_from_path (channels[0].write_source->name()),
0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile));
_session.add_undo (_playlist->get_memento());
_playlist->freeze ();
- for (buffer_position = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
+ for (buffer_position = channels[0].write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
string region_name;
_session.region_name (region_name, _name, false);
{
was_recording = false;
+ if (_flags & Recordable) {
+ cerr << "STOP CAPTURE\n";
+ for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
+ (*chan).write_source->mark_capture_end ();
+ }
+ }
+
if (capture_captured == 0) {
return;
}