fix up file renaming code a little bit
[ArdourMidi.git] / libs / ardour / broadcast_info.cc
blobccdb4a331eddb4117a072efc79a3aa4019db6f7f
1 /*
2 Copyright (C) 2008 Paul Davis
3 Author: Sakari Bergen
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "ardour/broadcast_info.h"
22 #include <iostream>
23 #include <sstream>
24 #include <iomanip>
26 #include <glibmm.h>
28 #include "ardour/svn_revision.h"
29 #include "ardour/ardour.h"
30 #include "ardour/session.h"
32 #include "pbd/convert.h"
34 using namespace PBD;
36 namespace ARDOUR
39 static void
40 snprintf_bounded_null_filled (char* target, size_t target_size, char const * fmt, ...)
42 char buf[target_size+1];
43 va_list ap;
45 va_start (ap, fmt);
46 vsnprintf (buf, target_size+1, fmt, ap);
47 va_end (ap);
49 memset (target, 0, target_size);
50 memcpy (target, buf, target_size);
54 BroadcastInfo::BroadcastInfo () :
55 _has_info (false)
57 info = new SF_BROADCAST_INFO;
58 memset (info, 0, sizeof (*info));
60 // Note: Set version to 1 when UMID is used, otherwise version should stay at 0
61 info->version = 0;
63 time_t rawtime;
64 std::time (&rawtime);
65 _time = *localtime (&rawtime);
68 BroadcastInfo::~BroadcastInfo ()
70 delete info;
73 void
74 BroadcastInfo::set_from_session (Session const & session, int64_t time_ref)
76 set_description (session.name());
77 set_time_reference (time_ref);
78 set_origination_time ();
79 set_originator ();
80 set_originator_ref (session);
83 bool
84 BroadcastInfo::load_from_file (std::string const & filename)
86 SNDFILE * file = 0;
87 SF_INFO info;
89 info.format = 0;
91 if (!(file = sf_open (filename.c_str(), SFM_READ, &info))) {
92 update_error();
93 return false;
96 bool ret = load_from_file (file);
98 sf_close (file);
99 return ret;
102 bool
103 BroadcastInfo::load_from_file (SNDFILE* sf)
105 if (sf_command (sf, SFC_GET_BROADCAST_INFO, info, sizeof (*info)) != SF_TRUE) {
106 update_error();
107 _has_info = false;
108 return false;
111 _has_info = true;
112 return true;
115 std::string
116 BroadcastInfo::get_description () const
118 return info->description;
121 int64_t
122 BroadcastInfo::get_time_reference () const
124 if (!_has_info) {
125 return 0;
128 int64_t ret = (uint32_t) info->time_reference_high;
129 ret <<= 32;
130 ret |= (uint32_t) info->time_reference_low;
131 return ret;
134 struct tm
135 BroadcastInfo::get_origination_time () const
137 struct tm ret;
139 std::string date = info->origination_date;
140 ret.tm_year = atoi (date.substr (0, 4)) - 1900;
141 ret.tm_mon = atoi (date.substr (5, 2));
142 ret.tm_mday = atoi (date.substr (8, 2));
144 std::string time = info->origination_time;
145 ret.tm_hour = atoi (time.substr (0,2));
146 ret.tm_min = atoi (time.substr (3,2));
147 ret.tm_sec = atoi (time.substr (6,2));
149 return ret;
152 std::string
153 BroadcastInfo::get_originator () const
155 return info->originator;
158 std::string
159 BroadcastInfo::get_originator_ref () const
161 return info->originator_reference;
164 bool
165 BroadcastInfo::write_to_file (std::string const & filename)
167 SNDFILE * file = 0;
168 SF_INFO info;
170 info.format = 0;
172 if (!(file = sf_open (filename.c_str(), SFM_RDWR, &info))) {
173 update_error();
174 return false;
177 bool ret = write_to_file (file);
179 sf_close (file);
180 return ret;
183 bool
184 BroadcastInfo::write_to_file (SNDFILE* sf)
186 if (sf_command (sf, SFC_SET_BROADCAST_INFO, info, sizeof (*info)) != SF_TRUE) {
187 update_error();
188 return false;
191 return true;
194 void
195 BroadcastInfo::set_description (std::string const & desc)
197 _has_info = true;
199 snprintf_bounded_null_filled (info->description, sizeof (info->description), desc.c_str());
202 void
203 BroadcastInfo::set_time_reference (int64_t when)
205 _has_info = true;
207 info->time_reference_high = (when >> 32);
208 info->time_reference_low = (when & 0xffffffff);
211 void
212 BroadcastInfo::set_origination_time (struct tm * now)
214 _has_info = true;
216 if (now) {
217 _time = *now;
220 snprintf_bounded_null_filled (info->origination_date, sizeof (info->origination_date), "%4d-%02d-%02d",
221 _time.tm_year + 1900,
222 _time.tm_mon + 1,
223 _time.tm_mday);
225 snprintf_bounded_null_filled (info->origination_time, sizeof (info->origination_time), "%02d:%02d:%02d",
226 _time.tm_hour,
227 _time.tm_min,
228 _time.tm_sec);
231 void
232 BroadcastInfo::set_originator (std::string const & str)
234 _has_info = true;
236 if (!str.empty()) {
237 snprintf_bounded_null_filled (info->originator, sizeof (info->originator), str.c_str());
238 return;
241 snprintf_bounded_null_filled (info->originator, sizeof (info->originator), Glib::get_real_name().c_str());
244 void
245 BroadcastInfo::set_originator_ref (Session const & session, std::string const & str)
247 _has_info = true;
249 if (!str.empty()) {
250 snprintf_bounded_null_filled (info->originator_reference, sizeof (info->originator_reference), str.c_str());
251 return;
254 /* random code is 9 digits */
256 int random_code = random() % 999999999;
258 /* Serial number is 12 chars */
260 std::ostringstream serial_number;
261 serial_number << "ARDOUR" << "r" << std::setfill('0') << std::right << std::setw(5) << svn_revision;
263 snprintf_bounded_null_filled (info->originator_reference, sizeof (info->originator_reference), "%2s%3s%12s%02d%02d%02d%9d",
264 session.config.get_bwf_country_code().c_str(),
265 session.config.get_bwf_organization_code().c_str(),
266 serial_number.str().c_str(),
267 _time.tm_hour,
268 _time.tm_min,
269 _time.tm_sec,
270 random_code);
274 void
275 BroadcastInfo::update_error ()
277 char errbuf[256];
278 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
279 error = errbuf;
282 } // namespace ARDOUR