2 Copyright (C) 2008 Paul Davis
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 "audiographer/broadcast_info.h"
22 #include "audiographer/sndfile/sndfile_base.h"
31 namespace AudioGrapher
35 snprintf_bounded_null_filled (char* target, size_t target_size, char const * fmt, ...)
37 char buf[target_size+1];
41 vsnprintf (buf, target_size+1, fmt, ap);
44 memset (target, 0, target_size);
45 memcpy (target, buf, target_size);
49 BroadcastInfo::BroadcastInfo ()
52 info = new SF_BROADCAST_INFO;
53 memset (info, 0, sizeof (*info));
55 // Note: Set version to 1 when UMID is used, otherwise version should stay at 0
60 _time = *localtime (&rawtime);
63 BroadcastInfo::~BroadcastInfo ()
69 BroadcastInfo::load_from_file (std::string const & filename)
76 if (!(file = sf_open (filename.c_str(), SFM_READ, &info))) {
81 bool ret = load_from_file (file);
88 BroadcastInfo::load_from_file (SNDFILE* sf)
90 if (sf_command (sf, SFC_GET_BROADCAST_INFO, info, sizeof (*info)) != SF_TRUE) {
101 BroadcastInfo::get_description () const
103 return info->description;
107 BroadcastInfo::get_time_reference () const
113 int64_t ret = (uint32_t) info->time_reference_high;
115 ret |= (uint32_t) info->time_reference_low;
120 BroadcastInfo::get_origination_time () const
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());
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());
138 BroadcastInfo::get_originator () const
140 return info->originator;
144 BroadcastInfo::get_originator_ref () const
146 return info->originator_reference;
150 BroadcastInfo::write_to_file (std::string const & filename)
157 if (!(file = sf_open (filename.c_str(), SFM_RDWR, &info))) {
162 bool ret = write_to_file (file);
169 BroadcastInfo::write_to_file (SNDFILE* sf)
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) {
181 BroadcastInfo::write_to_file (SndfileHandle* sf)
183 if (sf->command (SFC_SET_BROADCAST_INFO, info, sizeof (*info)) != SF_TRUE) {
192 BroadcastInfo::set_description (std::string const & desc)
196 snprintf_bounded_null_filled (info->description, sizeof (info->description), desc.c_str());
200 BroadcastInfo::set_time_reference (int64_t when)
204 info->time_reference_high = (when >> 32);
205 info->time_reference_low = (when & 0xffffffff);
209 BroadcastInfo::set_origination_time (struct tm * now)
217 snprintf_bounded_null_filled (info->origination_date, sizeof (info->origination_date), "%4d-%02d-%02d",
218 _time.tm_year + 1900,
222 snprintf_bounded_null_filled (info->origination_time, sizeof (info->origination_time), "%02d:%02d:%02d",
229 BroadcastInfo::set_originator (std::string const & str)
233 snprintf_bounded_null_filled (info->originator, sizeof (info->originator), str.c_str());
237 BroadcastInfo::set_originator_ref (std::string const & str)
241 snprintf_bounded_null_filled (info->originator_reference, sizeof (info->originator_reference), str.c_str());
245 BroadcastInfo::update_error ()
248 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
252 } // namespace AudioGrapher