+jack_nframes_t
+SndFileSource::write_unlocked (Sample *data, jack_nframes_t cnt, char * workbuf)
+{
+ if (!writable()) {
+ return 0;
+ }
+
+ if (_info.channels != 1) {
+ fatal << string_compose (_("programming error: %1 %2"), X_("SndFileSource::write called on non-mono file"), _path) << endmsg;
+ /*NOTREACHED*/
+ return 0;
+ }
+
+ jack_nframes_t oldlen;
+ int32_t frame_pos = _length;
+
+ if (write_float (data, frame_pos, cnt) != cnt) {
+ return 0;
+ }
+
+ oldlen = _length;
+ update_length (oldlen, cnt);
+
+ if (_build_peakfiles) {
+ PeakBuildRecord *pbr = 0;
+
+ if (pending_peak_builds.size()) {
+ pbr = pending_peak_builds.back();
+ }
+
+ if (pbr && pbr->frame + pbr->cnt == oldlen) {
+
+ /* the last PBR extended to the start of the current write,
+ so just extend it again.
+ */
+
+ pbr->cnt += cnt;
+ } else {
+ pending_peak_builds.push_back (new PeakBuildRecord (oldlen, cnt));
+ }
+
+ _peaks_built = false;
+ }
+
+
+ if (_build_peakfiles) {
+ queue_for_peaks (*this);
+ }
+
+ _write_data_count = cnt;
+
+ return cnt;
+}
+
+int
+SndFileSource::update_header (jack_nframes_t when, struct tm& now, time_t tnow)
+{
+ set_timeline_position (when);
+
+ if (_flags & Broadcast) {
+ if (setup_broadcast_info (when, now, tnow)) {
+ return -1;
+ }
+ }
+
+ return flush_header ();
+}
+
+int
+SndFileSource::flush_header ()
+{
+ if (!writable() || (sf == 0)) {
+ return -1;
+ }
+
+ return (sf_command (sf, SFC_UPDATE_HEADER_NOW, 0, 0) != SF_TRUE);
+}
+
+int
+SndFileSource::setup_broadcast_info (jack_nframes_t when, struct tm& now, time_t tnow)
+{
+ if (!writable()) {
+ return -1;
+ }
+
+ if (!(_flags & Broadcast)) {
+ return 0;
+ }
+
+ /* random code is 9 digits */
+
+ int random_code = random() % 999999999;
+
+ snprintf (_broadcast_info->originator_reference, sizeof (_broadcast_info->originator_reference), "%2s%3s%12s%02d%02d%02d%9d",
+ bwf_country_code,
+ bwf_organization_code,
+ bwf_serial_number,
+ now.tm_hour,
+ now.tm_min,
+ now.tm_sec,
+ random_code);
+
+ snprintf (_broadcast_info->origination_date, sizeof (_broadcast_info->origination_date), "%4d-%02d-%02d",
+ 1900 + now.tm_year,
+ now.tm_mon,
+ now.tm_mday);
+
+ snprintf (_broadcast_info->origination_time, sizeof (_broadcast_info->origination_time), "%02d:%02d:%02d",
+ now.tm_hour,
+ now.tm_min,
+ now.tm_sec);
+
+ /* now update header position taking header offset into account */
+
+ set_header_timeline_position ();
+
+ if (sf_command (sf, SFC_SET_BROADCAST_INFO, _broadcast_info, sizeof (*_broadcast_info)) != SF_TRUE) {
+ error << string_compose (_("cannot set broadcast info for audio file %1; Dropping broadcast info for this file"), _path) << endmsg;
+ _flags = Flag (_flags & ~Broadcast);
+ free (_broadcast_info);
+ _broadcast_info = 0;
+ return -1;
+ }
+
+ return 0;
+}
+
+void
+SndFileSource::set_header_timeline_position ()
+{
+ if (!(_flags & Broadcast)) {
+ return;
+ }
+
+ _broadcast_info->time_reference_high = (timeline_position >> 32);
+ _broadcast_info->time_reference_low = (timeline_position & 0xffffffff);
+
+ if (sf_command (sf, SFC_SET_BROADCAST_INFO, _broadcast_info, sizeof (*_broadcast_info)) != SF_TRUE) {
+ error << string_compose (_("cannot set broadcast info for audio file %1; Dropping broadcast info for this file"), _path) << endmsg;
+ _flags = Flag (_flags & ~Broadcast);
+ free (_broadcast_info);
+ _broadcast_info = 0;
+ }
+}
+
+jack_nframes_t
+SndFileSource::write_float (Sample* data, jack_nframes_t frame_pos, jack_nframes_t cnt)
+{
+ if (sf_seek (sf, frame_pos, SEEK_SET|SFM_WRITE) != frame_pos) {
+ error << string_compose (_("%1: cannot seek to %2"), _path, frame_pos) << endmsg;
+ return 0;
+ }
+
+ if (sf_writef_float (sf, data, cnt) != (ssize_t) cnt) {
+ return 0;
+ }
+
+ return cnt;
+}
+
+jack_nframes_t
+SndFileSource::natural_position() const
+{
+ return timeline_position;
+}