more 64bit VM debugging nonsense (svn in the fastest route between my system and...
[ardour.git] / libs / audiographer / src / general / 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 "audiographer/broadcast_info.h"
22 #include "audiographer/sndfile/sndfile_base.h"
23 #include <iostream>
24 #include <sstream>
25 #include <iomanip>
26 #include <cstdarg>
27 #include <cstring>
28 #include <inttypes.h>
29 #include <cstdlib>
30
31 namespace AudioGrapher
32 {
33
34 static void
35 snprintf_bounded_null_filled (char* target, size_t target_size, char const * fmt, ...)
36 {
37         char buf[target_size+1];
38         va_list ap;
39
40         va_start (ap, fmt);
41         vsnprintf (buf, target_size+1, fmt, ap);
42         va_end (ap);
43
44         memset (target, 0, target_size);
45         memcpy (target, buf, target_size);
46
47 }
48
49 BroadcastInfo::BroadcastInfo ()
50         : _has_info (false)
51 {
52         info = new SF_BROADCAST_INFO;
53         memset (info, 0, sizeof (*info));
54
55         // Note: Set version to 1 when UMID is used, otherwise version should stay at 0
56         info->version = 0;
57
58         time_t rawtime;
59         std::time (&rawtime);
60         _time = *localtime (&rawtime);
61 }
62
63 BroadcastInfo::~BroadcastInfo ()
64 {
65         delete info;
66 }
67
68 bool
69 BroadcastInfo::load_from_file (std::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 std::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         std::string date = info->origination_date;
125         ret.tm_year = atoi (date.substr (0, 4).c_str()) - 1900;
126         ret.tm_mon = atoi (date.substr (5, 2).c_str());
127         ret.tm_mday = atoi (date.substr (8, 2).c_str());
128
129         std::string time = info->origination_time;
130         ret.tm_hour = atoi (time.substr (0,2).c_str());
131         ret.tm_min = atoi (time.substr (3,2).c_str());
132         ret.tm_sec = atoi (time.substr (6,2).c_str());
133
134         return ret;
135 }
136
137 std::string
138 BroadcastInfo::get_originator () const
139 {
140         return info->originator;
141 }
142
143 std::string
144 BroadcastInfo::get_originator_ref () const
145 {
146         return info->originator_reference;
147 }
148
149 bool
150 BroadcastInfo::write_to_file (std::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         std::cerr << "AG set BWF as " << sizeof(*info) << std::endl;
172         if (sf_command (sf, SFC_SET_BROADCAST_INFO, info, sizeof (*info)) != SF_TRUE) {
173                 update_error();
174                 return false;
175         }
176
177         return true;
178 }
179
180 bool
181 BroadcastInfo::write_to_file (SndfileHandle* sf)
182 {
183         if (sf->command (SFC_SET_BROADCAST_INFO, info, sizeof (*info)) != SF_TRUE) {
184                 update_error ();
185                 return false;
186         }
187
188         return true;
189 }
190
191 void
192 BroadcastInfo::set_description (std::string const & desc)
193 {
194         _has_info = true;
195
196         snprintf_bounded_null_filled (info->description, sizeof (info->description), desc.c_str());
197 }
198
199 void
200 BroadcastInfo::set_time_reference (int64_t when)
201 {
202         _has_info = true;
203
204         info->time_reference_high = (when >> 32);
205         info->time_reference_low = (when & 0xffffffff);
206 }
207
208 void
209 BroadcastInfo::set_origination_time (struct tm * now)
210 {
211         _has_info = true;
212
213         if (now) {
214                 _time = *now;
215         }
216
217         snprintf_bounded_null_filled (info->origination_date, sizeof (info->origination_date), "%4d-%02d-%02d",
218                   _time.tm_year + 1900,
219                   _time.tm_mon + 1,
220                   _time.tm_mday);
221
222         snprintf_bounded_null_filled (info->origination_time, sizeof (info->origination_time), "%02d:%02d:%02d",
223                   _time.tm_hour,
224                   _time.tm_min,
225                   _time.tm_sec);
226 }
227
228 void
229 BroadcastInfo::set_originator (std::string const & str)
230 {
231         _has_info = true;
232
233         snprintf_bounded_null_filled (info->originator, sizeof (info->originator), str.c_str());
234 }
235
236 void
237 BroadcastInfo::set_originator_ref (std::string const & str)
238 {
239         _has_info = true;
240
241         snprintf_bounded_null_filled (info->originator_reference, sizeof (info->originator_reference), str.c_str());
242 }
243
244 void
245 BroadcastInfo::update_error ()
246 {
247         char errbuf[256];
248         sf_error_str (0, errbuf, sizeof (errbuf) - 1);
249         error = errbuf;
250 }
251
252 } // namespace AudioGrapher
253