new files from sakari, missed last time
[ardour.git] / libs / ardour / broadcast_info.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
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.
9
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.
14
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.
18
19 */
20
21 #include <ardour/broadcast_info.h>
22
23 #include <sstream>
24 #include <iomanip>
25
26 #include <glibmm.h>
27
28 #include <ardour/svn_revision.h>
29 #include <ardour/ardour.h>
30 #include <ardour/session.h>
31
32 #include <pbd/convert.h>
33
34 using namespace PBD;
35
36 namespace ARDOUR
37 {
38
39 BroadcastInfo::BroadcastInfo () :
40   _has_info (false)
41 {
42         info = new SF_BROADCAST_INFO;
43         memset (info, 0, sizeof (*info));
44         
45         // Note: Set version to 1 when UMID is used, otherwise version should stay at 0
46         info->version = 0;
47         
48         time_t rawtime;
49         std::time (&rawtime);
50         _time = *localtime (&rawtime);
51 }
52
53 BroadcastInfo::~BroadcastInfo ()
54 {
55         delete info;
56 }
57
58 void
59 BroadcastInfo::set_from_session (Session const & session, int64_t time)
60 {
61         set_description (session.name());
62         set_time_reference (time);
63         set_origination_time ();
64         set_originator ();
65         set_originator_ref ();
66 }
67
68 bool
69 BroadcastInfo::load_from_file (string const & filename)
70 {
71         SNDFILE * file = 0;
72         SF_INFO info;
73         
74         info.format = 0;
75         
76         if (!(file = sf_open (filename.c_str(), SFM_READ, &info))) {
77                 update_error();
78                 return false;
79         }
80         
81         bool ret = load_from_file (file);
82         
83         sf_close (file);
84         return ret;
85 }
86
87 bool
88 BroadcastInfo::load_from_file (SNDFILE* sf)
89 {
90         if (sf_command (sf, SFC_GET_BROADCAST_INFO, info, sizeof (*info)) != SF_TRUE) {
91                 update_error();
92                 _has_info = false;
93                 return false;
94         }
95         
96         _has_info = true;
97         return true;
98 }
99
100 string
101 BroadcastInfo::get_description () const
102 {
103         return info->description;
104 }
105
106 int64_t
107 BroadcastInfo::get_time_reference () const
108 {
109         if (!_has_info) {
110                 return 0;
111         }
112         
113         int64_t ret = (uint32_t) info->time_reference_high;
114         ret <<= 32;
115         ret |= (uint32_t) info->time_reference_low;
116         return ret;
117 }
118
119 struct tm
120 BroadcastInfo::get_origination_time () const
121 {
122         struct tm ret;
123         
124         string date = info->origination_date;
125         ret.tm_year = atoi (date.substr (0, 4)) - 1900;
126         ret.tm_mon = atoi (date.substr (5, 2));
127         ret.tm_mday = atoi (date.substr (8, 2));
128         
129         string time = info->origination_time;
130         ret.tm_hour = atoi (time.substr (0,2));
131         ret.tm_min = atoi (time.substr (3,2));
132         ret.tm_sec = atoi (time.substr (6,2));
133         
134         return ret;
135 }
136
137 string
138 BroadcastInfo::get_originator () const
139 {
140         return info->originator;
141 }
142
143 string
144 BroadcastInfo::get_originator_ref () const
145 {
146         return info->originator_reference;
147 }
148
149 bool
150 BroadcastInfo::write_to_file (string const & filename)
151 {
152         SNDFILE * file = 0;
153         SF_INFO info;
154         
155         info.format = 0;
156         
157         if (!(file = sf_open (filename.c_str(), SFM_RDWR, &info))) {
158                 update_error();
159                 return false;
160         }
161         
162         bool ret = write_to_file (file);
163         
164         sf_close (file);
165         return ret;
166 }
167
168 bool
169 BroadcastInfo::write_to_file (SNDFILE* sf)
170 {
171         if (sf_command (sf, SFC_SET_BROADCAST_INFO, info, sizeof (*info)) != SF_TRUE) {
172                 update_error();
173                 return false;
174         }
175         
176         return true;
177 }
178
179 void
180 BroadcastInfo::set_description (string const & desc)
181 {
182         _has_info = true;
183         
184         snprintf (info->description, sizeof (info->description), desc.c_str());
185 }
186
187 void
188 BroadcastInfo::set_time_reference (int64_t when)
189 {
190         _has_info = true;
191         
192         info->time_reference_high = (when >> 32);
193         info->time_reference_low = (when & 0xffffffff);
194 }
195
196 void
197 BroadcastInfo::set_origination_time (struct tm * now)
198 {
199         _has_info = true;
200         
201         if (now) {
202                 _time = *now;
203         }
204         
205         snprintf (info->origination_date, sizeof (info->origination_date), "%4d-%02d-%02d",
206                   _time.tm_year + 1900,
207                   _time.tm_mon + 1,
208                   _time.tm_mday);
209         
210         snprintf (info->origination_time, sizeof (info->origination_time), "%02d:%02d:%02d",
211                   _time.tm_hour,
212                   _time.tm_min,
213                   _time.tm_sec);
214 }
215
216 void
217 BroadcastInfo::set_originator (string const & str)
218 {
219         _has_info = true;
220         
221         if (!str.empty()) {
222                 snprintf (info->originator, sizeof (info->originator), str.c_str());
223                 return;
224         }
225         
226         snprintf (info->originator, sizeof (info->originator), Glib::get_real_name().c_str());
227 }
228
229 void
230 BroadcastInfo::set_originator_ref (string const & str)
231 {
232         _has_info = true;
233         
234         if (!str.empty()) {
235                 snprintf (info->originator_reference, sizeof (info->originator_reference), str.c_str());
236                 return;
237         }
238         
239         /* random code is 9 digits */
240         
241         int random_code = random() % 999999999;
242         
243         /* Serial number is 12 chars */
244         
245         std::ostringstream serial_number;
246         serial_number << "ARDOUR" << "r" <<  std::setfill('0') << std::right << std::setw(5) << svn_revision;
247         
248         snprintf (info->originator_reference, sizeof (info->originator_reference), "%2s%3s%12s%02d%02d%02d%9d",
249                   Config->get_bwf_country_code().c_str(),
250                   Config->get_bwf_organization_code().c_str(),
251                   serial_number.str().c_str(),
252                   _time.tm_hour,
253                   _time.tm_min,
254                   _time.tm_sec,
255                   random_code);
256         
257 }
258
259 void
260 BroadcastInfo::update_error ()
261 {
262         char errbuf[256];
263         sf_error_str (0, errbuf, sizeof (errbuf) - 1);
264         error = errbuf;
265 }
266
267 } // namespace ARDOUR
268