1 /*
2  * Copyright (C) 2008 Sakari Bergen <sakari.bergen@beatwaves.net>
3  * Copyright (C) 2012 Paul Davis <paul@linuxaudiosystems.com>
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 along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #include "audiographer/broadcast_info.h"
21 #include "audiographer/sndfile/sndfile_base.h"
22 #include <iostream>
23 #include <sstream>
24 #include <iomanip>
25 #include <cstdarg>
26 #include <cstring>
27 #include <inttypes.h>
28 #include <cstdlib>
29 
30 namespace AudioGrapher
31 {
32 
33 static void
snprintf_bounded_null_filled(char * target,size_t target_size,char const * fmt,...)34 snprintf_bounded_null_filled (char* target, size_t target_size, char const * fmt, ...)
35 {
36 	char *buf = (char*)alloca(target_size+1);
37 	va_list ap;
38 
39 	va_start (ap, fmt);
40 	vsnprintf (buf, target_size+1, fmt, ap);
41 	va_end (ap);
42 
43 	memset (target, 0, target_size);
44 	memcpy (target, buf, target_size);
45 
46 }
47 
BroadcastInfo()48 BroadcastInfo::BroadcastInfo ()
49 	: _has_info (false)
50 {
51 	info = new SF_BROADCAST_INFO;
52 	memset (info, 0, sizeof (*info));
53 
54 	// Note: Set version to 1 when UMID is used, otherwise version should stay at 0
55 	info->version = 0;
56 
57 	time_t rawtime;
58 	std::time (&rawtime);
59 	_time = *localtime (&rawtime);
60 }
61 
~BroadcastInfo()62 BroadcastInfo::~BroadcastInfo ()
63 {
64 	delete info;
65 }
66 
67 bool
load_from_file(std::string const & filename)68 BroadcastInfo::load_from_file (std::string const & filename)
69 {
70 	SNDFILE * file = 0;
71 	SF_INFO info;
72 
73 	info.format = 0;
74 
75 	if (!(file = sf_open (filename.c_str(), SFM_READ, &info))) {
76 		update_error();
77 		return false;
78 	}
79 
80 	bool ret = load_from_file (file);
81 
82 	sf_close (file);
83 	return ret;
84 }
85 
86 bool
load_from_file(SNDFILE * sf)87 BroadcastInfo::load_from_file (SNDFILE* sf)
88 {
89 	if (sf_command (sf, SFC_GET_BROADCAST_INFO, info, sizeof (*info)) != SF_TRUE) {
90 		update_error();
91 		_has_info = false;
92 		return false;
93 	}
94 
95 	_has_info = true;
96 	return true;
97 }
98 
99 std::string
get_description() const100 BroadcastInfo::get_description () const
101 {
102 	return info->description;
103 }
104 
105 int64_t
get_time_reference() const106 BroadcastInfo::get_time_reference () const
107 {
108 	if (!_has_info) {
109 		return 0;
110 	}
111 
112 	if (info->time_reference_high & 0x80000000) {
113 		return 0;
114 	}
115 
116 	int64_t ret = (uint32_t) (info->time_reference_high & 0x7fffffff);
117 	ret <<= 32;
118 	ret |= (uint32_t) (info->time_reference_low & 0xffffffff);
119 	return ret;
120 }
121 
122 struct tm
get_origination_time() const123 BroadcastInfo::get_origination_time () const
124 {
125 	struct tm ret;
126 
127 	std::string date = info->origination_date;
128 	ret.tm_year = atoi (date.substr (0, 4).c_str()) - 1900;
129 	ret.tm_mon = atoi (date.substr (5, 2).c_str());
130 	ret.tm_mday = atoi (date.substr (8, 2).c_str());
131 
132 	std::string time = info->origination_time;
133 	ret.tm_hour = atoi (time.substr (0,2).c_str());
134 	ret.tm_min = atoi (time.substr (3,2).c_str());
135 	ret.tm_sec = atoi (time.substr (6,2).c_str());
136 
137 	return ret;
138 }
139 
140 std::string
get_originator() const141 BroadcastInfo::get_originator () const
142 {
143 	return info->originator;
144 }
145 
146 std::string
get_originator_ref() const147 BroadcastInfo::get_originator_ref () const
148 {
149 	return info->originator_reference;
150 }
151 
152 bool
write_to_file(std::string const & filename)153 BroadcastInfo::write_to_file (std::string const & filename)
154 {
155 	SNDFILE * file = 0;
156 	SF_INFO info;
157 
158 	info.format = 0;
159 
160 	if (!(file = sf_open (filename.c_str(), SFM_RDWR, &info))) {
161 		update_error();
162 		return false;
163 	}
164 
165 	bool ret = write_to_file (file);
166 
167 	sf_close (file);
168 	return ret;
169 }
170 
171 bool
write_to_file(SNDFILE * sf)172 BroadcastInfo::write_to_file (SNDFILE* sf)
173 {
174 	if (sf_command (sf, SFC_SET_BROADCAST_INFO, info, sizeof (*info)) != SF_TRUE) {
175 		update_error();
176 		return false;
177 	}
178 
179 	return true;
180 }
181 
182 bool
write_to_file(SndfileHandle * sf)183 BroadcastInfo::write_to_file (SndfileHandle* sf)
184 {
185 	if (sf->command (SFC_SET_BROADCAST_INFO, info, sizeof (*info)) != SF_TRUE) {
186 		update_error ();
187 		return false;
188 	}
189 
190 	return true;
191 }
192 
193 void
set_description(std::string const & desc)194 BroadcastInfo::set_description (std::string const & desc)
195 {
196 	_has_info = true;
197 
198 	snprintf_bounded_null_filled (info->description, sizeof (info->description), desc.c_str());
199 }
200 
201 void
set_time_reference(int64_t when)202 BroadcastInfo::set_time_reference (int64_t when)
203 {
204 	_has_info = true;
205 
206 	info->time_reference_high = (when >> 32);
207 	info->time_reference_low = (when & 0xffffffff);
208 }
209 
210 void
set_origination_time(struct tm * now)211 BroadcastInfo::set_origination_time (struct tm * now)
212 {
213 	_has_info = true;
214 
215 	if (now) {
216 		_time = *now;
217 	}
218 
219 	snprintf_bounded_null_filled (info->origination_date, sizeof (info->origination_date), "%4d-%02d-%02d",
220 		  _time.tm_year + 1900,
221 		  _time.tm_mon + 1,
222 		  _time.tm_mday);
223 
224 	snprintf_bounded_null_filled (info->origination_time, sizeof (info->origination_time), "%02d:%02d:%02d",
225 		  _time.tm_hour,
226 		  _time.tm_min,
227 		  _time.tm_sec);
228 }
229 
230 void
set_originator(std::string const & str)231 BroadcastInfo::set_originator (std::string const & str)
232 {
233 	_has_info = true;
234 
235 	snprintf_bounded_null_filled (info->originator, sizeof (info->originator), str.c_str());
236 }
237 
238 void
set_originator_ref(std::string const & str)239 BroadcastInfo::set_originator_ref (std::string const & str)
240 {
241 	_has_info = true;
242 
243 	snprintf_bounded_null_filled (info->originator_reference, sizeof (info->originator_reference), str.c_str());
244 }
245 
246 void
update_error()247 BroadcastInfo::update_error ()
248 {
249 	char errbuf[256];
250 	sf_error_str (0, errbuf, sizeof (errbuf) - 1);
251 	error = errbuf;
252 }
253 
254 } // namespace AudioGrapher
255 
256