2 Copyright (C) 2000 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.
23 /* This is is very hacky way to get pread and pwrite declarations.
24 First, include <features.h> so that we can avoid its #undef __USE_UNIX98.
25 Then define __USE_UNIX98, include <unistd.h>, and then undef it
26 again. If #define _XOPEN_SOURCE actually worked, I'd use that, but
27 despite claims in the header that it does, it doesn't.
29 features.h isn't available on osx and it compiles fine without it.
32 #ifdef HAVE_FEATURES_H
37 // #define _XOPEN_SOURCE 500
49 #include <sys/types.h>
52 #include <sys/utsname.h>
54 #include <cstdio> /* for rename(2) */
58 #include <pbd/stl_delete.h>
60 #include <glibmm/thread.h>
61 #include <pbd/pathscanner.h>
63 #include <ardour/ardour.h>
64 #include <ardour/version.h>
65 #include <ardour/source.h>
66 #include <ardour/filesource.h>
67 #include <ardour/session.h>
68 #include <ardour/cycle_timer.h>
69 #include <ardour/pcm_utils.h>
73 using namespace ARDOUR;
75 string prepare_string(string& regex);
77 char FileSource::bwf_country_code[3] = "us";
78 char FileSource::bwf_organization_code[4] = "las";
79 char FileSource::bwf_serial_number[13] = "000000000000";
80 string FileSource::search_path;
82 #undef WE_ARE_BIGENDIAN
84 #define WE_ARE_BIGENDIAN true
86 #define WE_ARE_BIGENDIAN false
89 #define Swap_32(value) \
90 (((((uint32_t)value)<<24) & 0xFF000000) | \
91 ((((uint32_t)value)<< 8) & 0x00FF0000) | \
92 ((((uint32_t)value)>> 8) & 0x0000FF00) | \
93 ((((uint32_t)value)>>24) & 0x000000FF))
95 #define Swap_16(value) \
96 (((((uint16_t)value)>> 8) & 0x000000FF) | \
97 ((((uint16_t)value)<< 8) & 0x0000FF00))
101 FileSource::set_search_path (string p)
106 FileSource::FileSource (string pathstr, jack_nframes_t rate, bool repair_first, SampleFormat samp_format)
108 /* constructor used when the file cannot already exist or might be damaged */
109 _sample_format = samp_format;
110 if (samp_format == FormatInt24) {
113 _sample_size = sizeof(float);
116 if (repair_first && repair (pathstr, rate)) {
117 throw failed_constructor ();
120 if (init (pathstr, false, rate)) {
121 throw failed_constructor ();
124 SourceCreated (this); /* EMIT SIGNAL */
127 FileSource::FileSource (const XMLNode& node, jack_nframes_t rate)
130 _sample_format = FormatFloat;
131 _sample_size = sizeof(float);
133 if (set_state (node)) {
134 throw failed_constructor();
137 /* constructor used when the file must already exist */
139 if (init (_name, true, rate)) {
140 throw failed_constructor ();
143 SourceCreated (this); /* EMIT SIGNAL */
147 FileSource::init (string pathstr, bool must_exist, jack_nframes_t rate)
149 bool new_file = false;
153 /* all native files end in .wav. this lets us discard
154 SndFileSource paths, which have ":N" at the end to
155 indicate which channel to read from, as well as any
156 other kind of non-native file. obviously, there
157 are more subtle checks later on.
160 if (pathstr.length() < 4 || pathstr.rfind (".wav") != pathstr.length() - 4) {
167 remove_at_unref = false;
168 next_peak_clear_should_notify = false;
169 allow_remove_if_empty = true;
171 if (pathstr[0] != '/') {
173 /* find pathstr in search path */
175 if (search_path.length() == 0) {
176 error << _("FileSource: search path not set") << endmsg;
180 /* force exact match on the filename component by prefixing the regexp.
181 otherwise, "Drums-2.wav" matches "Comp_Drums-2.wav".
185 regexp += prepare_string(pathstr);
188 vector<string*>* result = scanner (search_path, regexp, false, true, -1);
190 if (result == 0 || result->size() == 0) {
191 error << string_compose (_("FileSource: \"%1\" not found when searching %2 using %3"),
192 pathstr, search_path, regexp) << endmsg;
196 if (result->size() > 1) {
197 string msg = string_compose (_("FileSource: \"%1\" is ambigous when searching %2\n\t"), pathstr, search_path);
198 vector<string*>::iterator x = result->begin();
204 if (x == result->end()) {
211 error << msg << endmsg;
216 _path = *(result->front());
218 vector_delete (result);
223 /* old style sessions include full paths */
226 _name = pathstr.substr (pathstr.find_last_of ('/') + 1);
230 if (access (_path.c_str(), F_OK) != 0) {
232 error << string_compose(_("Filesource: cannot find required file (%1): %2"), _path, strerror (errno)) << endmsg;
237 if (errno == ENOENT) {
240 error << string_compose(_("Filesource: cannot check for existing file (%1): %2"), _path, strerror (errno)) << endmsg;
245 if ((fd = open64 (_path.c_str(), O_RDWR|O_CREAT, 0644)) < 0) {
246 error << string_compose(_("FileSource: could not open \"%1\": (%2)"), _path, strerror (errno)) << endmsg;
250 /* if there was no timestamp available via XML,
251 then get it from the filesystem.
254 if (_timestamp == 0) {
257 fstat (fd, &statbuf);
258 _timestamp = statbuf.st_mtime;
261 if (lseek (fd, 0, SEEK_END) == 0) {
265 /* check that its a RIFF/WAVE format file */
269 switch (Config->get_native_file_header_format()) {
278 if (fill_header (rate)) {
279 error << string_compose (_("FileSource: cannot write header in %1"), _path) << endmsg;
287 now = localtime (&xnow);
289 update_header (0, *now, xnow);
293 if (discover_chunks (must_exist)) {
294 error << string_compose (_("FileSource: cannot locate chunks in %1"), _path) << endmsg;
298 if (read_header (must_exist)) {
299 error << string_compose (_("FileSource: cannot read header in %1"), _path) << endmsg;
303 if (check_header (rate, must_exist)) {
304 error << string_compose (_("FileSource: cannot check header in %1"), _path) << endmsg;
308 compute_header_size ();
311 if ((ret = initialize_peakfile (new_file, _path))) {
312 error << string_compose (_("FileSource: cannot initialize peakfile for %1 as %2"), _path, peakpath) << endmsg;
323 unlink (_path.c_str());
331 FileSource::~FileSource ()
333 GoingAway (this); /* EMIT SIGNAL */
337 if (remove_at_unref || (is_empty (_path) && allow_remove_if_empty)) {
338 unlink (_path.c_str());
339 unlink (peakpath.c_str());
347 FileSource::set_allow_remove_if_empty (bool yn)
349 allow_remove_if_empty = yn;
353 FileSource::set_name (string newname, bool destructive)
355 Glib::Mutex::Lock lm (_lock);
356 string oldpath = _path;
357 string newpath = Session::change_audio_path_by_name (oldpath, _name, newname, destructive);
359 if (newpath.empty()) {
360 error << string_compose (_("programming error: %1"), "cannot generate a changed audio path") << endmsg;
364 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
365 error << string_compose (_("cannot rename audio file for %1 to %2"), _name, newpath) << endmsg;
369 _name = Glib::path_get_basename (newpath);
372 return rename_peakfile (peak_path (_path));
376 FileSource::peak_path (string audio_path)
378 return Session::peak_path_from_audio_path (audio_path);
382 FileSource::discover_chunks (bool silent)
387 char null_terminated_id[5];
390 if ((end = lseek (fd, 0, SEEK_END)) < 0) {
391 error << _("FileSource: cannot seek to end of file") << endmsg;
395 if (::pread64 (fd, &rw, sizeof (rw), 0) != sizeof (rw)) {
396 error << _("FileSource: cannot read RIFF/WAVE chunk from file") << endmsg;
400 if (memcmp (rw.id, "RIFF", 4) == 0 && memcmp (rw.text, "WAVE", 4) == 0) {
401 header.bigendian = false;
403 else if (memcmp(rw.id, "RIFX", 4) == 0 && memcmp (rw.text, "WAVE", 4) == 0) {
404 header.bigendian = true;
408 error << string_compose (_("FileSource %1: not a RIFF/WAVE file"), _path) << endmsg;
413 null_terminated_id[4] = '\0';
415 /* OK, its a RIFF/WAVE file. Find each chunk */
417 doswap = header.bigendian != WE_ARE_BIGENDIAN;
425 memcpy (null_terminated_id, rw.id, 4);
426 chunk_info.push_back (ChunkInfo (null_terminated_id, rw.size, 0));
428 offset = sizeof (rw);
430 while (offset < end) {
432 GenericChunk this_chunk;
434 if (::pread64 (fd, &this_chunk, sizeof (this_chunk), offset) != sizeof (this_chunk)) {
435 error << _("FileSource: can't read a chunk") << endmsg;
440 swap_endian(this_chunk);
443 memcpy (null_terminated_id, this_chunk.id, 4);
445 /* do sanity check and possible correction to legacy ardour RIFF wavs
446 created on big endian platforms. after swapping, the size field will not be
447 in range for the fmt chunk
449 if ((memcmp(null_terminated_id, "fmt ", 4) == 0 || memcmp(null_terminated_id, "bext", 4) == 0)
450 && !header.bigendian && (this_chunk.size > 700 || this_chunk.size < 0))
452 warning << _("filesource: correcting mis-written RIFF file to become a RIFX: ") << name() << endmsg;
454 memcpy (&rw.id, "RIFX", 4);
455 ::pwrite64 (fd, &rw.id, 4, 0);
456 header.bigendian = true;
457 // fix wave chunk already read
460 doswap = header.bigendian != WE_ARE_BIGENDIAN;
462 // now reset offset and continue the loop
463 // to reread all the chunks
465 memcpy (null_terminated_id, rw.id, 4);
466 chunk_info.push_back (ChunkInfo (null_terminated_id, rw.size, 0));
467 offset = sizeof (rw);
473 if ((memcmp(null_terminated_id, "data", 4) == 0))
474 if ((this_chunk.size == 0) || (this_chunk.size > (end - offset)))
475 this_chunk.size = end - offset;
477 chunk_info.push_back (ChunkInfo (null_terminated_id, this_chunk.size, offset));
479 /* skip to the next chunk */
481 offset += sizeof(GenericChunk) + this_chunk.size;
488 FileSource::swap_endian (GenericChunk & chunk) const
490 chunk.size = Swap_32(chunk.size);
494 FileSource::swap_endian (FMTChunk & chunk) const
496 chunk.size = Swap_32(chunk.size);
498 chunk.formatTag = Swap_16(chunk.formatTag);
499 chunk.nChannels = Swap_16(chunk.nChannels);
500 chunk.nSamplesPerSec = Swap_32(chunk.nSamplesPerSec);
501 chunk.nAvgBytesPerSec = Swap_32(chunk.nAvgBytesPerSec);
502 chunk.nBlockAlign = Swap_16(chunk.nBlockAlign);
503 chunk.nBitsPerSample = Swap_16(chunk.nBitsPerSample);
507 FileSource::swap_endian (BroadcastChunk & chunk) const
509 chunk.size = Swap_32(chunk.size);
511 chunk.time_reference_low = Swap_32(chunk.time_reference_low);
512 chunk.time_reference_high = Swap_32(chunk.time_reference_high);
513 chunk.version = Swap_16(chunk.version);
516 void FileSource::swap_endian (Sample *buf, jack_nframes_t cnt) const
518 for (jack_nframes_t n=0; n < cnt; ++n) {
519 uint32_t * tmp = (uint32_t *) &buf[n];
520 *tmp = Swap_32(*tmp);
525 FileSource::ChunkInfo*
526 FileSource::lookup_chunk (string what)
528 for (vector<ChunkInfo>::iterator i = chunk_info.begin(); i != chunk_info.end(); ++i) {
529 if ((*i).name == what) {
537 FileSource::fill_header (jack_nframes_t rate)
541 if (WE_ARE_BIGENDIAN) {
542 memcpy (header.wave.id, "RIFX", 4);
543 header.bigendian = true;
546 memcpy (header.wave.id, "RIFF", 4);
547 header.bigendian = false;
549 header.wave.size = 0; /* file size */
550 memcpy (header.wave.text, "WAVE", 4);
552 /* BROADCAST WAVE EXTENSION */
556 /* fill the entire BWF header with nulls */
558 memset (&header.bext, 0, sizeof (header.bext));
560 memcpy (header.bext.id, "bext", 4);
562 snprintf (header.bext.description, sizeof (header.bext.description), "%s", "ambiguity is clearer than precision.");
564 struct passwd *pwinfo;
565 struct utsname utsinfo;
567 if ((pwinfo = getpwuid (getuid())) == 0) {
568 error << string_compose(_("FileSource: cannot get user information for BWF header (%1)"), strerror(errno)) << endmsg;
571 if (uname (&utsinfo)) {
572 error << string_compose(_("FileSource: cannot get host information for BWF header (%1)"), strerror(errno)) << endmsg;
576 snprintf (header.bext.originator, sizeof (header.bext.originator), "ardour:%s:%s:%s:%s:%s)",
583 header.bext.version = 1;
585 /* XXX do something about this field */
587 snprintf (header.bext.umid, sizeof (header.bext.umid), "%s", "fnord");
589 /* add some coding history */
593 /* encode: PCM,rate,mono,24bit,ardour-version
595 Note that because we use JACK, there is no way to tell
596 what the original bit depth of the signal was.
599 snprintf (buf, sizeof(buf), "F=%u,A=PCM,M=mono,W=24,T=ardour-%d.%d.%d",
601 libardour_major_version,
602 libardour_minor_version,
603 libardour_micro_version);
605 header.coding_history.push_back (buf);
607 /* initial size reflects coding history + "\r\n" */
609 header.bext.size = sizeof (BroadcastChunk) - sizeof (GenericChunk) + strlen (buf) + 2;
612 memcpy (header.format.id, "fmt ", 4);
613 header.format.size = sizeof (FMTChunk) - sizeof (GenericChunk);
615 if (_sample_format == FormatInt24) {
616 header.format.formatTag = 1; // PCM
617 header.format.nBlockAlign = 3;
618 header.format.nBitsPerSample = 24;
621 header.format.formatTag = 3; /* little-endian IEEE float format */
622 header.format.nBlockAlign = 4;
623 header.format.nBitsPerSample = 32;
625 header.format.nChannels = 1; /* mono */
626 header.format.nSamplesPerSec = rate;
627 header.format.nAvgBytesPerSec = rate * _sample_size;
631 memcpy (header.data.id, "data", 4);
632 header.data.size = 0;
638 FileSource::compute_header_size ()
641 int32_t coding_history_size = 0;
643 end_of_file = lseek (fd, 0, SEEK_END);
647 /* include the coding history */
649 for (vector<string>::iterator i = header.coding_history.begin(); i != header.coding_history.end(); ++i) {
650 coding_history_size += (*i).length() + 2; // include "\r\n";
653 header.bext.size = sizeof (BroadcastChunk) - sizeof (GenericChunk) + coding_history_size;
654 data_offset = bwf_header_size + coding_history_size;
657 data_offset = wave_header_size;
660 if (end_of_file == 0) {
662 /* newfile condition */
665 /* include "WAVE" then all the chunk sizes (bext, fmt, data) */
666 header.wave.size = 4 + sizeof (BroadcastChunk) + coding_history_size + sizeof (FMTChunk) + sizeof (GenericChunk);
668 /* include "WAVE" then all the chunk sizes (fmt, data) */
669 header.wave.size = 4 + sizeof (FMTChunk) + sizeof (GenericChunk);
672 header.data.size = 0;
676 header.wave.size = end_of_file - 8; /* size of initial RIFF+size pseudo-chunk */
677 header.data.size = end_of_file - data_offset;
682 FileSource::update_header (jack_nframes_t when, struct tm& now, time_t tnow)
684 Glib::Mutex::Lock lm (_lock);
687 /* random code is 9 digits */
689 int random_code = random() % 999999999;
691 snprintf (header.bext.originator_reference, sizeof (header.bext.originator_reference), "%2s%3s%12s%02d%02d%02d%9d",
693 bwf_organization_code,
700 snprintf (header.bext.origination_date, sizeof (header.bext.origination_date), "%4d-%02d-%02d",
705 snprintf (header.bext.origination_time, sizeof (header.bext.origination_time), "%02d-%02d-%02d",
710 header.bext.time_reference_high = 0;
711 header.bext.time_reference_low = when;
714 compute_header_size ();
716 if (write_header()) {
717 error << string_compose(_("FileSource[%1]: cannot update data size: %2"), _path, strerror (errno)) << endmsg;
727 FileSource::read_header (bool silent)
729 /* we already have the chunk info, so just load up whatever we have */
733 if (header.bigendian == false && (info = lookup_chunk ("RIFF")) == 0) {
734 error << _("FileSource: can't find RIFF chunk info") << endmsg;
737 else if (header.bigendian == true && (info = lookup_chunk ("RIFX")) == 0) {
738 error << _("FileSource: can't find RIFX chunk info") << endmsg;
743 /* just fill this chunk/header ourselves, disk i/o is stupid */
745 if (header.bigendian) {
746 memcpy (header.wave.id, "RIFX", 4);
749 memcpy (header.wave.id, "RIFF", 4);
751 header.wave.size = 0;
752 memcpy (header.wave.text, "WAVE", 4);
754 if ((info = lookup_chunk ("bext")) != 0) {
758 if (::pread64 (fd, &header.bext, sizeof (header.bext), info->offset) != sizeof (header.bext)) {
759 error << _("FileSource: can't read RIFF chunk") << endmsg;
763 if (read_broadcast_data (*info)) {
768 if ((info = lookup_chunk ("fmt ")) == 0) {
769 error << _("FileSource: can't find format chunk info") << endmsg;
773 if (::pread64 (fd, &header.format, sizeof (header.format), info->offset) != sizeof (header.format)) {
774 error << _("FileSource: can't read format chunk") << endmsg;
778 if (header.bigendian != WE_ARE_BIGENDIAN) {
779 swap_endian (header.format);
782 if ((info = lookup_chunk ("data")) == 0) {
783 error << _("FileSource: can't find data chunk info") << endmsg;
787 if (::pread64 (fd, &header.data, sizeof (header.data), info->offset) != sizeof (header.data)) {
788 error << _("FileSource: can't read data chunk") << endmsg;
792 if (header.bigendian != WE_ARE_BIGENDIAN) {
793 swap_endian (header.data);
800 FileSource::read_broadcast_data (ChunkInfo& info)
802 int32_t coding_history_size;
804 if (::pread64 (fd, (char *) &header.bext, sizeof (header.bext), info.offset + sizeof (GenericChunk)) != sizeof (header.bext)) {
805 error << string_compose(_("FileSource: cannot read Broadcast Wave data from existing audio file \"%1\" (%2)"),
806 _path, strerror (errno)) << endmsg;
810 if (header.bigendian != WE_ARE_BIGENDIAN) {
811 swap_endian (header.bext);
814 if (info.size > sizeof (header.bext)) {
816 coding_history_size = info.size - (sizeof (header.bext) - sizeof (GenericChunk));
818 char data[coding_history_size];
820 if (::pread64 (fd, data, coding_history_size, info.offset + sizeof (BroadcastChunk)) != coding_history_size) {
821 error << string_compose(_("FileSource: cannot read Broadcast Wave coding history from audio file \"%1\" (%2)"),
822 _path, strerror (errno)) << endmsg;
826 /* elements of the coding history are divided by \r\n */
829 char *end = data + coding_history_size;
833 if (*p == '\r' && (p+1) != end && *(p+1) == '\n') {
835 header.coding_history.push_back (tmp);
850 FileSource::check_header (jack_nframes_t rate, bool silent)
852 if (header.format.formatTag == 1 && header.format.nBitsPerSample == 24) {
854 _sample_format = FormatInt24;
856 } else if (header.format.formatTag == 3) {
858 _sample_format = FormatFloat;
863 error << string_compose(_("FileSource \"%1\" does not use valid sample format.\n"
864 "This is probably a programming error."), _path) << endmsg;
869 /* compute the apparent length of the data */
873 for (vector<ChunkInfo>::iterator i = chunk_info.begin(); i != chunk_info.end();) {
874 vector<ChunkInfo>::iterator n;
879 if ((*i).name == "data") {
881 data_offset = (*i).offset + sizeof (GenericChunk);
883 if (n == chunk_info.end()) {
885 end_of_file = lseek (fd, 0, SEEK_END);
887 _length = end_of_file - data_offset;
890 _length = (*n).offset - data_offset;
893 _length /= sizeof (Sample);
901 if (data_offset == 0) {
902 error << string_compose(_("FileSource \"%1\" has no \"data\" chunk"), _path) << endmsg;
906 if (_length * sizeof (Sample) != (jack_nframes_t) header.data.size) {
907 warning << string_compose(_("%1: data length in header (%2) differs from implicit size in file (%3)"),
908 _path, header.data.size, _length * sizeof (Sample)) << endmsg;
911 // if ((jack_nframes_t) header.format.nSamplesPerSec != rate) {
912 // warning << string_compose(_("\"%1\" has a sample rate of %2 instead of %3 as used by this session"),
913 // _path, header.format.nSamplesPerSec, rate) << endmsg;
920 FileSource::sample_rate () const
922 return header.format.nSamplesPerSec;
926 FileSource::write_header()
930 /* write RIFF/WAVE boilerplate */
934 WAVEChunk wchunk = header.wave;
936 if (header.bigendian != WE_ARE_BIGENDIAN) {
940 if (::pwrite64 (fd, (char *) &wchunk, sizeof (wchunk), pos) != sizeof (wchunk)) {
941 error << string_compose(_("FileSource: cannot write WAVE chunk: %1"), strerror (errno)) << endmsg;
945 pos += sizeof (header.wave);
949 /* write broadcast chunk data without copy history */
951 BroadcastChunk bchunk = header.bext;
952 if (header.bigendian != WE_ARE_BIGENDIAN) {
953 swap_endian (bchunk);
956 if (::pwrite64 (fd, (char *) &bchunk, sizeof (bchunk), pos) != sizeof (bchunk)) {
960 pos += sizeof (header.bext);
962 /* write copy history */
964 for (vector<string>::iterator i = header.coding_history.begin(); i != header.coding_history.end(); ++i) {
970 if (::pwrite64 (fd, x.c_str(), x.length(), pos) != (int32_t) x.length()) {
978 /* write fmt and data chunks */
979 FMTChunk fchunk = header.format;
980 if (header.bigendian != WE_ARE_BIGENDIAN) {
981 swap_endian (fchunk);
984 if (::pwrite64 (fd, (char *) &fchunk, sizeof (fchunk), pos) != sizeof (fchunk)) {
985 error << string_compose(_("FileSource: cannot write format chunk: %1"), strerror (errno)) << endmsg;
989 pos += sizeof (header.format);
991 GenericChunk dchunk = header.data;
992 if (header.bigendian != WE_ARE_BIGENDIAN) {
993 swap_endian (dchunk);
996 if (::pwrite64 (fd, (char *) &dchunk, sizeof (dchunk), pos) != sizeof (dchunk)) {
997 error << string_compose(_("FileSource: cannot data chunk: %1"), strerror (errno)) << endmsg;
1005 FileSource::mark_for_remove ()
1007 remove_at_unref = true;
1011 FileSource::read (Sample *dst, jack_nframes_t start, jack_nframes_t cnt, char * workbuf) const
1013 Glib::Mutex::Lock lm (_lock);
1014 return read_unlocked (dst, start, cnt, workbuf);
1018 FileSource::read_unlocked (Sample *dst, jack_nframes_t start, jack_nframes_t cnt, char * workbuf) const
1020 jack_nframes_t file_cnt;
1022 if (start > _length) {
1024 /* read starts beyond end of data, just memset to zero */
1028 } else if (start + cnt > _length) {
1030 /* read ends beyond end of data, read some, memset the rest */
1032 file_cnt = _length - start;
1036 /* read is entirely within data */
1042 if (file_read(dst, start, file_cnt, workbuf) != (ssize_t) file_cnt) {
1047 if (file_cnt != cnt) {
1048 jack_nframes_t delta = cnt - file_cnt;
1049 memset (dst+file_cnt, 0, sizeof (Sample) * delta);
1056 FileSource::write (Sample *data, jack_nframes_t cnt, char * workbuf)
1059 Glib::Mutex::Lock lm (_lock);
1061 jack_nframes_t oldlen;
1062 int32_t frame_pos = _length;
1064 if (file_write(data, frame_pos, cnt, workbuf) != (ssize_t) cnt) {
1071 if (_build_peakfiles) {
1072 PeakBuildRecord *pbr = 0;
1074 if (pending_peak_builds.size()) {
1075 pbr = pending_peak_builds.back();
1078 if (pbr && pbr->frame + pbr->cnt == oldlen) {
1080 /* the last PBR extended to the start of the current write,
1081 so just extend it again.
1086 pending_peak_builds.push_back (new PeakBuildRecord (oldlen, cnt));
1089 _peaks_built = false;
1095 if (_build_peakfiles) {
1096 queue_for_peaks (*this);
1103 FileSource::write_float(Sample *data, jack_nframes_t framepos, jack_nframes_t cnt, char * workbuf)
1105 int32_t byte_cnt = cnt * _sample_size;
1106 int32_t byte_pos = data_offset + (framepos * _sample_size);
1109 if ((retval = ::pwrite64 (fd, (char *) data, byte_cnt, byte_pos)) != (ssize_t) byte_cnt) {
1110 error << string_compose(_("FileSource: \"%1\" bad write (%2)"), _path, strerror (errno)) << endmsg;
1112 return retval / _sample_size;
1119 _write_data_count = byte_cnt;
1125 FileSource::read_float (Sample *dst, jack_nframes_t start, jack_nframes_t cnt, char * workbuf) const
1128 ssize_t byte_cnt = (ssize_t) cnt * sizeof (Sample);
1131 /* open, read, close */
1133 if ((readfd = open64 (_path.c_str(), O_RDONLY)) < 0) {
1134 error << string_compose(_("FileSource: could not open \"%1\": (%2)"), _path, strerror (errno)) << endmsg;
1138 nread = ::pread64 (readfd, (char *) dst, byte_cnt, data_offset + (start * _sample_size));
1141 if (nread != byte_cnt) {
1143 cerr << "FileSource: \""
1145 << "\" bad read at frame "
1151 << ") frames [length = " << _length
1152 << " eor = " << start + cnt << "] ("
1155 << nread / sizeof (Sample)
1156 << " (bytes=" <<nread
1160 << start << '*' << sizeof(Sample)
1161 << " = " << data_offset + (start * sizeof(Sample))
1165 return nread / _sample_size;
1171 if (header.bigendian != WE_ARE_BIGENDIAN) {
1172 swap_endian(dst, cnt);
1175 _read_data_count = byte_cnt;
1181 FileSource::write_pcm_24(Sample *data, jack_nframes_t framepos, jack_nframes_t cnt, char * workbuf)
1183 int32_t byte_cnt = cnt * _sample_size;
1184 int32_t byte_pos = data_offset + (framepos * _sample_size);
1188 if (header.bigendian) {
1189 pcm_f2bet_clip_array (data, workbuf, cnt);
1191 pcm_f2let_clip_array (data, workbuf, cnt);
1194 if ((retval = ::pwrite64 (fd, (char *) workbuf, byte_cnt, byte_pos)) != (ssize_t) byte_cnt) {
1195 error << string_compose(_("FileSource: \"%1\" bad write (%2)"), _path, strerror (errno)) << endmsg;
1197 return retval / _sample_size;
1204 return (ssize_t) cnt;
1208 FileSource::read_pcm_24 (Sample *dst, jack_nframes_t start, jack_nframes_t cnt, char * workbuf) const
1211 ssize_t byte_cnt = (ssize_t) cnt * _sample_size;
1214 /* open, read, close */
1216 if ((readfd = open64 (_path.c_str(), O_RDONLY)) < 0) {
1217 error << string_compose(_("FileSource: could not open \"%1\": (%2)"), _path, strerror (errno)) << endmsg;
1221 nread = ::pread64 (readfd, (char *) workbuf, byte_cnt, data_offset + (start * _sample_size));
1224 if (nread != byte_cnt) {
1226 cerr << "May be OK - FileSource: \""
1228 << "\" bad 24bit read at frame "
1234 << ") frames [length = " << _length
1235 << " eor = " << start + cnt << "] ("
1238 << nread / sizeof (Sample)
1239 << " (bytes=" <<nread
1243 << start << '*' << sizeof(Sample)
1244 << " = " << data_offset + (start * sizeof(Sample))
1248 return nread / _sample_size;
1255 // convert from 24bit->float
1257 if (header.bigendian) {
1258 pcm_bet2f_array (workbuf, cnt, dst);
1260 pcm_let2f_array (workbuf, cnt, dst);
1263 _read_data_count = byte_cnt;
1265 return (ssize_t) cnt;
1270 FileSource::is_empty (string path)
1272 struct stat statbuf;
1274 stat (path.c_str(), &statbuf);
1276 /* its a bit of a problem if an audio file happens
1277 to be a regular WAVE file with just enough data
1278 to match the size of an empty BWF. hmmm. not very
1279 likely however - that represents a duration of
1280 less than 1msec at typical sample rates.
1283 /* NOTE: 698 bytes is the size of a BWF header structure *plus* our minimal coding history */
1285 return (statbuf.st_size == 0 || statbuf.st_size == wave_header_size || statbuf.st_size == 698);
1289 FileSource::mark_streaming_write_completed ()
1291 Glib::Mutex::Lock lm (_lock);
1293 next_peak_clear_should_notify = true;
1295 if (_peaks_built || pending_peak_builds.empty()) {
1296 _peaks_built = true;
1297 PeaksReady (); /* EMIT SIGNAL */
1302 FileSource::mark_take (string id)
1308 FileSource::move_to_trash (const string trash_dir_name)
1312 /* don't move the file across filesystems, just
1313 stick it in the `trash_dir_name' directory
1314 on whichever filesystem it was already on.
1319 newpath = Glib::path_get_dirname (_path);
1320 newpath = Glib::path_get_dirname (newpath);
1323 newpath += trash_dir_name;
1325 newpath += Glib::path_get_basename (_path);
1327 if (access (newpath.c_str(), F_OK) == 0) {
1329 /* the new path already exists, try versioning */
1331 char buf[PATH_MAX+1];
1335 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
1338 while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
1339 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
1343 if (version == 999) {
1344 error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
1348 newpath = newpath_v;
1353 /* it doesn't exist, or we can't read it or something */
1357 if (::rename (_path.c_str(), newpath.c_str()) != 0) {
1358 error << string_compose (_("cannot rename audio file source from %1 to %2 (%3)"),
1359 _path, newpath, strerror (errno))
1364 if (::unlink (peakpath.c_str()) != 0) {
1365 error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
1366 peakpath, _path, strerror (errno))
1368 /* try to back out */
1369 rename (newpath.c_str(), _path.c_str());
1375 remove_at_unref = false;
1381 prepare_string(string& str)
1385 for (uint32_t i = 0; i < str.size(); ++i){
1387 if (isdigit(c) || isalpha(c)){
1399 FileSource::repair (string path, jack_nframes_t rate)
1404 struct stat statbuf;
1407 bool bigend = false;
1408 bool doswap = false;
1410 if (stat (path.c_str(), &statbuf)) {
1414 if (statbuf.st_size <= (off_t) sizeof (buf)) {
1415 /* nothing was ever written to the file, so there is nothing
1421 if ((in = fopen (path.c_str(), "r+")) == NULL) {
1425 if (fread (buf, sizeof (buf), 1, in) != 1) {
1429 if ((memcmp (&buf[0], "RIFF", 4) && memcmp (&buf[0], "RIFX", 4)) || memcmp (&buf[8], "WAVE", 4)) {
1430 /* no header. too dangerous to proceed */
1434 if (memcmp (&buf[0], "RIFX", 4)==0) {
1438 doswap = bigend != WE_ARE_BIGENDIAN;
1440 /* reset the size of the RIFF chunk header */
1443 *((int32_t *)&buf[4]) = Swap_32((int32_t)(statbuf.st_size - 8));
1446 *((int32_t *)&buf[4]) = statbuf.st_size - 8;
1449 /* find data chunk and reset the size */
1453 for (i = 0; i < sizeof (buf); ) {
1455 if (memcmp (ptr, "fmt ", 4) == 0) {
1459 memcpy (&fmt, ptr, sizeof (fmt));
1464 fmt.nSamplesPerSec = rate;
1465 fmt.nAvgBytesPerSec = rate * 4;
1472 memcpy (ptr, &fmt, sizeof (fmt));
1473 ptr += sizeof (fmt);
1476 } else if (memcmp (ptr, "data", 4) == 0) {
1477 GenericChunk dchunk;
1478 memcpy(&dchunk, ptr, sizeof(dchunk));
1481 swap_endian(dchunk);
1484 dchunk.size = statbuf.st_size - i - 8;
1487 swap_endian(dchunk);
1489 memcpy (ptr, &dchunk, sizeof (dchunk));
1498 /* now flush it back to disk */
1502 if (fwrite (buf, sizeof (buf), 1, in) != 1) {