2 Copyright (C) 2015 Robin Gareus <robin@gareus.org>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "pbd/gstdio_compat.h"
29 #include "pbd/error.h"
30 #include "pbd/failed_constructor.h"
32 #include "temporal/time.h"
34 #include "ardour/ltc_file_reader.h"
39 using namespace ARDOUR;
43 #define BUFFER_SIZE 1024 // audio chunk size
45 LTCReader::LTCReader (int expected_apv, LTC_TV_STANDARD tv_standard)
48 _decoder = ltc_decoder_create (expected_apv, 8); // must be able to hold frmes for BUFFER_SIZE
51 LTCReader::~LTCReader ()
53 ltc_decoder_free (_decoder);
57 LTCReader::write (float const* data, samplecnt_t n_samples, samplepos_t pos)
59 ltc_off_t off = _position;
62 _position += n_samples;
65 samplecnt_t remain = n_samples;
67 ltcsnd_sample_t sound[BUFFER_SIZE];
68 int c = std::min (remain, (samplecnt_t)BUFFER_SIZE);
69 for (int i = 0; i < c; ++i) {
70 sound[i] = 128 + (*data++) * 127.0;
72 ltc_decoder_write (_decoder, sound, c, off);
79 LTCReader::raw_write (ltcsnd_sample_t* buf, size_t size, ltc_off_t off)
81 ltc_decoder_write (_decoder, buf, size, off);
85 LTCReader::read (uint32_t& hh, uint32_t& mm, uint32_t& ss, uint32_t& ff)
87 LTCFrameExt ltc_frame;
88 if (0 == ltc_decoder_read (_decoder, <c_frame)) {
93 ltc_frame_to_time (&stime, <c_frame.ltc, /*use_date*/ 0);
98 return ltc_frame.off_start;
102 LTCFileReader::LTCFileReader (std::string path, double expected_fps, LTC_TV_STANDARD tv_standard)
104 , _expected_fps (expected_fps)
105 , _ltc_tv_standard (tv_standard)
108 , _interleaved_audio_buffer (0)
111 memset (&_info, 0, sizeof (_info));
112 assert (Glib::file_test (_path, Glib::FILE_TEST_EXISTS));
115 throw failed_constructor ();
118 const int apv = rintf (_info.samplerate / _expected_fps);
120 #if 0 // TODO allow to auto-detect
121 if (expected_fps == 25.0) {
122 _ltc_tv_standard = LTC_TV_625_50;
124 else if (expected_fps == 30.0) {
125 _ltc_tv_standard = LTC_TV_525_60;
128 _ltc_tv_standard = LTC_TV_FILM_24;
131 _reader = new LTCReader (apv, _ltc_tv_standard);
134 LTCFileReader::~LTCFileReader ()
138 free (_interleaved_audio_buffer);
142 LTCFileReader::open ()
148 #ifdef PLATFORM_WINDOWS
149 int fd = g_open (_path.c_str (), O_RDONLY, 0444);
151 int fd = ::open (_path.c_str (), O_RDONLY, 0444);
154 error << string_compose (_("LTCFileReader: cannot open file \"%1\""), _path) << endmsg;
158 _sndfile = sf_open_fd (fd, SFM_READ, &_info, true);
162 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
163 error << string_compose (_("LTCFileReader: cannot open file \"%1\" (%3)"), _path, errbuf) << endmsg;
166 if (_info.frames == 0 || _info.channels < 1) {
167 error << string_compose (_("LTCFileReader: \"%1\" is an empty audio file"), _path) << endmsg;
170 _interleaved_audio_buffer = (float*) calloc (_info.channels * BUFFER_SIZE, sizeof (float));
175 LTCFileReader::close ()
183 std::vector<LTCFileReader::LTCMap>
184 LTCFileReader::read_ltc (uint32_t channel, uint32_t max_frames)
186 std::vector<LTCFileReader::LTCMap> rv;
187 ltcsnd_sample_t sound[BUFFER_SIZE];
189 const uint32_t channels = _info.channels;
190 if (channel >= channels) {
191 warning << _("LTCFileReader:: invalid audio channel selected") << endmsg;
196 int64_t n = sf_readf_float (_sndfile, _interleaved_audio_buffer, BUFFER_SIZE);
201 /* convert audio to 8bit unsigned */
202 for (int64_t i = 0; i < n; ++i) {
203 sound [i]= 128 + _interleaved_audio_buffer[channels * i + channel] * 127;
206 _reader->raw_write (sound, n, _samples_read);
207 Timecode::Time timecode (_expected_fps);
209 samplepos_t off_start;
211 while ((off_start = _reader->read (timecode.hours, timecode.minutes, timecode.seconds, timecode.frames)) >= 0) {
213 Timecode::timecode_to_sample (
214 timecode, sample, false, false,
218 /* align LTC frame relative to video-frame */
219 off_start += ltc_frame_alignment (
220 _info.samplerate / _expected_fps,
223 /* convert to seconds (session can use session-rate) */
224 double fp_sec = off_start / (double) _info.samplerate;
225 double tc_sec = sample / (double) _info.samplerate;
226 rv.push_back (LTCMap (fp_sec, tc_sec));
233 if (max_frames > 0 && rv.size () >= max_frames) {